5 #ifndef SPIDAR_QUATERNION_HPP 6 #define SPIDAR_QUATERNION_HPP 57 this->x =
static_cast<ValueType
>(x);
58 this->y =
static_cast<ValueType
>(y);
59 this->z =
static_cast<ValueType
>(z);
60 this->w =
static_cast<ValueType
>(
w);
63 Quaternion(ValueType angle,
const VectorType &axis)
65 w =
static_cast<ValueType
>(cos(0.5*angle));
67 ValueType s =
static_cast<ValueType
>(sin(0.5*angle) / axis.norm());
74 template <
class OtherType>
77 x =
static_cast<ValueType
>(other.x);
78 y =
static_cast<ValueType
>(other.y);
79 z =
static_cast<ValueType
>(other.z);
80 w =
static_cast<ValueType
>(other.w);
93 template <
class OtherType>
96 x =
static_cast<ValueType
>(other.x);
97 y =
static_cast<ValueType
>(other.y);
98 z =
static_cast<ValueType
>(other.z);
99 w =
static_cast<ValueType
>(other.w);
106 x +=
static_cast<ValueType
>(other.x);
107 y +=
static_cast<ValueType
>(other.y);
108 z +=
static_cast<ValueType
>(other.z);
109 w +=
static_cast<ValueType
>(other.
w);
116 x -=
static_cast<ValueType
>(other.x);
117 y -=
static_cast<ValueType
>(other.y);
118 z -=
static_cast<ValueType
>(other.z);
119 w -=
static_cast<ValueType
>(other.
w);
126 x =
static_cast<ValueType
>(value * x);
127 y =
static_cast<ValueType
>(value * y);
128 z =
static_cast<ValueType
>(value * z);
129 w =
static_cast<ValueType
>(value *
w);
138 temp.x = w * other.x + x * other.
w + y * other.z - z * other.y;
139 temp.y = w * other.y + y * other.
w + z * other.x - x * other.z;
140 temp.z = w * other.z + z * other.
w + x * other.y - y * other.x;
141 temp.
w = w * other.
w - x * other.x - y * other.y - z * other.z;
150 x =
static_cast<ValueType
>(x / value);
151 y =
static_cast<ValueType
>(y / value);
152 z =
static_cast<ValueType
>(z / value);
153 w =
static_cast<ValueType
>(w / value);
159 ValueType angle(
void)
const 163 temp = std::min(temp, check);
165 temp = std::max(temp, check);
166 return static_cast<ValueType
>(2.0 * std::acos(temp));
170 VectorType axis(
void)
const 172 VectorType &r =
static_cast<VectorType
>(*this);
173 ValueType norm = r.norm();
175 if (norm > 0)
return r / norm;
180 const ValueType dot(
const Quaternion &other)
const 182 return x * other.x + y * other.y + z * other.z + w * other.
w;
186 const ValueType norm(
void)
const 188 return static_cast<ValueType
>(std::sqrt(static_cast<double>(dot(*
this))));
194 double norm = std::sqrt(static_cast<double>(dot(*
this)));
196 if (norm>0)
return *
this / norm;
215 ValueType temp = dot(*
this);
226 const Quaternion derivative(
const VectorType &avel)
const 232 const VectorType angularVelocity(
const Quaternion &qd)
const 234 return *
reinterpret_cast<const VectorType*
>(&(qd * conjugated())) * 2.0;
238 const VectorType rotate(
const VectorType &v)
const 240 return *
reinterpret_cast<const VectorType*
>(&(*
this *
Quaternion(v) * conjugated()));
247 std::ostream& operator<<(std::ostream &os, const Quaternion<T> &q)
249 os <<
"(" << q.w <<
" " << q.x <<
" " << q.y <<
" " << q.z <<
")";
301 typedef typename QuaternionType::ValueType
ValueType;
305 double dot = from.dot(to);
314 if (dot > 1) dot = 1;
315 if (dot < -1) dot = -1;
317 double angle =
static_cast<ValueType
>(std::acos(dot));
318 ValueType sina =
static_cast<ValueType
>(std::sin(angle));
319 ValueType sinat =
static_cast<ValueType
>(std::sin(angle * t));
320 ValueType sinaomt =
static_cast<ValueType
>(std::sin(angle * (1.0-t)));
324 return QuaternionType((from.x * sinaomt + q.x * sinat) / sina,
325 (from.y * sinaomt + q.y * sinat) / sina,
326 (from.z * sinaomt + q.z * sinat) / sina,
327 (from.
w * sinaomt + q.
w * sinat) / sina);
338 #endif // SPIDAR_QUATERNION_HPP
Vector< T, 3 > VectorType
ベクトルの型