ifc-geometry 0.10.0

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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//! Reading representation contexts, including DERIVED inheritance.

use ifc_geometry::{
    all_contexts, context_of, plan_contexts, select_plan_representation,
    select_shape_representation, RepresentationContext, TargetView,
};
use ifc_model::{Entity, EntityId, Model, Value};

fn put(model: &mut Model, id: u64, type_name: &str, attributes: Vec<Value>) -> EntityId {
    let entity_id = EntityId(id);
    model.insert(entity_id, Entity::new(type_name, attributes));
    entity_id
}

fn text(value: &str) -> Value {
    Value::Text(value.into())
}

/// A root context carrying real values, plus a sub-context that redeclares the
/// inherited six as `*` — exactly the shape real exporters write.
fn model_with_contexts() -> (Model, EntityId, EntityId) {
    let mut model = Model::new();
    let placement = put(&mut model, 1, "IFCAXIS2PLACEMENT3D", vec![]);
    let root = put(
        &mut model,
        2,
        "IFCGEOMETRICREPRESENTATIONCONTEXT",
        vec![
            Value::Null,
            text("Model"),
            Value::Integer(3),
            Value::Real(1.0e-5),
            Value::Ref(placement),
            Value::Null,
        ],
    );
    let plan = put(
        &mut model,
        3,
        "IFCGEOMETRICREPRESENTATIONSUBCONTEXT",
        vec![
            text("Plan"),
            text("Plan"),
            Value::Derived,
            Value::Derived,
            Value::Derived,
            Value::Derived,
            Value::Ref(root),
            Value::Real(0.01),
            Value::Enum("PLAN_VIEW".into()),
            Value::Null,
        ],
    );
    (model, root, plan)
}

#[test]
fn a_sub_context_reports_its_own_attributes() {
    let (model, root, plan) = model_with_contexts();
    let entity = model.get(plan).unwrap();
    let context = RepresentationContext::new(plan, entity);

    assert!(context.is_sub_context());
    assert_eq!(context.identifier().as_deref(), Some("Plan"));
    assert_eq!(context.parent(), Some(root));
    assert_eq!(context.target_scale(), Some(0.01));
    assert_eq!(context.target_view(), Some(TargetView::PlanView));
    assert!(context.is_plan_view());
}

/// The `*` trap: these six live on the parent, not on the sub-context.
#[test]
fn derived_attributes_resolve_through_the_parent_context() {
    let (model, _, plan) = model_with_contexts();
    let entity = model.get(plan).unwrap();
    let context = RepresentationContext::new(plan, entity);

    assert_eq!(
        context.precision(&model),
        Some(1.0e-5),
        "precision is written as * and lives on the parent"
    );
    assert_eq!(context.coordinate_space_dimension(&model), Some(3));
    assert_eq!(
        context.world_coordinate_system(&model),
        Some(EntityId(1)),
        "without this a sub-context appears to have no placement"
    );
}

/// `$` is not `*`: an explicitly unset value on the CHILD must not trigger a
/// walk to the parent, even when the parent holds a value.
///
/// The distinction only shows when the parent has something to steal: if the
/// child says `$` and inherits anyway, it wrongly reports the parent's number.
#[test]
fn an_explicitly_null_attribute_is_not_inherited() {
    let mut model = Model::new();
    let root = put(
        &mut model,
        1,
        "IFCGEOMETRICREPRESENTATIONCONTEXT",
        vec![
            Value::Null,
            text("Model"),
            Value::Integer(3),
            Value::Real(1.0e-5),
            Value::Null,
            Value::Null,
        ],
    );
    // Precision is `$` here, not `*`: the author stated "unset", and the
    // parent's 1.0e-5 must not leak in.
    let child = put(
        &mut model,
        2,
        "IFCGEOMETRICREPRESENTATIONSUBCONTEXT",
        vec![
            Value::Null,
            text("Plan"),
            Value::Derived,
            Value::Null,
            Value::Derived,
            Value::Derived,
            Value::Ref(root),
            Value::Null,
            Value::Enum("PLAN_VIEW".into()),
            Value::Null,
        ],
    );
    let context = RepresentationContext::new(child, model.get(child).unwrap());

    assert_eq!(
        context.precision(&model),
        None,
        "`$` means unset; inheriting the parent's 1.0e-5 would be wrong"
    );
    assert_eq!(
        context.coordinate_space_dimension(&model),
        Some(3),
        "but `*` in the neighbouring slot still inherits"
    );
}

