affn 0.10.0

Affine geometry primitives: strongly-typed coordinate systems, reference frames, and centers for scientific computing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
//! # Typed inter-reference-system transforms
//!
//! This module wraps the pure affine operators in [`crate::ops`] with
//! compile-time source and destination reference-system tags:
//!
//! ```text
//! Transform<FromCenter, FromFrame, ToCenter, ToFrame, Op>
//! ```
//!
//! where `Op` is typically [`Rotation3`], [`Translation3`], or [`Isometry3`].
//!
//! ## Why this exists
//!
//! Bare operators intentionally preserve `Position` center/frame tags; callers
//! re-tag after the fact. That is correct for low-level math, but it cannot
//! enforce that an `A → B` transform is only applied to an `A` source, nor that
//! `A → B` composes only with `B → C`.
//!
//! [`Transform`] encodes those relationships in the type system so that:
//!
//! - application requires a matching source center/frame;
//! - the result is tagged with the destination center/frame;
//! - compatible transforms compose (`A → B` then `B → C` ⇒ `A → C`);
//! - incompatible compositions fail to compile.
//!
//! The module is domain-agnostic. Downstream crates decide *which* rotation or
//! translation is required (epochs, models, ephemerides, …); `affn` only
//! represents and safely composes *how* two reference systems relate.
//!
//! ## Valid operator shapes
//!
//! | Operator | Meaning | Transform shape |
//! |----------|---------|-----------------|
//! | [`Rotation3`] | Frame-only (same center) | `Transform<C, F1, C, F2, Rotation3>` |
//! | [`Translation3`] | Center-only (same frame) | `Transform<C1, F, C2, F, Translation3<U>>` |
//! | [`Isometry3`] | Center and frame | `Transform<C1, F1, C2, F2, Isometry3<U>>` |
//!
//! Only these shapes can be constructed via [`Transform::new`]. Invalid
//! combinations (for example `Rotation3` with different source and destination
//! centers) are rejected at compile time.
//!
//! Center-changing transforms require [`AffineCenter`] on both endpoints
//! (centers marked `#[center(affine = false)]` cannot be used).
//!
//! ## Parameterized centers
//!
//! [`ReferenceCenter::Params`] may be non-trivial. Producing a
//! `Position<ToCenter, …>` requires knowing `ToCenter::Params`.
//!
//! **MVP limitation:** center-changing transforms
//! ([`Translation3`] / [`Isometry3`]) require `Params = ()` on both centers.
//! Frame-only transforms ([`Rotation3`]) preserve `center_params` and work
//! with any [`ReferenceCenter`], including parameterized centers.
//!
//! ## Example
//!
//! ```rust
//! use affn::cartesian::Position;
//! use affn::ops::{Rotation3, Translation3};
//! use affn::prelude::*;
//! use affn::transform::Transform;
//! use qtty::angular::Radians;
//! use qtty::units::Meter;
//! use core::f64::consts::FRAC_PI_2;
//!
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FrameA;
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FrameB;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct CenterA;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct CenterB;
//!
//! // Frame-only: CenterA stays CenterA; FrameA → FrameB.
//! let frame_tf = Transform::<CenterA, FrameA, CenterA, FrameB, Rotation3>::new(
//!     Rotation3::rz(Radians::new(FRAC_PI_2)),
//! );
//!
//! // Center-only: FrameA stays FrameA; CenterA → CenterB.
//! let center_tf =
//!     Transform::<CenterA, FrameA, CenterB, FrameA, Translation3<Meter>>::new(
//!         Translation3::new(1.0, 0.0, 0.0),
//!     );
//!
//! let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 0.0, 0.0);
//! let after_frame: Position<CenterA, FrameB, Meter> = frame_tf.apply(pos);
//! assert!((after_frame.x().value()).abs() < 1e-12);
//! assert!((after_frame.y().value() - 1.0).abs() < 1e-12);
//!
//! let after_center: Position<CenterB, FrameA, Meter> = center_tf.apply(pos);
//! assert!((after_center.x().value() - 2.0).abs() < 1e-12);
//! ```
//!
//! Invalid source tags fail to compile:
//!
//! ```compile_fail
//! use affn::cartesian::Position;
//! use affn::ops::Rotation3;
//! use affn::prelude::*;
//! use affn::transform::Transform;
//! use qtty::units::Meter;
//!
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FrameA;
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FrameB;
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FrameC;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct Origin;
//!
//! let tf = Transform::<Origin, FrameA, Origin, FrameB, Rotation3>::new(
//!     Rotation3::IDENTITY,
//! );
//! let wrong = Position::<Origin, FrameC, Meter>::new(1.0, 0.0, 0.0);
//! // FrameC is not FrameA — this must not compile:
//! let _ = tf.apply(wrong);
//! ```
//!
//! `Rotation3` cannot be constructed with different source and destination centers:
//!
//! ```compile_fail
//! use affn::ops::Rotation3;
//! use affn::prelude::*;
//! use affn::transform::Transform;
//!
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct F1;
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct F2;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct A;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct B;
//!
//! let _ = Transform::<A, F1, B, F2, Rotation3>::new(Rotation3::IDENTITY);
//! ```
//!
//! `Translation3` cannot be constructed with different frames:
//!
//! ```compile_fail
//! use affn::ops::Translation3;
//! use affn::prelude::*;
//! use affn::transform::Transform;
//! use qtty::units::Meter;
//!
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct F1;
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct F2;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct A;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct B;
//!
//! let _ = Transform::<A, F1, B, F2, Translation3<Meter>>::new(Translation3::new(
//!     0.0, 0.0, 0.0,
//! ));
//! ```
//!
//! Non-[`AffineCenter`] types cannot be used in center-changing transforms:
//!
//! ```compile_fail
//! use affn::ops::Translation3;
//! use affn::prelude::*;
//! use affn::transform::Transform;
//! use qtty::units::Meter;
//!
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct F;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct A;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! #[center(affine = false)]
//! struct NonAffine;
//!
//! let _ = Transform::<A, F, NonAffine, F, Translation3<Meter>>::new(Translation3::new(
//!     0.0, 0.0, 0.0,
//! ));
//! ```
//!
//! Incompatible composition fails to compile:
//!
//! ```compile_fail
//! use affn::ops::Rotation3;
//! use affn::prelude::*;
//! use affn::transform::Transform;
//!
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FA;
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FB;
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FC;
//! #[derive(Debug, Copy, Clone, ReferenceFrame)]
//! struct FD;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct A;
//! #[derive(Debug, Copy, Clone, ReferenceCenter)]
//! struct C;
//!
//! let ab = Transform::<A, FA, A, FB, Rotation3>::new(Rotation3::IDENTITY);
//! let cd = Transform::<C, FC, C, FD, Rotation3>::new(Rotation3::IDENTITY);
//! // A/FB does not match C/FC — this must not compile:
//! let _ = ab.then(cd);
//! ```

