GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
stability_margin_renderer.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
9
10#include <vector>
11
12#include <Dxlib.h>
13
14#include "dxlib_util.h"
15#include "math_polygon2.h"
16#include "leg_state.h"
17
18
19namespace designlab
20{
21
23 const std::shared_ptr<const IHexapodCoordinateConverter>& converter_ptr) :
24 kMarginColor(GetColor(0, 255, 0)),
25 kMarginErrorColor(GetColor(255, 0, 0)),
26 kAlpha(128),
27 converter_ptr_(converter_ptr)
28{
29}
30
31
33{
34 Polygon2 polygon_xy; // 平面に投影した多角形.
35
36 std::vector<Vector3> polygon; // 多角形の頂点.
37
38 Vector3 polygon_sum{ 0, 0, 0 }; // 多角形の頂点の合計,重心を求めるために使用する
39
40
41 for (int i = 0; i < HexapodConst::kLegNum; ++i)
42 {
43 if (leg_func::IsGrounded(node_.leg_state, i))
44 {
45 polygon.push_back(
46 converter_ptr_->ConvertLegToGlobalCoordinate(node_.leg_pos[i], i, node_.center_of_mass_global_coord, node_.posture, true));
47
48 polygon.back() += Vector3{ 0, 0, 5 }; // わかりやすさのため,高さを少し上げる.
49
50 polygon_xy.AddVertex(polygon.back().ProjectedXY());
51
52 polygon_sum += polygon.back();
53 }
54 }
55
56 // 重心の座標.
57 const Vector3 center = polygon_sum / static_cast<float>(polygon.size());
58
59
60 // 多角形を描画する.
61 for (size_t i = 0; i < polygon.size(); ++i)
62 {
63 const VECTOR poly[3] = {
65 dxlib_util::ConvertToDxlibVec(polygon[(i + 1) % polygon.size()]),
67 };
68
69 SetDrawBlendMode(DX_BLENDMODE_ALPHA, kAlpha);
70
71 if (polygon_xy.IsInside(node_.center_of_mass_global_coord.ProjectedXY()))
72 {
73 DrawTriangle3D(poly[0], poly[1], poly[2], kMarginColor, TRUE);
74 }
75 else
76 {
77 DrawTriangle3D(poly[0], poly[1], poly[2], kMarginErrorColor, TRUE);
78 }
79
80
81 SetDrawBlendMode(DX_BLENDMODE_NOBLEND, 0);
82 }
83
84 // 投射した重心を描画する.
85 VECTOR projected_center_pos = dxlib_util::ConvertToDxlibVec(
86 // わかりやすさのため,重心の高さを少し上げる.
87 { node_.center_of_mass_global_coord.x, node_.center_of_mass_global_coord.y, center.z + 10 });
88
89 DrawSphere3D(projected_center_pos, 5, 10, 10, GetColor(255, 255, 255), TRUE);
90}
91
92} // namespace designlab
static constexpr int kLegNum
StabilityMarginRenderer(const std::shared_ptr< const IHexapodCoordinateConverter > &converter_ptr)
void Draw() const override
描画処理を行う. const 関数にしているのは, 描画処理の中でメンバ変数を変更しないようにするため.
VECTOR ConvertToDxlibVec(const Vector3 &vec)
Dxlibの座標を示すVECTORと,このプログラムで使用しているVectorを変換する. ロボット座標系は右手座標系, Dxlibは左手座標系(工学は右手・ゲームライブラリは左手が多い)なのでyを...
Definition dxlib_util.h:35
bool IsGrounded(const LegStateBit &leg_state, const int leg_index)
脚番号 leg_index 0 ~ 5 に応じて,その脚が接地しているかを調べる. 脚は右前脚を0番として,時計回りに0,1,2,3,4,5となる.左前足が5番.
Definition leg_state.cpp:43
2次元の多角形を表す構造体.
bool IsInside(const Vector2 &point) const
点が多角形の内部にあるかどうか調べる関数. 多角形が凸でない場合は正しく判定できない.
constexpr void AddVertex(const Vector2 &v)
頂点を追加する関数.
std::array< Vector3, HexapodConst::kLegNum > leg_pos
[4 * 3 * 6 = 72 byte] 脚先の座標.(coxa(脚の付け根)を原点とする)
leg_func::LegStateBit leg_state
[4 byte] 脚状態,重心パターンを bitで表す.旧名 leg_con.
Vector3 center_of_mass_global_coord
[4 * 3 = 12byte] グローバル座標系における重心の位置.旧名 GCOM
Quaternion posture
[4 * 4 = 16byte] 姿勢を表すクォータニオン.
3次元の位置ベクトルを表す構造体.
float x
ロボットの正面方向に正.
float z
ロボットの上向きに正.
constexpr Vector2 ProjectedXY() const noexcept
XY平面に射影したベクトルを返す.
float y
ロボットの左向きに正.