ifc-geometry 0.6.1

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
//! `IfcCompositeCurve` and its segments.
//!
//! # Why segments are entities, not just curve references
//!
//! A composite curve is a chain of `IfcCompositeCurveSegment`, and each segment
//! carries two facts the parent curve cannot: whether it is traversed forwards
//! or backwards, and what kind of continuity holds at its far end.
//!
//! **`SameSense = .F.` is common, not exotic.** Exporters reuse one
//! `IfcCircle` or `IfcPolyline` for several outlines and flip the sense rather
//! than emitting a mirrored copy. A consumer that ignores `SameSense` produces
//! a chain whose segments do not meet: each reversed segment starts where its
//! neighbour also starts. The failure looks like a tolerance problem and is
//! not one.
//!
//! **`Transition` is a promise about the joint**, checked by nobody. The last
//! segment of a closed composite curve must say `DISCONTINUOUS` only if the
//! curve is open; a closed curve repeats the continuity of the first joint.
//! The code carries useful information for a kernel deciding whether it may
//! fuse two segments into one edge, so it is exposed rather than discarded.

use crate::error::GeometryResult;
use crate::slots::Slots;
use ifc_model::{Entity, EntityId};

/// `IfcCompositeCurve` attribute slots.
///
/// From IFC4 ADD2 TC1. `IfcCompositeCurveOnSurface`, `IfcBoundaryCurve` and
/// `IfcOuterBoundaryCurve` are subtypes that add no explicit attributes, so
/// these same indices apply to all four.
pub(crate) mod curve_slot {
    /// `Segments`: `LIST [1:?] OF IfcCompositeCurveSegment`.
    pub const SEGMENTS: usize = 0;
    /// `SelfIntersect`: `IfcLogical`, informational only.
    pub const SELF_INTERSECT: usize = 1;
}

/// `IfcCompositeCurveSegment` attribute slots.
///
/// From IFC4 ADD2 TC1. `IfcReparametrisedCompositeCurveSegment` adds
/// `ParamLength` at slot 3 and inherits these three unchanged.
pub(crate) mod segment_slot {
    /// `Transition`: `IfcTransitionCode`.
    pub const TRANSITION: usize = 0;
    /// `SameSense`: `IfcBoolean`.
    pub const SAME_SENSE: usize = 1;
    /// `ParentCurve`: the `IfcCurve` this segment is a piece of.
    pub const PARENT_CURVE: usize = 2;
    /// `ParamLength` on `IfcReparametrisedCompositeCurveSegment`.
    pub const PARAM_LENGTH: usize = 3;
}

/// `IfcTransitionCode`: what holds where one segment meets the next.
///
/// The values are ordered from weakest to strongest guarantee, and each
/// implies all the weaker ones.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransitionCode {
    /// The segments do not even meet; the curve has a gap at this joint.
    Discontinuous,
    /// The end point of this segment is the start point of the next.
    Continuous,
    /// Positions and tangent *directions* match.
    ///
    /// Tangent magnitude may still jump, so a kernel must not assume the
    /// parameterisation is smooth across the joint.
    ContSameGradient,
    /// Positions, tangent directions and curvature all match.
    ContSameGradientSameCurvature,
}

impl TransitionCode {
    /// Parse the enumeration token.
    ///
    /// Returns `None` for an unrecognised token; defaulting would claim a
    /// continuity the file never asserted.
    pub fn from_token(token: &str) -> Option<Self> {
        match token.to_ascii_uppercase().as_str() {
            "DISCONTINUOUS" => Some(Self::Discontinuous),
            "CONTINUOUS" => Some(Self::Continuous),
            "CONTSAMEGRADIENT" => Some(Self::ContSameGradient),
            "CONTSAMEGRADIENTSAMECURVATURE" => Some(Self::ContSameGradientSameCurvature),
            _ => None,
        }
    }

    /// The EXPRESS token, the inverse of [`Self::from_token`].
    pub fn token(self) -> &'static str {
        match self {
            Self::Discontinuous => "DISCONTINUOUS",
            Self::Continuous => "CONTINUOUS",
            Self::ContSameGradient => "CONTSAMEGRADIENT",
            Self::ContSameGradientSameCurvature => "CONTSAMEGRADIENTSAMECURVATURE",
        }
    }

    /// Do the segments at this joint at least touch?
    ///
    /// The question a kernel actually asks before deciding whether the chain
    /// forms one connected wire.
    pub fn is_connected(&self) -> bool {
        !matches!(self, Self::Discontinuous)
    }
}