mod compose;

pub use compose::ComposeAfter;

use crate::cartesian::Position;
use crate::centers::{AffineCenter, ReferenceCenter};
use crate::frames::ReferenceFrame;
use crate::ops::{Isometry3, Rotation3, Translation3};
use core::marker::PhantomData;
use core::ops::Mul;
use qtty::length::LengthUnit;
use qtty::Quantity;

/// Phantom carrier for the four reference-system tags on [`Transform`].
///
/// Uses `fn() -> …` so the markers do not affect `Send`/`Sync` of the transform.
type SystemMarker<FromCenter, FromFrame, ToCenter, ToFrame> =
    PhantomData<fn() -> (FromCenter, FromFrame, ToCenter, ToFrame)>;

/// A typed transform between two reference systems.
///
/// The type parameters encode the source and destination systems; `Op` is the
/// underlying affine operator ([`Rotation3`], [`Translation3`], or
/// [`Isometry3`]).
///
/// Source/destination tags are zero-cost phantom data. See the [module-level
/// documentation](self) for application rules, composition, and the
/// parameterized-center limitation.
#[derive(Debug, Clone, Copy)]
pub struct Transform<FromCenter, FromFrame, ToCenter, ToFrame, Op> {
    op: Op,
    _marker: SystemMarker<FromCenter, FromFrame, ToCenter, ToFrame>,
}

