From 04554f634be015878771d02ebad5454278e18573 Mon Sep 17 00:00:00 2001 From: Emilia Allison Date: Fri, 15 Nov 2024 15:28:01 -0600 Subject: [PATCH] fix: Optimize region transfer map --- plate-tool-lib/src/transfer_region.rs | 58 ++++++++++++++++----------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/plate-tool-lib/src/transfer_region.rs b/plate-tool-lib/src/transfer_region.rs index a2227a2..a452c8a 100644 --- a/plate-tool-lib/src/transfer_region.rs +++ b/plate-tool-lib/src/transfer_region.rs @@ -202,22 +202,30 @@ impl TransferRegion { ); let count = ( // How many times can we replicate? - (1..) - .position(|n| { - n * number_used_src_wells.0 * il_dest.0.unsigned_abs() - - il_dest.0.unsigned_abs() - + 1 - > d_dims.0 - }) - .unwrap() as u8, - (1..) - .position(|n| { - n * number_used_src_wells.1 * il_dest.1.unsigned_abs() - - il_dest.1.unsigned_abs() - + 1 - > d_dims.1 - }) - .unwrap() as u8, + if il_dest.0.unsigned_abs() == 0 { + 1 + } else { + (1..) + .position(|n| { + (n * number_used_src_wells.0 * il_dest.0.unsigned_abs()) + .saturating_sub(il_dest.0.unsigned_abs()) + + 1 + > d_dims.0 + }) + .unwrap() as u8 + }, + if il_dest.1.unsigned_abs() == 0 { + 1 + } else { + (1..) + .position(|n| { + (n * number_used_src_wells.1 * il_dest.1.unsigned_abs()) + .saturating_sub(il_dest.1.unsigned_abs()) + + 1 + > d_dims.1 + }) + .unwrap() as u8 + }, ); let i = i .saturating_sub(s_ul.row) @@ -225,30 +233,34 @@ impl TransferRegion { let j = j .saturating_sub(s_ul.col) .saturating_div(il_source.1.unsigned_abs()); + let checked_il_dest = (u8::max(il_dest.0.unsigned_abs(), 1u8), + u8::max(il_dest.1.unsigned_abs(), 1u8)); + let row_modulus = number_used_src_wells.0 * checked_il_dest.0; + let column_modulus = number_used_src_wells.1 * checked_il_dest.1; Some( possible_destination_wells .into_iter() - .filter(|Well { row: x , ..}| { + .filter(|Well { row: x, .. }| { x.checked_sub(d_ul.row).unwrap() - % (number_used_src_wells.0 * il_dest.0.unsigned_abs()) // Counter along x + % row_modulus // Counter along x == (il_dest.0.unsigned_abs() *i) - % (number_used_src_wells.0 * il_dest.0.unsigned_abs()) + % row_modulus }) .filter(|Well { col: y, .. }| { y.checked_sub(d_ul.col).unwrap() - % (number_used_src_wells.1 * il_dest.1.unsigned_abs()) // Counter along u + % column_modulus // Counter along u == (il_dest.1.unsigned_abs() *j) - % (number_used_src_wells.1 * il_dest.1.unsigned_abs()) + % column_modulus }) .filter(|Well { row: x, col: y }| { // How many times have we replicated? < How many are we allowed // to replicate? x.checked_sub(d_ul.row).unwrap().div_euclid( - number_used_src_wells.0 * il_dest.0.unsigned_abs(), + row_modulus ) < count.0 && y.checked_sub(d_ul.col).unwrap().div_euclid( - number_used_src_wells.1 * il_dest.1.unsigned_abs(), + column_modulus ) < count.1 }) .collect(),