ifc-geometry 0.4.4

IFC semantic views lowered into the format-neutral geometry DAG.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! `IfcTopologyResource`: the faceted-B-rep entity family.
//!
//! # Why these are views
//!
//! One `IfcClosedShell` in the corpus holds 169 faces, each with bounds and
//! a loop of shared points. Materializing every level eagerly would copy the
//! same 196-point pool 12 times. These borrow the model and resolve on
//! demand, so the lowerer decides what to intern.
//!
//! Slot indices follow STEP inheritance: a subtype's own attributes start
//! after every supertype attribute.

use ifc_model::{Entity, EntityId, Model};

use crate::error::{GeometryError, GeometryResult};
use crate::slots::Slots;

/// Attribute positions for the topology entities.
pub mod slot {
    /// `IfcPolyLoop.Polygon`
    pub const POLYGON: usize = 0;
    /// `IfcFaceBound.Bound`
    pub const BOUND: usize = 0;
    /// `IfcFaceBound.Orientation`
    pub const ORIENTATION: usize = 1;
    /// `IfcFace.Bounds`
    pub const BOUNDS: usize = 0;
    /// `IfcConnectedFaceSet.CfsFaces`
    pub const CFS_FACES: usize = 0;
    /// `IfcManifoldSolidBrep.Outer`
    pub const OUTER: usize = 0;
    /// `IfcFacetedBrepWithVoids.Voids`
    pub const VOIDS: usize = 1;
    /// `IfcVertexPoint.VertexGeometry`
    pub const VERTEX_GEOMETRY: usize = 0;
    /// `IfcEdge.EdgeStart`
    pub const EDGE_START: usize = 0;
    /// `IfcEdge.EdgeEnd`
    pub const EDGE_END: usize = 1;
    /// `IfcEdgeCurve.EdgeGeometry`
    pub const EDGE_GEOMETRY: usize = 2;
    /// `IfcEdgeCurve.SameSense`
    pub const EDGE_SAME_SENSE: usize = 3;
    /// `IfcOrientedEdge.EdgeElement`; slots 0-1 are the inherited, unset
    /// `IfcEdge` vertices, written `*` in a STEP file.
    pub const EDGE_ELEMENT: usize = 2;
    /// `IfcOrientedEdge.Orientation`
    pub const EDGE_ORIENTATION: usize = 3;
    /// `IfcSubedge.ParentEdge`; slots 0-1 are the inherited `IfcEdge`
    /// vertices, which a subedge does state.
    pub const PARENT_EDGE: usize = 2;
    /// `IfcEdgeLoop.EdgeList`
    pub const EDGE_LIST: usize = 0;
    /// `IfcFaceSurface.FaceSurface`
    pub const FACE_SURFACE: usize = 1;
    /// `IfcFaceSurface.SameSense`
    pub const FACE_SAME_SENSE: usize = 2;
}

/// `IfcPolyLoop`: a closed wire given as an ordered point list.
#[derive(Debug, Clone, Copy)]
pub struct PolyLoop<'m> {
    slots: Slots<'m>,
}

impl<'m> PolyLoop<'m> {
    /// Wrap an entity assumed to be an `IfcPolyLoop`.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The entity id.
    pub fn id(&self) -> EntityId {
        self.slots.id()
    }

    /// The polygon point references in file order.
    ///
    /// The schema requires at least three unique points; a shorter list
    /// bounds no area and is rejected rather than silently skipped.
    pub fn polygon(&self) -> GeometryResult<Vec<EntityId>> {
        let points = self.slots.req_ref_list(slot::POLYGON, "Polygon")?;
        if points.len() < 3 {
            return Err(self.slots.degenerate(format!(
                "polygon has {} points; a loop needs at least 3",
                points.len()
            )));
        }
        Ok(points)
    }
}

/// `IfcFaceBound` and its `IfcFaceOuterBound` subtype.
#[derive(Debug, Clone, Copy)]
pub struct FaceBound<'m> {
    slots: Slots<'m>,
}

impl<'m> FaceBound<'m> {
    /// Wrap an entity assumed to be an `IfcFaceBound` subtype.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The entity id.
    pub fn id(&self) -> EntityId {
        self.slots.id()
    }

    /// The bounding `IfcLoop`.
    pub fn bound(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::BOUND, "Bound")
    }

    /// Whether the loop orientation agrees with the face normal.
    ///
    /// `.F.` means the sense is reversed: the loop must be traversed backwards
    /// to bound the face correctly. Defaulting a missing value to `true` would
    /// silently flip such a face inside out, so absence is an error.
    pub fn orientation(&self) -> GeometryResult<bool> {
        self.slots.req_bool(slot::ORIENTATION, "Orientation")
    }

    /// Whether this is the outer bound rather than a hole.
    pub fn is_outer(&self) -> bool {
        self.slots
            .type_name()
            .eq_ignore_ascii_case("IFCFACEOUTERBOUND")
    }
}

/// `IfcFace`: a bounded region, possibly with holes.
#[derive(Debug, Clone, Copy)]
pub struct Face<'m> {
    slots: Slots<'m>,
}

