2022-04-20 03:05:34 +02:00
|
|
|
#ifndef FAST_WFC_UTILS_ARRAY2D_HPP_
|
|
|
|
#define FAST_WFC_UTILS_ARRAY2D_HPP_
|
|
|
|
|
|
|
|
#include "assert.h"
|
2022-04-21 16:31:03 +02:00
|
|
|
#include "core/vector.h"
|
2022-04-20 03:05:34 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Array2D {
|
|
|
|
public:
|
2022-04-21 16:36:33 +02:00
|
|
|
uint32_t height;
|
|
|
|
uint32_t width;
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-21 16:31:03 +02:00
|
|
|
Vector<T> data;
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-21 16:36:33 +02:00
|
|
|
Array2D(uint32_t p_height, uint32_t p_width) {
|
2022-04-20 03:24:50 +02:00
|
|
|
height = p_height;
|
|
|
|
width = p_width;
|
|
|
|
data.resize(width * height);
|
|
|
|
}
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-21 16:36:33 +02:00
|
|
|
Array2D(uint32_t p_height, uint32_t p_width, T p_value) {
|
2022-04-20 03:24:50 +02:00
|
|
|
height = p_height;
|
|
|
|
width = p_width;
|
2022-04-21 16:31:03 +02:00
|
|
|
data.resize(width * height);
|
|
|
|
data.fill(p_value);
|
2022-04-20 03:24:50 +02:00
|
|
|
}
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-21 16:36:33 +02:00
|
|
|
const T &get(uint32_t i, uint32_t j) const {
|
2022-04-20 03:05:34 +02:00
|
|
|
assert(i < height && j < width);
|
|
|
|
return data[j + i * width];
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:36:33 +02:00
|
|
|
T &get(uint32_t i, uint32_t j) {
|
2022-04-20 03:05:34 +02:00
|
|
|
assert(i < height && j < width);
|
2022-04-21 16:31:03 +02:00
|
|
|
return data.write[j + i * width];
|
2022-04-20 03:05:34 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
Array2D<T> reflected() const {
|
2022-04-20 03:05:34 +02:00
|
|
|
Array2D<T> result = Array2D<T>(width, height);
|
2022-04-21 16:36:33 +02:00
|
|
|
for (uint32_t y = 0; y < height; y++) {
|
|
|
|
for (uint32_t x = 0; x < width; x++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
result.get(y, x) = get(y, width - 1 - x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
Array2D<T> rotated() const {
|
2022-04-20 03:05:34 +02:00
|
|
|
Array2D<T> result = Array2D<T>(width, height);
|
2022-04-21 16:36:33 +02:00
|
|
|
for (uint32_t y = 0; y < width; y++) {
|
|
|
|
for (uint32_t x = 0; x < height; x++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
result.get(y, x) = get(x, width - 1 - y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:36:33 +02:00
|
|
|
Array2D<T> get_sub_array(uint32_t y, uint32_t x, uint32_t sub_width, uint32_t sub_height) const {
|
2022-04-20 03:05:34 +02:00
|
|
|
Array2D<T> sub_array_2d = Array2D<T>(sub_width, sub_height);
|
2022-04-21 16:36:33 +02:00
|
|
|
for (uint32_t ki = 0; ki < sub_height; ki++) {
|
|
|
|
for (uint32_t kj = 0; kj < sub_width; kj++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
sub_array_2d.get(ki, kj) = get((y + ki) % height, (x + kj) % width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sub_array_2d;
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
bool operator==(const Array2D<T> &a) const {
|
2022-04-20 03:05:34 +02:00
|
|
|
if (height != a.height || width != a.width) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:36:33 +02:00
|
|
|
for (uint32_t i = 0; i < data.size(); i++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
if (a.data[i] != data[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
#endif
|