Mantid
Loading...
Searching...
No Matches
V3R.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 +
8namespace Mantid::Geometry {
9
11V3R::V3R() : m_x(0), m_y(0), m_z(0) {}
12
14V3R::V3R(const RationalNumber &x, const RationalNumber &y, const RationalNumber &z) : m_x(x), m_y(y), m_z(z) {}
15
17V3R::V3R(const std::vector<int> &vector) {
18 if (vector.size() != 3) {
19 throw Kernel::Exception::MisMatch<size_t>(vector.size(), 3, "V3R(const std::vector<int> &vector)");
20 }
21
22 m_x = vector[0];
23 m_y = vector[1];
24 m_z = vector[2];
25}
26
28const RationalNumber &V3R::x() const { return m_x; }
29
31void V3R::setX(const RationalNumber &newX) { m_x = newX; }
32
34const RationalNumber &V3R::y() const { return m_y; }
35
37void V3R::setY(const RationalNumber &newY) { m_y = newY; }
38
40const RationalNumber &V3R::z() const { return m_z; }
41
43void V3R::setZ(const RationalNumber &newZ) { m_z = newZ; }
44
48 switch (index) {
49 case 0:
50 return m_x;
51 case 1:
52 return m_y;
53 case 2:
54 return m_z;
55 default:
56 throw Kernel::Exception::IndexError(index, 2, "V3R::operator [] index out of range.");
57 }
58}
59
62const RationalNumber &V3R::operator[](size_t index) const {
63 switch (index) {
64 case 0:
65 return m_x;
66 case 1:
67 return m_y;
68 case 2:
69 return m_z;
70 default:
71 throw Kernel::Exception::IndexError(index, 2, "V3R::operator [] index out of range.");
72 }
73}
74
75// Operations with other vectors
77V3R V3R::operator+(const V3R &other) const {
78 V3R result(*this);
79 return result += other;
80}
81
84V3R &V3R::operator+=(const V3R &other) {
85 m_x += other.m_x;
86 m_y += other.m_y;
87 m_z += other.m_z;
88
89 return *this;
90}
91
93V3R V3R::operator-() const { return V3R(-m_x, -m_y, -m_z); }
94
96V3R V3R::operator-(const V3R &other) const {
97 V3R result(*this);
98 return result -= other;
99}
100
103V3R &V3R::operator-=(const V3R &other) {
104 m_x -= other.m_x;
105 m_y -= other.m_y;
106 m_z -= other.m_z;
107
108 return *this;
109}
110
111// Operations with int
114V3R V3R::operator+(int other) const {
115 V3R result(*this);
116 return result += other;
117}
118
121V3R &V3R::operator+=(int other) {
122 m_x += other;
123 m_y += other;
124 m_z += other;
125
126 return *this;
127}
128
131V3R V3R::operator-(int other) const {
132 V3R result(*this);
133 return result -= other;
134}
135
138V3R &V3R::operator-=(int other) {
139 m_x -= other;
140 m_y -= other;
141 m_z -= other;
142
143 return *this;
144}
145
148V3R V3R::operator*(int other) const {
149 V3R result(*this);
150 return result *= other;
151}
152
155V3R &V3R::operator*=(int other) {
156 m_x *= other;
157 m_y *= other;
158 m_z *= other;
159
160 return *this;
161}
162
165V3R V3R::operator/(int other) const {
166 V3R result(*this);
167 return result /= other;
168}
169
172V3R &V3R::operator/=(int other) {
173 m_x /= other;
174 m_y /= other;
175 m_z /= other;
176
177 return *this;
178}
179
180// Operations with rational numbers
183V3R V3R::operator+(const RationalNumber &other) const {
184 V3R result(*this);
185 return result += other;
186}
187
191 m_x += other;
192 m_y += other;
193 m_z += other;
194
195 return *this;
196}
197
200V3R V3R::operator-(const RationalNumber &other) const {
201 V3R result(*this);
202 return result -= other;
203}
204
208 m_x -= other;
209 m_y -= other;
210 m_z -= other;
211
212 return *this;
213}
214
217V3R V3R::operator*(const RationalNumber &other) const {
218 V3R result(*this);
219 return result *= other;
220}
221
225 m_x *= other;
226 m_y *= other;
227 m_z *= other;
228
229 return *this;
230}
231
234V3R V3R::operator/(const RationalNumber &other) const {
235 V3R result(*this);
236 return result /= other;
237}
238
242 m_x /= other;
243 m_y /= other;
244 m_z /= other;
245
246 return *this;
247}
248
251V3R::operator Kernel::V3D() const {
252 return Kernel::V3D(boost::rational_cast<double>(m_x), boost::rational_cast<double>(m_y),
253 boost::rational_cast<double>(m_z));
254}
255
258Kernel::V3D V3R::operator+(const Kernel::V3D &other) const { return other + static_cast<Kernel::V3D>(*this); }
259
262Kernel::V3D V3R::operator-(const Kernel::V3D &other) const { return static_cast<Kernel::V3D>(*this) - other; }
263
266bool V3R::operator==(const V3R &other) const { return m_x == other.m_x && m_y == other.m_y && m_z == other.m_z; }
267
269bool V3R::operator!=(const V3R &other) const { return !(this->operator==(other)); }
270
273bool V3R::operator<(const V3R &other) const {
274 if (m_x != other.m_x) {
275 return m_x < other.m_x;
276 }
277
278 if (m_y != other.m_y) {
279 return m_y < other.m_y;
280 }
281
282 return m_z < other.m_z;
283}
284
287bool V3R::operator==(int other) const { return m_x == other && m_y == other && m_z == other; }
288
290bool V3R::operator!=(int other) const { return !(this->operator==(other)); }
291
293V3R V3R::getPositiveVector() const { return V3R(boost::abs(m_x), boost::abs(m_y), boost::abs(m_z)); }
294
296V3R::operator std::vector<double>() const {
297 std::vector<double> vector;
298 vector.emplace_back(boost::rational_cast<double>(m_x));
299 vector.emplace_back(boost::rational_cast<double>(m_y));
300 vector.emplace_back(boost::rational_cast<double>(m_z));
301
302 return vector;
303}
304
305} // namespace Mantid::Geometry
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
V3R & operator*=(int other)
Performs the operation v1 *= i in place, which multiplies each component of v1 with the integer i.
Definition: V3R.cpp:155
V3R operator/(int other) const
Performs the operation v' = v1 / i, which divides each component of v1 by the integer i.
Definition: V3R.cpp:165
V3R operator*(int other) const
Performs the operation v' = v1 * i, which multiplies each component of v1 with the integer i.
Definition: V3R.cpp:148
V3R()
Default constructor, all elements 0.
Definition: V3R.cpp:11
const RationalNumber & z() const
Returns the z-component of the vector.
Definition: V3R.cpp:40
bool operator==(const V3R &other) const
Returns true if all components of the compared vectors are equal, false otherwise.
Definition: V3R.cpp:266
RationalNumber m_y
Definition: V3R.h:109
V3R & operator/=(int other)
Performs the operation v1 /= i in place, which divides each component of v1 by the integer i.
Definition: V3R.cpp:172
RationalNumber & operator[](size_t index)
Array-style non-const access to the components.
Definition: V3R.cpp:47
bool operator<(const V3R &other) const
Compares x of both vectors first, if those are equal the function compares y and finally z.
Definition: V3R.cpp:273
void setZ(const RationalNumber &newZ)
Assigns a new value to the z-component.
Definition: V3R.cpp:43
V3R operator-() const
Negates all components of the vector.
Definition: V3R.cpp:93
V3R getPositiveVector() const
Returns a V3R with absolute components.
Definition: V3R.cpp:293
RationalNumber m_z
Definition: V3R.h:110
void setX(const RationalNumber &newX)
Assigns a new value to the x-component.
Definition: V3R.cpp:31
V3R & operator+=(const V3R &other)
Performs the operation v1 += v2 in place, which adds the components of v2 to the components of v1.
Definition: V3R.cpp:84
V3R & operator-=(const V3R &other)
Performs the operation v1 -= v2 in place, which subtracts the components of v2 from the components of...
Definition: V3R.cpp:103
void setY(const RationalNumber &newY)
Assigns a new value to the y-component.
Definition: V3R.cpp:37
const RationalNumber & y() const
Returns the y-component of the vector.
Definition: V3R.cpp:34
const RationalNumber & x() const
Returns the x-component of the vector.
Definition: V3R.cpp:28
RationalNumber m_x
Definition: V3R.h:108
bool operator!=(const V3R &other) const
Returns true if the compared vectors are not equal.
Definition: V3R.cpp:269
V3R operator+(const V3R &other) const
Performs the operation v1 + v2, which sums the vectors component-wise.
Definition: V3R.cpp:77
Exception for index errors.
Definition: Exception.h:284
Error when two numbers should be identical (or close)
Definition: Exception.h:263
Class for 3D vectors.
Definition: V3D.h:34
boost::rational< int > RationalNumber
V3R :
Definition: V3R.h:35