#[test]
fn a_root_context_has_no_target_view_or_parent() {
    let (model, root, _) = model_with_contexts();
    let entity = model.get(root).unwrap();
    let context = RepresentationContext::new(root, entity);

    assert!(!context.is_sub_context());
    assert_eq!(context.parent(), None);
    assert_eq!(context.target_view(), None);
    assert!(!context.is_plan_view());
    assert_eq!(context.precision(&model), Some(1.0e-5), "read directly");
}

/// The depth bound is the single termination mechanism, so pin both sides of
/// it: a chain within the bound must still resolve, and one past it must give
/// up rather than loop. A bound nobody tests at its edge is a guess.
#[test]
fn a_deep_but_finite_chain_still_resolves_within_the_bound() {
    let mut model = Model::new();
    // Root holds the real value.
    let root = put(
        &mut model,
        1,
        "IFCGEOMETRICREPRESENTATIONCONTEXT",
        vec![
            Value::Null,
            text("Model"),
            Value::Integer(3),
            Value::Real(1.0e-5),
            Value::Null,
            Value::Null,
        ],
    );
    // Four sub-contexts chained onto it, each deferring with `*`.
    let mut parent = root;
    for id in 2u64..=5 {
        parent = put(
            &mut model,
            id,
            "IFCGEOMETRICREPRESENTATIONSUBCONTEXT",
            vec![
                Value::Null,
                Value::Null,
                Value::Derived,
                Value::Derived,
                Value::Derived,
                Value::Derived,
                Value::Ref(parent),
            ],
        );
    }
    let deepest = RepresentationContext::new(parent, model.get(parent).unwrap());
    assert_eq!(
        deepest.precision(&model),
        Some(1.0e-5),
        "a legal chain must resolve, not hit the ceiling"
    );
}

/// A cycle: the walk must terminate and report the value as unresolved.
#[test]
fn a_context_cycle_gives_up_instead_of_looping() {
    let mut model = Model::new();
    let a = EntityId(1);
    let b = EntityId(2);
    let attrs = |parent: EntityId| {
        vec![
            Value::Null,
            Value::Null,
            Value::Derived,
            Value::Derived,
            Value::Derived,
            Value::Derived,
            Value::Ref(parent),
        ]
    };
    model.insert(
        a,
        Entity::new("IFCGEOMETRICREPRESENTATIONSUBCONTEXT", attrs(b)),
    );
    model.insert(
        b,
        Entity::new("IFCGEOMETRICREPRESENTATIONSUBCONTEXT", attrs(a)),
    );

    for id in [a, b] {
        let view = RepresentationContext::new(id, model.get(id).unwrap());
        assert_eq!(view.precision(&model), None, "terminates on a cycle");
        assert_eq!(view.world_coordinate_system(&model), None);
    }
}

