GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
divided_map_state.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 "divided_map_state.h"
9
10#include "math_util.h"
11
12
13namespace designlab
14{
15
17 kMapMinZ(min_z),
18 global_robot_com_{},
19 divided_map_point_(math_util::Squared(kDividedNum)),
20 divided_map_top_z_(math_util::Squared(kDividedNum))
21{
22 // コンストラクタで,配列のサイズを確保しておく.
23 Clear();
24}
25
26void DividedMapState::Init(const MapState& map_state, const Vector3 global_robot_com)
27{
28 // 配列のサイズが確保されているか確認.
29 assert(divided_map_point_.size() == math_util::Squared(kDividedNum));
30 assert(divided_map_top_z_.size() == math_util::Squared(kDividedNum));
31
32 Clear();
33
34 global_robot_com_ = global_robot_com; // ロボットの位置を更新する.
35
36 // マップのデータ全てを参照し,切り分ける.
37 const size_t kMapPointSize = map_state.GetMapPointSize();
38
39 for (size_t i = 0; i < kMapPointSize; ++i)
40 {
41 // xy方向のブロック番号をそれぞれ求める.
42 const designlab::Vector3 point = map_state.GetMapPoint(i);
43
44 // 範囲内にいないならば処理をやめ,続行.
45 if (!IsInMap(point)) { continue; }
46
47 const int x = GetDividedMapIndexX(point.x);
48 const int y = GetDividedMapIndexY(point.y);
49
50 // マップの範囲内にいる時のみ追加する.
51 if (IsValidIndex(x) && IsValidIndex(y))
52 {
53 divided_map_point_[GetDividedMapIndex(x, y)].push_back(point);
54
55 // 最大値を更新する.
56 divided_map_top_z_[GetDividedMapIndex(x, y)] =
57 (std::max)(point.z, divided_map_top_z_[GetDividedMapIndex(x, y)]);
58 }
59 }
60}
61
63{
64 // vectorの中身を全てクリアする.
65 for (auto& i : divided_map_point_)
66 {
67 i.clear();
68 }
69
70 // 最小値で埋める.
71 for (auto& i : divided_map_top_z_)
72 {
73 i = kMapMinZ;
74 }
75}
76
77int DividedMapState::GetPointNum(const int x_index, const int y_index) const
78{
79 // 存在していなければ終了
80 if (!IsValidIndex(x_index) || !IsValidIndex(y_index))
81 {
82 return 0;
83 }
84
85 return static_cast<int>(divided_map_point_[GetDividedMapIndex(x_index, y_index)].size());
86}
87
88Vector3 DividedMapState::GetPointPos(int x_index, int y_index, int devide_map_index) const
89{
90 // 存在していなければ終了.
91 if (!IsValidIndex(x_index) || !IsValidIndex(y_index))
92 {
93 return Vector3{ 0, 0, 0 };
94 }
95
96 const size_t size = divided_map_point_[GetDividedMapIndex(x_index, y_index)].size();
97
98 if (devide_map_index < 0 || static_cast<int>(size) <= devide_map_index)
99 {
100 return Vector3::GetZeroVec();
101 }
102
103 // 存在しているならば値を返す.
104 return divided_map_point_[GetDividedMapIndex(x_index, y_index)][devide_map_index];
105}
106
107void DividedMapState::GetPointVector(int x_index, int y_index,
108 std::vector<Vector3>* point_vec) const
109{
110 if (point_vec == nullptr)
111 {
112 return;
113 }
114
115 // 存在していなければ終了.
116 if (!IsValidIndex(x_index) || !IsValidIndex(y_index))
117 {
118 return;
119 }
120
121 (*point_vec) = divided_map_point_[GetDividedMapIndex(x_index, y_index)];
122}
123
124float DividedMapState::GetTopZ(int x_index, int y_index) const
125{
126 // 存在していなければ終了.
127 if (!IsValidIndex(x_index) || !IsValidIndex(y_index))
128 {
129 return kMapMinZ;
130 }
131
132 return divided_map_top_z_[GetDividedMapIndex(x_index, y_index)];
133}
134
135} // namespace designlab
constexpr int GetDividedMapIndexX(const float pos_x) const noexcept
指定した座標を DividedMap のインデックスに変換する. 範囲外の値を指定した場合でも,値を丸めずに返す. そのため,IsInMap で範囲内に存在するかどうかを確認する必要がある.
Vector3 GetPointPos(int x_index, int y_index, int divided_map_index) const
格子状に切り分けられたマップから,脚設置可能点の実際の座標を取得する. 範囲外の値を指定した場合は,(0,0,0)を返す.
void Init(const MapState &map_state, const Vector3 global_robot_com)
マップのデータを初期化する. ロボットの重心座標を中心にマップのデータを格子状に分割し, その中に存在する脚設置可能点を集める.
constexpr bool IsInMap(const float x, const float y) const
指定した座標がマップの範囲内に存在するかどうかを返す.
void Clear()
マップのデータを消去する.
constexpr int GetDividedMapIndexY(const float pos_y) const noexcept
指定した座標を DividedMap のインデックスに変換する. 範囲外の値を指定した場合でも,値を丸めずに返す. そのため,IsInMap で範囲内に存在するかどうかを確認する必要がある.
int GetPointNum(int x_index, int y_index) const
格子状に切り分けられたマップから,脚設置可能点の数を取得する. 範囲外の値を指定した場合は,0を返す.
float GetTopZ(int x_index, int y_index) const
格子状に切り分けられたマップから,最も高いZ座標を返す.
void GetPointVector(int x_index, int y_index, std::vector< Vector3 > *point_vec) const
格子状に切り分けられたマップから,脚設置可能点の vector を取得する 範囲外の値を指定した場合は,空の vector を返す.
DividedMapState(float min_z=MapState::kMapMinZ)
static constexpr int kDividedNum
格子の数.
マップを表すクラス.
Definition map_state.h:32
size_t GetMapPointSize() const noexcept
脚設置可能点の総数を返す.
Definition map_state.h:53
Vector3 GetMapPoint(const size_t index) const noexcept
脚設置可能点の座標を返す.
Definition map_state.h:44
constexpr T Squared(const T num) noexcept
2乗した値を返す関数.
Definition math_util.h:59
3次元の位置ベクトルを表す構造体.
float x
ロボットの正面方向に正.
float z
ロボットの上向きに正.
static constexpr Vector3 GetZeroVec() noexcept
零ベクトルを返す. 静的な関数なので,Vector3::GetZeroVec() と呼び出せる.
float y
ロボットの左向きに正.