levelz-cpp 0.2.0
Loading...
Searching...
No Matches
matrix.hpp
1#pragma once
2
3#include <vector>
4#include <array>
5#include <regex>
6
7#include "coordinate.hpp"
8
9namespace LevelZ {
10
19 virtual std::string to_string() const = 0;
20 };
21
26 public:
30 int minX;
31
35 int maxX;
36
40 int minY;
41
45 int maxY;
46
51
59
69
74 std::vector<LevelZ::Coordinate2D> getCoordinates() const {
75 std::vector<LevelZ::Coordinate2D> coordinates;
76 for (int x = minX; x <= maxX; x++) {
77 for (int y = minY; y <= maxY; y++) {
78 coordinates.push_back(LevelZ::Coordinate2D(x, y));
79 }
80 }
81 return coordinates;
82 }
83
84 std::vector<LevelZ::Coordinate2D>::const_iterator begin() const {
85 return getCoordinates().begin();
86 }
87
88 std::vector<LevelZ::Coordinate2D>::const_iterator end() const {
89 return getCoordinates().end();
90 }
91
97 bool operator==(const CoordinateMatrix2D& other) const {
98 return minX == other.minX && maxX == other.maxX && minY == other.minY && maxY == other.maxY && start == other.start;
99 }
100
106 bool operator!=(const CoordinateMatrix2D& other) const {
107 return minX != other.minX || maxX != other.maxX || minY != other.minY || maxY != other.maxY || start != other.start;
108 }
109
114 std::string to_string() const {
115 return "(" + std::to_string(minX) + ", " + std::to_string(maxX) + ", " + std::to_string(minY) + ", " + std::to_string(maxY) + ")^" + start.to_string();
116 }
117
123 static LevelZ::CoordinateMatrix2D from_string(const std::string& str) {
124 const std::regex matrix("[\\[\\]()]");
125 std::string s0 = std::regex_replace(str, matrix, "");
126 s0 = s0.replace(s0.find('^'), 1, ", ");
127
128 unsigned int i = 0;
129 size_t cpos = 0;
130 std::string s1;
131
132 int x1, x2, y1, y2;
133 double cx, cy;
134
135 while ((cpos = s0.find(',')) != std::string::npos) {
136 s1 = s0.substr(0, cpos);
137 switch (i) {
138 case 0: x1 = std::stoi(s1); break;
139 case 1: x2 = std::stoi(s1); break;
140 case 2: y1 = std::stoi(s1); break;
141 case 3: y2 = std::stoi(s1); break;
142 case 4: cx = std::stod(s1); break;
143 }
144
145 s0.erase(0, cpos + 1);
146 i++;
147 }
148 cy = std::stod(s0);
149
150 return CoordinateMatrix2D(x1, x2, y1, y2, LevelZ::Coordinate2D(cx, cy));
151 }
152 };
153
158 public:
162 int minX;
163
167 int maxX;
168
172 int minY;
173
177 int maxY;
178
182 int minZ;
183
187 int maxZ;
188
193
201 CoordinateMatrix3D(int x, int y, int z, LevelZ::Coordinate3D start) : minX(0), maxX(x), minY(0), maxY(y), minZ(0), maxZ(z), start(start) {}
202
214
219 std::vector<LevelZ::Coordinate3D> getCoordinates() const {
220 std::vector<LevelZ::Coordinate3D> coordinates;
221 for (int x = minX; x <= maxX; x++) {
222 for (int y = minY; y <= maxY; y++) {
223 for (int z = minZ; z <= maxZ; z++) {
224 coordinates.push_back(LevelZ::Coordinate3D(x, y, z));
225 }
226 }
227 }
228 return coordinates;
229 }
230
231 std::vector<LevelZ::Coordinate3D>::const_iterator begin() const {
232 return getCoordinates().begin();
233 }
234
235 std::vector<LevelZ::Coordinate3D>::const_iterator end() const {
236 return getCoordinates().end();
237 }
238
244 bool operator==(const CoordinateMatrix3D& other) const {
245 return minX == other.minX && maxX == other.maxX && minY == other.minY && maxY == other.maxY && minZ == other.minZ && maxZ == other.maxZ && start == other.start;
246 }
247
253 bool operator!=(const CoordinateMatrix3D& other) const {
254 return minX != other.minX || maxX != other.maxX || minY != other.minY || maxY != other.maxY || minZ != other.minZ || maxZ != other.maxZ || start == other.start;
255 }
256
261 std::string to_string() const {
262 return "(" + std::to_string(minX) + ", " + std::to_string(maxX) + ", " + std::to_string(minY) + ", " + std::to_string(maxY) + ", " + std::to_string(minZ) + ", " + std::to_string(maxZ) + ")^" + start.to_string();
263 }
264
270 static LevelZ::CoordinateMatrix3D from_string(const std::string& str) {
271 const std::regex matrix("[\\[\\]()]");
272 std::string s0 = std::regex_replace(str, matrix, "");
273 s0 = s0.replace(s0.find('^'), 1, ", ");
274
275 unsigned int i = 0;
276 size_t cpos = 0;
277 std::string s1;
278
279 int x1, x2, y1, y2, z1, z2;
280 double cx, cy, cz;
281
282 while ((cpos = s0.find(',')) != std::string::npos) {
283 s1 = s0.substr(0, cpos);
284 switch (i) {
285 case 0: x1 = std::stoi(s1); break;
286 case 1: x2 = std::stoi(s1); break;
287 case 2: y1 = std::stoi(s1); break;
288 case 3: y2 = std::stoi(s1); break;
289 case 4: z1 = std::stoi(s1); break;
290 case 5: z2 = std::stoi(s1); break;
291 case 6: cx = std::stod(s1); break;
292 case 7: cy = std::stod(s1); break;
293 }
294
295 s0.erase(0, cpos + 1);
296 i++;
297 }
298 cz = std::stod(s0);
299
300 return CoordinateMatrix3D(x1, x2, y1, y2, z1, z2, LevelZ::Coordinate3D(cx, cy, cz));
301 }
302 };
303}
Definition coordinate.hpp:28
std::string to_string() const
Definition coordinate.hpp:149
Definition coordinate.hpp:177
std::string to_string() const
Definition coordinate.hpp:305
Definition matrix.hpp:25
std::vector< LevelZ::Coordinate2D > getCoordinates() const
Definition matrix.hpp:74
CoordinateMatrix2D(int x, int y, LevelZ::Coordinate2D start)
Definition matrix.hpp:58
int minX
Definition matrix.hpp:30
static LevelZ::CoordinateMatrix2D from_string(const std::string &str)
Definition matrix.hpp:123
LevelZ::Coordinate2D start
Definition matrix.hpp:50
int maxX
Definition matrix.hpp:35
int minY
Definition matrix.hpp:40
bool operator!=(const CoordinateMatrix2D &other) const
Definition matrix.hpp:106
CoordinateMatrix2D(int minX, int maxX, int minY, int maxY, LevelZ::Coordinate2D start)
Definition matrix.hpp:68
std::string to_string() const
Definition matrix.hpp:114
int maxY
Definition matrix.hpp:45
bool operator==(const CoordinateMatrix2D &other) const
Definition matrix.hpp:97
Definition matrix.hpp:157
CoordinateMatrix3D(int minX, int maxX, int minY, int maxY, int minZ, int maxZ, LevelZ::Coordinate3D start)
Definition matrix.hpp:213
int minX
Definition matrix.hpp:162
std::vector< LevelZ::Coordinate3D > getCoordinates() const
Definition matrix.hpp:219
bool operator==(const CoordinateMatrix3D &other) const
Definition matrix.hpp:244
LevelZ::Coordinate3D start
Definition matrix.hpp:192
int minY
Definition matrix.hpp:172
int minZ
Definition matrix.hpp:182
static LevelZ::CoordinateMatrix3D from_string(const std::string &str)
Definition matrix.hpp:270
std::string to_string() const
Definition matrix.hpp:261
CoordinateMatrix3D(int x, int y, int z, LevelZ::Coordinate3D start)
Definition matrix.hpp:201
int maxZ
Definition matrix.hpp:187
int maxX
Definition matrix.hpp:167
int maxY
Definition matrix.hpp:177
bool operator!=(const CoordinateMatrix3D &other) const
Definition matrix.hpp:253
Definition matrix.hpp:14
virtual std::string to_string() const =0