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
//! Unit tests for tessellated face-set lowering.
//!
//! These target the failure modes that survive a visual check: an off-by-one
//! that still renders, a `PnIndex` hop skipped so vertices permute, and
//! normals dragged through a translation. Each produces a mesh that looks
//! plausible and is wrong.

use axiolid_model::GeometryNode;

use super::{lower_polygonal_face_set_node, lower_triangulated_face_set_node};
use crate::lower::dispatch::lower_representation_item;
use crate::lower::session::LoweringSession;
use crate::solid::testkit::{entity, int_grid as grid, ints, list, model, n, r, refs};
use crate::transform::Transform;
use crate::units::UnitScale;

/// A unit tetrahedron: 4 points, 4 triangles, indices as the file writes them.
fn tetrahedron(pn: Option<&[i64]>) -> ifc_model::Model {
    let coords = entity(
        "IFCCARTESIANPOINTLIST3D",
        vec![list(vec![
            list(vec![n(0.0), n(0.0), n(0.0)]),
            list(vec![n(1.0), n(0.0), n(0.0)]),
            list(vec![n(0.0), n(1.0), n(0.0)]),
            list(vec![n(0.0), n(0.0), n(1.0)]),
        ])],
    );
    let face_set = entity(
        "IFCTRIANGULATEDFACESET",
        vec![
            r(1),
            ifc_model::Value::Null,
            ifc_model::Value::Bool(true),
            grid(&[&[1, 3, 2], &[1, 2, 4], &[2, 3, 4], &[3, 1, 4]]),
            match pn {
                Some(values) => ints(values),
                None => ifc_model::Value::Null,
            },
        ],
    );
    model(vec![(1, coords), (2, face_set)])
}

fn lower_tri(model: &ifc_model::Model, frame: Transform) -> axiolid_mesh::TriMesh {
    let scale = UnitScale::default();
    let mut session = LoweringSession::new(model, &scale);
    let node = lower_triangulated_face_set_node(&mut session, ifc_model::EntityId(2), frame)
        .expect("the face set must lower");
    let lowered = session.finish(node).expect("session finishes");
    match lowered.graph.get(lowered.root).expect("root node") {
        GeometryNode::TriMesh(mesh) => mesh.clone(),
        other => panic!("expected a TriMesh, got {other:?}"),
    }
}

/// Indices are 1-based in the file and 0-based in the mesh.
///
/// The file's first triangle is `(1,3,2)`. If the conversion is skipped the
/// mesh still builds — it just addresses the wrong vertices, and with four
/// points it does not even go out of bounds. That is why this asserts the
/// exact index triple rather than only the counts.
#[test]
fn coord_index_is_converted_from_one_based_to_zero_based() {
    let mesh = lower_tri(&tetrahedron(None), Transform::identity());

    assert_eq!(mesh.positions.len(), 4, "four shared points");
    assert_eq!(mesh.indices.len(), 12, "four triangles, three corners each");
    assert_eq!(
        &mesh.indices[0..3],
        &[0, 2, 1],
        "file triangle (1,3,2) must become (0,2,1)"
    );
}

/// A `PnIndex` permutation is applied, not ignored.
///
/// `PnIndex` reverses the point order here, so every triangle must address
/// different vertices than the no-PnIndex case. Skipping the hop is invisible
/// in the vertex count and in the triangle count.
#[test]
fn pn_index_indirection_permutes_the_addressed_vertices() {
    let direct = lower_tri(&tetrahedron(None), Transform::identity());
    let permuted = lower_tri(&tetrahedron(Some(&[4, 3, 2, 1])), Transform::identity());

    assert_eq!(
        permuted.positions, direct.positions,
        "PnIndex reorders addressing, never the point list itself"
    );
    assert_eq!(
        &permuted.indices[0..3],
        &[3, 1, 2],
        "through PnIndex [4,3,2,1], file triangle (1,3,2) addresses (3,1,2)"
    );
    assert_ne!(
        direct.indices, permuted.indices,
        "ignoring PnIndex would leave the indices unchanged"
    );
}

