lsst.afw  13.0-94-ga1c444047
linearTransform.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include "pybind11/pybind11.h"
24 #include "pybind11/stl.h"
25 
26 #include "numpy/arrayobject.h"
27 #include "ndarray/pybind11.h"
28 
29 #include "lsst/utils/python.h"
30 
31 #include "lsst/afw/geom/Extent.h"
32 #include "lsst/afw/geom/Point.h"
34 
35 namespace py = pybind11;
36 using namespace pybind11::literals;
37 
38 namespace lsst {
39 namespace afw {
40 namespace geom {
41 namespace {
42 
43 using PyLinearTransform = py::class_<LinearTransform, std::shared_ptr<LinearTransform>>;
44 
46  py::module mod("linearTransform");
47 
48  py::module::import("lsst.afw.geom.coordinates");
49 
50  if (_import_array() < 0) {
51  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
52  return nullptr;
53  }
54 
55  PyLinearTransform cls(mod, "LinearTransform");
56 
57  // Parameters enum is really only used as integer constants.
58  cls.attr("XX") = py::cast(int(LinearTransform::Parameters::XX));
59  cls.attr("YX") = py::cast(int(LinearTransform::Parameters::YX));
60  cls.attr("XY") = py::cast(int(LinearTransform::Parameters::XY));
61  cls.attr("YY") = py::cast(int(LinearTransform::Parameters::YY));
62 
63  /* Constructors */
64  cls.def(py::init<>());
65  cls.def(py::init<LinearTransform::Matrix const &>(), "matrix"_a);
66 
67  /* Operators */
68  cls.def("__call__", (Point2D (LinearTransform::*)(Point2D const &) const) & LinearTransform::operator());
69  cls.def("__call__",
70  (Extent2D (LinearTransform::*)(Extent2D const &) const) & LinearTransform::operator());
71  cls.def("__getitem__",
72  [](LinearTransform const &self, int i) { return self[lsst::utils::python::cppIndex(4, i)]; });
73  cls.def("__getitem__", [](LinearTransform const &self, std::pair<int, int> i) {
74  auto row = lsst::utils::python::cppIndex(2, i.first);
75  auto col = lsst::utils::python::cppIndex(2, i.second);
76  return self.getMatrix()(row, col);
77  });
78  cls.def("__mul__", &LinearTransform::operator*, py::is_operator());
79  cls.def("__add__", &LinearTransform::operator+, py::is_operator());
80  cls.def("__sub__", &LinearTransform::operator-, py::is_operator());
81  cls.def("__iadd__", &LinearTransform::operator+=);
82  cls.def("__isub__", &LinearTransform::operator-=);
83 
84  /* Members */
85  cls.def_static("makeScaling", (LinearTransform(*)(double))LinearTransform::makeScaling, "scale"_a);
86  cls.def_static("makeScaling", (LinearTransform(*)(double, double))LinearTransform::makeScaling);
87  cls.def_static("makeRotation", (LinearTransform(*)(Angle t))LinearTransform::makeRotation, "angle"_a);
88  cls.def("getParameterVector", &LinearTransform::getParameterVector);
89  cls.def("getMatrix",
90  (LinearTransform::Matrix const &(LinearTransform::*)() const) & LinearTransform::getMatrix);
91  cls.def("invert", &LinearTransform::invert);
92  cls.def("computeDeterminant", &LinearTransform::computeDeterminant);
93  cls.def("isIdentity", &LinearTransform::isIdentity);
94 
95  cls.def("set",
96  [](LinearTransform &self, double xx, double yx, double xy, double yy) {
97  self[LinearTransform::XX] = xx;
98  self[LinearTransform::XY] = xy;
99  self[LinearTransform::YX] = yx;
100  self[LinearTransform::YY] = yy;
101  },
102  "xx"_a, "yx"_a, "xy"_a, "yy"_a);
103 
104  cls.def("__str__", [](LinearTransform const &self) { return py::str(py::cast(self.getMatrix())); });
105  cls.def("__repr__", [](LinearTransform const &self) {
106  return py::str("LinearTransform(\n{}\n)").format(py::cast(self.getMatrix()));
107  });
108  cls.def("__reduce__", [cls](LinearTransform const &self) {
109  return py::make_tuple(cls, py::make_tuple(py::cast(self.getMatrix())));
110  });
111 
112  return mod.ptr();
113 }
114 }
115 }
116 }
117 } // namespace lsst::afw::geom::<anonymous>
lsst::afw::geom::Angle Angle
Definition: misc.h:36
Extent< double, 2 > Extent2D
Definition: Extent.h:344
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
A base class for image defects.
Definition: cameraGeom.dox:3
Point< double, 2 > Point2D
Definition: Point.h:273