MeshLib C++ Docs
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 distanceSq( const Vector2<T> & a, const Vector2<T> & b )
92{
93 return ( a - b ).lengthSq();
94}
95
97template <typename T>
98inline T distance( const Vector2<T> & a, const Vector2<T> & b )
99{
100 return ( a - b ).length();
101}
102
104template <typename T>
105inline T cross( const Vector2<T> & a, const Vector2<T> & b )
106{
107 return a.x * b.y - a.y * b.x;
108}
109
111template <typename T>
112inline T dot( 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 T sqr( const Vector2<T> & a )
120{
121 return a.lengthSq();
122}
123
125template <typename T>
126inline Vector2<T> mult( const Vector2<T>& a, const Vector2<T>& b )
127{
128 return { a.x * b.x,a.y * b.y };
129}
130
132template <typename T>
133inline Vector2<T> div( const Vector2<T>& a, const Vector2<T>& b )
134{
135 return { a.x / b.x, a.y / b.y };
136}
137
139template <typename T>
140inline T angle( const Vector2<T> & a, const Vector2<T> & b )
141{
142 return std::atan2( std::abs( cross( a, b ) ), dot( a, b ) );
143 // this version is slower and less precise
144 //return std::acos( std::clamp( dot( a.normalized(), b.normalized() ), T(-1), T(1) ) );
145}
146
147template <typename T>
148inline Vector2<T> Vector2<T>::furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> )
149{
150 using std::abs; // This allows boost.multiprecision numbers.
151 if ( abs( x ) < abs( y ) )
152 return Vector2( 1, 0 );
153 else
154 return Vector2( 0, 1 );
155}
156
157template <typename T>
158inline bool operator ==( const Vector2<T> & a, const Vector2<T> & b )
159 { return a.x == b.x && a.y == b.y; }
160
161template <typename T>
162inline bool operator !=( const Vector2<T> & a, const Vector2<T> & b )
163 { return !( a == b ); }
164
165template <typename T>
166inline Vector2<T> operator +( const Vector2<T> & a, const Vector2<T> & b )
167 { return { a.x + b.x, a.y + b.y }; }
168
169template <typename T>
170inline Vector2<T> operator -( const Vector2<T> & a, const Vector2<T> & b )
171 { return { a.x - b.x, a.y - b.y }; }
172
173template <typename T>
174inline Vector2<T> operator *( T a, const Vector2<T> & b )
175 { return { a * b.x, a * b.y }; }
176
177template <typename T>
178inline Vector2<T> operator *( const Vector2<T> & b, T a )
179 { return { a * b.x, a * b.y }; }
180
181template <typename T>
182inline Vector2<T> operator /( Vector2<T> b, T a )
183 { b /= a; return b; }
184
185template <typename T>
186inline auto begin( const Vector2<T> & v ) { return &v[0]; }
187template <typename T>
188inline auto begin( Vector2<T> & v ) { return &v[0]; }
189
190template <typename T>
191inline auto end( const Vector2<T> & v ) { return &v[2]; }
192template <typename T>
193inline auto end( Vector2<T> & v ) { return &v[2]; }
194
196
197} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:29
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:357
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:295
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...
MR_BIND_IGNORE bool operator!=(const SetBitIteratorT< T > &a, const SetBitIteratorT< T > &b)
Definition MRMesh/MRBitSet.h:291
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:297
Color operator/(const Color &b, float a)
Definition MRMesh/MRColor.h:128
Color operator*(float a, const Color &b)
Definition MRMesh/MRColor.h:118
Color operator+(const Color &a, const Color &b)
Definition MRMesh/MRColor.h:108
Definition MRMatrix2.h:13
Definition MRMesh/MRMeshFwd.h:89
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:133
T cross(const Vector2< T > &a, const Vector2< T > &b)
cross product
Definition MRVector2.h:105
Vector2 furthestBasisVector() const
returns one of 2 basis unit vector that makes the biggest angle with the direction specified by this
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
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
T dot(const Vector2< T > &a, const Vector2< T > &b)
dot product
Definition MRVector2.h:112
constexpr Vector2() noexcept
Definition MRVector2.h:26
bool isFinite() const
Definition MRVector2.h:80
static constexpr Vector2 diagonal(T a) noexcept
Definition MRVector2.h:31
Vector2 & operator*=(T b)
Definition MRVector2.h:71
T distanceSq(const Vector2< T > &a, const Vector2< T > &b)
squared distance between two points, which is faster to compute than just distance
Definition MRVector2.h:91
Vector2 perpendicular() const
returns same length vector orthogonal to this (rotated 90 degrees counter-clockwise)
Definition MRVector2.h:67
T distance(const Vector2< T > &a, const Vector2< T > &b)
distance between two points, better use distanceSq for higher performance
Definition MRVector2.h:98
constexpr Vector2(T x, T y) noexcept
Definition MRVector2.h:28
T sqr(const Vector2< T > &a)
squared length
Definition MRVector2.h:119
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:126
Vector2 normalized() const
Definition MRVector2.h:52
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:140
Vector2 & operator+=(const Vector2< T > &b)
Definition MRVector2.h:69
Definition MRMesh/MRVector3.h:19