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 explicit constexpr Vector2( const Vector3<T> & v ) noexcept : x( v.x ), y( v.y ) { }
37
38 static constexpr Vector2 diagonal( T a ) noexcept { return Vector2( a, a ); }
39 static constexpr Vector2 plusX() noexcept { return Vector2( 1, 0 ); }
40 static constexpr Vector2 plusY() noexcept { return Vector2( 0, 1 ); }
41 static constexpr Vector2 minusX() noexcept { return Vector2( -1, 0 ); }
42 static constexpr Vector2 minusY() noexcept { return Vector2( 0, -1 ); }
43
44 template <typename U>
45 constexpr explicit Vector2( const Vector2<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ) { }
46
47 constexpr const T & operator []( int e ) const noexcept { return *( &x + e ); }
48 constexpr T & operator []( int e ) noexcept { return *( &x + e ); }
49
50 T lengthSq() const { return x * x + y * y; }
51 auto length() const
52 {
53 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
54 // Returning `auto` to not break on integral types.
55 using std::sqrt;
56 return sqrt( lengthSq() );
57 }
58
59 Vector2 normalized() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
60 {
61 auto len = length();
62 if ( len <= 0 )
63 return {};
64 return ( 1 / len ) * (*this);
65 }
66
68 Vector2 furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> );
69
71 constexpr Vector2 perpendicular() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> ) { return Vector2{ -y, x }; }
72
73 [[nodiscard]] bool isFinite() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
74 {
75 return std::isfinite( x ) && std::isfinite( y );
76 }
77
78 [[nodiscard]] friend constexpr bool operator ==( const Vector2<T> & a, const Vector2<T> & b ) { return a.x == b.x && a.y == b.y; }
79 [[nodiscard]] friend constexpr bool operator !=( const Vector2<T> & a, const Vector2<T> & b ) { return !( a == b ); }
80
81 // 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.
82
83 [[nodiscard]] friend constexpr const Vector2<T> & operator +( const Vector2<T> & a ) { return a; }
84 [[nodiscard]] friend constexpr auto operator -( const Vector2<T> & a ) -> Vector2<decltype( -std::declval<T>() )> { return { -a.x, -a.y }; }
85
86 [[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 }; }
87 [[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 }; }
88 [[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 }; }
89 [[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 }; }
90 [[nodiscard]] friend constexpr auto operator /( Vector2<T> b, T a ) -> Vector2<decltype( std::declval<T>() / std::declval<T>() )>
91 {
92 if constexpr ( std::is_integral_v<T> )
93 return { b.x / a, b.y / a };
94 else
95 return b * ( 1 / a );
96 }
97
98 friend constexpr Vector2<T> & operator +=( Vector2<T> & a, const Vector2<T> & b ) MR_REQUIRES_IF_SUPPORTED( requires{ a + b; } ) { a.x += b.x; a.y += b.y; return a; }
99 friend constexpr Vector2<T> & operator -=( Vector2<T> & a, const Vector2<T> & b ) MR_REQUIRES_IF_SUPPORTED( requires{ a - b; } ) { a.x -= b.x; a.y -= b.y; return a; }
100 friend constexpr Vector2<T> & operator *=( Vector2<T> & a, T b ) MR_REQUIRES_IF_SUPPORTED( requires{ a * b; } ) { a.x *= b; a.y *= b; return a; }
101 friend constexpr Vector2<T> & operator /=( Vector2<T> & a, T b ) MR_REQUIRES_IF_SUPPORTED( requires{ a / b; } )
102 {
103 if constexpr ( std::is_integral_v<T> )
104 { a.x /= b; a.y /= b; return a; }
105 else
106 return a *= ( 1 / b );
107 }
108};
109
112
114template <typename T>
115inline T distanceSq( const Vector2<T> & a, const Vector2<T> & b )
116{
117 return ( a - b ).lengthSq();
118}
119
121template <typename T>
122inline T distance( const Vector2<T> & a, const Vector2<T> & b )
123{
124 return ( a - b ).length();
125}
126
128template <typename T>
129inline T cross( const Vector2<T> & a, const Vector2<T> & b )
130{
131 return a.x * b.y - a.y * b.x;
132}
133
135template <typename T>
136inline auto dot( const Vector2<T> & a, const Vector2<T> & b ) -> decltype( a.x * b.x )
137{
138 return a.x * b.x + a.y * b.y;
139}
140
142template <typename T>
143inline T sqr( const Vector2<T> & a )
144{
145 return a.lengthSq();
146}
147
149template <typename T>
150inline Vector2<T> mult( const Vector2<T>& a, const Vector2<T>& b )
151{
152 return { a.x * b.x,a.y * b.y };
153}
154
156template <typename T>
157inline Vector2<T> div( const Vector2<T>& a, const Vector2<T>& b )
158{
159 return { a.x / b.x, a.y / b.y };
160}
161
163template <typename T>
164inline T angle( const Vector2<T> & a, const Vector2<T> & b )
165{
166 return std::atan2( std::abs( cross( a, b ) ), dot( a, b ) );
167 // this version is slower and less precise
168 //return std::acos( std::clamp( dot( a.normalized(), b.normalized() ), T(-1), T(1) ) );
169}
170
171template <typename T>
172inline Vector2<T> Vector2<T>::furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> )
173{
174 using std::abs; // This allows boost.multiprecision numbers.
175 if ( abs( x ) < abs( y ) )
176 return Vector2( 1, 0 );
177 else
178 return Vector2( 0, 1 );
179}
180
181
182// We don't need to bind those functions themselves. This doesn't prevent `__iter__` from being generated for the type.
183
184template <typename T>
185MR_BIND_IGNORE inline auto begin( const Vector2<T> & v ) { return &v[0]; }
186template <typename T>
187MR_BIND_IGNORE inline auto begin( Vector2<T> & v ) { return &v[0]; }
188
189template <typename T>
190MR_BIND_IGNORE inline auto end( const Vector2<T> & v ) { return &v[2]; }
191template <typename T>
192MR_BIND_IGNORE inline auto end( Vector2<T> & v ) { return &v[2]; }
193
195
196#ifdef _MSC_VER
197#pragma warning(pop)
198#endif
199
200} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:31
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:295
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:297
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:157
T cross(const Vector2< T > &a, const Vector2< T > &b)
cross product
Definition MRVector2.h:129
friend constexpr bool operator!=(const Vector2< T > &a, const Vector2< T > &b)
Definition MRVector2.h:79
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:40
constexpr Vector2(const Vector2< U > &v) noexcept
Definition MRVector2.h:45
static constexpr Vector2 minusX() noexcept
Definition MRVector2.h:41
friend constexpr Vector2< T > & operator-=(Vector2< T > &a, const Vector2< T > &b)
Definition MRVector2.h:99
friend constexpr Vector2< T > & operator+=(Vector2< T > &a, const Vector2< T > &b)
Definition MRVector2.h:98
friend constexpr auto operator*(T a, const Vector2< T > &b) -> Vector2< decltype(std::declval< T >() *std::declval< T >())>
Definition MRVector2.h:88
T ValueType
Definition MRVector2.h:26
friend constexpr Vector2< T > & operator/=(Vector2< T > &a, T b)
Definition MRVector2.h:101
T y
Definition MRVector2.h:31
constexpr Vector2(const Vector3< T > &v) noexcept
Definition MRVector2.h:36
constexpr const T & operator[](int e) const noexcept
Definition MRVector2.h:47
friend constexpr bool operator==(const Vector2< T > &a, const Vector2< T > &b)
Definition MRVector2.h:78
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:90
bool isFinite() const
Definition MRVector2.h:73
static constexpr Vector2 diagonal(T a) noexcept
Definition MRVector2.h:38
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:115
constexpr Vector2 perpendicular() const
returns same length vector orthogonal to this (rotated 90 degrees counter-clockwise)
Definition MRVector2.h:71
T distance(const Vector2< T > &a, const Vector2< T > &b)
distance between two points, better use distanceSq for higher performance
Definition MRVector2.h:122
constexpr Vector2(T x, T y) noexcept
Definition MRVector2.h:35
T sqr(const Vector2< T > &a)
squared length
Definition MRVector2.h:143
static constexpr int elements
Definition MRVector2.h:29
auto length() const
Definition MRVector2.h:51
static constexpr Vector2 plusX() noexcept
Definition MRVector2.h:39
auto dot(const Vector2< T > &a, const Vector2< T > &b) -> decltype(a.x *b.x)
dot product
Definition MRVector2.h:136
Vector2< T > mult(const Vector2< T > &a, const Vector2< T > &b)
per component multiplication
Definition MRVector2.h:150
friend constexpr Vector2< T > & operator*=(Vector2< T > &a, T b)
Definition MRVector2.h:100
Vector2 normalized() const
Definition MRVector2.h:59
friend constexpr auto operator-(const Vector2< T > &a) -> Vector2< decltype(-std::declval< T >())>
Definition MRVector2.h:84
T lengthSq() const
Definition MRVector2.h:50
static constexpr Vector2 minusY() noexcept
Definition MRVector2.h:42
friend constexpr const Vector2< T > & operator+(const Vector2< T > &a)
Definition MRVector2.h:83
T angle(const Vector2< T > &a, const Vector2< T > &b)
angle in radians between two vectors
Definition MRVector2.h:164
Definition MRMesh/MRVector3.h:26