2525#include < PAX_SAPIENTICA/FileRead/Convert.hpp>
2626#include < PAX_SAPIENTICA/FileRead/Read.hpp>
2727#include < PAX_SAPIENTICA/FileRead/Split.hpp>
28+ #include < PAX_SAPIENTICA/Simulation/Data.hpp>
2829#include < PAX_SAPIENTICA/Simulation/GeographicInformation.hpp>
2930#include < PAX_SAPIENTICA/Type/Vector2.hpp>
3031
@@ -34,116 +35,61 @@ namespace paxs {
3435 public:
3536 using Vector2 = paxs::Vector2<T>;
3637
37- int pixel_size = 256 ;
38- std::map<Vector2, GeographicInformation> geographic_informations;
38+ const int pixel_size = 256 ;
3939
40- Environment () = default ;
41- Environment (const std::string& land_file_path, const std::string& slope_file_path, const Vector2& start_position, const Vector2& end_position, const int z) : start_position(start_position), end_position(end_position), z(z) {
42- // TODO: ファイルパスをファイルで管理する
43- // TODO:: z10のslopeファイルを作成する
44- // loadIsLand(land_file_path);
45- loadSlope (slope_file_path);
46- }
47-
48- Vector2 getStartPosition () const { return start_position; }
49- Vector2 getEndPosition () const { return end_position; }
50- bool isLand (const Vector2& position) const {
51- auto it = geographic_informations.find (position);
52- if (it != geographic_informations.end ()) {
53- return it->second .isLand ();
54- }
55- return false ;
56- }
57- private:
58- Vector2 start_position;
59- Vector2 end_position;
60- int z;
61-
62- void loadIsLand (const std::string& land_file_path) {
63- std::cout << " Loading is land..." << std::endl;
64- const std::vector<std::string> file_names = getFileNames (land_file_path);
65- std::cout << file_names.size () << " files are found." << std::endl;
66-
67- unsigned int file_count = 0 ;
68-
69- for (const auto & file_name : file_names) {
70- displayProgressBar (file_count, int (file_names.size ()));
40+ using DataVariant = std::variant<Data<std::uint_least8_t >, Data<std::uint_least32_t >, Data<float >>;
41+ std::map<std::string, DataVariant> data_map;
7142
72- Vector2 default_position = (getXAndYFromFileName (file_name) - start_position) * pixel_size;
73- std::vector<std::string> file = readFile (file_name);
74- for (std::size_t y = 0 ;y < file.size ();++y) {
75- for (std::size_t x = 0 ;x < file[y].size ();++x) {
76- Vector2 position = default_position + Vector2 ((int )x, (int )y);
77- setIsLand (file_name, position, file[y][x]);
78- }
43+ Environment () = default ;
44+ Environment (const std::string& setting_file_path, const Vector2& start_position, const Vector2& end_position, const int z) : start_position(start_position), end_position(end_position), z(z) {
45+ std::vector<std::vector<std::string>> settings = readTSV (setting_file_path);
46+ // 1行目からdata_typeのカラム番号を取得
47+ int key_column = -1 ;
48+ int data_type_column = -1 ;
49+ int file_path_column = -1 ;
50+ int z_column = -1 ;
51+ for (std::size_t i = 0 ;i < settings[0 ].size ();++i) {
52+ if (settings[0 ][i] == " key" ) {
53+ key_column = i;
54+ }
55+ if (settings[0 ][i] == " data_type" ) {
56+ data_type_column = i;
57+ }
58+ if (settings[0 ][i] == " file_path" ) {
59+ file_path_column = i;
60+ }
61+ if (settings[0 ][i] == " z" ) {
62+ z_column = i;
7963 }
80- ++file_count;
81- }
82- displayProgressBar (file_count, int (file_names.size ()));
83- std::cout << std::endl << " Loading is land is completed." << std::endl;
84- }
85- void setIsLand (const std::string& file_name, const Vector2& position, const char value) {
86- if (value == ' 1' ) {
87- geographic_informations[position] = GeographicInformation (true );
88- }
89- else if (value == ' 0' ) {
90- geographic_informations[position] = GeographicInformation ();
9164 }
92- else {
93- std::cerr << " Error: " << file_name << " is not a binary file ." << std::endl;
94- std:: exit (1 );
65+ if (key_column == - 1 | data_type_column == - 1 | file_path_column == - 1 | z_column == - 1 ) {
66+ std::cerr << " Error: column is not found ." << std::endl;
67+ exit (1 );
9568 }
96- }
97- void loadSlope (const std::string& slope_file_path) {
98- std::cout << " Loading slope..." << std::endl;
99- const std::vector<std::string> file_names = getFileNames (slope_file_path);
100- std::cout << file_names.size () << " files are found." << std::endl;
101-
102- unsigned int file_count = 0 ;
10369
104- for (const auto & file_name : file_names) {
105- displayProgressBar (file_count, int (file_names.size ()));
106-
107- Vector2 default_position = (getXAndYFromFileName (file_name) - start_position) * pixel_size;
108- std::vector<std::string> file = readFile (file_name);
109- for (std::size_t y = 0 ;y < file.size ();++y) {
110- // タブ区切り
111- std::vector<std::string> values = split (file[y], ' \t ' );
112- for (std::size_t x = 0 ;x < values.size ();++x) {
113- Vector2 position = default_position + Vector2 ((int )x, (int )y);
114- setSlope (file_name, position, values[x]);
115- }
116- }
117- ++file_count;
118- }
119- displayProgressBar (file_count, int (file_names.size ()));
120- std::cout << std::endl << " Loading slope is completed." << std::endl;
121- }
122- void setSlope (const std::string& file_name, const Vector2& position, const std::string value) {
123- auto result = convertString (value);
124- if (std::holds_alternative<double >(result)) {
125- // mapにすでに登録されているかどうか
126- auto it = geographic_informations.find (position);
127- if (it != geographic_informations.end ()) {
128- it->second .setSlope (std::get<double >(result));
70+ for (std::size_t i = 1 ;i < settings.size ();++i) {
71+ // 型名をstringからTに変換
72+ std::string data_type = settings[i][data_type_column];
73+ std::string key = settings[i][key_column];
74+ if (data_type == " u8" ){
75+ data_map.emplace (key, Data<std::uint_least8_t >(settings[i][file_path_column], key, start_position, end_position, std::stoi (settings[i][z_column]), z));
76+ } else if (data_type == " u32" ){
77+ data_map.emplace (key, Data<std::uint_least32_t >(settings[i][file_path_column], key, start_position, end_position, std::stoi (settings[i][z_column]), z));
78+ } else if (data_type == " f32" ){
79+ data_map.emplace (key, Data<float >(settings[i][file_path_column], key, start_position, end_position, std::stoi (settings[i][z_column]), z));
80+ } else {
81+ std::cerr << " Error: data_type is not found." << std::endl;
82+ exit (1 );
12983 }
130- // else {
131- // geographic_informations[position] = GeographicInformation(std::get<double>(result));
132- // }
13384 }
13485 }
135- Vector2 getXAndYFromFileName (const std::string& file_name) {
136- std::regex pattern (R"( (\w+)_(\d+)_(\d+)_(\d+)\.(\w+))" );
137- std::smatch matches;
13886
139- if (std::regex_search (file_name, matches, pattern)) {
140- return Vector2 (std::stoi (matches[3 ]), std::stoi (matches[4 ]));
141- }
142- else {
143- std::cerr << " Error: The format of " << file_name << " is incorrect." << std::endl;
144- std::exit (1 );
145- }
146- }
87+ Vector2 getStartPosition () const { return start_position; }
88+ Vector2 getEndPosition () const { return end_position; }
89+ private:
90+ Vector2 start_position;
91+ Vector2 end_position;
92+ int z;
14793 };
14894}
14995
0 commit comments