18 T
b = 0,
c = 0,
d = 0;
32 [[nodiscard]]
constexpr T
angle() const noexcept {
return 2 * std::acos( std::clamp(
a, T(-1), T(1) ) ); }
36 [[nodiscard]]
constexpr T
normSq()
const {
return a *
a +
b *
b +
c *
c +
d *
d; }
37 [[nodiscard]]
constexpr T
norm()
const {
return std::sqrt(
normSq() ); }
64 [[nodiscard]]
static AffineXf3<T>
slerp(
const AffineXf3<T> & xf0,
const AffineXf3<T> & xf1, T t,
const Vector3<T> & p = {} )
66 auto xfA =
slerp( xf0.A, xf1.A, t );
67 return { xfA, ( 1 - t ) * xf0( p ) + t * xf1( p ) - xfA * p };
80 a = std::cos(
angle / 2 );
81 Vector3<T> im = std::sin(
angle / 2 ) * axis.normalized();
91 const auto tr = m.trace();
94 auto S = std::sqrt( tr + 1 ) * 2;
96 b = ( m.z.y - m.y.z ) / S;
97 c = ( m.x.z - m.z.x ) / S;
98 d = ( m.y.x - m.x.y ) / S;
100 else if ( m.x.x > m.y.y && m.x.x > m.z.z )
102 auto S = std::sqrt( 1 + m.x.x - m.y.y - m.z.z ) * 2;
103 a = ( m.z.y - m.y.z ) / S;
105 c = ( m.x.y + m.y.x ) / S;
106 d = ( m.x.z + m.z.x ) / S;
108 else if ( m.y.y > m.z.z )
110 auto S = std::sqrt( 1 + m.y.y - m.x.x - m.z.z ) * 2;
111 a = ( m.x.z - m.z.x ) / S;
112 b = ( m.x.y + m.y.x ) / S;
114 d = ( m.y.z + m.z.y ) / S;
118 auto S = std::sqrt( 1 + m.z.z - m.x.x - m.y.y ) * 2;
119 a = ( m.y.x - m.x.y ) / S;
120 b = ( m.x.z + m.z.x ) / S;
121 c = ( m.y.z + m.z.y ) / S;
131 auto cr = cross( from, to );
132 if( cr.x == 0 && cr.y == 0 && cr.z == 0 )
138 a = 1; b = 0; c = 0; d = 0;
144 auto perp = cross( from, from.furthestBasisVector() );
145 a = 0; b = perp.x; c = perp.y; d = perp.z;
151 a += std::sqrt( from.lengthSq() * to.lengthSq() );
152 b = cr.x; c = cr.y; d = cr.z;
164 T cosTheta = std::clamp(
dot( q0, q1 ), T(-1), T(1) );
168 cosTheta = -cosTheta;
170 T theta = std::acos( cosTheta );
171 T sinTheta = std::sin( theta );
173 return lerp( q0, q1, t ).normalized();
175 return std::sin( theta * ( 1 - t ) ) / sinTheta * q0 + std::sin( theta * t ) / sinTheta * q1;
179Quaternion<T>::operator Matrix3<T>()
const
182 res.x = Vector3<T>{ a * a + b * b - c * c - d * d, 2*(b * c - a * d), 2*(b * d + a * c) };
183 res.y = Vector3<T>{ 2*(b * c + a * d), a * a + c * c - b * b - d * d, 2*(c * d - a * b) };
184 res.z = Vector3<T>{ 2*(b * d - a * c), 2*(c * d + a * b), a * a + d * d - b * b - c * c };
189[[nodiscard]]
inline bool operator ==(
const Quaternion<T> & a,
const Quaternion<T> & b )
191 return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d;
195[[nodiscard]]
inline bool operator !=(
const Quaternion<T> & a,
const Quaternion<T> & b )
201[[nodiscard]]
inline Quaternion<T>
operator +(
const Quaternion<T> & a,
const Quaternion<T> & b )
203 return {a.
a + b.a, a.b + b.b, a.c + b.c, a.d + b.d};
207[[nodiscard]]
inline Quaternion<T>
operator -(
const Quaternion<T> & a,
const Quaternion<T> & b )
209 return {a.a - b.a, a.b - b.b, a.c - b.c, a.d - b.d};
213[[nodiscard]]
inline Quaternion<T>
operator *( T a,
const Quaternion<T> & b )
215 return {a * b.a, a * b.b, a * b.c, a * b.d};
219[[nodiscard]]
inline Quaternion<T>
operator *(
const Quaternion<T> & b, T a )
221 return {a * b.a, a * b.b, a * b.c, a * b.d};
225[[nodiscard]]
inline Quaternion<T>
operator /(
const Quaternion<T> & b, T a )
227 return b * ( 1 / a );
234 return a.
a * b.
a + a.
b * b.
b + a.
c * b.
c + a.
d * b.
d;
243 q1.
a * q2.
a - q1.
b * q2.
b - q1.
c * q2.
c - q1.
d * q2.
d,
244 q1.
a * q2.
b + q1.
b * q2.
a + q1.
c * q2.
d - q1.
d * q2.
c,
245 q1.
a * q2.
c - q1.
b * q2.
d + q1.
c * q2.
a + q1.
d * q2.
b,
246 q1.
a * q2.
d + q1.
b * q2.
c - q1.
c * q2.
b + q1.
d * q2.
a
254 return ( *
this *
Quaternion( T(0), p ) * this->conjugate() ).im();
300 const Quaternion<T>* canonQuats = getCanonicalQuaternions<T>();
301 for (
int i = 0; i < 24; ++i )
306 T cos = std::abs( relativeQuat.
a );
313 return canonQuats[closestIndex];
333[[nodiscard]]
inline AffineXf3<T>
slerp(
const AffineXf3<T> & xf0,
const AffineXf3<T> & xf1, T t,
const Vector3<T> & p = {} )
351 res.A = orthonormalized( xf.A );
352 res.b = xf( center ) - res.A * center;
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:457
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...
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:18
uint8_t a
Definition MRColor.h:13
constexpr T norm() const
Definition MRQuaternion.h:37
constexpr Quaternion inverse() const noexcept
computes reciprocal quaternion
Definition MRQuaternion.h:48
T dot(const Quaternion< T > &a, const Quaternion< T > &b)
dot product
Definition MRQuaternion.h:232
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:61
Matrix3< T > orthonormalized(const Matrix3< T > &m)
given any matrix, returns a close rotation matrix
Definition MRQuaternion.h:340
constexpr Quaternion(const Vector3< T > &axis, T angle) noexcept
T c
Definition MRQuaternion.h:18
AffineXf3< T > orthonormalized(const AffineXf3< T > &xf, const Vector3< T > ¢er={})
Definition MRQuaternion.h:348
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:57
constexpr Vector3< T > axis() const noexcept
returns axis of rotation encoded in this quaternion
Definition MRQuaternion.h:34
constexpr Quaternion(const Matrix3< T > &m)
bool operator!=(const Color &a, const Color &b)
Definition MRColor.h:104
constexpr Quaternion(T real, const Vector3< T > &im) noexcept
Definition MRQuaternion.h:23
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:325
Color operator/(const Color &b, float a)
Definition MRColor.h:129
constexpr Quaternion operator-() const
returns quaternion representing the same rotation, using the opposite rotation direction and opposite...
Definition MRQuaternion.h:39
Quaternion & operator*=(T s)
Definition MRQuaternion.h:70
Color operator*(float a, const Color &b)
Definition MRColor.h:119
Quaternion & operator/=(T s)
Definition MRQuaternion.h:71
const Quaternion< T > * getCanonicalQuaternions() noexcept
Definition MRQuaternion.h:258
constexpr Quaternion conjugate() const noexcept
computes conjugate quaternion, which for unit quaternions encodes the opposite rotation
Definition MRQuaternion.h:46
constexpr Quaternion() noexcept=default
Quaternion normalized() const
Definition MRQuaternion.h:43
T a
real part of the quaternion
Definition MRQuaternion.h:17
T b
Definition MRQuaternion.h:18
constexpr Vector3< T > im() const noexcept
returns imaginary part of the quaternion as a vector
Definition MRQuaternion.h:29
Quaternion< T > getClosestCanonicalQuaternion(const Quaternion< T > &base) noexcept
returns closest to base canonical quaternion
Definition MRQuaternion.h:295
static AffineXf3< T > slerp(const AffineXf3< T > &xf0, const AffineXf3< T > &xf1, T t, const Vector3< T > &p={})
Definition MRQuaternion.h:64
constexpr T angle() const noexcept
returns angle of rotation encoded in this quaternion
Definition MRQuaternion.h:32
void normalize()
scales this quaternion to make its norm unit
Definition MRQuaternion.h:42
constexpr T normSq() const
Definition MRQuaternion.h:36
AffineXf3< T > slerp(const AffineXf3< T > &xf0, const AffineXf3< T > &xf1, T t, const Vector3< T > &p={})
Definition MRQuaternion.h:333
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:109
constexpr Vector3< T > operator()(const Vector3< T > &p) const noexcept
@ angle
Direction, normally Vector3f.
constexpr A normalize(A a)
Definition MRImGuiVectorOperators.h:134
constexpr auto dot(A a, A b)
Definition MRImGuiVectorOperators.h:129
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRMatrix3.h:24
Definition MRQuaternion.h:16
Definition MRVector3.h:33