impl<FromCenter, FromFrame, ToCenter, ToFrame, Op> PartialEq
    for Transform<FromCenter, FromFrame, ToCenter, ToFrame, Op>
where
    Op: PartialEq,
{
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.op == other.op
    }
}

/// Frame-only transform: same center, `FromFrame → ToFrame`, operator [`Rotation3`].
pub type FrameTransform<C, FromFrame, ToFrame> = Transform<C, FromFrame, C, ToFrame, Rotation3>;

/// Center-only transform: same frame, `FromCenter → ToCenter`, operator [`Translation3`].
pub type CenterTransform<FromCenter, ToCenter, F, U> =
    Transform<FromCenter, F, ToCenter, F, Translation3<U>>;

/// Rigid (center + frame) transform with operator [`Isometry3`].
pub type RigidTransform<FromCenter, FromFrame, ToCenter, ToFrame, U> =
    Transform<FromCenter, FromFrame, ToCenter, ToFrame, Isometry3<U>>;

impl<FromCenter, FromFrame, ToCenter, ToFrame, Op>
    Transform<FromCenter, FromFrame, ToCenter, ToFrame, Op>
{
    /// Internal constructor for composition and other in-crate paths where the
    /// shape invariant is already established by the input transforms.
    #[inline]
    const fn from_op_unchecked(op: Op) -> Self {
        Self {
            op,
            _marker: PhantomData,
        }
    }

    /// Returns a reference to the underlying affine operator.
    #[inline]
    #[must_use]
    pub const fn op(&self) -> &Op {
        &self.op
    }

    /// Consumes the transform and returns the underlying affine operator.
    #[inline]
    #[must_use]
    pub fn into_op(self) -> Op {
        self.op
    }

    /// Composes `self` with `next`, applying `self` first, then `next`.
    ///
    /// Type-level requirement: `next`'s source system must equal `self`'s
    /// destination system. The resulting transform is `From → next.To` with an
    /// operator produced by composing the two `Op` values.
    ///
    /// Mathematically this follows the existing operator convention
    /// `result = next * previous` (apply previous, then next). Mixed
    /// rotation/translation pairs promote to [`Isometry3`].
    #[inline]
    #[must_use]
    pub fn then<NextCenter, NextFrame, NextOp>(
        self,
        next: Transform<ToCenter, ToFrame, NextCenter, NextFrame, NextOp>,
    ) -> Transform<FromCenter, FromFrame, NextCenter, NextFrame, NextOp::Output>
    where
        NextOp: ComposeAfter<Op>,
    {
        Transform::from_op_unchecked(next.op.after(self.op))
    }
}

// =============================================================================
// Public constructors (valid shapes only)
// =============================================================================

impl<C, F1, F2> Transform<C, F1, C, F2, Rotation3>
where
    C: ReferenceCenter,
    F1: ReferenceFrame,
    F2: ReferenceFrame,
{
    /// Frame-only transform: same center `C`, rotation `F1 → F2`.
    ///
    /// Call as `Transform::<C, F1, C, F2, Rotation3>::new(op)` (or bind to a
    /// [`FrameTransform`] alias) so the compiler selects this constructor.
    #[inline]
    #[must_use]
    pub const fn new(op: Rotation3) -> Self {
        Self::from_op_unchecked(op)
    }
}

impl<C1, C2, F, U> Transform<C1, F, C2, F, Translation3<U>>
where
    C1: AffineCenter<Params = ()>,
    C2: AffineCenter<Params = ()>,
    F: ReferenceFrame,
    U: LengthUnit,
{
    /// Center-only transform: same frame `F`, translation `C1 → C2`.
    ///
    /// Requires [`AffineCenter`] on both centers. Use
    /// `Transform::<C1, F, C2, F, Translation3<U>>::new(op)`.
    #[inline]
    #[must_use]
    pub const fn new(op: Translation3<U>) -> Self {
        Self::from_op_unchecked(op)
    }
}

