22#include "pybind11/pybind11.h"
38template <
typename OtherT>
39void declareAngleComparisonOperators(
PyAngle& cls) {
40 cls.def(
"__eq__", [](
Angle const& self, OtherT
const& other) {
return self == other; },
42 cls.def(
"__ne__", [](
Angle const& self, OtherT
const& other) {
return self != other; },
44 cls.def(
"__le__", [](
Angle const& self, OtherT
const& other) {
return self <= other; },
46 cls.def(
"__ge__", [](
Angle const& self, OtherT
const& other) {
return self >= other; },
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());
54void wrapAngle(utils::python::WrapperCollection & wrappers) {
57 [](
auto & mod,
auto & cls)
mutable {
58 cls.def(
"__eq__", [](AngleUnit const& self, AngleUnit const& other) { return self == other; },
60 cls.def(
"__ne__", [](
AngleUnit const& self,
AngleUnit const& other) {
return !(self == other); },
62 cls.def(
"_mul", [](
AngleUnit const& self,
double other) {
return other * self; },
64 cls.def(
"_rmul", [](
AngleUnit const& self,
double other) {
return other * self; },
66 mod.attr(
"radians") = py::cast(
radians);
67 mod.attr(
"degrees") = py::cast(
degrees);
68 mod.attr(
"hours") = py::cast(
hours);
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; },
85 cls.def(
"__mul__", [](Angle
const& self,
int other) {
return self * other; },
87 cls.def(
"__rmul__", [](Angle
const& self,
double other) {
return self * other; },
89 cls.def(
"__rmul__", [](Angle
const& self,
int other) {
return self * other; },
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; },
95 cls.def(
"__sub__", [](Angle
const& self, Angle
const& other) {
return self - other; },
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; },
103 cls.def(
"__truediv__", [](Angle
const& self, Angle
const& other) {
104 throw py::type_error(
"unsupported operand type(s) for /: 'Angle' and 'Angle'");
106 cls.def(
"__float__", &Angle::operator
double);
107 cls.def(
"__abs__", [](Angle
const& self) {
return std::abs(self.asRadians()) *
radians; });
109 cls.def(
"__reduce__", [cls](Angle
const& self) {
110 return py::make_tuple(cls, py::make_tuple(py::cast(self.asRadians())));
112 utils::python::addOutputOp(cls,
"__str__");
113 cls.def(
"__repr__", [](Angle
const & self) {
115 return py::str(
"Angle({:0.17g}, degrees)").format(self.asDegrees());
117 return py::str(
"Angle(float('{}'), degrees)").format(self.asDegrees());
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>();
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);
144 mod.attr(
"SQRTPI") = py::float_(
SQRTPI);
145 mod.attr(
"INVSQRTPI") = py::float_(
INVSQRTPI);
146 mod.attr(
"ROOT2") = py::float_(
ROOT2);
A class representing an angle.
Angle wrapCtr() const noexcept
Wrap this angle to the range [-π, π).
constexpr double asRadians() const noexcept
Return an Angle's value in radians.
constexpr double asHours() const noexcept
Return an Angle's value in hours.
Angle separation(Angle const &other) const noexcept
The signed difference between two Angles.
constexpr double asDegrees() const noexcept
Return an Angle's value in degrees.
constexpr double asMilliarcseconds() const noexcept
Return an Angle's value in milliarcseconds.
constexpr double asArcseconds() const noexcept
Return an Angle's value in arcseconds.
Angle wrapNear(Angle const &refAng) const noexcept
Wrap this angle to a value x such that -π ≤ x - refAng ≤ π, approximately.
Angle wrap() const noexcept
Wrap this angle to the range [0, 2π).
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle's value in the specified units.
constexpr double asArcminutes() const noexcept
Return an Angle's value in arcminutes.
A class used to convert scalar POD types such as double to Angle.
constexpr double arcsecToRad(double x) noexcept
void wrapAngle(utils::python::WrapperCollection &wrappers)
AngleUnit constexpr milliarcseconds
constant with units of milliarcseconds
constexpr double radToMas(double x) noexcept
double constexpr ONE_OVER_PI
AngleUnit constexpr degrees
constant with units of degrees
AngleUnit constexpr arcseconds
constant with units of arcseconds
constexpr double radToDeg(double x) noexcept
constexpr double masToRad(double x) noexcept
constexpr double radToArcsec(double x) noexcept
AngleUnit constexpr arcminutes
constant with units of arcminutes
AngleUnit constexpr radians
constant with units of radians
py::class_< Angle > PyAngle
constexpr double degToRad(double x) noexcept
double constexpr PI
The ratio of a circle's circumference to diameter.
py::class_< AngleUnit > PyAngleUnit
AngleUnit constexpr hours
constant with units of hours