MeshLib C++ Docs
Loading...
Searching...
No Matches
MRVector4.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <iosfwd>
5#include <type_traits>
6#include "MRPch/MRBindingMacros.h"
7#include "MRMesh/MRMacros.h"
8#include "MRVector3.h"
9
10namespace MR
11{
12
13#ifdef _MSC_VER
14#pragma warning(push)
15#pragma warning(disable: 4804) // unsafe use of type 'bool' in operation
16#pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
17#endif
18
21template <typename T>
22struct Vector4
23{
24 using ValueType = T;
27 static constexpr int elements = 4;
28
29 T x, y, z, w;
30
31 constexpr Vector4() noexcept : x( 0 ), y( 0 ), z( 0 ), w( 0 )
32 {
33 static_assert( sizeof( Vector4<ValueType> ) == elements * sizeof( ValueType ), "Struct size invalid" );
34 static_assert( elements == 4, "Invalid number of elements" );
35 }
36 explicit Vector4( NoInit ) noexcept { }
37 constexpr Vector4( T x, T y, T z, T w ) noexcept : x( x ), y( y ), z( z ), w( w ) { }
38 static constexpr Vector4 diagonal( T a ) noexcept
39 {
40 return Vector4( a, a, a, a );
41 }
42
43 // Here `T == U` doesn't seem to cause any issues in the C++ code, but we're still disabling it because it somehow gets emitted
44 // when generating the bindings, and looks out of place there. Specifically for Vector4, it only gets emitted on Windows (but not on Linux) for some reason.
45 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
46 constexpr explicit Vector4( const Vector4<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ), z( T( v.z ) ), w( T( v.w ) )
47 {
48 }
49
50 constexpr const T & operator []( int e ) const noexcept { return *( ( ValueType *)this + e ); }
51 constexpr T & operator []( int e ) noexcept { return *( ( ValueType* )this + e ); }
52
53 T lengthSq() const
54 {
55 return x * x + y * y + z * z + w * w;
56 }
57 auto length() const
58 {
59 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
60 // Returning `auto` to not break on integral types.
61 using std::sqrt;
62 return sqrt( lengthSq() );
63 }
64
65 Vector4 normalized() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
66 {
67 auto len = length();
68 if ( len <= 0 )
69 return {};
70 return ( 1 / len ) * ( *this );
71 }
72
74 Vector3<T> proj3d() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
75 {
76 return { x / w, y / w, z / w };
77 }
78
79 [[nodiscard]] bool isFinite() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
80 {
81 return std::isfinite( x ) && std::isfinite( y ) && std::isfinite( z ) && std::isfinite( w );
82 }
83
84 [[nodiscard]] friend constexpr bool operator ==( const Vector4<T> & a, const Vector4<T> & b ) { return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; }
85 [[nodiscard]] friend constexpr bool operator !=( const Vector4<T> & a, const Vector4<T> & b ) { return !( a == b ); }
86
87 // 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.
88
89 [[nodiscard]] friend constexpr const Vector4<T> & operator +( const Vector4<T> & a ) { return a; }
90 [[nodiscard]] friend constexpr auto operator -( const Vector4<T> & a ) -> Vector4<decltype( -std::declval<T>() )> { return { -a.x, -a.y, -a.z, -a.w }; }
91
92 [[nodiscard]] friend constexpr auto operator +( const Vector4<T> & a, const Vector4<T> & b ) -> Vector4<decltype( std::declval<T>() + std::declval<T>() )> { return { a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w }; }
93 [[nodiscard]] friend constexpr auto operator -( const Vector4<T> & a, const Vector4<T> & b ) -> Vector4<decltype( std::declval<T>() - std::declval<T>() )> { return { a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w }; }
94 [[nodiscard]] friend constexpr auto operator *( T a, const Vector4<T> & b ) -> Vector4<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y, a * b.z, a * b.w }; }
95 [[nodiscard]] friend constexpr auto operator *( const Vector4<T> & b, T a ) -> Vector4<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y, a * b.z, a * b.w }; }
96 [[nodiscard]] friend constexpr auto operator /( Vector4<T> b, T a ) -> Vector4<decltype( std::declval<T>() / std::declval<T>() )>
97 {
98 if constexpr ( std::is_integral_v<T> )
99 return { b.x / a, b.y / a, b.z / a, b.w / a };
100 else
101 return b * ( 1 / a );
102 }
103
104 friend constexpr Vector4<T> & operator +=( Vector4<T> & a, const Vector4<T> & b ) { a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; return a; }
105 friend constexpr Vector4<T> & operator -=( Vector4<T> & a, const Vector4<T> & b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; return a; }
106 friend constexpr Vector4<T> & operator *=( Vector4<T> & a, T b ) { a.x *= b; a.y *= b; a.z *= b; a.w *= b; return a; }
107 friend constexpr Vector4<T> & operator /=( Vector4<T> & a, T b )
108 {
109 if constexpr ( std::is_integral_v<T> )
110 { a.x /= b; a.y /= b; a.z /= b; a.w /= b; return a; }
111 else
112 return a *= ( 1 / b );
113 }
114
115 friend std::ostream& operator<<( std::ostream& s, const Vector4& vec )
116 {
117 return s << vec.x << ' ' << vec.y << ' ' << vec.z << ' ' << vec.w;
118 }
119
120 friend std::istream& operator>>( std::istream& s, Vector4& vec )
121 {
122 return s >> vec.x >> vec.y >> vec.z >> vec.w;
123 }
124};
125
128
130template <typename T>
131inline T distanceSq( const Vector4<T> & a, const Vector4<T> & b )
132{
133 return ( a - b ).lengthSq();
134}
135
137template <typename T>
138inline T distance( const Vector4<T> & a, const Vector4<T> & b )
139{
140 return ( a - b ).length();
141}
142
144template <typename T>
145inline auto dot( const Vector4<T> & a, const Vector4<T> & b ) -> decltype( a.x * b.x )
146{
147 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
148}
149
151template <typename T>
152inline T sqr( const Vector4<T> & a )
153{
154 return a.lengthSq();
155}
156
158template <typename T>
159inline Vector4<T> mult( const Vector4<T>& a, const Vector4<T>& b )
160{
161 return { a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w };
162}
163
165template <typename T>
166inline Vector4<T> div( const Vector4<T>& a, const Vector4<T>& b )
167{
168 return { a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w };
169}
170
171
172// We don't need to bind those functions themselves. This doesn't prevent `__iter__` from being generated for the type.
173
174template <typename T>
175MR_BIND_IGNORE auto begin( const Vector4<T> & v ) { return &v[0]; }
176template <typename T>
177MR_BIND_IGNORE auto begin( Vector4<T> & v ) { return &v[0]; }
178
179template <typename T>
180MR_BIND_IGNORE auto end( const Vector4<T> & v ) { return &v[4]; }
181template <typename T>
182MR_BIND_IGNORE auto end( Vector4<T> & v ) { return &v[4]; }
183
185
186#ifdef _MSC_VER
187#pragma warning(pop)
188#endif
189
190} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:380
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:382
Definition MRCameraOrientationPlugin.h:8
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:10
Definition MRMatrix4.h:22
Definition MRMeshFwd.h:94
Definition MRSymMatrix4.h:13
Definition MRMesh/MRVector3.h:30
Definition MRVector4.h:23
friend constexpr Vector4< T > & operator*=(Vector4< T > &a, T b)
Definition MRVector4.h:106
friend constexpr bool operator!=(const Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:85
T y
Definition MRVector4.h:29
T z
Definition MRVector4.h:29
Vector3< T > proj3d() const MR_REQUIRES_IF_SUPPORTED(!std
assuming this is a point represented in homogeneous 4D coordinates, returns the point as 3D-vector
Definition MRVector4.h:74
T x
Definition MRVector4.h:29
friend constexpr Vector4< T > & operator-=(Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:105
friend std::istream & operator>>(std::istream &s, Vector4 &vec)
Definition MRVector4.h:120
T sqr(const Vector4< T > &a)
squared length
Definition MRVector4.h:152
T w
Definition MRVector4.h:29
T lengthSq() const
Definition MRVector4.h:53
Vector4(NoInit) noexcept
Definition MRVector4.h:36
friend std::ostream & operator<<(std::ostream &s, const Vector4 &vec)
Definition MRVector4.h:115
friend constexpr const Vector4< T > & operator+(const Vector4< T > &a)
Definition MRVector4.h:89
friend constexpr auto operator-(const Vector4< T > &a) -> Vector4< decltype(-std::declval< T >())>
Definition MRVector4.h:90
T ValueType
Definition MRVector4.h:24
Vector4< T > div(const Vector4< T > &a, const Vector4< T > &b)
per component division
Definition MRVector4.h:166
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Vector4(const Vector4< U > &v) noexcept
Definition MRVector4.h:45
T distanceSq(const Vector4< T > &a, const Vector4< T > &b)
squared distance between two points, which is faster to compute than just distance
Definition MRVector4.h:131
constexpr Vector4(T x, T y, T z, T w) noexcept
Definition MRVector4.h:37
auto dot(const Vector4< T > &a, const Vector4< T > &b) -> decltype(a.x *b.x)
dot product
Definition MRVector4.h:145
Vector4< T > mult(const Vector4< T > &a, const Vector4< T > &b)
per component multiplication
Definition MRVector4.h:159
friend constexpr bool operator==(const Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:84
auto length() const
Definition MRVector4.h:57
constexpr const T & operator[](int e) const noexcept
Definition MRVector4.h:50
static constexpr Vector4 diagonal(T a) noexcept
Definition MRVector4.h:38
bool isFinite() const MR_REQUIRES_IF_SUPPORTED(std
Definition MRVector4.h:79
friend constexpr auto operator/(Vector4< T > b, T a) -> Vector4< decltype(std::declval< T >()/std::declval< T >())>
Definition MRVector4.h:96
friend constexpr Vector4< T > & operator/=(Vector4< T > &a, T b)
Definition MRVector4.h:107
static constexpr int elements
Definition MRVector4.h:27
friend constexpr Vector4< T > & operator+=(Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:104
Vector4 normalized() const MR_REQUIRES_IF_SUPPORTED(!std
Definition MRVector4.h:65
constexpr Vector4() noexcept
Definition MRVector4.h:31
friend constexpr auto operator*(T a, const Vector4< T > &b) -> Vector4< decltype(std::declval< T >() *std::declval< T >())>
Definition MRVector4.h:94
T distance(const Vector4< T > &a, const Vector4< T > &b)
distance between two points, better use distanceSq for higher performance
Definition MRVector4.h:138