/// Refusal for an IFC4X3 `IfcCurveSegment` met where a composite segment is read.
///
/// IFC4X3 widens `IfcCompositeCurve.Segments` to `IfcSegment`, so a segment
/// may be an `IfcCurveSegment` (`Transition`, `Placement`, `SegmentStart`,
/// `SegmentLength`, `ParentCurve`). Reading it through the
/// `IfcCompositeCurveSegment` slots would take `SegmentLength` for the parent
/// curve, so the view refuses it by name. Every lowering path checks for an
/// `IfcCurveSegment` before building this view (the curve lowering lowers it,
/// sweep ranges, p-curves and profile boundaries refuse it with their own
/// reason); the refusal is the backstop for any other caller.
pub const CURVE_SEGMENT_UNREAD: &str = "an IfcCurveSegment is placed and trimmed by \
     SegmentStart and SegmentLength and cannot be read as an \
     IfcCompositeCurveSegment; lower it with lower::curve instead";

/// A borrowed view of an `IfcCompositeCurveSegment`.
///
/// Also covers `IfcReparametrisedCompositeCurveSegment`, whose extra
/// `ParamLength` is read by [`Self::param_length`]. An IFC4X3
/// `IfcCurveSegment` has a different slot layout; every slot read refuses it
/// with [`CURVE_SEGMENT_UNREAD`].
#[derive(Debug, Clone, Copy)]
pub struct CompositeCurveSegment<'m> {
    slots: Slots<'m>,
}

impl<'m> CompositeCurveSegment<'m> {
    /// Wrap an entity known to be an `IfcCompositeCurveSegment` 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 curve this segment is a piece of.
    ///
    /// Frequently shared with other segments and other composite curves, which
    /// is precisely why [`Self::same_sense`] exists.
    pub fn parent_curve_ref(&self) -> GeometryResult<EntityId> {
        self.refuse_curve_segment()?;
        self.slots
            .req_ref(segment_slot::PARENT_CURVE, "ParentCurve")
    }

    /// Refuse an IFC4X3 `IfcCurveSegment`, whose slots differ from ours.
    fn refuse_curve_segment(&self) -> GeometryResult<()> {
        if self.slots.entity().is_type("IFCCURVESEGMENT") {
            return Err(self.slots.unsupported(CURVE_SEGMENT_UNREAD));
        }
        Ok(())
    }

    /// Is the segment traversed along the parent curve's own direction?
    ///
    /// `false` means reverse it before joining it to its neighbours.
    pub fn same_sense(&self) -> GeometryResult<bool> {
        self.refuse_curve_segment()?;
        self.slots.req_bool(segment_slot::SAME_SENSE, "SameSense")
    }

    /// The continuity asserted at the joint with the *next* segment.
    ///
    /// Note "next": the code describes the transition at the end of this
    /// segment, so the last segment of an open curve says `DISCONTINUOUS`.
    pub fn transition(&self) -> GeometryResult<TransitionCode> {
        let token = self
            .slots
            .opt_enum(segment_slot::TRANSITION)
            .ok_or_else(|| {
                self.slots
                    .degenerate("Transition is missing or is not an enumeration token")
            })?;
        TransitionCode::from_token(token).ok_or_else(|| {
            self.slots
                .degenerate(format!("unknown IfcTransitionCode .{token}."))
        })
    }

    /// `ParamLength`, present only on `IfcReparametrisedCompositeCurveSegment`.
    ///
    /// Returns `None` for a plain `IfcCompositeCurveSegment`. When present it
    /// rescales the segment's parameter range to `[0, ParamLength]`, so a
    /// consumer that ignores it will evaluate the parent curve at the wrong
    /// parameters. Values must be positive.
    pub fn param_length(&self) -> GeometryResult<Option<f64>> {
        self.refuse_curve_segment()?;
        let Some(value) = self.slots.opt_f64(segment_slot::PARAM_LENGTH) else {
            return Ok(None);
        };
        if value > 0.0 {
            Ok(Some(value))
        } else {
            Err(self
                .slots
                .degenerate(format!("ParamLength must be positive, found {value}")))
        }
    }

    /// Is this the reparametrised subtype?
    pub fn is_reparametrised(&self) -> bool {
        self.slots
            .entity()
            .type_name
            .eq_ignore_ascii_case("IFCREPARAMETRISEDCOMPOSITECURVESEGMENT")
    }
}

