Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Feb 18, 2024
1 parent bcf5e48 commit 85af117
Show file tree
Hide file tree
Showing 52 changed files with 381 additions and 525 deletions.
38 changes: 26 additions & 12 deletions assets/config_lbf.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
{
"cde_config": {
"quadtree": {
"FixedDepth": 5
},
"haz_prox": {
"Enabled": {
"n_cells": 10000
}
},
"quadtree_depth": 5,
"hpg_n_cells": 2000,
"item_surrogate_config": {
"pole_coverage_goal": 0.9,
"max_poles": 10,
Expand All @@ -16,16 +10,36 @@
}
},
"poly_simpl_config": {
"Enabled": {
"mode": "enabled",
"params": {
"tolerance": 0.001
}
},
"deterministic_mode": true,
"deterministic_mode": false,
"n_samples_per_item": 5000,
"ls_samples_fraction": 0.5,
"ls_samples_fraction": 0.2,
"svg_draw_options": {
"theme": {
"stroke_width_multiplier": 2.0,
"bin_fill": "#CC824A",
"item_fill": "#FFC879",
"hole_fill": "#2D2D2D",
"qz_fill": [
"#000000",
"#FF0000",
"#FF5E00",
"#FFA500",
"#FFAD00",
"#FFFF00",
"#CBFF00",
"#CBFF00",
"#CBFF00",
"#CBFF00"
],
"qz_stroke_opac": 0.5
},
"quadtree": false,
"haz_prox_grid": false,
"surrogate": false
}
}
}
32 changes: 12 additions & 20 deletions jaguars/src/collision_detection/cd_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use indexmap::IndexSet;
use itertools::Itertools;
use tribool::Tribool;

use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::HazardEntity;
use crate::collision_detection::hpg::grid::Grid;
use crate::collision_detection::hpg::hazard_proximity_grid::{HazardProximityGrid, PendingChangesErr};
use crate::collision_detection::hpg::hpg_cell::HPGCell;
use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::HazardEntity;
use crate::collision_detection::quadtree::qt_node::QTNode;
use crate::geometry::fail_fast::sp_surrogate::SPSurrogate;
use crate::geometry::geo_enums::{GeoPosition, GeoRelation};
Expand All @@ -18,8 +18,7 @@ use crate::geometry::primitives::point::Point;
use crate::geometry::primitives::simple_polygon::SimplePolygon;
use crate::geometry::transformation::Transformation;
use crate::util::assertions;
use crate::util::config::{CDEConfig, HazProxConfig, QuadTreeConfig};

use crate::util::config::CDEConfig;

