Mantid
Loading...
Searching...
No Matches
V2D.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2011 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//-----------------------------------------------------------------------------
10// Includes
11//-----------------------------------------------------------------------------
12#include "MantidKernel/DllConfig.h"
13#include "MantidKernel/V3D.h"
14#include <array>
15#include <cmath>
16#include <iosfwd>
17#include <limits>
18
19namespace Mantid {
20namespace Kernel {
21
29class MANTID_KERNEL_DLL V2D {
30public:
34 inline V2D() noexcept : m_pt{{0.0, 0.0}} {}
38 inline V2D(double x, double y) noexcept : m_pt{{x, y}} {}
39
44 inline double X() const { return m_pt[0]; }
49 inline double Y() const { return m_pt[1]; }
50
55 inline double &X() { return m_pt[0]; }
60 inline double &Y() { return m_pt[1]; }
61
63 inline const double &operator[](const size_t index) const { return m_pt[index]; }
64
68 inline V2D operator+(const V2D &rhs) const { return V2D(X() + rhs.X(), Y() + rhs.Y()); }
70 inline V2D &operator+=(const V2D &rhs) {
71 X() += rhs.X();
72 Y() += rhs.Y();
73 return *this;
74 }
76 inline V2D operator-(const V2D &rhs) const { return V2D(X() - rhs.X(), Y() - rhs.Y()); }
78 inline V2D &operator-=(const V2D &rhs) {
79 X() -= rhs.X();
80 Y() -= rhs.Y();
81 return *this;
82 }
84 inline V2D operator*(const double factor) const { return V2D(X() * factor, Y() * factor); }
86 V2D &operator*=(const double factor) {
87 X() *= factor;
88 Y() *= factor;
89 return *this;
90 }
92 inline V2D operator-() const noexcept { return V2D{-X(), -Y()}; }
93
95
98
103 inline bool operator==(const V2D &rhs) const {
104 return (std::fabs(X() - rhs.X()) < std::numeric_limits<double>::epsilon() &&
105 std::fabs(Y() - rhs.Y()) < std::numeric_limits<double>::epsilon());
106 }
112 inline bool operator!=(const V2D &rhs) const { return !(rhs == *this); }
114
116 double normalize();
121 inline double norm() const { return std::sqrt(norm2()); }
122
127 inline double norm2() const { return X() * X() + Y() * Y(); }
128
133 inline double scalar_prod(const V2D &other) const { return X() * other.X() + Y() * other.Y(); }
134
138 inline V3D cross_prod(const V2D &other) const { return V3D(0.0, 0.0, X() * other.Y() - Y() * other.X()); }
139
145 inline double distance(const V2D &other) const { return V2D(X() - other.X(), Y() - other.Y()).norm(); }
146
147 // Angle between this and another vector
148 double angle(const V2D &other) const;
149
150private:
151 // X,Y
152 std::array<double, 2> m_pt;
153};
154
155// Overload operator <<
156MANTID_KERNEL_DLL std::ostream &operator<<(std::ostream &, const V2D &);
157
158} // namespace Kernel
159} // namespace Mantid
const std::vector< double > & rhs
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
Implements a 2-dimensional vector embedded in a 3D space, i.e.
Definition: V2D.h:29
std::array< double, 2 > m_pt
Definition: V2D.h:152
double Y() const
Y position.
Definition: V2D.h:49
V2D operator*(const double factor) const
Scale and return.
Definition: V2D.h:84
bool operator!=(const V2D &rhs) const
Inequality operator including a tolerance.
Definition: V2D.h:112
double & X()
X position (non-const reference)
Definition: V2D.h:55
V2D & operator-=(const V2D &rhs)
Decrement this by rhs.
Definition: V2D.h:78
bool operator==(const V2D &rhs) const
Equality operator including a tolerance.
Definition: V2D.h:103
V2D(double x, double y) noexcept
Constructor taking an x and y value.
Definition: V2D.h:38
double distance(const V2D &other) const
Distance (R) between two points defined as vectors.
Definition: V2D.h:145
V2D operator+(const V2D &rhs) const
Definition: V2D.h:68
V2D & operator*=(const double factor)
Scale this.
Definition: V2D.h:86
V2D operator-(const V2D &rhs) const
Subtract rhs.
Definition: V2D.h:76
V2D & operator+=(const V2D &rhs)
Increment this vector by rhs.
Definition: V2D.h:70
double & Y()
Y position (non-const)
Definition: V2D.h:60
double scalar_prod(const V2D &other) const
Compute the scalar product with another vector.
Definition: V2D.h:133
const double & operator[](const size_t index) const
Unchecked index access.
Definition: V2D.h:63
double X() const
X position.
Definition: V2D.h:44
double norm2() const
Compute the square of the norm.
Definition: V2D.h:127
V2D operator-() const noexcept
Negate.
Definition: V2D.h:92
V2D() noexcept
Default constructor.
Definition: V2D.h:34
V3D cross_prod(const V2D &other) const
Cross product.
Definition: V2D.h:138
double norm() const
Compute the norm.
Definition: V2D.h:121
Class for 3D vectors.
Definition: V3D.h:34
MANTID_KERNEL_DLL std::ostream & operator<<(std::ostream &, CPUTimer &)
Convenience function to provide for easier debug printing.
Definition: CPUTimer.cpp:86
MANTID_KERNEL_DLL V3D normalize(V3D v)
Normalizes a V3D.
Definition: V3D.h:341
Helper class which provides the Collimation Length for SANS instruments.