MeshLib
 
Loading...
Searching...
No Matches
MRVector2.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMacros.h"
4#include "MRMeshFwd.h"
5#include <cmath>
6#include <algorithm>
7
8namespace MR
9{
10
13
16template <typename T>
17struct Vector2
18{
19 using ValueType = T;
22 static constexpr int elements = 2;
23
24 T x, y;
25
26 constexpr Vector2() noexcept : x( 0 ), y( 0 ) { }
27 explicit Vector2( NoInit ) noexcept { }
28 constexpr Vector2( T x, T y ) noexcept : x( x ), y( y ) { }
29 explicit constexpr Vector2( const Vector3<T> & v ) noexcept : x( v.x ), y( v.y ) { }
30
31 static constexpr Vector2 diagonal( T a ) noexcept { return Vector2( a, a ); }
32 static constexpr Vector2 plusX() noexcept { return Vector2( 1, 0 ); }
33 static constexpr Vector2 plusY() noexcept { return Vector2( 0, 1 ); }
34 static constexpr Vector2 minusX() noexcept { return Vector2( -1, 0 ); }
35 static constexpr Vector2 minusY() noexcept { return Vector2( 0, -1 ); }
36
37 template <typename U>
38 constexpr explicit Vector2( const Vector2<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ) { }
39
40 constexpr const T & operator []( int e ) const noexcept { return *( &x + e ); }
41 constexpr T & operator []( int e ) noexcept { return *( &x + e ); }
42
43 T lengthSq() const { return x * x + y * y; }
44 auto length() const
45 {
46 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
47 // Returning `auto` to not break on integral types.
48 using std::sqrt;
49 return sqrt( lengthSq() );
50 }
51
52 Vector2 normalized() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
53 {
54 auto len = length();
55 if ( len <= 0 )
56 return {};
57 return ( 1 / len ) * (*this);
58 }
59
60 Vector2 operator -() const { return Vector2( -x, -y ); }
61 const Vector2 & operator +() const { return *this; }
62
64 Vector2 furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> );
65
67 Vector2 perpendicular() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> ) { return Vector2{ -y, x }; }
68
69 Vector2 & operator +=( const Vector2<T> & b ) { x += b.x; y += b.y; return * this; }
70 Vector2 & operator -=( const Vector2<T> & b ) { x -= b.x; y -= b.y; return * this; }
71 Vector2 & operator *=( T b ) { x *= b; y *= b; return * this; }
73 {
74 if constexpr ( std::is_integral_v<T> )
75 { x /= b; y /= b; return * this; }
76 else
77 return *this *= ( 1 / b );
78 }
79
80 [[nodiscard]] bool isFinite() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
81 {
82 return std::isfinite( x ) && std::isfinite( y );
83 }
84};
85
88
90template <typename T>
91inline T cross( const Vector2<T> & a, const Vector2<T> & b )
92{
93 return a.x * b.y - a.y * b.x;
94}
95
97template <typename T>
98inline T dot( const Vector2<T> & a, const Vector2<T> & b )
99{
100 return a.x * b.x + a.y * b.y;
101}
102
104template <typename T>
105inline T sqr( const Vector2<T> & a )
106{
107 return a.lengthSq();
108}
109
111template <typename T>
112inline Vector2<T> mult( const Vector2<T>& a, const Vector2<T>& b )
113{
114 return { a.x * b.x,a.y * b.y };
115}
116
118template <typename T>
119inline Vector2<T> div( const Vector2<T>& a, const Vector2<T>& b )
120{
121 return { a.x / b.x, a.y / b.y };
122}
123
125template <typename T>
126inline T angle( const Vector2<T> & a, const Vector2<T> & b )
127{
128 return std::atan2( std::abs( cross( a, b ) ), dot( a, b ) );
129 // this version is slower and less precise
130 //return std::acos( std::clamp( dot( a.normalized(), b.normalized() ), T(-1), T(1) ) );
131}
132
133template <typename T>
134inline Vector2<T> Vector2<T>::furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> )
135{
136 using std::abs; // This allows boost.multiprecision numbers.
137 if ( abs( x ) < abs( y ) )
138 return Vector2( 1, 0 );
139 else
140 return Vector2( 0, 1 );
141}
142
143template <typename T>
144inline bool operator ==( const Vector2<T> & a, const Vector2<T> & b )
145 { return a.x == b.x && a.y == b.y; }
146
147template <typename T>
148inline bool operator !=( const Vector2<T> & a, const Vector2<T> & b )
149 { return !( a == b ); }
150
151template <typename T>
152inline Vector2<T> operator +( const Vector2<T> & a, const Vector2<T> & b )
153 { return { a.x + b.x, a.y + b.y }; }
154
155template <typename T>
156inline Vector2<T> operator -( const Vector2<T> & a, const Vector2<T> & b )
157 { return { a.x - b.x, a.y - b.y }; }
158
159template <typename T>
160inline Vector2<T> operator *( T a, const Vector2<T> & b )
161 { return { a * b.x, a * b.y }; }
162
163template <typename T>
164inline Vector2<T> operator *( const Vector2<T> & b, T a )
165 { return { a * b.x, a * b.y }; }
166
167template <typename T>
168inline Vector2<T> operator /( Vector2<T> b, T a )
169 { b /= a; return b; }
170
171template <typename T>
172inline auto begin( const Vector2<T> & v ) { return &v[0]; }
173template <typename T>
174inline auto begin( Vector2<T> & v ) { return &v[0]; }
175
176template <typename T>
177inline auto end( const Vector2<T> & v ) { return &v[2]; }
178template <typename T>
179inline auto end( Vector2<T> & v ) { return &v[2]; }
180
182
183} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:26
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:325
auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:263
MRMESH_API bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:265
bool operator!=(const SetBitIteratorT< T > &a, const SetBitIteratorT< T > &b)
Definition MRMesh/MRBitSet.h:259
Definition MRCameraOrientationPlugin.h:7
Color operator/(const Color &b, float a)
Definition MRColor.h:128
Color operator*(float a, const Color &b)
Definition MRColor.h:118
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:108
Definition MRMatrix2.h:13
Definition MRMesh/MRMeshFwd.h:56
Definition MRSymMatrix2.h:14
Definition MRVector2.h:18
T x
Definition MRVector2.h:24
Vector2(NoInit) noexcept
Definition MRVector2.h:27
Vector2< T > div(const Vector2< T > &a, const Vector2< T > &b)
per component division
Definition MRVector2.h:119
T cross(const Vector2< T > &a, const Vector2< T > &b)
cross product
Definition MRVector2.h:91
static constexpr Vector2 plusY() noexcept
Definition MRVector2.h:33
constexpr Vector2(const Vector2< U > &v) noexcept
Definition MRVector2.h:38
const Vector2 & operator+() const
Definition MRVector2.h:61
static constexpr Vector2 minusX() noexcept
Definition MRVector2.h:34
T ValueType
Definition MRVector2.h:19
Vector2 furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED(!std Vector2 perpendicular() const MR_REQUIRES_IF_SUPPORTED(!std
returns one of 2 basis unit vector that makes the biggest angle with the direction specified by this
Definition MRVector2.h:67
T y
Definition MRVector2.h:24
Vector2 operator-() const
Definition MRVector2.h:60
constexpr Vector2(const Vector3< T > &v) noexcept
Definition MRVector2.h:29
Vector2 & operator-=(const Vector2< T > &b)
Definition MRVector2.h:70
constexpr const T & operator[](int e) const noexcept
Definition MRVector2.h:40
Vector2 normalized() const MR_REQUIRES_IF_SUPPORTED(!std
Definition MRVector2.h:52
T dot(const Vector2< T > &a, const Vector2< T > &b)
dot product
Definition MRVector2.h:98
constexpr Vector2() noexcept
Definition MRVector2.h:26
static constexpr Vector2 diagonal(T a) noexcept
Definition MRVector2.h:31
Vector2 & operator*=(T b)
Definition MRVector2.h:71
constexpr Vector2(T x, T y) noexcept
Definition MRVector2.h:28
T sqr(const Vector2< T > &a)
squared length
Definition MRVector2.h:105
static constexpr int elements
Definition MRVector2.h:22
auto length() const
Definition MRVector2.h:44
static constexpr Vector2 plusX() noexcept
Definition MRVector2.h:32
Vector2< T > mult(const Vector2< T > &a, const Vector2< T > &b)
per component multiplication
Definition MRVector2.h:112
Vector2 & operator/=(T b)
Definition MRVector2.h:72
T lengthSq() const
Definition MRVector2.h:43
static constexpr Vector2 minusY() noexcept
Definition MRVector2.h:35
T angle(const Vector2< T > &a, const Vector2< T > &b)
angle in radians between two vectors
Definition MRVector2.h:126
bool isFinite() const MR_REQUIRES_IF_SUPPORTED(std
Definition MRVector2.h:80
Vector2 & operator+=(const Vector2< T > &b)
Definition MRVector2.h:69
Definition MRMesh/MRVector3.h:19