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#include <cstring>
10#include <utility>
11#if MR_HAS_REQUIRES
12#include <concepts>
13#endif
14
15namespace MR
16{
17
18#ifdef _MSC_VER
19#pragma warning(push)
20#pragma warning(disable: 4804) // unsafe use of type 'bool' in operation
21#pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
22#endif
23
26template <typename T>
27struct Vector3
28{
29 using ValueType = T;
32 static constexpr int elements = 3;
33
34 T x, y, z;
35
36 constexpr Vector3() noexcept : x( 0 ), y( 0 ), z( 0 ) { }
37 explicit Vector3( NoInit ) noexcept { }
38 constexpr Vector3( T x, T y, T z ) noexcept : x( x ), y( y ), z( z ) { }
39
40 template <typename U> MR_REQUIRES_IF_SUPPORTED( std::constructible_from<T, U> )
41 explicit constexpr Vector3( const Vector2<U> & v ) noexcept : x( v.x ), y( v.y ), z( 0 ) { }
42
43 static constexpr Vector3 diagonal( T a ) noexcept { return Vector3( a, a, a ); }
44 static constexpr Vector3 plusX() noexcept { return Vector3( 1, 0, 0 ); }
45 static constexpr Vector3 plusY() noexcept { return Vector3( 0, 1, 0 ); }
46 static constexpr Vector3 plusZ() noexcept { return Vector3( 0, 0, 1 ); }
47 static constexpr Vector3 minusX() noexcept { return Vector3( -1, 0, 0 ); }
48 static constexpr Vector3 minusY() noexcept { return Vector3( 0, -1, 0 ); }
49 static constexpr Vector3 minusZ() noexcept { return Vector3( 0, 0, -1 ); }
50
51 // 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
52 // when generating the bindings, and looks out of place there.
53 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
54 constexpr explicit Vector3( const Vector3<U> & v ) noexcept : x( T( v.x ) ), y( T( v.y ) ), z( T( v.z ) ) { }
55
56 constexpr const T & operator []( int e ) const noexcept { return *( &x + e ); }
57 constexpr T & operator []( int e ) noexcept { return *( &x + e ); }
58
59 T lengthSq() const { return x * x + y * y + z * z; }
60 auto length() const
61 {
62 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
63 // Returning `auto` to not break on integral types.
64 using std::sqrt;
65 return sqrt( lengthSq() );
66 }
67
68 [[nodiscard]] Vector3 normalized() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
69 {
70 auto len = length();
71 if ( len <= 0 )
72 return {};
73 return ( 1 / len ) * (*this);
74 }
75
77 Vector3 furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> );
78
81 std::pair<Vector3, Vector3> perpendicular() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> );
82
84 template <MR_SAME_TYPE_TEMPLATE_PARAM(T, TT)> // Need this, otherwise the bindings try to instantiate `AffineXf3` with non-FP arguments.
85 Vector3 transformed( const AffineXf3<TT>* xf ) const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
86 {
87 return xf ? ( *xf )( *this ) : *this;
88 }
89
91 void unsignZeroValues() MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
92 {
93 for ( auto i = 0; i < elements; ++i )
94 if ( (*this)[i] == 0.f && std::signbit( (*this)[i] ) )
95 (*this)[i] = 0.f;
96 }
97
98 [[nodiscard]] bool isFinite() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
99 {
100 return std::isfinite( x ) && std::isfinite( y ) && std::isfinite( z );
101 }
102
103 [[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; }
104 [[nodiscard]] friend constexpr bool operator !=( const Vector3<T> & a, const Vector3<T> & b ) { return !( a == b ); }
105
106 // 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.
107
108 [[nodiscard]] friend constexpr const Vector3<T> & operator +( const Vector3<T> & a ) { return a; }
109 [[nodiscard]] friend constexpr auto operator -( const Vector3<T> & a ) -> Vector3<decltype( -std::declval<T>() )> { return { -a.x, -a.y, -a.z }; }
110
111 [[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 }; }
112 [[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 }; }
113 [[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 }; }
114 [[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 }; }
115 [[nodiscard]] friend constexpr auto operator /( Vector3<T> b, T a ) -> Vector3<decltype( std::declval<T>() / std::declval<T>() )>
116 {
117 if constexpr ( std::is_integral_v<T> )
118 return { b.x / a, b.y / a, b.z / a };
119 else
120 return b * ( 1 / a );
121 }
122
123 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; }
124 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; }
125 friend constexpr Vector3<T> & operator *=( Vector3<T> & a, T b ) { a.x *= b; a.y *= b; a.z *= b; return a; }
126 friend constexpr Vector3<T> & operator /=( Vector3<T> & a, T b )
127 {
128 if constexpr ( std::is_integral_v<T> )
129 { a.x /= b; a.y /= b; a.z /= b; return a; }
130 else
131 return a *= ( 1 / b );
132 }
133};
134
137
138
140template <typename T>
141inline T distanceSq( const Vector3<T> & a, const Vector3<T> & b )
142{
143 return ( a - b ).lengthSq();
144}
145
147template <typename T>
148inline T distance( const Vector3<T> & a, const Vector3<T> & b )
149{
150 return ( a - b ).length();
151}
152
154template <typename T>
155inline Vector3<T> cross( const Vector3<T> & a, const Vector3<T> & b )
156{
157 return {
158 a.y * b.z - a.z * b.y,
159 a.z * b.x - a.x * b.z,
160 a.x * b.y - a.y * b.x
161 };
162}
163
165template <typename T>
166inline auto dot( const Vector3<T> & a, const Vector3<T> & b ) -> decltype( a.x * b.x )
167{
168 return a.x * b.x + a.y * b.y + a.z * b.z;
169}
170
172template <typename T>
173inline T sqr( const Vector3<T> & a )
174{
175 return a.lengthSq();
176}
177
179template <typename T>
180inline T mixed( const Vector3<T> & a, const Vector3<T> & b, const Vector3<T> & c )
181{
182 return dot( a, cross( b, c ) );
183}
184
186template <typename T>
187inline Vector3<T> mult( const Vector3<T>& a, const Vector3<T>& b )
188{
189 return { a.x * b.x,a.y * b.y,a.z * b.z };
190}
191
193template <typename T>
194inline Vector3<T> div( const Vector3<T>& a, const Vector3<T>& b )
195{
196 return { a.x / b.x, a.y / b.y, a.z / b.z };
197}
198
199
202template <typename T>
203inline T angle( const Vector3<T> & a, const Vector3<T> & b )
204{
205 return std::atan2( cross( a, b ).length(), dot( a, b ) );
206 // this version is slower and less precise
207 //return std::acos( std::clamp( dot( a.normalized(), b.normalized() ), T(-1), T(1) ) );
208}
209
210template <typename T>
211inline Vector3<T> Vector3<T>::furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, bool> )
212{
213 using std::abs; // This should allow boost.multiprecision numbers here.
214 if ( abs( x ) < abs( y ) )
215 return ( abs( x ) < abs( z ) ) ? Vector3( 1, 0, 0 ) : Vector3( 0, 0, 1 );
216 else
217 return ( abs( y ) < abs( z ) ) ? Vector3( 0, 1, 0 ) : Vector3( 0, 0, 1 );
218}
219
220template <typename T>
221inline std::pair<Vector3<T>, Vector3<T>> Vector3<T>::perpendicular() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
222{
223 std::pair<Vector3<T>, Vector3<T>> res;
224 auto c1 = furthestBasisVector();
225 res.first = cross( *this, c1 ).normalized();
226 res.second = cross( *this, res.first ).normalized();
227 return res;
228}
229
231template <typename T>
232Vector3<T> unitVector3( T azimuth, T altitude )
233{
234 const auto zenithAngle = T( PI2 ) - altitude;
235 return
236 {
237 std::sin( zenithAngle ) * std::cos( azimuth ),
238 std::sin( zenithAngle ) * std::sin( azimuth ),
239 std::cos( zenithAngle )
240 };
241}
242
243
244// We don't need to bind those functions themselves. This doesn't prevent `__iter__` from being generated for the type.
245
246template <typename T>
247MR_BIND_IGNORE inline auto begin( const Vector3<T> & v ) { return &v[0]; }
248template <typename T>
249MR_BIND_IGNORE inline auto begin( Vector3<T> & v ) { return &v[0]; }
250
251template <typename T>
252MR_BIND_IGNORE inline auto end( const Vector3<T> & v ) { return &v[3]; }
253template <typename T>
254MR_BIND_IGNORE inline auto end( Vector3<T> & v ) { return &v[3]; }
255
257
258#ifdef _MSC_VER
259#pragma warning(pop)
260#endif
261
262} // namespace MR
263
264template<>
265struct std::hash<MR::Vector3f>
266{
267 size_t operator()( MR::Vector3f const& p ) const noexcept
268 {
269 // standard implementation is slower:
270 // phmap::HashState().combine(phmap::Hash<float>()(p.x), p.y, p.z);
271 std::uint64_t xy;
272 std::uint32_t z;
273 static_assert( sizeof( float ) == sizeof( std::uint32_t ) );
274 std::memcpy( &xy, &p.x, sizeof( std::uint64_t ) );
275 std::memcpy( &z, &p.z, sizeof( std::uint32_t ) );
276 return size_t( xy ) ^ ( size_t( z ) << 16 );
277 }
278};
#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: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 MRMesh/MRAffineXf.h:21
Definition MRMesh/MRMatrix3.h:19
Definition MRMesh/MRMeshFwd.h:90
Definition MRSymMatrix3.h:15
Definition MRVector2.h:27
Definition MRMesh/MRVector3.h:28
static constexpr Vector3 plusX() noexcept
Definition MRMesh/MRVector3.h:44
void unsignZeroValues() MR_REQUIRES_IF_SUPPORTED(std
get rid of signed zero values to be sure that equal vectors have identical binary representation
Definition MRMesh/MRVector3.h:91
auto dot(const Vector3< T > &a, const Vector3< T > &b) -> decltype(a.x *b.x)
dot product
Definition MRMesh/MRVector3.h:166
Vector3< T > unitVector3(T azimuth, T altitude)
returns a point on unit sphere given two angles
Definition MRMesh/MRVector3.h:232
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:141
friend constexpr bool operator!=(const Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:104
Vector3< T > div(const Vector3< T > &a, const Vector3< T > &b)
per component division
Definition MRMesh/MRVector3.h:194
friend constexpr auto operator*(T a, const Vector3< T > &b) -> Vector3< decltype(std::declval< T >() *std::declval< T >())>
Definition MRMesh/MRVector3.h:113
friend constexpr auto operator-(const Vector3< T > &a) -> Vector3< decltype(-std::declval< T >())>
Definition MRMesh/MRVector3.h:109
T x
Definition MRMesh/MRVector3.h:34
static constexpr Vector3 minusX() noexcept
Definition MRMesh/MRVector3.h:47
Vector3 furthestBasisVector() const MR_REQUIRES_IF_SUPPORTED(!std std::pair< Vector3, Vector3 > perpendicular() const MR_REQUIRES_IF_SUPPORTED(std Vector3 transformed(const AffineXf3< TT > *xf) const MR_REQUIRES_IF_SUPPORTED(std
returns one of 3 basis unit vector that makes the biggest angle with the direction specified by this
Definition MRMesh/MRVector3.h:85
static constexpr Vector3 minusY() noexcept
Definition MRMesh/MRVector3.h:48
T y
Definition MRMesh/MRVector3.h:34
static constexpr int elements
Definition MRMesh/MRVector3.h:32
friend constexpr const Vector3< T > & operator+(const Vector3< T > &a)
Definition MRMesh/MRVector3.h:108
friend constexpr Vector3< T > & operator/=(Vector3< T > &a, T b)
Definition MRMesh/MRVector3.h:126
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Vector3(const Vector3< U > &v) noexcept
Definition MRMesh/MRVector3.h:53
Vector3(NoInit) noexcept
Definition MRMesh/MRVector3.h:37
T ValueType
Definition MRMesh/MRVector3.h:29
constexpr const T & operator[](int e) const noexcept
Definition MRMesh/MRVector3.h:56
auto length() const
Definition MRMesh/MRVector3.h:60
MR_REQUIRES_IF_SUPPORTED(std::constructible_from< T, U >) explicit const expr Vector3(const Vector2< U > &v) noexcept
Definition MRMesh/MRVector3.h:40
constexpr Vector3(T x, T y, T z) noexcept
Definition MRMesh/MRVector3.h:38
friend constexpr bool operator==(const Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:103
friend constexpr Vector3< T > & operator*=(Vector3< T > &a, T b)
Definition MRMesh/MRVector3.h:125
T distance(const Vector3< T > &a, const Vector3< T > &b)
distance between two points, better use distanceSq for higher performance
Definition MRMesh/MRVector3.h:148
T mixed(const Vector3< T > &a, const Vector3< T > &b, const Vector3< T > &c)
mixed product
Definition MRMesh/MRVector3.h:180
static constexpr Vector3 plusZ() noexcept
Definition MRMesh/MRVector3.h:46
Vector3 normalized() const MR_REQUIRES_IF_SUPPORTED(std
Definition MRMesh/MRVector3.h:68
friend constexpr auto operator/(Vector3< T > b, T a) -> Vector3< decltype(std::declval< T >()/std::declval< T >())>
Definition MRMesh/MRVector3.h:115
T sqr(const Vector3< T > &a)
squared length
Definition MRMesh/MRVector3.h:173
T angle(const Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:203
constexpr Vector3() noexcept
Definition MRMesh/MRVector3.h:36
static constexpr Vector3 minusZ() noexcept
Definition MRMesh/MRVector3.h:49
Vector3< T > mult(const Vector3< T > &a, const Vector3< T > &b)
per component multiplication
Definition MRMesh/MRVector3.h:187
T z
Definition MRMesh/MRVector3.h:34
friend constexpr Vector3< T > & operator-=(Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:124
T lengthSq() const
Definition MRMesh/MRVector3.h:59
static constexpr Vector3 plusY() noexcept
Definition MRMesh/MRVector3.h:45
bool isFinite() const MR_REQUIRES_IF_SUPPORTED(std
Definition MRMesh/MRVector3.h:98
static constexpr Vector3 diagonal(T a) noexcept
Definition MRMesh/MRVector3.h:43
Vector3< T > cross(const Vector3< T > &a, const Vector3< T > &b)
cross product
Definition MRMesh/MRVector3.h:155
friend constexpr Vector3< T > & operator+=(Vector3< T > &a, const Vector3< T > &b)
Definition MRMesh/MRVector3.h:123