ifc_lite_imported_geometry/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Numeric ownership for already-composed imported USD tessellation.
6//! No STEP identifiers, source parser, renderer or host callbacks are required.
7pub mod scene;
8pub mod graph;
9mod graph_packet;
10mod math;
11mod packet;
12pub use math::{compute_normal_matrix, compute_normals, stage_transform, stage_values};
13pub use packet::process_packet;
14
15use std::sync::Arc;
16
17pub type RawMatrix = Vec<Vec<f64>>;
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
19pub struct ImportIdentity {
20    pub store: u64,
21    pub revision: u64,
22    pub definition: u64,
23    pub occurrence: u64,
24}
25#[derive(Clone, Debug)]
26pub struct RawMesh {
27    pub points: Vec<Vec<f64>>,
28    pub face_vertex_indices: Vec<f64>,
29    pub face_vertex_counts: Option<Vec<f64>>,
30    pub normals: Option<Vec<Vec<f64>>>,
31}
32#[derive(Clone, Debug)]
33pub enum ImportedSource { Mesh(RawMesh), Points(Vec<f64>) }
34#[derive(Clone, Debug)]
35pub struct ImportedDefinition {
36    pub identity: ImportIdentity,
37    pub source: ImportedSource,
38    pub transform_stack: Vec<RawMatrix>,
39}
40#[derive(Clone, Debug)]
41pub struct ReadyOperation { definition: Arc<ImportedDefinition>, seal: Arc<()> }
42impl ReadyOperation {
43    pub fn definition(&self) -> &ImportedDefinition { &self.definition }
44}
45#[derive(Clone, Debug)]
46pub struct MeshOutput { pub positions: Vec<f32>, pub normals: Vec<f32>, pub indices: Vec<u32> }
47#[derive(Clone, Debug)]
48pub struct PointsOutput { pub positions: Vec<f32>, pub min: [f64; 3], pub max: [f64; 3] }
49#[derive(Clone, Debug)]
50pub enum ImportedOutput { Mesh(MeshOutput), Points(PointsOutput) }
51#[derive(Clone, Debug)]
52pub struct ExecutedOperation { identity: ImportIdentity, output: ImportedOutput, seal: Arc<()> }
53impl ExecutedOperation {
54    pub fn identity(&self) -> ImportIdentity { self.identity }
55    pub fn output(&self) -> &ImportedOutput { &self.output }
56}
57#[derive(Clone, Debug)]
58pub struct ImportedAssembly { pub definition: ImportedDefinition, pub output: ImportedOutput }
59#[derive(Clone, Debug, PartialEq, Eq)]
60pub enum ImportError { HomogeneousMesh, HomogeneousPoints, IdentityMismatch, InvalidPacket }
61impl std::fmt::Display for ImportError {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        f.write_str(match self {
64            Self::HomogeneousMesh => "IFCx geometry: usd::xformop produces non-finite homogeneous w; matrix is malformed or singular",
65            Self::HomogeneousPoints => "IFCx pointcloud: usd::xformop produces non-finite homogeneous w; matrix is malformed or singular",
66            Self::IdentityMismatch => "Imported geometry: stale operation identity",
67            Self::InvalidPacket => "Imported geometry: invalid binary packet",
68        })
69    }
70}
71impl std::error::Error for ImportError {}
72pub fn plan(definition: ImportedDefinition) -> ReadyOperation { ReadyOperation { definition: Arc::new(definition), seal: Arc::new(()) } }
73pub fn execute(op: &ReadyOperation) -> Result<ExecutedOperation, ImportError> {
74    let d = &op.definition;
75    let output = match &d.source {
76        ImportedSource::Mesh(mesh) => ImportedOutput::Mesh(math::mesh(mesh, &d.transform_stack)?),
77        ImportedSource::Points(points) => ImportedOutput::Points(math::points(points, &d.transform_stack)?),
78    };
79    Ok(ExecutedOperation { identity: d.identity, output, seal: Arc::clone(&op.seal) })
80}
81pub fn assemble(op: ReadyOperation, executed: ExecutedOperation) -> Result<ImportedAssembly, ImportError> {
82    if !Arc::ptr_eq(&op.seal, &executed.seal) || op.definition.identity != executed.identity { return Err(ImportError::IdentityMismatch); }
83    let definition = Arc::try_unwrap(op.definition).unwrap_or_else(|shared| (*shared).clone());
84    Ok(ImportedAssembly { definition, output: executed.output })
85}
86
87#[cfg(test)]
88mod tests;