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 "MRPch/MRBindingMacros.h"
6#include <cmath>
7#include <algorithm>
8
9namespace MR
10{
11
12#ifdef _MSC_VER
13#pragma warning(push)
14#pragma warning(disable: 4804) // unsafe use of type 'bool' in operation
15#pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
16#endif
17
20
23template <typename T>
24struct Vector2
25{
26 using ValueType = T;
29 static constexpr int elements = 2;
30
31 T x, y;
32
33 constexpr Vector2() noexcept : x( 0 ), y( 0 ) { }
34 explicit Vector2( NoInit ) noexcept { }
35 constexpr Vector2( T x, T y ) noexcept : x( x ), y( y ) { }
36
37 template <typename U> MR_REQUIRES_IF_SUPPORTED( std::constructible_from<T, U> )
38 explicit constexpr Vector2( const Vector3<U> & v ) noexcept : x( v.x ), y( v.y ) { }
39
40 static constexpr Vector2 diagonal( T a ) noexcept { return Vector2( a, a ); }
41 static constexpr Vector2 plusX() noexcept { return Vector2( 1, 0 ); }
42 static constexpr Vector2 plusY() noexcept { return Vector2( 0, 1 ); }
43 static constexpr Vector2 minusX() noexcept { return Vector2( -1, 0 ); }
44 static constexpr Vector2 minusY() noexcept { return Vector2( 0, -1 ); }
45
46 template <typename U>
47 constexpr explicit Vector2( const Vector2<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ) { }
48
49 constexpr const T & operator []( int e ) const noexcept { return *( &x + e ); }
50 constexpr T & operator []( int e ) noexcept { return *( &x + e ); }
51
52 T lengthSq() const { return x * x + y * y; }
53 auto length() const
54 {
55 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
56 // Returning `auto` to not break on integral types.
57 using std::sqrt;
58 return sqrt( lengthSq() );
59 }
60
61 Vector2 normalized() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
62 {
63 auto len = length();
64 if ( len <= 0 )
65 return {};
66 return ( 1 / len ) * (*this);
67 }
68
70 Vector2 furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> );
71
73 constexpr Vector2 perpendicular() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> ) { return Vector2{ -y, x }; }
74
75 [[nodiscard]] bool isFinite() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
76 {
77 return std::isfinite( x ) && std::isfinite( y );
78 }
79
80 [[nodiscard]] friend constexpr bool operator ==( const Vector2<T> & a, const Vector2<T> & b ) { return a.x == b.x && a.y == b.y; }
81 [[nodiscard]] friend constexpr bool operator !=( const Vector2<T> & a, const Vector2<T> & b ) { return !( a == b ); }
82
83 // NOTE: We use `std::declval()` in the operators below because libclang 18 in our binding generator is bugged and chokes on decltyping `a.x` and such. TODO fix this when we update libclang.
84
85 [[nodiscard]] friend constexpr const Vector2<T> & operator +( const Vector2<T> & a ) { return a; }
86 [[nodiscard]] friend constexpr auto operator -( const Vector2<T> & a ) -> Vector2<decltype( -std::declval<T>() )> { return { -a.x, -a.y }; }
87
88 [[nodiscard]] friend constexpr auto operator +( const Vector2<T> & a, const Vector2<T> & b ) -> Vector2<decltype( std::declval<T>() + std::declval<T>() )> { return { a.x + b.x, a.y + b.y }; }
89 [[nodiscard]] friend constexpr auto operator -( const Vector2<T> & a, const Vector2<T> & b ) -> Vector2<decltype( std::declval<T>() - std::declval<T>() )> { return { a.x - b.x, a.y - b.y }; }
90 [[nodiscard]] friend constexpr auto operator *( T a, const Vector2<T> & b ) -> Vector2<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y }; }
91 [[nodiscard]] friend constexpr auto operator *( const Vector2<T> & b, T a ) -> Vector2<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y }; }
92 [[nodiscard]] friend constexpr auto operator /( Vector2<T> b, T a ) -> Vector2<decltype( std::declval<T>() / std::declval<T>() )>
93 {
94 if constexpr ( std::is_integral_v<T> )
95 return { b.x / a, b.y / a };
96 else
97 return b * ( 1 / a );
98 }
99
100 friend constexpr Vector2<T> & operator +=( Vector2<T> & a, const Vector2<T> & b ) { a.x += b.x; a.y += b.y; return a; }
101 friend constexpr Vector2<T> & operator -=( Vector2<T> & a, const Vector2<T> & b ) { a.x -= b.x; a.y -= b.y; return a; }
102 friend constexpr Vector2<T> & operator *=( Vector2<T> & a, T b ) { a.x *= b; a.y *= b; return a; }
103 friend constexpr Vector2<T> & operator /=( Vector2<T> & a, T b )
104 {
105 if constexpr ( std::is_integral_v<T> )
106 { a.x /= b; a.y /= b; return a; }
107 else
108 return a *= ( 1 / b );
109 }
110};
111
114
116template <typename T>
117inline T distanceSq( const Vector2<T> & a, const Vector2<T> & b )
118{
119 return ( a - b ).lengthSq();
120}
121
123template <typename T>
124inline T distance( const Vector2<T> & a, const Vector2<T> & b )
125{
126 return ( a - b ).length();
127}
128
130template <typename T>
131inline T cross( const Vector2<T> & a, const Vector2<T> & b )
132{
133 return a.x * b.y - a.y * b.x;
134}
135
137template <typename T>
138inline auto dot( const Vector2<T> & a, const Vector2<T> & b ) -> decltype( a.x * b.x )
139{
140 return a.x * b.x + a.y * b.y;
141}
142
144template <typename T>
145inline T sqr( const Vector2<T> & a )
146{
147 return a.lengthSq();
148}
149
151template <typename T>
152inline Vector2<T> mult( const Vector2<T>& a, const Vector2<T>& b )
153{
154 return { a.x * b.x,a.y * b.y };
155}
156
158template <typename T>
159inline Vector2<T> div( const Vector2<T>& a, const Vector2<T>& b )
160{
161 return { a.x / b.x, a.y / b.y };
162}
163
165template <typename T>
166inline T angle( const Vector2<T> & a, const Vector2<T> & b )
167{
168 return std::atan2( std::abs( cross( a, b ) ), dot( a, b ) );
169 // this version is slower and less precise
170 //return std::acos( std::clamp( dot( a.normalized(), b.normalized() ), T(-1), T(1) ) );
171}
172
173template <typename T>
174inline Vector2<T> Vector2<T>::furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> )
175{
176 using std::abs; // This allows boost.multiprecision numbers.
177 if ( abs( x ) < abs( y ) )
178 return Vector2( 1, 0 );
179 else
180 return Vector2( 0, 1 );
181}
182
183
184// We don't need to bind those functions themselves. This doesn't prevent `__iter__` from being generated for the type.
185
186template <typename T>
187MR_BIND_IGNORE inline auto begin( const Vector2<T> & v ) { return &v[0]; }
188template <typename T>
189MR_BIND_IGNORE inline auto begin( Vector2<T> & v ) { return &v[0]; }
190
191template <typename T>
192MR_BIND_IGNORE inline auto end( const Vector2<T> & v ) { return &v[2]; }
193template <typename T>
194MR_BIND_IGNORE inline auto end( Vector2<T> & v ) { return &v[2]; }
195
197
198#ifdef _MSC_VER
199#pragma warning(pop)
200#endif
201
202} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:31
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:307
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:309
Definition MRCameraOrientationPlugin.h:8
Definition MRMatrix2.h:19
Definition MRMesh/MRMeshFwd.h:89
Definition MRSymMatrix2.h:14
Definition MRVector2.h:25
T x
Definition MRVector2.h:31
Vector2(NoInit) noexcept
Definition MRVector2.h:34
Vector2< T > div(const Vector2< T > &a, const Vector2< T > &b)
per component division
Definition MRVector2.h:159
T cross(const Vector2< T > &a, const Vector2< T > &b)
cross product
Definition MRVector2.h:131
friend constexpr bool operator!=(const Vector2< T > &a, const Vector2< T > &b)
Definition MRVector2.h:81
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:42
constexpr Vector2(const Vector2< U > &v) noexcept
Definition MRVector2.h:47
static constexpr Vector2 minusX() noexcept
Definition MRVector2.h:43
friend constexpr Vector2< T > & operator-=(Vector2< T > &a, const Vector2< T > &b)
Definition MRVector2.h:101
friend constexpr Vector2< T > & operator+=(Vector2< T > &a, const Vector2< T > &b)
Definition MRVector2.h:100
friend constexpr auto operator*(T a, const Vector2< T > &b) -> Vector2< decltype(std::declval< T >() *std::declval< T >())>
Definition MRVector2.h:90
T ValueType
Definition MRVector2.h:26
friend constexpr Vector2< T > & operator/=(Vector2< T > &a, T b)
Definition MRVector2.h:103
T y
Definition MRVector2.h:31
constexpr const T & operator[](int e) const noexcept
Definition MRVector2.h:49
friend constexpr bool operator==(const Vector2< T > &a, const Vector2< T > &b)
Definition MRVector2.h:80
constexpr Vector2() noexcept
Definition MRVector2.h:33
friend constexpr auto operator/(Vector2< T > b, T a) -> Vector2< decltype(std::declval< T >()/std::declval< T >())>
Definition MRVector2.h:92
bool isFinite() const
Definition MRVector2.h:75
static constexpr Vector2 diagonal(T a) noexcept
Definition MRVector2.h:40
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:117
constexpr Vector2 perpendicular() const
returns same length vector orthogonal to this (rotated 90 degrees counter-clockwise)
Definition MRVector2.h:73
T distance(const Vector2< T > &a, const Vector2< T > &b)
distance between two points, better use distanceSq for higher performance
Definition MRVector2.h:124
constexpr Vector2(T x, T y) noexcept
Definition MRVector2.h:35
T sqr(const Vector2< T > &a)
squared length
Definition MRVector2.h:145
static constexpr int elements
Definition MRVector2.h:29
auto length() const
Definition MRVector2.h:53
static constexpr Vector2 plusX() noexcept
Definition MRVector2.h:41
auto dot(const Vector2< T > &a, const Vector2< T > &b) -> decltype(a.x *b.x)
dot product
Definition MRVector2.h:138
Vector2< T > mult(const Vector2< T > &a, const Vector2< T > &b)
per component multiplication
Definition MRVector2.h:152
friend constexpr Vector2< T > & operator*=(Vector2< T > &a, T b)
Definition MRVector2.h:102
Vector2 normalized() const
Definition MRVector2.h:61
friend constexpr auto operator-(const Vector2< T > &a) -> Vector2< decltype(-std::declval< T >())>
Definition MRVector2.h:86
T lengthSq() const
Definition MRVector2.h:52
static constexpr Vector2 minusY() noexcept
Definition MRVector2.h:44
friend constexpr const Vector2< T > & operator+(const Vector2< T > &a)
Definition MRVector2.h:85
T angle(const Vector2< T > &a, const Vector2< T > &b)
angle in radians between two vectors
Definition MRVector2.h:166
Definition MRMesh/MRVector3.h:26