lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
affineTransform.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 
30 
31 namespace py = pybind11;
32 using namespace pybind11::literals;
33 
34 namespace lsst {
35 namespace afw {
36 namespace geom {
37 namespace {
38 
39 using PyAffineTransform = py::class_<AffineTransform, std::shared_ptr<AffineTransform>>;
40 
41 PYBIND11_PLUGIN(affineTransform) {
42  py::module mod("affineTransform");
43 
44  py::module::import("lsst.afw.geom.linearTransform");
45  py::module::import("lsst.afw.geom.coordinates");
46 
47  // Need to import numpy for ndarray and eigen conversions
48  if (_import_array() < 0) {
49  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
50  return nullptr;
51  }
52 
53  PyAffineTransform cls(mod, "AffineTransform");
54 
55  // Parameters enum is really only used as integer constants.
56  cls.attr("XX") = py::cast(int(AffineTransform::Parameters::XX));
57  cls.attr("YX") = py::cast(int(AffineTransform::Parameters::YX));
58  cls.attr("XY") = py::cast(int(AffineTransform::Parameters::XY));
59  cls.attr("YY") = py::cast(int(AffineTransform::Parameters::YY));
60  cls.attr("X") = py::cast(int(AffineTransform::Parameters::X));
61  cls.attr("Y") = py::cast(int(AffineTransform::Parameters::Y));
62 
63  /* Constructors */
64  cls.def(py::init<>());
65  cls.def(py::init<Eigen::Matrix3d const &>(), "matrix"_a);
66  cls.def(py::init<Eigen::Matrix2d const &>(), "linear"_a);
67  cls.def(py::init<Eigen::Vector2d const &>(), "translation"_a);
68  cls.def(py::init<Eigen::Matrix2d const &, Eigen::Vector2d const &>(), "linear"_a, "translation"_a);
69  cls.def(py::init<LinearTransform const &>(), "linear"_a);
70  cls.def(py::init<Extent2D const &>(), "translation"_a);
71  cls.def(py::init<LinearTransform const &, Extent2D const &>(), "linear"_a, "translation"_a);
72 
73  /* Operators and special methods */
74  cls.def("__mul__", &AffineTransform::operator*, py::is_operator());
75  cls.def("__call__", (Point2D (AffineTransform::*)(Point2D const &) const) & AffineTransform::operator());
76  cls.def("__call__",
77  (Extent2D (AffineTransform::*)(Extent2D const &) const) & AffineTransform::operator());
78  cls.def("__setitem__", [](AffineTransform &self, int i, double value) {
79  if (i < 0 || i > 5) {
80  PyErr_Format(PyExc_IndexError, "Invalid index for AffineTransform: %d", i);
81  throw py::error_already_set();
82  }
83  self[i] = value;
84  });
85  cls.def("__getitem__", [](AffineTransform const &self, int row, int col) {
86  if (row < 0 || row > 2 || col < 0 || col > 2) {
87  PyErr_Format(PyExc_IndexError, "Invalid index for AffineTransform: %d, %d", row, col);
88  throw py::error_already_set();
89  }
90  return (self.getMatrix())(row, col);
91  });
92  cls.def("__getitem__", [](AffineTransform const &self, int i) {
93  if (i < 0 || i > 5) {
94  PyErr_Format(PyExc_IndexError, "Invalid index for AffineTransform: %d", i);
95  throw py::error_already_set();
96  }
97  return self[i];
98  });
99  cls.def("__str__", [](AffineTransform const &self) { return py::str(py::cast(self.getMatrix())); });
100  cls.def("__repr__", [](AffineTransform const &self) {
101  return py::str("AffineTransform(\n{}\n)").format(py::cast(self.getMatrix()));
102  });
103  cls.def("__reduce__", [cls](AffineTransform const &self) {
104  return py::make_tuple(cls, py::make_tuple(py::cast(self.getMatrix())));
105  });
106 
107  /* Members */
108  cls.def("invert", &AffineTransform::invert);
109  cls.def("isIdentity", &AffineTransform::isIdentity);
110  cls.def("getTranslation", (Extent2D & (AffineTransform::*)()) & AffineTransform::getTranslation);
111  cls.def("getLinear", (LinearTransform & (AffineTransform::*)()) & AffineTransform::getLinear);
112  cls.def("getMatrix", &AffineTransform::getMatrix);
113  cls.def("getParameterVector", &AffineTransform::getParameterVector);
114  cls.def("setParameterVector", &AffineTransform::setParameterVector);
115  cls.def_static("makeScaling", (AffineTransform(*)(double)) & AffineTransform::makeScaling);
116  cls.def_static("makeScaling", (AffineTransform(*)(double, double)) & AffineTransform::makeScaling);
117  cls.def_static("makeRotation", &AffineTransform::makeRotation, "angle"_a);
118  cls.def_static("makeTranslation", &AffineTransform::makeTranslation, "translation"_a);
119 
120  /* Non-members */
121  mod.def("makeAffineTransformFromTriple", makeAffineTransformFromTriple);
122 
123  return mod.ptr();
124 }
125 }
126 }
127 }
128 } // namespace lsst::afw::geom::<anonymous>
Extent< double, 2 > Extent2D
Definition: Extent.h:344
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
Point< double, 2 > Point2D
Definition: Point.h:273
double const _a
Definition: RandomImage.cc:77
AffineTransform makeAffineTransformFromTriple(Point2D const &p1, Point2D const &p2, Point2D const &p3, Point2D const &q1, Point2D const &q2, Point2D const &q3)