Skip to content

Commit 22281bb

Browse files
committed
change: マップのデータ構造の変更
1 parent 11ad322 commit 22281bb

File tree

7 files changed

+233
-113
lines changed

7 files changed

+233
-113
lines changed

Data/Simulation/MapList.tsv

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
key file_name_type data_type file_path z min_x min_y max_x max_y file_name_extension file_format
2-
elevation z_original f32 Data/Map/XYZTile/Elevation/Data/Elevation/2010/ 8 0 0 255 255 .tsv numeric_tsv
3-
slope z_original f32 Data/Map/XYZTile/Slope/Data/SlopeData20230514/2010/ 8 0 0 255 255 .tsv numeric_tsv
4-
gbank z_original u8 Data/Map/XYZTile/LandAndWater/Data/BlackAndWhiteBinary/1868/ 10 861 366 950 441 .txt numeric
5-
water z_original u8 Data/Map/XYZTile/RiversAndLakes/Data/RiversAndLakes/2023/ 12 3511 1465 3691 1712 .txt numeric
2+
elevation z_original f32 Data/Map/XYZTile/Elevation/Data/Elevation/2010/8/ 8 0 0 255 255 .tsv numeric_tsv
3+
slope z_original f32 Data/Map/XYZTile/Slope/Data/SlopeData20230514/2010/8/ 8 0 0 255 255 .tsv numeric_tsv
4+
gbank z_original u8 Data/Map/XYZTile/LandAndWater/Data/BlackAndWhiteBinary/1868/10/ 10 861 366 950 441 .txt numeric
5+
water z_original u8 Data/Map/XYZTile/RiversAndLakes/Data/RiversAndLakes/2023/12/ 12 3511 1465 3691 1712 .txt numeric

Library/PAX_SAPIENTICA/CuiOutput/Graphic.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <iomanip>
2121

