lsst.afw  13.0-91-g2a3e2ee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/afw/geom/Angle.h"
26 
27 namespace py = pybind11;
28 
29 namespace lsst {
30 namespace afw {
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 
51 PYBIND11_PLUGIN(angle) {
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  auto streamStr = [](Angle const& self) {
111  std::stringstream buffer;
112  buffer << self;
113  return buffer.str();
114  };
115  clsAngle.def("__str__", streamStr);
116  clsAngle.def("__repr__", streamStr);
117 
118  clsAngle.def("asAngularUnits", &Angle::asAngularUnits);
119  clsAngle.def("asRadians", &Angle::asRadians);
120  clsAngle.def("asDegrees", &Angle::asDegrees);
121  clsAngle.def("asHours", &Angle::asHours);
122  clsAngle.def("asArcminutes", &Angle::asArcminutes);
123  clsAngle.def("asArcseconds", &Angle::asArcseconds);
124  clsAngle.def("wrap", &Angle::wrap);
125  clsAngle.def("wrapCtr", &Angle::wrapCtr);
126  clsAngle.def("wrapNear", &Angle::wrapNear);
127  clsAngle.def("separation", &Angle::separation);
128 
129  /* Non-members */
130 
131  mod.attr("PI") = py::float_(PI);
132  mod.attr("TWOPI") = py::float_(TWOPI);
133  mod.attr("HALFPI") = py::float_(HALFPI);
134  mod.attr("ONE_OVER_PI") = py::float_(ONE_OVER_PI);
135  mod.attr("SQRTPI") = py::float_(SQRTPI);
136  mod.attr("INVSQRTPI") = py::float_(INVSQRTPI);
137  mod.attr("ROOT2") = py::float_(ROOT2);
138 
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  mod.def("isAngle", isAngle<Angle>);
146  mod.def("isAngle", isAngle<double>);
147 
148  return mod.ptr();
149 }
150 }
151 }
152 }
153 } // lsst::afw::geom::<anonymous>
AngleUnit constexpr arcminutes
constant with units of arcminutes
Definition: Angle.h:90
lsst::afw::geom::Angle Angle
Definition: misc.h:36
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:398
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
constexpr double asArcseconds() const noexcept
Return an Angle&#39;s value in arcseconds.
Definition: Angle.h:151
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:148
Angle wrap() const noexcept
Wrap this angle to the range [0, 2π).
Definition: Angle.h:370
double constexpr ROOT2
Definition: Angle.h:28
Angle wrapCtr() const noexcept
Wrap this angle to the range [-π, π).
Definition: Angle.h:379
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle&#39;s value in the specified units.
Definition: Angle.h:136
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:139
constexpr double radToDeg(double x) noexcept
Definition: Angle.h:34
Angle separation(Angle const &other) const noexcept
The signed difference between two Angles.
Definition: Angle.h:416
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:142
constexpr double radToMas(double x) noexcept
Definition: Angle.h:36
PYBIND11_PLUGIN(spanSet)
Definition: spanSet.cc:204
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:145