Skip to main content

ifc_geometry/lower/
point.rs

1//! Point lowering: exact parametric references, never evaluated here.
2//!
3//! # Why these are not evaluated to Cartesian coordinates
4//!
5//! `IfcPointOnCurve` and `IfcPointOnSurface` are legal `IfcPoint` subtypes
6//! whose position is defined by a parameter on a basis curve or surface, not
7//! by stored coordinates. Evaluating them here would require a curve/surface
8//! evaluator, which this crate deliberately does not own (ADR 0004).
9//! Preserving the reference and the parameter(s)
10//! exactly, as `axiolid_model::PointOnCurve`/`PointOnSurface`, keeps the
11//! adapter kernel-agnostic: an application chooses when and how to evaluate.
12//!
13//! # Parameters are not lengths
14//!
15//! Both parameters are in the *basis*'s own parameter space: an angle on a
16//! conic or revolved surface, a dimensionless ordinal on a polyline, a length
17//! on most other curves and surfaces. `scale_parameter` (curve) and
18//! `trim_converter` (surface) already encode this per-basis-kind distinction
19//! for trims; both are reused here rather than re-deriving the unit logic.
20
21use axiolid_model::PointOnSurface as KernelPointOnSurface;
22use axiolid_model::{GeometryNode, NodeId, PointOnCurve as KernelPointOnCurve};
23use ifc_model::EntityId;
24
25use crate::error::GeometryResult;
26use crate::lower::curve::{lower_curve_node, scale_parameter};
27use crate::lower::session::LoweringSession;
28use crate::lower::surface::lower_surface_node;
29use crate::resource::point::{PointOnCurve, PointOnSurface};
30use crate::transform::Transform;
31
32/// Family label used for point memoization.
33const KIND: &str = "point";
34
35/// Lower an `IfcPointOnCurve` into a `PointOnCurve` node.
36///
37/// The basis curve is lowered first (through the total curve dispatcher, so
38/// any curve family this crate supports may serve as the basis), then the
39/// parameter is converted into the basis's own unit convention.
40pub fn lower_point_on_curve_node(
41    session: &mut LoweringSession<'_>,
42    id: EntityId,
43    frame: Transform,
44) -> GeometryResult<NodeId> {
45    if let Some(node) = session.memoized(id, KIND, frame) {
46        return Ok(node);
47    }
48    session.enter(id, KIND)?;
49    let result = build_point_on_curve(session, id, frame);
50    session.exit(id);
51    let node = result?;
52    session.memoize(id, KIND, frame, node);
53    Ok(node)
54}
55
56fn build_point_on_curve(
57    session: &mut LoweringSession<'_>,
58    id: EntityId,
59    frame: Transform,
60) -> GeometryResult<NodeId> {
61    let entity = session.entity(id, id)?;
62    let view = PointOnCurve::new(id, entity);
63    let basis_ref = view.basis_curve()?;
64    let raw_parameter = view.point_parameter()?;
65
66    let basis_kind = session.type_name(basis_ref)?;
67    let curve = lower_curve_node(session, basis_ref, frame)?;
68    let parameter = scale_parameter(session, id, &basis_kind, raw_parameter)?;
69
70    session.node_for(
71        id,
72        GeometryNode::PointOnCurve(KernelPointOnCurve { curve, parameter }),
73    )
74}
75
76/// Lower an `IfcPointOnSurface` into a `PointOnSurface` node.
77///
78/// Both `(u, v)` parameters are in the basis surface's own parameter space
79/// and may use different unit conventions on a mixed analytic surface (for
80/// example a cylinder: `u` an angle, `v` a length). `trim_converter` resolves
81/// one factor per direction from the basis kind.
82pub fn lower_point_on_surface_node(
83    session: &mut LoweringSession<'_>,
84    id: EntityId,
85    frame: Transform,
86) -> GeometryResult<NodeId> {
87    if let Some(node) = session.memoized(id, KIND, frame) {
88        return Ok(node);
89    }
90    session.enter(id, KIND)?;
91    let result = build_point_on_surface(session, id, frame);
92    session.exit(id);
93    let node = result?;
94    session.memoize(id, KIND, frame, node);
95    Ok(node)
96}
97
98fn build_point_on_surface(
99    session: &mut LoweringSession<'_>,
100    id: EntityId,
101    frame: Transform,
102) -> GeometryResult<NodeId> {
103    let entity = session.entity(id, id)?;
104    let view = PointOnSurface::new(id, entity);
105    let basis_ref = view.basis_surface()?;
106    let (raw_u, raw_v) = view.parameters()?;
107
108    let surface = lower_surface_node(session, basis_ref, frame)?;
109    let (convert_u, convert_v) = crate::lower::surface::trim_converter(session, id, basis_ref)?;
110    let (u, v) = (convert_u(raw_u), convert_v(raw_v));
111
112    session.node_for(
113        id,
114        GeometryNode::PointOnSurface(KernelPointOnSurface { surface, u, v }),
115    )
116}
117
118#[cfg(test)]
119mod tests;