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 double d) const noexcept { return V2D(X() / d, Y() / d); }
93 V2D &operator/=(const double d) {
94 X() /= d;
95 Y() /= d;
96 return *this;
97 }
99 inline V2D operator-() const noexcept { return V2D{-X(), -Y()}; }
100
102
105
110 inline bool operator==(const V2D &rhs) const {
111 return (std::fabs(X() - rhs.X()) < std::numeric_limits<double>::epsilon() &&
112 std::fabs(Y() - rhs.Y()) < std::numeric_limits<double>::epsilon());
113 }
119 inline bool operator!=(const V2D &rhs) const { return !(rhs == *this); }
121
123 double normalize();
128 inline double norm() const { return std::sqrt(norm2()); }
129
134 inline double norm2() const { return X() * X() + Y() * Y(); }
135
140 inline double scalar_prod(const V2D &other) const { return X() * other.X() + Y() * other.Y(); }
141
145 inline V3D cross_prod(const V2D &other) const { return V3D(0.0, 0.0, X() * other.Y() - Y() * other.X()); }
146
152 inline double distance(const V2D &other) const { return V2D(X() - other.X(), Y() - other.Y()).norm(); }
153
154 // Angle between this and another vector
155 double angle(const V2D &other) const;
156
157private:
158 // X,Y
159 std::array<double, 2> m_pt;
160};
161
162// Overload operator <<
163MANTID_KERNEL_DLL std::ostream &operator<<(std::ostream &, const V2D &);
164
165} // namespace Kernel
166} // namespace Mantid
const std::vector< double > & rhs
std::map< DeltaEMode::Type, std::string > index
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:159
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:119
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:110
V2D(double x, double y) noexcept
Constructor taking an x and y value.
Definition V2D.h:38
V2D & operator/=(const double d)
Definition V2D.h:93
double distance(const V2D &other) const
Distance (R) between two points defined as vectors.
Definition V2D.h:152
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:140
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:134
V2D operator-() const noexcept
Negate.
Definition V2D.h:99
V2D() noexcept
Default constructor.
Definition V2D.h:34
V3D cross_prod(const V2D &other) const
Cross product.
Definition V2D.h:145
double norm() const
Compute the norm.
Definition V2D.h:128
V2D operator/(const double d) const noexcept
Divide by d.
Definition V2D.h:92
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:352
Helper class which provides the Collimation Length for SANS instruments.