engeom 0.5.0

Library for working with geometry with a focus on engineering metrology applications.
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
//! This module contains the parameterization of the alignment problem

use crate::common::PCoords;
use crate::geom3::align3::{AlignStorage3, Dof6, iso3_from_param};
use crate::na::{Matrix3, UnitQuaternion};
use crate::{Iso3, Point3, Vector3};

// Skew-symmetric matrices
const SK_X: Matrix3<f64> = Matrix3::new(0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
const SK_Y: Matrix3<f64> = Matrix3::new(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0);
const SK_Z: Matrix3<f64> = Matrix3::new(0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0);

/// This struct holds the current values of the alignment problem, including the full
/// transformation from test entity space to target entity space, the vectors of translation
/// associated with the partial derivatives of the translation parameters, and functions for
/// calculating the vectors of rotation associated with the partial derivatives of the rotation
/// parameters. It is created from an [`AlignParams3`] struct based on the current values of
/// the alignment parameters.
///
/// During an alignment, the [`AlignParams3`]'s internal values will be changing as the solver
/// modifies it. This struct is generated to pre-calculate a number of values needed for working
/// with the alignment at that step in the alignment process.
#[derive(Clone, Debug)]
pub struct AlignValues3 {
    /// The transformation of the test entity(s) from their native, local coordinates to the
    /// current position in the target entity's coordinate system. This transformation is a
    /// composite of the active internal parameters and the working transformation.
    pub transform: Iso3,

    /// The transformation created by the tx, ty, tz, rx, ry, and rz parameters.
    pub align: Iso3,

    /// The degrees of freedom that are active
    pub dof: Dof6,

    /// The direction vector for the partial derivative of tx
    pub dtx: Vector3,

    /// The direction vector for the partial derivative of ty
    pub dty: Vector3,

    /// The direction vector for the partial derivative of tz
    pub dtz: Vector3,

    /// The matrix to extract the partial derivative of rx in the euler angle coordinate system
    /// and rotate it back to the target entity's coordinate system
    m_drx: Matrix3<f64>,

    /// The matrix to extract the partial derivative of ry in the euler angle coordinate system
    /// and rotate it back to the target entity's coordinate system
    m_dry: Matrix3<f64>,

    /// The matrix to extract the partial derivative of rz in the euler angle coordinate system
    /// and rotate it back to the target entity's coordinate system
    m_drz: Matrix3<f64>,

    /// The transformation from the target entity's coordinate system to the coordinate system
    /// where the euler angles of the alignment transformation are applied.
    pre_rot: Iso3,
}

impl AlignValues3 {
    /// Given a point in the target entity's coordinate system, return the vector that describes
    /// the instantaneous vector of motion corresponding with the partial derivative of rx
    pub fn drx(&self, point: &impl PCoords<3>) -> Vector3 {
        self.m_drx * (self.pre_rot * Point3::from(point.coords())).coords()
    }

    /// Given a point in the target entity's coordinate system, return the vector that describes
    /// the instantaneous vector of motion corresponding with the partial derivative of ry
    pub fn dry(&self, point: &impl PCoords<3>) -> Vector3 {
        self.m_dry * (self.pre_rot * Point3::from(point.coords())).coords()
    }

    /// Given a point in the target entity's coordinate system, return the vector that describes
    /// the instantaneous vector of motion corresponding with the partial derivative of rz
    pub fn drz(&self, point: &impl PCoords<3>) -> Vector3 {
        self.m_drz * (self.pre_rot * Point3::from(point.coords())).coords()
    }
}

/// This is a mechanism of defining a local origin for the test entity.
#[derive(Clone, Copy, Debug)]
pub enum AlignOrigin3 {
    /// The local origin is the origin of the coordinate system that the test entity(s) geometry
    Origin,

    /// The local origin is centered at the given point, but the directions of translation are
    /// the same as the coordinate system of the test entity.
    Center(Point3),

    /// The local origin is defined by full transformation from the origin of the coordinate system
    /// with the test entity(s) geometry, allowing full control over the center of rotation and
    /// the directions of translation.
    Local(Iso3),
}

/// The [`AlignParams3`] struct holds the parameters being optimized in a 3D alignment problem,
/// expressed as an euler angle rotation and translation problem around an arbitrary local origin.
/// Additionally, a working transformation allows the test entity(s) to be aligned as if they were
/// in a different position without actually requiring them to be transformed ahead of time.
///
/// The parameterization consists of 6 numbers in an owned vector, representing tx, ty, tz, rx, ry,
/// and rz, in that order, as a transformation at the local origin.
///
/// An alignment consists of a target (aka reference) entity and a test entity(s). The alignment
/// problem is to find the transformation that best aligns the test entity(s) to the target entity.
///
/// The entire alignment problem will take place in the target entity's coordinate system, such
/// that any components of the target entity which have positions will retain them wherever they
/// are. For the alignment, the target entity is in the world coordinate system.
///
/// The test entity(s) will be aligned to the geometry of the target entry by applying a 6-DOF
/// transformation ($A$). The transformation consists of three translations and three rotations. By
/// convention rotation is applied first around the origin, and then translation is applied along
/// the cardinal axes.
///
/// To allow for control over the center of rotation and the directions of translation, the
/// alignment can optionally define a local origin $L$ for the test entity(s). The local origin is
/// provided as a transformation from the origin of the coordinate system that the test entity(s)
/// geometry is defined in.
///
/// Lastly, there is an optional offset transformation $O$ that is applied after the alignment
/// transformation. This can be used to start the alignment process at a different position than
/// the current position of the test entity(s), or to counteract the effects of the local origin,
/// or a combination of the two.
///
/// The combined transformation from the test entity geometry to the target entity is:
///
/// $$ O * A * L^{-1} $$
///
#[derive(Clone, Debug)]
pub struct AlignParams3 {
    /// The local origin $L$, defined in the same space as the test entity's geometry. Leave this
    /// at the origin for simplicity, near the center of the test geometry to maximize numerical
    /// stability over rotations, or in a position/orientation to make use of the DOF constraints
    /// for a special case.
    pub local: Iso3,

    /// The degrees of freedom that are active during the alignment. Usually, all degrees of
    /// freedom will be active, but certain cases may require specific ones are locked. The local
    /// origin can be used in conjunction with the DOF constraints to control exactly how the test
    /// entity is allowed to move during alignment.
    pub dof: Dof6,

    /// The current working offset transformation $O$, which is the transformation applied after
    /// the alignment transformation.
    pub offset: Iso3,

    /// The storage for the six parameters
    storage: AlignStorage3,
}

impl AlignParams3 {
    /// Creates an `AlignParams3` with full control over its properties by separately specifying
    /// the local origin, working transformation, and degrees of freedom.
    ///
    /// # Arguments
    ///
    /// * `local`: The local origin $L$, defined in the same space as the test entity's geometry.
    ///   You can leave this as the world origin, pick a rotation center, or specify a full
    ///   transformation with origin and cardinal directions.
    /// * `offset`: An optional working offset transformation $O$, which is the transformation
    ///   applied after the alignment transformation.
    /// * `dof`: Optional constraint on the degrees of freedom. If `None` is provided, all degrees
    ///   of freedom will be active.
    ///
    /// returns: AlignParams3
    pub fn new(local: AlignOrigin3, offset: Option<Iso3>, dof: Option<Dof6>) -> Self {
        let local = match local {
            AlignOrigin3::Origin => Iso3::identity(),
            AlignOrigin3::Center(p) => Iso3::translation(p.x, p.y, p.z),
            AlignOrigin3::Local(t) => t,
        };

        let working = offset.unwrap_or_else(Iso3::identity);
        let dof = dof.unwrap_or_else(Dof6::all);

        Self {
            local,
            dof,
            offset: working,
            storage: AlignStorage3::default(),
        }
    }

    /// Creates an `AlignParams3` which applies its transformation to the test entity(s) at a given
    /// local origin. The local origin and the working offset transformation will be identical.
    ///
    /// The physical interpretation of configuring the parameters this way is that the test
    /// geometry is transformed directly according to the local origin's position and orientation.
    /// That is, tx, ty, and tz mean transformation along the local origin's x, y, and z axes.
    /// Rotation from rx, ry, and rz mean rotation around the local origin's centerpoint by the
    /// local origin's x, y, and z axes. Constraints applied to the degrees of freedom will refer
    /// to the local origin's axes.
    ///
    /// Use this configuration method when the test geometry is already in a good starting location,
    /// and you want to control exactly how the test geometry will move, such as if you want to
    /// apply DOF constraints in some arbitrary direction(s).
    ///
    /// # Arguments
    ///
    /// * `local`: the local origin $L$, defined in the same space as the test entity's geometry.
    /// * `dof`: Optional constraint on the degrees of freedom. If `None` is provided, all degrees
    ///   of freedom will be active.
    pub fn from_local(local: Iso3, dof: Option<Dof6>) -> Self {
        let working = local;
        Self::new(AlignOrigin3::Local(local), Some(working), dof)
    }

    /// Creates an `AlignParams3` which applies its rotations to the test entity(s) around a given
    /// rotation center point. In this case the local origin $L$ will be created at the specified
    /// rotation center point but with the same cardinal directions as the world coordinate system.
    /// The working offset transformation will be the same as the local origin.
    ///
    /// Use this configuration method when the test geometry is already in a good starting location,
    /// but you want to provide a rotation center point instead of allowing rotation to happen
    /// around the world origin. This is important for the numerical stability of rotations if the
    /// test geometry is far from the world origin.
    ///
    /// # Arguments
    ///
    /// * `center`: The point around which the test entity(s) will be rotated.
    /// * `dof`: Optional constraint on the degrees of freedom. If `None` is provided, all degrees
    ///   of freedom will be active.
    pub fn from_center(center: Point3, dof: Option<Dof6>) -> Self {
        let local = Iso3::translation(center.x, center.y, center.z);
        let working = local;
        Self::new(AlignOrigin3::Local(local), Some(working), dof)
    }

    /// Creates an `AlignParams3` with the local and working transformations set to the identity.
    /// Use this configuration method when the test geometry is already in a good starting location
    /// and close enough to the origin that you aren't worried about the numerical stability of
    /// rotations.
    ///
    /// # Arguments
    ///
    /// * `dof`: Optional constraint on the degrees of freedom. If `None` is provided, all degrees
    ///   of freedom will be active.
    ///
    /// returns: AlignParams3
    pub fn from_origin(dof: Option<Dof6>) -> Self {
        Self::new(AlignOrigin3::Origin, None, dof)
    }

    pub fn tx(&self) -> f64 {
        self.storage[0]
    }
    pub fn ty(&self) -> f64 {
        self.storage[1]
    }
    pub fn tz(&self) -> f64 {
        self.storage[2]
    }
    pub fn rx(&self) -> f64 {
        self.storage[3]
    }
    pub fn ry(&self) -> f64 {
        self.storage[4]
    }
    pub fn rz(&self) -> f64 {
        self.storage[5]
    }

    /// Get the stored isometry without the working transformation. If the problem has converged,
    /// this is the final result.
    pub fn compute_transform(&self) -> Iso3 {
        self.compute_values().transform
    }

    /// Calculate the current alignment values, including the full transform, the translation
    /// directions, and the rotation partial derivative matrices
    pub fn compute_values(&self) -> AlignValues3 {
        let align = iso3_from_param(&self.storage);
        let transform = self.offset * align * self.local.inverse();

        let dtx = self.offset * Vector3::new(1.0, 0.0, 0.0);
        let dty = self.offset * Vector3::new(0.0, 1.0, 0.0);
        let dtz = self.offset * Vector3::new(0.0, 0.0, 1.0);

        let (m_drx, m_dry, m_drz) = euler_partials(self.ry(), self.rz());

        let pre_rot = align.translation.inverse() * self.offset.inverse();
        let post_rot = self.offset.rotation.to_rotation_matrix();

        AlignValues3 {
            transform,
            align,
            dof: self.dof,
            dtx,
            dty,
            dtz,
            m_drx: post_rot * m_drx,
            m_dry: post_rot * m_dry,
            m_drz: post_rot * m_drz,
            pre_rot,
        }
    }

    pub fn storage(&self) -> AlignStorage3 {
        self.storage
    }

    fn enforce_constraint(&mut self) {
        if !self.dof.tx {
            self.storage[0] = 0.0;
        }
        if !self.dof.ty {
            self.storage[1] = 0.0;
        }
        if !self.dof.tz {
            self.storage[2] = 0.0;
        }
        if !self.dof.rx {
            self.storage[3] = 0.0;
        }
        if !self.dof.ry {
            self.storage[4] = 0.0;
        }
        if !self.dof.rz {
            self.storage[5] = 0.0;
        }
    }

    pub fn set_storage(&mut self, storage: AlignStorage3) {
        self.storage = storage;
        self.enforce_constraint();
    }

    pub fn set_index(&mut self, index: usize, value: f64) {
        self.storage[index] = value;
        self.enforce_constraint();
    }

    pub fn with_tx(&self, tx: f64) -> AlignParams3 {
        self.with_index(0, tx)
    }

    pub fn with_ty(&self, ty: f64) -> AlignParams3 {
        self.with_index(1, ty)
    }

    pub fn with_tz(&self, tz: f64) -> AlignParams3 {
        self.with_index(2, tz)
    }

    pub fn with_rx(&self, rx: f64) -> AlignParams3 {
        self.with_index(3, rx)
    }

    pub fn with_ry(&self, ry: f64) -> AlignParams3 {
        self.with_index(4, ry)
    }

    pub fn with_rz(&self, rz: f64) -> AlignParams3 {
        self.with_index(5, rz)
    }

    fn with_index(&self, index: usize, value: f64) -> AlignParams3 {
        let mut params = self.clone();
        params.set_index(index, value);
        params
    }

    pub fn with_storage(&self, storage: AlignStorage3) -> AlignParams3 {
        let mut params = self.clone();
        params.set_storage(storage);
        params
    }
}

/// This function finds the three matrices that, when multiplied by the coordinate vector of a
/// point in space, find the instantaneous velocity vector of that point for an infinitesimal
/// change in the rx, ry, and rz parameters, given the current values of the y and z parameters.
///
/// This assumes that the center of rotation is the origin.
///
/// The skew symmetric matrices are the correct matrices to find the velocity vectors when the
/// existing parameters are all at zero, as they correspond with instantaneous rotations around
/// their respective axes. However, to account for the gimbal effect, we will need to be able to
/// undo the effects of the later rotations for any rotation in the order.
///
/// Since the nalgebra library uses the Euler order X-Y-Z, the Z rotation has nothing following it
/// and can be measured directly with the skew symmetric Z matrix. For the Y rotation, we will need
/// to first undo Z, then apply the skew symmetric Y matrix, and then re-apply the Z rotation. For
/// X we will need to undo then redo both Z and Y. This is why this function requires the ry and
/// rz parameters, but not the rx parameter.
///
/// # Arguments
///
/// * `ry`: the current rotation around the Y axis
/// * `rz`: the current rotation around the Z axis
///
/// returns: (Matrix<f64, Const<3>, Const<3>, ArrayStorage<f64, 3, 3>>, Matrix<f64, Const<3>, Const<3>, ArrayStorage<f64, 3, 3>>, Matrix<f64, Const<3>, Const<3>, ArrayStorage<f64, 3, 3>>)
fn euler_partials(ry: f64, rz: f64) -> (Matrix3<f64>, Matrix3<f64>, Matrix3<f64>) {
    let y = UnitQuaternion::from_euler_angles(0.0, ry, 0.0).to_rotation_matrix();
    let z = UnitQuaternion::from_euler_angles(0.0, 0.0, rz).to_rotation_matrix();

    // Z will always be the skew symmetric matrix, because it is the last rotation applied,
    // so it will always produce a uniform rotation around Z

    // To find the matrix for Y, we need to undo the Z rotation, apply the skew symmetric matrix,
    // then re-rotate back to the original Z
    let dy = z * SK_Y * z.transpose();

    // To find the matrix for X, we need to undo the Z rotation, then the Y rotation, apply the
    // skew symmetric matrix, then re-rotate the Y, and finally re-rotate the Z.
    let dx = z * y * SK_X * y.transpose() * z.transpose();

    (dx, dy, SK_Z)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::random_geometry::RandomGeometry3;
    use crate::geom3::IsoExtensions3;
    use crate::geom3::align3::params::{AlignOrigin3, AlignParams3};
    use crate::na::{Rotation, Rotation3, UnitQuaternion};
    use crate::{Iso3, Point3, Vector3};
    use approx::assert_relative_eq;
    use std::f64::consts::PI;

    const ANGLE_EPSILON: f64 = 1e-8;
    const TRANS_EPSILON: f64 = 1e-8;

    // ============================================================================================
    // These first tests are to check some simple and obvious properties of AlignParams3
    // ============================================================================================

    #[test]
    fn rotation_around_origin() {
        // Checks that the transformation occurs around the origin when the rotation center is set
        // to the origin.
        let params = AlignParams3::from_origin(None).with_rz(PI / 2.0);
        let test_point = Point3::new(1.0, 2.0, 3.0);
        let expected = Point3::new(-2.0, 1.0, 3.0);

        assert_relative_eq!(
            expected,
            params.compute_values().transform * test_point,
            epsilon = 1e-8
        );
    }

    #[test]
    fn rotation_around_center() {
        // Checks that the transformation occurs around the rotation center when it is nonzero
        let rc = Point3::new(1.0, 0.0, 0.0);
        let params = AlignParams3::from_center(rc, None).with_rz(PI / 2.0);

        let test_point = Point3::new(2.0, 2.0, 3.0);
        let expected = Point3::new(-1.0, 1.0, 3.0);

        assert_relative_eq!(
            expected,
            params.compute_values().transform * test_point,
            epsilon = 1e-8
        );
    }

    #[test]
    fn builders_respect_locked_dof() {
        // The `with_*` builders must enforce the DOF constraints the same way `set_index` and
        // `set_storage` do, otherwise a locked degree of freedom can be given a nonzero value
        // through the builder path.
        let dof = Dof6::new(false, true, true, true, false, true);
        let params = AlignParams3::from_origin(Some(dof));

        assert_eq!(params.with_tx(1.0).tx(), 0.0);
        assert_eq!(params.with_ry(1.0).ry(), 0.0);

        // The unlocked degrees of freedom still take their values
        assert_eq!(params.with_ty(1.0).ty(), 1.0);
        assert_eq!(params.with_rx(1.0).rx(), 1.0);

        // ...and the same holds when the whole storage vector is set at once
        let all = params.with_storage(AlignStorage3::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0));
        assert_eq!(all.tx(), 0.0);
        assert_eq!(all.ry(), 0.0);
        assert_eq!(all.ty(), 2.0);
        assert_eq!(all.rz(), 6.0);
    }

    #[test]
    fn translations_are_in_local_origin_directions() {
        let local = Iso3::from_basis_xy(
            &Vector3::new(0.0, -1.0, 0.0),
            &Vector3::new(0.0, 0.0, 1.0),
            None,
        )
        .unwrap();
        let params = AlignParams3::from_local(local, None).with_tx(1.0);

        let test_point = Point3::new(1.0, 2.0, 3.0);
        let expected = Point3::new(1.0, 1.0, 3.0);

        assert_relative_eq!(
            expected,
            params.compute_values().transform * test_point,
            epsilon = 1e-8
        );
    }

    // ============================================================================================
    // These next tests are to check that the current translation directions match the direction
    // that they actually move entities.
    // ============================================================================================

    #[test]
    fn partials_of_translations_at_zero() {
        let params = AlignParams3::from_origin(None);
        let current = params.compute_values();

        assert_relative_eq!(current.dtx, Vector3::x_axis(), epsilon = 1e-12);
        assert_relative_eq!(current.dty, Vector3::y_axis(), epsilon = 1e-12);
        assert_relative_eq!(current.dtz, Vector3::z_axis(), epsilon = 1e-12);
    }

    #[test]
    fn partials_of_translations_with_rotations() {
        let params = AlignParams3::from_origin(None)
            .with_rx(0.1)
            .with_ry(0.2)
            .with_rz(0.3);
        let test_point = Point3::new(1.0, 2.0, 3.0);

        let exp_x = finite_diff(&params, &test_point, 0);
        let exp_y = finite_diff(&params, &test_point, 1);
        let exp_z = finite_diff(&params, &test_point, 2);
        let c = params.compute_values();

        // Because translations are applied after rotations, the direction vectors don't change
        // when rotation parameters are applied
        assert_relative_eq!(c.dtx, Vector3::x_axis(), epsilon = 1e-12);
        assert_relative_eq!(c.dty, Vector3::y_axis(), epsilon = 1e-12);
        assert_relative_eq!(c.dtz, Vector3::z_axis(), epsilon = 1e-12);

        // Sanity check that I've thought through this correctly
        assert_relative_eq!(exp_x, c.dtx, epsilon = 1e-8);
        assert_relative_eq!(exp_y, c.dty, epsilon = 1e-8);
        assert_relative_eq!(exp_z, c.dtz, epsilon = 1e-8);
    }

    #[test]
    fn stress_partials_translations() {
        // This is the main test in this section; it checks that the actual translation directions
        // match the measured directions even with random numbers in every possible parameter.
        let mut rg = RandomGeometry3::new();
        for _ in 0..1000 {
            let local = rg.iso3(10.0);
            let working = rg.iso3(10.0);
            let params = AlignParams3::new(AlignOrigin3::Local(local), Some(working), None)
                .with_storage(rg.vector(PI));

            let test_point = rg.point(10.0);
            let exp_x = finite_diff(&params, &test_point, 0);
            let exp_y = finite_diff(&params, &test_point, 1);
            let exp_z = finite_diff(&params, &test_point, 2);

            let c = params.compute_values();
            assert_relative_eq!(exp_x, c.dtx, epsilon = 1e-6);
            assert_relative_eq!(exp_y, c.dty, epsilon = 1e-6);
            assert_relative_eq!(exp_z, c.dtz, epsilon = 1e-6);
        }
    }

    // ============================================================================================
    // At this point we have to deal with the partials of the rotation by euler angles. I'm not
    // good enough at linear algebra to derive the direct solution, so I'm hoping to figure it out
    // as we go.
    // ============================================================================================

    #[test]
    fn partials_of_rotation_at_zero() {
        let params = AlignParams3::from_origin(None);
        let current = params.compute_values();
        let p_local = Point3::new(1.0, 2.0, 3.0);
        let p = current.transform * p_local;

        let exp_rx = finite_diff(&params, &p_local, 3);
        let exp_ry = finite_diff(&params, &p_local, 4);
        let exp_rz = finite_diff(&params, &p_local, 5);

        assert_relative_eq!(exp_rx, current.drx(&p), epsilon = 1e-6);
        assert_relative_eq!(exp_ry, current.dry(&p), epsilon = 1e-6);
        assert_relative_eq!(exp_rz, current.drz(&p), epsilon = 1e-6);
    }

    #[test]
    fn verify_euler_order_is_x_y_z() {
        // This verifies that the order of nalgebra's euler angles is x, then y, then z.
        let rx = UnitQuaternion::from_axis_angle(&Vector3::x_axis(), PI / 4.0).to_rotation_matrix();
        let ry = UnitQuaternion::from_axis_angle(&Vector3::y_axis(), PI / 5.0).to_rotation_matrix();
        let rz = UnitQuaternion::from_axis_angle(&Vector3::z_axis(), PI / 6.0).to_rotation_matrix();

        let expected =
            UnitQuaternion::from_euler_angles(PI / 4.0, PI / 5.0, PI / 6.0).to_rotation_matrix();

        // Remember that matrix multiplication is ordered backwards
        let actual = rz * ry * rx;

        assert_relative_eq!(expected, actual, epsilon = 1e-12);
    }

    #[test]
    fn euler_partial_rx() {
        let f = EulerFixture::new(PI / 4.0, 0.0, 0.0);
        let p_local = Point3::new(1.0, 1.0, 1.0);
        let p_rot = f.rot * p_local;

        let (rdx, rdy, rdz) = euler_partials(f.ry, f.rz);
        assert_relative_eq!(f.exp_z(&p_local), rdz * p_rot.coords, epsilon = 1e-6);
        assert_relative_eq!(f.exp_y(&p_local), rdy * p_rot.coords, epsilon = 1e-6);
        assert_relative_eq!(f.exp_x(&p_local), rdx * p_rot.coords, epsilon = 1e-6);
    }

    #[test]
    fn euler_partial_ry() {
        let f = EulerFixture::new(0.0, PI / 4.0, 0.0);
        let p_local = Point3::new(1.0, 1.0, 1.0);
        let p_rot = f.rot * p_local;

        let (rdx, rdy, rdz) = euler_partials(f.ry, f.rz);
        assert_relative_eq!(f.exp_z(&p_local), rdz * p_rot.coords, epsilon = 1e-6);
        assert_relative_eq!(f.exp_y(&p_local), rdy * p_rot.coords, epsilon = 1e-6);
        assert_relative_eq!(f.exp_x(&p_local), rdx * p_rot.coords, epsilon = 1e-6);
    }

    #[test]
    fn euler_partial_rz() {
        let f = EulerFixture::new(0.0, 0.0, PI / 4.0);
        let p_local = Point3::new(1.0, 1.0, 1.0);
        let p_rot = f.rot * p_local;

        let (rdx, rdy, rdz) = euler_partials(f.ry, f.rz);
        assert_relative_eq!(f.exp_z(&p_local), rdz * p_rot.coords, epsilon = 1e-6);
        assert_relative_eq!(f.exp_y(&p_local), rdy * p_rot.coords, epsilon = 1e-6);
        assert_relative_eq!(f.exp_x(&p_local), rdx * p_rot.coords, epsilon = 1e-6);
    }

    #[test]
    fn stress_euler_partial_rotated() {
        let mut rg = RandomGeometry3::new();
        for _ in 0..1000 {
            let f = EulerFixture::new(rg.f64_sym(PI), rg.f64_sym(PI), rg.f64_sym(PI));
            let p_local = rg.point(10.0);
            let p_rot = f.rot * p_local;
            let (rdx, rdy, rdz) = euler_partials(f.ry, f.rz);

            assert_relative_eq!(f.exp_z(&p_local), rdz * p_rot.coords, epsilon = 1e-6);
            assert_relative_eq!(f.exp_y(&p_local), rdy * p_rot.coords, epsilon = 1e-6);
            assert_relative_eq!(f.exp_x(&p_local), rdx * p_rot.coords, epsilon = 1e-6);
        }
    }

    // ============================================================================================
    // With the rotation vectors figured out from the euler angles at the origin, we just need to
    // figure out how to make this work in the larger context of the full alignment problem, which
    // may have a local origin _and_ a working offset, and also which needs to receive the point
    // being measured in the target entity's space.
    // ============================================================================================

    #[test]
    fn stress_partials_rotation() {
        let mut rg = RandomGeometry3::new();
        for _ in 0..1000 {
            let local = rg.iso3(10.0);
            let working = rg.iso3(10.0);
            let params = AlignParams3::new(AlignOrigin3::Local(local), Some(working), None)
                .with_storage(rg.vector(PI));
            let c = params.compute_values();

            // The test point in the test entity's space
            let test_point = rg.point(10.0);
            let exp_x = finite_diff(&params, &test_point, 3);
            let exp_y = finite_diff(&params, &test_point, 4);
            let exp_z = finite_diff(&params, &test_point, 5);

            // The test point in the target entity's space
            let target_point = c.transform * test_point;

            // Because of the possible large distance between the test point and the rotation,
            // the difference between the finite difference estimation and the actual theoretical
            // vectors can be well into the fourth decimal place. Since this is a stress test for
            // plausibility, that's totally fine. The theoretical values will be more accurate
            // than the estimate, but they must be reasonably close.
            assert_relative_eq!(exp_x, c.drx(&target_point), epsilon = 1e-3);
            assert_relative_eq!(exp_y, c.dry(&target_point), epsilon = 1e-3);
            assert_relative_eq!(exp_z, c.drz(&target_point), epsilon = 1e-3);
        }
    }

    // ============================================================================================
    // Test support methods
    // ============================================================================================
    struct EulerFixture {
        rx: f64,
        ry: f64,
        rz: f64,
        rot: Rotation<f64, 3>,
    }

    impl EulerFixture {
        fn new(rx: f64, ry: f64, rz: f64) -> Self {
            let rot = UnitQuaternion::from_euler_angles(rx, ry, rz).to_rotation_matrix();
            Self { rx, ry, rz, rot }
        }

        fn exp_x(&self, point: &Point3) -> Vector3 {
            euler_finite_diff(self.rx, self.ry, self.rz, Ax::X, point)
        }

        fn exp_y(&self, point: &Point3) -> Vector3 {
            euler_finite_diff(self.rx, self.ry, self.rz, Ax::Y, point)
        }

        fn exp_z(&self, point: &Point3) -> Vector3 {
            euler_finite_diff(self.rx, self.ry, self.rz, Ax::Z, point)
        }
    }

    enum Ax {
        X,
        Y,
        Z,
    }

    fn euler_finite_diff(rx: f64, ry: f64, rz: f64, axis: Ax, point: &Point3) -> Vector3 {
        let t0 = match axis {
            Ax::X => unit_quat(rx - ANGLE_EPSILON, ry, rz),
            Ax::Y => unit_quat(rx, ry - ANGLE_EPSILON, rz),
            Ax::Z => unit_quat(rx, ry, rz - ANGLE_EPSILON),
        };
        let t1 = match axis {
            Ax::X => unit_quat(rx + ANGLE_EPSILON, ry, rz),
            Ax::Y => unit_quat(rx, ry + ANGLE_EPSILON, rz),
            Ax::Z => unit_quat(rx, ry, rz + ANGLE_EPSILON),
        };
        let p0 = t0 * point;
        let p1 = t1 * point;
        (p1 - p0) / (2.0 * ANGLE_EPSILON)
    }

    fn unit_quat(rx: f64, ry: f64, rz: f64) -> Rotation3<f64> {
        UnitQuaternion::from_euler_angles(rx, ry, rz).to_rotation_matrix()
    }

    /// This function computes the vector of motion of a point in the original coordinate system
    /// of the test entity for a finitely approximated infinitesimal change in one of the
    /// parameters. The parameters by index are: (0: tx, 1: ty, 2: tz, 3: rx, 4: ry, 5: rz)
    fn finite_diff(params: &AlignParams3, point: &Point3, index: usize) -> Vector3 {
        let mut w0 = params.clone();
        let mut w1 = params.clone();
        let stored = params.storage();

        let eps = if index < 3 {
            TRANS_EPSILON
        } else {
            ANGLE_EPSILON
        };

        w0.set_index(index, stored[index] - eps);
        w1.set_index(index, stored[index] + eps);

        let p0 = w0.compute_values().transform * point;
        let p1 = w1.compute_values().transform * point;

        (p1 - p0) / (2.0 * eps)
    }
}