2222
namespace paxs {
23+
// プログレスバーの表示
2324
void displayProgressBar(unsigned int current, unsigned int total) {
2425
const int bar_width = 50;
2526

Library/PAX_SAPIENTICA/FileRead/Read.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
1717
##########################################################################################*/
1818

19+
#include <PAX_SAPIENTICA/FileRead/Split.hpp>
20+
1921
#include <filesystem>
2022
#include <fstream>
2123
#include <vector>
@@ -35,6 +37,22 @@ namespace paxs {
3537
}
3638
return result;
3739
}
40+
std::vector<std::vector<std::string>> readCSV(const std::string& file_path) {
41+
std::vector<std::string> contents = readFile(file_path);
42+
std::vector<std::vector<std::string>> result;
43+
for(auto& content : contents) {
44+
result.emplace_back(split(content, ','));
45+
}
46+
return result;
47+
}
48+
std::vector<std::vector<std::string>> readTSV(const std::string& file_path) {
49+
std::vector<std::string> contents = readFile(file_path);
50+
std::vector<std::vector<std::string>> result;
51+
for(auto& content : contents) {
52+
result.emplace_back(split(content, '\t'));
53+
}
54+
return result;
55+
}
3856
std::vector<std::string> getFileNames(const std::string& directory_path) {
3957
std::filesystem::path dir_path(directory_path);
4058
std::filesystem::directory_iterator dir_iter(dir_path);
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*##########################################################################################
2+
3+
PAX SAPIENTICA Library 💀🌿🌏
4+
5+
[Planning] 2023 As Project
6+
[Production] 2023 As Project
7+
[Contact Us] wanotaitei@gmail.com https://github.com/AsPJT/PAX_SAPIENTICA
8+
[License] Distributed under the CC0 1.0. https://creativecommons.org/publicdomain/zero/1.0/
9+
10+
##########################################################################################*/
11+
12+
#ifndef PAX_SAPIENTICA_SIMULATION_DATA_HPP
13+
#define PAX_SAPIENTICA_SIMULATION_DATA_HPP
14+
15+
/*##########################################################################################
16+
17+
##########################################################################################*/
18+
19+
#include <PAX_SAPIENTICA/CuiOutput/Graphic.hpp>
20+
#include <PAX_SAPIENTICA/FileRead/Read.hpp>
21+
#include <PAX_SAPIENTICA/Type/Vector2.hpp>
22+
23+
#include <iostream>
24+
#include <map>
25+
26+
namespace paxs {
27+
template <typename T>
28+
class Data {
29+
public:
30+
using Vector2 = paxs::Vector2<int>;
31+
Data(const std::string& file_path, const std::string name, const Vector2& start_position, const Vector2& end_position, const int default_z, const int pj_z) : name(name), start_position(start_position), end_position(end_position), default_z(default_z), pj_z(pj_z) {
32+
load(file_path);
33+
}
34+
Data(const Data& other)
35+
: start_position(other.start_position),
36+
end_position(other.end_position),
37+
name(other.name),
38+
data(other.data),
39+
default_z(other.default_z),
40+
pj_z(other.pj_z) {}
41+
42+
// Copy assignment operator
43+
Data& operator=(const Data& other) {
44+
if (this != &other) {
45+
start_position = other.start_position;
46+
end_position = other.end_position;
47+
name = other.name;
48+
data = other.data;
49+
default_z = other.default_z;
50+
pj_z = other.pj_z;
51+
}
52+
return *this;
53+
}
54+
private:
55+
const int pixel_size = 256;
56+
57+
Vector2 start_position;
58+
Vector2 end_position;
59+
std::string name;
60+
std::map<Vector2, T> data;
61+
int default_z;
62+
int pj_z;
63+
64+
// ファイルのロード
65+
void load(const std::string& file_path) {
66+
std::cout << "Loading " << name << " data..." << std::endl;
67+
const std::vector<std::string> file_names = getFileNames("../../../"+file_path);
68+
std::cout << file_names.size() << " files are found." << std::endl;
69+
70+
if(file_names.size() == 0) {
71+
std::cout << "No files are found." << std::endl;
72+
std::exit(1);
73+
}
74+
75+
if(file_names[0].find(".tsv") != std::string::npos) {
76+
loadNumericTSV(file_names);
77+
} else if(file_names[0].find(".txt") != std::string::npos) {
78+
loadNumericText(file_names);
79+
} else {
80+
std::cout << "File format is not supported." << std::endl;
81+
std::exit(1);
82+
}
83+
}
84+
85+
// 数値TSVファイルのロード
86+
void loadNumericTSV(const std::vector<std::string>& file_names) {
87+
unsigned int file_count = 0;
88+
89+
for(const auto& file_name : file_names) {
90+
displayProgressBar(file_count, int(file_names.size()));
91+
92+
Vector2 xyz_position = getXAndYFromFileName(file_name);
93+
if(xyz_position < start_position || end_position < xyz_position) {
94+
++file_count;
95+
continue;
96+
}
97+
Vector2 default_position = (xyz_position - start_position) * pixel_size;
98+
std::vector<std::string> file = readFile(file_name);
99+
for(std::size_t y = 0;y < file.size();++y) {
100+
// タブ区切り
101+
std::vector<std::string> values = split(file[y], '\t');
102+
for(std::size_t x = 0;x < values.size();++x) {
103+
Vector2 position = default_position + Vector2((int)x, (int)y);
104+
// T型に変換
105+
data[position] = static_cast<T>(std::stod(values[x]));
106+
}
107+
}
108+
++file_count;
109+
}
110+
displayProgressBar(file_count, int(file_names.size()));
111+
std::cout << std::endl << "Loading " << name << " is completed." << std::endl;
112+
}
113+
// 数値ファイルのロード
114+
void loadNumericText(const std::vector<std::string>& file_names) {
115+
unsigned int file_count = 0;
116+
117+
for(const auto& file_name : file_names) {
118+
displayProgressBar(file_count, int(file_names.size()));
119+
120+
Vector2 xyz_position = getXAndYFromFileName(file_name);
121+
if(xyz_position < start_position || end_position < xyz_position) {
122+
++file_count;
123+
continue;
124+
}
125+
Vector2 default_position = (xyz_position - start_position) * pixel_size;
126+
std::vector<std::string> file = readFile(file_name);
127+
for(std::size_t y = 0;y < file.size();++y) {
128+
for(std::size_t x = 0;x < file[y].size();++x) {
129+
Vector2 position = default_position + Vector2((int)x, (int)y);
130+
// T型に変換
131+
data[position] = static_cast<T>(file[y][x]);
132+
}
133+
}
134+
++file_count;
135+
}
136+
displayProgressBar(file_count, int(file_names.size()));
137+
std::cout << std::endl << "Loading " << name << " is completed." << std::endl;
138+
}
139+
140+
// ファイルの名前からX座標とY座標を取得
141+
Vector2 getXAndYFromFileName(const std::string& file_name) {
142+
std::regex pattern(R"((\w+)_(\d+)_(\d+)_(\d+)\.(\w+))");
143+
std::smatch matches;
144+
145+
if(std::regex_search(file_name, matches, pattern)) {
146+
return Vector2(std::stoi(matches[3]), std::stoi(matches[4]));
147+
}
148+
else {
149+
std::cerr << "Error: The format of " << file_name << " is incorrect." << std::endl;
150+
std::exit(1);
151+
}
152+
}
153+
};
154+
}
155+
156+
#endif // !PAX_SAPIENTICA_SIMULATION_DATA_HPP

Library/PAX_SAPIENTICA/Simulation/Environment.hpp

Lines changed: 46 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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

Comments
 (0)