GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
math_vector3.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_MATH_VECTOR3_H_
9#define DESIGNLAB_MATH_VECTOR3_H_
10
11#include <cmath>
12#include <string>
13
14#include "math_util.h"
15#include "math_vector2.h"
16
17
18namespace designlab
19{
20
39struct Vector3 final
40{
42 constexpr Vector3() : x(0.f), y(0.f), z(0.f) {}
43
44 constexpr Vector3(const float x_pos, const float y_pos, const float z_pos) :
45 x(x_pos),
46 y(y_pos),
47 z(z_pos)
48 {
49 }
50
52 constexpr Vector3(const Vector3& other) = default;
53
55 constexpr Vector3(Vector3&& other) noexcept = default;
56
58 constexpr Vector3& operator = (const Vector3& other) = default;
59
60 ~Vector3() = default;
61
62
64 constexpr bool operator == (const Vector3& v) const noexcept
65 {
66 if (math_util::IsEqual(v.x, x) && math_util::IsEqual(v.y, y) && math_util::IsEqual(v.z, z))
67 {
68 return true;
69 }
70
71 return false;
72 }
73
74 constexpr bool operator != (const Vector3& other) const noexcept
75 {
76 return !(*this == other);
77 }
78
79 bool operator < (const Vector3& other) const noexcept
80 {
81 // 長さで比較する.
82 return GetLength() < other.GetLength();
83 }
84
85 bool operator > (const Vector3& other) const noexcept
86 {
87 return other < *this;
88 }
89
90 bool operator <= (const Vector3& other) const noexcept
91 {
92 return !(*this > other);
93 }
94
95 bool operator >= (const Vector3& other) const noexcept
96 {
97 return !(*this < other);
98 }
99
100 // 算術演算子,ベクトル同士の掛け算(内積・外積)はメンバ関数として実装する.
101 constexpr Vector3 operator +() const noexcept { return *this; }
102
103 constexpr Vector3 operator -() const noexcept { return { -x, -y, -z }; }
104
105 constexpr Vector3 operator +(const Vector3& other) const noexcept
106 {
107 return { x + other.x , y + other.y, z + other.z };
108 }
109
110 constexpr Vector3 operator -(const Vector3& other) const noexcept
111 {
112 return { x - other.x , y - other.y, z - other.z };
113 }
114
115 constexpr Vector3 operator *(const float num) const noexcept
116 {
117 return { x * num, y * num, z * num };
118 }
119
120 constexpr Vector3 operator /(const float num) const
121 {
122 return { x / num, y / num, z / num };
123 }
124
125 Vector3& operator += (const Vector3& other) noexcept;
126 Vector3& operator -= (const Vector3& other) noexcept;
127 Vector3& operator *= (const float num) noexcept;
128 Vector3& operator /= (const float num);
129
130
134 [[nodiscard]] constexpr float GetSquaredLength() const noexcept
135 {
136 return x * x + y * y + z * z;
137 }
138
143 [[nodiscard]] inline float GetLength() const noexcept
144 {
145 return std::sqrt(GetSquaredLength());
146 }
147
151 [[nodiscard]] constexpr float Dot(const Vector3& other) const noexcept
152 {
153 return x * other.x + y * other.y + z * other.z;
154 }
155
160 [[nodiscard]] constexpr Vector3 Cross(const Vector3& other) const noexcept
161 {
162 return Vector3{ y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x };
163 }
164
168 [[nodiscard]] inline float GetDistanceFrom(const Vector3& other) const noexcept
169 {
170 return (*this - other).GetLength();
171 }
172
177 [[nodiscard]] Vector3 GetNormalized() const noexcept;
178
182 [[nodiscard]] constexpr bool IsZero() const noexcept
183 {
184 if (math_util::IsEqual(x, 0.0f) && math_util::IsEqual(y, 0.0f) && math_util::IsEqual(z, 0.0f))
185 {
186 return true;
187 }
188
189 return false;
190 }
191
197 void Normalize() noexcept { *this = GetNormalized(); }
198
201 [[nodiscard]]
202 constexpr Vector2 ProjectedXY() const noexcept { return { x, y }; }
203
210 [[nodiscard]]
211 constexpr static Vector3 GetFrontVec() noexcept { return { 1, 0, 0 }; }
212
216 [[nodiscard]]
217 constexpr static Vector3 GetLeftVec() noexcept { return { 0, 1, 0 }; }
218
222 [[nodiscard]]
223 constexpr static Vector3 GetUpVec() noexcept { return { 0, 0, 1 }; }
224
228 [[nodiscard]]
229 constexpr static Vector3 GetZeroVec() noexcept { return { 0, 0, 0 }; }
230
231
235 [[nodiscard]] std::string ToString() const;
236
240 [[nodiscard]] std::string ToCsvString() const;
241
242
243 float x;
244 float y;
245 float z;
246};
247
248
253constexpr Vector3 operator * (const float s, const Vector3& vec) noexcept
254{
255 return { s * vec.x, s * vec.y, s * vec.z };
256}
257
258
261template <class Char>
262std::basic_ostream<Char>& operator <<(std::basic_ostream<Char>& os, const Vector3& v)
263{
264 return os << math_util::FloatingPointNumToString(v.x) << Char(',') <<
265 math_util::FloatingPointNumToString(v.y) << Char(',') <<
267}
268
269
271template <class Char>
272inline std::basic_istream<Char>& operator >>(std::basic_istream<Char>& is, Vector3& v)
273{
274 Char unused = 0;
275 return is >> v.x >> unused >> v.y >> unused >> v.z;
276}
277
278
279// 正規化直行座標系となっているか確認する,
280// 条件を満たさないならばコンパイルが通らない.
281static_assert(math_util::IsEqual(Vector3::GetFrontVec().Dot(Vector3::GetLeftVec()), 0.f), "The dot product of FrontVec and LeftVec is not zero.");
282
283static_assert(math_util::IsEqual(Vector3::GetFrontVec().Dot(Vector3::GetUpVec()), 0.f), "The dot product of FrontVec and UpVec is not zero.");
284
285static_assert(math_util::IsEqual(Vector3::GetLeftVec().Dot(Vector3::GetUpVec()), 0.f), "The dot product of LeftVec and UpVec is not zero.");
286
287static_assert(math_util::IsEqual(Vector3::GetFrontVec().GetSquaredLength(), 1.f), "FrontVec is not normalized.");
288
289static_assert(math_util::IsEqual(Vector3::GetLeftVec().GetSquaredLength(), 1.f), "LeftVec is not normalized.");
290
291static_assert(math_util::IsEqual(Vector3::GetUpVec().GetSquaredLength(), 1.f), "UpVec is not normalized.");
292
293// 0ベクトルが返ってくるか確認する.
294static_assert(Vector3::GetZeroVec().x == 0.f, "It is not a 0 vector.");
295static_assert(Vector3::GetZeroVec().y == 0.f, "It is not a 0 vector.");
296static_assert(Vector3::GetZeroVec().z == 0.f, "It is not a 0 vector.");
297
298} // namespace designlab
299
300
301#endif // DESIGNLAB_MATH_VECTOR3_H_
std::string FloatingPointNumToString(const T num, const int digit=kDigit, const int width=kWidth)
小数を文字列に変換する関数. C++ では C のフォーマットのように %3.3f とかで小数を文字列に変換できないため自作する.
Definition math_util.h:161
constexpr bool IsEqual(const T num1, const T num2) noexcept
C++において,小数同士の計算は誤差が出てしまう. 誤差込みで値が等しいか調べる.
Definition math_util.h:45
constexpr Quaternion operator*(float s, const Quaternion &q)
std::basic_istream< Char > & operator>>(std::basic_istream< Char > &is, EulerXYZ &r)
入力ストリーム
Definition math_euler.h:128
std::basic_ostream< Char > & operator<<(std::basic_ostream< Char > &os, const EulerXYZ &r)
出力ストリーム.Csv形式で出力する.カンマ区切り.単位は [rad].
Definition math_euler.h:117
2次元の位置ベクトルを表す構造体.
3次元の位置ベクトルを表す構造体.
constexpr Vector3 operator+() const noexcept
Vector3 & operator*=(const float num) noexcept
float x
ロボットの正面方向に正.
constexpr bool operator==(const Vector3 &v) const noexcept
2つのベクトルが等しいかどうかを返す.誤差を許容する.
constexpr Vector3 Cross(const Vector3 &other) const noexcept
自分×引数 の外積の結果を返す.
constexpr float GetSquaredLength() const noexcept
ベクトルの長さの2乗を返す.
bool operator<(const Vector3 &other) const noexcept
Vector3 & operator-=(const Vector3 &other) noexcept
static constexpr Vector3 GetUpVec() noexcept
上に進む単位ベクトルを返す. 静的な関数なので,Vector3::GetUpVec() と呼び出せる.
constexpr Vector3(const float x_pos, const float y_pos, const float z_pos)
constexpr bool IsZero() const noexcept
x,y,zともに絶対値が0ならば true を返す.
constexpr bool operator!=(const Vector3 &other) const noexcept
Vector3 GetNormalized() const noexcept
単位ベクトルを返す. normalizeとは,ベクトルを正規化(単位ベクトルに変換)する操作を表す. 絶対値が0のベクトルの場合,そのまま0ベクトルを返す.
constexpr Vector3 operator*(const float num) const noexcept
constexpr Vector3 operator-() const noexcept
constexpr Vector3 operator/(const float num) const
float z
ロボットの上向きに正.
constexpr Vector3()
< デフォルトコンストラクタ.(0,0,0)で初期化される.
bool operator>=(const Vector3 &other) const noexcept
std::string ToCsvString() const
このベクトルをCSV形式の文字列にして返す. x, y, z の形式,小数点以下3桁まで.
static constexpr Vector3 GetFrontVec() noexcept
正面に進む単位ベクトルを返す. 静的な関数なので,Vector3::GetFrontVec() と呼び出せる.
constexpr Vector3(const Vector3 &other)=default
コピーコンストラクタ.
constexpr Vector3 & operator=(const Vector3 &other)=default
代入演算子.
constexpr float Dot(const Vector3 &other) const noexcept
自分・引数 の内積の結果を返す.
float GetLength() const noexcept
ベクトルの長さを返す.
float GetDistanceFrom(const Vector3 &other) const noexcept
別のベクトルと,このベクトルの距離を返す.
constexpr Vector2 ProjectedXY() const noexcept
XY平面に射影したベクトルを返す.
void Normalize() noexcept
このベクトルを正規化する. 絶対値が0のベクトルの場合,そのまま0ベクトルになる.
static constexpr Vector3 GetLeftVec() noexcept
左に進む単位ベクトルを返す. 静的な関数なので,Vector3::GetLeftVec() と呼び出せる.
bool operator<=(const Vector3 &other) const noexcept
Vector3 & operator+=(const Vector3 &other) noexcept
constexpr Vector3(Vector3 &&other) noexcept=default
ムーブコンストラクタ.
Vector3 & operator/=(const float num)
static constexpr Vector3 GetZeroVec() noexcept
零ベクトルを返す. 静的な関数なので,Vector3::GetZeroVec() と呼び出せる.
std::string ToString() const
このベクトルを文字列にして返す. (x, y, z) の形式,小数点以下3桁まで.
float y
ロボットの左向きに正.
bool operator>(const Vector3 &other) const noexcept