impl<C1, F1, C2, F2, U> Transform<C1, F1, C2, F2, Isometry3<U>>
where
    C1: AffineCenter<Params = ()>,
    C2: AffineCenter<Params = ()>,
    F1: ReferenceFrame,
    F2: ReferenceFrame,
    U: LengthUnit,
{
    /// Rigid transform: rotation and translation `C1/F1 → C2/F2`.
    ///
    /// Requires [`AffineCenter`] on both centers. Use
    /// `Transform::<C1, F1, C2, F2, Isometry3<U>>::new(op)`.
    #[inline]
    #[must_use]
    pub const fn new(op: Isometry3<U>) -> Self {
        Self::from_op_unchecked(op)
    }
}

// =============================================================================
// Application: Rotation3 (frame-only; preserves center params)
// =============================================================================

impl<C, F1, F2> Transform<C, F1, C, F2, Rotation3>
where
    C: ReferenceCenter,
    F1: ReferenceFrame,
    F2: ReferenceFrame,
{
    /// Applies this frame transform to a position in the source system.
    ///
    /// Center parameters are preserved. The result is tagged with `F2`.
    #[inline]
    #[must_use]
    pub fn apply<U: LengthUnit>(self, pos: Position<C, F1, U>) -> Position<C, F2, U>
    where
        C::Params: Clone,
    {
        (self.op * pos).reinterpret_frame()
    }

    /// Returns the inverse frame transform (`F2 → F1`, same center).
    #[inline]
    #[must_use]
    pub fn inverse(self) -> Transform<C, F2, C, F1, Rotation3> {
        Transform::<C, F2, C, F1, Rotation3>::from_op_unchecked(self.op.inverse())
    }
}

impl<C, F1, F2, U> Mul<Position<C, F1, U>> for Transform<C, F1, C, F2, Rotation3>
where
    C: ReferenceCenter,
    C::Params: Clone,
    F1: ReferenceFrame,
    F2: ReferenceFrame,
    U: LengthUnit,
{
    type Output = Position<C, F2, U>;

    #[inline]
    fn mul(self, rhs: Position<C, F1, U>) -> Self::Output {
        self.apply(rhs)
    }
}

// =============================================================================
// Application: Translation3 (center-only; Params = () only)
// =============================================================================

impl<C1, C2, F, U> Transform<C1, F, C2, F, Translation3<U>>
where
    C1: AffineCenter<Params = ()>,
    C2: AffineCenter<Params = ()>,
    F: ReferenceFrame,
    U: LengthUnit,
{
    /// Applies this center transform to a position in the source system.
    ///
    /// Restricted to centers with `Params = ()` so destination parameters are
    /// never manufactured or discarded. See the [module docs](self).
    #[inline]
    #[must_use]
    pub fn apply(self, pos: Position<C1, F, U>) -> Position<C2, F, U> {
        let [x, y, z] = self
            .op
            .apply_array([pos.x().value(), pos.y().value(), pos.z().value()]);
        Position::new(
            Quantity::<U>::new(x),
            Quantity::<U>::new(y),
            Quantity::<U>::new(z),
        )
    }

    /// Returns the inverse center transform (`C2 → C1`, same frame).
    #[inline]
    #[must_use]
    pub fn inverse(self) -> Transform<C2, F, C1, F, Translation3<U>> {
        Transform::<C2, F, C1, F, Translation3<U>>::from_op_unchecked(self.op.inverse())
    }
}

impl<C1, C2, F, U> Mul<Position<C1, F, U>> for Transform<C1, F, C2, F, Translation3<U>>
where
    C1: AffineCenter<Params = ()>,
    C2: AffineCenter<Params = ()>,
    F: ReferenceFrame,
    U: LengthUnit,
{
    type Output = Position<C2, F, U>;

    #[inline]
    fn mul(self, rhs: Position<C1, F, U>) -> Self::Output {
        self.apply(rhs)
    }
}

