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"
13#include <algorithm>
14#include <array>
15#include <cassert>
16#include <cmath>
17#include <numeric>
18#include <sstream>
19#include <vector>
20
21namespace Mantid::Nexus {
22class File;
23}
24
25namespace Mantid {
26namespace Kernel {
27template <class T> class Matrix;
35class MANTID_KERNEL_DLL V3D final {
36public:
37 constexpr V3D() noexcept : m_pt({{0., 0., 0.}}) {}
38 constexpr V3D(double xx, double yy, double zz) noexcept : m_pt({{xx, yy, zz}}) {}
39
41 static bool compareMagnitude(const Kernel::V3D &v1, const Kernel::V3D &v2);
42
43 // explicit conversion into vector
44 operator std::vector<double>() const { return std::vector<double>(m_pt.cbegin(), m_pt.cend()); }
45
50 std::size_t size() const noexcept { return m_pt.size(); }
51
57 constexpr V3D operator+(const V3D &v) const noexcept {
58 return V3D(m_pt[0] + v.m_pt[0], m_pt[1] + v.m_pt[1], m_pt[2] + v.m_pt[2]);
59 }
60
66 constexpr V3D operator-(const V3D &v) const noexcept {
67 return V3D(m_pt[0] - v.m_pt[0], m_pt[1] - v.m_pt[1], m_pt[2] - v.m_pt[2]);
68 }
69
75 constexpr V3D operator*(const V3D &v) const noexcept {
76 return V3D(m_pt[0] * v.m_pt[0], m_pt[1] * v.m_pt[1], m_pt[2] * v.m_pt[2]);
77 }
78
84 constexpr V3D operator/(const V3D &v) const noexcept {
85 return V3D(m_pt[0] / v.m_pt[0], m_pt[1] / v.m_pt[1], m_pt[2] / v.m_pt[2]);
86 }
87
93 V3D &operator+=(const V3D &v) noexcept {
94 for (size_t i = 0; i < m_pt.size(); ++i) {
95 m_pt[i] += v.m_pt[i];
96 }
97 return *this;
98 }
99
105 V3D &operator-=(const V3D &v) noexcept {
106 for (size_t i = 0; i < m_pt.size(); ++i) {
107 m_pt[i] -= v.m_pt[i];
108 }
109 return *this;
110 }
111
117 V3D &operator*=(const V3D &v) noexcept {
118 for (size_t i = 0; i < m_pt.size(); ++i) {
119 m_pt[i] *= v.m_pt[i];
120 }
121 return *this;
122 }
123
129 V3D &operator/=(const V3D &v) noexcept {
130 for (size_t i = 0; i < m_pt.size(); ++i) {
131 m_pt[i] /= v.m_pt[i];
132 }
133 return *this;
134 }
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 constexpr V3D operator/(const double D) const noexcept { return V3D(m_pt[0] / D, m_pt[1] / D, m_pt[2] / D); }
149
155 V3D &operator*=(const double D) noexcept {
156 std::for_each(m_pt.begin(), m_pt.end(), [D](auto &pt) { pt *= D; });
157 return *this;
158 }
159
166 V3D &operator/=(const double D) noexcept {
167 std::for_each(m_pt.begin(), m_pt.end(), [D](auto &pt) { pt /= D; });
168 return *this;
169 }
170
175 constexpr V3D operator-() const noexcept { return V3D(-m_pt[0], -m_pt[1], -m_pt[2]); }
176
182 bool operator==(const V3D &v) const noexcept {
183 return !(std::abs(m_pt[0] - v.m_pt[0]) > Tolerance || std::abs(m_pt[1] - v.m_pt[1]) > Tolerance ||
184 std::abs(m_pt[2] - v.m_pt[2]) > Tolerance);
185 }
186
191 bool operator!=(const V3D &other) const noexcept { return !(this->operator==(other)); }
192
197 constexpr bool operator<(const V3D &V) const noexcept {
198 if (m_pt[0] != V.m_pt[0])
199 return m_pt[0] < V.m_pt[0];
200 if (m_pt[1] != V.m_pt[1])
201 return m_pt[1] < V.m_pt[1];
202 return m_pt[2] < V.m_pt[2];
203 }
204
206 constexpr bool operator>(const V3D &rhs) const noexcept { return rhs < *this; }
207
214 void operator()(const double xx, const double yy, const double zz) noexcept { m_pt = {{xx, yy, zz}}; }
215
216 // Access
217 // Setting x, y and z values
218 void spherical(const double R, const double theta, const double phi) noexcept;
219 void spherical_rad(const double R, const double polar, const double azimuth) noexcept;
220 void azimuth_polar_SNS(const double R, const double azimuth, const double polar) noexcept;
225 void setX(const double xx) noexcept { m_pt[0] = xx; }
226
231 void setY(const double yy) noexcept { m_pt[1] = yy; }
232
237 void setZ(const double zz) noexcept { m_pt[2] = zz; }
238
239 constexpr double X() const noexcept { return m_pt[0]; }
240 constexpr double Y() const noexcept { return m_pt[1]; }
241 constexpr double Z() const noexcept { return m_pt[2]; }
242
248 constexpr double operator[](const size_t index) const noexcept {
249 assert(index < m_pt.size());
250 return m_pt[index];
251 }
252
258 double &operator[](const size_t index) noexcept {
259 assert(index < m_pt.size());
260 return m_pt[index];
261 }
262
263 void getSpherical(double &R, double &theta, double &phi) const noexcept;
264
265 void rotate(const Matrix<double> &) noexcept;
266
267 void round() noexcept;
269 double normalize();
270 double norm() const noexcept { return sqrt(norm2()); }
272 constexpr double norm2() const noexcept { return m_pt[0] * m_pt[0] + m_pt[1] * m_pt[1] + m_pt[2] * m_pt[2]; }
275 double toMillerIndexes(double eps = 1.e-3);
281 constexpr double scalar_prod(const V3D &v) const noexcept {
282 return m_pt[0] * v.m_pt[0] + m_pt[1] * v.m_pt[1] + m_pt[2] * v.m_pt[2];
283 }
285 constexpr V3D cross_prod(const V3D &v) const noexcept {
286 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],
287 m_pt[0] * v.m_pt[1] - m_pt[1] * v.m_pt[0]);
288 }
294 double distance(const V3D &v) const noexcept { return (*this - v).norm(); }
296 double zenith(const V3D &) const noexcept;
298 double angle(const V3D &) const;
300 double cosAngle(const V3D &) const;
302 V3D directionAngles(bool inDegrees = true) const;
304 int maxCoeff() const;
306 V3D absoluteValue() const;
308 double hklError() const;
309
310 // Make 2 vectors into 3 orthogonal vectors
311 static std::vector<V3D> makeVectorsOrthogonal(const std::vector<V3D> &vectors);
312
313 // Send to a stream
314 void printSelf(std::ostream &) const;
315 void readPrinted(std::istream &);
316 void read(std::istream &);
317 void write(std::ostream &) const;
318 std::string toString() const;
319 void fromString(const std::string &str);
320
322 double volume() const noexcept { return std::abs(m_pt[0] * m_pt[1] * m_pt[2]); }
324 int reBase(const V3D &, const V3D &, const V3D &) noexcept;
326 int masterDir(const double Tol = 1e-3) const noexcept;
328 bool nullVector(const double tolerance = 1e-3) const noexcept;
329 bool unitVector(const double tolerance = Kernel::Tolerance) const noexcept;
330 bool coLinear(const V3D &, const V3D &) const noexcept;
331
332 void saveNexus(Nexus::File *file, const std::string &name) const;
333 void loadNexus(Nexus::File *file, const std::string &name);
334
338 static bool isnan(V3D const &vec) { return (std::isnan(vec.X()) || std::isnan(vec.Y()) || std::isnan(vec.Z())); }
339
340private:
341 std::array<double, 3> m_pt;
342};
343
344// Overload operator <<
345MANTID_KERNEL_DLL std::ostream &operator<<(std::ostream &, const V3D &);
346MANTID_KERNEL_DLL std::istream &operator>>(std::istream &, V3D &);
347
353inline MANTID_KERNEL_DLL V3D normalize(V3D v) {
354 v.normalize();
355 return v;
356}
357
358} // Namespace Kernel
359} // Namespace Mantid
std::string name
Definition Run.cpp:60
const std::vector< double > & rhs
std::map< DeltaEMode::Type, std::string > index
std::vector< T > const * vec
double tolerance
Numerical Matrix class.
Definition Matrix.h:41
Class for 3D vectors.
Definition V3D.h:35
double distance(const V3D &v) const noexcept
Calculates the distance between two vectors.
Definition V3D.h:294
constexpr bool operator<(const V3D &V) const noexcept
compare
Definition V3D.h:197
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:214
std::array< double, 3 > m_pt
Definition V3D.h:341
std::size_t size() const noexcept
Number of components in V3D.
Definition V3D.h:50
constexpr V3D operator*(const double D) const noexcept
Scalar product.
Definition V3D.h:141
constexpr V3D operator/(const V3D &v) const noexcept
Inner division.
Definition V3D.h:84
V3D & operator*=(const double D) noexcept
Scalar product.
Definition V3D.h:155
constexpr double operator[](const size_t index) const noexcept
Returns the axis value based in the index provided.
Definition V3D.h:248
constexpr double scalar_prod(const V3D &v) const noexcept
Calculates the cross product.
Definition V3D.h:281
V3D & operator*=(const V3D &v) noexcept
Self-Inner product.
Definition V3D.h:117
constexpr double X() const noexcept
Get x.
Definition V3D.h:239
V3D & operator/=(const V3D &v) noexcept
Self-Inner division.
Definition V3D.h:129
double normalize()
Make a normalized vector (return norm value)
Definition V3D.cpp:129
constexpr V3D cross_prod(const V3D &v) const noexcept
Cross product (this * argument)
Definition V3D.h:285
constexpr V3D operator-() const noexcept
Negation.
Definition V3D.h:175
constexpr double Y() const noexcept
Get y.
Definition V3D.h:240
void setZ(const double zz) noexcept
Set is z position.
Definition V3D.h:237
V3D & operator+=(const V3D &v) noexcept
Self-Addition operator.
Definition V3D.h:93
constexpr double norm2() const noexcept
Vector length squared.
Definition V3D.h:272
double volume() const noexcept
Calculate the volume of a cube X*Y*Z.
Definition V3D.h:322
constexpr bool operator>(const V3D &rhs) const noexcept
Comparison operator greater than.
Definition V3D.h:206
double & operator[](const size_t index) noexcept
Returns the axis value based in the index provided.
Definition V3D.h:258
void setX(const double xx) noexcept
Set is x position.
Definition V3D.h:225
constexpr V3D operator*(const V3D &v) const noexcept
Inner product.
Definition V3D.h:75
V3D & operator-=(const V3D &v) noexcept
Self-Subtraction operator.
Definition V3D.h:105
bool operator==(const V3D &v) const noexcept
Equals operator with tolerance factor.
Definition V3D.h:182
constexpr V3D operator-(const V3D &v) const noexcept
Subtraction operator.
Definition V3D.h:66
void setY(const double yy) noexcept
Set is y position.
Definition V3D.h:231
constexpr double Z() const noexcept
Get z.
Definition V3D.h:241
constexpr V3D operator/(const double D) const noexcept
Scalar divsion.
Definition V3D.h:148
constexpr V3D() noexcept
Definition V3D.h:37
constexpr V3D(double xx, double yy, double zz) noexcept
Definition V3D.h:38
V3D & operator/=(const double D) noexcept
Scalar division.
Definition V3D.h:166
bool operator!=(const V3D &other) const noexcept
Not equals operator with tolerance factor.
Definition V3D.h:191
constexpr V3D operator+(const V3D &v) const noexcept
Addtion operator.
Definition V3D.h:57
MANTID_KERNEL_DLL std::ostream & operator<<(std::ostream &, CPUTimer &)
Convenience function to provide for easier debug printing.
Definition CPUTimer.cpp:86
MANTID_KERNEL_DLL std::istream & operator>>(std::istream &, Interpolation &)
Reads in parameter value.
std::string toString(const T &value)
Convert values to strings.
constexpr double Tolerance
Standard tolerance value.
Definition Tolerance.h:12
MANTID_KERNEL_DLL V3D normalize(V3D v)
Normalizes a V3D.
Definition V3D.h:353
Header for a base Nexus::Exception.
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)