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
31Py_hash_t hashV3D(V3D &self) {
32 boost::python::object tmpObj(self.toString());
33
34 return PyObject_Hash(tmpObj.ptr());
35}
36
37double getV3DItem(V3D &self, int index) {
38 switch (index) {
39 case -3:
40 case 0:
41 return self.X();
42 case -2:
43 case 1:
44 return self.Y();
45 case -1:
46 case 2:
47 return self.Z();
48 }
49 PyErr_SetString(PyExc_IndexError, "index out of range");
50 throw boost::python::error_already_set();
51}
52
53void setV3DItem(V3D &self, int index, double value) {
54 switch (index) {
55 case -3:
56 case 0:
57 self.setX(value);
58 break;
59 case -2:
60 case 1:
61 self.setY(value);
62 break;
63 case -1:
64 case 2:
65 self.setZ(value);
66 break;
67 default:
68 PyErr_SetString(PyExc_IndexError, "index out of range");
69 throw boost::python::error_already_set();
70 }
71}
72
73int getV3DLength(V3D &self) {
74 UNUSED_ARG(self);
75 return 3;
76}
77} // namespace
78
79class V3DPickleSuite : public boost::python::pickle_suite {
80public:
81 static dict getstate(const V3D &vector) {
82 dict data;
83 data['x'] = vector.X();
84 data['y'] = vector.Y();
85 data['z'] = vector.Z();
86 return data;
87 }
88
89 static void setstate(V3D &vector, dict state) {
90 vector.setX(extract<double>(state['x']));
91 vector.setY(extract<double>(state['y']));
92 vector.setZ(extract<double>(state['z']));
93 }
94};
95
96void export_V3D() {
97 // V3D class
98 GNU_DIAG_OFF("self-assign-overloaded")
99 class_<V3D>("V3D", init<>("Construct a V3D at the origin"))
100 .def_pickle(V3DPickleSuite())
101 .def(init<double, double, double>("Construct a V3D with X,Y,Z coordinates"))
102 .def("X", &V3D::X, arg("self"), "Returns the X coordinate")
103 .def("Y", &V3D::Y, arg("self"), "Returns the Y coordinate")
104 .def("Z", &V3D::Z, arg("self"), "Returns the Z coordinate")
105 .def("getX", &V3D::X, arg("self"),
106 "Returns the X coordinate") // Traditional name
107 .def("getY", &V3D::Y, arg("self"),
108 "Returns the Y coordinate") // Traditional name
109 .def("getZ", &V3D::Z, arg("self"),
110 "Returns the Z coordinate") // Traditional name
111 .def("distance", &V3D::distance, (arg("self"), arg("other")),
112 "Returns the distance between this vector and another")
113 .def("angle", &V3D::angle, (arg("self"), arg("other")), "Returns the angle between this vector and another")
114 .def("cosAngle", &V3D::cosAngle, (arg("self"), arg("other")),
115 "Returns cos(angle) between this vector and another")
116 .def("zenith", &V3D::zenith, (arg("self"), arg("other")), "Returns the zenith between this vector and another")
117 .def("scalar_prod", &V3D::scalar_prod, (arg("self"), arg("other")),
118 "Computes the scalar product between this and another vector")
119 .def("cross_prod", &V3D::cross_prod, (arg("self"), arg("other")),
120 "Computes the cross product between this and another vector")
121 .def("norm", &V3D::norm, arg("self"), "Calculates the length of the vector")
122 .def("norm2", &V3D::norm2, arg("self"), "Calculates the squared length of the vector")
123 .def("__add__", &V3D::operator+, (arg("left"), arg("right")))
124 .def("__iadd__", &V3D::operator+=, return_self<>(), (arg("self"), arg("other")))
125 .def("__sub__", static_cast<V3D (V3D::*)(const V3D &) const>(&V3D::operator-), (arg("left"), arg("right")))
126 .def("__isub__", &V3D::operator-=, return_self<>(), (arg("self"), arg("other")))
127 .def("__neg__", static_cast<V3D (V3D::*)() const>(&V3D::operator-), (arg("self")))
128 .def("__len__", &getV3DLength, (arg("self")),
129 "Returns the length of the vector for list-like interface. Always "
130 "returns 3.")
131 .def("__getitem__", &getV3DItem, (arg("self"), arg("index")),
132 "Access the V3D-object like a list for getting elements.")
133 .def("__setitem__", &setV3DItem, (arg("self"), arg("index"), arg("value")),
134 "Access the V3D-object like a list for setting elements.")
135
136 .def(self * self)
137 .def(self *= self)
138 .def(self / self)
139 .def(self /= self)
140 .def(self * int())
141 .def(self *= int())
142 .def(self * double())
143 .def(self *= double())
144 .def(self < self)
145 .def(self == self)
146 .def(self != self) // must define != as Python's default is to compare
147 // object address
148 .def(self_ns::str(self))
149 .def(self_ns::repr(self))
150 .def("__hash__", &hashV3D)
151 .def("directionAngles", &V3D::directionAngles, (arg("self"), arg("inDegrees")),
152 "Calculate direction angles from direction cosines")
153 .def("directionAngles", &directionAnglesDefault, arg("self"),
154 "Calculate direction angles from direction cosines");
155 GNU_DIAG_ON("self-assign-overloaded")
156}
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
void export_V3D()
Definition: V3D.cpp:96
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
#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:287
double zenith(const V3D &) const noexcept
Zenith (theta) angle between this and another vector.
Definition: V3D.cpp:150
constexpr double scalar_prod(const V3D &v) const noexcept
Calculates the cross product.
Definition: V3D.h:274
constexpr double X() const noexcept
Get x.
Definition: V3D.h:232
constexpr V3D cross_prod(const V3D &v) const noexcept
Cross product (this * argument)
Definition: V3D.h:278
constexpr double Y() const noexcept
Get y.
Definition: V3D.h:233
std::string toString() const
Definition: V3D.cpp:340
void setZ(const double zz) noexcept
Set is z position.
Definition: V3D.h:230
V3D directionAngles(bool inDegrees=true) const
Direction angles.
Definition: V3D.cpp:495
double angle(const V3D &) const
Angle between this and another vector.
Definition: V3D.cpp:165
double norm() const noexcept
Definition: V3D.h:263
constexpr double norm2() const noexcept
Vector length squared.
Definition: V3D.h:265
void setX(const double xx) noexcept
Set is x position.
Definition: V3D.h:218
double cosAngle(const V3D &) const
cos(Angle) between this and another vector
Definition: V3D.cpp:180
void setY(const double yy) noexcept
Set is y position.
Definition: V3D.h:224
constexpr double Z() const noexcept
Get z.
Definition: V3D.h:234
static void setstate(V3D &vector, dict state)
Definition: V3D.cpp:89
static dict getstate(const V3D &vector)
Definition: V3D.cpp:81