Mantid
Loading...
Searching...
No Matches
V3D.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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#pragma once
8
9#include "MantidKernel/DllConfig.h"
12#include <algorithm>
13#include <array>
14#include <cassert>
15#include <cmath>
16#include <numeric>
17#include <sstream>
18#include <vector>
19
20namespace NeXus {
21class File;
22}
23
24namespace Mantid {
25namespace Kernel {
26template <class T> class Matrix;
34class MANTID_KERNEL_DLL V3D final {
35public:
36 constexpr V3D() noexcept : m_pt({{0., 0., 0.}}) {}
37 constexpr V3D(double xx, double yy, double zz) noexcept : m_pt({{xx, yy, zz}}) {}
38
40 static bool compareMagnitude(const Kernel::V3D &v1, const Kernel::V3D &v2);
41
42 // explicit conversion into vector
43 operator std::vector<double>() const { return std::vector<double>(m_pt.cbegin(), m_pt.cend()); }
44
50 constexpr V3D operator+(const V3D &v) const noexcept {
51 return V3D(m_pt[0] + v.m_pt[0], m_pt[1] + v.m_pt[1], m_pt[2] + v.m_pt[2]);
52 }
53
59 constexpr V3D operator-(const V3D &v) const noexcept {
60 return V3D(m_pt[0] - v.m_pt[0], m_pt[1] - v.m_pt[1], m_pt[2] - v.m_pt[2]);
61 }
62
68 constexpr V3D operator*(const V3D &v) const noexcept {
69 return V3D(m_pt[0] * v.m_pt[0], m_pt[1] * v.m_pt[1], m_pt[2] * v.m_pt[2]);
70 }
71
77 constexpr V3D operator/(const V3D &v) const noexcept {
78 return V3D(m_pt[0] / v.m_pt[0], m_pt[1] / v.m_pt[1], m_pt[2] / v.m_pt[2]);
79 }
80
86 V3D &operator+=(const V3D &v) noexcept {
87 for (size_t i = 0; i < m_pt.size(); ++i) {
88 m_pt[i] += v.m_pt[i];
89 }
90 return *this;
91 }
92
98 V3D &operator-=(const V3D &v) noexcept {
99 for (size_t i = 0; i < m_pt.size(); ++i) {
100 m_pt[i] -= v.m_pt[i];
101 }
102 return *this;
103 }
104
110 V3D &operator*=(const V3D &v) noexcept {
111 for (size_t i = 0; i < m_pt.size(); ++i) {
112 m_pt[i] *= v.m_pt[i];
113 }
114 return *this;
115 }
116
122 V3D &operator/=(const V3D &v) noexcept {
123 for (size_t i = 0; i < m_pt.size(); ++i) {
124 m_pt[i] /= v.m_pt[i];
125 }
126 return *this;
127 }
128
134 constexpr V3D operator*(const double D) const noexcept { return V3D(m_pt[0] * D, m_pt[1] * D, m_pt[2] * D); }
135
141 constexpr V3D operator/(const double D) const noexcept { return V3D(m_pt[0] / D, m_pt[1] / D, m_pt[2] / D); }
142
148 V3D &operator*=(const double D) noexcept {
149 std::for_each(m_pt.begin(), m_pt.end(), [D](auto &pt) { pt *= D; });
150 return *this;
151 }
152
159 V3D &operator/=(const double D) noexcept {
160 std::for_each(m_pt.begin(), m_pt.end(), [D](auto &pt) { pt /= D; });
161 return *this;
162 }
163
168 constexpr V3D operator-() const noexcept { return V3D(-m_pt[0], -m_pt[1], -m_pt[2]); }
169
175 bool operator==(const V3D &v) const noexcept {
176 return !(std::abs(m_pt[0] - v.m_pt[0]) > Tolerance || std::abs(m_pt[1] - v.m_pt[1]) > Tolerance ||
177 std::abs(m_pt[2] - v.m_pt[2]) > Tolerance);
178 }
179
184 bool operator!=(const V3D &other) const noexcept { return !(this->operator==(other)); }
185
190 constexpr bool operator<(const V3D &V) const noexcept {
191 if (m_pt[0] != V.m_pt[0])
192 return m_pt[0] < V.m_pt[0];
193 if (m_pt[1] != V.m_pt[1])
194 return m_pt[1] < V.m_pt[1];
195 return m_pt[2] < V.m_pt[2];
196 }
197
199 constexpr bool operator>(const V3D &rhs) const noexcept { return rhs < *this; }
200
207 void operator()(const double xx, const double yy, const double zz) noexcept { m_pt = {{xx, yy, zz}}; }
208
209 // Access
210 // Setting x, y and z values
211 void spherical(const double R, const double theta, const double phi) noexcept;
212 void spherical_rad(const double R, const double polar, const double azimuth) noexcept;
213 void azimuth_polar_SNS(const double R, const double azimuth, const double polar) noexcept;
218 void setX(const double xx) noexcept { m_pt[0] = xx; }
219
224 void setY(const double yy) noexcept { m_pt[1] = yy; }
225
230 void setZ(const double zz) noexcept { m_pt[2] = zz; }
231
232 constexpr double X() const noexcept { return m_pt[0]; }
233 constexpr double Y() const noexcept { return m_pt[1]; }
234 constexpr double Z() const noexcept { return m_pt[2]; }
235
241 constexpr double operator[](const size_t index) const noexcept {
242 assert(index < m_pt.size());
243 return m_pt[index];
244 }
245
251 double &operator[](const size_t index) noexcept {
252 assert(index < m_pt.size());
253 return m_pt[index];
254 }
255
256 void getSpherical(double &R, double &theta, double &phi) const noexcept;
257
258 void rotate(const Matrix<double> &) noexcept;
259
260 void round() noexcept;
262 double normalize();
263 double norm() const noexcept { return sqrt(norm2()); }
265 constexpr double norm2() const noexcept { return m_pt[0] * m_pt[0] + m_pt[1] * m_pt[1] + m_pt[2] * m_pt[2]; }
268 double toMillerIndexes(double eps = 1.e-3);
274 constexpr double scalar_prod(const V3D &v) const noexcept {
275 return m_pt[0] * v.m_pt[0] + m_pt[1] * v.m_pt[1] + m_pt[2] * v.m_pt[2];
276 }
278 constexpr V3D cross_prod(const V3D &v) const noexcept {
279 return V3D(m_pt[1] * v.m_pt[2] - m_pt[2] * v.m_pt[1], m_pt[2] * v.m_pt[0] - m_pt[0] * v.m_pt[2],
280 m_pt[0] * v.m_pt[1] - m_pt[1] * v.m_pt[0]);
281 }
287 double distance(const V3D &v) const noexcept { return (*this - v).norm(); }
289 double zenith(const V3D &) const noexcept;
291 double angle(const V3D &) const;
293 double cosAngle(const V3D &) const;
295 V3D directionAngles(bool inDegrees = true) const;
297 int maxCoeff();
299 V3D absoluteValue() const;
301 double hklError() const;
302
303 // Make 2 vectors into 3 orthogonal vectors
304 static std::vector<V3D> makeVectorsOrthogonal(const std::vector<V3D> &vectors);
305
306 // Send to a stream
307 void printSelf(std::ostream &) const;
308 void readPrinted(std::istream &);
309 void read(std::istream &);
310 void write(std::ostream &) const;
311 std::string toString() const;
312 void fromString(const std::string &str);
313
315 double volume() const noexcept { return std::abs(m_pt[0] * m_pt[1] * m_pt[2]); }
317 int reBase(const V3D &, const V3D &, const V3D &) noexcept;
319 int masterDir(const double Tol = 1e-3) const noexcept;
321 bool nullVector(const double tolerance = 1e-3) const noexcept;
322 bool unitVector(const double tolerance = Kernel::Tolerance) const noexcept;
323 bool coLinear(const V3D &, const V3D &) const noexcept;
324
325 void saveNexus(::NeXus::File *file, const std::string &name) const;
326 void loadNexus(::NeXus::File *file, const std::string &name);
327
328private:
329 std::array<double, 3> m_pt;
330};
331
332// Overload operator <<
333MANTID_KERNEL_DLL std::ostream &operator<<(std::ostream &, const V3D &);
334MANTID_KERNEL_DLL std::istream &operator>>(std::istream &, V3D &);
335
341inline MANTID_KERNEL_DLL V3D normalize(V3D v) {
342 v.normalize();
343 return v;
344}
345
346} // Namespace Kernel
347} // Namespace Mantid
const std::vector< double > & rhs
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
double tolerance
Numerical Matrix class.
Definition: Matrix.h:42
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
constexpr bool operator<(const V3D &V) const noexcept
compare
Definition: V3D.h:190
void operator()(const double xx, const double yy, const double zz) noexcept
Sets the vector position from a triplet of doubles x,y,z.
Definition: V3D.h:207
std::array< double, 3 > m_pt
Definition: V3D.h:329
constexpr V3D operator*(const double D) const noexcept
Scalar product.
Definition: V3D.h:134
constexpr V3D operator/(const V3D &v) const noexcept
Inner division.
Definition: V3D.h:77
V3D & operator*=(const double D) noexcept
Scalar product.
Definition: V3D.h:148
constexpr double operator[](const size_t index) const noexcept
Returns the axis value based in the index provided.
Definition: V3D.h:241
constexpr double scalar_prod(const V3D &v) const noexcept
Calculates the cross product.
Definition: V3D.h:274
V3D & operator*=(const V3D &v) noexcept
Self-Inner product.
Definition: V3D.h:110
constexpr double X() const noexcept
Get x.
Definition: V3D.h:232
V3D & operator/=(const V3D &v) noexcept
Self-Inner division.
Definition: V3D.h:122
double normalize()
Make a normalized vector (return norm value)
Definition: V3D.cpp:130
constexpr V3D cross_prod(const V3D &v) const noexcept
Cross product (this * argument)
Definition: V3D.h:278
constexpr V3D operator-() const noexcept
Negation.
Definition: V3D.h:168
constexpr double Y() const noexcept
Get y.
Definition: V3D.h:233
void setZ(const double zz) noexcept
Set is z position.
Definition: V3D.h:230
V3D & operator+=(const V3D &v) noexcept
Self-Addition operator.
Definition: V3D.h:86
constexpr double norm2() const noexcept
Vector length squared.
Definition: V3D.h:265
double volume() const noexcept
Calculate the volume of a cube X*Y*Z.
Definition: V3D.h:315
constexpr bool operator>(const V3D &rhs) const noexcept
Comparison operator greater than.
Definition: V3D.h:199
double & operator[](const size_t index) noexcept
Returns the axis value based in the index provided.
Definition: V3D.h:251
void setX(const double xx) noexcept
Set is x position.
Definition: V3D.h:218
constexpr V3D operator*(const V3D &v) const noexcept
Inner product.
Definition: V3D.h:68
V3D & operator-=(const V3D &v) noexcept
Self-Subtraction operator.
Definition: V3D.h:98
bool operator==(const V3D &v) const noexcept
Equals operator with tolerance factor.
Definition: V3D.h:175
constexpr V3D operator-(const V3D &v) const noexcept
Subtraction operator.
Definition: V3D.h:59
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
constexpr V3D operator/(const double D) const noexcept
Scalar divsion.
Definition: V3D.h:141
constexpr V3D() noexcept
Definition: V3D.h:36
constexpr V3D(double xx, double yy, double zz) noexcept
Definition: V3D.h:37
V3D & operator/=(const double D) noexcept
Scalar division.
Definition: V3D.h:159
bool operator!=(const V3D &other) const noexcept
Not equals operator with tolerance factor.
Definition: V3D.h:184
constexpr V3D operator+(const V3D &v) const noexcept
Addtion operator.
Definition: V3D.h:50
constexpr double Tolerance
Standard tolerance value.
Definition: Tolerance.h:12
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
constexpr bool operator==(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)