LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
_Angle.cc
Go to the documentation of this file.
1 /*
2  * Developed for the LSST Data Management System.
3  * This product includes software developed by the LSST Project
4  * (https://www.lsst.org).
5  * See the COPYRIGHT file at the top-level directory of this distribution
6  * for details of code ownership.
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 GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include "pybind11/pybind11.h"
23 
24 #include "lsst/utils/python.h"
25 
26 #include "lsst/geom/Angle.h"
27 
28 namespace py = pybind11;
29 
30 namespace lsst {
31 namespace geom {
32 
33 using PyAngle = py::class_<Angle>;
34 using PyAngleUnit = py::class_<AngleUnit>;
35 
36 namespace {
37 
38 template <typename OtherT>
39 void declareAngleComparisonOperators(PyAngle& cls) {
40  cls.def("__eq__", [](Angle const& self, OtherT const& other) { return self == other; },
41  py::is_operator());
42  cls.def("__ne__", [](Angle const& self, OtherT const& other) { return self != other; },
43  py::is_operator());
44  cls.def("__le__", [](Angle const& self, OtherT const& other) { return self <= other; },
45  py::is_operator());
46  cls.def("__ge__", [](Angle const& self, OtherT const& other) { return self >= other; },
47  py::is_operator());
48  cls.def("__lt__", [](Angle const& self, OtherT const& other) { return self < other; }, py::is_operator());
49  cls.def("__gt__", [](Angle const& self, OtherT const& other) { return self > other; }, py::is_operator());
50 }
51 
52 } // anonymous
53 
55  wrappers.wrapType(
56  PyAngleUnit(wrappers.module, "AngleUnit"),
57  [](auto & mod, auto & cls) mutable {
58  cls.def("__eq__", [](AngleUnit const& self, AngleUnit const& other) { return self == other; },
59  py::is_operator());
60  cls.def("__ne__", [](AngleUnit const& self, AngleUnit const& other) { return !(self == other); },
61  py::is_operator());
62  cls.def("_mul", [](AngleUnit const& self, double other) { return other * self; },
63  py::is_operator());
64  cls.def("_rmul", [](AngleUnit const& self, double other) { return other * self; },
65  py::is_operator());
66  mod.attr("radians") = py::cast(radians);
67  mod.attr("degrees") = py::cast(degrees);
68  mod.attr("hours") = py::cast(hours);
69  mod.attr("arcminutes") = py::cast(arcminutes);
70  mod.attr("arcseconds") = py::cast(arcseconds);
71  mod.attr("milliarcseconds") = py::cast(milliarcseconds);
72  }
73  );
74 
75  wrappers.wrapType(
76  PyAngle(wrappers.module, "Angle"),
77  [](auto & mod, auto & cls) mutable {
78  cls.def(py::init<double, AngleUnit>(), py::arg("val"), py::arg("units") = radians);
79  cls.def(py::init<>());
80  declareAngleComparisonOperators<Angle>(cls);
81  declareAngleComparisonOperators<double>(cls);
82  declareAngleComparisonOperators<int>(cls);
83  cls.def("__mul__", [](Angle const& self, double other) { return self * other; },
84  py::is_operator());
85  cls.def("__mul__", [](Angle const& self, int other) { return self * other; },
86  py::is_operator());
87  cls.def("__rmul__", [](Angle const& self, double other) { return self * other; },
88  py::is_operator());
89  cls.def("__rmul__", [](Angle const& self, int other) { return self * other; },
90  py::is_operator());
91  cls.def("__imul__", [](Angle& self, double other) { return self *= other; });
92  cls.def("__imul__", [](Angle& self, int other) { return self *= other; });
93  cls.def("__add__", [](Angle const& self, Angle const& other) { return self + other; },
94  py::is_operator());
95  cls.def("__sub__", [](Angle const& self, Angle const& other) { return self - other; },
96  py::is_operator());
97  cls.def("__neg__", [](Angle const& self) { return -self; }, py::is_operator());
98  cls.def("__iadd__", [](Angle& self, Angle const& other) { return self += other; });
99  cls.def("__isub__", [](Angle& self, Angle const& other) { return self -= other; });
100  cls.def("__truediv__", [](Angle const& self, double other) { return self / other; },
101  py::is_operator());
102  // Without an explicit wrapper, Python lets Angle / Angle -> Angle
103  cls.def("__truediv__", [](Angle const& self, Angle const& other) {
104  throw py::type_error("unsupported operand type(s) for /: 'Angle' and 'Angle'");
105  });
106  cls.def("__float__", &Angle::operator double);
107  cls.def("__abs__", [](Angle const& self) { return std::abs(self.asRadians()) * radians; });
108 
109  cls.def("__reduce__", [cls](Angle const& self) {
110  return py::make_tuple(cls, py::make_tuple(py::cast(self.asRadians())));
111  });
112  utils::python::addOutputOp(cls, "__str__");
113  utils::python::addOutputOp(cls, "__repr__");
114  cls.def("asAngularUnits", &Angle::asAngularUnits);
115  cls.def("asRadians", &Angle::asRadians);
116  cls.def("asDegrees", &Angle::asDegrees);
117  cls.def("asHours", &Angle::asHours);
118  cls.def("asArcminutes", &Angle::asArcminutes);
119  cls.def("asArcseconds", &Angle::asArcseconds);
120  cls.def("asMilliarcseconds", &Angle::asMilliarcseconds);
121  cls.def("wrap", &Angle::wrap);
122  cls.def("wrapCtr", &Angle::wrapCtr);
123  cls.def("wrapNear", &Angle::wrapNear);
124  cls.def("separation", &Angle::separation);
125  mod.def("isAngle", isAngle<Angle>);
126  mod.def("isAngle", isAngle<double>);
127  }
128  );
129 
130  wrappers.wrap(
131  [](auto & mod) mutable {
132  mod.attr("PI") = py::float_(PI);
133  mod.attr("TWOPI") = py::float_(TWOPI);
134  mod.attr("HALFPI") = py::float_(HALFPI);
135  mod.attr("ONE_OVER_PI") = py::float_(ONE_OVER_PI);
136  mod.attr("SQRTPI") = py::float_(SQRTPI);
137  mod.attr("INVSQRTPI") = py::float_(INVSQRTPI);
138  mod.attr("ROOT2") = py::float_(ROOT2);
139  mod.def("degToRad", degToRad);
140  mod.def("radToDeg", radToDeg);
141  mod.def("radToArcsec", radToArcsec);
142  mod.def("radToMas", radToMas);
143  mod.def("arcsecToRad", arcsecToRad);
144  mod.def("masToRad", masToRad);
145  }
146  );
147 }
148 
149 } // namespace geom
150 } // namespace lsst
ItemVariant const * other
Definition: Schema.cc:56
A class representing an angle.
Definition: Angle.h:127
Angle wrapCtr() const noexcept
Wrap this angle to the range [-π, π).
Definition: Angle.h:412
constexpr double asRadians() const noexcept
Return an Angle's value in radians.
Definition: Angle.h:166
constexpr double asHours() const noexcept
Return an Angle's value in hours.
Definition: Angle.h:172
Angle separation(Angle const &other) const noexcept
The signed difference between two Angles.
Definition: Angle.h:449
constexpr double asDegrees() const noexcept
Return an Angle's value in degrees.
Definition: Angle.h:169
constexpr double asMilliarcseconds() const noexcept
Return an Angle's value in milliarcseconds.
Definition: Angle.h:181
constexpr double asArcseconds() const noexcept
Return an Angle's value in arcseconds.
Definition: Angle.h:178
Angle wrapNear(Angle const &refAng) const noexcept
Wrap this angle to a value x such that -π ≤ x - refAng ≤ π, approximately.
Definition: Angle.h:431
Angle wrap() const noexcept
Wrap this angle to the range [0, 2π).
Definition: Angle.h:403
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle's value in the specified units.
Definition: Angle.h:163
constexpr double asArcminutes() const noexcept
Return an Angle's value in arcminutes.
Definition: Angle.h:175
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:70
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition: python.h:242
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition: python.h:448
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
Definition: python.h:391
lsst::geom::Angle Angle
Definition: misc.h:33
double const SQRTPI
Definition: Angle.h:44
constexpr double arcsecToRad(double x) noexcept
Definition: Angle.h:55
void wrapAngle(utils::python::WrapperCollection &wrappers)
Definition: _Angle.cc:54
double const INVSQRTPI
Definition: Angle.h:45
constexpr double radToMas(double x) noexcept
Definition: Angle.h:54
constexpr double PI
The ratio of a circle's circumference to diameter.
Definition: Angle.h:39
constexpr AngleUnit arcseconds
constant with units of arcseconds
Definition: Angle.h:112
constexpr double HALFPI
Definition: Angle.h:41
constexpr AngleUnit degrees
constant with units of degrees
Definition: Angle.h:109
constexpr double radToDeg(double x) noexcept
Definition: Angle.h:52
constexpr double masToRad(double x) noexcept
Definition: Angle.h:56
constexpr double radToArcsec(double x) noexcept
Definition: Angle.h:53
constexpr double TWOPI
Definition: Angle.h:40
constexpr AngleUnit hours
constant with units of hours
Definition: Angle.h:110
constexpr AngleUnit milliarcseconds
constant with units of milliarcseconds
Definition: Angle.h:115
py::class_< Angle > PyAngle
Definition: _Angle.cc:33
constexpr double degToRad(double x) noexcept
Definition: Angle.h:51
constexpr double ONE_OVER_PI
Definition: Angle.h:42
constexpr double ROOT2
Definition: Angle.h:46
constexpr AngleUnit radians
constant with units of radians
Definition: Angle.h:108
py::class_< AngleUnit > PyAngleUnit
Definition: _Angle.cc:34
constexpr AngleUnit arcminutes
constant with units of arcminutes
Definition: Angle.h:111
Angle abs(Angle const &a)
Definition: Angle.h:106
void addOutputOp(PyClass &cls, std::string const &method)
Add __str__ or __repr__ method implemented by operator<<.
Definition: python.h:87
A base class for image defects.