ifc_lite_geometry/document/
mod.rs1mod retained;
26#[cfg(any(all(test, any(not(target_arch = "wasm32"), feature = "wasm_source_capture")), feature = "retained-runtime"))]
28#[cfg_attr(not(test), allow(dead_code))]
29pub(crate) mod retained_frames;
30#[cfg(any(all(test, any(not(target_arch = "wasm32"), feature = "wasm_source_capture")), feature = "retained-runtime"))]
32#[cfg_attr(not(test), allow(dead_code))]
33pub(crate) mod retained_affine;
34mod retained_disk;
35mod retained_display;
36mod retained_sweep;
37#[cfg(any(all(test, any(not(target_arch = "wasm32"), feature = "wasm_source_capture")), feature = "retained-runtime"))]
38mod retained_prism;
39#[cfg(any(all(test, any(not(target_arch = "wasm32"), feature = "wasm_source_capture")), feature = "retained-runtime"))]
40pub use retained_prism::{RetainedPrism, PrismRole, PrismLoop, PrismSegment};
41#[cfg(any(all(test, any(not(target_arch = "wasm32"), feature = "wasm_source_capture")), feature = "retained-runtime"))]
42mod retained_face_set;
43#[cfg(any(all(test, any(not(target_arch = "wasm32"), feature = "wasm_source_capture")), feature = "retained-runtime"))]
44pub use retained_face_set::RetainedFaceSet;
45#[cfg(any(all(test, any(not(target_arch = "wasm32"), feature = "wasm_source_capture")), feature = "retained-runtime"))]
46pub use crate::program::bounds::EvaluationBounds;
47mod types;
48mod validation;
49
50pub use retained::{
51 RetainedCompletion, RetainedDefinitionHandle, RetainedDisplayContract, RetainedExtrusion,
52 RetainedRecipe, RetainedRefusal, RetainedRequest, RetainedTessellation,
53};
54pub use retained_disk::RetainedSweptDisk;
55pub use retained_sweep::{RegionQualification, SweepPayload};
56pub use types::{DefinitionHandle, DocumentError, DocumentId, Occurrence, OccurrenceHandle};
57
58use crate::Mesh;
59use std::sync::atomic::{AtomicU64, Ordering};
60use std::sync::Arc;
61
62static NEXT_DOCUMENT: AtomicU64 = AtomicU64::new(1);
63
64#[derive(Debug)]
65struct Definition {
66 source_entity_id: u32,
67 mesh: Mesh,
68}
69
70#[derive(Debug)]
74pub struct GeometryDocument {
75 id: DocumentId,
76 revision: u64,
77 definitions: Vec<Definition>,
78 occurrences: Vec<Occurrence>,
79 retained: Vec<Arc<retained::RetainedEntry>>,
80}
81
82impl Default for GeometryDocument {
83 fn default() -> Self {
84 Self::new()
85 }
86}
87
88impl GeometryDocument {
89 pub fn new() -> Self {
92 Self {
93 id: DocumentId(NEXT_DOCUMENT.fetch_add(1, Ordering::Relaxed)),
94 revision: 0,
95 definitions: Vec::new(),
96 occurrences: Vec::new(),
97 retained: Vec::new(),
98 }
99 }
100
101 pub fn id(&self) -> DocumentId {
102 self.id
103 }
104
105 pub fn revision(&self) -> u64 {
107 self.revision
108 }
109
110 pub fn add_mesh_definition(
120 &mut self,
121 source_entity_id: u32,
122 mesh: Mesh,
123 ) -> Result<DefinitionHandle, DocumentError> {
124 validation::validate_mesh(&mesh)?;
125 let revision = self.next_revision()?;
126 let handle = DefinitionHandle {
127 document: self.id,
128 slot: self.definitions.len(),
129 };
130 self.definitions.push(Definition {
131 source_entity_id,
132 mesh,
133 });
134 self.revision = revision;
135 Ok(handle)
136 }
137
138 pub fn mesh(&self, handle: DefinitionHandle) -> Result<&Mesh, DocumentError> {
140 Ok(&self.definition(handle)?.mesh)
141 }
142
143 pub fn source_entity_id(&self, handle: DefinitionHandle) -> Result<u32, DocumentError> {
144 Ok(self.definition(handle)?.source_entity_id)
145 }
146
147 pub fn add_occurrence(
153 &mut self,
154 definition: DefinitionHandle,
155 source_entity_id: u32,
156 local_to_model: [f64; 16],
157 ) -> Result<OccurrenceHandle, DocumentError> {
158 self.definition(definition)?;
159 validation::validate_transform(&local_to_model)?;
160 let revision = self.next_revision()?;
161 let handle = OccurrenceHandle {
162 document: self.id,
163 slot: self.occurrences.len(),
164 };
165 self.occurrences.push(Occurrence {
166 source_entity_id,
167 definition,
168 local_to_model,
169 });
170 self.revision = revision;
171 Ok(handle)
172 }
173
174 pub fn occurrence(&self, handle: OccurrenceHandle) -> Result<&Occurrence, DocumentError> {
175 self.check_document(handle.document)?;
176 self.occurrences
178 .get(handle.slot)
179 .ok_or(DocumentError::WrongDocument)
180 }
181
182 fn definition(&self, handle: DefinitionHandle) -> Result<&Definition, DocumentError> {
183 self.check_document(handle.document)?;
184 self.definitions
185 .get(handle.slot)
186 .ok_or(DocumentError::WrongDocument)
187 }
188
189 fn check_document(&self, id: DocumentId) -> Result<(), DocumentError> {
190 if id == self.id {
191 Ok(())
192 } else {
193 Err(DocumentError::WrongDocument)
194 }
195 }
196
197 fn next_revision(&self) -> Result<u64, DocumentError> {
198 self.revision
199 .checked_add(1)
200 .ok_or(DocumentError::RevisionExhausted)
201 }
202}
203
204#[cfg(test)]
205#[path = "tests.rs"]
206mod tests;
207
208#[doc(hidden)]
209#[cfg(feature = "retained-runtime")]
210pub mod retained_wire;