/// The 3D selector must keep REJECTING 2D identifiers. Without this, widening
/// SOLID_IDENTIFIERS would silently hand a viewer a centreline instead of a
/// solid -- the exact failure the original selector was written to prevent.
#[test]
fn the_solid_selector_refuses_2d_only_products() {
    let (mut model, root, _) = model_with_contexts();

    for (id, identifier) in [(10u64, "Axis"), (11, "FootPrint")] {
        put(
            &mut model,
            id,
            "IFCSHAPEREPRESENTATION",
            vec![
                Value::Ref(root),
                text(identifier),
                text("Curve2D"),
                Value::List(vec![]),
            ],
        );
    }
    let shape = put(
        &mut model,
        12,
        "IFCPRODUCTDEFINITIONSHAPE",
        vec![
            Value::Null,
            Value::Null,
            Value::List(vec![Value::Ref(EntityId(10)), Value::Ref(EntityId(11))]),
        ],
    );
    let wall = put(
        &mut model,
        13,
        "IFCWALL",
        vec![
            text("3vB2YO$MX4xv5uCqZZG05x"),
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Ref(shape),
        ],
    );

    assert_eq!(
        select_shape_representation(&model, wall).unwrap(),
        None,
        "a curve must never be returned as a body"
    );
    assert_eq!(
        select_plan_representation(&model, wall).unwrap(),
        Some(EntityId(11)),
        "but the plan selector finds the FootPrint"
    );
}

/// A product carrying Axis, FootPrint and Body representations: the shape any
/// Revit-authored wall has.
fn model_with_three_representations() -> (Model, EntityId, EntityId, EntityId, EntityId) {
    let (mut model, root, _plan) = model_with_contexts();

    let curve = |identifier: &str| {
        vec![
            Value::Ref(root),
            text(identifier),
            text("Curve2D"),
            Value::List(vec![]),
        ]
    };
    let axis = put(&mut model, 10, "IFCSHAPEREPRESENTATION", curve("Axis"));
    let footprint = put(&mut model, 11, "IFCSHAPEREPRESENTATION", curve("FootPrint"));
    let body = put(
        &mut model,
        12,
        "IFCSHAPEREPRESENTATION",
        vec![
            Value::Ref(root),
            text("Body"),
            text("SweptSolid"),
            Value::List(vec![]),
        ],
    );
    let shape = put(
        &mut model,
        13,
        "IFCPRODUCTDEFINITIONSHAPE",
        vec![
            Value::Null,
            Value::Null,
            Value::List(vec![
                Value::Ref(axis),
                Value::Ref(footprint),
                Value::Ref(body),
            ]),
        ],
    );
    let wall = put(
        &mut model,
        14,
        "IFCWALL",
        vec![
            text("3vB2YO$MX4xv5uCqZZG05x"),
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Ref(shape),
        ],
    );
    (model, wall, axis, footprint, body)
}

/// The two selectors must disagree: that is the whole point of adding one.
#[test]
fn the_solid_and_plan_selectors_choose_differently() {
    let (model, wall, _, footprint, body) = model_with_three_representations();

    assert_eq!(
        select_shape_representation(&model, wall).unwrap(),
        Some(body),
        "a 3D viewer wants the Body"
    );
    assert_eq!(
        select_plan_representation(&model, wall).unwrap(),
        Some(footprint),
        "a drawing wants the FootPrint, not the Body"
    );
}

#[test]
fn the_plan_selector_prefers_footprint_over_axis() {
    let (model, wall, axis, footprint, _) = model_with_three_representations();
    let chosen = select_plan_representation(&model, wall).unwrap();
    assert_eq!(chosen, Some(footprint));
    assert_ne!(
        chosen,
        Some(axis),
        "Axis is the least specific 2D identifier"
    );
}

/// An explicit PLAN_VIEW context beats any identifier heuristic.
#[test]
fn an_explicit_plan_context_wins_over_the_identifier_order() {
    let (mut model, root, plan) = model_with_contexts();

    // An Axis representation, but authored into the PLAN_VIEW sub-context.
    let axis_in_plan = put(
        &mut model,
        10,
        "IFCSHAPEREPRESENTATION",
        vec![
            Value::Ref(plan),
            text("Axis"),
            text("Curve2D"),
            Value::List(vec![]),
        ],
    );
    // A FootPrint in the plain model context, which the identifier order
    // would otherwise prefer.
    let footprint = put(
        &mut model,
        11,
        "IFCSHAPEREPRESENTATION",
        vec![
            Value::Ref(root),
            text("FootPrint"),
            text("Curve2D"),
            Value::List(vec![]),
        ],
    );
    let shape = put(
        &mut model,
        12,
        "IFCPRODUCTDEFINITIONSHAPE",
        vec![
            Value::Null,
            Value::Null,
            Value::List(vec![Value::Ref(axis_in_plan), Value::Ref(footprint)]),
        ],
    );
    let wall = put(
        &mut model,
        13,
        "IFCWALL",
        vec![
            text("3vB2YO$MX4xv5uCqZZG05x"),
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Ref(shape),
        ],
    );

    assert_eq!(
        select_plan_representation(&model, wall).unwrap(),
        Some(axis_in_plan),
        "stated authorial intent beats the identifier heuristic"
    );
}