impl<'m> Face<'m> {
    /// Wrap an entity assumed to be an `IfcFace` subtype.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The entity id.
    pub fn id(&self) -> EntityId {
        self.slots.id()
    }

    /// The bound references. The schema requires at least one.
    pub fn bounds(&self) -> GeometryResult<Vec<EntityId>> {
        let bounds = self.slots.req_ref_list(slot::BOUNDS, "Bounds")?;
        if bounds.is_empty() {
            return Err(self.slots.degenerate("face has no bounds"));
        }
        Ok(bounds)
    }
}

/// `IfcConnectedFaceSet` and its `IfcClosedShell`/`IfcOpenShell` subtypes.
#[derive(Debug, Clone, Copy)]
pub struct ConnectedFaceSet<'m> {
    slots: Slots<'m>,
}

impl<'m> ConnectedFaceSet<'m> {
    /// Wrap an entity assumed to be an `IfcConnectedFaceSet` subtype.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The entity id.
    pub fn id(&self) -> EntityId {
        self.slots.id()
    }

    /// The member face references.
    pub fn faces(&self) -> GeometryResult<Vec<EntityId>> {
        let faces = self.slots.req_ref_list(slot::CFS_FACES, "CfsFaces")?;
        if faces.is_empty() {
            return Err(self.slots.degenerate("face set has no faces"));
        }
        Ok(faces)
    }

    /// Whether the source asserts this shell is closed.
    ///
    /// Only `IfcClosedShell` carries that guarantee. Reporting an open shell
    /// as closed would let a downstream boolean assume a valid interior.
    pub fn is_closed(&self) -> bool {
        self.slots
            .type_name()
            .eq_ignore_ascii_case("IFCCLOSEDSHELL")
    }
}

/// `IfcManifoldSolidBrep` and its `IfcFacetedBrep`/`WithVoids` subtypes.
#[derive(Debug, Clone, Copy)]
pub struct ManifoldSolidBrep<'m> {
    slots: Slots<'m>,
}

impl<'m> ManifoldSolidBrep<'m> {
    /// Wrap an entity assumed to be an `IfcManifoldSolidBrep` subtype.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The entity id.
    pub fn id(&self) -> EntityId {
        self.slots.id()
    }

    /// The outer boundary shell.
    pub fn outer(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::OUTER, "Outer")
    }

    /// Interior void shells, empty unless this is an `IfcFacetedBrepWithVoids`.
    ///
    /// Voids are what make a brick a hollow block. Dropping them yields a
    /// solid that is visually identical from outside and wrong by volume, so
    /// the attribute is read whenever the subtype declares it.
    pub fn voids(&self) -> GeometryResult<Vec<EntityId>> {
        if !self
            .slots
            .type_name()
            .eq_ignore_ascii_case("IFCFACETEDBREPWITHVOIDS")
        {
            return Ok(Vec::new());
        }
        let voids = self.slots.req_ref_list(slot::VOIDS, "Voids")?;
        if voids.is_empty() {
            return Err(self
                .slots
                .degenerate("IfcFacetedBrepWithVoids declares no voids"));
        }
        Ok(voids)
    }
}

/// Resolve an entity and confirm it belongs to an expected type family.
pub fn expect_type<'m>(
    model: &'m Model,
    referrer: EntityId,
    id: EntityId,
    accepted: &[&str],
    expected: &'static str,
) -> GeometryResult<&'m Entity> {
    let entity = model.get(id).ok_or(GeometryError::MissingEntity {
        referrer,
        missing: id,
    })?;
    if accepted
        .iter()
        .any(|name| entity.type_name.eq_ignore_ascii_case(name))
    {
        return Ok(entity);
    }
    Err(GeometryError::WrongEntityType {
        entity: id,
        actual: entity.type_name.to_string(),
        expected,
    })
}

/// `IfcVertexPoint`: a topological vertex carrying its geometric point.
#[derive(Debug, Clone, Copy)]
pub struct VertexPoint<'m> {
    slots: Slots<'m>,
}

impl<'m> VertexPoint<'m> {
    /// Wrap an entity assumed to be an `IfcVertexPoint`.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The entity id.
    pub fn id(&self) -> EntityId {
        self.slots.id()
    }

    /// The `IfcCartesianPoint` this vertex sits on.
    pub fn vertex_geometry(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::VERTEX_GEOMETRY, "VertexGeometry")
    }
}

/// `IfcSubedge`: an edge carved from a longer parent edge.
///
/// The subedge states its own `EdgeStart`/`EdgeEnd`; `ParentEdge` supplies
/// the carrier geometry the subedge is a piece of. The parent may itself be
/// a subedge, so resolving the carrier is a walk, not a single hop.
#[derive(Debug, Clone, Copy)]
pub struct Subedge<'m> {
    slots: Slots<'m>,
}

impl<'m> Subedge<'m> {
    /// Wrap an entity assumed to be an `IfcSubedge`.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The entity id.
    pub fn id(&self) -> EntityId {
        self.slots.id()
    }

