Skip to content

Commit

Permalink
refactor: faster iteration (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smirkey authored Jan 21, 2024
1 parent e443fea commit d550464
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions powerboxesrs/src/tiou.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::vec;

use ndarray::Array2;
use num_traits::{Num, ToPrimitive};

Expand Down Expand Up @@ -41,26 +43,29 @@ where
let mut tiou_matrix = Array2::<f64>::zeros((num_boxes1, num_boxes2));
let areas_boxes1 = boxes::box_areas(&boxes1);
let areas_boxes2 = boxes::box_areas(&boxes2);
for (i, a1) in boxes1.outer_iter().enumerate() {
let a1_x1 = a1[0];
let a1_y1 = a1[1];
let a1_x2 = a1[2];
let a1_y2 = a1[3];
let boxes1_vecs: Vec<(N, N, N, N)> = boxes1
.rows()
.into_iter()
.map(|r| (r[0], r[1], r[2], r[3]))
.collect();
let boxes2_vecs: Vec<(N, N, N, N)> = boxes2
.rows()
.into_iter()
.map(|r| (r[0], r[1], r[2], r[3]))
.collect();
for (i, a1) in boxes1_vecs.iter().enumerate() {
let (a1_x1, a1_y1, a1_x2, a1_y2) = a1;

let area1 = areas_boxes1[i];

for (j, a2) in boxes2.outer_iter().enumerate() {
let a2_x1 = a2[0];
let a2_y1 = a2[1];
let a2_x2 = a2[2];
let a2_y2 = a2[3];
for (j, a2) in boxes2_vecs.iter().enumerate() {
let (a2_x1, a2_y1, a2_x2, a2_y2) = a2;
let area2 = areas_boxes2[j];

// Calculate the enclosing box (C) coordinates
let c_x1 = utils::min(a1_x1, a2_x1);
let c_y1 = utils::min(a1_y1, a2_y1);
let c_x2 = utils::max(a1_x2, a2_x2);
let c_y2 = utils::max(a1_y2, a2_y2);
let c_x1 = utils::min(*a1_x1, *a2_x1);
let c_y1 = utils::min(*a1_y1, *a2_y1);
let c_x2 = utils::max(*a1_x2, *a2_x2);
let c_y2 = utils::max(*a1_y2, *a2_y2);
// Calculate the area of the enclosing box (C)
let c_area = (c_x2 - c_x1) * (c_y2 - c_y1);
let c_area = c_area.to_f64().unwrap();
Expand Down

0 comments on commit d550464

Please sign in to comment.