MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMesh/MRVector3.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMacros.h"
4#include "MRMeshFwd.h"
5#include "MRConstants.h"
6#include "MRPch/MRBindingMacros.h"
7#include <algorithm>
8#include <cmath>
9#if MR_HAS_REQUIRES
10#include <concepts>
11#endif
12
13namespace MR
14{
15
16#ifdef _MSC_VER
17#pragma warning(push)
18#pragma warning(disable: 4804) // unsafe use of type 'bool' in operation
19#pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
20#endif
21
24template <typename T>
25struct Vector3
26{
27 using ValueType = T;
30 static constexpr int elements = 3;
31
32 T x, y, z;
33
34 constexpr Vector3() noexcept : x( 0 ), y( 0 ), z( 0 ) { }
35 explicit Vector3( NoInit ) noexcept { }
36 constexpr Vector3( T x, T y, T z ) noexcept : x( x ), y( y ), z( z ) { }
37
38 template <typename U> MR_REQUIRES_IF_SUPPORTED( std::constructible_from<T, U> )
39 explicit constexpr Vector3( const Vector2<U> & v ) noexcept : x( v.x ), y( v.y ), z( 0 ) { }
40
41 static constexpr Vector3 diagonal( T a ) noexcept { return Vector3( a, a, a ); }
42 static constexpr Vector3 plusX() noexcept { return Vector3( 1, 0, 0 ); }
43 static constexpr Vector3 plusY() noexcept { return Vector3( 0, 1, 0 ); }
44 static constexpr Vector3 plusZ() noexcept { return Vector3( 0, 0, 1 ); }
45 static constexpr Vector3 minusX() noexcept { return Vector3( -1, 0, 0 ); }
46 static constexpr Vector3 minusY() noexcept { return Vector3( 0, -1, 0 ); }
47 static constexpr Vector3 minusZ() noexcept { return Vector3( 0, 0, -1 ); }
48
49 template <typename U>
50 constexpr explicit Vector3( const Vector3<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ), z( T( v.z ) ) { }
51
52 constexpr const T & operator []( int e ) const noexcept { return *( &x + e ); }
53 constexpr T & operator []( int e ) noexcept { return *( &x + e ); }
54
55 T lengthSq() const { return x * x + y * y + z * z; }
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 [[nodiscard]] Vector3 normalized() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
65 {
66 auto len = length();
67 if ( len <= 0 )
68 return {};
69 return ( 1 / len ) * (*this);
70 }
71
73 Vector3 furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> );
74
77 std::pair<Vector3, Vector3> perpendicular() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> );
78
80 template <MR_SAME_TYPE_TEMPLATE_PARAM(T, TT)> // Need this, otherwise the bindings try to instantiate `AffineXf3` with non-FP arguments.
81 Vector3 transformed( const AffineXf3<TT>* xf ) const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
82 {
83 return xf ? ( *xf )( *this ) : *this;
84 }
85
87 void unsignZeroValues() MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
88 {
89 for ( auto i = 0; i < elements; ++i )
90 if ( (*this)[i] == 0.f && std::signbit( (*this)[i] ) )
91 (*this)[i] = 0.f;
92 }
93
94 [[nodiscard]] bool isFinite() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
95 {
96 return std::isfinite( x ) && std::isfinite( y ) && std::isfinite( z );
97 }
98
99 [[nodiscard]] friend constexpr bool operator ==( const Vector3<T> & a, const Vector3<T> & b ) { return a.x == b.x && a.y == b.y && a.z == b.z; }
100 [[nodiscard]] friend constexpr bool operator !=( const Vector3<T> & a, const Vector3<T> & b ) { return !( a == b ); }
101
102 // 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.
103
104 [[nodiscard]] friend constexpr const Vector3<T> & operator +( const Vector3<T> & a ) { return a; }
105 [[nodiscard]] friend constexpr auto operator -( const Vector3<T> & a ) -> Vector3<decltype( -std::declval<T>() )> { return { -a.x, -a.y, -a.z }; }
106
107 [[nodiscard]] friend constexpr auto operator +( const Vector3<T> & a, const Vector3<T> & b ) -> Vector3<decltype( std::declval<T>() + std::declval<T>() )> { return { a.x + b.x, a.y + b.y, a.z + b.z }; }
108 [[nodiscard]] friend constexpr auto operator -( const Vector3<T> & a, const Vector3<T> & b ) -> Vector3<decltype( std::declval<T>() - std::declval<T>() )> { return { a.x - b.x, a.y - b.y, a.z - b.z }; }
109 [[nodiscard]] friend constexpr auto operator *( T a, const Vector3<T> & b ) -> Vector3<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y, a * b.z }; }
110 [[nodiscard]] friend constexpr auto operator *( const Vector3<T> & b, T a ) -> Vector3<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y, a * b.z }; }
111 [[nodiscard]] friend constexpr auto operator /( Vector3<T> b, T a ) -> Vector3<decltype( std::declval<T>() / std::declval<T>() )>
112 {
113 if constexpr ( std::is_integral_v<T> )
114 return { b.x / a, b.y / a, b.z / a };
115 else
116 return b * ( 1 / a );
117 }
118
119 friend constexpr Vector3<T> & operator +=( Vector3<T> & a, const Vector3<T> & b ) { a.x += b.x; a.y += b.y; a.z += b.z; return a; }
120 friend constexpr Vector3<T> & operator -=( Vector3<T> & a, const Vector3<T> & b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; }
121 friend constexpr Vector3<T> & operator *=( Vector3<T> & a, T b ) { a.x *= b; a.y *= b; a.z *= b; return a; }
122 friend constexpr Vector3<T> & operator /=( Vector3<T> & a, T b )
123 {
124 if constexpr ( std::is_integral_v<T> )
125 { a.x /= b; a.y /= b; a.z /= b; return a; }
126 else
127 return a *= ( 1 / b );
128 }
129};
130
133
134
136template <typename T>
137inline T distanceSq( const Vector3<T> & a, const Vector3<T> & b )
138{
139 return ( a - b ).lengthSq();
140}
141
143template <typename T>
144inline T distance( const Vector3<T> & a, const Vector3<T> & b )
145{
146 return ( a - b ).length();
147}
148
150template <typename T>
151inline Vector3<T> cross( const Vector3<T> & a, const Vector3<T> & b )
152{
153 return {
154 a.y * b.z - a.z * b.y,
155 a.z * b.x - a.x * b.z,
156 a.x * b.y - a.y * b.x
157 };
158}
159
161template <typename T>
162inline auto dot( const Vector3<T> & a, const Vector3<T> & b ) -> decltype( a.x * b.x )
163{
164 return a.x * b.x + a.y * b.y + a.z * b.z;
165}
166
168template <typename T>
169inline T sqr( const Vector3<T> & a )
170{
171 return a.lengthSq();
172}
173
175template <typename T>
176inline T mixed( const Vector3<T> & a, const Vector3<T> & b, const Vector3<T> & c )
177{
178 return dot( a, cross( b, c ) );
179}
180
182template <typename T>
183inline Vector3<T> mult( const Vector3<T>& a, const Vector3<T>& b )
184{
185 return { a.x * b.x,a.y * b.y,a.z * b.z };
186}
187
189template <typename T>
190inline Vector3<T> div( const Vector3<T>& a, const Vector3<T>& b )
191{
192 return { a.x / b.x, a.y / b.y, a.z / b.z };
193}
194
195
198template <typename T>
199inline T angle( const Vector3<T> & a, const Vector3<T> & b )
200{
201 return std::atan2( cross( a, b ).length(), dot( a, b ) );
202 // this version is slower and less precise
203 //return std::acos( std::clamp( dot( a.normalized(), b.normalized() ), T(-1), T(1) ) );
204}
205
206template <typename T>
207inline Vector3<T> Vector3<T>::furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> )
208{
209 using std::abs; // This should allow boost.multiprecision numbers here.
210 if ( abs( x ) < abs( y ) )
211 return ( abs( x ) < abs( z ) ) ? Vector3( 1, 0, 0 ) : Vector3( 0, 0, 1 );
212 else
213 return ( abs( y ) < abs( z ) ) ? Vector3( 0, 1, 0 ) : Vector3( 0, 0, 1 );
214}
215
216template <typename T>
217inline std::pair<Vector3<T>, Vector3<T>> Vector3<T>::perpendicular() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
218{
219 std::pair<Vector3<T>, Vector3<T>> res;
220 auto c1 = furthestBasisVector();
221 res.first = cross( *this, c1 ).normalized();
222 res.second = cross( *this, res.first ).normalized();
223 return res;
224}
225
227template <typename T>
228Vector3<T> unitVector3( T azimuth, T altitude )
229{
230 const auto zenithAngle = T( PI2 ) - altitude;
231 return
232 {
233 std::sin( zenithAngle ) * std::cos( azimuth ),
234 std::sin( zenithAngle ) * std::sin( azimuth ),
235 std::cos( zenithAngle )
236 };
237}
238
239
240// We don't need to bind those functions themselves. This doesn't prevent `__iter__` from being generated for the type.
241
242template <typename T>
243MR_BIND_IGNORE inline auto begin( const Vector3<T> & v ) { return &v[0]; }
244template <typename T>
245MR_BIND_IGNORE inline auto begin( Vector3<T> & v ) { return &v[0]; }
246
247template <typename T>
248MR_BIND_IGNORE inline auto end( const Vector3<T> & v ) { return &v[3]; }
249template <typename T>
250MR_BIND_IGNORE inline auto end( Vector3<T> & v ) { return &v[3]; }
251
253
254#ifdef _MSC_VER
255#pragma warning(pop)
256#endif
257
258} // namespace MR
#define MR_SAME_TYPE_TEMPLATE_PARAM(target_, name_)
Definition MRMacros.h:32
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:31
length
Definition MRObjectDimensionsEnum.h:14
MR_BIND_IGNORE auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:307
MR_BIND_IGNORE auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:309
Definition MRCameraOrientationPlugin.h:8
MRMESH_CLASS Vector3
Definition MRMesh/MRMeshFwd.h:177
Definition MRMesh/MRAffineXf.h:14
Definition MRMesh/MRMatrix3.h:19
Definition MRMesh/MRMeshFwd.h:89
Definition MRSymMatrix3.h:15
Definition MRVector2.h:25
Definition MRMesh/MRVector3.h:26
static constexpr Vector3 plusX() noexcept
Definition MRMesh/MRVector3.h:42
auto dot(const Vector3< T > &a, const Vector3< T > &b) -> decltype(a.x *b.x)
dot product
Definition MRMesh/MRVector3.h:162
Vector3< T > unitVector3(T azimuth, T altitude)
returns a point on unit sphere given two angles
Definition MRMesh/MRVector3.h:228
T distanceSq(const Vector3< T > &a, const Vector3< T > &b)
squared distance between two points, which is faster to compute than just distance
Definition MRMesh/MRVector3.h:137
friend constexpr bool operator!=(const Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:100
Vector3< T > div(const Vector3< T > &a, const Vector3< T > &b)
per component division
Definition MRMesh/MRVector3.h:190
friend constexpr auto operator*(T a, const Vector3< T > &b) -> Vector3< decltype(std::declval< T >() *std::declval< T >())>
Definition MRMesh/MRVector3.h:109
friend constexpr auto operator-(const Vector3< T > &a) -> Vector3< decltype(-std::declval< T >())>
Definition MRMesh/MRVector3.h:105
T x
Definition MRMesh/MRVector3.h:32
static constexpr Vector3 minusX() noexcept
Definition MRMesh/MRVector3.h:45
static constexpr Vector3 minusY() noexcept
Definition MRMesh/MRVector3.h:46
bool isFinite() const
Definition MRMesh/MRVector3.h:94
T y
Definition MRMesh/MRVector3.h:32
static constexpr int elements
Definition MRMesh/MRVector3.h:30
friend constexpr const Vector3< T > & operator+(const Vector3< T > &a)
Definition MRMesh/MRVector3.h:104
friend constexpr Vector3< T > & operator/=(Vector3< T > &a, T b)
Definition MRMesh/MRVector3.h:122
std::pair< Vector3, Vector3 > perpendicular() const
void unsignZeroValues()
get rid of signed zero values to be sure that equal vectors have identical binary representation
Definition MRMesh/MRVector3.h:87
Vector3(NoInit) noexcept
Definition MRMesh/MRVector3.h:35
T ValueType
Definition MRMesh/MRVector3.h:27
constexpr const T & operator[](int e) const noexcept
Definition MRMesh/MRVector3.h:52
auto length() const
Definition MRMesh/MRVector3.h:56
constexpr Vector3(T x, T y, T z) noexcept
Definition MRMesh/MRVector3.h:36
friend constexpr bool operator==(const Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:99
friend constexpr Vector3< T > & operator*=(Vector3< T > &a, T b)
Definition MRMesh/MRVector3.h:121
T distance(const Vector3< T > &a, const Vector3< T > &b)
distance between two points, better use distanceSq for higher performance
Definition MRMesh/MRVector3.h:144
T mixed(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
mixed product
Definition MRMesh/MRVector3.h:176
static constexpr Vector3 plusZ() noexcept
Definition MRMesh/MRVector3.h:44
friend constexpr auto operator/(Vector3< T > b, T a) -> Vector3< decltype(std::declval< T >()/std::declval< T >())>
Definition MRMesh/MRVector3.h:111
T sqr(const Vector3< T > &a)
squared length
Definition MRMesh/MRVector3.h:169
Vector3 normalized() const
Definition MRMesh/MRVector3.h:64
T angle(const Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:199
constexpr Vector3(const Vector3< U > &v) noexcept
Definition MRMesh/MRVector3.h:50
constexpr Vector3() noexcept
Definition MRMesh/MRVector3.h:34
static constexpr Vector3 minusZ() noexcept
Definition MRMesh/MRVector3.h:47
Vector3< T > mult(const Vector3< T > &a, const Vector3< T > &b)
per component multiplication
Definition MRMesh/MRVector3.h:183
T z
Definition MRMesh/MRVector3.h:32
friend constexpr Vector3< T > & operator-=(Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:120
T lengthSq() const
Definition MRMesh/MRVector3.h:55
static constexpr Vector3 plusY() noexcept
Definition MRMesh/MRVector3.h:43
Vector3 furthestBasisVector() const
returns one of 3 basis unit vector that makes the biggest angle with the direction specified by this
Vector3 transformed(const AffineXf3< TT > *xf) const
returns this vector transformed by xf if it is
Definition MRMesh/MRVector3.h:81
static constexpr Vector3 diagonal(T a) noexcept
Definition MRMesh/MRVector3.h:41
Vector3< T > cross(const Vector3< T > &a, const Vector3< T > &b)
cross product
Definition MRMesh/MRVector3.h:151
friend constexpr Vector3< T > & operator+=(Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:119