/// Positions are translated by the frame; the mesh stays indexed.
#[test]
fn the_frame_places_positions_without_duplicating_them() {
    let frame = Transform::translation([10.0, 0.0, 0.0]);
    let mesh = lower_tri(&tetrahedron(None), frame);

    assert_eq!(mesh.positions.len(), 4, "placement must not unshare points");
    assert_eq!(
        mesh.positions[0].to_array(),
        [10.0, 0.0, 0.0],
        "the origin vertex moves with the frame"
    );
}

/// Normals take the linear part only.
///
/// Sending a normal through the full affine transform adds the translation,
/// which turns a unit normal into a position vector. The mesh still renders;
/// the lighting is wrong everywhere the product is not at the origin.
#[test]
fn normals_are_rotated_but_never_translated() {
    let coords = entity(
        "IFCCARTESIANPOINTLIST3D",
        vec![list(vec![
            list(vec![n(0.0), n(0.0), n(0.0)]),
            list(vec![n(1.0), n(0.0), n(0.0)]),
            list(vec![n(0.0), n(1.0), n(0.0)]),
        ])],
    );
    let face_set = entity(
        "IFCTRIANGULATEDFACESET",
        vec![
            r(1),
            list(vec![list(vec![n(0.0), n(0.0), n(1.0)])]),
            ifc_model::Value::Bool(true),
            grid(&[&[1, 2, 3]]),
            ifc_model::Value::Null,
        ],
    );
    let model = model(vec![(1, coords), (2, face_set)]);

    let frame = Transform::translation([5.0, 7.0, 9.0]);
    let mesh = lower_tri(&model, frame);
    let normals = mesh.normals.expect("the source declares normals");

    assert_eq!(
        normals.values[0].to_array(),
        [0.0, 0.0, 1.0],
        "a pure translation must leave the normal untouched"
    );
}

/// An index past the end of `Coordinates` is refused, not clamped.
///
/// Substituting the origin would draw a spike to (0,0,0) that looks like
/// geometry. The error must name the offending entity.
#[test]
fn an_index_past_the_end_of_coordinates_is_rejected() {
    let coords = entity(
        "IFCCARTESIANPOINTLIST3D",
        vec![list(vec![
            list(vec![n(0.0), n(0.0), n(0.0)]),
            list(vec![n(1.0), n(0.0), n(0.0)]),
            list(vec![n(0.0), n(1.0), n(0.0)]),
        ])],
    );
    let face_set = entity(
        "IFCTRIANGULATEDFACESET",
        vec![
            r(1),
            ifc_model::Value::Null,
            ifc_model::Value::Bool(true),
            grid(&[&[1, 2, 9]]),
            ifc_model::Value::Null,
        ],
    );
    let model = model(vec![(1, coords), (2, face_set)]);
    let scale = UnitScale::default();
    let mut session = LoweringSession::new(&model, &scale);

    let error = lower_triangulated_face_set_node(
        &mut session,
        ifc_model::EntityId(2),
        Transform::identity(),
    )
    .expect_err("a dangling index must not lower");
    let text = format!("{error:?}");
    assert!(
        text.contains('9') || text.contains("Coordinates"),
        "the error must name the bad index or the slot, got {text}"
    );
}

