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