LSSTApplications  19.0.0-11-g2ce9f25+2,20.0.0+1,20.0.0+11,20.0.0+2,20.0.0+3,20.0.0+4,20.0.0+5,20.0.0+8,20.0.0+9,20.0.0-1-g009f3de,20.0.0-1-g10df615+8,20.0.0-1-g253301a+4,20.0.0-1-g32a200e+8,20.0.0-1-g596936a+9,20.0.0-1-g8a53f90+1,20.0.0-1-gc96f8cb+10,20.0.0-1-gd1c87d7+1,20.0.0-15-g34741e2+3,20.0.0-2-g04cfba9+2,20.0.0-2-gec03fae+2,20.0.0-3-g082faa5+1,20.0.0-3-g2fa8bb8+6,20.0.0-3-gbdbfa727+1,20.0.0-4-gde602ef96+3,20.0.0-4-ge48a6ca+4,20.0.0-8-g1acaa36,20.0.0-8-g7eef53f7+5,w.2020.27
LSSTDataManagementBasePackage
unitVector3d.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * See COPYRIGHT file at the top of the source tree.
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 #include "pybind11/pybind11.h"
23 
24 #include <memory>
25 #include <string>
26 
27 #include "lsst/sphgeom/Angle.h"
28 #include "lsst/sphgeom/LonLat.h"
30 #include "lsst/sphgeom/Vector3d.h"
32 
33 namespace py = pybind11;
34 using namespace pybind11::literals;
35 
36 namespace lsst {
37 namespace sphgeom {
38 namespace {
39 
40 PYBIND11_MODULE(unitVector3d, mod) {
41  py::module::import("lsst.sphgeom.vector3d");
42 
43  py::class_<UnitVector3d, std::shared_ptr<UnitVector3d>> cls(mod,
44  "UnitVector3d");
45  // Provide the equivalent of the UnitVector3d to Vector3d C++ cast
46  // operator in Python
47  py::implicitly_convertible<UnitVector3d, Vector3d>();
48 
49  cls.def_static(
50  "orthogonalTo",
51  (UnitVector3d(*)(Vector3d const &)) & UnitVector3d::orthogonalTo,
52  "vector"_a);
53  cls.def_static("orthogonalTo",
54  (UnitVector3d(*)(Vector3d const &, Vector3d const &)) &
55  UnitVector3d::orthogonalTo,
56  "vector1"_a, "vector2"_a);
57  cls.def_static("orthogonalTo",
58  (UnitVector3d(*)(NormalizedAngle const &)) &
59  UnitVector3d::orthogonalTo,
60  "meridian"_a);
61  cls.def_static("northFrom", &UnitVector3d::northFrom, "vector"_a);
62  cls.def_static("X", &UnitVector3d::X);
63  cls.def_static("Y", &UnitVector3d::Y);
64  cls.def_static("Z", &UnitVector3d::Z);
65  // The fromNormalized static factory functions are not exposed to
66  // Python, as they are easy to misuse and intended only for performance
67  // critical code (i.e. not Python).
68 
69  cls.def(py::init<>());
70  cls.def(py::init<UnitVector3d const &>(), "unitVector"_a);
71  cls.def(py::init<Vector3d const &>(), "vector"_a);
72  cls.def(py::init<double, double, double>(), "x"_a, "y"_a, "z"_a);
73  cls.def(py::init<LonLat const &>(), "lonLat"_a);
74  cls.def(py::init<Angle, Angle>(), "lon"_a, "lat"_a);
75 
76  cls.def("__eq__", &UnitVector3d::operator==, py::is_operator());
77  cls.def("__ne__", &UnitVector3d::operator!=, py::is_operator());
78  cls.def("__neg__",
79  (UnitVector3d(UnitVector3d::*)() const) & UnitVector3d::operator-);
80  cls.def("__add__", &UnitVector3d::operator+, py::is_operator());
81  cls.def("__sub__",
82  (Vector3d(UnitVector3d::*)(Vector3d const &) const) &
83  UnitVector3d::operator-,
84  py::is_operator());
85  cls.def("__mul__", &UnitVector3d::operator*, py::is_operator());
86  cls.def("__truediv__", &UnitVector3d::operator/, py::is_operator());
87 
88  cls.def("x", &UnitVector3d::x);
89  cls.def("y", &UnitVector3d::y);
90  cls.def("z", &UnitVector3d::z);
91  cls.def("x", &UnitVector3d::dot);
92  cls.def("dot", &UnitVector3d::dot);
93  cls.def("cross", &UnitVector3d::cross);
94  cls.def("robustCross", &UnitVector3d::robustCross);
95  cls.def("cwiseProduct", &UnitVector3d::cwiseProduct);
96  cls.def("rotatedAround", &UnitVector3d::rotatedAround, "axis"_a, "angle"_a);
97 
98  cls.def("__len__", [](UnitVector3d const &self) { return py::int_(3); });
99  cls.def("__getitem__", [](UnitVector3d const &self, py::int_ i) {
100  return self(python::convertIndex(3, i));
101  });
102 
103  cls.def("__str__", [](UnitVector3d const &self) {
104  return py::str("[{!s}, {!s}, {!s}]")
105  .format(self.x(), self.y(), self.z());
106  });
107  cls.def("__repr__", [](UnitVector3d const &self) {
108  return py::str("UnitVector3d({!r}, {!r}, {!r})")
109  .format(self.x(), self.y(), self.z());
110  });
111 
112  // Do not implement __reduce__ for pickling. Why? Given:
113  //
114  // u = UnitVector3d(x, y, z)
115  // v = UnitVector3d(u.x(), u.y(), u.z())
116  //
117  // u may not be identical to v, since the constructor normalizes its input
118  // components. Furthermore, UnitVector3d::fromNormalized is not visible to
119  // Python, and even if it were, pybind11 is currently incapable of returning
120  // a picklable reference to it.
121  cls.def(py::pickle([](UnitVector3d const &self) { return py::make_tuple(self.x(), self.y(), self.z()); },
122  [](py::tuple t) {
123  if (t.size() != 3) {
124  throw std::runtime_error("Tuple size = " + std::to_string(t.size()) +
125  "; must be 3 for a UnitVector3d");
126  }
127  return new UnitVector3d(UnitVector3d::fromNormalized(
128  t[0].cast<double>(), t[1].cast<double>(), t[2].cast<double>()));
129  }));
130 }
131 
132 } // <anonymous>
133 } // sphgeom
134 } // lsst
y
int y
Definition: SpanSet.cc:49
UnitVector3d.h
This file declares a class for representing unit vectors in ℝ³.
utils.h
Angle.h
This file declares a class for representing angles.
lsst::afw::image::X
@ X
Definition: ImageUtils.h:36
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
lsst::afw.display.ds9.dot
def dot(symb, c, r, frame=None, size=2, ctype=None, origin=afwImage.PARENT, *args, **kwargs)
Definition: ds9.py:101
z
double z
Definition: Match.cc:44
x
double x
Definition: ChebyshevBoundedField.cc:277
Vector3d.h
This file declares a class for representing vectors in ℝ³.
std::to_string
T to_string(T... args)
lsst::afw::image::Y
@ Y
Definition: ImageUtils.h:36
std::runtime_error
STL class.
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LonLat.h
This file contains a class representing spherical coordinates.
pybind11
Definition: _GenericMap.cc:40
lsst::afw::cameraGeom::PYBIND11_MODULE
PYBIND11_MODULE(camera, mod)
Definition: camera.cc:133