From a9b40f133b069a30e3d7ddf6b96eb977378a2715 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Mon, 21 Mar 2022 14:55:38 +0800 Subject: [PATCH] refactor: no undef --- src/experimental/_impl/_tiles_wfc_impl.scad | 23 +++++++-------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/experimental/_impl/_tiles_wfc_impl.scad b/src/experimental/_impl/_tiles_wfc_impl.scad index accdaab2..74631c0b 100644 --- a/src/experimental/_impl/_tiles_wfc_impl.scad +++ b/src/experimental/_impl/_tiles_wfc_impl.scad @@ -55,25 +55,18 @@ function wf_eigenstates_at(wf, x, y) = wf_eigenstates(wf)[y][x]; function wf_collapse(wf, x, y) = let( - weights = wf_weights(wf), - states_xy = wf_eigenstates_at(wf, x, y), - weights_xy = [ - for(state = states_xy) - let(w = hashmap_get(weights, state)) - if(w != undef) - [state, w] - ], - leng = len(weights_xy), - threshold = rand() * sum([for(i = 0; i < leng; i = i + 1) weights_xy[i][1]]) + states = wf_eigenstates_at(wf, x, y), + all_weights = wf_weights(wf), + weights = [for(state = states) hashmap_get(all_weights, state)], + threshold = rand() * sum(weights) ) - _wf_collapse(wf, x, y, weights_xy, len(weights_xy), threshold); + _wf_collapse(wf, x, y, states, weights, len(states), threshold); -function _wf_collapse(wf, x, y, states_weights, leng, threshold, i = 0) = +function _wf_collapse(wf, x, y, states, weights, leng, threshold, i = 0) = threshold < 0 || i == leng ? wf : - let(state_weight = states_weights[i]) _wf_collapse( - threshold < state_weight[1] ? _replaceStatesAt(wf, x, y, [state_weight[0]]) : wf, - x, y, states_weights, leng, threshold - state_weight[1], i + 1 + threshold < weights[i] ? _replaceStatesAt(wf, x, y, [states[i]]) : wf, + x, y, states, weights, leng, threshold - weights[i], i + 1 ); // Shannon entropy