20 #if !defined(__MITSUBA_CORE_QUAT_H_)
21 #define __MITSUBA_CORE_QUAT_H_
107 SLog(
EWarn,
"Quaternion: Division by zero!");
117 SLog(
EWarn,
"Quaternion: Division by zero!");
120 v *= recip; w *= recip;
127 v =
cross(v, q.
v) + q.
w * v + w * q.
v;
135 w * q.
w -
dot(v, q.
v));
140 return v == q.
v && w == q.
w;
145 return v != q.
v || w != q.
w;
150 return v.isZero() && w == 1;
167 T theta = v.length();
168 T c = std::cos(theta);
171 return TQuaternion(v * (std::sin(theta) / theta), c);
183 T scale = v.length();
184 T theta = std::atan2(scale, w);
197 T sinValue = std::sin(angle/2.0f), cosValue = std::cos(angle/2.0f);
210 }
else if (dp < -(1-
Epsilon)) {
213 Float length = rotAxis.length();
216 length = rotAxis.length();
222 Float cosTheta = std::sqrt(0.5f * (1 + dp));
223 Float sinTheta = std::sqrt(0.5f * (1 - dp));
240 T trace = m(0, 0) + m(1, 1) + m(2, 2);
244 T s = std::sqrt(trace + 1.0f);
247 v.
x = (m(2, 1) - m(1, 2)) * s;
248 v.
y = (m(0, 2) - m(2, 0)) * s;
249 v.
z = (m(1, 0) - m(0, 1)) * s;
251 const int nxt[3] = {1, 2, 0};
254 if (m(1, 1) > m(0, 0)) i = 1;
255 if (m(2, 2) > m(i, i)) i = 2;
258 T s = std::sqrt((m(i, i) - (m(j, j) + m(k, k))) + 1.0f);
260 if (s != 0.f) s = 0.5f / s;
261 w = (m(k, j) - m(j, k)) * s;
262 q[j] = (m(j, i) + m(i, j)) * s;
263 q[k] = (m(k, i) + m(i, k)) * s;
303 Float xx = v.x * v.x, yy = v.y * v.y, zz = v.z * v.z;
304 Float xy = v.x * v.y, xz = v.x * v.z, yz = v.y * v.z;
305 Float wx = v.x * w, wy = v.y * w, wz = v.z * w;
308 m.
m[0][0] = 1.f - 2.f * (yy + zz);
309 m.
m[0][1] = 2.f * (xy + wz);
310 m.
m[0][2] = 2.f * (xz - wy);
312 m.
m[1][0] = 2.f * (xy - wz);
313 m.
m[1][1] = 1.f - 2.f * (xx + zz);
314 m.
m[1][2] = 2.f * (yz + wx);
316 m.
m[2][0] = 2.f * (xz + wy);
317 m.
m[2][1] = 2.f * (yz - wx);
318 m.
m[2][2] = 1.f - 2.f * (xx + yy);
338 std::ostringstream oss;
339 oss <<
"Quaternion[v=" << v.toString() <<
", w=" << w <<
"]";
349 return dot(q1.
v, q2.
v) + q1.
w * q2.
w;
353 return q / std::sqrt(
dot(q, q));
360 T cosTheta =
dot(q1, q2);
364 cosTheta = -cosTheta;
366 if (cosTheta > .9995f) {
368 return normalize(q1 * (1.0f - t) + q2 * t);
371 Float thetap = theta * t;
373 return q1 * std::cos(thetap) + qperp * std::sin(thetap);
#define Epsilon
Definition: constants.h:28
bool operator!=(const TQuaternion &q) const
Inequality test.
Definition: quat.h:144
T readElement()
Read an element from the stream (uses partial template specialization to select a method appropriate ...
Definition: stream.h:508
TQuaternion(Stream *stream)
Unserialize a quaternion from a binary data stream.
Definition: quat.h:60
Transform toTransform() const
Compute the rotation matrix for the given quaternion.
Definition: quat.h:301
TQuaternion operator-() const
Unary negation operator.
Definition: quat.h:88
TQuaternion operator/(T f) const
Divide the quaternion by the given scalar and return the result.
Definition: quat.h:104
TQuaternion log() const
Compute the natural logarithm of a unit quaternion.
Definition: quat.h:182
Scalar angle() const
Return the rotation angle of this quaternion (in radians)
Definition: quat.h:157
T y
Definition: vector.h:455
TVector3< Float > Vector
Definition: fwd.h:113
TQuaternion exp() const
Compute the exponential of a quaternion with scalar part w = 0.
Definition: quat.h:166
TQuaternion operator+(const TQuaternion &q) const
Add two quaternions and return the result.
Definition: quat.h:66
TQuaternion< T > slerp(const TQuaternion< T > &q1, const TQuaternion< T > &_q2, Float t)
Definition: quat.h:356
#define SLog(level, fmt,...)
Write a Log message to the console (static version - to be used outside of classes that derive from O...
Definition: logger.h:49
TQuaternion(const TVector3< T > &v, T w)
Definition: quat.h:57
TQuaternion operator-(const TQuaternion &q) const
Subtract two quaternions and return the result.
Definition: quat.h:71
static TQuaternion fromAxisAngle(const Vector &axis, Float angle)
Construct an unit quaternion, which represents a rotation around axis by angle radians.
Definition: quat.h:196
static TQuaternion fromTransform(const Transform &trafo)
Definition: quat.h:229
TQuaternion()
Create a unit quaternion.
Definition: quat.h:51
TQuaternion< T > operator*(T f, const TQuaternion< T > &v)
Definition: quat.h:344
EEulerAngleConvention
Used by TQuaternion::fromEulerAngles.
Definition: quat.h:35
T Scalar
Definition: quat.h:32
TQuaternion & operator+=(const TQuaternion &q)
Add another quaternions to the current one.
Definition: quat.h:76
void serialize(Stream *stream) const
Serialize this quaternion to a binary data stream.
Definition: quat.h:331
void transpose(Matrix< N, M, T > &target) const
Compute the transpose of this matrix.
Definition: matrix.h:414
T z
Definition: vector.h:455
Parameterizable three-dimensional vector data structure.
Definition: vector.h:450
void writeElement(T value)
Write an element to the stream (uses partial template specialization to select a method appropriate t...
Definition: stream.h:512
TQuaternion & operator*=(T f)
Multiply the quaternion by the given scalar.
Definition: quat.h:98
Abstract seekable stream class.
Definition: stream.h:58
T m[M][N]
Definition: matrix.h:35
Warning message.
Definition: formatter.h:32
static TQuaternion fromEulerAngles(EEulerAngleConvention conv, Float x, Float y, Float z)
Construct an unit quaternion matching the supplied rotation expressed in Euler angles (in radians) ...
Definition: quat.h:275
T x
Definition: vector.h:455
TQuaternion operator*(const TQuaternion &q) const
Quaternion multiplication (creates a temporary)
Definition: quat.h:133
TQuaternion operator*(T f) const
Multiply the quaternion by the given scalar and return the result.
Definition: quat.h:93
T w
Real component.
Definition: quat.h:48
bool isIdentity() const
Identity test.
Definition: quat.h:149
Error message, causes an exception to be thrown.
Definition: formatter.h:33
Parameterizable quaternion data structure.
Definition: quat.h:31
Basic 4x4 matrix data type.
Definition: matrix.h:656
TQuaternion & operator/=(T f)
Divide the quaternion by the given scalar.
Definition: quat.h:114
TQuaternion< T > normalize(const TQuaternion< T > &q)
Definition: quat.h:352
TVector3< T > v
Imaginary component.
Definition: quat.h:45
static TQuaternion fromDirectionPair(const Vector &from, const Vector &to)
Construct an unit quaternion, which rotates unit direction from onto to.
Definition: quat.h:205
float safe_acos(float value)
Arccosine variant that gracefully handles arguments > 1 that are due to roundoff errors.
Definition: math.h:250
TQuaternion & operator*=(const TQuaternion &q)
Quaternion multiplication.
Definition: quat.h:125
TVector3< T > cross(const TVector3< T > &v1, const TVector3< T > &v2)
Definition: vector.h:617
bool operator==(const TQuaternion &q) const
Equality test.
Definition: quat.h:139
T dot(const TQuaternion< T > &q1, const TQuaternion< T > &q2)
Definition: quat.h:348
TQuaternion & operator-=(const TQuaternion &q)
Subtract a quaternion.
Definition: quat.h:82
TVector3< T > axis() const
Return the rotation axis of this quaternion.
Definition: quat.h:154
static TQuaternion fromMatrix(const Matrix4x4 &m)
Construct an unit quaternion matching the supplied rotation matrix.
Definition: quat.h:237
Scalar clamp(Scalar value, Scalar min, Scalar max)
Generic clamping function.
Definition: math.h:51
std::string toString() const
Return a readable string representation of this quaternion.
Definition: quat.h:337