/// The ArchiCAD trap: a bounding box authored *inside* a `PLAN_VIEW` context.
///
/// Regression for openbimrs/ifc#2. A context-first rule returns the box and
/// never consults the identifier list; on `AC20-FZK-Haus.ifc` that made 107 of
/// 253 shape representations resolve to `Box`, so every plan lookup produced a
/// rectangle instead of an outline. `Box` is not in `PLAN_IDENTIFIERS`, so the
/// intersected rule must skip it and take the drawable `FootPrint` even though
/// the box has the stronger context.
#[test]
fn a_bounding_box_in_a_plan_context_loses_to_a_drawable_representation() {
    let (mut model, root, plan) = model_with_contexts();

    // Exactly what ArchiCAD writes: Box/BoundingBox in the PLAN_VIEW sub-context.
    let box_in_plan = put(
        &mut model,
        10,
        "IFCSHAPEREPRESENTATION",
        vec![
            Value::Ref(plan),
            text("Box"),
            text("BoundingBox"),
            Value::List(vec![]),
        ],
    );
    let footprint = put(
        &mut model,
        11,
        "IFCSHAPEREPRESENTATION",
        vec![
            Value::Ref(root),
            text("FootPrint"),
            text("Curve2D"),
            Value::List(vec![]),
        ],
    );
    let shape = put(
        &mut model,
        12,
        "IFCPRODUCTDEFINITIONSHAPE",
        vec![
            Value::Null,
            Value::Null,
            Value::List(vec![Value::Ref(box_in_plan), Value::Ref(footprint)]),
        ],
    );
    let wall = put(
        &mut model,
        13,
        "IFCWALL",
        vec![
            text("3vB2YO$MX4xv5uCqZZG05x"),
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Ref(shape),
        ],
    );

    let chosen = select_plan_representation(&model, wall).unwrap();
    assert_ne!(
        chosen,
        Some(box_in_plan),
        "a bounding box is not drawable geometry, whatever context it sits in"
    );
    assert_eq!(
        chosen,
        Some(footprint),
        "the drawable FootPrint is the only real candidate"
    );
}

/// A product whose *only* plan-context representation is a box has no plan
/// geometry at all.
///
/// The honest answer is `None`. Returning the box would let a caller draw a
/// rectangle believing it was an outline -- the failure mode that made 47
/// products in `AC20-FZK-Haus.ifc` render as boxes.
#[test]
fn a_box_only_product_has_no_plan_representation() {
    let (mut model, _, plan) = model_with_contexts();
    let box_in_plan = put(
        &mut model,
        10,
        "IFCSHAPEREPRESENTATION",
        vec![
            Value::Ref(plan),
            text("Box"),
            text("BoundingBox"),
            Value::List(vec![]),
        ],
    );
    let shape = put(
        &mut model,
        11,
        "IFCPRODUCTDEFINITIONSHAPE",
        vec![
            Value::Null,
            Value::Null,
            Value::List(vec![Value::Ref(box_in_plan)]),
        ],
    );
    let wall = put(
        &mut model,
        12,
        "IFCWALL",
        vec![
            text("3vB2YO$MX4xv5uCqZZG05x"),
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Ref(shape),
        ],
    );

    assert_eq!(
        select_plan_representation(&model, wall).unwrap(),
        None,
        "a box-only product must report no plan geometry, not offer the box"
    );
}