/// The Collision Detection Engine (CDE) is responsible for validating potential placements of items in a `Layout`.
/// It can be queried to check if a given shape or surrogate collides with any `Hazard`s in the `Layout`.
Expand All @@ -46,19 +45,12 @@ pub struct CDESnapshot {

impl CDEngine {
pub fn new(bbox: AARectangle, static_hazards: Vec<Hazard>, config: CDEConfig) -> CDEngine {
let qt_depth = match config.quadtree {
QuadTreeConfig::FixedDepth(depth) => depth,
QuadTreeConfig::Auto => panic!("not implemented, quadtree depth must be specified"),
};

let haz_prox_grid = match config.haz_prox {
HazProxConfig::Disabled => None,
HazProxConfig::Enabled { n_cells } => {
Some(HazardProximityGrid::new(bbox.clone(), &static_hazards, n_cells))
}
let haz_prox_grid = match config.hpg_n_cells {
0 => None,
hpg_n_cells => Some(HazardProximityGrid::new(bbox.clone(), &static_hazards, hpg_n_cells))
};

let mut qt_root = QTNode::new(qt_depth, bbox.clone(), None);
let mut qt_root = QTNode::new(config.quadtree_depth, bbox.clone(), None);

for haz in static_hazards.iter() {
qt_root.register_hazard(haz.into());
Expand Down Expand Up @@ -120,7 +112,7 @@ impl CDEngine {
assert!(!self.haz_prox_grid.as_ref().map_or(false, |hpg| hpg.has_pending_deregisters()));
CDESnapshot {
dynamic_hazards: self.dynamic_hazards.clone(),
grid: self.haz_prox_grid.as_ref().map(|hpg| hpg.grid().clone())
grid: self.haz_prox_grid.as_ref().map(|hpg| hpg.grid.clone())
}
}

Expand Down Expand Up @@ -190,8 +182,8 @@ impl CDEngine {
}

pub fn smallest_qt_node_dimension(&self) -> f64 {
let bbox = self.quadtree.bbox();
let level = self.quadtree.level();
let bbox = &self.quadtree.bbox;
let level = self.quadtree.level;
//every level, the dimension is halved
bbox.width() / 2.0_f64.powi(level as i32)
}
Expand Down Expand Up @@ -346,12 +338,12 @@ impl CDEngine {
if std::ptr::eq(haz_shape, s_omega) {
//s_omega is registered in the quadtree.
//maybe the quadtree can help us.
match self.quadtree.point_definitely_collides_with(&s_mu.poi().center, &haz.entity).try_into() {
match self.quadtree.point_definitely_collides_with(&s_mu.poi.center, &haz.entity).try_into() {
Ok(collides) => return collides,
Err(_) => (), //no definitive answer
}
}
let inclusion = s_omega.collides_with(&s_mu.poi().center);
let inclusion = s_omega.collides_with(&s_mu.poi.center);

match haz.entity.position() {
GeoPosition::Interior => inclusion,
Expand Down
1 change: 0 additions & 1 deletion jaguars/src/collision_detection/hazard_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use itertools::Itertools;
use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::HazardEntity;


/// Trait that allows for filtering out irrelevant hazards depending on the context.
/// Enables ignoring certain hazards when querying the CDE.
pub trait HazardFilter {
Expand Down
4 changes: 2 additions & 2 deletions jaguars/src/collision_detection/hpg/boundary_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl BoundaryFillGrid {

/// Creates a new BoundaryFillGrid, add all cells inside the seed_bbox to the queue
pub fn new<T>(grid: &Grid<T>, seed_bbox: AARectangle) -> Self {
let state = vec![CellState::new(); grid.n_rows() * grid.n_cols()];
let state = vec![CellState::new(); grid.n_rows * grid.n_cols];

//Find the range of rows and columns which reside inside the seed_bbox
let row_range = grid.rows_in_range(seed_bbox.y_min..=seed_bbox.y_max);
Expand Down Expand Up @@ -50,7 +50,7 @@ impl BoundaryFillGrid {
false => {
match self.seed_iterator.next() {
Some((row, col)) => {
let cell = grid.get_index(row, col);
let cell = grid.to_index(row, col);
if let Some(cell) = cell {
self.state[cell].visit();
self.n_visited += 1;
Expand Down
31 changes: 8 additions & 23 deletions jaguars/src/collision_detection/hpg/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ use std::ops::RangeInclusive;

use itertools::Itertools;
use ordered_float::NotNan;

use crate::geometry::primitives::point::Point;

/// Representation of a grid of optional elements of type T
/// Divided into rows and columns, where each row and column has a unique coordinate
#[derive(Clone, Debug)]
pub struct Grid<T> {
cells: Vec<Option<T>>,
rows: Vec<NotNan<f64>>,
cols: Vec<NotNan<f64>>,
n_rows: usize,
n_cols: usize,
pub cells: Vec<Option<T>>,
pub rows: Vec<NotNan<f64>>,
pub cols: Vec<NotNan<f64>>,
pub n_rows: usize,
pub n_cols: usize,
}

impl<T> Grid<T> {
Expand Down Expand Up @@ -113,7 +114,7 @@ impl<T> Grid<T> {
neighbors
}

pub fn get_index(&self, row: usize, col: usize) -> Option<usize> {
pub fn to_index(&self, row: usize, col: usize) -> Option<usize> {
Self::calculate_index(row, col, self.n_rows, self.n_cols)
}

Expand All @@ -124,7 +125,7 @@ impl<T> Grid<T> {
}
}

pub fn get_row_col(&self, index: usize) -> Option<(usize, usize)> {
pub fn to_row_col(&self, index: usize) -> Option<(usize, usize)> {
match index.cmp(&(self.n_rows * self.n_cols)) {
Ordering::Less => {
let row = index / self.n_cols;
Expand All @@ -134,20 +135,4 @@ impl<T> Grid<T> {
_ => None //out of bounds
}
}

pub fn n_rows(&self) -> usize {
self.n_rows
}

pub fn n_cols(&self) -> usize {
self.n_cols
}

pub fn elements(&self) -> &Vec<Option<T>> {
&self.cells
}

pub fn elements_mut(&mut self) -> &mut Vec<Option<T>> {
&mut self.cells
}
}
3 changes: 2 additions & 1 deletion jaguars/src/collision_detection/hpg/grid_generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cmp::Ordering;
use log::{debug};

use log::debug;

use crate::collision_detection::hazard::Hazard;
use crate::geometry::geo_traits::{DistanceFrom, Shape};
Expand Down
38 changes: 13 additions & 25 deletions jaguars/src/collision_detection/hpg/hazard_proximity_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ use std::iter;

use itertools::Itertools;

use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::HazardEntity;
use crate::collision_detection::hpg::boundary_fill::BoundaryFillGrid;
use crate::collision_detection::hpg::grid::Grid;
use crate::collision_detection::hpg::grid_generator;
use crate::collision_detection::hpg::hpg_cell::{HPGCellUpdate, HPGCell};
use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::HazardEntity;
use crate::collision_detection::hpg::hpg_cell::{HPGCell, HPGCellUpdate};
use crate::geometry::geo_traits::Shape;
use crate::geometry::primitives::aa_rectangle::AARectangle;

/// Grid of cells which store information about hazards in their vicinity.
/// The grid is a part of the `CDEngine` and is thus automatically updated when hazards are registered or deregistered.
#[derive(Debug, Clone)]
pub struct HazardProximityGrid {
grid: Grid<HPGCell>,
pub grid: Grid<HPGCell>,
pending_deregisters: Vec<HazardEntity>,
cell_radius : f64,
pub cell_radius : f64,
}

impl HazardProximityGrid {
Expand Down Expand Up @@ -45,7 +45,7 @@ impl HazardProximityGrid {
}

pub fn restore(&mut self, grid: Grid<HPGCell>) {
assert_eq!(self.grid.elements().len(), grid.elements().len());
assert_eq!(self.grid.cells.len(), grid.cells.len());
self.grid = grid;
self.pending_deregisters.clear();
}
Expand All @@ -64,7 +64,7 @@ impl HazardProximityGrid {
let mut b_fill = BoundaryFillGrid::new(&self.grid, seed_bbox);

while let Some(next_dot_index) = b_fill.pop(&self.grid) {
let cell = self.grid.elements_mut()[next_dot_index].as_mut();
let cell = self.grid.cells[next_dot_index].as_mut();
if let Some(cell) = cell {
match cell.register_hazard(to_register) {
HPGCellUpdate::Affected => {
Expand All @@ -81,23 +81,23 @@ impl HazardProximityGrid {
//TODO: move this to an assertion check
debug_assert!(
{
let old_cells = self.grid.elements().clone();
let old_cells = self.grid.cells.clone();

//ensure no changes remain
let undetected = self.grid.elements_mut().iter_mut().enumerate()
let undetected = self.grid.cells.iter_mut().enumerate()
.flat_map(|(i, cell)| cell.as_mut().map(|cell| (i, cell)))
.map(|(i, cell)| (i, cell.register_hazard(to_register)))
.filter(|(_i, res)| res == &HPGCellUpdate::Affected)
.map(|(i, _res)| i)
.collect_vec();

let undetected_row_cols = undetected.iter().map(|i| self.grid.get_row_col(*i).unwrap()).collect_vec();
let undetected_row_cols = undetected.iter().map(|i| self.grid.to_row_col(*i).unwrap()).collect_vec();

if undetected.len() != 0 {
println!("{:?} undetected affected cells", undetected_row_cols);
for i in undetected {
println!("old {:?}", &old_cells[i]);
println!("new {:?}", &self.grid.elements()[i]);
println!("new {:?}", &self.grid.cells[i]);
}
false
} else {
Expand All @@ -112,7 +112,7 @@ impl HazardProximityGrid {
{
match process_now {
true => {
for cell in self.grid.elements_mut().iter_mut().flatten() {
for cell in self.grid.cells.iter_mut().flatten() {
let result = cell.deregister_hazards(iter::once(to_deregister), remaining.clone());
match result {
HPGCellUpdate::Affected => (),
Expand All @@ -133,7 +133,7 @@ impl HazardProximityGrid {
if self.has_pending_deregisters() {
let to_deregister = self.pending_deregisters.iter();

for cell in self.grid.elements_mut().iter_mut().flatten() {
for cell in self.grid.cells.iter_mut().flatten() {
cell.deregister_hazards(to_deregister.clone(), remaining.clone());
}

Expand All @@ -145,18 +145,6 @@ impl HazardProximityGrid {
!self.pending_deregisters.is_empty()
}

pub fn cells(&self) -> &Vec<Option<HPGCell>> {
&self.grid.elements()
}

pub fn cell_radius(&self) -> f64 {
self.cell_radius
}

pub fn grid(&self) -> &Grid<HPGCell> {
&self.grid
}

}


Expand Down
8 changes: 4 additions & 4 deletions jaguars/src/collision_detection/hpg/hpg_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use std::cmp::Ordering;

use itertools::Itertools;

use crate::collision_detection::hpg::proximity::Proximity;
use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::HazardEntity;
use crate::collision_detection::hpg::proximity::Proximity;
use crate::entities::item::Item;
use crate::entities::quality_zone::N_QUALITIES;
use crate::geometry::geo_enums::GeoPosition;
use crate::geometry::geo_traits::{DistanceFrom, Shape};
use crate::geometry::primitives::aa_rectangle::AARectangle;
use crate::geometry::primitives::circle::Circle;
use crate::geometry::primitives::point::Point;
use crate::entities::quality_zone::N_QUALITIES;

/// Represents a cell in the Hazard Proximity Grid
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -165,8 +165,8 @@ impl HPGCell {
}

pub fn could_accommodate_item(&self, item: &Item) -> bool {
let haz_prox : f64 = (&self.hazard_proximity(item.base_quality())).into();
let item_poi_radius = item.shape().poi().radius;
let haz_prox : f64 = (&self.hazard_proximity(item.base_quality)).into();
let item_poi_radius = item.shape.poi.radius;

item_poi_radius < haz_prox + self.radius
}
Expand Down
Loading

0 comments on commit 85af117

Please sign in to comment.