From 5be272e5cb78f1dbbdbe3e632f072f5a0d57daab Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 21 Apr 2022 14:28:04 +0200 Subject: [PATCH] Fix a few typos and remove std::optional from wcf. --- modules/wfc/array_2d.h | 2 +- modules/wfc/array_3d.h | 2 +- modules/wfc/overlapping_wfc.h | 16 +++++++++------- modules/wfc/tiling_wfc.h | 15 ++++++++------- modules/wfc/wave.cpp | 7 ++----- modules/wfc/wfc.cpp | 7 +++---- modules/wfc/wfc.h | 2 +- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/modules/wfc/array_2d.h b/modules/wfc/array_2d.h index 63638242c..79b14d4aa 100644 --- a/modules/wfc/array_2d.h +++ b/modules/wfc/array_2d.h @@ -21,7 +21,7 @@ public: Array2D(std::size_t p_height, std::size_t p_width, T p_value) { height = p_height; width = p_width; - data.resize(width * height, value); + data.resize(width * height, p_value); } const T &get(std::size_t i, std::size_t j) const { diff --git a/modules/wfc/array_3d.h b/modules/wfc/array_3d.h index 405c357a2..d91ead2ad 100644 --- a/modules/wfc/array_3d.h +++ b/modules/wfc/array_3d.h @@ -20,7 +20,7 @@ public: data.resize(width * height * depth); } - Array3D(std::size_t height, std::size_t width, std::size_t depth, T value) { + Array3D(std::size_t p_height, std::size_t p_width, std::size_t p_depth, T value) { height = p_height; width = p_width; depth = p_depth; diff --git a/modules/wfc/overlapping_wfc.h b/modules/wfc/overlapping_wfc.h index 3f59ee18e..414439e46 100644 --- a/modules/wfc/overlapping_wfc.h +++ b/modules/wfc/overlapping_wfc.h @@ -228,14 +228,14 @@ private: return output; } - std::optional get_pattern_id(const Array2D &pattern) { + unsigned get_pattern_id(const Array2D &pattern) { unsigned *pattern_id = std::find(patterns.begin(), patterns.end(), pattern); if (pattern_id != patterns.end()) { return *pattern_id; } - return std::nullopt; + return -1; } // Set the pattern at a specific position, given its pattern id @@ -267,12 +267,14 @@ public: } // Run the WFC algorithm, and return the result if the algorithm succeeded. - std::optional> run() { - std::optional> result = wfc.run(); - if (result.has_value()) { - return to_image(*result); + Array2D run() { + Array2D result = wfc.run(); + + if (result.width == 0 && result.height == 0) { + return Array2D(0, 0); } - return std::nullopt; + + return to_image(result); } }; diff --git a/modules/wfc/tiling_wfc.h b/modules/wfc/tiling_wfc.h index 5586509db..947d588ef 100644 --- a/modules/wfc/tiling_wfc.h +++ b/modules/wfc/tiling_wfc.h @@ -64,8 +64,7 @@ struct Tile { // Generate the map associating an orientation id to the orientation // id obtained when reflecting the tile along the x axis. - static std::vector - generate_reflection_map(const Symmetry &symmetry) { + static std::vector generate_reflection_map(const Symmetry &symmetry) { switch (symmetry) { case Symmetry::X: return { 0 }; @@ -332,12 +331,14 @@ public: } // Run the tiling wfc and return the result if the algorithm succeeded - std::optional> run() { - auto a = wfc.run(); - if (a == std::nullopt) { - return std::nullopt; + Array2D run() { + Array2D a = wfc.run(); + + if (a.width == 0 && a.height == 0) { + return Array2D(0, 0); } - return id_to_tiling(*a); + + return id_to_tiling(a); } }; diff --git a/modules/wfc/wave.cpp b/modules/wfc/wave.cpp index b50aeb589..390e97432 100644 --- a/modules/wfc/wave.cpp +++ b/modules/wfc/wave.cpp @@ -5,8 +5,7 @@ namespace { // Return distribution * log(distribution). -std::vector -get_plogp(const std::vector &distribution) { +std::vector get_plogp(const std::vector &distribution) { std::vector plogp; for (unsigned i = 0; i < distribution.size(); i++) { plogp.push_back(distribution[i] * log(distribution[i])); @@ -65,9 +64,7 @@ void Wave::set(unsigned index, unsigned pattern, bool value) { memoisation.sum[index] -= patterns_frequencies[pattern]; memoisation.log_sum[index] = log(memoisation.sum[index]); memoisation.nb_patterns[index]--; - memoisation.entropy[index] = - memoisation.log_sum[index] - - memoisation.plogp_sum[index] / memoisation.sum[index]; + memoisation.entropy[index] = memoisation.log_sum[index] - memoisation.plogp_sum[index] / memoisation.sum[index]; // If there is no patterns possible in the cell, then there is a // contradiction. if (memoisation.nb_patterns[index] == 0) { diff --git a/modules/wfc/wfc.cpp b/modules/wfc/wfc.cpp index 8044a62ba..4894c7c14 100644 --- a/modules/wfc/wfc.cpp +++ b/modules/wfc/wfc.cpp @@ -36,14 +36,14 @@ WFC::WFC(bool periodic_output, int seed, unsigned wave_width) : gen(seed), patterns_frequencies(normalize(patterns_frequencies)), wave(wave_height, wave_width, patterns_frequencies), nb_patterns(propagator.size()), propagator(wave.height, wave.width, periodic_output, propagator) {} -std::optional> WFC::run() { +Array2D WFC::run() { while (true) { // Define the value of an undefined cell. ObserveStatus result = observe(); // Check if the algorithm has terminated. if (result == failure) { - return std::nullopt; + return Array2D(0, 0); } else if (result == success) { return wave_to_output(); } @@ -90,8 +90,7 @@ WFC::ObserveStatus WFC::observe() { // And define the cell with the pattern. for (unsigned k = 0; k < nb_patterns; k++) { if (wave.get(argmin, k) != (k == chosen_value)) { - propagator.add_to_propagator(argmin / wave.width, argmin % wave.width, - k); + propagator.add_to_propagator(argmin / wave.width, argmin % wave.width, k); wave.set(argmin, k, false); } } diff --git a/modules/wfc/wfc.h b/modules/wfc/wfc.h index 51ac7ab44..0914ec9d0 100644 --- a/modules/wfc/wfc.h +++ b/modules/wfc/wfc.h @@ -37,7 +37,7 @@ public: unsigned wave_width); // Run the algorithm, and return a result if it succeeded. - std::optional> run(); + Array2D run(); // Return value of observe. enum ObserveStatus {