/// A product with only a Body has no plan geometry. Deriving one needs
/// sectioning, which this crate does not do, so None is the honest answer.
#[test]
fn a_solid_only_product_has_no_plan_representation() {
    let (mut model, root, _) = model_with_contexts();
    let body = put(
        &mut model,
        10,
        "IFCSHAPEREPRESENTATION",
        vec![
            Value::Ref(root),
            text("Body"),
            text("SweptSolid"),
            Value::List(vec![]),
        ],
    );
    let shape = put(
        &mut model,
        11,
        "IFCPRODUCTDEFINITIONSHAPE",
        vec![
            Value::Null,
            Value::Null,
            Value::List(vec![Value::Ref(body)]),
        ],
    );
    let wall = put(
        &mut model,
        12,
        "IFCWALL",
        vec![
            text("3vB2YO$MX4xv5uCqZZG05x"),
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Null,
            Value::Ref(shape),
        ],
    );

    assert_eq!(select_plan_representation(&model, wall).unwrap(), None);
    assert_eq!(
        select_shape_representation(&model, wall).unwrap(),
        Some(body),
        "the 3D selector still finds it"
    );
}

#[test]
fn contexts_are_discoverable_from_the_model() {
    let (model, root, plan) = model_with_contexts();

    let all: Vec<_> = all_contexts(&model).iter().map(|c| c.id()).collect();
    assert_eq!(all, [root, plan], "root and sub-context, in id order");

    let plans: Vec<_> = plan_contexts(&model).iter().map(|c| c.id()).collect();
    assert_eq!(plans, [plan], "only the PLAN_VIEW sub-context");
}

#[test]
fn a_representation_reports_the_context_it_was_authored_into() {
    let (model, _, _, footprint, _) = model_with_three_representations();
    let context = context_of(&model, footprint).expect("has a context");

    assert_eq!(context.id(), EntityId(2), "the root model context");
    assert_eq!(context.context_type().as_deref(), Some("Model"));
    assert_eq!(context.identifier(), None, "this fixture leaves it unset");
    assert!(!context.is_plan_view());
}

#[test]
fn unknown_target_views_are_preserved_not_flattened() {
    let mut model = Model::new();
    let id = put(
        &mut model,
        1,
        "IFCGEOMETRICREPRESENTATIONSUBCONTEXT",
        vec![
            Value::Null,
            Value::Null,
            Value::Derived,
            Value::Derived,
            Value::Derived,
            Value::Derived,
            Value::Null,
            Value::Null,
            Value::Enum("SOME_FUTURE_VIEW".into()),
            Value::Null,
        ],
    );
    let context = RepresentationContext::new(id, model.get(id).unwrap());
    assert_eq!(
        context.target_view(),
        Some(TargetView::Other("SOME_FUTURE_VIEW".into()))
    );
    assert!(!context.is_plan_view());
}

#[test]
fn a_user_defined_view_carries_its_label() {
    let mut model = Model::new();
    let id = put(
        &mut model,
        1,
        "IFCGEOMETRICREPRESENTATIONSUBCONTEXT",
        vec![
            Value::Null,
            Value::Null,
            Value::Derived,
            Value::Derived,
            Value::Derived,
            Value::Derived,
            Value::Null,
            Value::Null,
            Value::Enum("USERDEFINED".into()),
            text("Baugenehmigung"),
        ],
    );
    let context = RepresentationContext::new(id, model.get(id).unwrap());
    assert_eq!(
        context.target_view(),
        Some(TargetView::UserDefined(Some("Baugenehmigung".into())))
    );
}

#[test]
fn a_reflected_plan_counts_as_a_plan() {
    assert!(TargetView::ReflectedPlanView.is_plan());
    assert!(TargetView::PlanView.is_plan());
    assert!(!TargetView::ModelView.is_plan());
    assert!(!TargetView::GraphView.is_plan());
}