lsst.afw  14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
coordinates.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2017 LSST/AURA.
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 "numpy/arrayobject.h"
25 #include "ndarray/pybind11.h"
26 
27 #include "lsst/afw/geom/Point.h"
28 #include "lsst/afw/geom/Extent.h"
31 #include "lsst/utils/python.h"
32 
33 namespace py = pybind11;
34 using namespace pybind11::literals;
35 
36 namespace lsst {
37 namespace afw {
38 namespace geom {
39 namespace {
40 
41 template <typename Derived, typename T, int N>
42 using PyCoordinateBase = py::class_<CoordinateBase<Derived, T, N>>;
43 
44 template <int N>
45 using PyCoordinateExpr = py::class_<CoordinateExpr<N>, CoordinateBase<CoordinateExpr<N>, bool, N>>;
46 
47 template <typename T, int N>
48 using PyExtentBase = py::class_<ExtentBase<T, N>, CoordinateBase<Extent<T, N>, T, N>>;
49 
50 template <typename T, int N>
51 using PyExtent = py::class_<Extent<T, N>, ExtentBase<T, N>>;
52 
53 template <typename T, int N>
54 using PyPointBase = py::class_<PointBase<T, N>, CoordinateBase<Point<T, N>, T, N>>;
55 
56 template <typename T, int N>
57 using PyPoint = py::class_<Point<T, N>, PointBase<T, N>>;
58 
59 template <typename Derived, typename T, int N>
60 void declareCoordinateBase(py::module &mod, std::string const &suffix) {
61  static std::string const name = "CoordinateBase" + suffix;
62  PyCoordinateBase<Derived, T, N> cls(mod, name.c_str());
63 
64  /* Operators */
65  cls.def("__getitem__", [](CoordinateBase<Derived, T, N> &self, int i) -> T {
66  return self[utils::python::cppIndex(N, i)];
67  });
68  cls.def("__setitem__", [](CoordinateBase<Derived, T, N> &self, int i, T value) {
69  self[utils::python::cppIndex(N, i)] = value;
70  });
71  cls.def("__len__", [](CoordinateBase<Derived, T, N> &c) -> int { return N; });
72 }
73 
74 template <int N>
75 void declareCoordinateExpr(py::module &mod, std::string const &suffix) {
76  static std::string const name = "CoordinateExpr" + suffix;
77  declareCoordinateBase<CoordinateExpr<N>, bool, N>(mod, name);
78 
79  PyCoordinateExpr<N> clsCoordinateExpr(mod, name.c_str());
80 
81  /* Constructors */
82  clsCoordinateExpr.def(py::init<bool>(), "val"_a = false);
83 
84  /* Operators */
85  clsCoordinateExpr.def("and_", &CoordinateExpr<N>::and_);
86  clsCoordinateExpr.def("or_", &CoordinateExpr<N>::or_);
87  clsCoordinateExpr.def("not_", &CoordinateExpr<N>::not_);
88 
89  mod.def("all", all<N>);
90  mod.def("any", any<N>);
91 }
92 
93 template <typename T, int N>
94 void declareExtentBase(py::module &mod, std::string const &suffix) {
95  declareCoordinateBase<Extent<T, N>, T, N>(mod, "Extent" + suffix);
96  static std::string const name = "ExtentBase" + suffix;
97 
98  PyExtentBase<T, N> cls(mod, name.c_str());
99 
100  // These are not the usual Python double-underscore operators - they do elementwise comparisons,
101  // returning a CoordinateExpr object with boolean x and y values. NumPy examples to the contrary
102  // notwithstanding, true Python comparison operators are expected to return scalar bools.
103  cls.def("eq", [](ExtentBase<T, N> const &self, Extent<T, N> other) { return self.eq(other); });
104  cls.def("ne", [](ExtentBase<T, N> const &self, Extent<T, N> other) { return self.ne(other); });
105  cls.def("lt", [](ExtentBase<T, N> const &self, Extent<T, N> other) { return self.lt(other); });
106  cls.def("le", [](ExtentBase<T, N> const &self, Extent<T, N> other) { return self.le(other); });
107  cls.def("gt", [](ExtentBase<T, N> const &self, Extent<T, N> other) { return self.gt(other); });
108  cls.def("ge", [](ExtentBase<T, N> const &self, Extent<T, N> other) { return self.ge(other); });
109  cls.def("eq", [](ExtentBase<T, N> const &self, T other) { return self.eq(other); });
110  cls.def("ne", [](ExtentBase<T, N> const &self, T other) { return self.ne(other); });
111  cls.def("lt", [](ExtentBase<T, N> const &self, T other) { return self.lt(other); });
112  cls.def("le", [](ExtentBase<T, N> const &self, T other) { return self.le(other); });
113  cls.def("gt", [](ExtentBase<T, N> const &self, T other) { return self.gt(other); });
114  cls.def("ge", [](ExtentBase<T, N> const &self, T other) { return self.ge(other); });
115 
116  /* Members */
117  cls.def("asPoint", &ExtentBase<T, N>::asPoint);
118  cls.def("computeNorm", &ExtentBase<T, N>::computeNorm);
119  cls.def("computeSquaredNorm", &ExtentBase<T, N>::computeSquaredNorm);
120 }
121 
122 // Common functionality for all Extents, including declaring base classes.
123 template <typename T, int N>
124 PyExtent<T, N> declareExtent(py::module &mod, std::string const &suffix) {
125  declareExtentBase<T, N>(mod, suffix);
126  static std::string const name = "Extent" + suffix;
127 
128  PyExtent<T, N> cls(mod, name.c_str());
129 
130  /* Constructors */
131  cls.def(py::init<T>(), "value"_a = static_cast<T>(0));
132  cls.def(py::init<Point<int, N> const &>());
133  cls.def(py::init<Point<T, N> const &>());
134  cls.def(py::init<Extent<int, N> const &>());
135  cls.def(py::init<Extent<T, N> const &>());
136  cls.def(py::init<typename Extent<T, N>::EigenVector>());
137 
138  /* Operators */
139  cls.def("__neg__", [](Extent<T, N> const &self) { return -self; });
140  cls.def("__pos__", [](Extent<T, N> const &self) { return self; });
141  cls.def("__mul__", [](Extent<T, N> const &self, int other) { return self * other; }, py::is_operator());
142  cls.def("__mul__", [](Extent<T, N> const &self, double other) { return self * other; },
143  py::is_operator());
144  cls.def("__rmul__", [](Extent<T, N> const &self, int other) { return self * other; }, py::is_operator());
145  cls.def("__rmul__", [](Extent<T, N> const &self, double other) { return self * other; },
146  py::is_operator());
147  cls.def("__add__", [](Extent<T, N> const &self, Extent<int, N> const &other) { return self + other; },
148  py::is_operator());
149  cls.def("__add__", [](Extent<T, N> const &self, Extent<double, N> const &other) { return self + other; },
150  py::is_operator());
151  cls.def("__add__",
152  [](Extent<T, N> const &self, Point<int, N> const &other) { return self + Point<T, N>(other); },
153  py::is_operator());
154  cls.def("__add__", [](Extent<T, N> const &self, Point<double, N> const &other) { return self + other; },
155  py::is_operator());
156  cls.def("__sub__",
157  [](Extent<T, N> const &self, Extent<int, N> const &other) { return self - Extent<T, N>(other); },
158  py::is_operator());
159  cls.def("__sub__", [](Extent<T, N> const &self, Extent<double, N> const &other) { return self - other; },
160  py::is_operator());
161  cls.def("__eq__", [](Extent<T, N> const &self, Extent<T, N> const &other) { return self == other; },
162  py::is_operator());
163  cls.def("__ne__", [](Extent<T, N> const &self, Extent<T, N> const &other) { return self != other; },
164  py::is_operator());
165 
166  /* Members */
167  cls.def("clone", [](Extent<T, N> const &self) { return Extent<T, N>{self}; });
168 
169  return cls;
170 }
171 
172 // Add functionality only found in N=2 Extents (and delgate to declareExtent for the rest)
173 template <typename T>
174 PyExtent<T, 2> declareExtent2(py::module &mod, std::string const &suffix) {
175  auto cls = declareExtent<T, 2>(mod, std::string("2") + suffix);
176 
177  /* Members types and enums */
178  cls.def_property_readonly_static("dimensions", [](py::object /* cls */) { return 2; });
179 
180  /* Constructors */
181  cls.def(py::init<int, int>(), "x"_a, "y"_a);
182  cls.def(py::init<double, double>(), "x"_a, "y"_a);
183 
184  /* Members */
185  cls.def("getX", [](Extent<T, 2> const &self) { return self[0]; });
186  cls.def("getY", [](Extent<T, 2> const &self) { return self[1]; });
187  cls.def("setX", [](Extent<T, 2> &self, T other) { self[0] = other; });
188  cls.def("setY", [](Extent<T, 2> &self, T other) { self[1] = other; });
189 
190  return cls;
191 }
192 
193 // Add functionality only found in N=3 Extents (and delgate to declareExtent for the rest)
194 template <typename T>
195 PyExtent<T, 3> declareExtent3(py::module &mod, const std::string &suffix) {
196  auto cls = declareExtent<T, 3>(mod, std::string("3") + suffix);
197 
198  /* Member types and enums */
199  cls.def_property_readonly_static("dimensions", [](py::object /* cls */) { return 3; });
200 
201  /* Constructors */
202  cls.def(py::init<int, int, int>(), "x"_a, "y"_a, "z"_a);
203  cls.def(py::init<double, double, double>(), "x"_a, "y"_a, "z"_a);
204 
205  /* Members */
206  cls.def("getX", [](Extent<T, 3> const &self) { return self[0]; });
207  cls.def("getY", [](Extent<T, 3> const &self) { return self[1]; });
208  cls.def("getZ", [](Extent<T, 3> const &self) { return self[2]; });
209  cls.def("setX", [](Extent<T, 3> &self, T other) { self[0] = other; });
210  cls.def("setY", [](Extent<T, 3> &self, T other) { self[1] = other; });
211  cls.def("setZ", [](Extent<T, 3> &self, T other) { self[2] = other; });
212 
213  return cls;
214 }
215 
216 // Declare mixed-type and type-overloaded operators for Extent with dimension
217 // N for both int and double. Because pybind11 tries operators (like any
218 // overload) `in order', int has to come before double in any overloaded
219 // operators that dispatch on a scalar, and hence they have to be defined here
220 // instead of declareExtent.
221 template <int N>
222 void declareExtentOperators(py::module &mod, PyExtent<int, N> &clsI, PyExtent<double, N> &clsD) {
223  // Python's integer division works differently than C++'s for negative numbers - Python
224  // uses floor (rounds towards more negative), while C++ truncates (rounds towards zero).
225  // Therefore one needs to be careful in the definition of division operators.
226  clsI.def("__floordiv__",
227  [](Extent<int, N> const &self, int other) -> Extent<int, N> {
228  return floor(self / static_cast<double>(other));
229  },
230  py::is_operator());
231 
232  clsI.def("__truediv__", [](Extent<int, N> const &self, double other) { return self / other; },
233  py::is_operator());
234  clsD.def("__truediv__", [](Extent<double, N> const &self, double other) { return self / other; },
235  py::is_operator());
236 
237  clsI.def("__ifloordiv__", [](Extent<int, N> &self, int other) -> Extent<int, N> & {
238  self = floor(self / static_cast<double>(other));
239  return self;
240  });
241 
242  clsI.def("__itruediv__", [](Extent<int, N> &self, double other) {
243  PyErr_SetString(PyExc_TypeError, "In-place true division not supported for Extent<int,N>.");
244  throw py::error_already_set();
245  });
246  clsD.def("__itruediv__", [](Extent<double, N> &self, double other) -> Extent<double, N> & {
247  self /= other;
248  return self;
249  });
250 
251  clsI.def("__iadd__", [](Extent<int, N> &self, Extent<int, N> const &other) -> Extent<int, N> & {
252  self += other;
253  return self;
254  });
255  clsD.def("__iadd__", [](Extent<double, N> &self, Extent<double, N> const &other) -> Extent<double, N> & {
256  self += other;
257  return self;
258  });
259  clsD.def("__iadd__", [](Extent<double, N> &self, Extent<int, N> const &other) -> Extent<double, N> & {
260  self += other;
261  return self;
262  });
263 
264  clsI.def("__isub__", [](Extent<int, N> &self, Extent<int, N> const &other) -> Extent<int, N> & {
265  self -= other;
266  return self;
267  });
268  clsD.def("__isub__", [](Extent<double, N> &self, Extent<double, N> const &other) -> Extent<double, N> & {
269  self -= other;
270  return self;
271  });
272  clsD.def("__isub__", [](Extent<double, N> &self, Extent<int, N> const &other) -> Extent<double, N> & {
273  self -= other;
274  return self;
275  });
276 
277  clsI.def("__imul__", [](Extent<int, N> &self, int other) -> Extent<int, N> & {
278  self *= other;
279  return self;
280  });
281  clsD.def("__imul__", [](Extent<double, N> &self, int other) -> Extent<double, N> & {
282  self *= other;
283  return self;
284  });
285  clsD.def("__imul__", [](Extent<double, N> &self, double other) -> Extent<double, N> & {
286  self *= other;
287  return self;
288  });
289 
290  // Operator-like free functions
291  mod.def("truncate", truncate<N>);
292  mod.def("floor", floor<N>);
293  mod.def("ceil", ceil<N>);
294  // And method versions, since Python doens't have ADL
295  clsD.def("truncate", truncate<N>);
296  clsD.def("floor", floor<N>);
297  clsD.def("ceil", ceil<N>);
298 }
299 
300 template <typename T, int N>
301 void declarePointBase(py::module &mod, std::string const &suffix) {
302  declareCoordinateBase<Point<T, N>, T, N>(mod, "Point" + suffix);
303  static std::string const name = "PointBase" + suffix;
304  PyPointBase<T, N> cls(mod, name.c_str());
305 
306  // These are not the usual Python double-underscore operators - they do elementwise comparisons,
307  // returning a CoordinateExpr object with boolean x and y values. NumPy examples to the contrary
308  // notwithstanding, true Python comparison operators are expected to return scalar bools.
309  cls.def("eq", [](PointBase<T, N> const &self, Point<T, N> const &other) { return self.eq(other); });
310  cls.def("ne", [](PointBase<T, N> const &self, Point<T, N> const &other) { return self.ne(other); });
311  cls.def("lt", [](PointBase<T, N> const &self, Point<T, N> const &other) { return self.lt(other); });
312  cls.def("le", [](PointBase<T, N> const &self, Point<T, N> const &other) { return self.le(other); });
313  cls.def("gt", [](PointBase<T, N> const &self, Point<T, N> const &other) { return self.gt(other); });
314  cls.def("ge", [](PointBase<T, N> const &self, Point<T, N> const &other) { return self.ge(other); });
315  cls.def("eq", [](PointBase<T, N> const &self, T other) { return self.eq(other); });
316  cls.def("ne", [](PointBase<T, N> const &self, T other) { return self.ne(other); });
317  cls.def("lt", [](PointBase<T, N> const &self, T other) { return self.lt(other); });
318  cls.def("le", [](PointBase<T, N> const &self, T other) { return self.le(other); });
319  cls.def("gt", [](PointBase<T, N> const &self, T other) { return self.gt(other); });
320  cls.def("ge", [](PointBase<T, N> const &self, T other) { return self.ge(other); });
321 
322  /* Members */
323  cls.def("asExtent", &PointBase<T, N>::asExtent);
324  cls.def("shift", &PointBase<T, N>::shift);
325  cls.def("scale", &PointBase<T, N>::scale);
326  cls.def("distanceSquared", &PointBase<T, N>::distanceSquared);
327  cls.def("toString", &PointBase<T, N>::toString);
328 }
329 
330 // Common functionality
331 template <typename T, int N>
332 PyPoint<T, N> declarePoint(py::module &mod, std::string const &suffix) {
333  declarePointBase<T, N>(mod, suffix);
334  static std::string const name = "Point" + suffix;
335  PyPoint<T, N> cls(mod, name.c_str());
336 
337  /* Constructors */
338  cls.def(py::init<T>(), "value"_a = static_cast<T>(0));
339  // Note that we can't use T here because both types are needed
340  cls.def(py::init<Point<double, N> const &>());
341  cls.def(py::init<Point<int, N> const &>());
342  cls.def(py::init<Extent<T, N> const &>());
343  cls.def(py::init<typename Point<T, N>::EigenVector>());
344 
345  /* Operators */
346  cls.def("__add__", [](Point<T, N> const &self, Extent<double, N> &other) { return self + other; },
347  py::is_operator());
348  cls.def("__add__", [](Point<T, N> const &self, Extent<int, N> &other) { return self + other; },
349  py::is_operator());
350  cls.def("__sub__", [](Point<T, N> const &self, Point<T, N> &other) { return self - other; },
351  py::is_operator());
352  cls.def("__sub__", [](Point<T, N> const &self, Extent<T, N> &other) { return self - other; },
353  py::is_operator());
354  cls.def("__sub__", [](Point<T, N> const &self, Point<double, N> &other) { return self - other; },
355  py::is_operator());
356  cls.def("__sub__", [](Point<T, N> const &self, Point<int, N> &other) { return self - other; },
357  py::is_operator());
358  cls.def("__sub__", [](Point<T, N> const &self, Extent<double, N> &other) { return self - other; },
359  py::is_operator());
360  cls.def("__sub__", [](Point<T, N> const &self, Extent<int, N> &other) { return self - other; },
361  py::is_operator());
362  cls.def("__eq__", [](Point<T, N> const &self, Point<T, N> const &other) { return self == other; },
363  py::is_operator());
364  cls.def("__ne__", [](Point<T, N> const &self, Point<T, N> const &other) { return self != other; },
365  py::is_operator());
366 
367  /* Members */
368  cls.def("clone", [](Point<T, N> const &self) { return Point<T, N>{self}; });
369 
370  return cls;
371 }
372 
373 // Add functionality only found in N=2 Points
374 template <typename T>
375 PyPoint<T, 2> declarePoint2(py::module &mod, std::string const &suffix) {
376  auto cls = declarePoint<T, 2>(mod, std::string("2") + suffix);
377 
378  /* Member types and enums */
379  cls.def_property_readonly_static("dimensions", [](py::object /* cls */) { return 2; });
380 
381  /* Constructors */
382  cls.def(py::init<int, int>(), "x"_a, "y"_a);
383  cls.def(py::init<double, double>(), "x"_a, "y"_a);
384 
385  /* Members */
386  cls.def("getX", [](Point<T, 2> const &self) { return self[0]; });
387  cls.def("getY", [](Point<T, 2> const &self) { return self[1]; });
388  cls.def("setX", [](Point<T, 2> &self, T other) { self[0] = other; });
389  cls.def("setY", [](Point<T, 2> &self, T other) { self[1] = other; });
390  return cls;
391 }
392 
393 // Add functionality only found in N=3 Points
394 template <typename T>
395 PyPoint<T, 3> declarePoint3(py::module &mod, std::string const &suffix) {
396  auto cls = declarePoint<T, 3>(mod, std::string("3") + suffix);
397 
398  /* Member types and enums */
399  cls.def_property_readonly_static("dimensions", [](py::object /* cls */) { return 3; });
400 
401  /* Constructors */
402  cls.def(py::init<int, int, int>(), "x"_a, "y"_a, "z"_a);
403  cls.def(py::init<double, double, double>(), "x"_a, "y"_a, "z"_a);
404 
405  /* Members */
406  cls.def("getX", [](Point<T, 3> const &self) { return self[0]; });
407  cls.def("getY", [](Point<T, 3> const &self) { return self[1]; });
408  cls.def("getZ", [](Point<T, 3> const &self) { return self[2]; });
409  cls.def("setX", [](Point<T, 3> &self, T other) { self[0] = other; });
410  cls.def("setY", [](Point<T, 3> &self, T other) { self[1] = other; });
411  cls.def("setZ", [](Point<T, 3> &self, T other) { self[2] = other; });
412 
413  return cls;
414 }
415 
416 // Declare mixed-type and type-overloaded operators for Point with dimension
417 // N for both int and double. Because pybind11 tries operators (like any
418 // overload) `in order', int has to come before double in any overloaded
419 // operators that dispatch on a scalar, and hence they have to be defined here
420 // instead of declareExtent.
421 template <int N>
422 void declarePointOperators(py::module &mod, PyPoint<int, N> &clsI, PyPoint<double, N> &clsD) {
423  clsI.def("__iadd__", [](Point<int, N> &self, Extent<int, N> const &other) {
424  self += other;
425  return &self;
426  });
427  clsD.def("__iadd__", [](Point<double, N> &self, Extent<int, N> const &other) {
428  self += other;
429  return &self;
430  });
431  clsD.def("__iadd__", [](Point<double, N> &self, Extent<double, N> const &other) {
432  self += other;
433  return &self;
434  });
435  clsI.def("__isub__", [](Point<int, N> &self, Extent<int, N> const &other) {
436  self -= other;
437  return &self;
438  });
439  clsD.def("__isub__", [](Point<double, N> &self, Extent<int, N> const &other) {
440  self -= other;
441  return &self;
442  });
443  clsD.def("__isub__", [](Point<double, N> &self, Extent<double, N> const &other) {
444  self -= other;
445  return &self;
446  });
447 }
448 
449 PYBIND11_PLUGIN(coordinates) {
450  py::module mod("coordinates");
451 
452  if (_import_array() < 0) {
453  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
454  return nullptr;
455  };
456 
457  // Only the interface-level classes are defined here, and these functions
458  // call others to define their base classes, since those are never shared.
459 
460  declareCoordinateExpr<2>(mod, "2");
461  declareCoordinateExpr<3>(mod, "3");
462 
463  auto clsExtent2I = declareExtent2<int>(mod, "I");
464  auto clsExtent2D = declareExtent2<double>(mod, "D");
465  declareExtentOperators(mod, clsExtent2I, clsExtent2D);
466 
467  auto clsExtent3I = declareExtent3<int>(mod, "I");
468  auto clsExtent3D = declareExtent3<double>(mod, "D");
469  declareExtentOperators(mod, clsExtent3I, clsExtent3D);
470 
471  auto clsPoint2I = declarePoint2<int>(mod, "I");
472  auto clsPoint2D = declarePoint2<double>(mod, "D");
473  declarePointOperators(mod, clsPoint2I, clsPoint2D);
474 
475  auto clsPoint3I = declarePoint3<int>(mod, "I");
476  auto clsPoint3D = declarePoint3<double>(mod, "D");
477  declarePointOperators(mod, clsPoint3I, clsPoint3D);
478 
479  return mod.ptr();
480 }
481 }
482 }
483 }
484 } // namespace lsst::afw::geom::<anonymous>
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
Extent< int, N > floor(Extent< double, N > const &input)
Return the component-wise floor (round towards more negative).
table::Key< std::string > name
Definition: ApCorrMap.cc:71
double const _a
Definition: RandomImage.cc:77