lsst.afw  14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
transform.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * See COPYRIGHT file at the top of the source tree.
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 <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 #include "pybind11/pybind11.h"
23 
24 #include <memory>
25 
26 #include "astshim.h"
27 #include "pybind11/stl.h"
28 #include "numpy/arrayobject.h"
29 #include "ndarray/pybind11.h"
30 
31 #include "lsst/afw/geom/Endpoint.h"
33 
34 namespace py = pybind11;
35 using namespace py::literals;
36 
37 namespace lsst {
38 namespace afw {
39 namespace geom {
40 namespace {
41 
42 // Return a string consisting of "_pythonClassName_[_fromNAxes_->_toNAxes_]",
43 // for example "TransformGenericToPoint2[4->2]"
44 template <class Class>
45 std::string formatStr(Class const &self, std::string const &pyClassName) {
46  std::ostringstream os;
47  os << pyClassName;
48  auto const frameSet = self.getFrameSet();
49  os << "[" << frameSet->getNIn() << "->" << frameSet->getNOut() << "]";
50  return os.str();
51 }
52 
53 template <class FromEndpoint, class ToEndpoint, class NextToEndpoint, class PyClass>
54 void declareMethodTemplates(PyClass &cls) {
55  using ThisTransform = Transform<FromEndpoint, ToEndpoint>;
56  using NextTransform = Transform<ToEndpoint, NextToEndpoint>;
57  using SeriesTransform = Transform<FromEndpoint, NextToEndpoint>;
58  // Need Python-specific logic to give sensible errors for mismatched Transform types
59  cls.def("_then",
60  (SeriesTransform(ThisTransform::*)(NextTransform const &) const) &
61  ThisTransform::template then<NextToEndpoint>,
62  "next"_a);
63 }
64 
65 // Declare Transform<FromEndpoint, ToEndpoint> using python class name TransformFrom<X>To<Y>
66 // where <X> and <Y> are the name of the from endpoint and to endpoint class, respectively,
67 // for example TransformFromGenericToPoint2
68 template <class FromEndpoint, class ToEndpoint>
69 void declareTransform(py::module &mod) {
70  using Class = Transform<FromEndpoint, ToEndpoint>;
71  using ToPoint = typename ToEndpoint::Point;
72  using ToArray = typename ToEndpoint::Array;
73  using FromPoint = typename FromEndpoint::Point;
74  using FromArray = typename FromEndpoint::Array;
75 
76  std::string const pyClassName = Class::getShortClassName();
77 
78  py::class_<Class, std::shared_ptr<Class>> cls(mod, pyClassName.c_str());
79 
80  cls.def(py::init<ast::FrameSet const &, bool>(), "frameSet"_a, "simplify"_a = true);
81  cls.def(py::init<ast::Mapping const &, bool>(), "mapping"_a, "simplify"_a = true);
82 
83  cls.def_property_readonly("hasForward", &Class::hasForward);
84  cls.def_property_readonly("hasInverse", &Class::hasInverse);
85  cls.def_property_readonly("fromEndpoint", &Class::getFromEndpoint);
86  cls.def_property_readonly("toEndpoint", &Class::getToEndpoint);
87 
88  // Return a copy of the contained FrameSet in order to assure changing the returned FrameSet
89  // will not affect the contained FrameSet (since Python ignores constness)
90  cls.def("getFrameSet", [](Class const &self) { return self.getFrameSet()->copy(); });
91 
92  cls.def("applyForward", (ToArray(Class::*)(FromArray const &) const) & Class::applyForward, "array"_a);
93  cls.def("applyForward", (ToPoint(Class::*)(FromPoint const &) const) & Class::applyForward, "point"_a);
94  cls.def("applyInverse", (FromArray(Class::*)(ToArray const &) const) & Class::applyInverse, "array"_a);
95  cls.def("applyInverse", (FromPoint(Class::*)(ToPoint const &) const) & Class::applyInverse, "point"_a);
96  cls.def("getInverse", &Class::getInverse);
97  /* Need some extra handling of ndarray return type in Python to prevent dimensions
98  * of length 1 from being deleted */
99  cls.def("_getJacobian", &Class::getJacobian);
100  // Do not wrap getShortClassName because it returns the name of the class;
101  // use `<class>.__name__` or `type(<instance>).__name__` instead.
102  // Do not wrap readStream or writeStream because C++ streams are not easy to wrap.
103  cls.def_static("readString", &Class::readString);
104  cls.def("writeString", &Class::writeString);
105 
106  declareMethodTemplates<FromEndpoint, ToEndpoint, GenericEndpoint>(cls);
107  declareMethodTemplates<FromEndpoint, ToEndpoint, Point2Endpoint>(cls);
108  declareMethodTemplates<FromEndpoint, ToEndpoint, IcrsCoordEndpoint>(cls);
109 
110  // str(self) = "<Python class name>[<nIn>-><nOut>]"
111  cls.def("__str__", [pyClassName](Class const &self) { return formatStr(self, pyClassName); });
112  // repr(self) = "lsst.afw.geom.<Python class name>[<nIn>-><nOut>]"
113  cls.def("__repr__",
114  [pyClassName](Class const &self) { return "lsst.afw.geom." + formatStr(self, pyClassName); });
115 }
116 
117 PYBIND11_PLUGIN(transform) {
118  py::module mod("transform");
119 
120  py::module::import("astshim");
121  py::module::import("lsst.afw.geom.endpoint");
122 
123  // Need to import numpy for ndarray and eigen conversions
124  if (_import_array() < 0) {
125  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
126  return nullptr;
127  }
128 
129  declareTransform<GenericEndpoint, GenericEndpoint>(mod);
130  declareTransform<GenericEndpoint, Point2Endpoint>(mod);
131  declareTransform<GenericEndpoint, IcrsCoordEndpoint>(mod);
132  declareTransform<Point2Endpoint, GenericEndpoint>(mod);
133  declareTransform<Point2Endpoint, Point2Endpoint>(mod);
134  declareTransform<Point2Endpoint, IcrsCoordEndpoint>(mod);
135  declareTransform<IcrsCoordEndpoint, GenericEndpoint>(mod);
136  declareTransform<IcrsCoordEndpoint, Point2Endpoint>(mod);
137  declareTransform<IcrsCoordEndpoint, IcrsCoordEndpoint>(mod);
138 
139  return mod.ptr();
140 }
141 
142 } // namespace
143 } // namespace geom
144 } // namespace afw
145 } // namespace lsst
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
table::Key< std::string > frameSet
double const _a
Definition: RandomImage.cc:77