LSSTApplications  12.1-5-gbdcc3ab+2,15.0+14,15.0+30,15.0-1-g19261fa+21,15.0-1-g417ea41,15.0-1-g60afb23+30,15.0-1-g615e0bb+22,15.0-1-g788a293+30,15.0-1-ga91101e+30,15.0-1-gae1598d+13,15.0-1-gd076f1f+28,15.0-1-gdf18595+5,15.0-12-g3681e7a+8,15.0-12-g7952b551+2,15.0-16-g6f0eb036+3,15.0-17-g076ea75+3,15.0-2-g100d730+23,15.0-2-g8aea5f4+1,15.0-2-gf38729e+25,15.0-2-gf60f3cf,15.0-3-g707930d+3,15.0-3-g9103c06+12,15.0-30-g9378914ca+1,15.0-4-g9ee0f43+3,15.0-4-gf6f1c6c+3,15.0-4-gf906033+2,15.0-5-g23e394c+18,15.0-5-g4be42a9+4,15.0-5-gae1eaf0+3,15.0-6-g09241ba+6,15.0-6-g69628aa+4,15.0-6-g81517ef+3,15.0-6-gc1213af+3,15.0-6-gfa9b38f+8,15.0-7-ged79c87+3,15.0-8-g13fca11+3,15.0-8-g67a62d3+5,15.0-8-gcf05001+5,15.0-9-g1e7c341+2,w.2018.25
LSSTDataManagementBasePackage
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 namespace {
33 
34 using PyAngle = py::class_<Angle>;
35 using PyAngleUnit = py::class_<AngleUnit>;
36 
37 template <typename OtherT>
38 void declareAngleComparisonOperators(PyAngle& cls) {
39  cls.def("__eq__", [](Angle const& self, OtherT const& other) { return self == other; },
40  py::is_operator());
41  cls.def("__ne__", [](Angle const& self, OtherT const& other) { return self != other; },
42  py::is_operator());
43  cls.def("__le__", [](Angle const& self, OtherT const& other) { return self <= other; },
44  py::is_operator());
45  cls.def("__ge__", [](Angle const& self, OtherT const& other) { return self >= other; },
46  py::is_operator());
47  cls.def("__lt__", [](Angle const& self, OtherT const& other) { return self < other; }, py::is_operator());
48  cls.def("__gt__", [](Angle const& self, OtherT const& other) { return self > other; }, py::is_operator());
49 }
50 
52  py::module mod("angle");
53 
54  /* AngleUnit */
55 
56  PyAngleUnit clsAngleUnit(mod, "AngleUnit");
57 
58  clsAngleUnit.def("__eq__", [](AngleUnit const& self, AngleUnit const& other) { return self == other; },
59  py::is_operator());
60  clsAngleUnit.def("__ne__", [](AngleUnit const& self, AngleUnit const& other) { return !(self == other); },
61  py::is_operator());
62  clsAngleUnit.def("_mul", [](AngleUnit const& self, double other) { return other * self; },
63  py::is_operator());
64  clsAngleUnit.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 
72  /* Angle */
73 
74  PyAngle clsAngle(mod, "Angle");
75 
76  clsAngle.def(py::init<double, AngleUnit>(), py::arg("val"), py::arg("units") = radians);
77  clsAngle.def(py::init<>());
78 
79  declareAngleComparisonOperators<Angle>(clsAngle);
80  declareAngleComparisonOperators<double>(clsAngle);
81  declareAngleComparisonOperators<int>(clsAngle);
82 
83  clsAngle.def("__mul__", [](Angle const& self, double other) { return self * other; }, py::is_operator());
84  clsAngle.def("__mul__", [](Angle const& self, int other) { return self * other; }, py::is_operator());
85  clsAngle.def("__rmul__", [](Angle const& self, double other) { return self * other; }, py::is_operator());
86  clsAngle.def("__rmul__", [](Angle const& self, int other) { return self * other; }, py::is_operator());
87  clsAngle.def("__imul__", [](Angle& self, double other) { return self *= other; });
88  clsAngle.def("__imul__", [](Angle& self, int other) { return self *= other; });
89  clsAngle.def("__add__", [](Angle const& self, Angle const& other) { return self + other; },
90  py::is_operator());
91  clsAngle.def("__sub__", [](Angle const& self, Angle const& other) { return self - other; },
92  py::is_operator());
93  clsAngle.def("__neg__", [](Angle const& self) { return -self; }, py::is_operator());
94  clsAngle.def("__iadd__", [](Angle& self, Angle const& other) { return self += other; });
95  clsAngle.def("__isub__", [](Angle& self, Angle const& other) { return self -= other; });
96  clsAngle.def("__truediv__", [](Angle const& self, double other) { return self / other; },
97  py::is_operator());
98  // Without an explicit wrapper, Python lets Angle / Angle -> Angle
99  clsAngle.def("__truediv__", [](Angle const& self, Angle const& other) {
100  throw py::type_error("unsupported operand type(s) for /: 'Angle' and 'Angle'");
101  });
102 
103  clsAngle.def("__float__", &Angle::operator double);
104  clsAngle.def("__abs__", [](Angle const& self) { return std::abs(self.asRadians()) * radians; });
105 
106  clsAngle.def("__reduce__", [clsAngle](Angle const& self) {
107  return py::make_tuple(clsAngle, py::make_tuple(py::cast(self.asRadians())));
108  });
109 
110  utils::python::addOutputOp(clsAngle, "__str__");
111  utils::python::addOutputOp(clsAngle, "__repr__");
112 
113  clsAngle.def("asAngularUnits", &Angle::asAngularUnits);
114  clsAngle.def("asRadians", &Angle::asRadians);
115  clsAngle.def("asDegrees", &Angle::asDegrees);
116  clsAngle.def("asHours", &Angle::asHours);
117  clsAngle.def("asArcminutes", &Angle::asArcminutes);
118  clsAngle.def("asArcseconds", &Angle::asArcseconds);
119  clsAngle.def("wrap", &Angle::wrap);
120  clsAngle.def("wrapCtr", &Angle::wrapCtr);
121  clsAngle.def("wrapNear", &Angle::wrapNear);
122  clsAngle.def("separation", &Angle::separation);
123 
124  /* Non-members */
125 
126  mod.attr("PI") = py::float_(PI);
127  mod.attr("TWOPI") = py::float_(TWOPI);
128  mod.attr("HALFPI") = py::float_(HALFPI);
129  mod.attr("ONE_OVER_PI") = py::float_(ONE_OVER_PI);
130  mod.attr("SQRTPI") = py::float_(SQRTPI);
131  mod.attr("INVSQRTPI") = py::float_(INVSQRTPI);
132  mod.attr("ROOT2") = py::float_(ROOT2);
133 
134  mod.def("degToRad", degToRad);
135  mod.def("radToDeg", radToDeg);
136  mod.def("radToArcsec", radToArcsec);
137  mod.def("radToMas", radToMas);
138  mod.def("arcsecToRad", arcsecToRad);
139  mod.def("masToRad", masToRad);
140  mod.def("isAngle", isAngle<Angle>);
141  mod.def("isAngle", isAngle<double>);
142 
143  return mod.ptr();
144 }
145 
146 } // namespace
147 } // namespace geom
148 } // namespace lsst
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle&#39;s value in the specified units.
Definition: Angle.h:156
AngleUnit constexpr arcminutes
constant with units of arcminutes
Definition: Angle.h:108
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:82
constexpr double asRadians() const noexcept
Return an Angle&#39;s value in radians.
Definition: Angle.h:159
constexpr double asDegrees() const noexcept
Return an Angle&#39;s value in degrees.
Definition: Angle.h:162
double constexpr TWOPI
Definition: Angle.h:40
AngleUnit constexpr hours
constant with units of hours
Definition: Angle.h:107
constexpr double radToDeg(double x) noexcept
Definition: Angle.h:52
Angle separation(Angle const &other) const noexcept
The signed difference between two Angles.
Definition: Angle.h:436
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:105
Angle wrapCtr() const noexcept
Wrap this angle to the range [-π, π).
Definition: Angle.h:399
constexpr double radToArcsec(double x) noexcept
Definition: Angle.h:53
PYBIND11_PLUGIN(_cameraSys)
Definition: cameraSys.cc:62
double constexpr ONE_OVER_PI
Definition: Angle.h:42
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:106
A base class for image defects.
Definition: cameraGeom.dox:3
constexpr double asHours() const noexcept
Return an Angle&#39;s value in hours.
Definition: Angle.h:165
AngleUnit constexpr arcseconds
constant with units of arcseconds
Definition: Angle.h:109
constexpr double degToRad(double x) noexcept
Definition: Angle.h:51
constexpr double asArcseconds() const noexcept
Return an Angle&#39;s value in arcseconds.
Definition: Angle.h:171
Angle wrapNear(Angle const &refAng) const noexcept
Wrap this angle to a value x such that -π ≤ x - refAng ≤ π, approximately.
Definition: Angle.h:418
double const INVSQRTPI
Definition: Angle.h:45
double constexpr HALFPI
Definition: Angle.h:41
constexpr double radToMas(double x) noexcept
Definition: Angle.h:54
double const SQRTPI
Definition: Angle.h:44
ItemVariant const * other
Definition: Schema.cc:55
constexpr double asArcminutes() const noexcept
Return an Angle&#39;s value in arcminutes.
Definition: Angle.h:168
lsst::geom::Angle Angle
Definition: misc.h:33
Angle wrap() const noexcept
Wrap this angle to the range [0, 2π).
Definition: Angle.h:390
constexpr double masToRad(double x) noexcept
Definition: Angle.h:56
constexpr double arcsecToRad(double x) noexcept
Definition: Angle.h:55
double constexpr ROOT2
Definition: Angle.h:46
double constexpr PI
The ratio of a circle&#39;s circumference to diameter.
Definition: Angle.h:39