/// A borrowed view of an `IfcCompositeCurve`.
///
/// Also covers the subtypes that add no attributes:
/// `IfcCompositeCurveOnSurface`, `IfcBoundaryCurve`, `IfcOuterBoundaryCurve`.
#[derive(Debug, Clone, Copy)]
pub struct CompositeCurve<'m> {
    slots: Slots<'m>,
}

impl<'m> CompositeCurve<'m> {
    /// Wrap an entity known to be an `IfcCompositeCurve` 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 `IfcCompositeCurveSegment` references, in traversal order.
    ///
    /// Order is significant and must not be sorted: the chain's shape is the
    /// list order combined with each segment's `SameSense`.
    pub fn segment_refs(&self) -> GeometryResult<Vec<EntityId>> {
        let segments = self.slots.req_ref_list(curve_slot::SEGMENTS, "Segments")?;
        if segments.is_empty() {
            return Err(self
                .slots
                .degenerate("Segments is empty; LIST [1:?] requires at least one segment"));
        }
        Ok(segments)
    }

    /// The informational `SelfIntersect` flag, `None` when absent or `.U.`.
    pub fn self_intersect(&self) -> Option<bool> {
        self.slots.opt_bool(curve_slot::SELF_INTERSECT)
    }

    /// Is this curve constrained to lie on a surface?
    ///
    /// True for `IfcCompositeCurveOnSurface` and its `IfcBoundaryCurve` /
    /// `IfcOuterBoundaryCurve` descendants, whose segments must all have
    /// `IfcPcurve` or surface-curve parents.
    pub fn is_on_surface(&self) -> bool {
        matches!(
            self.slots.entity().type_name.to_ascii_uppercase().as_str(),
            "IFCCOMPOSITECURVEONSURFACE" | "IFCBOUNDARYCURVE" | "IFCOUTERBOUNDARYCURVE"
        )
    }

