LSST Applications g1653933729+b86e4b8053,g2303c004a3+a99d6d42bd,g28da252d5a+3e0fd49a16,g2bbee38e9b+db5ba6d06c,g2bc492864f+db5ba6d06c,g2cdde0e794+b03b09fb0a,g3156d2b45e+b0ccf75d80,g347aa1857d+db5ba6d06c,g35bb328faa+b86e4b8053,g3a166c0a6a+db5ba6d06c,g3e281a1b8c+c9a36f98fc,g414038480c+70d5606f74,g42360bd172+a99d6d42bd,g461a3dce89+b86e4b8053,g5c9cb89fa5+b7dae1cdc6,g7f102674c4+d27c5e2769,g7f343241b9+516b07f5d7,g80478fca09+ee10125a54,g82479be7b0+11c35b2845,g858d7b2824+a99d6d42bd,g8cd86fa7b1+cb4c24add2,g9125e01d80+b86e4b8053,g979bb04a14+30ae7a28c8,g9ddcbc5298+b4cb6f9276,ga44b09e51c+390e0c76f2,ga5288a1d22+c32cd41bfc,gae0086650b+b86e4b8053,gb58c049af0+2fe46b4317,gc28159a63d+db5ba6d06c,gcd45df26be+a99d6d42bd,gcf0d15dbbd+be93eff7c7,gda6a2b7d83+be93eff7c7,gdaeeff99f8+b9455e0a82,ge5cf12406b+e20940066e,ge79ae78c31+db5ba6d06c,ge7c99ddb59+26aa1bc97f,gf048a9a2f4+f6d4943a12,gf0baf85859+e0282cf37d,w.2024.32
LSST Data Management Base Package
Loading...
Searching...
No Matches
_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
25
26#include "lsst/geom/Angle.h"
27
28namespace py = pybind11;
29
30namespace lsst {
31namespace geom {
32
33using PyAngle = py::class_<Angle>;
34using PyAngleUnit = py::class_<AngleUnit>;
35
36namespace {
37
38template <typename OtherT>
39void 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 cpputils::python::addOutputOp(cls, "__str__");
113 cls.def("__repr__", [](Angle const & self) {
114 if (std::isfinite(self.asDegrees())) {
115 return py::str("Angle({:0.17g}, degrees)").format(self.asDegrees());
116 } else {
117 return py::str("Angle(float('{}'), degrees)").format(self.asDegrees());
118 }
119 });
120 cls.def("asAngularUnits", &Angle::asAngularUnits);
121 cls.def("asRadians", &Angle::asRadians);
122 cls.def("asDegrees", &Angle::asDegrees);
123 cls.def("asHours", &Angle::asHours);
124 cls.def("asArcminutes", &Angle::asArcminutes);
125 cls.def("asArcseconds", &Angle::asArcseconds);
126 cls.def("asMilliarcseconds", &Angle::asMilliarcseconds);
127 cls.def("wrap", &Angle::wrap);
128 cls.def("wrapCtr", &Angle::wrapCtr);
129 cls.def("wrapNear", &Angle::wrapNear);
130 cls.def("separation", &Angle::separation);
131 mod.def("isAngle", isAngle<Angle>);
132 mod.def("isAngle", isAngle<double>);
133 py::implicitly_convertible<Angle, sphgeom::Angle>();
134 py::implicitly_convertible<sphgeom::Angle, Angle>();
135 }
136 );
137
138 wrappers.wrap(
139 [](auto & mod) mutable {
140 mod.attr("PI") = py::float_(PI);
141 mod.attr("TWOPI") = py::float_(TWOPI);
142 mod.attr("HALFPI") = py::float_(HALFPI);
143 mod.attr("ONE_OVER_PI") = py::float_(ONE_OVER_PI);
144 mod.attr("SQRTPI") = py::float_(SQRTPI);
145 mod.attr("INVSQRTPI") = py::float_(INVSQRTPI);
146 mod.attr("ROOT2") = py::float_(ROOT2);
147 mod.def("degToRad", degToRad);
148 mod.def("radToDeg", radToDeg);
149 mod.def("radToArcsec", radToArcsec);
150 mod.def("radToMas", radToMas);
151 mod.def("arcsecToRad", arcsecToRad);
152 mod.def("masToRad", masToRad);
153 }
154 );
155}
156
157} // namespace geom
158} // namespace lsst
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition python.h:242
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
A class representing an angle.
Definition Angle.h:128
Angle wrapCtr() const noexcept
Wrap this angle to the range [-π, π).
Definition Angle.h:419
constexpr double asRadians() const noexcept
Return an Angle's value in radians.
Definition Angle.h:173
constexpr double asHours() const noexcept
Return an Angle's value in hours.
Definition Angle.h:179
Angle separation(Angle const &other) const noexcept
The signed difference between two Angles.
Definition Angle.h:456
constexpr double asDegrees() const noexcept
Return an Angle's value in degrees.
Definition Angle.h:176
constexpr double asMilliarcseconds() const noexcept
Return an Angle's value in milliarcseconds.
Definition Angle.h:188
constexpr double asArcseconds() const noexcept
Return an Angle's value in arcseconds.
Definition Angle.h:185
Angle wrapNear(Angle const &refAng) const noexcept
Wrap this angle to a value x such that -π ≤ x - refAng ≤ π, approximately.
Definition Angle.h:438
Angle wrap() const noexcept
Wrap this angle to the range [0, 2π).
Definition Angle.h:410
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle's value in the specified units.
Definition Angle.h:170
constexpr double asArcminutes() const noexcept
Return an Angle's value in arcminutes.
Definition Angle.h:182
A class used to convert scalar POD types such as double to Angle.
Definition Angle.h:71
T isfinite(T... args)
void addOutputOp(PyClass &cls, std::string const &method)
Add __str__ or __repr__ method implemented by operator<<.
Definition python.h:87
double const SQRTPI
Definition Angle.h:45
constexpr double arcsecToRad(double x) noexcept
Definition Angle.h:56
double constexpr HALFPI
Definition Angle.h:42
double const INVSQRTPI
Definition Angle.h:46
double constexpr ROOT2
Definition Angle.h:47
AngleUnit constexpr milliarcseconds
constant with units of milliarcseconds
Definition Angle.h:116
void wrapAngle(cpputils::python::WrapperCollection &wrappers)
Definition _Angle.cc:54
constexpr double radToMas(double x) noexcept
Definition Angle.h:55
double constexpr ONE_OVER_PI
Definition Angle.h:43
double constexpr TWOPI
Definition Angle.h:41
constexpr double radToDeg(double x) noexcept
Definition Angle.h:53
constexpr double masToRad(double x) noexcept
Definition Angle.h:57
constexpr double radToArcsec(double x) noexcept
Definition Angle.h:54
AngleUnit constexpr arcminutes
constant with units of arcminutes
Definition Angle.h:112
AngleUnit constexpr radians
constant with units of radians
Definition Angle.h:109
constexpr double degToRad(double x) noexcept
Definition Angle.h:52
double constexpr PI
The ratio of a circle's circumference to diameter.
Definition Angle.h:40
AngleUnit constexpr hours
constant with units of hours
Definition Angle.h:111
py::class_< AngleUnit > PyAngleUnit
Definition _Angle.cc:34
py::class_< Angle > PyAngle
Definition _Angle.cc:33