Fix a few typos and remove std::optional from wcf.

This commit is contained in:
Relintai 2022-04-21 14:28:04 +02:00
parent dfbae29faa
commit 5be272e5cb
7 changed files with 25 additions and 26 deletions

View File

@ -21,7 +21,7 @@ public:
Array2D(std::size_t p_height, std::size_t p_width, T p_value) { Array2D(std::size_t p_height, std::size_t p_width, T p_value) {
height = p_height; height = p_height;
width = p_width; 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 { const T &get(std::size_t i, std::size_t j) const {

View File

@ -20,7 +20,7 @@ public:
data.resize(width * height * depth); 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; height = p_height;
width = p_width; width = p_width;
depth = p_depth; depth = p_depth;

View File

@ -228,14 +228,14 @@ private:
return output; return output;
} }
std::optional<unsigned> get_pattern_id(const Array2D<T> &pattern) { unsigned get_pattern_id(const Array2D<T> &pattern) {
unsigned *pattern_id = std::find(patterns.begin(), patterns.end(), pattern); unsigned *pattern_id = std::find(patterns.begin(), patterns.end(), pattern);
if (pattern_id != patterns.end()) { if (pattern_id != patterns.end()) {
return *pattern_id; return *pattern_id;
} }
return std::nullopt; return -1;
} }
// Set the pattern at a specific position, given its pattern id // 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. // Run the WFC algorithm, and return the result if the algorithm succeeded.
std::optional<Array2D<T>> run() { Array2D<T> run() {
std::optional<Array2D<unsigned>> result = wfc.run(); Array2D<unsigned> result = wfc.run();
if (result.has_value()) {
return to_image(*result); if (result.width == 0 && result.height == 0) {
return Array2D<T>(0, 0);
} }
return std::nullopt;
return to_image(result);
} }
}; };

View File

@ -64,8 +64,7 @@ struct Tile {
// Generate the map associating an orientation id to the orientation // Generate the map associating an orientation id to the orientation
// id obtained when reflecting the tile along the x axis. // id obtained when reflecting the tile along the x axis.
static std::vector<unsigned> static std::vector<unsigned> generate_reflection_map(const Symmetry &symmetry) {
generate_reflection_map(const Symmetry &symmetry) {
switch (symmetry) { switch (symmetry) {
case Symmetry::X: case Symmetry::X:
return { 0 }; return { 0 };
@ -332,12 +331,14 @@ public:
} }
// Run the tiling wfc and return the result if the algorithm succeeded // Run the tiling wfc and return the result if the algorithm succeeded
std::optional<Array2D<T>> run() { Array2D<T> run() {
auto a = wfc.run(); Array2D<unsigned> a = wfc.run();
if (a == std::nullopt) {
return std::nullopt; if (a.width == 0 && a.height == 0) {
return Array2D<T>(0, 0);
} }
return id_to_tiling(*a);
return id_to_tiling(a);
} }
}; };

View File

@ -5,8 +5,7 @@
namespace { namespace {
// Return distribution * log(distribution). // Return distribution * log(distribution).
std::vector<double> std::vector<double> get_plogp(const std::vector<double> &distribution) {
get_plogp(const std::vector<double> &distribution) {
std::vector<double> plogp; std::vector<double> plogp;
for (unsigned i = 0; i < distribution.size(); i++) { for (unsigned i = 0; i < distribution.size(); i++) {
plogp.push_back(distribution[i] * log(distribution[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.sum[index] -= patterns_frequencies[pattern];
memoisation.log_sum[index] = log(memoisation.sum[index]); memoisation.log_sum[index] = log(memoisation.sum[index]);
memoisation.nb_patterns[index]--; memoisation.nb_patterns[index]--;
memoisation.entropy[index] = memoisation.entropy[index] = memoisation.log_sum[index] - memoisation.plogp_sum[index] / memoisation.sum[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 // If there is no patterns possible in the cell, then there is a
// contradiction. // contradiction.
if (memoisation.nb_patterns[index] == 0) { if (memoisation.nb_patterns[index] == 0) {

View File

@ -36,14 +36,14 @@ WFC::WFC(bool periodic_output, int seed,
unsigned wave_width) : 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) {} 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<Array2D<unsigned>> WFC::run() { Array2D<unsigned> WFC::run() {
while (true) { while (true) {
// Define the value of an undefined cell. // Define the value of an undefined cell.
ObserveStatus result = observe(); ObserveStatus result = observe();
// Check if the algorithm has terminated. // Check if the algorithm has terminated.
if (result == failure) { if (result == failure) {
return std::nullopt; return Array2D<unsigned>(0, 0);
} else if (result == success) { } else if (result == success) {
return wave_to_output(); return wave_to_output();
} }
@ -90,8 +90,7 @@ WFC::ObserveStatus WFC::observe() {
// And define the cell with the pattern. // And define the cell with the pattern.
for (unsigned k = 0; k < nb_patterns; k++) { for (unsigned k = 0; k < nb_patterns; k++) {
if (wave.get(argmin, k) != (k == chosen_value)) { if (wave.get(argmin, k) != (k == chosen_value)) {
propagator.add_to_propagator(argmin / wave.width, argmin % wave.width, propagator.add_to_propagator(argmin / wave.width, argmin % wave.width, k);
k);
wave.set(argmin, k, false); wave.set(argmin, k, false);
} }
} }

View File

@ -37,7 +37,7 @@ public:
unsigned wave_width); unsigned wave_width);
// Run the algorithm, and return a result if it succeeded. // Run the algorithm, and return a result if it succeeded.
std::optional<Array2D<unsigned>> run(); Array2D<unsigned> run();
// Return value of observe. // Return value of observe.
enum ObserveStatus { enum ObserveStatus {