25 return { 0.f, 0.f, 0.f, 0.f };
28 return *
this * (1.f / norm);
35 const float half_angle = angle * 0.5f;
37 return { cosf(half_angle),
38 Vector3{ axis.
x, axis.
y, axis.
z }.GetNormalized() * sinf(half_angle) };
60 assert(0 <= t && t <= 1);
62 if (q1 == q2) {
return q1; }
65 float dot = q1.
Dot(q2);
78 const float theta = acosf(dot);
83 const float sin_theta = sinf(theta);
84 const float sin_theta_inv = 1 / sin_theta;
86 const float sin_t_theta = sinf(t * theta);
87 const float sin_1_t_theta = sinf((1 - t) * theta);
90 return sin_1_t_theta * sin_theta_inv * q1 + sin_t_theta * sin_theta_inv * q2;
std::string FloatingPointNumToString(const T num, const int digit=kDigit, const int width=kWidth)
小数を文字列に変換する関数. C++ では C のフォーマットのように %3.3f とかで小数を文字列に変換できないため自作する.
constexpr bool IsEqual(const T num1, const T num2) noexcept
C++において,小数同士の計算は誤差が出てしまう. 誤差込みで値が等しいか調べる.
Quaternion SlerpQuaternion(const Quaternion &q1, const Quaternion &q2, const float t)
球面線形補間を行う.
Quaternion GetNormalized() const noexcept
正規化したクォータニオンを返す. クォータニオンの正規化とは,ノルムを1にすることを表す. クォータニオンqの正規化は,q / |q| で求められる.
std::string ToString() const
クォータニオンを文字列に変換する. w, x, y, z の順で出力する.
static Quaternion MakeByAngleAxis(float rad_angle, const Vector3 &axis)
回転軸と回転角からクォータニオンを作成する. q = cos(θ/2) * w + sin(θ/2) * { v.x + v.y + v.z } となる. ノルムは必ず1になる.
float GetNorm() const noexcept
クォータニオンのノルムを返す. ノルムとは,ベクトルの大きさのこと. クォータニオンのノルムは,w^2 + x^2 + y^2 + z^2 の平方根で求められる.
std::string ToCsvString() const
クォータニオンをCsv形式の文字列に変換する.カンマ区切り. w, x, y, z の順にカンマ区切りで出力する.
constexpr float Dot(Quaternion other) const noexcept
クォータニオンの内積を返す. クォータニオンを4次元のベクトルとみなし,ベクトルの内積を求める.