Skip to content

Commit

Permalink
minor cleanup of cd_engine methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Feb 7, 2024
1 parent df7a0de commit 66d512a
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 33 deletions.
24 changes: 10 additions & 14 deletions jaguars/src/collision_detection/cd_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,22 +217,13 @@ impl CDEngine {
}

//QUERY ----------------------------------------------------------------------------------------
pub fn poly_collides(&self, shape: &SimplePolygon, ignored_entities: &[HazardEntity]) -> bool {
pub fn shape_collides(&self, shape: &SimplePolygon, ignored_entities: &[HazardEntity]) -> bool {
match self.bbox.relation_to(&shape.bbox()) {
GeoRelation::Disjoint | GeoRelation::Enclosed | GeoRelation::Intersecting => {
return true;
} //Not fully inside quadtree -> definite collision
//Not fully inside bbox => definite collision
GeoRelation::Disjoint | GeoRelation::Enclosed | GeoRelation::Intersecting => true,
GeoRelation::Surrounding => {
//Intersection test
if shape.edge_iter().any(|e| self.quadtree.collides(&e, ignored_entities).is_some()) {
return true;
}

//Containment test
if self.collision_by_containment(shape, ignored_entities) {
return true;
}
return false;
self.collision_by_edge_intersection(shape, ignored_entities) ||
self.collision_by_containment(shape, ignored_entities)
}
}
}
Expand Down Expand Up @@ -297,6 +288,11 @@ impl CDEngine {
colliding_entities
}

fn collision_by_edge_intersection(&self, shape: &SimplePolygon, ignored_entities: &[HazardEntity]) -> bool {
shape.edge_iter()
.any(|e| self.quadtree.collides(&e, ignored_entities).is_some())
}

fn collision_by_containment(&self, shape: &SimplePolygon, ignored_entities: &[HazardEntity]) -> bool
{
//collect all active and non-ignored hazard_filters
Expand Down
7 changes: 4 additions & 3 deletions jaguars/src/collision_detection/quadtree/qt_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::collision_detection::quadtree::qt_hazard::QTHazard;
use crate::collision_detection::quadtree::qt_hazard::QTHazPresence;
use crate::collision_detection::quadtree::qt_hazard_vec::QTHazardVec;
use crate::collision_detection::quadtree::qt_partial_hazard::QTPartialHazard;
use crate::geometry::geo_traits::CollidesWith;
use crate::geometry::geo_traits::{CollidesWith, Shape};
use crate::geometry::primitives::aa_rectangle::AARectangle;
use crate::geometry::primitives::point::Point;

Expand Down Expand Up @@ -134,6 +134,8 @@ impl QTNode {
&self.hazards
}

/// Returns None if no collision between the entity and any hazard is detected,
/// otherwise returns the first encountered hazard that collides with the entity
pub fn collides<T>(&self, entity: &T, ignored_entities: &[HazardEntity]) -> Option<&HazardEntity>
where T: CollidesWith<AARectangle>, QTPartialHazard: CollidesWith<T>
{
Expand All @@ -146,7 +148,7 @@ impl QTNode {
QTHazPresence::Entire => Some(&strongest_hazard.entity),
QTHazPresence::Partial(_) => match self.children() {
Some(children) => {
//Search if any of the children intersect with the circle
//Search if any of the children intersect with the entity
children.iter()
.map(|child| child.collides(entity, ignored_entities))
.find(|x| x.is_some())
Expand Down Expand Up @@ -174,7 +176,6 @@ impl QTNode {
}
}


pub fn definitely_collides<T>(&self, entity: &T, ignored_entities: &[HazardEntity]) -> Tribool
where T: CollidesWith<AARectangle>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use std::hash::{Hash};
use std::sync::{Arc, Weak};

use crate::collision_detection::hazard::Hazard;
use crate::geometry::geo_enums::GeoPosition;
use crate::geometry::geo_traits::{CollidesWith, Shape};
use crate::geometry::primitives::circle::Circle;
use crate::geometry::primitives::edge::Edge;
Expand Down
2 changes: 1 addition & 1 deletion jaguars/src/entities/problems/sp_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl SPProblem {
let transf = p_uid.d_transf.compose();
if !self.layout.cde().surrogate_collides(shape.surrogate(), &transf, entities_to_ignore.as_slice()) {
let transformed_shape = shape.transform_clone(&transf);
if !self.layout.cde().poly_collides(&transformed_shape, entities_to_ignore.as_ref()) {
if !self.layout.cde().shape_collides(&transformed_shape, entities_to_ignore.as_ref()) {
let insert_opt = PlacingOption {
layout_index: LayoutIndex::Existing(0),
item_id: p_uid.item_id,
Expand Down
5 changes: 2 additions & 3 deletions jaguars/src/util/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ pub fn layouts_match(layout: &Layout, layout_snapshot: &LayoutSnapshot) -> bool
}
}
//TODO: add dotgrid check, check if quadtree does not contain any more uncommitted removals

true
}

Expand Down Expand Up @@ -110,7 +109,7 @@ pub fn item_to_place_does_not_collide(item: &Item, transformation: &Transformati
.map_or(vec![], |f| hazard_filter::ignored_entities(f, layout.cde().all_hazards()));

if layout.cde().surrogate_collides(shape.surrogate(), transformation, &entities_to_ignore) ||
layout.cde().poly_collides(&t_shape, &entities_to_ignore) {
layout.cde().shape_collides(&t_shape, &entities_to_ignore) {
return false;
}
return true;
Expand All @@ -125,7 +124,7 @@ pub fn layout_is_collision_free(layout: &Layout) -> bool {
};
let entities_to_ignore = hazard_filter::ignored_entities(&combo_filter,layout.cde().all_hazards());

if layout.cde().poly_collides(pi.shape(), &entities_to_ignore) {
if layout.cde().shape_collides(pi.shape(), &entities_to_ignore) {
println!("Collision detected for item {:.?}", pi.uid());
util::print_layout(layout);
return false;
Expand Down
2 changes: 1 addition & 1 deletion lbf/benches/edge_sensitivity_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn edge_sensitivity_bench(config: Config, mut g: BenchmarkGroup<WallTime>) {
true => true,
false => {
buffer_shape.transform_from(item.shape(), transf);
layout.cde().poly_collides(&buffer_shape, &[])
layout.cde().shape_collides(&buffer_shape, &[])
}
};
match collides {
Expand Down
2 changes: 1 addition & 1 deletion lbf/benches/fast_fail_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn fast_fail_query_bench(c: &mut Criterion) {
true => true,
false => {
buffer_shape.transform_from(item.shape(), transf);
layout.cde().poly_collides(&buffer_shape, &[])
layout.cde().shape_collides(&buffer_shape, &[])
}
};
match collides {
Expand Down
2 changes: 1 addition & 1 deletion lbf/benches/hpg_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn hpg_bench(c: &mut Criterion) {
let transf = sampler.sample(&mut rng);
if !layout.cde().surrogate_collides(surrogate, &transf, &[]) {
buffer_shape.transform_from(item.shape(), &transf);
if !layout.cde().poly_collides(&buffer_shape, &[]) {
if !layout.cde().shape_collides(&buffer_shape, &[]) {
n_valid_samples += 1;
}
}
Expand Down
16 changes: 11 additions & 5 deletions lbf/benches/quadtree_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ use lbf::samplers::uniform_rect_sampler::UniformAARectSampler;
use crate::util::{create_base_config, N_ITEMS_REMOVED, N_SAMPLES, SWIM_PATH};

criterion_main!(benches);
//criterion_group!(benches, quadtree_query_update_1000_1, quadtree_query_bench, quadtree_update_bench);
criterion_group!(benches,quadtree_query_bench);

criterion_group!(benches, quadtree_query_update_1000_1, quadtree_query_bench, quadtree_update_bench);

mod util;

Expand All @@ -40,6 +38,8 @@ fn quadtree_update_bench(c: &mut Criterion) {
//disable fail fast surrogates
config.cde_config.item_surrogate_config.n_ff_poles = 0;
config.cde_config.item_surrogate_config.n_ff_piers = 0;
//disable haz prox grid
config.cde_config.haz_prox = HazProxConfig::Enabled { n_cells: 1};

let mut group = c.benchmark_group("quadtree_update");
for depth in QT_DEPTHS {
Expand All @@ -62,6 +62,7 @@ fn quadtree_update_bench(c: &mut Criterion) {
//println!("Removing item with id: {}\n", pi_uid.item_id);
problem.remove_item(layout_index, pi_uid);
}
problem.flush_changes();

// Insert 5 items back into the layout
for pi_uid in selected_pi_uids.into_iter().flatten() {
Expand All @@ -87,6 +88,8 @@ fn quadtree_query_bench(c: &mut Criterion) {
//disable fail fast surrogates
config.cde_config.item_surrogate_config.n_ff_poles = 0;
config.cde_config.item_surrogate_config.n_ff_piers = 0;
//disable haz prox grid
config.cde_config.haz_prox = HazProxConfig::Enabled { n_cells: 1};

let mut group = c.benchmark_group("quadtree_query");
for depth in QT_DEPTHS {
Expand All @@ -113,7 +116,7 @@ fn quadtree_query_bench(c: &mut Criterion) {
let mut buffer_shape = item.shape().clone();
for transf in samples.iter() {
buffer_shape.transform_from(item.shape(), transf);
let collides = layout.cde().poly_collides(&buffer_shape, &[]);
let collides = layout.cde().shape_collides(&buffer_shape, &[]);
if collides {
n_invalid += 1;
} else {
Expand All @@ -134,6 +137,9 @@ fn quadtree_query_update_1000_1(c: &mut Criterion) {
//disable fail fast surrogates
config.cde_config.item_surrogate_config.n_ff_poles = 0;
config.cde_config.item_surrogate_config.n_ff_piers = 0;
//disable haz prox grid
config.cde_config.haz_prox = HazProxConfig::Enabled { n_cells: 1};


let mut group = c.benchmark_group("quadtree_query_update_1000_1");
for depth in QT_DEPTHS {
Expand Down Expand Up @@ -166,7 +172,7 @@ fn quadtree_query_update_1000_1(c: &mut Criterion) {
for _ in 0..1000 {
let transf = sampler.sample(&mut rng);
buffer_shape.transform_from(item.shape(), &transf.compose());
let collides = layout.cde().poly_collides(&buffer_shape, &[]);
let collides = layout.cde().shape_collides(&buffer_shape, &[]);
criterion::black_box(collides); // prevent the compiler from optimizing the loop away
}
}
Expand Down
4 changes: 2 additions & 2 deletions lbf/src/lbf_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn sample_layout(problem: &ProblemEnum, layout_index: LayoutIndex, item: &It
let worth_testing = best.as_ref()
.map_or(true, |(_, best_cost)| cost.cmp(best_cost) == Ordering::Less);

if worth_testing && !layout.cde().poly_collides(&buffer_shape, entities_to_ignore.as_ref()) {
if worth_testing && !layout.cde().shape_collides(&buffer_shape, entities_to_ignore.as_ref()) {
//sample is a valid option
let d_transf = transf.decompose();
let i_opt = PlacingOption { layout_index, item_id, transf, d_transf };
Expand Down Expand Up @@ -214,7 +214,7 @@ pub fn sample_layout(problem: &ProblemEnum, layout_index: LayoutIndex, item: &It
let worth_testing = best.as_ref()
.map_or(true, |(_, best_cost)| cost.cmp(best_cost) == Ordering::Less);

if worth_testing && !layout.cde().poly_collides(&buffer_shape, entities_to_ignore.as_ref()) {
if worth_testing && !layout.cde().shape_collides(&buffer_shape, entities_to_ignore.as_ref()) {
//sample is a valid option
let d_transf = transf.decompose();
ls_sampler.set_mean(&d_transf);
Expand Down

0 comments on commit 66d512a

Please sign in to comment.