From 2680c89948bcc8cff0bdbdd12c0d75554a15e657 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 21 Apr 2022 17:17:50 +0200 Subject: [PATCH] Got rid of std::touples in the wfc module. --- modules/wfc/propagator.cpp | 22 ++++++++++++++-------- modules/wfc/propagator.h | 21 ++++++++++++++++++--- modules/wfc/tiling_wfc.h | 16 ++++++++++++---- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/modules/wfc/propagator.cpp b/modules/wfc/propagator.cpp index 2a4df7715..db8615ffb 100644 --- a/modules/wfc/propagator.cpp +++ b/modules/wfc/propagator.cpp @@ -7,7 +7,6 @@ void Propagator::init_compatible() { for (uint32_t y = 0; y < wave_height; y++) { for (uint32_t x = 0; x < wave_width; x++) { for (uint32_t pattern = 0; pattern < patterns_size; pattern++) { - for (int direction = 0; direction < 4; direction++) { value.direction[direction] = static_cast(propagator_state[pattern].directions[get_opposite_direction(direction)].size()); } @@ -22,8 +21,13 @@ void Propagator::propagate(Wave &wave) { // We propagate every element while there is element to propagate. while (propagating.size() != 0) { // The cell and pattern that has been set to false. - uint32_t y1, x1, pattern; - std::tie(y1, x1, pattern) = propagating[propagating.size() - 1]; + + const PropagatingEntry &e = propagating[propagating.size() - 1]; + + uint32_t y1 = e.data[0]; + uint32_t x1 = e.data[1]; + uint32_t pattern = e.data[2]; + propagating.resize(propagating.size() - 1); // We propagate the information in all 4 directions. @@ -38,9 +42,11 @@ void Propagator::propagate(Wave &wave) { } else { x2 = x1 + dx; y2 = y1 + dy; + if (x2 < 0 || x2 >= (int)wave.width) { continue; } + if (y2 < 0 || y2 >= (int)wave.height) { continue; } @@ -54,19 +60,19 @@ void Propagator::propagate(Wave &wave) { // contradiction with pattern1 int size = patterns.size(); for (int i = 0; i < size; ++i) { - uint32_t pattern = patterns[i]; - + uint32_t pattern_entry = patterns[i]; + // We decrease the number of compatible patterns in the opposite // direction If the pattern was discarded from the wave, the element // is still negative, which is not a problem - CompatibilityEntry &value = compatible.get(y2, x2, pattern); + CompatibilityEntry &value = compatible.get(y2, x2, pattern_entry); value.direction[direction]--; // If the element was set to 0 with this operation, we need to remove // the pattern from the wave, and propagate the information if (value.direction[direction] == 0) { - add_to_propagator(y2, x2, pattern); - wave.set(i2, pattern, false); + add_to_propagator(y2, x2, pattern_entry); + wave.set(i2, pattern_entry, false); } } } diff --git a/modules/wfc/propagator.h b/modules/wfc/propagator.h index 00a0f2a09..2ceed6b8b 100644 --- a/modules/wfc/propagator.h +++ b/modules/wfc/propagator.h @@ -4,7 +4,6 @@ #include "array_3d.h" #include "core/vector.h" #include "direction.h" -#include class Wave; @@ -26,10 +25,26 @@ private: const bool periodic_output; + struct PropagatingEntry { + uint32_t data[3]; + + PropagatingEntry() { + for (int i = 0; i < 3; ++i) { + data[i] = 0; + } + } + + PropagatingEntry(uint32_t x, uint32_t y, uint32_t z) { + data[0] = x; + data[1] = y; + data[2] = z; + } + }; + // All the tuples (y, x, pattern) that should be propagated. // The tuple should be propagated when wave.get(y, x, pattern) is set to // false. - Vector> propagating; + Vector propagating; struct CompatibilityEntry { int direction[4]; @@ -67,7 +82,7 @@ public: CompatibilityEntry temp; compatible.get(y, x, pattern) = temp; - propagating.push_back(std::tuple(y, x, pattern)); + propagating.push_back(PropagatingEntry(y, x, pattern)); } void propagate(Wave &wave); diff --git a/modules/wfc/tiling_wfc.h b/modules/wfc/tiling_wfc.h index c39a31002..ea33f6bba 100644 --- a/modules/wfc/tiling_wfc.h +++ b/modules/wfc/tiling_wfc.h @@ -212,11 +212,10 @@ private: // Generate the propagator which will be used in the wfc algorithm. static Vector generate_propagator( - const Vector> &neighbors, + const Vector &neighbors, Vector> tiles, Vector> id_to_oriented_tile, Vector> oriented_tile_ids) { - size_t nb_oriented_tiles = id_to_oriented_tile.size(); Vector dense_propagator(nb_oriented_tiles, { Vector(nb_oriented_tiles, false), Vector(nb_oriented_tiles, false), Vector(nb_oriented_tiles, false), Vector(nb_oriented_tiles, false) }); dense_propagator.resize(nb_oriented_tiles); @@ -311,11 +310,20 @@ private: } public: + struct NeighbourData { + uint32_t data[4]; + + NeighbourData() { + for (int i = 0; i < 4; ++i) { + direction[i] = 0; + } + } + }; + // Construct the TilingWFC class to generate a tiled image. TilingWFC( const Vector> &tiles, - const Vector> - &neighbors, + const Vector &neighbors, const uint32_t height, const uint32_t width, const TilingWFCOptions &options, int seed) : tiles(tiles),