Skip to main content

openbim_ifc_capi/
curves.rs

1//! Polylines (#458): the curves of a curve representation as a handle to a
2//! compiled set, read as a mesh set is.
3//!
4//! One record per product, whose `ProductPolyline` records each name a
5//! slice of the product's one position array: the host copies the array
6//! once and draws each slice as a line strip. The set owns its data, as a
7//! mesh set does.
8
9use openbim_ifc_binding_core::value::Tagged;
10use openbim_ifc_binding_core::{ProductCurves, ToRecord};
11
12use crate::buffer::{fill, put, text};
13use crate::domains::Out;
14use crate::geometry::selection;
15use crate::model::{with_model, OpenbimIfcModel};
16use crate::registry;
17use crate::status::{boundary, OpenbimIfcStatus};
18use crate::tape::OpenbimIfcValueNode;
19
20/// Opaque handle to a compiled polyline set. Zero is never a valid handle.
21pub type OpenbimIfcCurves = u64;
22
23/// Compile the curves of each product's `representation` as polylines
24/// (#458), for `ids` (`id_count` of them) or, with `ids` null and
25/// `id_count` 0, every product with a shape, and write the new set's
26/// handle to `out_curves`. `representation` is `representation_len` bytes
27/// of UTF-8: `Plan` (the best drawing curves: `Plan`, `Annotation`,
28/// `FootPrint`, then `Axis`, preferring a `PLAN_VIEW` context), `Axis`,
29/// `FootPrint`, `Annotation`, `Body`, `Surface` or `Reference`. Read the set with
30/// `openbim_ifc_v0_1_curves_records` and `_curves_positions`; destroy it
31/// with `openbim_ifc_v0_1_curves_destroy`. The chord budget is one
32/// millimetre.
33///
34/// A product whose curves cannot be compiled is a record with a refusal,
35/// not a failed call: a curve the reference compiler has no certified
36/// polyline for (an implicit curve, an offset of an offset) is
37/// `unsupported`, naming the product; trimmed, composite, offset and
38/// intrinsic curves compile (#506). `InvalidValue` for another
39/// representation name,
40/// `InvalidArgument` for text that is not UTF-8, `UnsupportedSchema`;
41/// `FeatureDisabled` in a library built without the `mesh` feature.
42///
43/// # Safety
44/// `ids` valid for `id_count` reads when non-null; `representation` valid
45/// for `representation_len` reads; `out_curves` valid for one write.
46#[no_mangle]
47pub unsafe extern "C" fn openbim_ifc_v0_1_model_product_curves(
48    model: OpenbimIfcModel,
49    ids: *const u64,
50    id_count: usize,
51    representation: *const u8,
52    representation_len: usize,
53    out_curves: *mut OpenbimIfcCurves,
54) -> OpenbimIfcStatus {
55    boundary(|| {
56        if out_curves.is_null() {
57            return OpenbimIfcStatus::NullPointer;
58        }
59        // SAFETY: caller contract above.
60        let inputs = unsafe {
61            (
62                selection(ids, id_count),
63                text(representation, representation_len),
64            )
65        };
66        let (ids, representation) = match inputs {
67            (Ok(ids), Ok(representation)) => (ids, representation),
68            (Err(status), _) | (_, Err(status)) => return status,
69        };
70        with_model(model, |m| {
71            let handle = registry::insert_curves(m.product_curves(ids, representation)?);
72            // SAFETY: checked non-null; caller contract above.
73            Ok(unsafe { put(out_curves, handle) }
74                .err()
75                .unwrap_or(OpenbimIfcStatus::Ok))
76        })
77    })
78}
79
80/// Run `read` on the live set behind `curves`.
81fn with_curves(
82    curves: OpenbimIfcCurves,
83    read: impl FnOnce(&[ProductCurves]) -> OpenbimIfcStatus,
84) -> OpenbimIfcStatus {
85    boundary(|| match registry::curves(curves) {
86        Some(set) => read(&set),
87        None => OpenbimIfcStatus::InvalidHandle,
88    })
89}
90
91/// The set's `ProductCurves` records as a tape, in the order compiled;
92/// `out_count` gets their number. `ProductCurves`: id (`REF`), global id,
93/// type name, transform (`LIST` of 16 `REAL`s, column-major, metres, or
94/// `NULL`), vertex count (`INTEGER`), polylines (`LIST` of
95/// `ProductPolyline`), refusal (`GeometryRefusal` or `NULL`), skipped
96/// (`LIST` of `REF`s: the representation's text literals and annotation
97/// fill areas, not drawn and not a refusal, #512).
98/// `ProductPolyline`: curve (`REF` of the IFC curve, or `NULL`), first
99/// vertex and vertex count (`INTEGER`s: its slice of the positions),
100/// closed (`BOOL`: its last vertex repeats its first), chord bound and
101/// chord requested (`REAL`s, metres: the certified bound on how far the
102/// exact curve lies from the polyline, `0` for a polyline the file
103/// authored, and the budget). No polylines and no refusal is a product
104/// without the representation, or with no curves.
105///
106/// # Safety
107/// `out_count` valid for one write; otherwise as for
108/// `openbim_ifc_v0_1_entity_attribute`.
109#[no_mangle]
110pub unsafe extern "C" fn openbim_ifc_v0_1_curves_records(
111    curves: OpenbimIfcCurves,
112    out_count: *mut usize,
113    nodes: *mut OpenbimIfcValueNode,
114    node_capacity: usize,
115    out_nodes_required: *mut usize,
116    strings: *mut u8,
117    string_capacity: usize,
118    out_strings_required: *mut usize,
119) -> OpenbimIfcStatus {
120    let out = Out {
121        nodes,
122        node_capacity,
123        out_nodes_required,
124        strings,
125        string_capacity,
126        out_strings_required,
127    };
128    with_curves(curves, |set| {
129        // SAFETY: caller contract above.
130        if let Err(status) = unsafe { put(out_count, set.len()) } {
131            return status;
132        }
133        let tape = Tagged::List(
134            set.iter()
135                .map(|product| product.to_record().to_tagged())
136                .collect(),
137        );
138        // SAFETY: caller contract above.
139        unsafe { out.write(&tape) }.unwrap_or_else(|error| OpenbimIfcStatus::from(&error))
140    })
141}
142
143/// Copy the positions of product `index` -- `x, y, z` per vertex, metres,
144/// relative to its record's transform, every polyline's vertices in turn
145/// -- into `buffer` (`capacity` floats), after writing the count needed
146/// (3 x vertex count) to `out_required`. `OutOfRange` for an index past
147/// the set.
148///
149/// # Safety
150/// As for `openbim_ifc_v0_1_meshes_positions`.
151#[no_mangle]
152pub unsafe extern "C" fn openbim_ifc_v0_1_curves_positions(
153    curves: OpenbimIfcCurves,
154    index: usize,
155    buffer: *mut f32,
156    capacity: usize,
157    out_required: *mut usize,
158) -> OpenbimIfcStatus {
159    with_curves(curves, |set| match set.get(index) {
160        // SAFETY: caller contract above.
161        Some(product) => unsafe { fill(&product.positions, buffer, capacity, out_required) },
162        None => OpenbimIfcStatus::OutOfRange,
163    })
164}
165
166/// Destroy a polyline set. A stale or repeated handle is `InvalidHandle`.
167#[no_mangle]
168pub extern "C" fn openbim_ifc_v0_1_curves_destroy(curves: OpenbimIfcCurves) -> OpenbimIfcStatus {
169    boundary(|| {
170        if registry::remove_curves(curves) {
171            OpenbimIfcStatus::Ok
172        } else {
173            OpenbimIfcStatus::InvalidHandle
174        }
175    })
176}