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{
14
15
16#ifdef _MSC_VER
17#pragma warning(push)
18#pragma warning(disable: 4804)
19#pragma warning(disable: 4146)
20#endif
21
24template <typename T>
25struct Vector4
26{
27 using ValueType = T;
30 static constexpr int elements = 4;
31
32 T x, y, z, w;
33
34 constexpr Vector4() noexcept : x( 0 ), y( 0 ), z( 0 ), w( 0 )
35 {
36 static_assert( sizeof( Vector4<ValueType> ) == elements * sizeof( ValueType ), "Struct size invalid" );
37 static_assert( elements == 4, "Invalid number of elements" );
38 }
39 explicit Vector4( NoInit ) noexcept { }
40 constexpr Vector4( T x, T y, T z, T w ) noexcept : x( x ), y( y ), z( z ), w( w ) { }
41 static constexpr Vector4 diagonal( T a ) noexcept
42 {
43 return Vector4( a, a, a, a );
44 }
45
48 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
49 constexpr explicit Vector4( const Vector4<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ), z( T( v.z ) ), w( T( v.w ) )
50 {
51 }
52
53 constexpr const T & operator []( int e ) const noexcept { return *( ( ValueType *)this + e ); }
54 constexpr T & operator []( int e ) noexcept { return *( ( ValueType* )this + e ); }
55
56 T lengthSq() const
57 {
58 return x * x + y * y + z * z + w * w;
59 }
60 auto length() const
61 {
64 using std::sqrt;
65 return sqrt( lengthSq() );
66 }
67
68 Vector4 normalized() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
69 {
70 auto len = length();
71 if ( len <= 0 )
72 return {};
73 return ( 1 / len ) * ( *this );
74 }
75
77 Vector3<T> proj3d() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
78 {
79 return { x / w, y / w, z / w };
80 }
81
82 [[nodiscard]] bool isFinite() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
83 {
84 return std::isfinite( x ) && std::isfinite( y ) && std::isfinite( z ) && std::isfinite( w );
85 }
86
87 [[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; }
88 [[nodiscard]] friend constexpr bool operator !=( const Vector4<T> & a, const Vector4<T> & b ) { return !( a == b ); }
89
91
92 [[nodiscard]] friend constexpr const Vector4<T> & operator +( const Vector4<T> & a ) { return a; }
93 [[nodiscard]] friend constexpr auto operator -( const Vector4<T> & a ) -> Vector4<decltype( -std::declval<T>() )> { return { -a.x, -a.y, -a.z, -a.w }; }
94
95 [[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 }; }
96 [[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 }; }
97 [[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 }; }
98 [[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 }; }
99 [[nodiscard]] friend constexpr auto operator /( Vector4<T> b, T a ) -> Vector4<decltype( std::declval<T>() / std::declval<T>() )>
100 {
101 if constexpr ( std::is_integral_v<T> )
102 return { b.x / a, b.y / a, b.z / a, b.w / a };
103 else
104 return b * ( 1 / a );
105 }
106
107 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; }
108 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; }
109 friend constexpr Vector4<T> & operator *=( Vector4<T> & a, T b ) { a.x *= b; a.y *= b; a.z *= b; a.w *= b; return a; }
110 friend constexpr Vector4<T> & operator /=( Vector4<T> & a, T b )
111 {
112 if constexpr ( std::is_integral_v<T> )
113 { a.x /= b; a.y /= b; a.z /= b; a.w /= b; return a; }
114 else
115 return a *= ( 1 / b );
116 }
117
118 friend std::ostream& operator<<( std::ostream& s, const Vector4& vec )
119 {
120 return s << vec.x << ' ' << vec.y << ' ' << vec.z << ' ' << vec.w;
121 }
122
123 friend std::istream& operator>>( std::istream& s, Vector4& vec )
124 {
125 return s >> vec.x >> vec.y >> vec.z >> vec.w;
126 }
127};
128
131
133template <typename T>
134inline T distanceSq( const Vector4<T> & a, const Vector4<T> & b )
135{
136 return ( a - b ).lengthSq();
137}
138
140template <typename T>
141inline T distance( const Vector4<T> & a, const Vector4<T> & b )
142{
143 return ( a - b ).length();
144}
145
147template <typename T>
148inline auto dot( const Vector4<T> & a, const Vector4<T> & b ) -> decltype( a.x * b.x )
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 T sqr( const Vector4<T> & a )
156{
157 return a.lengthSq();
158}
159
161template <typename T>
162inline Vector4<T> mult( const Vector4<T>& a, const Vector4<T>& b )
163{
164 return { a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w };
165}
166
168template <typename T>
169inline Vector4<T> div( const Vector4<T>& a, const Vector4<T>& b )
170{
171 return { a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w };
172}
173
174
176
177template <typename T>
178MR_BIND_IGNORE_PY auto begin( const Vector4<T> & v ) { return &v[0]; }
179template <typename T>
180MR_BIND_IGNORE_PY auto begin( Vector4<T> & v ) { return &v[0]; }
181
182template <typename T>
183MR_BIND_IGNORE_PY auto end( const Vector4<T> & v ) { return &v[4]; }
184template <typename T>
185MR_BIND_IGNORE_PY auto end( Vector4<T> & v ) { return &v[4]; }
186
188
189#ifdef _MSC_VER
190#pragma warning(pop)
191#endif
192
193}
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
MR_BIND_IGNORE_PY auto end(const BitSet &)
Definition MRBitSet.h:397
MR_BIND_IGNORE_PY auto begin(const BitSet &a)
Definition MRBitSet.h:395
friend constexpr Vector4< T > & operator*=(Vector4< T > &a, T b)
Definition MRVector4.h:109
friend constexpr bool operator!=(const Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:88
T y
Definition MRVector4.h:32
T z
Definition MRVector4.h:32
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:77
T x
Definition MRVector4.h:32
friend constexpr Vector4< T > & operator-=(Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:108
friend std::istream & operator>>(std::istream &s, Vector4 &vec)
Definition MRVector4.h:123
T sqr(const Vector4< T > &a)
squared length
Definition MRVector4.h:155
T w
Definition MRVector4.h:32
T lengthSq() const
Definition MRVector4.h:56
MR_BIND_IGNORE_PY auto begin(const Vector4< T > &v)
We don't need to bind those functions in Python, because this doesn't prevent __iter__ from being gen...
Definition MRVector4.h:178
Vector4(NoInit) noexcept
Definition MRVector4.h:39
friend std::ostream & operator<<(std::ostream &s, const Vector4 &vec)
Definition MRVector4.h:118
friend constexpr const Vector4< T > & operator+(const Vector4< T > &a)
NOTE: We use std::declval() in the operators below because libclang 18 in our binding generator is bu...
Definition MRVector4.h:92
friend constexpr auto operator-(const Vector4< T > &a) -> Vector4< decltype(-std::declval< T >())>
Definition MRVector4.h:93
T ValueType
Definition MRVector4.h:27
Vector4< T > div(const Vector4< T > &a, const Vector4< T > &b)
per component division
Definition MRVector4.h:169
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Vector4(const Vector4< U > &v) noexcept
Definition MRVector4.h:48
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:134
constexpr Vector4(T x, T y, T z, T w) noexcept
Definition MRVector4.h:40
auto dot(const Vector4< T > &a, const Vector4< T > &b) -> decltype(a.x *b.x)
dot product
Definition MRVector4.h:148
Vector4< T > mult(const Vector4< T > &a, const Vector4< T > &b)
per component multiplication
Definition MRVector4.h:162
friend constexpr bool operator==(const Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:87
auto length() const
Definition MRVector4.h:60
constexpr const T & operator[](int e) const noexcept
Definition MRVector4.h:53
static constexpr Vector4 diagonal(T a) noexcept
Definition MRVector4.h:41
bool isFinite() const MR_REQUIRES_IF_SUPPORTED(std
Definition MRVector4.h:82
friend constexpr auto operator/(Vector4< T > b, T a) -> Vector4< decltype(std::declval< T >()/std::declval< T >())>
Definition MRVector4.h:99
friend constexpr Vector4< T > & operator/=(Vector4< T > &a, T b)
Definition MRVector4.h:110
static constexpr int elements
Definition MRVector4.h:30
friend constexpr Vector4< T > & operator+=(Vector4< T > &a, const Vector4< T > &b)
Definition MRVector4.h:107
Vector4 normalized() const MR_REQUIRES_IF_SUPPORTED(!std
Definition MRVector4.h:68
constexpr Vector4() noexcept
Definition MRVector4.h:34
friend constexpr auto operator*(T a, const Vector4< T > &b) -> Vector4< decltype(std::declval< T >() *std::declval< T >())>
Definition MRVector4.h:97
T distance(const Vector4< T > &a, const Vector4< T > &b)
distance between two points, better use distanceSq for higher performance
Definition MRVector4.h:141
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRMatrix4.h:25
Definition MRSymMatrix4.h:16
Definition MRVector3.h:33
Definition MRVector4.h:26