17#include <vsg/maths/mat4.h>
20# pragma GCC diagnostic push
21# pragma GCC diagnostic ignored "-Wpedantic"
24# pragma clang diagnostic push
25# pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
26# pragma clang diagnostic ignored "-Wnested-anon-types"
43 value_type x, y, z, w;
48 value{numbers<value_type>::zero(), numbers<value_type>::zero(), numbers<value_type>::zero(), numbers<value_type>::one()} {}
49 constexpr t_quat(
const t_quat& v) :
50 value{v.x, v.y, v.z, v.w} {}
51 constexpr t_quat(value_type in_x, value_type in_y, value_type in_z, value_type in_w) :
52 value{in_x, in_y, in_z, in_w} {}
55 set(angle_radians, axis);
63 constexpr explicit t_quat(
const t_quat<R>& v) :
64 value{
static_cast<T
>(v.x),
static_cast<T
>(v.y),
static_cast<T
>(v.z),
static_cast<T
>(v.w)} {}
66 constexpr t_quat& operator=(
const t_quat&) =
default;
68 constexpr std::size_t size()
const {
return 4; }
70 value_type& operator[](std::size_t i) {
return value[i]; }
71 value_type operator[](std::size_t i)
const {
return value[i]; }
74 t_quat& operator=(
const t_quat<R>& rhs)
76 value[0] =
static_cast<value_type
>(rhs[0]);
77 value[1] =
static_cast<value_type
>(rhs[1]);
78 value[2] =
static_cast<value_type
>(rhs[2]);
79 value[3] =
static_cast<value_type
>(rhs[3]);
83 T* data() {
return value; }
84 const T* data()
const {
return value; }
86 void set(value_type in_x, value_type in_y, value_type in_z, value_type in_w)
96 value_type len = length(axis);
97 if (len < numbers<value_type>::epsilon())
104 value_type inversenorm = numbers<value_type>::one() / len;
105 value_type coshalfangle = cos(numbers<value_type>::half() * angle_radians);
106 value_type sinhalfangle = sin(numbers<value_type>::half() * angle_radians);
108 x = axis.x * sinhalfangle * inversenorm;
109 y = axis.y * sinhalfangle * inversenorm;
110 z = axis.z * sinhalfangle * inversenorm;
116 value_type dot_pd = vsg::dot(from, to);
117 value_type div = std::sqrt(length2(from) * length2(to));
119 if (div - std::abs(dot_pd) < numbers<value_type>::epsilon())
121 axis = orthogonal(from);
125 axis = cross(from, to);
128 value_type len = length(axis);
130 double angle_radians = acos(dot_pd / div);
132 value_type inversenorm = numbers<value_type>::one() / len;
133 value_type coshalfangle = cos(numbers<value_type>::half() * angle_radians);
134 value_type sinhalfangle = sin(numbers<value_type>::half() * angle_radians);
136 x = axis.x * sinhalfangle * inversenorm;
137 y = axis.y * sinhalfangle * inversenorm;
138 z = axis.z * sinhalfangle * inversenorm;
142 explicit operator bool()
const noexcept {
return value[0] != numbers<value_type>::zero() || value[1] != numbers<value_type>::zero() || value[2] != numbers<value_type>::zero() || value[3] != numbers<value_type>::zero(); }
149 VSG_type_name(vsg::quat);
150 VSG_type_name(vsg::dquat);
151 VSG_type_name(vsg::ldquat);
156 return lhs[0] == rhs[0] && lhs[1] == rhs[1] && lhs[2] == rhs[2] && lhs[3] == rhs[3];
160 constexpr bool operator!=(
const t_quat<T>& lhs,
const t_quat<T>& rhs)
162 return lhs[0] != rhs[0] || lhs[1] != rhs[1] || lhs[2] != rhs[2] || lhs[3] != rhs[3];
168 if (lhs[0] < rhs[0])
return true;
169 if (lhs[0] > rhs[0])
return false;
170 if (lhs[1] < rhs[1])
return true;
171 if (lhs[1] > rhs[1])
return false;
172 if (lhs[2] < rhs[2])
return true;
173 if (lhs[2] > rhs[2])
return false;
174 return lhs[3] < rhs[3];
180 return t_quat<T>(lhs[0] - rhs[0], lhs[1] - rhs[1], lhs[2] - rhs[2], lhs[3] - rhs[3]);
186 return t_quat<T>(-v[0], -v[1], -v[2], v[3]);
198 return t_quat<T>(lhs[0] + rhs[0], lhs[1] + rhs[1], lhs[2] + rhs[2], lhs[3] + rhs[3]);
205 t_quat<T> q(rhs[3] * lhs[0] + rhs[0] * lhs[3] + rhs[1] * lhs[2] - rhs[2] * lhs[1],
206 rhs[3] * lhs[1] - rhs[0] * lhs[2] + rhs[1] * lhs[3] + rhs[2] * lhs[0],
207 rhs[3] * lhs[2] + rhs[0] * lhs[1] - rhs[1] * lhs[0] + rhs[2] * lhs[3],
208 rhs[3] * lhs[3] - rhs[0] * lhs[0] - rhs[1] * lhs[1] - rhs[2] * lhs[2]);
221 uuv = cross(qvec, uv);
222 uv *= (numbers<T>::two() * q[3]);
223 uuv *= numbers<T>::two();
230 return t_quat<T>(lhs[0] * rhs, lhs[1] * rhs, lhs[2] * rhs, lhs[3] * rhs);
236 T inv = numbers<T>::one() / rhs;
237 return t_quat<T>(lhs[0] * inv, lhs[1] * inv, lhs[2] * inv, lhs[3] * inv);
243 return std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
249 T inverse_len = numbers<T>::one() / length(v);
250 return t_quat<T>(v[0] * inverse_len, v[1] * inverse_len, v[2] * inverse_len, v[3] * inverse_len);
256 return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2] + lhs[3] * rhs[3];
263 T inverse_len = numbers<T>::one() / length(v);
264 return t_quat<T>(c[0] * inverse_len, c[1] * inverse_len, c[2] * inverse_len, c[3] * inverse_len);
270 T cosomega = dot(from, to);
271 if (cosomega < numbers<T>::zero())
273 cosomega = -cosomega;
280 if ((numbers<T>::one() - cosomega) > numbers<T>::epsilon())
282 T omega = acos(cosomega);
283 T sinomega = sin(omega);
284 T scale_from = sin((numbers<T>::one() - r) * omega) / sinomega;
285 T scale_to = sin(r * omega) / sinomega;
286 return (from * scale_from) + (to * scale_to);
291 return (from * (numbers<T>::one() - r)) + (to * r);
297#if defined(__clang__)
298# pragma clang diagnostic pop
301# pragma GCC diagnostic pop
t_quat template class that represents a quaternion
Definition quat.h:35
t_vec3 template class that represents a 3D vector
Definition vec3.h:34