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