MeshLib
 
Loading...
Searching...
No Matches
MRVector4.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include "MRVector3.h"
5
6namespace MR
7{
8
11template <typename T>
12struct Vector4
13{
14 using ValueType = T;
17 static constexpr int elements = 4;
18
19 T x, y, z, w;
20
21 constexpr Vector4() noexcept : x( 0 ), y( 0 ), z( 0 ), w( 0 ) { }
22 explicit Vector4( NoInit ) noexcept { }
23 constexpr Vector4( T x, T y, T z, T w ) noexcept : x( x ), y( y ), z( z ), w( w ) { }
24 static constexpr Vector4 diagonal( T a ) noexcept
25 {
26 return Vector4( a, a, a, a );
27 }
28 template <typename U>
29 constexpr explicit Vector4( const Vector4<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ), z( T( v.z ) ), w( T( v.w ) )
30 {
31 }
32
33 constexpr const T & operator []( int e ) const noexcept { return *( &x + e ); }
34 constexpr T & operator []( int e ) noexcept { return *( &x + e ); }
35
36 T lengthSq() const
37 {
38 return x * x + y * y + z * z + w * w;
39 }
40 auto length() const
41 {
42 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
43 // Returning `auto` to not break on integral types.
44 using std::sqrt;
45 return sqrt( lengthSq() );
46 }
47
48 Vector4 normalized() const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
49 {
50 auto len = length();
51 if ( len <= 0 )
52 return {};
53 return ( 1 / len ) * ( *this );
54 }
55
56 Vector4 operator -() const { return Vector4( -x, -y, -z, -w ); }
57 const Vector4 & operator +() const { return *this; }
58
60 {
61 x += b.x; y += b.y; z += b.z; w += b.w; return *this;
62 }
64 {
65 x -= b.x; y -= b.y; z -= b.z; w -= b.w; return *this;
66 }
67 Vector4 & operator *=( T b ) { x *= b; y *= b; z *= b; w *= b; return * this; }
69 {
70 if constexpr ( std::is_integral_v<T> )
71 { x /= b; y /= b; z /= b; w /= b; return * this; }
72 else
73 return *this *= ( 1 / b );
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
90
91template <typename T>
92inline bool operator ==( const Vector4<T> & a, const Vector4<T> & b )
93{
94 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
95}
96
97template <typename T>
98inline bool operator !=( const Vector4<T> & a, const Vector4<T> & b )
99{
100 return !( a == b );
101}
102
103template <typename T>
104inline Vector4<T> operator +( const Vector4<T> & a, const Vector4<T> & b )
105{
106 return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
107}
108
109template <typename T>
110inline Vector4<T> operator -( const Vector4<T> & a, const Vector4<T> & b )
111{
112 return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
113}
114
115template <typename T>
116inline Vector4<T> operator *( T a, const Vector4<T> & b )
117{
118 return {a * b.x, a * b.y, a * b.z, a * b.w};
119}
120
121template <typename T>
122inline Vector4<T> operator *( const Vector4<T> & b, T a )
123{
124 return {a * b.x, a * b.y, a * b.z, a * b.w};
125}
126
127template <typename T>
128inline Vector4<T> operator /( Vector4<T> b, T a )
129 { b /= a; return b; }
130
131
133template <typename T>
134inline T dot( const Vector4<T>& a, const Vector4<T>& b )
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
161template <typename T>
162inline auto begin( const Vector4<T> & v ) { return &v[0]; }
163template <typename T>
164inline auto begin( Vector4<T> & v ) { return &v[0]; }
165
166template <typename T>
167inline auto end( const Vector4<T> & v ) { return &v[4]; }
168template <typename T>
169inline auto end( Vector4<T> & v ) { return &v[4]; }
170
172
173} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:26
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:325
auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:263
MRMESH_API bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:265
bool operator!=(const SetBitIteratorT< T > &a, const SetBitIteratorT< T > &b)
Definition MRMesh/MRBitSet.h:259
Definition MRCameraOrientationPlugin.h:7
Color operator/(const Color &b, float a)
Definition MRColor.h:128
Color operator*(float a, const Color &b)
Definition MRColor.h:118
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:108
Definition MRMatrix4.h:14
Definition MRMesh/MRMeshFwd.h:56
Definition MRSymMatrix4.h:13
Definition MRMesh/MRVector3.h:19
Definition MRVector4.h:13
Vector4 & operator*=(T b)
Definition MRVector4.h:67
T y
Definition MRVector4.h:19
T z
Definition MRVector4.h:19
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
const Vector4 & operator+() const
Definition MRVector4.h:57
T x
Definition MRVector4.h:19
T sqr(const Vector4< T > &a)
squared length
Definition MRVector4.h:141
T w
Definition MRVector4.h:19
T lengthSq() const
Definition MRVector4.h:36
Vector4(NoInit) noexcept
Definition MRVector4.h:22
T ValueType
Definition MRVector4.h:14
Vector4 & operator+=(const Vector4< T > &b)
Definition MRVector4.h:59
Vector4< T > div(const Vector4< T > &a, const Vector4< T > &b)
per component division
Definition MRVector4.h:155
Vector4 & operator-=(const Vector4< T > &b)
Definition MRVector4.h:63
constexpr Vector4(T x, T y, T z, T w) noexcept
Definition MRVector4.h:23
constexpr Vector4(const Vector4< U > &v) noexcept
Definition MRVector4.h:29
Vector4 operator-() const
Definition MRVector4.h:56
Vector4< T > mult(const Vector4< T > &a, const Vector4< T > &b)
per component multiplication
Definition MRVector4.h:148
Vector4 & operator/=(T b)
Definition MRVector4.h:68
auto length() const
Definition MRVector4.h:40
constexpr const T & operator[](int e) const noexcept
Definition MRVector4.h:33
static constexpr Vector4 diagonal(T a) noexcept
Definition MRVector4.h:24
bool isFinite() const MR_REQUIRES_IF_SUPPORTED(std
Definition MRVector4.h:82
static constexpr int elements
Definition MRVector4.h:17
Vector4 normalized() const MR_REQUIRES_IF_SUPPORTED(!std
Definition MRVector4.h:48
T dot(const Vector4< T > &a, const Vector4< T > &b)
dot product
Definition MRVector4.h:134
constexpr Vector4() noexcept
Definition MRVector4.h:21