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 <type_traits>
5#include "MRPch/MRBindingMacros.h"
6#include "MRMesh/MRMacros.h"
7#include "MRVector3.h"
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
20template <typename T>
21struct Vector4
22{
23 using ValueType = T;
26 static constexpr int elements = 4;
27
28 T x, y, z, w;
29
30 constexpr Vector4() noexcept : x( 0 ), y( 0 ), z( 0 ), w( 0 )
31 {
32 static_assert( sizeof( Vector4<ValueType> ) == elements * sizeof( ValueType ), "Struct size invalid" );
33 static_assert( elements == 4, "Invalid number of elements" );
34 }
35 explicit Vector4( NoInit ) noexcept { }
36 constexpr Vector4( T x, T y, T z, T w ) noexcept : x( x ), y( y ), z( z ), w( w ) { }
37 static constexpr Vector4 diagonal( T a ) noexcept
38 {
39 return Vector4( a, a, a, a );
40 }
41
42 // 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
43 // 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.
44 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
45 constexpr explicit Vector4( const Vector4<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ), z( T( v.z ) ), w( T( v.w ) )
46 {
47 }
48
49 constexpr const T & operator []( int e ) const noexcept { return *( ( ValueType *)this + e ); }
50 constexpr T & operator []( int e ) noexcept { return *( ( ValueType* )this + e ); }
51
52 T lengthSq() const
53 {
54 return x * x + y * y + z * z + w * w;
55 }
56 auto length() const
57 {
58 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
59 // Returning `auto` to not break on integral types.
60 using std::sqrt;
61 return sqrt( lengthSq() );
62 }
63
64 Vector4 normalized() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
65 {
66 auto len = length();
67 if ( len <= 0 )
68 return {};
69 return ( 1 / len ) * ( *this );
70 }
71
73 Vector3<T> proj3d() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
74 {
75 return { x / w, y / w, z / w };
76 }
77
78 [[nodiscard]] bool isFinite() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
79 {
80 return std::isfinite( x ) && std::isfinite( y ) && std::isfinite( z ) && std::isfinite( w );
81 }
82
83 [[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; }
84 [[nodiscard]] friend constexpr bool operator !=( const Vector4<T> & a, const Vector4<T> & b ) { return !( a == b ); }
85
86 // 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.
87
88 [[nodiscard]] friend constexpr const Vector4<T> & operator +( const Vector4<T> & a ) { return a; }
89 [[nodiscard]] friend constexpr auto operator -( const Vector4<T> & a ) -> Vector4<decltype( -std::declval<T>() )> { return { -a.x, -a.y, -a.z, -a.w }; }
90
91 [[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 }; }
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 *( 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 }; }
94 [[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 }; }
95 [[nodiscard]] friend constexpr auto operator /( Vector4<T> b, T a ) -> Vector4<decltype( std::declval<T>() / std::declval<T>() )>
96 {
97 if constexpr ( std::is_integral_v<T> )
98 return { b.x / a, b.y / a, b.z / a, b.w / a };
99 else
100 return b * ( 1 / a );
101 }
102
103 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; }
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, T b ) { a.x *= b; a.y *= b; a.z *= b; a.w *= b; return a; }
106 friend constexpr Vector4<T> & operator /=( Vector4<T> & a, T b )
107 {
108 if constexpr ( std::is_integral_v<T> )
109 { a.x /= b; a.y /= b; a.z /= b; a.w /= b; return a; }
110 else
111 return a *= ( 1 / b );
112 }
113};
114
117
119template <typename T>
120inline T distanceSq( const Vector4<T> & a, const Vector4<T> & b )
121{
122 return ( a - b ).lengthSq();
123}
124
126template <typename T>
127inline T distance( const Vector4<T> & a, const Vector4<T> & b )
128{
129 return ( a - b ).length();
130}
131
133template <typename T>
134inline auto dot( const Vector4<T> & a, const Vector4<T> & b ) -> decltype( a.x * b.x )
135{
136 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
137}
138
140template <typename T>
141inline T sqr( const Vector4<T> & a )
142{
143 return a.lengthSq();
144}
145
147template <typename T>
148inline Vector4<T> mult( const Vector4<T>& a, const Vector4<T>& b )
149{
150 return { a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w };
151}
152
154template <typename T>
155inline Vector4<T> div( const Vector4<T>& a, const Vector4<T>& b )
156{
157 return { a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w };
158}
159
160
161// We don't need to bind those functions themselves. This doesn't prevent `__iter__` from being generated for the type.
162
163template <typename T>
164MR_BIND_IGNORE auto begin( const Vector4<T> & v ) { return &v[0]; }
165template <typename T>
166MR_BIND_IGNORE auto begin( Vector4<T> & v ) { return &v[0]; }
167
168template <typename T>
169MR_BIND_IGNORE auto end( const Vector4<T> & v ) { return &v[4]; }
170template <typename T>
171MR_BIND_IGNORE auto end( Vector4<T> & v ) { return &v[4]; }
172
174
175#ifdef _MSC_VER
176#pragma warning(pop)
177#endif
178
179} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:308
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:310
Definition MRCameraOrientationPlugin.h:8
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:10
Definition MRMatrix4.h:20
Definition MRMesh/MRMeshFwd.h:90
Definition MRSymMatrix4.h:13
Definition MRMesh/MRVector3.h:29
Definition MRVector4.h:22
friend constexpr Vector4< T > & operator*=(Vector4< T > &a, T b)
Definition MRVector4.h:105
friend constexpr bool operator!=(const Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:84
T y
Definition MRVector4.h:28
T z
Definition MRVector4.h:28
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:73
T x
Definition MRVector4.h:28
friend constexpr Vector4< T > & operator-=(Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:104
T sqr(const Vector4< T > &a)
squared length
Definition MRVector4.h:141
T w
Definition MRVector4.h:28
T lengthSq() const
Definition MRVector4.h:52
Vector4(NoInit) noexcept
Definition MRVector4.h:35
friend constexpr const Vector4< T > & operator+(const Vector4< T > &a)
Definition MRVector4.h:88
friend constexpr auto operator-(const Vector4< T > &a) -> Vector4< decltype(-std::declval< T >())>
Definition MRVector4.h:89
T ValueType
Definition MRVector4.h:23
Vector4< T > div(const Vector4< T > &a, const Vector4< T > &b)
per component division
Definition MRVector4.h:155
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Vector4(const Vector4< U > &v) noexcept
Definition MRVector4.h:44
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:120
constexpr Vector4(T x, T y, T z, T w) noexcept
Definition MRVector4.h:36
auto dot(const Vector4< T > &a, const Vector4< T > &b) -> decltype(a.x *b.x)
dot product
Definition MRVector4.h:134
Vector4< T > mult(const Vector4< T > &a, const Vector4< T > &b)
per component multiplication
Definition MRVector4.h:148
friend constexpr bool operator==(const Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:83
auto length() const
Definition MRVector4.h:56
constexpr const T & operator[](int e) const noexcept
Definition MRVector4.h:49
static constexpr Vector4 diagonal(T a) noexcept
Definition MRVector4.h:37
bool isFinite() const MR_REQUIRES_IF_SUPPORTED(std
Definition MRVector4.h:78
friend constexpr auto operator/(Vector4< T > b, T a) -> Vector4< decltype(std::declval< T >()/std::declval< T >())>
Definition MRVector4.h:95
friend constexpr Vector4< T > & operator/=(Vector4< T > &a, T b)
Definition MRVector4.h:106
static constexpr int elements
Definition MRVector4.h:26
friend constexpr Vector4< T > & operator+=(Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:103
Vector4 normalized() const MR_REQUIRES_IF_SUPPORTED(!std
Definition MRVector4.h:64
constexpr Vector4() noexcept
Definition MRVector4.h:30
friend constexpr auto operator*(T a, const Vector4< T > &b) -> Vector4< decltype(std::declval< T >() *std::declval< T >())>
Definition MRVector4.h:93
T distance(const Vector4< T > &a, const Vector4< T > &b)
distance between two points, better use distanceSq for higher performance
Definition MRVector4.h:127