// =============================================================================
// Application: Isometry3 (center + frame; Params = () only)
// =============================================================================

impl<C1, F1, C2, F2, U> Transform<C1, F1, C2, F2, Isometry3<U>>
where
    C1: AffineCenter<Params = ()>,
    C2: AffineCenter<Params = ()>,
    F1: ReferenceFrame,
    F2: ReferenceFrame,
    U: LengthUnit,
{
    /// Applies this rigid transform to a position in the source system.
    ///
    /// Restricted to centers with `Params = ()`. See the [module docs](self).
    #[inline]
    #[must_use]
    pub fn apply(self, pos: Position<C1, F1, U>) -> Position<C2, F2, U> {
        let [x, y, z] = self
            .op
            .apply_point([pos.x().value(), pos.y().value(), pos.z().value()]);
        Position::new(
            Quantity::<U>::new(x),
            Quantity::<U>::new(y),
            Quantity::<U>::new(z),
        )
    }

    /// Returns the inverse rigid transform.
    #[inline]
    #[must_use]
    pub fn inverse(self) -> Transform<C2, F2, C1, F1, Isometry3<U>> {
        Transform::<C2, F2, C1, F1, Isometry3<U>>::from_op_unchecked(self.op.inverse())
    }
}

impl<C1, F1, C2, F2, U> Mul<Position<C1, F1, U>> for Transform<C1, F1, C2, F2, Isometry3<U>>
where
    C1: AffineCenter<Params = ()>,
    C2: AffineCenter<Params = ()>,
    F1: ReferenceFrame,
    F2: ReferenceFrame,
    U: LengthUnit,
{
    type Output = Position<C2, F2, U>;

    #[inline]
    fn mul(self, rhs: Position<C1, F1, U>) -> Self::Output {
        self.apply(rhs)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{DeriveReferenceCenter as ReferenceCenter, DeriveReferenceFrame as ReferenceFrame};
    use core::f64::consts::FRAC_PI_2;
    use qtty::angular::Radians;
    use qtty::units::{Kilometer, Meter};

    const EPSILON: f64 = 1e-12;

    fn make_rot<C, F1, F2>(op: Rotation3) -> Transform<C, F1, C, F2, Rotation3>
    where
        C: ReferenceCenter,
        F1: ReferenceFrame,
        F2: ReferenceFrame,
    {
        Transform::<C, F1, C, F2, Rotation3>::new(op)
    }

    fn make_trans<C1, C2, F, U>(op: Translation3<U>) -> Transform<C1, F, C2, F, Translation3<U>>
    where
        C1: AffineCenter<Params = ()>,
        C2: AffineCenter<Params = ()>,
        F: ReferenceFrame,
        U: LengthUnit,
    {
        Transform::<C1, F, C2, F, Translation3<U>>::new(op)
    }

    fn make_iso<C1, F1, C2, F2, U>(op: Isometry3<U>) -> Transform<C1, F1, C2, F2, Isometry3<U>>
    where
        C1: AffineCenter<Params = ()>,
        C2: AffineCenter<Params = ()>,
        F1: ReferenceFrame,
        F2: ReferenceFrame,
        U: LengthUnit,
    {
        Transform::<C1, F1, C2, F2, Isometry3<U>>::new(op)
    }

    #[derive(Debug, Copy, Clone, ReferenceFrame)]
    struct FrameA;
    #[derive(Debug, Copy, Clone, ReferenceFrame)]
    struct FrameB;
    #[derive(Debug, Copy, Clone, ReferenceFrame)]
    struct FrameC;

    #[derive(Debug, Copy, Clone, ReferenceCenter)]
    struct CenterA;
    #[derive(Debug, Copy, Clone, ReferenceCenter)]
    struct CenterB;
    #[derive(Debug, Copy, Clone, ReferenceCenter)]
    struct CenterC;

    #[derive(Clone, Debug, Default, PartialEq)]
    struct Site {
        id: i32,
    }

    #[derive(Debug, Copy, Clone, ReferenceCenter)]
    #[center(params = Site)]
    struct ParamCenter;

    fn assert_xyz_eq<C: crate::centers::ReferenceCenter, F: crate::frames::ReferenceFrame, U>(
        pos: &Position<C, F, U>,
        x: f64,
        y: f64,
        z: f64,
    ) where
        U: LengthUnit,
    {
        assert!(
            (pos.x().value() - x).abs() < EPSILON
                && (pos.y().value() - y).abs() < EPSILON
                && (pos.z().value() - z).abs() < EPSILON,
            "got ({}, {}, {}), expected ({x}, {y}, {z})",
            pos.x().value(),
            pos.y().value(),
            pos.z().value(),
        );
    }

    #[test]
    fn frame_only_transform() {
        let tf: FrameTransform<CenterA, FrameA, FrameB> =
            make_rot(Rotation3::rz(Radians::new(FRAC_PI_2)));
        let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 0.0, 0.0);
        let out = tf.apply(pos);
        assert_xyz_eq(&out, 0.0, 1.0, 0.0);
    }

    #[test]
    fn center_only_transform() {
        let tf: CenterTransform<CenterA, CenterB, FrameA, Meter> =
            make_trans(Translation3::new(1.0, 2.0, 3.0));
        let pos = Position::<CenterA, FrameA, Meter>::new(10.0, 20.0, 30.0);
        let out = tf.apply(pos);
        assert_xyz_eq(&out, 11.0, 22.0, 33.0);
    }

    #[test]
    fn combined_center_and_frame_transform() {
        let rot = Rotation3::rz(Radians::new(FRAC_PI_2));
        let trans = Translation3::<Meter>::new(10.0, 0.0, 0.0);
        let tf: RigidTransform<CenterA, FrameA, CenterB, FrameB, Meter> =
            make_iso(Isometry3::new(rot, trans));
        let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 0.0, 0.0);
        // R*(1,0,0)=(0,1,0), then +t → (10,1,0)
        let out = tf.apply(pos);
        assert_xyz_eq(&out, 10.0, 1.0, 0.0);
    }

    #[test]
    fn rotation_composition() {
        let ab: FrameTransform<CenterA, FrameA, FrameB> =
            make_rot(Rotation3::rz(Radians::new(FRAC_PI_2)));
        let bc: FrameTransform<CenterA, FrameB, FrameC> =
            make_rot(Rotation3::rz(Radians::new(FRAC_PI_2)));
        let ac = ab.then(bc);

        let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 0.0, 0.0);
        let sequential = bc.apply(ab.apply(pos));
        let composed = ac.apply(pos);
        assert_xyz_eq(&sequential, -1.0, 0.0, 0.0);
        assert_xyz_eq(&composed, -1.0, 0.0, 0.0);
    }

    #[test]
    fn translation_composition() {
        let ab: CenterTransform<CenterA, CenterB, FrameA, Meter> =
            make_trans(Translation3::new(1.0, 0.0, 0.0));
        let bc: CenterTransform<CenterB, CenterC, FrameA, Meter> =
            make_trans(Translation3::new(0.0, 2.0, 0.0));
        let ac = ab.then(bc);

        let pos = Position::<CenterA, FrameA, Meter>::new(0.0, 0.0, 0.0);
        let sequential = bc.apply(ab.apply(pos));
        let composed = ac.apply(pos);
        assert_xyz_eq(&sequential, 1.0, 2.0, 0.0);
        assert_xyz_eq(&composed, 1.0, 2.0, 0.0);
    }

    #[test]
    fn rotation_then_translation_composition() {
        // Apply rotation (frame A→B, same center), then translation (center A→B in frame B).
        let rot: FrameTransform<CenterA, FrameA, FrameB> =
            make_rot(Rotation3::rz(Radians::new(FRAC_PI_2)));
        let trans: CenterTransform<CenterA, CenterB, FrameB, Meter> =
            make_trans(Translation3::new(10.0, 0.0, 0.0));
        let composed = rot.then(trans);

        let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 0.0, 0.0);
        let sequential = trans.apply(rot.apply(pos));
        let via_composed = composed.apply(pos);
        assert_xyz_eq(&sequential, 10.0, 1.0, 0.0);
        assert_xyz_eq(&via_composed, 10.0, 1.0, 0.0);
    }

    #[test]
    fn translation_then_rotation_composition() {
        // Apply translation (center A→B in frame A), then rotation (frame A→B).
        let trans: CenterTransform<CenterA, CenterB, FrameA, Meter> =
            make_trans(Translation3::new(1.0, 0.0, 0.0));
        let rot: FrameTransform<CenterB, FrameA, FrameB> =
            make_rot(Rotation3::rz(Radians::new(FRAC_PI_2)));
        let composed = trans.then(rot);

        let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 0.0, 0.0);
        // sequential: translate (1,0,0)+(1,0,0)=(2,0,0), rotate → (0,2,0)
        let sequential = rot.apply(trans.apply(pos));
        let via_composed = composed.apply(pos);
        assert_xyz_eq(&sequential, 0.0, 2.0, 0.0);
        assert_xyz_eq(&via_composed, 0.0, 2.0, 0.0);
    }

    #[test]
    fn isometry_composition() {
        let ab: RigidTransform<CenterA, FrameA, CenterB, FrameB, Meter> = make_iso(Isometry3::new(
            Rotation3::rz(Radians::new(FRAC_PI_2)),
            Translation3::new(1.0, 0.0, 0.0),
        ));
        let bc: RigidTransform<CenterB, FrameB, CenterC, FrameC, Meter> = make_iso(Isometry3::new(
            Rotation3::rx(Radians::new(FRAC_PI_2)),
            Translation3::new(0.0, 1.0, 0.0),
        ));
        let ac = ab.then(bc);

        let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 2.0, 3.0);
        let sequential = bc.apply(ab.apply(pos));
        let composed = ac.apply(pos);
        assert!((sequential.x().value() - composed.x().value()).abs() < EPSILON);
        assert!((sequential.y().value() - composed.y().value()).abs() < EPSILON);
        assert!((sequential.z().value() - composed.z().value()).abs() < EPSILON);
    }

    #[test]
    fn sequential_equals_composed_mixed_ops() {
        let r1: FrameTransform<CenterA, FrameA, FrameB> =
            make_rot(Rotation3::ry(Radians::new(0.3)));
        let t: CenterTransform<CenterA, CenterB, FrameB, Meter> =
            make_trans(Translation3::new(0.5, -1.0, 2.0));
        let r2: FrameTransform<CenterB, FrameB, FrameC> =
            make_rot(Rotation3::rx(Radians::new(-0.7)));

        let composed = r1.then(t).then(r2);
        let pos = Position::<CenterA, FrameA, Meter>::new(1.25, -0.5, 3.0);
        let sequential = r2.apply(t.apply(r1.apply(pos)));
        let via_composed = composed.apply(pos);
        assert!((sequential.x().value() - via_composed.x().value()).abs() < EPSILON);
        assert!((sequential.y().value() - via_composed.y().value()).abs() < EPSILON);
        assert!((sequential.z().value() - via_composed.z().value()).abs() < EPSILON);
    }

    #[test]
    fn frame_only_preserves_parameterized_center_params() {
        let site = Site { id: 42 };
        let pos =
            Position::<ParamCenter, FrameA, Meter>::new_with_params(site.clone(), 1.0, 0.0, 0.0);
        let tf: FrameTransform<ParamCenter, FrameA, FrameB> =
            make_rot(Rotation3::rz(Radians::new(FRAC_PI_2)));
        let out = tf.apply(pos);
        assert_eq!(out.center_params(), &site);
        assert_xyz_eq(&out, 0.0, 1.0, 0.0);
    }

    #[test]
    fn units_are_compile_time_checked_and_preserved() {
        let tf: CenterTransform<CenterA, CenterB, FrameA, Kilometer> =
            make_trans(Translation3::new(1.5, 0.0, 0.0));
        let pos = Position::<CenterA, FrameA, Kilometer>::new(2.0, 0.0, 0.0);
        let out = tf.apply(pos);
        assert_xyz_eq(&out, 3.5, 0.0, 0.0);
        // to_unit still works on the result
        let meters: Position<CenterB, FrameA, Meter> = out.to_unit();
        assert!((meters.x().value() - 3500.0).abs() < EPSILON);
    }

    #[test]
    fn mul_operator_matches_apply() {
        let tf: FrameTransform<CenterA, FrameA, FrameB> =
            make_rot(Rotation3::rz(Radians::new(FRAC_PI_2)));
        let pos = Position::<CenterA, FrameA, Meter>::new(0.0, 1.0, 0.0);
        let via_apply = tf.apply(pos);
        let via_mul = tf * pos;
        assert_xyz_eq(&via_apply, -1.0, 0.0, 0.0);
        assert_xyz_eq(&via_mul, -1.0, 0.0, 0.0);
    }

    #[test]
    fn inverse_roundtrip_frame() {
        let tf: FrameTransform<CenterA, FrameA, FrameB> =
            make_rot(Rotation3::rz(Radians::new(0.42)));
        let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 2.0, 3.0);
        let roundtrip = tf.inverse().apply(tf.apply(pos));
        assert_xyz_eq(&roundtrip, 1.0, 2.0, 3.0);
    }

    #[test]
    fn inverse_roundtrip_center() {
        let tf: CenterTransform<CenterA, CenterB, FrameA, Meter> =
            make_trans(Translation3::new(1.0, 2.0, 3.0));
        let pos = Position::<CenterA, FrameA, Meter>::new(4.0, 5.0, 6.0);
        let roundtrip = tf.inverse().apply(tf.apply(pos));
        assert_xyz_eq(&roundtrip, 4.0, 5.0, 6.0);
    }

    #[test]
    fn inverse_roundtrip_rigid() {
        let tf: RigidTransform<CenterA, FrameA, CenterB, FrameB, Meter> = make_iso(Isometry3::new(
            Rotation3::rz(Radians::new(FRAC_PI_2)),
            Translation3::new(0.0, 1.0, 0.0),
        ));
        let pos = Position::<CenterA, FrameA, Meter>::new(1.0, 0.0, 0.0);
        let roundtrip = tf.inverse().apply(tf.apply(pos));
        assert_xyz_eq(&roundtrip, 1.0, 0.0, 0.0);
    }

    #[test]
    fn into_op_and_op_accessors() {
        let rot = Rotation3::rz(Radians::new(FRAC_PI_2));
        let tf: FrameTransform<CenterA, FrameA, FrameB> = make_rot(rot);
        assert_eq!(tf.op(), &rot);
        assert_eq!(tf.into_op(), rot);
    }

    #[test]
    fn partial_eq_ignores_phantom_tags() {
        #[derive(Debug, Copy, Clone, ReferenceFrame)]
        struct NoPartialEqFrame;

        #[derive(Debug, Copy, Clone)]
        struct NoPartialEqCenter;

        impl ReferenceCenter for NoPartialEqCenter {
            type Params = ();

            fn center_name() -> &'static str {
                "NoPartialEqCenter"
            }
        }

        impl AffineCenter for NoPartialEqCenter {}

        let a: FrameTransform<NoPartialEqCenter, FrameA, NoPartialEqFrame> =
            make_rot(Rotation3::IDENTITY);
        let b: FrameTransform<NoPartialEqCenter, FrameA, NoPartialEqFrame> =
            make_rot(Rotation3::IDENTITY);

        assert_eq!(a, b);
    }

    #[test]
    fn wrong_center_application_is_type_error() {
        // Documented via rustdoc compile_fail; this test only checks the happy path
        // still distinguishes centers at the type level by requiring an explicit tag.
        let tf: CenterTransform<CenterA, CenterB, FrameA, Meter> =
            make_trans(Translation3::new(1.0, 0.0, 0.0));
        let pos = Position::<CenterA, FrameA, Meter>::new(0.0, 0.0, 0.0);
        let _: Position<CenterB, FrameA, Meter> = tf.apply(pos);
    }
}