use super::boolean::OperandPath;
use crate::extrusion::apply_transform;
use crate::{BoolFailure, Error, Mesh, Result, TessellationQuality, Vector3};
use ifc_lite_core::{DecodedEntity, EntityDecoder, IfcSchema, IfcType};
use nalgebra::Point3;
use std::cell::RefCell;
use super::boolean::BooleanClippingProcessor;
use super::sphere::SphereProcessor;
use super::helpers::parse_axis2_placement_3d;
use crate::router::GeometryProcessor;
pub struct BlockProcessor;
impl BlockProcessor {
pub fn new() -> Self {
Self
}
}
impl Default for BlockProcessor {
fn default() -> Self {
Self::new()
}
}
impl GeometryProcessor for BlockProcessor {
fn process(
&self,
entity: &DecodedEntity,
decoder: &mut EntityDecoder,
_schema: &IfcSchema,
_quality: TessellationQuality,
) -> Result<Mesh> {
let x = entity
.get_float(1)
.ok_or_else(|| Error::geometry("IfcBlock missing XLength".to_string()))?;
let y = entity
.get_float(2)
.ok_or_else(|| Error::geometry("IfcBlock missing YLength".to_string()))?;
let z = entity
.get_float(3)
.ok_or_else(|| Error::geometry("IfcBlock missing ZLength".to_string()))?;
if !(x.is_finite() && y.is_finite() && z.is_finite() && x > 0.0 && y > 0.0 && z > 0.0) {
return Err(Error::geometry(format!(
"IfcBlock requires finite positive lengths, got ({}, {}, {})",
x, y, z
)));
}
let mut mesh = build_axis_aligned_box(x, y, z);
if let Some(pos_attr) = entity.get(0) {
if !pos_attr.is_null() {
if let Some(pos_entity) = decoder.resolve_ref(pos_attr)? {
if pos_entity.ifc_type == IfcType::IfcAxis2Placement3D {
let transform = parse_axis2_placement_3d(&pos_entity, decoder)?;
apply_transform(&mut mesh, &transform);
}
}
}
}
Ok(mesh)
}
fn supported_types(&self) -> Vec<IfcType> {
vec![IfcType::IfcBlock]
}
}
pub struct CsgSolidProcessor {
skip_small_cuts: bool,
failures: RefCell<Vec<BoolFailure>>,
}
impl CsgSolidProcessor {
pub fn new() -> Self {
Self::with_skip_small_cuts(false)
}
pub fn with_skip_small_cuts(skip_small_cuts: bool) -> Self {
Self {
skip_small_cuts,
failures: RefCell::new(Vec::new()),
}
}
pub fn take_failures(&self) -> Vec<BoolFailure> {
std::mem::take(&mut *self.failures.borrow_mut())
}
}
impl Default for CsgSolidProcessor {
fn default() -> Self {
Self::new()
}
}
impl CsgSolidProcessor {
pub(crate) fn process_with_boolean_cycle_guard(
&self,
entity: &DecodedEntity,
decoder: &mut EntityDecoder,
schema: &IfcSchema,
depth: u32,
quality: TessellationQuality,
visited: &mut OperandPath,
) -> Result<Mesh> {
if !visited.insert(entity.id) {
return Err(Error::geometry(format!(
"Cyclic boolean/CSG operand reference at #{}",
entity.id
)));
}
let out = self.resolve_tree_root(entity, decoder, schema, depth, quality, visited);
visited.remove(&entity.id);
out
}
fn resolve_tree_root(
&self,
entity: &DecodedEntity,
decoder: &mut EntityDecoder,
schema: &IfcSchema,
depth: u32,
quality: TessellationQuality,
visited: &mut OperandPath,
) -> Result<Mesh> {
let root_attr = entity.get(0).ok_or_else(|| {
Error::geometry("IfcCsgSolid missing TreeRootExpression".to_string())
})?;
let root = decoder.resolve_ref(root_attr)?.ok_or_else(|| {
Error::geometry("IfcCsgSolid TreeRootExpression unresolved".to_string())
})?;
match root.ifc_type {
IfcType::IfcBooleanResult | IfcType::IfcBooleanClippingResult => {
let boolean =
BooleanClippingProcessor::with_skip_small_cuts(self.skip_small_cuts);
let out =
boolean.process_with_depth(&root, decoder, schema, depth, quality, visited);
self.failures.borrow_mut().extend(boolean.take_failures());
out
}
IfcType::IfcBlock => BlockProcessor::new().process(&root, decoder, schema, quality),
IfcType::IfcSphere => SphereProcessor::new().process(&root, decoder, schema, quality),
IfcType::IfcCsgSolid => Err(Error::geometry(
"IfcCsgSolid TreeRootExpression must be IfcBooleanResult or \
IfcCsgPrimitive3D, not another IfcCsgSolid (spec violation)"
.to_string(),
)),
other => Err(Error::geometry(format!(
"Unsupported IfcCsgSolid TreeRootExpression: {}",
other
))),
}
}
}
impl GeometryProcessor for CsgSolidProcessor {
fn process(
&self,
entity: &DecodedEntity,
decoder: &mut EntityDecoder,
schema: &IfcSchema,
quality: TessellationQuality,
) -> Result<Mesh> {
let mut visited = OperandPath::default();
self.resolve_tree_root(entity, decoder, schema, 0, quality, &mut visited)
}
fn supported_types(&self) -> Vec<IfcType> {
vec![IfcType::IfcCsgSolid]
}
fn take_bool_failures(&self) -> Vec<BoolFailure> {
self.take_failures()
}
}
fn build_axis_aligned_box(x: f64, y: f64, z: f64) -> Mesh {
let mut mesh = Mesh::with_capacity(24, 36);
let faces: [([Point3<f64>; 4], Vector3<f64>); 6] = [
(
[
Point3::new(0.0, 0.0, 0.0),
Point3::new(0.0, y, 0.0),
Point3::new(x, y, 0.0),
Point3::new(x, 0.0, 0.0),
],
Vector3::new(0.0, 0.0, -1.0),
),
(
[
Point3::new(0.0, 0.0, z),
Point3::new(x, 0.0, z),
Point3::new(x, y, z),
Point3::new(0.0, y, z),
],
Vector3::new(0.0, 0.0, 1.0),
),
(
[
Point3::new(0.0, 0.0, 0.0),
Point3::new(x, 0.0, 0.0),
Point3::new(x, 0.0, z),
Point3::new(0.0, 0.0, z),
],
Vector3::new(0.0, -1.0, 0.0),
),
(
[
Point3::new(x, y, 0.0),
Point3::new(0.0, y, 0.0),
Point3::new(0.0, y, z),
Point3::new(x, y, z),
],
Vector3::new(0.0, 1.0, 0.0),
),
(
[
Point3::new(0.0, y, 0.0),
Point3::new(0.0, 0.0, 0.0),
Point3::new(0.0, 0.0, z),
Point3::new(0.0, y, z),
],
Vector3::new(-1.0, 0.0, 0.0),
),
(
[
Point3::new(x, 0.0, 0.0),
Point3::new(x, y, 0.0),
Point3::new(x, y, z),
Point3::new(x, 0.0, z),
],
Vector3::new(1.0, 0.0, 0.0),
),
];
for (corners, normal) in faces {
let base = (mesh.positions.len() / 3) as u32;
for p in &corners {
mesh.add_vertex(*p, normal);
}
mesh.add_triangle(base, base + 1, base + 2);
mesh.add_triangle(base, base + 2, base + 3);
}
mesh
}
#[cfg(test)]
#[path = "csg_primitive_tests.rs"]
mod tests;