ifc_lite_geometry/construction/
mod.rs

1// SPDX-License-Identifier: MPL-2.0
2//! Owned construction before product publication. This is an explicitly partial
3//! graph of raw representation items and ordered solid boolean/CSG dependencies.
4//! This is not a complete product plan or a replacement for canonical assembly.
5//!
6//! Coordinates are in file length units; constructor Position is applied by
7//! evaluation. Product/mapping transforms, RTC publication, appearance and openings
8//! remain outside this contract. Source/execution reports are captured separately
9//! from product diagnostics. Profiles may already be sampled.
10//! Triangle coordinates use the production f32 admission without RTC subtraction;
11//! a router's RTC offset is not applied to this raw construction batch. Survey-
12//! coordinate detail therefore needs frame-aware lowering before product use.
13//! The canonical element path remains responsible for final product geometry.
14
15use crate::{Error, Mesh, Result};
16
17pub use crate::router::ConstructionFamily;
18pub use reports::{ConstructionDiagnostic, ConstructionExecutionError, ConstructionReports};
19
20/// Why a requested item cannot be replayed by this construction batch.
21#[derive(Debug, Clone, PartialEq, Eq)]
22#[non_exhaustive]
23pub enum ConstructionRequirement {
24    /// A caller registration must not silently become a built-in constructor.
25    CustomProcessor,
26    /// The production family still needs owned lowering in this contract.
27    ConstructorNotLowered,
28    /// Existing source admission failed; the original diagnostic is retained.
29    SourceAdmission(String),
30    /// The operation exists, but this graph does not own its full policy yet.
31    OperationPolicyNotLowered(String),
32    /// A source operation refers back into its active dependency path.
33    CyclicDependency(u32),
34    /// The graph's explicit node/work bound was reached; no truncated success.
35    GraphBudgetExhausted,
36    /// The caller's router has explicitly refused the model's length unit.
37    LengthUnitRefused,
38}
39
40/// One source definition; multiple ordered roots may refer to this definition.
41#[derive(Debug, Clone)]
42pub struct ConstructionNode {
43    source_id: u32,
44    family: Option<ConstructionFamily>,
45    construction: std::result::Result<usize, ConstructionRequirement>,
46}
47
48#[derive(Debug, Clone)]
49enum Construction {
50    Builtin(crate::program::OwnedConstructor),
51    BrepOperand(brep_operand::BrepOperandPlan),
52}
53
54impl ConstructionNode {
55    pub fn source_id(&self) -> u32 { self.source_id }
56    pub fn family(&self) -> Option<ConstructionFamily> { self.family }
57    pub fn requirement(&self) -> Option<&ConstructionRequirement> {
58        self.construction.as_ref().err()
59    }
60}
61
62/// Source-scoped owned definitions and ordered occurrences, independent of IFC
63/// bytes, decoder and router after compilation. Definition reuse never removes
64/// a repeated occurrence. Unsupported roots remain explicit requirements.
65#[derive(Debug, Clone)]
66pub struct ConstructionBatch {
67    pub(super) nodes: Vec<ConstructionNode>,
68    pub(super) occurrences: Vec<usize>,
69    program: Vec<program::Instruction>,
70    program_sources: Vec<u32>,
71    reports: ConstructionReports,
72}
73
74/// Evaluated unique definitions plus the original ordered occurrence indices.
75/// Meshes retain raw file-unit construction coordinates, not world coordinates.
76pub struct ConstructionEvaluation {
77    pub definitions: Vec<Mesh>,
78    pub occurrences: Vec<usize>,
79    /// Unattributed diagnostics per unique requested definition, in root order.
80    pub diagnostics: Vec<Vec<crate::BoolFailure>>,
81    /// Durable source/execution reports, independent of caller thread-local state.
82    pub reports: ConstructionReports,
83    /// Fresh instruction failures in execution order; reused edges are not repeated.
84    pub observed_diagnostics: Vec<ConstructionDiagnostic>,
85}
86
87impl ConstructionBatch {
88    pub fn nodes(&self) -> &[ConstructionNode] { &self.nodes }
89    pub fn occurrences(&self) -> &[usize] { &self.occurrences }
90    pub fn reports(&self) -> &ConstructionReports { &self.reports }
91
92    /// Consume owned arrays through the same mesh builders as production.
93    /// Refuse the entire batch before evaluation if any root is unresolved;
94    /// there is no successful partial result that silently drops geometry.
95    pub fn evaluate(self) -> std::result::Result<ConstructionEvaluation, ConstructionExecutionError> {
96        if let Some((node, requirement)) = self.nodes.iter()
97            .find_map(|node| node.requirement().map(|requirement| (node, requirement))) {
98            return Err(ConstructionExecutionError {
99                root_source_id: Some(node.source_id), failed_source_id: Some(node.source_id),
100                error: Box::new(Error::geometry(format!("Construction item #{} requires {:?}", node.source_id, requirement))),
101                diagnostics: Vec::new(), reports: self.reports,
102            });
103        }
104        let (mut result, mut reports) = crate::processors::diagnostic_capture::capture(|| program::evaluate(self));
105        match &mut result {
106            Ok(evaluated) => {
107                reports.merge(std::mem::take(&mut evaluated.reports));
108                evaluated.reports = reports;
109            }
110            Err(error) => {
111                reports.merge(std::mem::take(&mut error.reports));
112                error.reports = reports;
113            }
114        }
115        result
116    }
117}
118
119mod lowering;
120mod leaf;
121mod program;
122mod source;
123mod reports;
124mod brep_operand;
125
126#[cfg(test)]
127mod tests;
128#[cfg(test)]
129mod graph_tests;
130#[cfg(test)]
131mod family_tests;
132
133impl Construction {
134    fn evaluate(self, recorder: &crate::processors::BooleanClippingProcessor) -> Result<(Mesh, bool)> {
135        match self {
136            Self::Builtin(plan) => plan.evaluate().map(|mesh| (mesh, false)),
137            Self::BrepOperand(plan) => plan.evaluate(recorder),
138        }
139    }
140}