GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
result_file_importer.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 <format>
11#include <fstream>
12#include <sstream>
13
14#include <filesystem>
15
16#include "cassert_define.h"
17#include "cmdio_util.h"
18
19
20namespace fs = ::std::filesystem;
21
22namespace designlab
23{
24
26 const std::string& file_path, std::vector<RobotStateNode>* node_list,
27 MapState* map_state) const
28{
29 // 引数の確認.
30 assert(node_list != nullptr);
31 assert(node_list->empty());
32 assert(map_state != nullptr);
33 assert(map_state->GetMapPointSize() == 0);
34
35
36 // ファイルが存在するかどうかを確認.ないならば falseを返す.
37 if (!fs::exists(file_path))
38 {
39 CmdIOUtil::ErrorOutput("The 'NodeList' file did not exist.");
40 return false;
41 }
42
43 // node_list1.csv ならば map_state1.csvも読み込む.
44 std::string map_file_path = file_path;
45
46 map_file_path.replace(
47 map_file_path.find(ResultFileConst::kNodeListName),
50
51 if (!fs::exists(map_file_path))
52 {
53 CmdIOUtil::ErrorOutput("The MapState file did not exist.");
54 return false;
55 }
56
57
58 if (!ImportNodeList(file_path, node_list) ||
59 !ImportMapState(map_file_path, map_state))
60 {
61 CmdIOUtil::ErrorOutput("An error occurred while loading the file.");
62 }
63
64 return true;
65}
66
67bool ResultFileImporter::ImportNodeList(
68 const std::string& file_path,
69 std::vector<RobotStateNode>* node_list) const
70{
71 // ファイルを開く.
72 std::ifstream ifs(file_path);
73
74 // ファイルが開けないならば falseを返す.
75 if (!ifs.is_open())
76 {
77 CmdIOUtil::SystemOutput("Could not open the file.");
78
79 return false;
80 }
81
82
83 // ファイルを読み込む.
84 std::string str;
85 std::vector<std::string> str_list;
86
87 while (std::getline(ifs, str))
88 {
89 str_list.push_back(str);
90 }
91
92 // ファイルを閉じる.
93 ifs.close();
94
95 // ファイルの内容を解析する.
96 // ノードリストの読み込み.
97 for (const auto& i : str_list)
98 {
99 RobotStateNode node;
100 std::stringstream ss(i);
101
102 node = RobotStateNode::FromString(ss.str());
103
104 (*node_list).push_back(node);
105 }
106
107 return true;
108}
109
110bool ResultFileImporter::ImportMapState(
111 const std::string& file_path, MapState* map_state) const
112{
113 assert(map_state != nullptr);
114
115 // ファイルを開く.
116 std::ifstream ifs(file_path);
117
118 // ファイルが開けないならば falseを返す.
119 if (!ifs.is_open())
120 {
121 CmdIOUtil::SystemOutput("Could not open the file.");
122
123 return false;
124 }
125
126
127 // ファイルを読み込む.
128 std::string str;
129 std::vector<std::string> str_list;
130
131 while (std::getline(ifs, str))
132 {
133 str_list.push_back(str);
134 }
135
136 // ファイルを閉じる.
137 ifs.close();
138
139 // ファイルの内容を解析する.
140 (*map_state).ClearMapPoint();
141
142 for (const auto& i : str_list)
143 {
144 Vector3 point;
145 std::stringstream ss(i);
146
147 try
148 {
149 ss >> point;
150
151 (*map_state).AddMapPoint(point);
152 }
153 catch (...)
154 {
157 "読み込むことのできない行があったため無視します.「{}」",
158 ss.str());
159 }
160 }
161
162 return true;
163}
164
165
166} // namespace designlab
static void SystemOutput(const std::string &str)
コマンドラインに文字を出力する関数.System用の出力.
Definition cmdio_util.h:105
static void ErrorOutput(const std::string &str)
コマンドラインに文字を出力する関数.Error用の出力.
Definition cmdio_util.h:98
static void FormatOutput(OutputDetail detail, const std::format_string< Args... > str, Args &&... args)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
Definition cmdio_util.h:117
マップを表すクラス.
Definition map_state.h:32
size_t GetMapPointSize() const noexcept
脚設置可能点の総数を返す.
Definition map_state.h:53
static const std::string kNodeListName
ノードリストのファイル名 (プログラムの読み込み用)
static const std::string kMapStateName
マップ状態のファイル名 (プログラムの読み込み用)
bool ImportNodeListAndMapState(const std::string &file_path, std::vector< RobotStateNode > *node_list, MapState *map_state) const
ノードリストとマップの状態をファイルから読み込む.
@ kWarning
警告メッセージ,エラーではないが注意が必要なメッセージ.
static RobotStateNode FromString(const std::string &str)
文字列をノードの情報に変換する関数.