LSSTApplications  11.0-24-g0a022a1,15.0+13,15.0+9,15.0-1-g19261fa+5,15.0-1-g1eca518+15,15.0-1-g60afb23+12,15.0-1-g615e0bb+4,15.0-1-g6668b0b+5,15.0-1-g788a293+12,15.0-1-ga91101e+12,15.0-1-gae1598d+8,15.0-1-gc45031d+15,15.0-1-gd076f1f+12,15.0-1-gdf18595+2,15.0-1-gf4f1c34+8,15.0-2-g100d730+5,15.0-2-g18f3f21+5,15.0-2-g35685a8+6,15.0-2-gf38729e+5,15.0-21-g91b8abf62,15.0-3-g150fc43+14,15.0-3-g6f085af+5,15.0-3-g707930d,15.0-3-g9103c06+8,15.0-3-ga03b4ca+16,15.0-3-gaec6799+5,15.0-3-gb7a597c+12,15.0-3-ge6a6747+5,15.0-4-g45f767a+8,15.0-4-g654b129+10,15.0-4-gf5d1e39,15.0-4-gff20472+15,15.0-5-ga70c291+5,15.0-6-g9a9df217+5,15.0-7-gab4c137+6,15.0-7-gab79a70c+4
LSSTDataManagementBasePackage
angle.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 AURA/LSST.
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 
25 #include "lsst/utils/python.h"
26 
27 #include "lsst/afw/geom/Angle.h"
28 
29 namespace py = pybind11;
30 
31 namespace lsst {
32 namespace afw {
33 namespace geom {
34 namespace {
35 
36 using PyAngle = py::class_<Angle>;
37 using PyAngleUnit = py::class_<AngleUnit>;
38 
39 template <typename OtherT>
40 void declareAngleComparisonOperators(PyAngle& cls) {
41  cls.def("__eq__", [](Angle const& self, OtherT const& other) { return self == other; },
42  py::is_operator());
43  cls.def("__ne__", [](Angle const& self, OtherT const& other) { return self != other; },
44  py::is_operator());
45  cls.def("__le__", [](Angle const& self, OtherT const& other) { return self <= other; },
46  py::is_operator());
47  cls.def("__ge__", [](Angle const& self, OtherT const& other) { return self >= other; },
48  py::is_operator());
49  cls.def("__lt__", [](Angle const& self, OtherT const& other) { return self < other; }, py::is_operator());
50  cls.def("__gt__", [](Angle const& self, OtherT const& other) { return self > other; }, py::is_operator());
51 }
52 
54  py::module mod("angle");
55 
56  /* AngleUnit */
57 
58  PyAngleUnit clsAngleUnit(mod, "AngleUnit");
59 
60  clsAngleUnit.def("__eq__", [](AngleUnit const& self, AngleUnit const& other) { return self == other; },
61  py::is_operator());
62  clsAngleUnit.def("__ne__", [](AngleUnit const& self, AngleUnit const& other) { return !(self == other); },
63  py::is_operator());
64  clsAngleUnit.def("_mul", [](AngleUnit const& self, double other) { return other * self; },
65  py::is_operator());
66  clsAngleUnit.def("_rmul", [](AngleUnit const& self, double other) { return other * self; },
67  py::is_operator());
68  mod.attr("radians") = py::cast(radians);
69  mod.attr("degrees") = py::cast(degrees);
70  mod.attr("hours") = py::cast(hours);
71  mod.attr("arcminutes") = py::cast(arcminutes);
72  mod.attr("arcseconds") = py::cast(arcseconds);
73 
74  /* Angle */
75 
76  PyAngle clsAngle(mod, "Angle");
77 
78  clsAngle.def(py::init<double, AngleUnit>(), py::arg("val"), py::arg("units") = radians);
79  clsAngle.def(py::init<>());
80 
81  declareAngleComparisonOperators<Angle>(clsAngle);
82  declareAngleComparisonOperators<double>(clsAngle);
83  declareAngleComparisonOperators<int>(clsAngle);
84 
85  clsAngle.def("__mul__", [](Angle const& self, double other) { return self * other; }, py::is_operator());
86  clsAngle.def("__mul__", [](Angle const& self, int other) { return self * other; }, py::is_operator());
87  clsAngle.def("__rmul__", [](Angle const& self, double other) { return self * other; }, py::is_operator());
88  clsAngle.def("__rmul__", [](Angle const& self, int other) { return self * other; }, py::is_operator());
89  clsAngle.def("__imul__", [](Angle& self, double other) { return self *= other; });
90  clsAngle.def("__imul__", [](Angle& self, int other) { return self *= other; });
91  clsAngle.def("__add__", [](Angle const& self, Angle const& other) { return self + other; },
92  py::is_operator());
93  clsAngle.def("__sub__", [](Angle const& self, Angle const& other) { return self - other; },
94  py::is_operator());
95  clsAngle.def("__neg__", [](Angle const& self) { return -self; }, py::is_operator());
96  clsAngle.def("__iadd__", [](Angle& self, Angle const& other) { return self += other; });
97  clsAngle.def("__isub__", [](Angle& self, Angle const& other) { return self -= other; });
98  clsAngle.def("__truediv__", [](Angle const& self, double other) { return self / other; },
99  py::is_operator());
100  // Without an explicit wrapper, Python lets Angle / Angle -> Angle
101  clsAngle.def("__truediv__", [](Angle const& self, Angle const& other) {
102  throw py::type_error("unsupported operand type(s) for /: 'Angle' and 'Angle'");
103  });
104 
105  clsAngle.def("__float__", &Angle::operator double);
106  clsAngle.def("__abs__", [](Angle const& self) { return std::abs(self.asRadians()) * radians; });
107 
108  clsAngle.def("__reduce__", [clsAngle](Angle const& self) {
109  return py::make_tuple(clsAngle, py::make_tuple(py::cast(self.asRadians())));
110  });
111 
112  utils::python::addOutputOp(clsAngle, "__str__");
113  utils::python::addOutputOp(clsAngle, "__repr__");
114 
115  clsAngle.def("asAngularUnits", &Angle::asAngularUnits);
116  clsAngle.def("asRadians", &Angle::asRadians);
117  clsAngle.def("asDegrees", &Angle::asDegrees);
118  clsAngle.def("asHours", &Angle::asHours);
119  clsAngle.def("asArcminutes", &Angle::asArcminutes);
120  clsAngle.def("asArcseconds", &Angle::asArcseconds);
121  clsAngle.def("wrap", &Angle::wrap);
122  clsAngle.def("wrapCtr", &Angle::wrapCtr);
123  clsAngle.def("wrapNear", &Angle::wrapNear);
124  clsAngle.def("separation", &Angle::separation);
125 
126  /* Non-members */
127 
128  mod.attr("PI") = py::float_(PI);
129  mod.attr("TWOPI") = py::float_(TWOPI);
130  mod.attr("HALFPI") = py::float_(HALFPI);
131  mod.attr("ONE_OVER_PI") = py::float_(ONE_OVER_PI);
132  mod.attr("SQRTPI") = py::float_(SQRTPI);
133  mod.attr("INVSQRTPI") = py::float_(INVSQRTPI);
134  mod.attr("ROOT2") = py::float_(ROOT2);
135 
136  mod.def("degToRad", degToRad);
137  mod.def("radToDeg", radToDeg);
138  mod.def("radToArcsec", radToArcsec);
139  mod.def("radToMas", radToMas);
140  mod.def("arcsecToRad", arcsecToRad);
141  mod.def("masToRad", masToRad);
142  mod.def("isAngle", isAngle<Angle>);
143  mod.def("isAngle", isAngle<double>);
144 
145  return mod.ptr();
146 }
147 }
148 }
149 }
150 } // lsst::afw::geom::<anonymous>
AngleUnit constexpr arcminutes
constant with units of arcminutes
Definition: Angle.h:90
void addOutputOp(PyClass &cls, std::string const &method)
Add __str__ or __repr__ method implemented by operator<<.
Definition: python.h:82
lsst::afw::geom::Angle Angle
Definition: misc.h:33
double constexpr HALFPI
Definition: Angle.h:23
Angle wrapNear(Angle const &refAng) const noexcept
Wrap this angle to a value x such that -π ≤ x - refAng ≤ π, approximately.
Definition: Angle.h:400
constexpr double masToRad(double x) noexcept
Definition: Angle.h:38
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:87
double const INVSQRTPI
Definition: Angle.h:27
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:88
table::Key< double > angle
constexpr double asArcseconds() const noexcept
Return an Angle&#39;s value in arcseconds.
Definition: Angle.h:153
double constexpr ONE_OVER_PI
Definition: Angle.h:24
double constexpr PI
The ratio of a circle&#39;s circumference to diameter.
Definition: Angle.h:21
constexpr double asArcminutes() const noexcept
Return an Angle&#39;s value in arcminutes.
Definition: Angle.h:150
A base class for image defects.
Definition: cameraGeom.dox:3
Angle wrap() const noexcept
Wrap this angle to the range [0, 2π).
Definition: Angle.h:372
double constexpr ROOT2
Definition: Angle.h:28
Angle wrapCtr() const noexcept
Wrap this angle to the range [-π, π).
Definition: Angle.h:381
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle&#39;s value in the specified units.
Definition: Angle.h:138
AngleUnit constexpr hours
constant with units of hours
Definition: Angle.h:89
constexpr double degToRad(double x) noexcept
Definition: Angle.h:33
AngleUnit constexpr arcseconds
constant with units of arcseconds
Definition: Angle.h:91
constexpr double radToArcsec(double x) noexcept
Definition: Angle.h:35
constexpr double asRadians() const noexcept
Return an Angle&#39;s value in radians.
Definition: Angle.h:141
constexpr double radToDeg(double x) noexcept
Definition: Angle.h:34
ItemVariant const * other
Definition: Schema.cc:55
Angle separation(Angle const &other) const noexcept
The signed difference between two Angles.
Definition: Angle.h:418
constexpr double arcsecToRad(double x) noexcept
Definition: Angle.h:37
constexpr double asDegrees() const noexcept
Return an Angle&#39;s value in degrees.
Definition: Angle.h:144
constexpr double radToMas(double x) noexcept
Definition: Angle.h:36
PYBIND11_PLUGIN(spanSet)
Definition: spanSet.cc:187
double const SQRTPI
Definition: Angle.h:26
double constexpr TWOPI
Definition: Angle.h:22
constexpr double asHours() const noexcept
Return an Angle&#39;s value in hours.
Definition: Angle.h:147