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