    /// `EdgeStart`, the vertex the carved piece starts at.
    pub fn start(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::EDGE_START, "EdgeStart")
    }

    /// `EdgeEnd`, the vertex the carved piece ends at.
    pub fn end(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::EDGE_END, "EdgeEnd")
    }

    /// `ParentEdge`, mandatory: without it a subedge carves nothing.
    pub fn parent_edge(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::PARENT_EDGE, "ParentEdge")
    }
}

/// `IfcEdge` and its `IfcEdgeCurve` subtype: a bounded piece of a curve.
///
/// `EdgeStart`/`EdgeEnd` are `IfcVertex` references, not points. An
/// `IfcEdgeCurve` adds the supporting curve and a sense flag saying whether
/// the edge runs along the curve or against it.
#[derive(Debug, Clone, Copy)]
pub struct EdgeCurve<'m> {
    slots: Slots<'m>,
}

impl<'m> EdgeCurve<'m> {
    /// Wrap an entity assumed to be an `IfcEdge` or `IfcEdgeCurve`.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The entity id.
    pub fn id(&self) -> EntityId {
        self.slots.id()
    }

    /// The start vertex reference.
    pub fn start(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::EDGE_START, "EdgeStart")
    }

    /// The end vertex reference.
    pub fn end(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::EDGE_END, "EdgeEnd")
    }

    /// The supporting curve, absent on a plain `IfcEdge`.
    ///
    /// A plain edge is straight between its vertices, so `None` is a complete
    /// description rather than a missing value.
    pub fn edge_geometry(&self) -> Option<EntityId> {
        self.slots.opt_ref(slot::EDGE_GEOMETRY)
    }

    /// Does the edge run along the curve's own direction?
    ///
    /// Defaults to true when absent. A false flag reverses the edge relative
    /// to its curve, which matters for any parameterised traversal.
    pub fn same_sense(&self) -> bool {
        self.slots.opt_bool(slot::EDGE_SAME_SENSE).unwrap_or(true)
    }
}

/// `IfcOrientedEdge`: a reuse of an edge, possibly reversed.
///
/// This is the entity that makes edge sharing explicit. Two faces meeting at
/// one edge each hold an oriented edge pointing at the SAME `IfcEdgeCurve`,
/// with opposite `Orientation`. Resolving through to the underlying edge is
/// what preserves the manifold; treating each use as its own edge silently
/// disconnects the solid.
#[derive(Debug, Clone, Copy)]
pub struct OrientedEdge<'m> {
    slots: Slots<'m>,
}

impl<'m> OrientedEdge<'m> {
    /// Wrap an entity assumed to be an `IfcOrientedEdge`.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The underlying edge this use points at.
    pub fn edge_element(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::EDGE_ELEMENT, "EdgeElement")
    }

    /// Does this use run along the underlying edge, or against it?
    pub fn orientation(&self) -> bool {
        self.slots.opt_bool(slot::EDGE_ORIENTATION).unwrap_or(true)
    }
}

/// `IfcEdgeLoop`: a closed wire given as a list of oriented edges.
///
/// The curved counterpart of `IfcPolyLoop`. Unlike a poly loop the closure
/// is explicit: the last edge's end vertex is the first edge's start, and no
/// implied closing segment is added.
#[derive(Debug, Clone, Copy)]
pub struct EdgeLoop<'m> {
    slots: Slots<'m>,
}

impl<'m> EdgeLoop<'m> {
    /// Wrap an entity assumed to be an `IfcEdgeLoop`.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The oriented edges in traversal order.
    ///
    /// A loop needs at least one edge; an empty list bounds nothing and is
    /// rejected rather than producing a face with no boundary.
    pub fn edge_list(&self) -> GeometryResult<Vec<EntityId>> {
        let edges = self.slots.req_ref_list(slot::EDGE_LIST, "EdgeList")?;
        if edges.is_empty() {
            return Err(self.slots.degenerate("edge loop has no edges"));
        }
        Ok(edges)
    }
}

/// `IfcFaceSurface` and its `IfcAdvancedFace` subtype.
///
/// Adds a support surface and a sense flag to `IfcFace`. `SameSense` says
/// whether the face normal agrees with the surface normal; ignoring it yields
/// an inside-out face that still passes every structural check.
#[derive(Debug, Clone, Copy)]
pub struct FaceSurface<'m> {
    slots: Slots<'m>,
}

impl<'m> FaceSurface<'m> {
    /// Wrap an entity assumed to be an `IfcFaceSurface` or `IfcAdvancedFace`.
    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
        Self {
            slots: Slots::new(id, entity),
        }
    }

    /// The supporting surface reference.
    pub fn face_surface(&self) -> GeometryResult<EntityId> {
        self.slots.req_ref(slot::FACE_SURFACE, "FaceSurface")
    }

    /// Does the face normal agree with the surface normal?
    pub fn same_sense(&self) -> bool {
        self.slots.opt_bool(slot::FACE_SAME_SENSE).unwrap_or(true)
    }
}