lsst.afw  13.0-49-g6f23dda
 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"
32 #include "lsst/afw/geom/Point.h"
35 
36 namespace py = pybind11;
37 using namespace py::literals;
38 
39 namespace lsst {
40 namespace afw {
41 namespace geom {
42 namespace {
43 
44 // Return a string consisting of "_pythonClassName_[_fromNAxes_->_toNAxes_]",
45 // for example "TransformGenericToPoint2[4->2]"
46 template <class Class>
47 std::string formatStr(Class const &self, std::string const &pyClassName) {
48  std::ostringstream os;
49  os << pyClassName;
50  auto const frameSet = self.getFrameSet();
51  os << "[" << frameSet->getNin() << "->" << frameSet->getNout() << "]";
52  return os.str();
53 }
54 
55 template <class ExtraEndpoint, class FromEndpoint, class ToEndpoint, class PyClass>
56 void declareMethodTemplates(PyClass &cls) {
57  using FirstTransform = Transform<ExtraEndpoint, FromEndpoint>;
58  using SecondTransform = Transform<FromEndpoint, ToEndpoint>;
59  using FinalTransform = Transform<ExtraEndpoint, ToEndpoint>;
60  // Need Python-specific logic to give sensible errors for mismatched Transform types
61  cls.def("_of", (FinalTransform (SecondTransform::*)(FirstTransform const &) const) &
62  SecondTransform::template of<ExtraEndpoint>,
63  "first"_a);
64 }
65 
66 // Declare Transform<FromEndpoint, ToEndpoint> using python class name TransformFrom<X>To<Y>
67 // where <X> and <Y> are the name of the from endpoint and to endpoint class, respectively,
68 // for example TransformFromGenericToPoint2
69 template <class FromEndpoint, class ToEndpoint>
70 void declareTransform(py::module &mod, std::string const &fromName, std::string const &toName) {
71  using Class = Transform<FromEndpoint, ToEndpoint>;
72  using ToPoint = typename ToEndpoint::Point;
73  using ToArray = typename ToEndpoint::Array;
74  using FromPoint = typename FromEndpoint::Point;
75  using FromArray = typename FromEndpoint::Array;
76 
77  std::string const pyClassName = "Transform" + fromName + "To" + toName;
78 
79  py::class_<Class, std::shared_ptr<Class>> cls(mod, pyClassName.c_str());
80 
81  cls.def(py::init<ast::FrameSet const &, bool>(), "frameSet"_a, "simplify"_a = true);
82  cls.def(py::init<ast::Mapping const &, bool>(), "mapping"_a, "simplify"_a = true);
83 
84  cls.def("hasForward", &Class::hasForward);
85  cls.def("hasInverse", &Class::hasInverse);
86 
87  cls.def("getFromEndpoint", &Class::getFromEndpoint);
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  cls.def("getToEndpoint", &Class::getToEndpoint);
92 
93  cls.def("tranForward", (ToArray (Class::*)(FromArray const &) const) & Class::tranForward, "array"_a);
94  cls.def("tranForward", (ToPoint (Class::*)(FromPoint const &) const) & Class::tranForward, "point"_a);
95  cls.def("tranInverse", (FromArray (Class::*)(ToArray const &) const) & Class::tranInverse, "array"_a);
96  cls.def("tranInverse", (FromPoint (Class::*)(ToPoint const &) const) & Class::tranInverse, "point"_a);
97  cls.def("getInverse", &Class::getInverse);
98  /* Need some extra handling of ndarray return type in Python to prevent dimensions
99  * of length 1 from being deleted */
100  cls.def("_getJacobian", &Class::getJacobian);
101 
102  declareMethodTemplates<GenericEndpoint, FromEndpoint, ToEndpoint>(cls);
103  declareMethodTemplates<Point2Endpoint, FromEndpoint, ToEndpoint>(cls);
104  declareMethodTemplates<SpherePointEndpoint, FromEndpoint, ToEndpoint>(cls);
105 
106  // str(self) = "<Python class name>[<nIn>-><nOut>]"
107  cls.def("__str__", [pyClassName](Class const &self) { return formatStr(self, pyClassName); });
108  // repr(self) = "lsst.afw.geom.<Python class name>[<nIn>-><nOut>]"
109  cls.def("__repr__",
110  [pyClassName](Class const &self) { return "lsst.afw.geom." + formatStr(self, pyClassName); });
111 }
112 
113 PYBIND11_PLUGIN(transform) {
114  py::module mod("transform");
115 
116  py::module::import("astshim");
117  py::module::import("lsst.afw.geom.endpoint");
118 
119  // Need to import numpy for ndarray and eigen conversions
120  if (_import_array() < 0) {
121  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
122  return nullptr;
123  }
124 
125  declareTransform<GenericEndpoint, GenericEndpoint>(mod, "Generic", "Generic");
126  declareTransform<GenericEndpoint, Point2Endpoint>(mod, "Generic", "Point2");
127  declareTransform<GenericEndpoint, SpherePointEndpoint>(mod, "Generic", "SpherePoint");
128  declareTransform<Point2Endpoint, GenericEndpoint>(mod, "Point2", "Generic");
129  declareTransform<Point2Endpoint, Point2Endpoint>(mod, "Point2", "Point2");
130  declareTransform<Point2Endpoint, SpherePointEndpoint>(mod, "Point2", "SpherePoint");
131  declareTransform<SpherePointEndpoint, GenericEndpoint>(mod, "SpherePoint", "Generic");
132  declareTransform<SpherePointEndpoint, Point2Endpoint>(mod, "SpherePoint", "Point2");
133  declareTransform<SpherePointEndpoint, SpherePointEndpoint>(mod, "SpherePoint", "SpherePoint");
134 
135  return mod.ptr();
136 }
137 
138 } // <anonymous>
139 } // geom
140 } // afw
141 } // lsst
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
double const _a
Definition: RandomImage.cc:77