15 T
b = 0,
c = 0,
d = 0;
29 [[nodiscard]]
constexpr T
angle() const noexcept {
return 2 * std::acos( std::clamp(
a, T(-1), T(1) ) ); }
33 [[nodiscard]]
constexpr T
normSq()
const {
return a *
a +
b *
b +
c *
c +
d *
d; }
34 [[nodiscard]]
constexpr T
norm()
const {
return std::sqrt(
normSq() ); }
63 auto xfA =
slerp( xf0.
A, xf1.
A, t );
64 return { xfA, ( 1 - t ) * xf0( p ) + t * xf1( p ) - xfA * p };
77 a = std::cos(
angle / 2 );
88 const auto tr = m.trace();
91 auto S = std::sqrt( tr + 1 ) * 2;
93 b = ( m.z.y - m.y.z ) / S;
94 c = ( m.x.z - m.z.x ) / S;
95 d = ( m.y.x - m.x.y ) / S;
97 else if ( m.x.x > m.y.y && m.x.x > m.z.z )
99 auto S = std::sqrt( 1 + m.x.x - m.y.y - m.z.z ) * 2;
100 a = ( m.z.y - m.y.z ) / S;
102 c = ( m.x.y + m.y.x ) / S;
103 d = ( m.x.z + m.z.x ) / S;
105 else if ( m.y.y > m.z.z )
107 auto S = std::sqrt( 1 + m.y.y - m.x.x - m.z.z ) * 2;
108 a = ( m.x.z - m.z.x ) / S;
109 b = ( m.x.y + m.y.x ) / S;
111 d = ( m.y.z + m.z.y ) / S;
115 auto S = std::sqrt( 1 + m.z.z - m.x.x - m.y.y ) * 2;
116 a = ( m.y.x - m.x.y ) / S;
117 b = ( m.x.z + m.z.x ) / S;
118 c = ( m.y.z + m.z.y ) / S;
128 auto cr = cross( from, to );
129 if( cr.x == 0 && cr.y == 0 && cr.z == 0 )
135 a = 1; b = 0; c = 0; d = 0;
141 auto perp = cross( from, from.furthestBasisVector() );
142 a = 0; b = perp.x; c = perp.y; d = perp.z;
148 a += std::sqrt( from.lengthSq() * to.lengthSq() );
149 b = cr.x; c = cr.y; d = cr.z;
161 T cosTheta = std::clamp(
dot( q0, q1 ), T(-1), T(1) );
165 cosTheta = -cosTheta;
167 T theta = std::acos( cosTheta );
168 T sinTheta = std::sin( theta );
170 return lerp( q0, q1, t ).normalized();
172 return std::sin( theta * ( 1 - t ) ) / sinTheta * q0 + std::sin( theta * t ) / sinTheta * q1;
176Quaternion<T>::operator Matrix3<T>()
const
179 res.x =
Vector3<T>{ a * a + b * b - c * c - d * d, 2*(b * c - a * d), 2*(b * d + a * c) };
180 res.y =
Vector3<T>{ 2*(b * c + a * d), a * a + c * c - b * b - d * d, 2*(c * d - a * b) };
181 res.z =
Vector3<T>{ 2*(b * d - a * c), 2*(c * d + a * b), a * a + d * d - b * b - c * c };
186[[nodiscard]]
inline bool operator ==(
const Quaternion<T> & a,
const Quaternion<T> & b )
188 return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d;
192[[nodiscard]]
inline bool operator !=(
const Quaternion<T> & a,
const Quaternion<T> & b )
198[[nodiscard]]
inline Quaternion<T>
operator +(
const Quaternion<T> & a,
const Quaternion<T> & b )
200 return {a.
a + b.a, a.b + b.b, a.c + b.c, a.d + b.d};
204[[nodiscard]]
inline Quaternion<T>
operator -(
const Quaternion<T> & a,
const Quaternion<T> & b )
206 return {a.a - b.a, a.b - b.b, a.c - b.c, a.d - b.d};
210[[nodiscard]]
inline Quaternion<T>
operator *( T a,
const Quaternion<T> & b )
212 return {a * b.a, a * b.b, a * b.c, a * b.d};
216[[nodiscard]]
inline Quaternion<T>
operator *(
const Quaternion<T> & b, T a )
218 return {a * b.a, a * b.b, a * b.c, a * b.d};
222[[nodiscard]]
inline Quaternion<T>
operator /(
const Quaternion<T> & b, T a )
224 return b * ( 1 / a );
231 return a.
a * b.
a + a.
b * b.
b + a.
c * b.
c + a.
d * b.
d;
240 q1.
a * q2.
a - q1.
b * q2.
b - q1.
c * q2.
c - q1.
d * q2.
d,
241 q1.
a * q2.
b + q1.
b * q2.
a + q1.
c * q2.
d - q1.
d * q2.
c,
242 q1.
a * q2.
c - q1.
b * q2.
d + q1.
c * q2.
a + q1.
d * q2.
b,
243 q1.
a * q2.
d + q1.
b * q2.
c - q1.
c * q2.
b + q1.
d * q2.
a
251 return ( *
this *
Quaternion( T(0), p ) * this->conjugate() ).im();
255[[nodiscard]]
const Quaternion<T>* getCanonicalQuaternions() noexcept
257 static Quaternion<T> canonQuats[24] =
271 Quaternion<T>(
Vector3<T>( T( 1 ),T( 1 ),T( 0 ) ), T( PI ) ),
272 Quaternion<T>(
Vector3<T>( T( 1 ),T( -1 ),T( 0 ) ), T( PI ) ),
273 Quaternion<T>(
Vector3<T>( T( 1 ),T( 0 ),T( 1 ) ), T( PI ) ),
274 Quaternion<T>(
Vector3<T>( T( 1 ),T( 0 ),T( -1 ) ), T( PI ) ),
275 Quaternion<T>(
Vector3<T>( T( 0 ),T( 1 ),T( 1 ) ), T( PI ) ),
276 Quaternion<T>(
Vector3<T>( T( 0 ),T( 1 ),T( -1 ) ), T( PI ) ),
278 Quaternion<T>(
Vector3<T>( T( 1 ),T( 1 ),T( 1 ) ), T( 2 * PI / 3 ) ),
279 Quaternion<T>(
Vector3<T>( T( 1 ),T( 1 ),T( -1 ) ), T( 2 * PI / 3 ) ),
280 Quaternion<T>(
Vector3<T>( T( 1 ),T( -1 ),T( 1 ) ), T( 2 * PI / 3 ) ),
281 Quaternion<T>(
Vector3<T>( T( 1 ),T( -1 ),T( -1 ) ), T( 2 * PI / 3 ) ),
282 Quaternion<T>(
Vector3<T>( T( -1 ),T( 1 ),T( 1 ) ), T( 2 * PI / 3 ) ),
283 Quaternion<T>(
Vector3<T>( T( -1 ),T( 1 ),T( -1 ) ), T( 2 * PI / 3 ) ),
284 Quaternion<T>(
Vector3<T>( T( -1 ),T( -1 ),T( 1 ) ), T( 2 * PI / 3 ) ),
285 Quaternion<T>(
Vector3<T>( T( -1 ),T( -1 ),T( -1 ) ), T( 2 * PI / 3 ) )
297 const Quaternion<T>* canonQuats = getCanonicalQuaternions<T>();
298 for (
int i = 0; i < 24; ++i )
303 T cos = std::abs( relativeQuat.
a );
310 return canonQuats[closestIndex];
348 res.
A = orthonormalized( xf.
A );
349 res.
b = xf( center ) - res.
A * center;
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:342
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...
bool operator!=(const SetBitIteratorT< T > &a, const SetBitIteratorT< T > &b)
Definition MRMesh/MRBitSet.h:276
constexpr A normalize(A a)
Definition MRImGuiVectorOperators.h:134
constexpr auto dot(A a, A b)
Definition MRImGuiVectorOperators.h:129
Definition MRCameraOrientationPlugin.h:8
Color operator/(const Color &b, float a)
Definition MRColor.h:128
Color operator*(float a, const Color &b)
Definition MRColor.h:118
MRMESH_CLASS Vector3
Definition MRMesh/MRMeshFwd.h:137
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:108
Definition MRMesh/MRAffineXf.h:14
V b
Definition MRMesh/MRAffineXf.h:19
M A
Definition MRMesh/MRAffineXf.h:18
uint8_t a
Definition MRColor.h:10
Definition MRMesh/MRMatrix3.h:13
Definition MRQuaternion.h:13
static Quaternion slerp(Quaternion q0, Quaternion q1, T t)
given t in [0,1] and two unit quaternions, interpolates them spherically and produces another unit qu...
constexpr Quaternion(const Vector3< T > &from, const Vector3< T > &to) noexcept
finds shorter arc rotation quaternion from one vector to another
T d
imaginary part: b*i + c*j + d*k
Definition MRQuaternion.h:15
constexpr T norm() const
Definition MRQuaternion.h:34
constexpr Quaternion inverse() const noexcept
computes reciprocal quaternion
Definition MRQuaternion.h:45
T dot(const Quaternion< T > &a, const Quaternion< T > &b)
dot product
Definition MRQuaternion.h:229
static Matrix3< T > slerp(const Matrix3< T > &m0, const Matrix3< T > &m1, T t)
given t in [0,1] and two rotation matrices, interpolates them spherically and produces another rotati...
Definition MRQuaternion.h:58
Matrix3< T > orthonormalized(const Matrix3< T > &m)
given any matrix, returns a close rotation matrix
Definition MRQuaternion.h:337
constexpr Quaternion(const Vector3< T > &axis, T angle) noexcept
T c
Definition MRQuaternion.h:15
AffineXf3< T > orthonormalized(const AffineXf3< T > &xf, const Vector3< T > ¢er={})
Definition MRQuaternion.h:345
static Quaternion lerp(const Quaternion &q0, const Quaternion &q1, T t)
given t in [0,1], interpolates linearly two quaternions giving in general not-unit quaternion
Definition MRQuaternion.h:54
constexpr Vector3< T > axis() const noexcept
returns axis of rotation encoded in this quaternion
Definition MRQuaternion.h:31
constexpr Quaternion(const Matrix3< T > &m)
constexpr Quaternion(T real, const Vector3< T > &im) noexcept
Definition MRQuaternion.h:20
Matrix3< T > slerp(const Matrix3< T > &m0, const Matrix3< T > &m1, T t)
given t in [0,1] and two rotation matrices, interpolates them spherically and produces another rotati...
Definition MRQuaternion.h:322
constexpr Quaternion operator-() const
returns quaternion representing the same rotation, using the opposite rotation direction and opposite...
Definition MRQuaternion.h:36
Quaternion & operator*=(T s)
Definition MRQuaternion.h:67
Quaternion & operator/=(T s)
Definition MRQuaternion.h:68
constexpr Quaternion conjugate() const noexcept
computes conjugate quaternion, which for unit quaternions encodes the opposite rotation
Definition MRQuaternion.h:43
constexpr Quaternion() noexcept=default
Quaternion normalized() const
Definition MRQuaternion.h:40
T a
real part of the quaternion
Definition MRQuaternion.h:14
T b
Definition MRQuaternion.h:15
constexpr Vector3< T > im() const noexcept
returns imaginary part of the quaternion as a vector
Definition MRQuaternion.h:26
Quaternion< T > getClosestCanonicalQuaternion(const Quaternion< T > &base) noexcept
returns closest to base canonical quaternion
Definition MRQuaternion.h:292
static AffineXf3< T > slerp(const AffineXf3< T > &xf0, const AffineXf3< T > &xf1, T t, const Vector3< T > &p={})
Definition MRQuaternion.h:61
constexpr T angle() const noexcept
returns angle of rotation encoded in this quaternion
Definition MRQuaternion.h:29
void normalize()
scales this quaternion to make its norm unit
Definition MRQuaternion.h:39
constexpr T normSq() const
Definition MRQuaternion.h:33
AffineXf3< T > slerp(const AffineXf3< T > &xf0, const AffineXf3< T > &xf1, T t, const Vector3< T > &p={})
Definition MRQuaternion.h:330
constexpr Vector3< T > operator()(const Vector3< T > &p) const noexcept
Definition MRMesh/MRVector3.h:19
static constexpr Vector3 plusX() noexcept
Definition MRMesh/MRVector3.h:33
static constexpr Vector3 plusZ() noexcept
Definition MRMesh/MRVector3.h:35
static constexpr Vector3 plusY() noexcept
Definition MRMesh/MRVector3.h:34