    /// Is this the *outer* boundary of a surface?
    ///
    /// `IfcOuterBoundaryCurve` is the only way a file marks which of a
    /// surface's boundaries is the outline rather than a hole, and the
    /// distinction is carried by the entity type alone.
    pub fn is_outer_boundary(&self) -> bool {
        self.slots
            .entity()
            .type_name
            .eq_ignore_ascii_case("IFCOUTERBOUNDARYCURVE")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ifc_model::Value;

    fn segment(transition: &str, same_sense: bool) -> Entity {
        Entity::new(
            "IFCCOMPOSITECURVESEGMENT",
            vec![
                Value::Enum(transition.into()),
                Value::Bool(same_sense),
                Value::Ref(EntityId(20)),
            ],
        )
    }

    /// An IFC4X3 curve segment's slot 2 is `SegmentStart`, not
    /// `ParentCurve`; reading it as one must be a named refusal.
    #[test]
    fn an_ifc4x3_curve_segment_is_refused_by_name_not_misread() {
        let length = |v: f64| Value::Typed {
            type_name: "IFCLENGTHMEASURE".into(),
            value: Box::new(Value::Real(v)),
        };
        let e = Entity::new(
            "IFCCURVESEGMENT",
            vec![
                Value::Enum("DISCONTINUOUS".into()),
                Value::Ref(EntityId(10)),
                length(0.0),
                length(50.0),
                Value::Ref(EntityId(20)),
            ],
        );
        let view = CompositeCurveSegment::new(EntityId(7), &e);
        for error in [
            view.parent_curve_ref().map(|_| ()).unwrap_err(),
            view.same_sense().map(|_| ()).unwrap_err(),
            view.param_length().map(|_| ()).unwrap_err(),
        ] {
            assert!(
                matches!(
                    &error,
                    crate::GeometryError::Unsupported { type_name, detail, .. }
                        if type_name == "IFCCURVESEGMENT" && *detail == CURVE_SEGMENT_UNREAD
                ),
                "{error}"
            );
        }
    }

    fn composite(type_name: &str, segments: &[u64]) -> Entity {
        Entity::new(
            type_name,
            vec![
                Value::List(segments.iter().map(|i| Value::Ref(EntityId(*i))).collect()),
                Value::Bool(false),
            ],
        )
    }

    #[test]
    fn a_reversed_segment_reports_same_sense_false_rather_than_being_normalised() {
        let e = segment("CONTINUOUS", false);
        let view = CompositeCurveSegment::new(EntityId(1), &e);
        assert!(!view.same_sense().unwrap());
        assert_eq!(view.parent_curve_ref().unwrap(), EntityId(20));
    }

    #[test]
    fn every_transition_code_round_trips_from_its_file_token() {
        for (token, expected) in [
            ("DISCONTINUOUS", TransitionCode::Discontinuous),
            ("CONTINUOUS", TransitionCode::Continuous),
            ("CONTSAMEGRADIENT", TransitionCode::ContSameGradient),
            (
                "CONTSAMEGRADIENTSAMECURVATURE",
                TransitionCode::ContSameGradientSameCurvature,
            ),
        ] {
            let e = segment(token, true);
            assert_eq!(
                CompositeCurveSegment::new(EntityId(1), &e)
                    .transition()
                    .unwrap(),
                expected,
                "token {token}"
            );
        }
    }

    /// Only DISCONTINUOUS leaves a gap; the other three all mean the segments
    /// touch, which is the question a wire builder asks.
    #[test]
    fn only_discontinuous_reports_a_gap_at_the_joint() {
        assert!(!TransitionCode::Discontinuous.is_connected());
        assert!(TransitionCode::Continuous.is_connected());
        assert!(TransitionCode::ContSameGradient.is_connected());
        assert!(TransitionCode::ContSameGradientSameCurvature.is_connected());
    }

    #[test]
    fn an_unknown_transition_token_is_rejected_rather_than_defaulted() {
        assert_eq!(TransitionCode::from_token("SMOOTHISH"), None);
        let e = segment("SMOOTHISH", true);
        assert!(CompositeCurveSegment::new(EntityId(1), &e)
            .transition()
            .is_err());
    }

    #[test]
    fn param_length_is_absent_on_a_plain_segment_and_read_on_the_reparametrised_one() {
        let plain = segment("CONTINUOUS", true);
        let view = CompositeCurveSegment::new(EntityId(1), &plain);
        assert_eq!(view.param_length().unwrap(), None);
        assert!(!view.is_reparametrised());

        let reparam = Entity::new(
            "IFCREPARAMETRISEDCOMPOSITECURVESEGMENT",
            vec![
                Value::Enum("CONTINUOUS".into()),
                Value::Bool(true),
                Value::Ref(EntityId(20)),
                Value::Typed {
                    type_name: "IFCPARAMETERVALUE".into(),
                    value: Box::new(Value::Real(2.0)),
                },
            ],
        );
        let view = CompositeCurveSegment::new(EntityId(1), &reparam);
        assert_eq!(view.param_length().unwrap(), Some(2.0));
        assert!(view.is_reparametrised());
    }

    #[test]
    fn a_non_positive_param_length_is_degenerate() {
        let e = Entity::new(
            "IFCREPARAMETRISEDCOMPOSITECURVESEGMENT",
            vec![
                Value::Enum("CONTINUOUS".into()),
                Value::Bool(true),
                Value::Ref(EntityId(20)),
                Value::Real(0.0),
            ],
        );
        assert!(CompositeCurveSegment::new(EntityId(1), &e)
            .param_length()
            .is_err());
    }

    #[test]
    fn composite_curve_segments_keep_their_file_order() {
        let e = composite("IFCCOMPOSITECURVE", &[1, 2, 3]);
        assert_eq!(
            CompositeCurve::new(EntityId(1), &e).segment_refs().unwrap(),
            vec![EntityId(1), EntityId(2), EntityId(3)]
        );
    }

    #[test]
    fn a_composite_curve_with_no_segments_is_degenerate() {
        let e = composite("IFCCOMPOSITECURVE", &[]);
        assert!(CompositeCurve::new(EntityId(1), &e).segment_refs().is_err());
    }

    /// Whether a boundary is the outline or a hole is carried by the entity
    /// type alone, so classification cannot be skipped.
    #[test]
    fn boundary_curve_subtypes_are_classified_from_the_type_name() {
        let plain = composite("IFCCOMPOSITECURVE", &[1]);
        let on_surface = composite("IFCCOMPOSITECURVEONSURFACE", &[1]);
        let boundary = composite("IFCBOUNDARYCURVE", &[1]);
        let outer = composite("IFCOUTERBOUNDARYCURVE", &[1]);

        assert!(!CompositeCurve::new(EntityId(1), &plain).is_on_surface());
        assert!(CompositeCurve::new(EntityId(1), &on_surface).is_on_surface());
        assert!(CompositeCurve::new(EntityId(1), &boundary).is_on_surface());
        assert!(!CompositeCurve::new(EntityId(1), &boundary).is_outer_boundary());
        assert!(CompositeCurve::new(EntityId(1), &outer).is_outer_boundary());
    }
}