Skip to content

Commit

Permalink
consolidated a lot of files
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Feb 2, 2024
1 parent 90ca9fa commit 45b84c6
Show file tree
Hide file tree
Showing 67 changed files with 571 additions and 775 deletions.
6 changes: 3 additions & 3 deletions assets/config_lbf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"cde_config": {
"quadtree": {
"FixedDepth": 8
"FixedDepth": 4
},
"haz_prox": {
"Enabled": {
Expand All @@ -12,7 +12,7 @@
"pole_coverage_goal": 0.9,
"max_poles": 10,
"n_ff_poles": 1,
"n_ff_piers": 1
"n_ff_piers": 0
}
},
"poly_simpl_config": {
Expand All @@ -26,6 +26,6 @@
"svg_draw_options": {
"quadtree": true,
"haz_prox_grid": false,
"surrogate": false
"surrogate": true
}
}
33 changes: 21 additions & 12 deletions jaguars/src/collision_detection/cd_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use indexmap::IndexSet;
use itertools::Itertools;
use tribool::Tribool;

use crate::collision_detection::cde_snapshot::CDESnapshot;
use crate::collision_detection::haz_prox_grid::grid::Grid;
use crate::collision_detection::haz_prox_grid::hazard_proximity_grid::{HazardProximityGrid, PendingChangesErr};
use crate::collision_detection::haz_prox_grid::hpg_cell::HPGCell;
use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::HazardEntity;
use crate::collision_detection::quadtree::qt_node::QTNode;
Expand Down Expand Up @@ -104,10 +105,10 @@ impl CDEngine {
pub fn create_snapshot(&mut self) -> CDESnapshot {
self.commit_deregisters();
assert!(!self.haz_prox_grid.as_ref().map_or(false, |hpg| hpg.has_pending_deregisters()));
CDESnapshot::new(
self.dynamic_hazards.clone(),
self.haz_prox_grid.as_ref().map(|hpg| hpg.grid().clone()),
)
CDESnapshot {
dynamic_hazards: self.dynamic_hazards.clone(),
grid: self.haz_prox_grid.as_ref().map(|hpg| hpg.grid().clone())
}
}

pub fn restore(&mut self, snapshot: &CDESnapshot) {
Expand All @@ -116,7 +117,7 @@ impl CDEngine {
debug_assert!(hazards_to_remove.len() == self.dynamic_hazards.len());
let mut hazards_to_add = vec![];

for hazard in snapshot.dynamic_hazards().iter() {
for hazard in snapshot.dynamic_hazards.iter() {
let hazard_already_present = hazards_to_remove.swap_remove(&hazard.entity);
if !hazard_already_present {
hazards_to_add.push(hazard.clone());
Expand Down Expand Up @@ -150,10 +151,10 @@ impl CDEngine {

//Hazard proximity grid
self.haz_prox_grid.as_mut().map(|hpg| {
hpg.restore(snapshot.grid().clone().expect("no hpg in snapshot"));
hpg.restore(snapshot.grid.clone().expect("no hpg in snapshot"));
});

debug_assert!(self.dynamic_hazards.len() == snapshot.dynamic_hazards().len());
debug_assert!(self.dynamic_hazards.len() == snapshot.dynamic_hazards.len());
}

fn commit_deregisters(&mut self) {
Expand Down Expand Up @@ -266,7 +267,7 @@ impl CDEngine {
}

pub fn circle_definitely_collides(&self, circle: &Circle, ignored_entities: &[HazardEntity]) -> Tribool {
match self.bbox.collides_with(&circle.center()) {
match self.bbox.collides_with(&circle.center) {
false => Tribool::True, //outside the quadtree, so definitely collides
true => self.quadtree.definitely_collides(circle, ignored_entities)
}
Expand All @@ -282,7 +283,7 @@ impl CDEngine {
ignored_entities.push(haz_entity.clone());
}

let circle_center_in_qt = self.bbox.collides_with(&circle.center());
let circle_center_in_qt = self.bbox.collides_with(&circle.center);

if !circle_center_in_qt && colliding_entities.is_empty() {
// The circle center is outside the quadtree
Expand Down Expand Up @@ -323,17 +324,25 @@ 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.presence() {
GeoPosition::Interior => inclusion,
GeoPosition::Exterior => !inclusion,
}
})
}
}

//Snapshot of the CDE state at a given time.
//Can be used to restore the CDE to a previous state.
#[derive(Clone, Debug)]
pub struct CDESnapshot {
dynamic_hazards: Vec<Hazard>,
grid: Option<Grid<HPGCell>>
}
28 changes: 0 additions & 28 deletions jaguars/src/collision_detection/cde_snapshot.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl BoundaryFillGrid {
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());
let col_range = grid.cols_in_range(seed_bbox.x_min()..=seed_bbox.x_max());
let row_range = grid.rows_in_range(seed_bbox.y_min..=seed_bbox.y_max);
let col_range = grid.cols_in_range(seed_bbox.x_min..=seed_bbox.x_max);

//FIFO queue to keep track of which cells to visit next
let queue = VecDeque::with_capacity(state.len());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub fn generate(bbox: AARectangle, hazards: &[Hazard], target_n_cells: usize) ->
let cell_radius = f64::sqrt(2.0 * (cell_dim / 2.0).powi(2)); //half of the maximum distance between two cell centers

for i in 0..n_cells_in_x {
let x_min = bbox.x_min() + cell_dim * i as f64;
let x_min = bbox.x_min + cell_dim * i as f64;
let x_max = x_min + cell_dim;
for j in 0..n_cells_in_y {
let y_min = bbox.y_min() + cell_dim * j as f64;
let y_min = bbox.y_min + cell_dim * j as f64;
let y_max = y_min + cell_dim;
let rect = AARectangle::new(x_min, y_min, x_max, y_max);
//test if the cell is relevant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ impl HazardProximityGrid {
let seed_bbox = {
let shape_bbox = to_register.shape.bbox();
AARectangle::new(
shape_bbox.x_min() - self.cell_radius,
shape_bbox.y_min() - self.cell_radius,
shape_bbox.x_max() + self.cell_radius,
shape_bbox.y_max() + self.cell_radius,
shape_bbox.x_min - self.cell_radius,
shape_bbox.y_min - self.cell_radius,
shape_bbox.x_max + self.cell_radius,
shape_bbox.y_max + self.cell_radius,
)
};

Expand Down
2 changes: 1 addition & 1 deletion jaguars/src/collision_detection/haz_prox_grid/hpg_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ 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 item_poi_radius = item.shape().poi().radius;

item_poi_radius < haz_prox + self.radius
}
Expand Down
2 changes: 1 addition & 1 deletion jaguars/src/collision_detection/hazard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::entities::placed_item_uid::PlacedItemUID;
use crate::entities::placed_item::PlacedItemUID;
use crate::geometry::geo_enums::GeoPosition;
use crate::geometry::primitives::simple_polygon::SimplePolygon;

Expand Down
2 changes: 0 additions & 2 deletions jaguars/src/collision_detection/hazard_filters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub mod hazard_filter;
pub mod combo_haz_filter;

pub mod entity_haz_filter;

pub mod qz_haz_filter;
pub mod bin_haz_filter;
1 change: 0 additions & 1 deletion jaguars/src/collision_detection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod cd_engine;
pub mod quadtree;
pub mod haz_prox_grid;
pub mod cde_snapshot;
pub mod hazard_filters;

pub mod hazard;
9 changes: 5 additions & 4 deletions jaguars/src/collision_detection/quadtree/qt_hazard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Borrow;
use std::cmp::Ordering;
use crate::collision_detection::hazard::Hazard;
use crate::collision_detection::hazard::HazardEntity;
Expand All @@ -14,11 +15,11 @@ pub struct QTHazard {
pub active: bool,
}

impl From<&Hazard> for QTHazard {
fn from(hazard: &Hazard) -> Self {
impl<T> From<T> for QTHazard where T: Borrow<Hazard> {
fn from(hazard: T) -> Self {
Self {
entity: hazard.entity.clone(),
presence: QTHazPresence::Partial(hazard.into()),
entity: hazard.borrow().entity.clone(),
presence: QTHazPresence::Partial(hazard.borrow().into()),
active: true,
}
}
Expand Down
35 changes: 7 additions & 28 deletions jaguars/src/collision_detection/quadtree/qt_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ impl QTNode {

let constricted_hazards = hazard.constrict(child_bboxes);

for (i,c_hazard) in constricted_hazards.into_iter().enumerate(){
for (i, c_hazard) in constricted_hazards.into_iter().enumerate() {
if let Some(c_hazard) = c_hazard {
children[i].register_hazard(c_hazard);
}
}
}
}

self.invalidate_cache();

//If the hazard is of the partial type, and we are not at the max tree depth: generate children
if !self.has_children() && self.level > 0 && matches!(hazard.presence, QTHazPresence::Partial(_)) {
self.generate_children();
Expand All @@ -64,14 +62,12 @@ impl QTNode {
}

pub fn deregister_hazard(&mut self, hazard_entity: &HazardEntity) {
self.invalidate_cache();

let removed_ch = self.hazards.remove(hazard_entity);

if removed_ch.is_some() && self.has_children() {
if self.hazards.is_empty() || self.hazards.has_only_entire_hazards() {
//If there are no more inclusion, or only inclusion of type Entire, drop the children
self.drop_children();
self.children = None;
} else {
//Otherwise, recursively deregister the entity from the children
self.children.as_mut().unwrap().iter_mut()
Expand All @@ -83,7 +79,6 @@ impl QTNode {
pub fn activate_hazard(&mut self, entity: &HazardEntity) {
let modified = self.hazards.activate_hazard(entity);
if modified {
self.invalidate_cache();
match &mut self.children {
Some(children) => children.iter_mut()
.for_each(|c| c.activate_hazard(entity)),
Expand All @@ -95,7 +90,6 @@ impl QTNode {
pub fn deactivate_hazard(&mut self, entity: &HazardEntity) {
let modified = self.hazards.deactivate_hazard(entity);
if modified {
self.invalidate_cache();
match &mut self.children {
Some(children) => children.iter_mut()
.for_each(|c| c.deactivate_hazard(entity)),
Expand All @@ -106,16 +100,10 @@ impl QTNode {

fn generate_children(&mut self) {
if self.level > 0 {
self.invalidate_cache();

self.children = Some(
Box::new(
self.bbox.quadrants()
.map(|split_bbox|
QTNode::new(self.level - 1, split_bbox, None)
)
)
);
self.children = Some(Box::new(
self.bbox.quadrants()
.map(|split_bbox| QTNode::new(self.level - 1, split_bbox, None))
));
}
}

Expand All @@ -126,11 +114,6 @@ impl QTNode {
}
}

pub fn drop_children(&mut self) {
self.invalidate_cache();
self.children = None
}

pub fn has_children(&self) -> bool {
self.children.is_some()
}
Expand All @@ -151,10 +134,6 @@ impl QTNode {
&self.hazards
}

fn invalidate_cache(&mut self) {
//leave this here in case it is useful later
}

pub fn collides<T>(&self, entity: &T, ignored_entities: &[HazardEntity]) -> Option<&HazardEntity>
where T: CollidesWith<AARectangle>, QTPartialHazard: CollidesWith<T>
{
Expand Down Expand Up @@ -197,7 +176,7 @@ impl QTNode {


pub fn definitely_collides<T>(&self, entity: &T, ignored_entities: &[HazardEntity]) -> Tribool
where T: CollidesWith<AARectangle>
where T: CollidesWith<AARectangle>
{
match self.hazards.strongest(ignored_entities) {
None => Tribool::False,
Expand Down
9 changes: 5 additions & 4 deletions jaguars/src/collision_detection/quadtree/qt_partial_hazard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Weak};

Expand All @@ -21,11 +22,11 @@ pub enum EdgeIndices {
Some(Vec<usize>)
}

impl From<&Hazard> for QTPartialHazard {
fn from(hazard: &Hazard) -> Self {
impl<T> From<T> for QTPartialHazard where T: Borrow<Hazard> {
fn from(hazard: T) -> Self {
Self {
shape: Arc::downgrade(&hazard.shape),
position: hazard.entity.presence(),
shape: Arc::downgrade(&hazard.borrow().shape),
position: hazard.borrow().entity.presence(),
edge_indices: EdgeIndices::All,
}
}
Expand Down
Loading

0 comments on commit 45b84c6

Please sign in to comment.