33 #ifndef HERMES_HERMES_GEOMETRY_QUATERNION_H
34 #define HERMES_HERMES_GEOMETRY_QUATERNION_H
47 static_assert(std::is_same<T, f32>::value || std::is_same<T, f64>::value ||
49 "Quaternion must hold a float type!");
104 float Nv =
v.x *
v.x +
v.y *
v.y +
v.z *
v.z +
r *
r;
105 float s = (Nv > 0.f) ? (2.f / Nv) : 0.f;
106 float xs =
v.x * s, ys =
v.y * s, zs =
v.z * s;
107 float wx =
r * xs, wy =
r * ys, wz =
r * zs;
108 float xx =
v.x * xs, xy =
v.x * ys, xz =
v.x * zs;
109 float yy =
v.y * ys, yz =
v.y * zs, zz =
v.z * zs;
110 m[0][0] = 1.f - (yy + zz);
114 m[1][1] = 1.f - (xx + zz);
118 m[2][2] = 1.f - (xx + yy);
119 m[0][3] = m[1][3] = m[2][3] = m[3][0] = m[3][1] = m[3][2] = 0.f;
126 T d = 1 / (
r *
r +
v.length2());
127 return {-
v * d,
r * d};
137 return v.length2() +
r *
r;
142 return sqrtf(
v.length2() +
r *
r);
147 auto d =
v.length2() +
r *
r;
168 return {q.
v / s, q.
r / s};
177 return {q.
v * s, q.
r * s};
186 return {q.
v * s, q.
r * s};
196 return {q.
v + p.
v, q.
r + p.
r};
206 return {q.
v - p.
v, q.
r - p.
r};
228 os <<
"Quaternion [(" << v[0] <<
" " << v[1] <<
" " << v[2] <<
"), " << v.w <<
"]";
235 using quat = Quaternion<real_t>;
236 using quatf = Quaternion<f32>;
237 using quatd = Quaternion<f64>;
4x4 Matrix representation
Definition: matrix.h:121
Quaternion representation v.x i + v.y j + v.z k + r.
Definition: quaternion.h:46
HERMES_DEVICE_CALLABLE Quaternion conjugate() const
Computes conjugate of this quaternion.
Definition: quaternion.h:131
T r
scalar part
Definition: quaternion.h:155
HERMES_DEVICE_CALLABLE Quaternion(T x, T y, T z, T w)
Construct from component values.
Definition: quaternion.h:72
HERMES_DEVICE_CALLABLE Quaternion inverse() const
Computes inverse of this quaternion.
Definition: quaternion.h:125
HERMES_DEVICE_CALLABLE const T & operator[](int i) const
Get i-th component.
Definition: quaternion.h:89
hermes::Vector3< T > v
vector part
Definition: quaternion.h:154
HERMES_DEVICE_CALLABLE Quaternion(const hermes::Vector3< T > &v, T r=0)
Construct from part values.
Definition: quaternion.h:76
HERMES_DEVICE_CALLABLE Quaternion normalized() const
Computes normalized copy.
Definition: quaternion.h:146
static HERMES_DEVICE_CALLABLE Quaternion< T > I()
Creates an identity quaternion (0,0,0,1)
Definition: quaternion.h:56
HERMES_DEVICE_CALLABLE T & operator[](int i)
Get i-th component reference.
Definition: quaternion.h:94
HERMES_DEVICE_CALLABLE T length() const
Computes the norm.
Definition: quaternion.h:141
HERMES_DEVICE_CALLABLE Quaternion()
Default constructor.
Definition: quaternion.h:66
HERMES_DEVICE_CALLABLE T length2() const
Computes the squared norm.
Definition: quaternion.h:136
HERMES_DEVICE_CALLABLE Matrix4x4< T > matrix() const
Definition: quaternion.h:102
Geometric 3-dimensional vector (x, y, z)
Definition: vector.h:166
#define HERMES_DEVICE_CALLABLE
Specifies that the function can be called from both host and device sides.
Definition: defs.h:45
#define HERMES_CHECK_EXP(expr)
Warns if expression is false.
Definition: debug.h:95
HERMES_DEVICE_CALLABLE T dot(const Normal3< T > &n, const Vector3< T > &v)
Computes dot product with vector.
Definition: normal.h:237
HERMES_DEVICE_CALLABLE Quaternion< T > operator+(const Quaternion< T > &q, const Quaternion< T > &p)
Adds two quaternions.
Definition: quaternion.h:195
HERMES_DEVICE_CALLABLE Quaternion< T > operator-(const Quaternion< T > &q, const Quaternion< T > &p)
Subtracts p from q.
Definition: quaternion.h:205
HERMES_DEVICE_CALLABLE Quaternion< T > operator/(const Quaternion< T > &q, T s)
Scalar division.
Definition: quaternion.h:167
HERMES_DEVICE_CALLABLE T cross(const Vector2< T > &a, const Vector2< T > &b)
Computes the cross product between two vectors.
Definition: vector.h:550