axiolid_evaluate/station.rs
1//! Stations: a position at a distance along a curve, framed by the curve's
2//! section (#241, ADR 0082).
3//!
4//! A station names a place by a measure along a basis curve instead of by
5//! coordinates, the way an alignment is staked out. Reading it back is
6//! curve evaluation, so it lives here, beside the arc-length machinery it
7//! reuses ([`crate::arc_parameter`]).
8//!
9//! # Distance convention
10//!
11//! The distance is measured in the basis curve's own convention, from its
12//! start:
13//!
14//! - an [`Elevated3`](axiolid_curve::Elevated3) or a
15//! [`Banked3`](axiolid_curve::Banked3) is authored against PLAN distance,
16//! its own parameter, and a station on it is a plan distance (the
17//! curve-evaluation contract's `DistanceConvention::PlanDistance`);
18//! - every other curve, 2D or 3D, is measured by its arc length (for a 2D
19//! curve, the plan length).
20//!
21//! The start is parameter `0` for a line, a circle, an ellipse, an
22//! intrinsic curve and a chain, and the first parameter of the domain for
23//! a polyline or a B-spline. A distance must lie in `[0, L]`, `L` the
24//! curve's length ([`station_length2`], [`station_length3`]): one full turn
25//! for a closed conic, no limit for a line. A negative or non-finite
26//! distance, and one past `L` by more than
27//! [`ARC_LENGTH_TOLERANCE`]` * max(1, L)`, is refused by name; one past it
28//! by less is read at `L`.
29//!
30//! # Section frame
31//!
32//! [`SectionFrame`] carries the point and three unit axes: `tangent` along
33//! the curve, `lateral` to the LEFT of it, and `up = tangent x lateral`.
34//!
35//! - A 2D curve lies in the plane `z = 0`: `lateral` is the left normal,
36//! `up` is `+Z`.
37//! - A [`Banked3`](axiolid_curve::Banked3) is framed by its rolled section
38//! ([`crate::banked::banked_section`]): `lateral` towards the left rail
39//! head.
40//! - Every other 3D curve, an [`Elevated3`](axiolid_curve::Elevated3)
41//! included, gets the reference-up frame against `+Z`, the one
42//! `ReferenceCurveEvaluator::frame_at` returns: `lateral` is horizontal,
43//! `up` leans back with the grade. A vertical tangent has no such frame
44//! and is refused by name.
45//!
46//! [`SectionFrame::frame`] returns the axes in the curve-evaluation
47//! provider's layout: `x` the tangent, `y` up, `z = -lateral` (to the
48//! right), the layout the contract's `frame_at` documents (#242).
49//!
50//! The curve-evaluation provider reads a seam side through this module:
51//! `ReferenceCurveEvaluator`'s `point_at_on`, `tangent_at_on` and
52//! `frame_at_on` answer a measure on a seam (below) with
53//! [`station_section3_on`] (#286).
54//!
55//! [`SectionFrame::plan`] is the vertical alternative: the tangent's
56//! horizontal projection, the horizontal left normal and `+Z`, with grade
57//! and bank dropped.
58//!
59//! # Explicit orientation (#246)
60//!
61//! [`SectionFrame::oriented`] turns a section frame by an explicit axis and
62//! reference direction given as components in that frame, `(tangent,
63//! lateral, up)`, not in world coordinates: a linear placement's axes "are
64//! relative to the curve used for linear referencing ..., maintaining the
65//! relationship to the tangent of the curve" (buildingSMART IFC 4.3,
66//! `IfcAxis2PlacementLinear`), whose local `X`, `Y`, `Z` are the tangent,
67//! the left lateral and up. The axis is the oriented up, exact; the
68//! reference direction is made perpendicular to it (Gram-Schmidt, axis
69//! primary) and becomes the oriented tangent. The point does not move.
70//!
71//! # Seams (#263)
72//!
73//! A curve made of pieces has two frames where a polyline turns at a
74//! vertex, a B-spline at a corner knot, an elevated curve's grade breaks
75//! or a banked curve's cant jumps. A station within
76//! [`ARC_LENGTH_TOLERANCE`]` * max(1, s)` of such a seam is ON it: it is
77//! read at the seam's own distance, from the piece that starts there
78//! (`SeamSide::Outgoing`, [`station_section2`] and [`station_section3`])
79//! or from the side [`station_section2_on`] and [`station_section3_on`]
80//! are given. [`station_seams2`] and [`station_seams3`] say where the
81//! seams are without evaluating the curve, and [`Mitre`] is the plane a
82//! run of sections crossing one stands in; see [`seam`] for both. At the
83//! curve's start and end there is one piece, and both sides read it.
84//!
85//! # Placing at a station (#264)
86//!
87//! [`SectionFrame::placement`] is the rigid motion a node placed at a
88//! station is moved by: local `x`, `y`, `z` onto the tangent, the left
89//! lateral and up, the origin onto the point (a linear placement's
90//! reading). [`station_frame_is_exact2`] and [`station_frame_is_exact3`]
91//! say whether that frame is exact (a line) or carries the tolerances
92//! below. [`SectionFrame::carried`] is the frame a station on the placed
93//! curve has (#285): the source's moved where the placement keeps `+Z`,
94//! the placed curve's own reference-up frame where it tilts it.
95//!
96//! # Composite bases (#285)
97//!
98//! [`CompositeBasis`] measures stations along pieces of atomic curves laid
99//! end to end ([`StationPiece`]: a span of a curve, reversed or placed),
100//! the neutral form of a composite curve relation. The distance runs
101//! through the pieces in their common convention, every joint is a seam
102//! read by the rule above, and a frame is exact only where every piece up
103//! to the one read is; see [`composite`].
104//!
105//! # Offset bases (#289)
106//!
107//! A piece may be an offset of a base piece ([`StationOffset`]), measured
108//! in its OWN length from its start: arc length, or its own plan length
109//! beside an elevated or banked curve. [`offset_pieces`] offsets pieces
110//! laid end to end, one offset piece per span between seams; see
111//! [`offset`] for the closed forms, the numerical reading and the
112//! refusals.
113//!
114//! # Certified joint distances and seam-snapping windows (#294)
115//!
116//! A composite's joints lie at the running sum of its pieces' lengths, and
117//! some of those lengths are quadratures (an offset other than a line or a
118//! circle beside one, an ellipse, a B-spline). [`certify`] bounds each from
119//! both sides, so [`CompositeBasis::joint_intervals`] answers every joint
120//! with a [`DistanceInterval`] that contains its true distance, no wider
121//! than a requested tolerance; a sum of stored lengths is a point. The
122//! curve-evaluation provider hands them out as `path_joint_distances`.
123//!
124//! A station may carry a seam-snapping window, wider than the arc-length
125//! tolerance: [`snapped_distance2`], [`snapped_distance3`] and
126//! [`CompositeBasis::snapped_distance`] read a station within its window
127//! of exactly one seam whose frame may jump at that seam, using the
128//! certified intervals where a seam's distance is numerical, and refuse a
129//! window holding two seams or with one on its edge by name; see
130//! [`window`].
131//!
132//! # Accuracy contract
133//!
134//! The point and frame are the exact curve's at a measure within
135//! [`ARC_LENGTH_TOLERANCE`]` * max(1, s)` of `s` where the arc length is
136//! numerical (a conic, a polyline is exact, a B-spline), at `s` itself on a
137//! line, an intrinsic curve, a chain and an elevated or banked curve (their
138//! parameter is the measure), each evaluated to its own stated accuracy
139//! (Gauss-Legendre quadrature for intrinsic positions, ADR 0060). A station
140//! on a seam is read at the seam's distance, within the same tolerance of
141//! its own.
142
143use axiolid_contracts::{GeomError, GeomResult};
144use axiolid_core::{Frame3, Point3, Scalar, Transform3, Vec3};
145use axiolid_curve::{Curve2, Curve3, SeamSide};
146/// The convention a station basis is measured in (#285): the
147/// curve-evaluation contract's, re-exported so a caller can name it.
148pub use axiolid_curve_evaluate_contract::DistanceConvention;
149
150pub mod certify;
151pub mod composite;
152pub mod offset;
153pub mod seam;
154pub mod window;
155
156pub use certify::{DistanceInterval, MAX_CERTIFIED_PANELS, POINT_ROUNDING, UNCERTIFIED_LENGTH};
157pub use composite::{CompositeBasis, StationCurve, StationPiece, JOINT_TOLERANCE};
158pub use offset::{offset_pieces, StationOffset, CUSP_TOLERANCE, OFFSET_TOLERANCE};
159pub use seam::{
160 exact_station_seams2, exact_station_seams3, station_seams2, station_seams3, Mitre, StationSeam,
161 MITRE_TOLERANCE, SEAM_TANGENT_TOLERANCE,
162};
163pub use window::{
164 snapped_distance2, snapped_distance3, SEAM_WINDOW_AMBIGUOUS, SEAM_WINDOW_STRADDLED,
165 WINDOW_CERTIFICATION,
166};
167
168use crate::arc_parameter::{
169 arc_length2, arc_length3, parameter_at_arc_length2, parameter_at_arc_length3,
170 ARC_LENGTH_TOLERANCE,
171};
172use crate::curve::{derivative2, derivative3, domain2, domain3, evaluate2, evaluate3};
173
174/// Largest sine of the angle between an orientation's axis and reference
175/// direction that still counts as parallel ([`SectionFrame::oriented`]);
176/// the graph refuses the same pairs when a station is pushed.
177pub const ORIENTATION_TOLERANCE: Scalar = 1e-9;
178
179/// How far a rigid placement's image of `+Z` may lean and still count as
180/// keeping `+Z` ([`SectionFrame::carried`]): rounding only.
181pub const KEEPS_UP_TOLERANCE: Scalar = 1e-12;
182
183fn invalid(detail: String) -> GeomError {
184 GeomError::InvalidInput(detail)
185}
186
187/// A point on a curve and the section axes there: tangent, left lateral
188/// and up, unit and right-handed (`up = tangent x lateral`).
189#[non_exhaustive]
190#[derive(Debug, Clone, Copy, PartialEq)]
191pub struct SectionFrame {
192 /// The curve point.
193 pub point: Point3,
194 /// Unit tangent, in the direction of increasing distance.
195 pub tangent: Vec3,
196 /// Unit lateral axis, to the left of the tangent.
197 pub lateral: Vec3,
198 /// Unit section up, `tangent x lateral`.
199 pub up: Vec3,
200}
201
202impl SectionFrame {
203 /// The point offset from this frame's point: `lateral` along the
204 /// lateral axis (positive to the left), `vertical` along up and
205 /// `longitudinal` along the tangent.
206 #[must_use]
207 pub fn place(&self, lateral: Scalar, vertical: Scalar, longitudinal: Scalar) -> Point3 {
208 self.point + lateral * self.lateral + vertical * self.up + longitudinal * self.tangent
209 }
210
211 /// The same point framed vertically: the tangent's horizontal
212 /// projection, the horizontal left normal and `+Z`. Grade and bank are
213 /// dropped.
214 ///
215 /// # Errors
216 ///
217 /// A vertical tangent, which has no horizontal projection.
218 pub fn plan(&self) -> GeomResult<Self> {
219 let horizontal = Vec3::new(self.tangent.x, self.tangent.y, 0.0);
220 let length = horizontal.length();
221 if !length.is_finite() || length <= 1e-12 {
222 return Err(invalid(
223 "station: the tangent is vertical, so it has no plan direction".into(),
224 ));
225 }
226 let tangent = horizontal / length;
227 Ok(Self {
228 point: self.point,
229 tangent,
230 lateral: Vec3::new(-tangent.y, tangent.x, 0.0),
231 up: Vec3::Z,
232 })
233 }
234
235 /// The same point with its axes turned by an explicit orientation
236 /// (#246): `axis` and `ref_direction` are components in THIS frame,
237 /// `(tangent, lateral, up)`, defaulting to `(0, 0, 1)` and `(1, 0, 0)`.
238 ///
239 /// Gram-Schmidt with the axis primary: `up' = axis / |axis|`,
240 /// `tangent' = normalise(r - (r . up') up')` for the unit reference
241 /// direction `r`, `lateral' = up' x tangent'`. This is the reading of a
242 /// linear placement's `Axis` and `RefDirection` relative to the curve
243 /// (see the [module documentation](self#explicit-orientation-246)).
244 ///
245 /// # Errors
246 ///
247 /// A zero or non-finite vector, or an axis and reference direction
248 /// parallel or anti-parallel within [`ORIENTATION_TOLERANCE`], by name.
249 pub fn oriented(&self, axis: Option<Vec3>, ref_direction: Option<Vec3>) -> GeomResult<Self> {
250 let unit = |vector: Vec3, what: &str| {
251 let length = vector.length();
252 if vector.is_finite() && length.is_finite() && length > 1e-12 {
253 Ok(vector / length)
254 } else {
255 Err(invalid(format!(
256 "station: the orientation's {what} {vector:?} is zero or not finite"
257 )))
258 }
259 };
260 let axis = unit(axis.unwrap_or(Vec3::Z), "axis")?;
261 let reference = unit(ref_direction.unwrap_or(Vec3::X), "reference direction")?;
262 if axis.cross(reference).length() <= ORIENTATION_TOLERANCE {
263 return Err(invalid(format!(
264 "station: the orientation's axis {axis:?} and reference direction \
265 {reference:?} are parallel"
266 )));
267 }
268 let x = (reference - reference.dot(axis) * axis).normalize();
269 let y = axis.cross(x);
270 let world = |v: Vec3| v.x * self.tangent + v.y * self.lateral + v.z * self.up;
271 Ok(Self {
272 point: self.point,
273 tangent: world(x),
274 lateral: world(y),
275 up: world(axis),
276 })
277 }
278
279 /// The rigid placement this frame stands for (#264): local `x` onto the
280 /// tangent, `y` onto the lateral (to the left), `z` onto up, the local
281 /// origin onto the point.
282 ///
283 /// This is how a linear placement maps its local axes (buildingSMART
284 /// IFC 4.3, `IfcAxis2PlacementLinear`: `X` the tangent, `Y` the left
285 /// lateral, `Z` up), and how a node placed at a station is moved. It is
286 /// not [`Self::frame`]'s provider layout (`y` up, `z` right).
287 #[must_use]
288 pub fn placement(&self) -> Transform3 {
289 Transform3::from_cols(self.tangent, self.lateral, self.up, self.point)
290 }
291
292 /// This frame carried by a rigid motion: the point by `rigid`, the
293 /// axes by its linear part.
294 ///
295 /// A section frame on a curve moved rigidly is the moved curve's own
296 /// only where its convention does not depend on the world: always for
297 /// its tangent, and for the reference-up and plan frames only when
298 /// `rigid` keeps `+Z`; checking that is the caller's.
299 #[must_use]
300 pub fn moved(&self, rigid: Transform3) -> Self {
301 Self {
302 point: rigid.transform_point3(self.point),
303 tangent: rigid.transform_vector3(self.tangent),
304 lateral: rigid.transform_vector3(self.lateral),
305 up: rigid.transform_vector3(self.up),
306 }
307 }
308
309 /// This frame, read on a curve, as the section frame of that curve
310 /// carried by the rigid motion `rigid` (#264, #285): the frame a
311 /// station on the placed curve has, `measure` the convention the
312 /// source curve is measured in.
313 ///
314 /// Where `rigid` keeps `+Z` (within [`KEEPS_UP_TOLERANCE`]) this is
315 /// [`Self::moved`]: the moved reference-up, planar or banked frame is
316 /// the placed curve's own, and plan distance is kept. Where it tilts
317 /// `+Z`, an arc-length-measured curve (arc length is kept by any rigid
318 /// motion) gets the reference-up frame against `+Z` of its moved point
319 /// and tangent, which is the placed 3D curve's own section frame; the
320 /// moved frame would not be.
321 ///
322 /// # Errors
323 ///
324 /// A plan-measured curve (elevated or banked) under a motion that
325 /// tilts `+Z`, whose plan distance and section frame are not the placed
326 /// curve's, by a typed [`GeomError::UnsupportedInput`]; and a moved
327 /// tangent with no reference-up frame (vertical), by name.
328 pub fn carried(&self, rigid: Transform3, measure: DistanceConvention) -> GeomResult<Self> {
329 if (rigid.transform_vector3(Vec3::Z) - Vec3::Z).length() <= KEEPS_UP_TOLERANCE {
330 return Ok(self.moved(rigid));
331 }
332 if measure == DistanceConvention::PlanDistance {
333 return Err(GeomError::UnsupportedInput {
334 backend: axiolid_contracts::BackendId::new("axiolid-evaluate"),
335 operation: axiolid_contracts::Operation::CurveEvaluation,
336 input: PLAN_MEASURED_TILTED,
337 });
338 }
339 reference_up_frame(
340 rigid.transform_point3(self.point),
341 rigid.transform_vector3(self.tangent),
342 )
343 }
344
345 /// The frame in the curve-evaluation provider's layout: `x` the
346 /// tangent, `y` up, `z = x x y`, which is `-lateral` (to the right).
347 #[must_use]
348 pub fn frame(&self) -> Frame3 {
349 Frame3 {
350 origin: self.point,
351 x: self.tangent,
352 y: self.up,
353 z: -self.lateral,
354 }
355 }
356}
357
358/// The refusal of a plan-measured curve placed in a tilted frame.
359const PLAN_MEASURED_TILTED: &str =
360 "a station along an elevated or banked curve placed at a station whose frame tilts +Z: its \
361 plan distance and its section frame carried by the placement are not the placed curve's own \
362 (#264)";
363
364/// A 2D curve's point and unit tangent lifted to `z = 0`, framed with the
365/// left normal and `+Z`.
366fn planar_frame(
367 point: axiolid_core::Point2,
368 tangent: axiolid_core::Vec2,
369) -> GeomResult<SectionFrame> {
370 let length = tangent.length();
371 if !length.is_finite() || length <= 0.0 {
372 return Err(invalid(
373 "station: the curve has no tangent direction there".into(),
374 ));
375 }
376 let t = tangent / length;
377 Ok(SectionFrame {
378 point: Point3::new(point.x, point.y, 0.0),
379 tangent: Vec3::new(t.x, t.y, 0.0),
380 lateral: Vec3::new(-t.y, t.x, 0.0),
381 up: Vec3::Z,
382 })
383}
384
385/// The reference-up frame against `+Z`, as `ReferenceCurveEvaluator`
386/// builds it: `right = t x Z`, `up = right x t`, `lateral = -right`.
387fn reference_up_frame(point: Point3, derivative: Vec3) -> GeomResult<SectionFrame> {
388 let length = derivative.length();
389 if !length.is_finite() || length <= 0.0 {
390 return Err(invalid(
391 "station: the curve has no tangent direction there".into(),
392 ));
393 }
394 let tangent = derivative / length;
395 let right = tangent.cross(Vec3::Z);
396 let magnitude = right.length();
397 if !magnitude.is_finite() || magnitude <= 1e-12 {
398 return Err(invalid(
399 "station: the tangent is vertical, so the section has no lateral axis".into(),
400 ));
401 }
402 let right = right / magnitude;
403 Ok(SectionFrame {
404 point,
405 tangent,
406 lateral: -right,
407 up: right.cross(tangent),
408 })
409}
410
411/// A banked curve's rolled section at plan distance `d`, as a station
412/// frame.
413fn banked_frame(curve: &axiolid_curve::Banked3, d: Scalar) -> GeomResult<SectionFrame> {
414 let section = crate::banked::banked_section(curve, d)?;
415 Ok(SectionFrame {
416 point: section.point,
417 tangent: section.tangent,
418 lateral: section.lateral,
419 up: section.up,
420 })
421}
422
423/// Check `distance` against the curve length `length` (`None`:
424/// unbounded) and return the distance to read, clamped onto `L` within
425/// tolerance.
426fn admitted(distance: Scalar, length: Option<Scalar>) -> GeomResult<Scalar> {
427 if !distance.is_finite() {
428 return Err(invalid(format!(
429 "station: distance {distance} along the curve is not finite"
430 )));
431 }
432 if distance < 0.0 {
433 return Err(invalid(format!(
434 "station: distance {distance} is before the curve's start"
435 )));
436 }
437 match length {
438 None => Ok(distance),
439 Some(length) => {
440 let slack = ARC_LENGTH_TOLERANCE * length.abs().max(1.0);
441 if distance > length + slack {
442 Err(invalid(format!(
443 "station: distance {distance} is beyond the curve's length {length}"
444 )))
445 } else {
446 Ok(distance.min(length))
447 }
448 }
449 }
450}
451
452fn unmeasured(dimension: &str) -> GeomError {
453 invalid(format!(
454 "station: this {dimension} curve family has no distance a station can be measured by"
455 ))
456}
457
458/// Where a 2D curve's measure starts: parameter `0`, or the domain's
459/// first parameter for a polyline or a B-spline.
460fn start2(curve: &Curve2) -> Scalar {
461 match curve {
462 Curve2::Polyline(_) | Curve2::BSpline(_) => {
463 let domain = domain2(curve);
464 domain.start.min(domain.end)
465 }
466 _ => 0.0,
467 }
468}
469
470fn start3(curve: &Curve3) -> Scalar {
471 match curve {
472 Curve3::Polyline(_) | Curve3::BSpline(_) => {
473 let domain = domain3(curve);
474 domain.start.min(domain.end)
475 }
476 _ => 0.0,
477 }
478}
479
480/// The length a station on a 2D curve is measured against: `None` for a
481/// line (unbounded), one turn for a circle or an ellipse, the arc length
482/// of its domain for a polyline or a B-spline, the stated length of an
483/// intrinsic curve or a chain.
484///
485/// # Errors
486///
487/// Any other family, a chain without a valid length, and the quadrature's
488/// refusals, by name.
489pub fn station_length2(curve: &Curve2) -> GeomResult<Option<Scalar>> {
490 match curve {
491 Curve2::Line(_) => Ok(None),
492 Curve2::Circle(_) | Curve2::Ellipse(_) => {
493 arc_length2(curve, 0.0, core::f64::consts::TAU).map(Some)
494 }
495 Curve2::Polyline(_) | Curve2::BSpline(_) => {
496 let domain = domain2(curve);
497 let (lo, hi) = (domain.start.min(domain.end), domain.start.max(domain.end));
498 if hi <= lo {
499 return Err(invalid("station: the curve has an empty domain".into()));
500 }
501 arc_length2(curve, lo, hi).map(Some)
502 }
503 Curve2::Intrinsic(intrinsic) => {
504 if intrinsic.length.is_finite() && intrinsic.length > 0.0 {
505 Ok(Some(intrinsic.length))
506 } else {
507 Err(invalid(
508 "station: the intrinsic curve has no positive finite length".into(),
509 ))
510 }
511 }
512 Curve2::Chain(chain) => chain
513 .length()
514 .map(Some)
515 .ok_or_else(|| invalid("station: the chain has no valid length".into())),
516 _ => Err(unmeasured("2D")),
517 }
518}
519
520/// The length a station on a 3D curve is measured against, as
521/// [`station_length2`]; an elevated curve's is its plan's, a banked
522/// curve's the span of its cant law.
523///
524/// # Errors
525///
526/// As [`station_length2`].
527pub fn station_length3(curve: &Curve3) -> GeomResult<Option<Scalar>> {
528 match curve {
529 Curve3::Line(_) => Ok(None),
530 Curve3::Circle(_) | Curve3::Ellipse(_) => {
531 arc_length3(curve, 0.0, core::f64::consts::TAU).map(Some)
532 }
533 Curve3::Polyline(_) | Curve3::BSpline(_) => {
534 let domain = domain3(curve);
535 let (lo, hi) = (domain.start.min(domain.end), domain.start.max(domain.end));
536 if hi <= lo {
537 return Err(invalid("station: the curve has an empty domain".into()));
538 }
539 arc_length3(curve, lo, hi).map(Some)
540 }
541 Curve3::Intrinsic(intrinsic) => {
542 if intrinsic.length.is_finite() && intrinsic.length > 0.0 {
543 Ok(Some(intrinsic.length))
544 } else {
545 Err(invalid(
546 "station: the intrinsic curve has no positive finite length".into(),
547 ))
548 }
549 }
550 Curve3::Elevated(elevated) => station_length2(&elevated.plan),
551 Curve3::Banked(banked) => {
552 let domain = domain3(curve);
553 if domain.end > domain.start {
554 Ok(Some(domain.end))
555 } else {
556 Err(invalid(format!(
557 "station: the banked curve's cant law has no valid span ({})",
558 banked.span()
559 )))
560 }
561 }
562 _ => Err(unmeasured("3D")),
563 }
564}
565
566/// Whether a station frame on this 2D curve is exact (#264): its point
567/// and axes at the station's distance itself, in closed form, rounding
568/// aside, with no quadrature or root find behind them.
569///
570/// Only a line: on every other family the distance is read by the
571/// arc-length inverse ([`ARC_LENGTH_TOLERANCE`], an estimate) or the point
572/// by quadrature (an intrinsic curve, a chain), so the frame is not exact
573/// and a placement in it must not be reported as exact.
574#[must_use]
575pub fn station_frame_is_exact2(curve: &Curve2) -> bool {
576 matches!(curve, Curve2::Line(_))
577}
578
579/// [`station_frame_is_exact2`] for a 3D curve: only a line.
580#[must_use]
581pub fn station_frame_is_exact3(curve: &Curve3) -> bool {
582 matches!(curve, Curve3::Line(_))
583}
584
585/// The section of a 2D curve at arc length `distance` from its start; see
586/// the [module documentation](self) for the conventions.
587///
588/// # Errors
589///
590/// A distance that is not finite, negative or beyond the curve's length,
591/// an unmeasurable family, and the evaluators' refusals, by name.
592pub fn station_section2(curve: &Curve2, distance: Scalar) -> GeomResult<SectionFrame> {
593 station_section2_on(curve, distance, SeamSide::Outgoing)
594}
595
596/// [`station_section2`] reading `side` of a seam the station lies on (see
597/// the [module documentation](self#seams-263)); off a seam the side does
598/// not matter.
599///
600/// # Errors
601///
602/// As [`station_section2`].
603pub fn station_section2_on(
604 curve: &Curve2,
605 distance: Scalar,
606 side: SeamSide,
607) -> GeomResult<SectionFrame> {
608 let distance = admitted(distance, station_length2(curve)?)?;
609 if let Some(section) = seam::seam_section2(curve, distance, side)? {
610 return Ok(section);
611 }
612 let t = parameter_at_arc_length2(curve, start2(curve), distance)?;
613 planar_frame(evaluate2(curve, t)?, derivative2(curve, t)?)
614}
615
616/// Whether a station at `distance` along a 3D curve lies ON a seam it
617/// reads two ways, by the rule [`station_section3_on`] applies (#286):
618/// the curve-evaluation provider's sided queries read a station there
619/// through it and answer every other measure side-lessly.
620///
621/// A distance outside `[0, L]` lies on no interior seam: `false`, so the
622/// side-less query keeps its own refusal or answer there.
623///
624/// # Errors
625///
626/// A curve whose length or seams cannot be read, by name.
627pub(crate) fn station_on_seam3(curve: &Curve3, distance: Scalar) -> GeomResult<bool> {
628 if !seam::sided3(curve) {
629 return Ok(false);
630 }
631 let Ok(distance) = admitted(distance, station_length3(curve)?) else {
632 return Ok(false);
633 };
634 Ok(seam::seam_at3(curve, distance)?.is_some())
635}
636
637/// The section of a 3D curve at `distance` from its start: plan distance
638/// on an elevated or banked curve, arc length on any other; see the
639/// [module documentation](self).
640///
641/// # Errors
642///
643/// As [`station_section2`], and a vertical tangent where the reference-up
644/// frame is built.
645pub fn station_section3(curve: &Curve3, distance: Scalar) -> GeomResult<SectionFrame> {
646 station_section3_on(curve, distance, SeamSide::Outgoing)
647}
648
649/// [`station_section3`] reading `side` of a seam the station lies on (see
650/// the [module documentation](self#seams-263)); off a seam the side does
651/// not matter.
652///
653/// # Errors
654///
655/// As [`station_section3`].
656pub fn station_section3_on(
657 curve: &Curve3,
658 distance: Scalar,
659 side: SeamSide,
660) -> GeomResult<SectionFrame> {
661 let distance = admitted(distance, station_length3(curve)?)?;
662 if let Some(section) = seam::seam_section3(curve, distance, side)? {
663 return Ok(section);
664 }
665 match curve {
666 Curve3::Banked(banked) => banked_frame(banked, distance),
667 Curve3::Elevated(elevated) => reference_up_frame(
668 crate::arc_length::elevated_point(elevated, distance)?,
669 crate::arc_length::elevated_tangent(elevated, distance)?,
670 ),
671 _ => {
672 let t = parameter_at_arc_length3(curve, start3(curve), distance)?;
673 reference_up_frame(evaluate3(curve, t)?, derivative3(curve, t)?)
674 }
675 }
676}