/// An n-gon face keeps its authored corner count: no triangulation here.
#[test]
fn polygonal_faces_keep_their_authored_n_gons() {
    let coords = entity(
        "IFCCARTESIANPOINTLIST3D",
        vec![list(vec![
            list(vec![n(0.0), n(0.0), n(0.0)]),
            list(vec![n(1.0), n(0.0), n(0.0)]),
            list(vec![n(1.0), n(1.0), n(0.0)]),
            list(vec![n(0.0), n(1.0), n(0.0)]),
        ])],
    );
    let face = entity("IFCINDEXEDPOLYGONALFACE", vec![ints(&[1, 2, 3, 4])]);
    let face_set = entity(
        "IFCPOLYGONALFACESET",
        vec![
            r(1),
            ifc_model::Value::Bool(true),
            refs(&[3]),
            ifc_model::Value::Null,
        ],
    );
    let model = model(vec![(1, coords), (3, face), (2, face_set)]);

    let scale = UnitScale::default();
    let mut session = LoweringSession::new(&model, &scale);
    let node =
        lower_polygonal_face_set_node(&mut session, ifc_model::EntityId(2), Transform::identity())
            .expect("the quad must lower");
    let lowered = session.finish(node).expect("finishes");
    let mesh = match lowered.graph.get(lowered.root).expect("root") {
        GeometryNode::PolygonMesh(mesh) => mesh.clone(),
        other => panic!("expected a PolygonMesh, got {other:?}"),
    };

    assert_eq!(mesh.faces.len(), 1, "one authored face");
    assert_eq!(
        mesh.faces[0].outer,
        vec![0, 1, 2, 3],
        "a quad stays a quad; triangulating here would report six indices"
    );
    assert!(mesh.faces[0].holes.is_empty(), "no voids declared");
}

/// A face with voids keeps its inner loops separate from the outer one.
///
/// Flattening holes into the outer loop produces a face that tessellates into
/// a filled polygon — the window disappears and the wall looks solid.
#[test]
fn a_face_with_voids_keeps_its_inner_loops() {
    let mut points = Vec::new();
    for (x, y) in [
        (0.0, 0.0),
        (4.0, 0.0),
        (4.0, 4.0),
        (0.0, 4.0),
        (1.0, 1.0),
        (2.0, 1.0),
        (2.0, 2.0),
        (1.0, 2.0),
    ] {
        points.push(list(vec![n(x), n(y), n(0.0)]));
    }
    let coords = entity("IFCCARTESIANPOINTLIST3D", vec![list(points)]);
    let face = entity(
        "IFCINDEXEDPOLYGONALFACEWITHVOIDS",
        vec![ints(&[1, 2, 3, 4]), list(vec![ints(&[5, 6, 7, 8])])],
    );
    let face_set = entity(
        "IFCPOLYGONALFACESET",
        vec![
            r(1),
            ifc_model::Value::Bool(true),
            refs(&[3]),
            ifc_model::Value::Null,
        ],
    );
    let model = model(vec![(1, coords), (3, face), (2, face_set)]);

    let scale = UnitScale::default();
    let mut session = LoweringSession::new(&model, &scale);
    let node =
        lower_polygonal_face_set_node(&mut session, ifc_model::EntityId(2), Transform::identity())
            .expect("the holed face must lower");
    let lowered = session.finish(node).expect("finishes");
    let mesh = match lowered.graph.get(lowered.root).expect("root") {
        GeometryNode::PolygonMesh(mesh) => mesh.clone(),
        other => panic!("expected a PolygonMesh, got {other:?}"),
    };

    assert_eq!(mesh.faces[0].outer, vec![0, 1, 2, 3], "the outer boundary");
    assert_eq!(mesh.faces[0].holes.len(), 1, "one void");
    assert_eq!(
        mesh.faces[0].holes[0],
        vec![4, 5, 6, 7],
        "the void keeps its own loop"
    );
}

