#[allow(clippy::module_inception)]
#[cfg(test)]
mod tests {
use crate::core::BufferGeometry;
use crate::geometries::BoxGeometry;
use crate::{
evaluate_hierarchy, CsgBrush, CsgEvaluator, CsgNode, CsgOperation, CsgOperationGroup,
ADDITION, INTERSECTION, SUBTRACTION,
};
fn volume(g: &BufferGeometry) -> f64 {
let p = &g.get_attribute("position").unwrap().array;
p.as_chunks::<9>()
.0
.iter()
.map(|t| {
let (a, b, c) = (
[t[0] as f64, t[1] as f64, t[2] as f64],
[t[3] as f64, t[4] as f64, t[5] as f64],
[t[6] as f64, t[7] as f64, t[8] as f64],
);
let bxc = [
b[1] * c[2] - b[2] * c[1],
b[2] * c[0] - b[0] * c[2],
b[0] * c[1] - b[1] * c[0],
];
(a[0] * bxc[0] + a[1] * bxc[1] + a[2] * bxc[2]) / 6.0
})
.sum()
}
#[test]
fn subtracting_a_nested_box_leaves_its_faces_as_a_cavity() {
let mut evaluator = CsgEvaluator::new();
evaluator.use_groups = false;
let mut outer = CsgBrush::new(BoxGeometry::new(4.0, 2.5, 2.5));
let mut inner = CsgBrush::new(BoxGeometry::new(3.6, 2.1, 2.1));
let result = evaluator.evaluate(&mut outer, &mut inner, SUBTRACTION);
assert_eq!(result.get_attribute("position").unwrap().count(), 72);
let want = 4.0 * 2.5 * 2.5 - 3.6 * 2.1 * 2.1;
assert!(
(volume(&result) - want).abs() < 1e-4,
"volume {}",
volume(&result)
);
}
#[test]
fn the_result_keeps_attributes_and_source_groups() {
let mut evaluator = CsgEvaluator::new();
let mut a = CsgBrush::new(BoxGeometry::new(2.0, 2.0, 2.0));
let mut b = CsgBrush::new(BoxGeometry::new(1.0, 1.0, 1.0));
b.set_position(1.0, 0.0, 0.0);
b.update_matrix_world(None);
let result = evaluator.evaluate(&mut a, &mut b, ADDITION);
for name in ["position", "uv", "normal"] {
assert!(result.get_attribute(name).is_some(), "no {name}");
}
let n = result.get_attribute("position").unwrap().count() as u32;
assert_eq!(
result.groups.iter().map(|g| g.count).sum::<u32>(),
n,
"groups cover every vertex"
);
assert!(
result.groups.iter().any(|g| g.material_index < 6),
"a's groups"
);
assert!(
result.groups.iter().any(|g| g.material_index >= 6),
"b's groups"
);
}
#[test]
fn several_operations_in_one_pass_partition_the_solid() {
let brushes = || {
let a = CsgBrush::new(BoxGeometry::new(2.0, 2.0, 2.0));
let mut b = CsgBrush::new(BoxGeometry::new(1.5, 1.5, 1.5));
b.set_position(0.9, 0.4, 0.3);
b.update_matrix_world(None);
(a, b)
};
let mut evaluator = CsgEvaluator::new();
let (mut a, mut b) = brushes();
let both = evaluator.evaluate_ops(&mut a, &mut b, &[SUBTRACTION, INTERSECTION]);
let (mut a, mut b) = brushes();
let alone = evaluator.evaluate(&mut a, &mut b, SUBTRACTION);
assert_eq!(
both[0].get_attribute("position").unwrap().array,
alone.get_attribute("position").unwrap().array
);
let total = volume(&both[0]) + volume(&both[1]);
assert!(
(total - 8.0).abs() < 1e-4,
"A − B and A ∩ B enclose {total}, not A's 8"
);
}
#[test]
fn a_group_transform_composes_into_its_children() {
let evaluate = |grouped: bool| {
let mut root = CsgOperation::new(BoxGeometry::new(2.0, 2.0, 2.0), ADDITION);
let mut cut = CsgOperation::new(BoxGeometry::new(1.0, 1.0, 1.0), SUBTRACTION);
if grouped {
let mut group = CsgOperationGroup::new();
group.set_position(1.0, 0.0, 0.0);
group.add_child(CsgNode::Operation(cut));
root.add_child(CsgNode::Group(group));
} else {
cut.set_position(1.0, 0.0, 0.0);
root.add_child(CsgNode::Operation(cut));
}
evaluate_hierarchy(&mut root, &mut CsgEvaluator::new())
};
let (grouped, direct) = (evaluate(true), evaluate(false));
assert!(
(volume(&grouped) - (8.0 - 0.5)).abs() < 1e-4,
"volume {}",
volume(&grouped)
);
assert_eq!(
grouped.get_attribute("position").unwrap().array,
direct.get_attribute("position").unwrap().array
);
}
}