2022-04-20 03:05:34 +02:00
|
|
|
#ifndef FAST_WFC_UTILS_ARRAY2D_HPP_
|
|
|
|
#define FAST_WFC_UTILS_ARRAY2D_HPP_
|
|
|
|
|
2023-12-18 00:25:33 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* array_2d.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* PANDEMONIUM ENGINE */
|
|
|
|
/* https://github.com/Relintai/pandemonium_engine */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2022-present Péter Magyar. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-08-17 12:53:49 +02:00
|
|
|
#include "core/containers/pool_vector.h"
|
2023-12-18 00:25:33 +01:00
|
|
|
#include "core/containers/vector.h"
|
2022-08-17 13:17:12 +02:00
|
|
|
#include "core/variant/variant.h"
|
2022-04-20 03:05:34 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Array2D {
|
|
|
|
public:
|
2022-04-22 19:07:29 +02:00
|
|
|
int height;
|
|
|
|
int 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 23:31:25 +02:00
|
|
|
Array2D() {
|
|
|
|
height = 0;
|
|
|
|
width = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:07:29 +02:00
|
|
|
Array2D(int p_height, int 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-22 19:07:29 +02:00
|
|
|
Array2D(int p_height, int 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-24 01:39:47 +02:00
|
|
|
Array2D(const PoolIntArray &p_data, int p_height, int p_width) {
|
|
|
|
int wh = p_width * p_height;
|
|
|
|
if (wh != p_data.size()) {
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
ERR_FAIL_MSG("wh != p_data.size()");
|
|
|
|
}
|
|
|
|
|
|
|
|
height = p_height;
|
|
|
|
width = p_width;
|
|
|
|
data.resize(wh);
|
|
|
|
|
|
|
|
int *w = data.ptrw();
|
|
|
|
int s = data.size();
|
|
|
|
|
|
|
|
PoolIntArray::Read r = p_data.read();
|
|
|
|
|
|
|
|
for (int i = 0; i < s; ++i) {
|
|
|
|
w[i] = r[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:07:29 +02:00
|
|
|
void resize(int p_height, int p_width) {
|
2022-04-21 23:31:25 +02:00
|
|
|
height = p_height;
|
|
|
|
width = p_width;
|
|
|
|
data.resize(width * height);
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:07:29 +02:00
|
|
|
void resize_fill(int p_height, int p_width, T p_value) {
|
2022-04-21 23:31:25 +02:00
|
|
|
height = p_height;
|
|
|
|
width = p_width;
|
|
|
|
data.resize(width * height);
|
|
|
|
data.fill(p_value);
|
|
|
|
}
|
|
|
|
|
2022-04-24 01:39:47 +02:00
|
|
|
void set_data(const PoolIntArray &p_data, int p_height, int p_width) {
|
|
|
|
int wh = p_width * p_height;
|
|
|
|
ERR_FAIL_COND(wh != p_data.size());
|
|
|
|
|
|
|
|
resize(p_height, p_width);
|
|
|
|
|
|
|
|
height = p_height;
|
|
|
|
width = p_width;
|
|
|
|
data.resize(wh);
|
|
|
|
|
|
|
|
int *w = data.ptrw();
|
|
|
|
int s = data.size();
|
|
|
|
|
|
|
|
PoolIntArray::Read r = p_data.read();
|
|
|
|
|
|
|
|
for (int i = 0; i < s; ++i) {
|
|
|
|
w[i] = r[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:07:29 +02:00
|
|
|
const T &get(int i, int j) const {
|
2022-04-21 16:51:10 +02:00
|
|
|
CRASH_BAD_INDEX(i, height);
|
|
|
|
CRASH_BAD_INDEX(j, width);
|
|
|
|
|
2022-04-20 03:05:34 +02:00
|
|
|
return data[j + i * width];
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:07:29 +02:00
|
|
|
T &get(int i, int j) {
|
2022-04-21 16:51:10 +02:00
|
|
|
CRASH_BAD_INDEX(i, height);
|
|
|
|
CRASH_BAD_INDEX(j, width);
|
2022-04-21 23:31:25 +02:00
|
|
|
|
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-24 17:15:18 +02:00
|
|
|
Array2D<T> result = Array2D<T>(height, width);
|
2022-04-22 19:07:29 +02:00
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
for (int 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-22 19:07:29 +02:00
|
|
|
for (int y = 0; y < width; y++) {
|
|
|
|
for (int 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-22 19:07:29 +02:00
|
|
|
Array2D<T> get_sub_array(int y, int x, int sub_width, int sub_height) const {
|
2022-04-24 17:15:18 +02:00
|
|
|
Array2D<T> sub_array_2d = Array2D<T>(sub_height, sub_width);
|
2022-04-22 19:07:29 +02:00
|
|
|
for (int ki = 0; ki < sub_height; ki++) {
|
|
|
|
for (int 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 17:33:44 +02:00
|
|
|
for (int 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
|