/// The set-level `PnIndex` hop applies to polygonal faces too.
///
/// `IfcPolygonalFaceSet.PnIndex` remaps what every face's `CoordIndex`
/// addresses. Skipping it leaves a mesh with the right face count, the right
/// vertex count, and silently permuted geometry.
#[test]
fn a_set_level_pn_index_remaps_polygonal_face_indices() {
    fn build(pn: Option<&[i64]>) -> axiolid_mesh::PolygonMesh {
        let coords = entity(
            "IFCCARTESIANPOINTLIST3D",
            vec![list(vec![
                list(vec![n(0.0), n(0.0), n(0.0)]),
                list(vec![n(1.0), n(0.0), n(0.0)]),
                list(vec![n(1.0), n(1.0), n(0.0)]),
                list(vec![n(0.0), n(1.0), n(0.0)]),
            ])],
        );
        let face = entity("IFCINDEXEDPOLYGONALFACE", vec![ints(&[1, 2, 3])]);
        let face_set = entity(
            "IFCPOLYGONALFACESET",
            vec![
                r(1),
                ifc_model::Value::Bool(true),
                refs(&[3]),
                match pn {
                    Some(values) => ints(values),
                    None => ifc_model::Value::Null,
                },
            ],
        );
        let model = model(vec![(1, coords), (3, face), (2, face_set)]);
        let scale = UnitScale::default();
        let mut session = LoweringSession::new(&model, &scale);
        let node = lower_polygonal_face_set_node(
            &mut session,
            ifc_model::EntityId(2),
            Transform::identity(),
        )
        .expect("lowers");
        let lowered = session.finish(node).expect("finishes");
        match lowered.graph.get(lowered.root).expect("root") {
            GeometryNode::PolygonMesh(mesh) => mesh.clone(),
            other => panic!("expected a PolygonMesh, got {other:?}"),
        }
    }

    let direct = build(None);
    let permuted = build(Some(&[4, 3, 2, 1]));

    assert_eq!(direct.faces[0].outer, vec![0, 1, 2], "without PnIndex");
    assert_eq!(
        permuted.faces[0].outer,
        vec![3, 2, 1],
        "through PnIndex [4,3,2,1], face (1,2,3) addresses (3,2,1)"
    );
    assert_ne!(
        direct.faces[0].outer, permuted.faces[0].outer,
        "ignoring the set-level PnIndex would leave the loop unchanged"
    );
}

/// Both families are reachable through the total dispatcher.
///
/// The unit tests above call the lowering functions directly, so they still
/// pass if the dispatch arm is deleted and the family silently reverts to
/// `Unsupported`. This routes through `lower_representation_item` instead.
#[test]
fn both_face_set_families_route_through_the_dispatcher() {
    let scale = UnitScale::default();

    let tri = tetrahedron(None);
    let mut session = LoweringSession::new(&tri, &scale);
    let node =
        lower_representation_item(&mut session, ifc_model::EntityId(2), Transform::identity())
            .expect("IFCTRIANGULATEDFACESET must dispatch, not report Unsupported");
    let lowered = session.finish(node).expect("finishes");
    assert!(
        matches!(
            lowered.graph.get(lowered.root).expect("root"),
            GeometryNode::TriMesh(_)
        ),
        "the dispatcher must produce a TriMesh"
    );

    let coords = entity(
        "IFCCARTESIANPOINTLIST3D",
        vec![list(vec![
            list(vec![n(0.0), n(0.0), n(0.0)]),
            list(vec![n(1.0), n(0.0), n(0.0)]),
            list(vec![n(1.0), n(1.0), n(0.0)]),
        ])],
    );
    let face = entity("IFCINDEXEDPOLYGONALFACE", vec![ints(&[1, 2, 3])]);
    let face_set = entity(
        "IFCPOLYGONALFACESET",
        vec![
            r(1),
            ifc_model::Value::Bool(true),
            refs(&[3]),
            ifc_model::Value::Null,
        ],
    );
    let poly = model(vec![(1, coords), (3, face), (2, face_set)]);
    let mut session = LoweringSession::new(&poly, &scale);
    let node =
        lower_representation_item(&mut session, ifc_model::EntityId(2), Transform::identity())
            .expect("IFCPOLYGONALFACESET must dispatch, not report Unsupported");
    let lowered = session.finish(node).expect("finishes");
    assert!(
        matches!(
            lowered.graph.get(lowered.root).expect("root"),
            GeometryNode::PolygonMesh(_)
        ),
        "the dispatcher must produce a PolygonMesh"
    );
}