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