2022-04-22 01:15:40 +02:00
|
|
|
#ifndef TILING_WAVE_FORM_COLLAPSE_H
|
|
|
|
#define TILING_WAVE_FORM_COLLAPSE_H
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
#include "array_2d.h"
|
2022-04-21 16:31:03 +02:00
|
|
|
#include "core/vector.h"
|
2022-04-21 17:05:26 +02:00
|
|
|
#include <unordered_map>
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
#include "wave_form_collapse.h"
|
2022-04-20 03:05:34 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Tile {
|
2022-04-22 01:15:40 +02:00
|
|
|
enum Symmetry {
|
|
|
|
SYMMETRY_X,
|
|
|
|
SYMMETRY_T,
|
|
|
|
SYMMETRY_I,
|
|
|
|
SYMMETRY_L,
|
|
|
|
SYMMETRY_BACKSLASH,
|
|
|
|
SYMMETRY_P
|
|
|
|
};
|
|
|
|
|
2022-04-21 16:31:03 +02:00
|
|
|
Vector<Array2D<T>> data; // The different orientations of the tile
|
2022-04-20 03:05:34 +02:00
|
|
|
Symmetry symmetry; // The symmetry of the tile
|
|
|
|
double weight; // Its weight on the distribution of presence of tiles
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
// Generate the map associating an orientation id to the orientation
|
|
|
|
// id obtained when rotating 90° anticlockwise the tile.
|
2022-04-21 16:43:04 +02:00
|
|
|
static Vector<uint32_t> generate_rotation_map(const Symmetry &symmetry) {
|
2022-04-20 03:05:34 +02:00
|
|
|
switch (symmetry) {
|
|
|
|
case Symmetry::X:
|
|
|
|
return { 0 };
|
|
|
|
case Symmetry::I:
|
|
|
|
case Symmetry::backslash:
|
|
|
|
return { 1, 0 };
|
|
|
|
case Symmetry::T:
|
|
|
|
case Symmetry::L:
|
|
|
|
return { 1, 2, 3, 0 };
|
|
|
|
case Symmetry::P:
|
|
|
|
default:
|
|
|
|
return { 1, 2, 3, 0, 5, 6, 7, 4 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
// Generate the map associating an orientation id to the orientation
|
|
|
|
// id obtained when reflecting the tile along the x axis.
|
2022-04-21 16:43:04 +02:00
|
|
|
static Vector<uint32_t> generate_reflection_map(const Symmetry &symmetry) {
|
2022-04-20 03:05:34 +02:00
|
|
|
switch (symmetry) {
|
|
|
|
case Symmetry::X:
|
|
|
|
return { 0 };
|
|
|
|
case Symmetry::I:
|
|
|
|
return { 0, 1 };
|
|
|
|
case Symmetry::backslash:
|
|
|
|
return { 1, 0 };
|
|
|
|
case Symmetry::T:
|
|
|
|
return { 0, 3, 2, 1 };
|
|
|
|
case Symmetry::L:
|
|
|
|
return { 1, 0, 3, 2 };
|
|
|
|
case Symmetry::P:
|
|
|
|
default:
|
|
|
|
return { 4, 7, 6, 5, 0, 3, 2, 1 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
// Generate the map associating an orientation id and an action to the
|
|
|
|
// resulting orientation id.
|
|
|
|
// Actions 0, 1, 2, and 3 are 0°, 90°, 180°, and 270° anticlockwise rotations.
|
|
|
|
// Actions 4, 5, 6, and 7 are actions 0, 1, 2, and 3 preceded by a reflection
|
|
|
|
// on the x axis.
|
2022-04-21 16:43:04 +02:00
|
|
|
static Vector<Vector<uint32_t>> generate_action_map(const Symmetry &symmetry) {
|
|
|
|
Vector<uint32_t> rotation_map = generate_rotation_map(symmetry);
|
|
|
|
Vector<uint32_t> reflection_map = generate_reflection_map(symmetry);
|
2022-04-21 17:33:44 +02:00
|
|
|
int size = rotation_map.size();
|
|
|
|
Vector<Vector<uint32_t>> action_map(8, Vector<uint32_t>(size));
|
|
|
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2022-04-20 03:05:34 +02:00
|
|
|
action_map[0][i] = i;
|
|
|
|
}
|
|
|
|
|
2022-04-21 17:33:44 +02:00
|
|
|
for (int a = 1; a < 4; ++a) {
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2022-04-20 03:05:34 +02:00
|
|
|
action_map[a][i] = rotation_map[action_map[a - 1][i]];
|
|
|
|
}
|
|
|
|
}
|
2022-04-22 01:15:40 +02:00
|
|
|
|
2022-04-21 17:33:44 +02:00
|
|
|
for (int i = 0; i < size; ++i) {
|
2022-04-20 03:05:34 +02:00
|
|
|
action_map[4][i] = reflection_map[action_map[0][i]];
|
|
|
|
}
|
2022-04-22 01:15:40 +02:00
|
|
|
|
2022-04-21 17:33:44 +02:00
|
|
|
for (int a = 5; a < 8; ++a) {
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2022-04-20 03:05:34 +02:00
|
|
|
action_map[a][i] = rotation_map[action_map[a - 1][i]];
|
|
|
|
}
|
|
|
|
}
|
2022-04-22 01:15:40 +02:00
|
|
|
|
2022-04-20 03:05:34 +02:00
|
|
|
return action_map;
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
// Generate all distincts rotations of a 2D array given its symmetries;
|
2022-04-21 17:33:44 +02:00
|
|
|
static Vector<Array2D<T>> generate_oriented(Array2D<T> data, Symmetry symmetry) {
|
2022-04-21 16:31:03 +02:00
|
|
|
Vector<Array2D<T>> oriented;
|
2022-04-20 03:05:34 +02:00
|
|
|
oriented.push_back(data);
|
|
|
|
|
|
|
|
switch (symmetry) {
|
|
|
|
case Symmetry::I:
|
|
|
|
case Symmetry::backslash:
|
|
|
|
oriented.push_back(data.rotated());
|
|
|
|
break;
|
|
|
|
case Symmetry::T:
|
|
|
|
case Symmetry::L:
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
break;
|
|
|
|
case Symmetry::P:
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
oriented.push_back(data = data.rotated().reflected());
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
oriented.push_back(data = data.rotated());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return oriented;
|
|
|
|
}
|
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
// Return the number of possible distinct orientations for a tile. An orientation is a combination of rotations and reflections.
|
|
|
|
static constexpr uint32_t nb_of_possible_orientations(const Symmetry &symmetry) {
|
|
|
|
switch (symmetry) {
|
|
|
|
case SYMMETRY_X:
|
|
|
|
return 1;
|
|
|
|
case SYMMETRY_I:
|
|
|
|
case SYMMETRY_BACKSLASH:
|
|
|
|
return 2;
|
|
|
|
case SYMMETRY_T:
|
|
|
|
case SYMMETRY_L:
|
|
|
|
return 4;
|
|
|
|
default:
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
// Create a tile with its differents orientations, its symmetries and its weight on the distribution of tiles.
|
|
|
|
Tile(const Vector<Array2D<T>> &p_data, Symmetry p_symmetry, double p_weight) {
|
|
|
|
data = p_data;
|
|
|
|
symmetry = p_symmetry;
|
|
|
|
weight = p_weight;
|
|
|
|
}
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
// Create a tile with its base orientation, its symmetries and its weight on the distribution of tiles.
|
|
|
|
// The other orientations are generated with its first one.
|
|
|
|
Tile(const Array2D<T> &data, Symmetry p_symmetry, double p_weight) {
|
|
|
|
data = generate_oriented(p_data, p_symmetry);
|
|
|
|
symmetry = p_symmetry;
|
|
|
|
weight = p_weight;
|
|
|
|
}
|
2022-04-20 03:05:34 +02:00
|
|
|
};
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
// Class generating a new image with the tiling WFC algorithm.
|
2022-04-20 03:05:34 +02:00
|
|
|
template <typename T>
|
|
|
|
class TilingWFC {
|
|
|
|
public:
|
2022-04-21 16:43:04 +02:00
|
|
|
uint32_t height;
|
|
|
|
uint32_t width;
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
struct NeighbourData {
|
|
|
|
uint32_t data[4];
|
|
|
|
|
|
|
|
NeighbourData() {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
direction[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TilingWFC(
|
|
|
|
const Vector<Tile<T>> &tiles,
|
|
|
|
const Vector<NeighbourData> &neighbors,
|
|
|
|
const uint32_t height, const uint32_t width,
|
|
|
|
const bool periodic_output, int seed) :
|
|
|
|
tiles(tiles),
|
|
|
|
id_to_oriented_tile(generate_oriented_tile_ids(tiles).first),
|
|
|
|
oriented_tile_ids(generate_oriented_tile_ids(tiles).second),
|
|
|
|
options(options),
|
|
|
|
wfc(options.periodic_output, seed, get_tiles_weights(tiles),
|
|
|
|
generate_propagator(neighbors, tiles, id_to_oriented_tile,
|
|
|
|
oriented_tile_ids),
|
|
|
|
height, width),
|
|
|
|
height(height),
|
|
|
|
width(width) {}
|
|
|
|
|
|
|
|
// Returns false if the given tile and orientation does not exist, or if the coordinates are not in the wave
|
|
|
|
bool set_tile(uint32_t tile_id, uint32_t orientation, uint32_t i, uint32_t j) {
|
|
|
|
if (tile_id >= oriented_tile_ids.size() || orientation >= oriented_tile_ids[tile_id].size() || i >= height || j >= width) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t oriented_tile_id = oriented_tile_ids[tile_id][orientation];
|
|
|
|
set_tile(oriented_tile_id, i, j);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array2D<T> run() {
|
|
|
|
Array2D<uint32_t> a = wfc.run();
|
|
|
|
|
|
|
|
if (a.width == 0 && a.height == 0) {
|
|
|
|
return Array2D<T>(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id_to_tiling(a);
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:05:34 +02:00
|
|
|
private:
|
2022-04-20 03:24:50 +02:00
|
|
|
// Generate mapping from id to oriented tiles and vice versa.
|
2022-04-21 17:45:30 +02:00
|
|
|
static std::pair<Vector<std::pair<uint32_t, uint32_t>>, Vector<Vector<uint32_t>>> generate_oriented_tile_ids(const Vector<Tile<T>> &tiles) {
|
2022-04-21 16:43:04 +02:00
|
|
|
Vector<std::pair<uint32_t, uint32_t>> id_to_oriented_tile;
|
|
|
|
Vector<Vector<uint32_t>> oriented_tile_ids;
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-21 16:43:04 +02:00
|
|
|
uint32_t id = 0;
|
2022-04-21 17:33:44 +02:00
|
|
|
for (int i = 0; i < tiles.size(); i++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
oriented_tile_ids.push_back({});
|
2022-04-21 17:33:44 +02:00
|
|
|
for (int j = 0; j < tiles[i].data.size(); j++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
id_to_oriented_tile.push_back({ i, j });
|
|
|
|
oriented_tile_ids[i].push_back(id);
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { id_to_oriented_tile, oriented_tile_ids };
|
|
|
|
}
|
|
|
|
|
2022-04-21 17:05:26 +02:00
|
|
|
struct DensePropagatorHelper {
|
|
|
|
Vector<bool> directions[4];
|
|
|
|
|
|
|
|
void resize(const int size) {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
directions[i].resize(size);
|
|
|
|
directions[i].fill(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
// Generate the propagator which will be used in the wfc algorithm.
|
2022-04-21 23:31:25 +02:00
|
|
|
static Vector<PropagatorStateEntry> generate_propagator(
|
2022-04-21 17:17:50 +02:00
|
|
|
const Vector<NeighbourData> &neighbors,
|
2022-04-21 16:31:03 +02:00
|
|
|
Vector<Tile<T>> tiles,
|
2022-04-21 16:43:04 +02:00
|
|
|
Vector<std::pair<uint32_t, uint32_t>> id_to_oriented_tile,
|
|
|
|
Vector<Vector<uint32_t>> oriented_tile_ids) {
|
2022-04-20 03:05:34 +02:00
|
|
|
size_t nb_oriented_tiles = id_to_oriented_tile.size();
|
2022-04-21 17:45:30 +02:00
|
|
|
|
|
|
|
Vector<DensePropagatorHelper> dense_propagator;
|
2022-04-21 17:05:26 +02:00
|
|
|
dense_propagator.resize(nb_oriented_tiles);
|
|
|
|
for (int i = 0; i < nb_oriented_tiles; ++i) {
|
|
|
|
dense_propagator.write[i].resize(nb_oriented_tiles);
|
|
|
|
}
|
2022-04-20 03:05:34 +02:00
|
|
|
|
|
|
|
for (auto neighbor : neighbors) {
|
2022-04-21 16:43:04 +02:00
|
|
|
uint32_t tile1 = std::get<0>(neighbor);
|
|
|
|
uint32_t orientation1 = std::get<1>(neighbor);
|
|
|
|
uint32_t tile2 = std::get<2>(neighbor);
|
|
|
|
uint32_t orientation2 = std::get<3>(neighbor);
|
2022-04-21 17:45:30 +02:00
|
|
|
Vector<Vector<uint32_t>> action_map1 = Tile<T>::generate_action_map(tiles[tile1].symmetry);
|
|
|
|
Vector<Vector<uint32_t>> action_map2 = Tile<T>::generate_action_map(tiles[tile2].symmetry);
|
2022-04-20 03:05:34 +02:00
|
|
|
|
2022-04-21 16:43:04 +02:00
|
|
|
auto add = [&](uint32_t action, uint32_t direction) {
|
|
|
|
uint32_t temp_orientation1 = action_map1[action][orientation1];
|
|
|
|
uint32_t temp_orientation2 = action_map2[action][orientation2];
|
|
|
|
uint32_t oriented_tile_id1 = oriented_tile_ids[tile1][temp_orientation1];
|
|
|
|
uint32_t oriented_tile_id2 = oriented_tile_ids[tile2][temp_orientation2];
|
2022-04-21 16:31:03 +02:00
|
|
|
dense_propagator[oriented_tile_id1][direction][oriented_tile_id2] = true;
|
2022-04-20 03:05:34 +02:00
|
|
|
direction = get_opposite_direction(direction);
|
2022-04-21 16:31:03 +02:00
|
|
|
dense_propagator[oriented_tile_id2][direction][oriented_tile_id1] = true;
|
2022-04-20 03:05:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
add(0, 2);
|
|
|
|
add(1, 0);
|
|
|
|
add(2, 1);
|
|
|
|
add(3, 3);
|
|
|
|
add(4, 1);
|
|
|
|
add(5, 3);
|
|
|
|
add(6, 2);
|
|
|
|
add(7, 0);
|
|
|
|
}
|
|
|
|
|
2022-04-21 23:31:25 +02:00
|
|
|
Vector<PropagatorStateEntry> propagator(nb_oriented_tiles);
|
2022-04-21 16:31:03 +02:00
|
|
|
|
2022-04-20 03:05:34 +02:00
|
|
|
for (size_t i = 0; i < nb_oriented_tiles; ++i) {
|
|
|
|
for (size_t j = 0; j < nb_oriented_tiles; ++j) {
|
|
|
|
for (size_t d = 0; d < 4; ++d) {
|
|
|
|
if (dense_propagator[i][d][j]) {
|
|
|
|
propagator[i][d].push_back(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return propagator;
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
// Get probability of presence of tiles.
|
2022-04-21 16:31:03 +02:00
|
|
|
static Vector<double> get_tiles_weights(const Vector<Tile<T>> &tiles) {
|
|
|
|
Vector<double> frequencies;
|
|
|
|
|
2022-04-21 17:33:44 +02:00
|
|
|
for (int i = 0; i < tiles.size(); ++i) {
|
|
|
|
for (int j = 0; j < tiles[i].data.size(); ++j) {
|
2022-04-20 03:05:34 +02:00
|
|
|
frequencies.push_back(tiles[i].weight / tiles[i].data.size());
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 16:31:03 +02:00
|
|
|
|
2022-04-20 03:05:34 +02:00
|
|
|
return frequencies;
|
|
|
|
}
|
|
|
|
|
2022-04-20 03:24:50 +02:00
|
|
|
// Translate the generic WFC result into the image result
|
2022-04-21 16:43:04 +02:00
|
|
|
Array2D<T> id_to_tiling(Array2D<uint32_t> ids) {
|
|
|
|
uint32_t size = tiles[0].data[0].height;
|
2022-04-20 03:05:34 +02:00
|
|
|
Array2D<T> tiling(size * ids.height, size * ids.width);
|
2022-04-21 16:31:03 +02:00
|
|
|
|
2022-04-21 16:43:04 +02:00
|
|
|
for (uint32_t i = 0; i < ids.height; i++) {
|
|
|
|
for (uint32_t j = 0; j < ids.width; j++) {
|
|
|
|
std::pair<uint32_t, uint32_t> oriented_tile = id_to_oriented_tile[ids.get(i, j)];
|
2022-04-21 16:31:03 +02:00
|
|
|
|
2022-04-21 16:43:04 +02:00
|
|
|
for (uint32_t y = 0; y < size; y++) {
|
|
|
|
for (uint32_t x = 0; x < size; x++) {
|
2022-04-21 16:31:03 +02:00
|
|
|
tiling.get(i * size + y, j * size + x) = tiles[oriented_tile.first].data[oriented_tile.second].get(y, x);
|
2022-04-20 03:05:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 16:31:03 +02:00
|
|
|
|
2022-04-20 03:05:34 +02:00
|
|
|
return tiling;
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:43:04 +02:00
|
|
|
void set_tile(uint32_t tile_id, uint32_t i, uint32_t j) {
|
2022-04-21 17:33:44 +02:00
|
|
|
for (int p = 0; p < id_to_oriented_tile.size(); p++) {
|
2022-04-20 03:05:34 +02:00
|
|
|
if (tile_id != p) {
|
|
|
|
wfc.remove_wave_pattern(i, j, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
Vector<Tile<T>> tiles;
|
|
|
|
Vector<std::pair<uint32_t, uint32_t>> id_to_oriented_tile;
|
|
|
|
Vector<Vector<uint32_t>> oriented_tile_ids;
|
2022-04-21 14:28:04 +02:00
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
bool periodic_output;
|
2022-04-21 14:28:04 +02:00
|
|
|
|
2022-04-22 01:15:40 +02:00
|
|
|
WFC wfc;
|
2022-04-20 03:05:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FAST_WFC_TILING_WFC_HPP_
|