Add CSG demand frontier counters
This commit is contained in:
parent
587408c6fa
commit
84371de2a2
5 changed files with 276 additions and 19 deletions
|
|
@ -18,6 +18,9 @@ pub enum BuildWarning {
|
|||
pub struct BuildReport {
|
||||
pub input_brushes: usize,
|
||||
pub emitted_convex_fragments: usize,
|
||||
pub operator_brushes: usize,
|
||||
pub candidate_pairs: usize,
|
||||
pub rejected_pairs: usize,
|
||||
pub warnings: Vec<BuildWarning>,
|
||||
}
|
||||
|
||||
|
|
@ -256,38 +259,49 @@ impl Assembler {
|
|||
),
|
||||
},
|
||||
BrushOp::Subtract => {
|
||||
report.operator_brushes += 1;
|
||||
if !brush
|
||||
.bounds
|
||||
.intersects_any(solids.iter().map(|solid| solid.bounds))
|
||||
{
|
||||
report.rejected_pairs += solids.len();
|
||||
continue;
|
||||
}
|
||||
if let Some(cutter) = brush.convex_cutter() {
|
||||
solids = subtract_from_solids(solids, &cutter);
|
||||
solids = subtract_from_solids(solids, &cutter, &mut report);
|
||||
} else {
|
||||
report
|
||||
.warnings
|
||||
.push(BuildWarning::SubtractIgnoredForNonBox {
|
||||
brush: brush.name.clone(),
|
||||
});
|
||||
let has_candidate = record_solid_pairs(&solids, brush.bounds, &mut report);
|
||||
if has_candidate {
|
||||
report
|
||||
.warnings
|
||||
.push(BuildWarning::SubtractIgnoredForNonBox {
|
||||
brush: brush.name.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
BrushOp::Intersect => {
|
||||
report.operator_brushes += 1;
|
||||
if !brush
|
||||
.bounds
|
||||
.intersects_any(solids.iter().map(|solid| solid.bounds))
|
||||
{
|
||||
report.rejected_pairs += solids.len();
|
||||
solids.clear();
|
||||
continue;
|
||||
}
|
||||
if let Some(cutter) = brush.convex_cutter() {
|
||||
solids = intersect_solids(solids, &cutter);
|
||||
solids = intersect_solids(solids, &cutter, &mut report);
|
||||
} else {
|
||||
report
|
||||
.warnings
|
||||
.push(BuildWarning::IntersectIgnoredForNonBox {
|
||||
brush: brush.name.clone(),
|
||||
});
|
||||
let has_candidate = record_solid_pairs(&solids, brush.bounds, &mut report);
|
||||
solids.clear();
|
||||
if has_candidate {
|
||||
report
|
||||
.warnings
|
||||
.push(BuildWarning::IntersectIgnoredForNonBox {
|
||||
brush: brush.name.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -319,12 +333,15 @@ impl Assembler {
|
|||
match brush.op {
|
||||
BrushOp::Add => boxes.push((bounds, brush.material)),
|
||||
BrushOp::Subtract => {
|
||||
report.operator_brushes += 1;
|
||||
if !bounds.intersects_any(boxes.iter().map(|(bounds, _)| *bounds)) {
|
||||
report.rejected_pairs += boxes.len();
|
||||
continue;
|
||||
}
|
||||
let mut out = Vec::with_capacity(boxes.len() + 4);
|
||||
for (solid, material) in boxes {
|
||||
if solid.intersects(bounds) {
|
||||
report.candidate_pairs += 1;
|
||||
out.extend(
|
||||
solid
|
||||
.subtract_box(bounds)
|
||||
|
|
@ -332,20 +349,29 @@ impl Assembler {
|
|||
.map(|piece| (piece, material)),
|
||||
);
|
||||
} else {
|
||||
report.rejected_pairs += 1;
|
||||
out.push((solid, material));
|
||||
}
|
||||
}
|
||||
boxes = out;
|
||||
}
|
||||
BrushOp::Intersect => {
|
||||
report.operator_brushes += 1;
|
||||
if !bounds.intersects_any(boxes.iter().map(|(bounds, _)| *bounds)) {
|
||||
report.rejected_pairs += boxes.len();
|
||||
boxes.clear();
|
||||
continue;
|
||||
}
|
||||
boxes = boxes
|
||||
.into_iter()
|
||||
.filter_map(|(solid, material)| {
|
||||
solid.intersection(bounds).map(|hit| (hit, material))
|
||||
if let Some(hit) = solid.intersection(bounds) {
|
||||
report.candidate_pairs += 1;
|
||||
Some((hit, material))
|
||||
} else {
|
||||
report.rejected_pairs += 1;
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
|
@ -463,24 +489,36 @@ impl CompiledBrush {
|
|||
}
|
||||
}
|
||||
|
||||
fn subtract_from_solids(solids: Vec<ConvexSolid>, cutter: &ConvexSolid) -> Vec<ConvexSolid> {
|
||||
fn subtract_from_solids(
|
||||
solids: Vec<ConvexSolid>,
|
||||
cutter: &ConvexSolid,
|
||||
report: &mut BuildReport,
|
||||
) -> Vec<ConvexSolid> {
|
||||
let mut out = Vec::with_capacity(solids.len() + 4);
|
||||
for solid in solids {
|
||||
if solid.bounds.intersects(cutter.bounds) {
|
||||
report.candidate_pairs += 1;
|
||||
out.extend(solid.subtract_convex_owned(cutter));
|
||||
} else {
|
||||
report.rejected_pairs += 1;
|
||||
out.push(solid);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn intersect_solids(solids: Vec<ConvexSolid>, cutter: &ConvexSolid) -> Vec<ConvexSolid> {
|
||||
fn intersect_solids(
|
||||
solids: Vec<ConvexSolid>,
|
||||
cutter: &ConvexSolid,
|
||||
report: &mut BuildReport,
|
||||
) -> Vec<ConvexSolid> {
|
||||
let mut out = Vec::with_capacity(solids.len());
|
||||
for solid in solids {
|
||||
if !solid.bounds.intersects(cutter.bounds) {
|
||||
report.rejected_pairs += 1;
|
||||
continue;
|
||||
}
|
||||
report.candidate_pairs += 1;
|
||||
if let Some(fragment) = solid.intersect_convex_owned(cutter) {
|
||||
out.push(fragment);
|
||||
}
|
||||
|
|
@ -488,6 +526,19 @@ fn intersect_solids(solids: Vec<ConvexSolid>, cutter: &ConvexSolid) -> Vec<Conve
|
|||
out
|
||||
}
|
||||
|
||||
fn record_solid_pairs(solids: &[ConvexSolid], bounds: Aabb, report: &mut BuildReport) -> bool {
|
||||
let mut has_candidate = false;
|
||||
for solid in solids {
|
||||
if solid.bounds.intersects(bounds) {
|
||||
report.candidate_pairs += 1;
|
||||
has_candidate = true;
|
||||
} else {
|
||||
report.rejected_pairs += 1;
|
||||
}
|
||||
}
|
||||
has_candidate
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use bevy_math::Vec3;
|
||||
|
|
@ -567,6 +618,9 @@ mod tests {
|
|||
|
||||
let output = asm.build();
|
||||
assert_eq!(output.report.emitted_convex_fragments, 1);
|
||||
assert_eq!(output.report.operator_brushes, 1);
|
||||
assert_eq!(output.report.candidate_pairs, 0);
|
||||
assert_eq!(output.report.rejected_pairs, 1);
|
||||
assert_eq!(output.mesh.triangle_count(), 12);
|
||||
}
|
||||
|
||||
|
|
@ -589,6 +643,9 @@ mod tests {
|
|||
|
||||
let output = asm.build();
|
||||
assert_eq!(output.report.emitted_convex_fragments, 1);
|
||||
assert_eq!(output.report.operator_brushes, 1);
|
||||
assert_eq!(output.report.candidate_pairs, 1);
|
||||
assert_eq!(output.report.rejected_pairs, 0);
|
||||
assert_eq!(output.report.warnings.len(), 0);
|
||||
assert_eq!(output.mesh.triangle_count(), 12);
|
||||
}
|
||||
|
|
@ -616,7 +673,16 @@ mod tests {
|
|||
let cached = asm.build();
|
||||
let dirty = asm.rebuild();
|
||||
|
||||
assert_eq!(stable.report.emitted_convex_fragments, dirty.report.emitted_convex_fragments);
|
||||
assert_eq!(
|
||||
stable.report.emitted_convex_fragments,
|
||||
dirty.report.emitted_convex_fragments
|
||||
);
|
||||
assert_eq!(
|
||||
stable.report.operator_brushes,
|
||||
dirty.report.operator_brushes
|
||||
);
|
||||
assert_eq!(stable.report.candidate_pairs, dirty.report.candidate_pairs);
|
||||
assert_eq!(stable.report.rejected_pairs, dirty.report.rejected_pairs);
|
||||
assert_eq!(stable.report.warnings, dirty.report.warnings);
|
||||
assert_eq!(stable.mesh.triangle_count(), dirty.mesh.triangle_count());
|
||||
assert_eq!(stable.mesh.vertex_count(), dirty.mesh.vertex_count());
|
||||
|
|
|
|||
167
src/frontier.rs
Normal file
167
src/frontier.rs
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
use crate::{Brush, BrushOp};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct DemandPair {
|
||||
pub source_index: usize,
|
||||
pub operator_index: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct DemandFrontier {
|
||||
pub pairs: Vec<DemandPair>,
|
||||
pub rejected_pairs: usize,
|
||||
pub operator_brushes: usize,
|
||||
}
|
||||
|
||||
impl DemandFrontier {
|
||||
pub fn from_ordered_brushes(brushes: &[Brush]) -> Self {
|
||||
let mut frontier = Self::default();
|
||||
let mut active_sources = Vec::<usize>::new();
|
||||
|
||||
for (operator_index, brush) in brushes.iter().enumerate() {
|
||||
match brush.op {
|
||||
BrushOp::Add => active_sources.push(operator_index),
|
||||
BrushOp::Subtract => {
|
||||
frontier.operator_brushes += 1;
|
||||
frontier.collect_pairs(brushes, &active_sources, operator_index);
|
||||
}
|
||||
BrushOp::Intersect => {
|
||||
frontier.operator_brushes += 1;
|
||||
let retained = frontier.collect_pairs(brushes, &active_sources, operator_index);
|
||||
active_sources = retained;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
frontier
|
||||
}
|
||||
|
||||
pub fn candidate_pairs(&self) -> usize {
|
||||
self.pairs.len()
|
||||
}
|
||||
|
||||
fn collect_pairs(
|
||||
&mut self,
|
||||
brushes: &[Brush],
|
||||
active_sources: &[usize],
|
||||
operator_index: usize,
|
||||
) -> Vec<usize> {
|
||||
let operator_bounds = brushes[operator_index].bounds();
|
||||
let mut retained = Vec::new();
|
||||
|
||||
for &source_index in active_sources {
|
||||
if brushes[source_index].bounds().intersects(operator_bounds) {
|
||||
self.pairs.push(DemandPair {
|
||||
source_index,
|
||||
operator_index,
|
||||
});
|
||||
retained.push(source_index);
|
||||
} else {
|
||||
self.rejected_pairs += 1;
|
||||
}
|
||||
}
|
||||
|
||||
retained
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use bevy_math::{Quat, Vec3};
|
||||
|
||||
use super::*;
|
||||
use crate::{Aabb, Brush, MaterialId, Primitive};
|
||||
|
||||
fn brush(name: &str, op: BrushOp, bounds: Aabb) -> Brush {
|
||||
Brush::new(
|
||||
crate::BrushId(0),
|
||||
name,
|
||||
op,
|
||||
Primitive::Box { bounds },
|
||||
MaterialId(1),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frontier_keeps_only_touching_ordered_pairs() {
|
||||
let brushes = vec![
|
||||
brush(
|
||||
"source",
|
||||
BrushOp::Add,
|
||||
Aabb::from_center_size(Vec3::ZERO, Vec3::splat(4.0)),
|
||||
),
|
||||
brush(
|
||||
"far cutter",
|
||||
BrushOp::Subtract,
|
||||
Aabb::from_center_size(Vec3::splat(20.0), Vec3::ONE),
|
||||
),
|
||||
brush(
|
||||
"near cutter",
|
||||
BrushOp::Subtract,
|
||||
Aabb::from_center_size(Vec3::ZERO, Vec3::ONE),
|
||||
),
|
||||
];
|
||||
|
||||
let frontier = DemandFrontier::from_ordered_brushes(&brushes);
|
||||
|
||||
assert_eq!(frontier.operator_brushes, 2);
|
||||
assert_eq!(frontier.rejected_pairs, 1);
|
||||
assert_eq!(
|
||||
frontier.pairs,
|
||||
vec![DemandPair {
|
||||
source_index: 0,
|
||||
operator_index: 2,
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intersection_frontier_reduces_active_sources() {
|
||||
let brushes = vec![
|
||||
brush(
|
||||
"a",
|
||||
BrushOp::Add,
|
||||
Aabb::from_center_size(Vec3::ZERO, Vec3::splat(2.0)),
|
||||
),
|
||||
brush(
|
||||
"b",
|
||||
BrushOp::Add,
|
||||
Aabb::from_center_size(Vec3::new(20.0, 0.0, 0.0), Vec3::splat(2.0)),
|
||||
),
|
||||
Brush::new(
|
||||
crate::BrushId(2),
|
||||
"common",
|
||||
BrushOp::Intersect,
|
||||
Primitive::OrientedBox {
|
||||
center: Vec3::ZERO,
|
||||
size: Vec3::splat(3.0),
|
||||
rotation: Quat::IDENTITY,
|
||||
},
|
||||
MaterialId(1),
|
||||
),
|
||||
brush(
|
||||
"cut",
|
||||
BrushOp::Subtract,
|
||||
Aabb::from_center_size(Vec3::ZERO, Vec3::ONE),
|
||||
),
|
||||
];
|
||||
|
||||
let frontier = DemandFrontier::from_ordered_brushes(&brushes);
|
||||
|
||||
assert_eq!(frontier.operator_brushes, 2);
|
||||
assert_eq!(frontier.rejected_pairs, 1);
|
||||
assert_eq!(
|
||||
frontier.pairs,
|
||||
vec![
|
||||
DemandPair {
|
||||
source_index: 0,
|
||||
operator_index: 2,
|
||||
},
|
||||
DemandPair {
|
||||
source_index: 0,
|
||||
operator_index: 3,
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ mod assembler;
|
|||
mod brush;
|
||||
mod convex;
|
||||
mod dsl;
|
||||
mod frontier;
|
||||
mod mesh;
|
||||
mod primitives;
|
||||
mod tree;
|
||||
|
|
@ -18,6 +19,7 @@ pub use assembler::{Assembler, BuildOutput, BuildReport, BuildWarning};
|
|||
pub use brush::{Aabb, Brush, BrushId, BrushOp, MaterialId, PolygonCategory, Primitive};
|
||||
pub use convex::{CategorizedPolygons, ConvexPolygon, ConvexSolid, Plane};
|
||||
pub use dsl::LevelDsl;
|
||||
pub use frontier::{DemandFrontier, DemandPair};
|
||||
pub use mesh::TriangleMesh;
|
||||
pub use primitives::{
|
||||
DomeCapZSpec, FloretArmSpec, append_cylinder_z, append_dome_cap_z, append_floret_arm,
|
||||
|
|
|
|||
Loading…
Reference in a new issue