From c133463b93c7ecbfd57cea5f1bcc225b14393a81 Mon Sep 17 00:00:00 2001 From: jfbourdon Date: Thu, 4 Jul 2024 13:27:42 -0400 Subject: [PATCH 1/3] sort by X, Y and then Z --- .../hydro_analysis/breach_depressions_least_cost.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs b/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs index 6a356589..93174bf0 100755 --- a/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs +++ b/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs @@ -458,7 +458,12 @@ impl WhiteboxTool for BreachDepressionsLeastCost { //////////////////////////////////////////////////////////////////////////////////////////// /* Vec is a stack and so if we want to pop the values from lowest to highest, we need to sort - them from highest to lowest. */ + them from highest to lowest. The stack being built with parallelism, the order in which the + values are added can't be garanted from run to run meaning that a simple sort by height might + lead to different breaching solutions caused by a different processing order if two pits have + the same height. To avoid this, pit cells are sorted first by X, then by Y and finally by height.*/ + undefined_flow_cells.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Equal)); + undefined_flow_cells.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Equal)); undefined_flow_cells.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(Equal)); let num_deps = undefined_flow_cells.len(); if num_deps == 0 && verbose { @@ -475,6 +480,8 @@ impl WhiteboxTool for BreachDepressionsLeastCost { let filter_size = ((max_dist * 2 + 1) * (max_dist * 2 + 1)) as usize; let mut minheap = BinaryHeap::with_capacity(filter_size); while let Some(cell) = undefined_flow_cells.pop() { + // Height is retrieved from the output raster instead of the popped cell because + // the current height could have been modified by a previous breaching iteration row = cell.0; col = cell.1; z = output.get_value(row, col); From 7799cf9f6066573fc4bdc4e2857aa8676827e52f Mon Sep 17 00:00:00 2001 From: jfbourdon Date: Tue, 3 Sep 2024 16:09:07 -0400 Subject: [PATCH 2/3] more elegant and fast sort --- .../tools/hydro_analysis/breach_depressions_least_cost.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs b/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs index 93174bf0..5b336215 100755 --- a/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs +++ b/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs @@ -462,9 +462,11 @@ impl WhiteboxTool for BreachDepressionsLeastCost { values are added can't be garanted from run to run meaning that a simple sort by height might lead to different breaching solutions caused by a different processing order if two pits have the same height. To avoid this, pit cells are sorted first by X, then by Y and finally by height.*/ - undefined_flow_cells.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Equal)); - undefined_flow_cells.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Equal)); - undefined_flow_cells.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(Equal)); + undefined_flow_cells.sort_by(|a, b| { + a.2.partial_cmp(&b.2).unwrap() + .then(b.1.cmp(&a.1)) + .then(b.0.cmp(&a.0)) + }); let num_deps = undefined_flow_cells.len(); if num_deps == 0 && verbose { println!("No depressions found. Process ending..."); From 719b25e46ef670e4c67a839b55bcba85e5bc5f6a Mon Sep 17 00:00:00 2001 From: jfbourdon Date: Tue, 3 Sep 2024 16:13:42 -0400 Subject: [PATCH 3/3] oups... inverted order --- .../src/tools/hydro_analysis/breach_depressions_least_cost.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs b/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs index 5b336215..3cb26ad4 100755 --- a/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs +++ b/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions_least_cost.rs @@ -463,7 +463,7 @@ impl WhiteboxTool for BreachDepressionsLeastCost { lead to different breaching solutions caused by a different processing order if two pits have the same height. To avoid this, pit cells are sorted first by X, then by Y and finally by height.*/ undefined_flow_cells.sort_by(|a, b| { - a.2.partial_cmp(&b.2).unwrap() + b.2.partial_cmp(&a.2).unwrap() .then(b.1.cmp(&a.1)) .then(b.0.cmp(&a.0)) });