44 inline bool operator == (
const Quat& rhs)
const noexcept {
return V == rhs.V &&
W == rhs.W; }
45 inline bool operator != (
const Quat& rhs)
const noexcept {
return V != rhs.V ||
W != rhs.W; }
47 [[nodiscard]] JPL_INLINE
static Quat Identity() noexcept {
return Quat({ 0, 0, 0, }, 1); }
53 const Float n2Inv = 1.0f / (DotProduct(
V,
V) +
W *
W);
54 return Quat{ -
V * n2Inv,
W * n2Inv };
64 return Quat(axis * s, c);
69 [[nodiscard]]
inline static Quat FromTo(
const Vec3& from,
const Vec3& to)
noexcept
71 const Float dot = DotProduct(from, to);
73 if (dot >
Float(0.999999))
76 if (dot <
Float(-0.999999))
91 Float tr = r00 + r11 + r22;
99 qx = (r21 - r12) * invS;
100 qy = (r02 - r20) * invS;
101 qz = (r10 - r01) * invS;
103 else if (r00 > r11 && r00 > r22)
107 qw = (r21 - r12) * invS;
108 qx =
Float(0.25) * s;
109 qy = (r01 + r10) * invS;
110 qz = (r02 + r20) * invS;
116 qw = (r02 - r20) * invS;
117 qx = (r01 + r10) * invS;
118 qy =
Float(0.25) * s;
119 qz = (r12 + r21) * invS;
125 qw = (r10 - r01) * invS;
126 qx = (r02 + r20) * invS;
127 qy = (r12 + r21) * invS;
128 qz =
Float(0.25) * s;
131 return Quat{ Vec3{ qx, qy, qz }, qw };
141 [[nodiscard]]
static JPL_INLINE
Quat LookAt(
const Vec3& direction,
const Vec3& up)
noexcept
151 .V = a.W * b.V + b.W * a.V + CrossProduct(a.V, b.V),
152 .W = a.W * b.W - DotProduct(a.V, b.V)
166 Float x = lw * rx + lx * rw + ly * rz - lz * ry;
167 Float y = lw * ry - lx * rz + ly * rw + lz * rx;
168 Float z = lw * rz + lx * ry - ly * rx + lz * rw;
169 Float w = lw * rw - lx * rx - ly * ry - lz * rz;
171 return Quat({ x, y, z }, w);
179 return Slerp(
Identity(), qFromTo, t);
183 [[nodiscard]] JPL_INLINE Vec3
Rotate(
const Vec3& vector)
const noexcept
186 const Vec3 t =
Float(2) * CrossProduct(
V, vector);
187 return vector +
W * t + CrossProduct(
V, t);
197 return Quat{
V * invLen,
W * invLen };
208 return W ==
Float(0.0) ? JPL_PI :
Float(2.0) * std::atan(DotProduct(
V, axis) /
W);
231 [[nodiscard]]
static inline Quat<Vec3> Slerp(
const Quat<Vec3>& a,
const Quat<Vec3>& b,
typename Quat<Vec3>::Float t)
noexcept
234 F dot = a.W * b.W + DotProduct(a.V, b.V);
235 Quat<Vec3> b1 = (dot < 0) ? Quat<Vec3>{-b.W, -b.V} : b;
242 return Quat<Vec3>{ v, w }.Normalized();
245 F phi = std::acos(dot);
246 F invSinPhi = F(1.0) / std::sin(phi);
247 F wA = std::sin((1 - t) * phi) * invSinPhi;
248 F wB = std::sin(t * phi) * invSinPhi;
250 .V = wA * a.V + wB * b1.V,
251 .W = wA * a.W + wB * b1.W
260 [[nodiscard]]
static JPL_INLINE Quat<Vec3> GetDeltaQuat(
const Quat<Vec3>& a,
const Quat<Vec3> b)
noexcept
262 return a * b.Conjugated();
266 [[nodiscard]]
static JPL_INLINE Quat<Vec3> QuatRotation(
const Vec3& axis,
Internal::FloatOf<Vec3> angleRad)
noexcept
272 [[nodiscard]]
static JPL_INLINE Quat<Vec3> QuatFromTo(
const Vec3& from,
const Vec3& to)
noexcept
278 [[nodiscard]]
static JPL_INLINE Quat<Vec3> QuatFromBasis(
const Basis<Vec3>& basis)
noexcept
284 [[nodiscard]]
static JPL_INLINE Quat<Vec3> QuatFromUpAndForward(
const Vec3& up,
const Vec3& forward)
noexcept
290 [[nodiscard]]
static JPL_INLINE Quat<Vec3> QuatLookAt(
const Vec3& direction,
const Vec3& up)
noexcept
std::remove_cvref_t< decltype(GetX(std::declval< Vec3 >()))> FloatOf
Definition Vec3Math.h:36
JPL_INLINE constexpr T Lerp(const T &v0, const T &v1, T t) noexcept
Linearly interpolate v0 towards v1.
Definition Math.h:172
JPL_INLINE constexpr T Sqrt(T x) noexcept
Definition Math.h:269
JPL_INLINE constexpr auto Abs(const T &value) noexcept
Standard abs is not constexpr in C++20.
Definition Math.h:87
JPL_INLINE std::pair< T, T > SinCos(T value) noexcept
Definition Math.h:164
Vec3 GetNormalizedPerpendicular(const Vec3 &vec) noexcept
Definition Vec3Math.h:67
Definition AcousticMaterial.h:36
JPL_INLINE auto GetX(const Vec3Type &v) noexcept
Definition Vec3Traits.h:35
JPL_INLINE auto GetZ(const Vec3Type &v) noexcept
Definition Vec3Traits.h:37
JPL_INLINE auto GetY(const Vec3Type &v) noexcept
Definition Vec3Traits.h:36
std::ostream & operator<<(std::ostream &os, const AcousticMaterial &v)
Definition AcousticMaterial.h:104
Orthonormal basis (column-major)
Definition MinimalBasis.h:35
Minimal quaternion (w + xi + yj + zk)
Definition MinimalQuat.h:38
JPL_INLINE Float Length() const noexcept
Definition MinimalQuat.h:192
Float W
Definition MinimalQuat.h:41
JPL_INLINE Float GetRotationAngle(const Vec3 &axis) const noexcept
Get rotation angle around axis.
Definition MinimalQuat.h:206
JPL_INLINE Quat Conjugated() const noexcept
Definition MinimalQuat.h:49
static JPL_INLINE Quat LookAt(const Vec3 &direction, const Vec3 &up) noexcept
Definition MinimalQuat.h:141
bool operator!=(const Quat &rhs) const noexcept
Definition MinimalQuat.h:45
JPL_INLINE Quat Inversed() const noexcept
Definition MinimalQuat.h:51
bool operator==(const Quat &rhs) const noexcept
Check if two quaternions are exactly equal.
Definition MinimalQuat.h:44
JPL_INLINE friend Quat operator*(const Quat &a, const Quat &b) noexcept
Quaternion multiplication (rotation composition)
Definition MinimalQuat.h:147
static JPL_INLINE Quat FromUpAndForward(const Vec3 &up, const Vec3 &forward) noexcept
Definition MinimalQuat.h:136
JPL_INLINE Vec3 Rotate(const Vec3 &vector) const noexcept
Rotate a vector by this quaternion.
Definition MinimalQuat.h:183
static Quat FromTo(const Vec3 &from, const Vec3 &to) noexcept
Definition MinimalQuat.h:69
static Quat FromBasis(const Basis< Vec3 > &basis) noexcept
Construct quaternian from basis columns X, Y, Z.
Definition MinimalQuat.h:84
Vec3 V
Definition MinimalQuat.h:40
Basis< Vec3 > ToBasis() const noexcept
Convert to an orthonormal Basis3 (column-major)
Definition MinimalQuat.h:212
static JPL_INLINE Quat Rotation(const Vec3 &axis, Float angleRad) noexcept
Definition MinimalQuat.h:60
JPL_INLINE Float LengthSquared() const noexcept
Definition MinimalQuat.h:190
Internal::FloatOf< Vec3 > Float
Definition MinimalQuat.h:39
JPL_INLINE bool IsNormalized(Float tolerance=Float(1.0e-5)) const noexcept
Definition MinimalQuat.h:200
static JPL_INLINE Quat MakeSlerp(const Vec3 &from, const Vec3 &to, Float t) noexcept
Create quaternian to slerp direction vector based on 'from' and 'to'.
Definition MinimalQuat.h:176
JPL_INLINE Quat Normalized() const noexcept
Definition MinimalQuat.h:194
static JPL_INLINE Quat Identity() noexcept
Definition MinimalQuat.h:47