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 Mantid::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
49 std::size_t size() const noexcept { return m_pt.size(); }
50
56 constexpr V3D operator+(const V3D &v) const noexcept {
57 return V3D(m_pt[0] + v.m_pt[0], m_pt[1] + v.m_pt[1], m_pt[2] + v.m_pt[2]);
58 }
59
65 constexpr V3D operator-(const V3D &v) const noexcept {
66 return V3D(m_pt[0] - v.m_pt[0], m_pt[1] - v.m_pt[1], m_pt[2] - v.m_pt[2]);
67 }
68
74 constexpr V3D operator*(const V3D &v) const noexcept {
75 return V3D(m_pt[0] * v.m_pt[0], m_pt[1] * v.m_pt[1], m_pt[2] * v.m_pt[2]);
76 }
77
83 constexpr V3D operator/(const V3D &v) const noexcept {
84 return V3D(m_pt[0] / v.m_pt[0], m_pt[1] / v.m_pt[1], m_pt[2] / v.m_pt[2]);
85 }
86
92 V3D &operator+=(const V3D &v) noexcept {
93 for (size_t i = 0; i < m_pt.size(); ++i) {
94 m_pt[i] += v.m_pt[i];
95 }
96 return *this;
97 }
98
104 V3D &operator-=(const V3D &v) noexcept {
105 for (size_t i = 0; i < m_pt.size(); ++i) {
106 m_pt[i] -= v.m_pt[i];
107 }
108 return *this;
109 }
110
116 V3D &operator*=(const V3D &v) noexcept {
117 for (size_t i = 0; i < m_pt.size(); ++i) {
118 m_pt[i] *= v.m_pt[i];
119 }
120 return *this;
121 }
122
128 V3D &operator/=(const V3D &v) noexcept {
129 for (size_t i = 0; i < m_pt.size(); ++i) {
130 m_pt[i] /= v.m_pt[i];
131 }
132 return *this;
133 }
134
140 constexpr V3D operator*(const double D) const noexcept { return V3D(m_pt[0] * D, m_pt[1] * D, m_pt[2] * D); }
141
147 constexpr V3D operator/(const double D) const noexcept { return V3D(m_pt[0] / D, m_pt[1] / D, m_pt[2] / D); }
148
154 V3D &operator*=(const double D) noexcept {
155 std::for_each(m_pt.begin(), m_pt.end(), [D](auto &pt) { pt *= D; });
156 return *this;
157 }
158
165 V3D &operator/=(const double D) noexcept {
166 std::for_each(m_pt.begin(), m_pt.end(), [D](auto &pt) { pt /= D; });
167 return *this;
168 }
169
174 constexpr V3D operator-() const noexcept { return V3D(-m_pt[0], -m_pt[1], -m_pt[2]); }
175
181 bool operator==(const V3D &v) const noexcept {
182 return !(std::abs(m_pt[0] - v.m_pt[0]) > Tolerance || std::abs(m_pt[1] - v.m_pt[1]) > Tolerance ||
183 std::abs(m_pt[2] - v.m_pt[2]) > Tolerance);
184 }
185
190 bool operator!=(const V3D &other) const noexcept { return !(this->operator==(other)); }
191
196 constexpr bool operator<(const V3D &V) const noexcept {
197 if (m_pt[0] != V.m_pt[0])
198 return m_pt[0] < V.m_pt[0];
199 if (m_pt[1] != V.m_pt[1])
200 return m_pt[1] < V.m_pt[1];
201 return m_pt[2] < V.m_pt[2];
202 }
203
205 constexpr bool operator>(const V3D &rhs) const noexcept { return rhs < *this; }
206
213 void operator()(const double xx, const double yy, const double zz) noexcept { m_pt = {{xx, yy, zz}}; }
214
215 // Access
216 // Setting x, y and z values
217 void spherical(const double R, const double theta, const double phi) noexcept;
218 void spherical_rad(const double R, const double polar, const double azimuth) noexcept;
219 void azimuth_polar_SNS(const double R, const double azimuth, const double polar) noexcept;
224 void setX(const double xx) noexcept { m_pt[0] = xx; }
225
230 void setY(const double yy) noexcept { m_pt[1] = yy; }
231
236 void setZ(const double zz) noexcept { m_pt[2] = zz; }
237
238 constexpr double X() const noexcept { return m_pt[0]; }
239 constexpr double Y() const noexcept { return m_pt[1]; }
240 constexpr double Z() const noexcept { return m_pt[2]; }
241
247 constexpr double operator[](const size_t index) const noexcept {
248 assert(index < m_pt.size());
249 return m_pt[index];
250 }
251
257 double &operator[](const size_t index) noexcept {
258 assert(index < m_pt.size());
259 return m_pt[index];
260 }
261
262 void getSpherical(double &R, double &theta, double &phi) const noexcept;
263
264 void rotate(const Matrix<double> &) noexcept;
265
266 void round() noexcept;
268 double normalize();
269 double norm() const noexcept { return sqrt(norm2()); }
271 constexpr double norm2() const noexcept { return m_pt[0] * m_pt[0] + m_pt[1] * m_pt[1] + m_pt[2] * m_pt[2]; }
274 double toMillerIndexes(double eps = 1.e-3);
280 constexpr double scalar_prod(const V3D &v) const noexcept {
281 return m_pt[0] * v.m_pt[0] + m_pt[1] * v.m_pt[1] + m_pt[2] * v.m_pt[2];
282 }
284 constexpr V3D cross_prod(const V3D &v) const noexcept {
285 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],
286 m_pt[0] * v.m_pt[1] - m_pt[1] * v.m_pt[0]);
287 }
293 double distance(const V3D &v) const noexcept { return (*this - v).norm(); }
295 double zenith(const V3D &) const noexcept;
297 double angle(const V3D &) const;
299 double cosAngle(const V3D &) const;
301 V3D directionAngles(bool inDegrees = true) const;
303 int maxCoeff() const;
305 V3D absoluteValue() const;
307 double hklError() const;
308
309 // Make 2 vectors into 3 orthogonal vectors
310 static std::vector<V3D> makeVectorsOrthogonal(const std::vector<V3D> &vectors);
311
312 // Send to a stream
313 void printSelf(std::ostream &) const;
314 void readPrinted(std::istream &);
315 void read(std::istream &);
316 void write(std::ostream &) const;
317 std::string toString() const;
318 void fromString(const std::string &str);
319
321 double volume() const noexcept { return std::abs(m_pt[0] * m_pt[1] * m_pt[2]); }
323 int reBase(const V3D &, const V3D &, const V3D &) noexcept;
325 int masterDir(const double Tol = 1e-3) const noexcept;
327 bool nullVector(const double tolerance = 1e-3) const noexcept;
328 bool unitVector(const double tolerance = Kernel::Tolerance) const noexcept;
329 bool coLinear(const V3D &, const V3D &) const noexcept;
330
331 void saveNexus(Nexus::File *file, const std::string &name) const;
332 void loadNexus(Nexus::File *file, const std::string &name);
333
337 static bool isnan(V3D const &vec) { return (std::isnan(vec.X()) || std::isnan(vec.Y()) || std::isnan(vec.Z())); }
338
339private:
340 std::array<double, 3> m_pt;
341};
342
343// Overload operator <<
344MANTID_KERNEL_DLL std::ostream &operator<<(std::ostream &, const V3D &);
345MANTID_KERNEL_DLL std::istream &operator>>(std::istream &, V3D &);
346
352inline MANTID_KERNEL_DLL V3D normalize(V3D v) {
353 v.normalize();
354 return v;
355}
356
357} // Namespace Kernel
358} // 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: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:293
constexpr bool operator<(const V3D &V) const noexcept
compare
Definition V3D.h:196
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:213
std::array< double, 3 > m_pt
Definition V3D.h:340
std::size_t size() const noexcept
Number of components in V3D.
Definition V3D.h:49
constexpr V3D operator*(const double D) const noexcept
Scalar product.
Definition V3D.h:140
constexpr V3D operator/(const V3D &v) const noexcept
Inner division.
Definition V3D.h:83
V3D & operator*=(const double D) noexcept
Scalar product.
Definition V3D.h:154
constexpr double operator[](const size_t index) const noexcept
Returns the axis value based in the index provided.
Definition V3D.h:247
constexpr double scalar_prod(const V3D &v) const noexcept
Calculates the cross product.
Definition V3D.h:280
V3D & operator*=(const V3D &v) noexcept
Self-Inner product.
Definition V3D.h:116
constexpr double X() const noexcept
Get x.
Definition V3D.h:238
V3D & operator/=(const V3D &v) noexcept
Self-Inner division.
Definition V3D.h:128
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:284
constexpr V3D operator-() const noexcept
Negation.
Definition V3D.h:174
constexpr double Y() const noexcept
Get y.
Definition V3D.h:239
void setZ(const double zz) noexcept
Set is z position.
Definition V3D.h:236
V3D & operator+=(const V3D &v) noexcept
Self-Addition operator.
Definition V3D.h:92
constexpr double norm2() const noexcept
Vector length squared.
Definition V3D.h:271
double volume() const noexcept
Calculate the volume of a cube X*Y*Z.
Definition V3D.h:321
constexpr bool operator>(const V3D &rhs) const noexcept
Comparison operator greater than.
Definition V3D.h:205
double & operator[](const size_t index) noexcept
Returns the axis value based in the index provided.
Definition V3D.h:257
void setX(const double xx) noexcept
Set is x position.
Definition V3D.h:224
constexpr V3D operator*(const V3D &v) const noexcept
Inner product.
Definition V3D.h:74
V3D & operator-=(const V3D &v) noexcept
Self-Subtraction operator.
Definition V3D.h:104
bool operator==(const V3D &v) const noexcept
Equals operator with tolerance factor.
Definition V3D.h:181
constexpr V3D operator-(const V3D &v) const noexcept
Subtraction operator.
Definition V3D.h:65
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
constexpr V3D operator/(const double D) const noexcept
Scalar divsion.
Definition V3D.h:147
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:165
bool operator!=(const V3D &other) const noexcept
Not equals operator with tolerance factor.
Definition V3D.h:190
constexpr V3D operator+(const V3D &v) const noexcept
Addtion operator.
Definition V3D.h:56
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:352
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)