Skip to content

Transform and reversal are shape traits

Moving and reversing a shape are trait methods, which finishes ADR 0015 for the two families it left inherent. Transformable::transformed moves a 3D curve or surface; ReversibleCurve gives reversed and reversed_parameter to the curves of both tiers; ReversibleSurface gives reversed_u, reversed_v, u_reversed_parameter and v_reversed_parameter to the surfaces. Every concrete type implements them, Bézier curves and surfaces and TrimmedCurve / TrimmedCurve2D included, and the shape enums forward them through their variant lists (ADR 0017), so Curve, ElementaryCurve, Curve2D, Surface and ElementarySurface answer them with no hand-written match. geomcore::prelude brings all three into scope.

Before this decision transformed was inherent on 14 types with 14 return types, absent on the Bézier types, the trimmed curves and every enum, and reversal was inherent on the surfaces and on some curves only. So every caller holding an enum wrote its own match: the swept surfaces dispatched their profile Curve by hand to move it, to reverse it (rebuilding a flipped frame for each conic) and to map its reversed parameter, and the integration tests did the same. Those matches are gone; swept.rs keeps only the profile geometry, and a swept surface moves and reverses its profile through the traits.

They are three small traits rather than methods of ParametricCurve, ParametricCurve2D and ParametricSurface because they return Self. The parametric traits are implemented for a reference to a curve, so a TrimmedCurve can borrow its basis, and the marching search holds a dyn ParametricSurface; neither can return Self. Reversal is one trait for both tiers because its signature has no point type in it. Transform was 3D only at first: the plane tier had no transform type, and none was added until a caller needed one (see the amendment).

One error. Every transform fails for the same two reasons, so there is one TransformError: Collapsed when the transform has a zero scale factor (a Transform is always a similarity, a uniform scale composed with a rigid motion or a mirror, so a zero scale is its only way to collapse a shape), and NonFinite when the image overflows f64. The per-type construction errors the elementary transforms returned named the same two failures in 14 vocabularies (NullNormal, InvalidRadius, ...), and a caller holding a Surface could not match them anyway.

B-splines fail like the rest. BSplineCurve::transformed and BSplineSurface::transformed returned the shape itself, since mapping the poles always yields a valid net. They now return Result and fail where every other shape fails: a zero scale, which used to give a B-spline collapsed onto a point that no other query handled, and an overflow, which used to give infinite poles. The trait could have kept them infallible only by making the elementary transforms panic or by splitting the trait in two; one fallible signature is the interface callers already handle for every other shape.

The reversed parameter. A reversed curve evaluates at reversed_parameter(u) to the point the curve has at u, its derivatives of odd order change sign there, and the map is its own inverse. On bounded parameters it is first + last - u: B-spline and Bézier curves reverse their poles and mirror their knots, and a circle or ellipse flips its frame's y direction, which maps u to 2π - u. An unbounded curve keeps the point at parameter 0 and maps u to -u: a line negates its direction, and a parabola or hyperbola flips its frame's y direction, which keeps the vertex at parameter 0. A trimmed curve reverses its basis and maps its range through the basis's map, so a trimmed line over [a, b] reverses to [-b, -a]. Surfaces follow the same rule per direction: period - u on a periodic elementary direction, -u on an unbounded one, first + last - u on a B-spline or Bézier direction, and a swept surface maps its profile direction as its profile does.

Amendment (2026-11-18): one Transformable for both tiers (#421)

The plane now has its transform, Transform2D: a similarity of the plane (a uniform scale composed with a rigid motion or a mirror across a line), with the constructors, composition, inverse and 3 × 3 homogeneous matrices of Transform. Its callers are a profile drawn in 2D and placed several times, and a curve in a surface's parameter plane mirrored with its face. Every 2D curve moves by one, the Curve2D enum and TrimmedCurve2D included, and so do Point2D, Vector2D, Axis2D and Frame2D; Vector and Axis gain the transformed that Point and Frame already had, so every value of both tiers moves the same way.

Transformable is generic over the tier rather than doubled: it gains an associated type, type Transform: Copy, and transformed takes Self::Transform. A 3D shape names Transform, a 2D curve Transform2D. TierPoint names its tier's transform, so a generic curve type (GenericCircle<P>, GenericTrimmedCurve<P, C>, ...) implements the trait once, with type Transform = P::Transform, through a crate-private TransformOperations trait that gives both transforms apply_point, apply_vector, scale_factor and apply_frame. The errors stay TransformError with its two variants: a similarity of the plane collapses a curve only by a zero scale, as one of space does.

A mirror across a line turns a 2D frame indirect, as a mirror across a plane turns a 3D one, so it turns a counterclockwise Circle2D clockwise (is_counterclockwise is false), and reversing the image turns it back, as reversing a mirrored surface restores its side. A point reflection is a half turn in the plane and keeps the turning sense.

Considered. A separate Transformable2D trait was rejected: the generic curve types would implement two traits from one body through the tier macro, the enums and trimmed curves would each need a second impl, and a function over shapes of either tier, which the associated type allows (fn f<S: Transformable>(s: &S, t: S::Transform)), would be impossible. The associated type breaks one thing: a generic caller that passes a Transform must now say so, C: Transformable<Transform = Transform>. That bound is what such a caller means, and the compiler names it at each site. A type parameter (Transformable<T>) was rejected too: a shape has exactly one transform, and a parameter would let a caller ask for a pairing that does not exist and force a turbofish at call sites the associated type infers.

Considered options

  • Methods on ParametricCurve, ParametricCurve2D and ParametricSurface: rejected. A method returning Self would need where Self: Sized on each, which removes it from dyn ParametricSurface and makes the reference impls of the curve traits answer it with a borrowed curve they cannot build. Small traits keep the parametric traits object-safe and the reference impls simple.
  • Keep each type's construction error: rejected. The failure modes are the same on every type, the enums would need an error enum wrapping 14 others, and Python already raised one ConstructionError for all of them.
  • Keep the B-spline transforms infallible, and the rest fallible: rejected. A trait has one signature; two traits for moving a shape would put the split back in every generic caller.
  • first + last - u for every curve, reparametrizing the unbounded ones: rejected. An unbounded curve has no first + last, and reparametrizing a line or conic would break the frame-based parametrization every closed form relies on. Flipping the frame keeps the reversed shape the same type with the same kind of parametrization.
  • A batch transform over many shapes: unchanged, still rejected (ADR 0002). Transforms stay per shape.

Consequences

Every 3D shape moves and every shape reverses through one interface, so a caller holding any enum, a trimmed curve or a generic C: Transformable moves it without knowing its type; the Python method sets gain transformed (3D curves and surfaces) and reversed / reversed_parameter (every curve) from the same place. A new curve or surface type implements the traits in its file and gets them on the enums from its line in the variant list. Rust code calling transformed or a reversal on a concrete type needs the prelude, and code that matched a per-type transform error matches TransformError instead. RevolutionSurfaceConstructionError::NonFinite, only ever returned by a transform, is deprecated. The per-type doc examples of these methods are replaced by the one on each trait (ADR 0015); the type-specific facts they showed stay in each type's unit tests, and tests/transform_and_reversal.rs checks the contracts on every enum variant and on trimmed curves.