GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
math_vector3.cpp
[詳解]
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#include "math_vector3.h"
9
10#include <format>
11#include <sstream>
12
13#include "math_util.h"
14
15
16namespace designlab
17{
18
20{
21 const float GetLength = this->GetLength();
22
23 if (math_util::IsEqual(GetLength, 0.0f))
24 {
25 return { 0.0f, 0.0f, 0.0f };
26 }
27
28 // 割り算は遅いので,逆数をかける.
29 const float inv_length = 1.0f / GetLength;
30 return *this * inv_length;
31}
32
33std::string Vector3::ToString() const
34{
35 return std::format("( x : {}, y : {}, z : {} )",
39}
40
41std::string Vector3::ToCsvString() const
42{
43 std::stringstream ss;
44 ss << *this;
45 return ss.str();
46}
47
48Vector3& Vector3::operator+=(const Vector3& other) noexcept
49{
50 x += other.x;
51 y += other.y;
52 z += other.z;
53 return *this;
54}
55
56Vector3& Vector3::operator-=(const Vector3& other) noexcept
57{
58 x -= other.x;
59 y -= other.y;
60 z -= other.z;
61 return *this;
62}
63
64Vector3& Vector3::operator*=(const float other) noexcept
65{
66 x *= other;
67 y *= other;
68 z *= other;
69 return *this;
70}
71
72Vector3& Vector3::operator/=(const float other)
73{
74 x /= other;
75 y /= other;
76 z /= other;
77 return *this;
78}
79
80} // namespace designlab
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
3次元の位置ベクトルを表す構造体.
Vector3 & operator*=(const float num) noexcept
float x
ロボットの正面方向に正.
Vector3 & operator-=(const Vector3 &other) noexcept
Vector3 GetNormalized() const noexcept
単位ベクトルを返す. normalizeとは,ベクトルを正規化(単位ベクトルに変換)する操作を表す. 絶対値が0のベクトルの場合,そのまま0ベクトルを返す.
float z
ロボットの上向きに正.
std::string ToCsvString() const
このベクトルをCSV形式の文字列にして返す. x, y, z の形式,小数点以下3桁まで.
float GetLength() const noexcept
ベクトルの長さを返す.
Vector3 & operator+=(const Vector3 &other) noexcept
Vector3 & operator/=(const float num)
std::string ToString() const
このベクトルを文字列にして返す. (x, y, z) の形式,小数点以下3桁まで.
float y
ロボットの左向きに正.