Mantid
Loading...
Searching...
No Matches
V3D.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include "MantidKernel/V3D.h"
9#include <boost/python/class.hpp>
10#include <boost/python/copy_const_reference.hpp>
11#include <boost/python/dict.hpp>
12#include <boost/python/errors.hpp>
13#include <boost/python/list.hpp>
14#include <boost/python/operators.hpp>
15#include <boost/python/return_arg.hpp>
16#include <boost/python/return_value_policy.hpp>
17
18using namespace boost::python;
20
21namespace {
22
29V3D directionAnglesDefault(V3D &self) { return self.directionAngles(); }
30
37std::vector<double> getSpherical(V3D &self) {
38 double R, theta, phi = 0;
39 self.getSpherical(R, theta, phi);
40 return {R, theta, phi};
41}
42
43Py_hash_t hashV3D(V3D &self) {
44 boost::python::object tmpObj(self.toString());
45
46 return PyObject_Hash(tmpObj.ptr());
47}
48
49double getV3DItem(V3D &self, int index) {
50 switch (index) {
51 case -3:
52 case 0:
53 return self.X();
54 case -2:
55 case 1:
56 return self.Y();
57 case -1:
58 case 2:
59 return self.Z();
60 }
61 PyErr_SetString(PyExc_IndexError, "index out of range");
62 throw boost::python::error_already_set();
63}
64
65void setV3DItem(V3D &self, int index, double value) {
66 switch (index) {
67 case -3:
68 case 0:
69 self.setX(value);
70 break;
71 case -2:
72 case 1:
73 self.setY(value);
74 break;
75 case -1:
76 case 2:
77 self.setZ(value);
78 break;
79 default:
80 PyErr_SetString(PyExc_IndexError, "index out of range");
81 throw boost::python::error_already_set();
82 }
83}
84
85int getV3DLength(V3D &self) {
86 UNUSED_ARG(self);
87 return 3;
88}
89} // namespace
90
91class V3DPickleSuite : public boost::python::pickle_suite {
92public:
93 static dict getstate(const V3D &vector) {
94 dict data;
95 data['x'] = vector.X();
96 data['y'] = vector.Y();
97 data['z'] = vector.Z();
98 return data;
99 }
100
101 static void setstate(V3D &vector, dict state) {
102 vector.setX(extract<double>(state['x']));
103 vector.setY(extract<double>(state['y']));
104 vector.setZ(extract<double>(state['z']));
105 }
106};
107
109 // V3D class
110 GNU_DIAG_OFF("self-assign-overloaded")
111 class_<V3D>("V3D", init<>("Construct a V3D at the origin"))
112 .def_pickle(V3DPickleSuite())
113 .def(init<double, double, double>("Construct a V3D with X,Y,Z coordinates"))
114 .def("X", &V3D::X, arg("self"), "Returns the X coordinate")
115 .def("Y", &V3D::Y, arg("self"), "Returns the Y coordinate")
116 .def("Z", &V3D::Z, arg("self"), "Returns the Z coordinate")
117 .def("getX", &V3D::X, arg("self"),
118 "Returns the X coordinate") // Traditional name
119 .def("getY", &V3D::Y, arg("self"),
120 "Returns the Y coordinate") // Traditional name
121 .def("getZ", &V3D::Z, arg("self"),
122 "Returns the Z coordinate") // Traditional name
123 .def("distance", &V3D::distance, (arg("self"), arg("other")),
124 "Returns the distance between this vector and another")
125 .def("angle", &V3D::angle, (arg("self"), arg("other")), "Returns the angle between this vector and another")
126 .def("cosAngle", &V3D::cosAngle, (arg("self"), arg("other")),
127 "Returns cos(angle) between this vector and another")
128 .def("zenith", &V3D::zenith, (arg("self"), arg("other")), "Returns the zenith between this vector and another")
129 .def("scalar_prod", &V3D::scalar_prod, (arg("self"), arg("other")),
130 "Computes the scalar product between this and another vector")
131 .def("cross_prod", &V3D::cross_prod, (arg("self"), arg("other")),
132 "Computes the cross product between this and another vector")
133 .def("norm", &V3D::norm, arg("self"), "Calculates the length of the vector")
134 .def("norm2", &V3D::norm2, arg("self"), "Calculates the squared length of the vector")
135 .def("getSpherical", &getSpherical, arg("self"), "Return the vector's position in spherical coordinates")
136 // cppcheck-suppress syntaxError
137 .def("__add__", &V3D::operator+, (arg("left"), arg("right")))
138 .def("__iadd__", &V3D::operator+=, return_self<>(), (arg("self"), arg("other")))
139 .def("__sub__", static_cast<V3D (V3D::*)(const V3D &) const>(&V3D::operator-), (arg("left"), arg("right")))
140 .def("__isub__", &V3D::operator-=, return_self<>(), (arg("self"), arg("other")))
141 .def("__neg__", static_cast<V3D (V3D::*)() const>(&V3D::operator-), (arg("self")))
142 .def("__len__", &getV3DLength, (arg("self")),
143 "Returns the length of the vector for list-like interface. Always "
144 "returns 3.")
145 .def("__getitem__", &getV3DItem, (arg("self"), arg("index")),
146 "Access the V3D-object like a list for getting elements.")
147 .def("__setitem__", &setV3DItem, (arg("self"), arg("index"), arg("value")),
148 "Access the V3D-object like a list for setting elements.")
149
150 .def(self * self)
151 .def(self *= self)
152 .def(self / self)
153 .def(self /= self)
154 .def(self * int())
155 .def(self *= int())
156 .def(self * double())
157 .def(self *= double())
158 .def(self < self)
159 .def(self == self)
160 .def(self != self) // must define != as Python's default is to compare
161 // object address
162 .def(self_ns::str(self))
163 .def(self_ns::repr(self))
164 .def("__hash__", &hashV3D)
165 .def("directionAngles", &V3D::directionAngles, (arg("self"), arg("inDegrees")),
166 "Calculate direction angles from direction cosines")
167 .def("directionAngles", &directionAnglesDefault, arg("self"),
168 "Calculate direction angles from direction cosines");
169 GNU_DIAG_ON("self-assign-overloaded")
170}
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
void export_V3D()
Definition V3D.cpp:108
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Class for 3D vectors.
Definition V3D.h:34
double distance(const V3D &v) const noexcept
Calculates the distance between two vectors.
Definition V3D.h:293
double zenith(const V3D &) const noexcept
Zenith (theta) angle between this and another vector.
Definition V3D.cpp:147
constexpr double scalar_prod(const V3D &v) const noexcept
Calculates the cross product.
Definition V3D.h:280
constexpr double X() const noexcept
Get x.
Definition V3D.h:238
constexpr V3D cross_prod(const V3D &v) const noexcept
Cross product (this * argument)
Definition V3D.h:284
constexpr double Y() const noexcept
Get y.
Definition V3D.h:239
std::string toString() const
Definition V3D.cpp:332
void setZ(const double zz) noexcept
Set is z position.
Definition V3D.h:236
V3D directionAngles(bool inDegrees=true) const
Direction angles.
Definition V3D.cpp:487
double angle(const V3D &) const
Angle between this and another vector.
Definition V3D.cpp:162
double norm() const noexcept
Definition V3D.h:269
constexpr double norm2() const noexcept
Vector length squared.
Definition V3D.h:271
void setX(const double xx) noexcept
Set is x position.
Definition V3D.h:224
void getSpherical(double &R, double &theta, double &phi) const noexcept
Return the vector's position in spherical coordinates.
Definition V3D.cpp:116
double cosAngle(const V3D &) const
cos(Angle) between this and another vector
Definition V3D.cpp:177
void setY(const double yy) noexcept
Set is y position.
Definition V3D.h:230
constexpr double Z() const noexcept
Get z.
Definition V3D.h:240
static void setstate(V3D &vector, dict state)
Definition V3D.cpp:101
static dict getstate(const V3D &vector)
Definition V3D.cpp:93