Skip to main content

ifc_spatial/
relation.rs

1//! Objectified relationship access.
2//!
3//! IFC does not store parent/child pointers on entities. A wall does not know
4//! its storey; an `IfcRelContainedInSpatialStructure` entity names both. So
5//! every containment question is a question about relationship entities, and
6//! this module reads them.
7//!
8//! ## Internal split
9//!
10//! - `slots.rs`: schema-fixed attribute positions for the `IfcRel*` types used here.
11//! - `link.rs`: reading a relationship's relating/related ends.
12//! - `boundary.rs`: space boundaries, which carry their own attributes
13//!   beyond the two ends and so do not fit the generic `Relationship` shape.
14
15pub mod boundary;
16mod index;
17pub(crate) mod link;
18pub(crate) mod slots;
19
20pub use boundary::{
21    BoundaryExposure, BoundaryPhysicality, ConnectionGeometryAnomaly, SpaceBoundary,
22};
23pub use index::RelationshipIndex;
24pub use link::{Relationship, RelationshipKind};
25
26use ifc_model::{EntityId, Model};
27
28/// Every aggregation, containment, nesting and covering relationship.
29///
30/// Found through the type index, so cost is proportional to the number of
31/// relationships rather than to model size.
32#[must_use]
33pub fn all(model: &Model) -> Vec<Relationship> {
34    let mut out = Vec::new();
35    for (slots, kind) in [
36        (slots::AGGREGATES, RelationshipKind::Aggregates),
37        (slots::CONTAINED_IN, RelationshipKind::ContainedIn),
38        (slots::NESTS, RelationshipKind::Nests),
39        (slots::COVERS_ELEMENTS, RelationshipKind::CoversElements),
40        (slots::COVERS_SPACES, RelationshipKind::CoversSpaces),
41        (
42            slots::INTERFERES_ELEMENTS,
43            RelationshipKind::InterferesElements,
44        ),
45        (slots::ASSIGNS_TO_ACTOR, RelationshipKind::AssignsToActor),
46        (
47            slots::ASSIGNS_TO_PROCESS,
48            RelationshipKind::AssignsToProcess,
49        ),
50        (
51            slots::ASSIGNS_TO_PRODUCT,
52            RelationshipKind::AssignsToProduct,
53        ),
54        (
55            slots::ASSIGNS_TO_GROUP_BY_FACTOR,
56            RelationshipKind::AssignsToGroupByFactor,
57        ),
58        (slots::DECLARES, RelationshipKind::Declares),
59        (slots::DEFINES_BY_OBJECT, RelationshipKind::DefinesByObject),
60        (
61            slots::FLOW_CONTROL_ELEMENTS,
62            RelationshipKind::FlowControlElements,
63        ),
64        (
65            slots::SERVICES_BUILDINGS,
66            RelationshipKind::ServicesBuildings,
67        ),
68        (
69            slots::CONNECTS_WITH_ECCENTRICITY,
70            RelationshipKind::ConnectsWithEccentricity,
71        ),
72    ] {
73        for id in model.ids_of_type(slots.type_name) {
74            if let Some(relationship) = link::read(model, *id, slots, kind) {
75                out.push(relationship);
76            }
77        }
78    }
79    // The IfcRelConnectsElements hierarchy shares one kind across three
80    // concrete types, and its ends sit at 5/6 rather than 4/5 because
81    // ConnectionGeometry occupies slot 4.
82    for slots in slots::CONNECTS_ELEMENT_TYPES {
83        for id in model.ids_of_type(slots.type_name) {
84            if let Some(relationship) =
85                link::read(model, *id, slots, RelationshipKind::ConnectsElements)
86            {
87                out.push(relationship);
88            }
89        }
90    }
91    out
92}
93
94/// The relationships whose related end names `target`.
95///
96/// The inverse query: given a wall, which containment relationship placed it?
97#[must_use]
98pub fn naming(model: &Model, target: EntityId) -> Vec<Relationship> {
99    all(model)
100        .into_iter()
101        .filter(|relationship| relationship.related.contains(&target))
102        .collect()
103}