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
//! This module contains the parameterization of the 2D alignment problem

use crate::common::PCoords;
use crate::geom2::align2::{AlignStorage2, Dof3};
use crate::na::{Matrix2, Translation2, UnitComplex};
use crate::{Iso2, Point2, Vector2};

// The skew-symmetric generator of a 2D rotation: d/dtheta R(theta) = R(theta) * SK2 = SK2 *
// R(theta), since 2D rotation matrices always commute with each other and with their own
// generator. This is why, unlike the 3D case, there is no gimbal correction to derive: a single
// rotation parameter has nothing "after" it in the rotation order that needs to be undone and
// reapplied.
const SK2: Matrix2<f64> = Matrix2::new(0.0, -1.0, 1.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 the matrix for
/// calculating the vector of rotation associated with the partial derivative of the rotation
/// parameter. It is created from an [`AlignParams2`] struct based on the current values of the
/// alignment parameters.
///
/// During an alignment, the [`AlignParams2`]'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 AlignValues2 {
    /// 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: Iso2,

    /// The transformation created by the tx, ty, and rz parameters.
    pub align: Iso2,

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

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

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

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

    /// The transformation from the target entity's coordinate system to the coordinate system
    /// where the rotation angle of the alignment transformation is applied.
    pre_rot: Iso2,
}

impl AlignValues2 {
    /// 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<2>) -> Vector2 {
        self.m_drz * (self.pre_rot * Point2::from(point.coords())).coords()
    }
}

/// This is a mechanism of defining a local origin for the test entity.
#[derive(Clone, Copy, Debug)]
pub enum AlignOrigin2 {
    /// 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(Point2),

    /// 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(Iso2),
}

/// The [`AlignParams2`] struct holds the parameters being optimized in a 2D alignment problem,
/// expressed as a rotation angle 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 3 numbers in an owned vector, representing tx, ty, 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 3-DOF
/// transformation ($A$). The transformation consists of two translations and one rotation. 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 AlignParams2 {
    /// 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: Iso2,

    /// 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: Dof3,

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

    /// The storage for the three parameters
    storage: AlignStorage2,
}

impl AlignParams2 {
    /// Creates an `AlignParams2` 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: AlignParams2
    pub fn new(local: AlignOrigin2, offset: Option<Iso2>, dof: Option<Dof3>) -> Self {
        let local = match local {
            AlignOrigin2::Origin => Iso2::identity(),
            AlignOrigin2::Center(p) => Iso2::translation(p.x, p.y),
            AlignOrigin2::Local(t) => t,
        };

        let offset = offset.unwrap_or_else(Iso2::identity);
        let dof = dof.unwrap_or_else(Dof3::all);

        Self {
            local,
            dof,
            offset,
            storage: AlignStorage2::zeros(),
        }
    }

    /// Creates an `AlignParams2` 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 and ty mean transformation along the local origin's x and y axes. Rotation
    /// from rz means rotation around the local origin's centerpoint. 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: Iso2, dof: Option<Dof3>) -> Self {
        Self::new(AlignOrigin2::Local(local), Some(local), dof)
    }

    /// Creates an `AlignParams2` 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: Point2, dof: Option<Dof3>) -> Self {
        let local = Iso2::translation(center.x, center.y);
        Self::new(AlignOrigin2::Local(local), Some(local), dof)
    }

    /// Creates an `AlignParams2` 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: AlignParams2
    pub fn from_origin(dof: Option<Dof3>) -> Self {
        Self::new(AlignOrigin2::Origin, None, dof)
    }

    pub fn tx(&self) -> f64 {
        self.storage[0]
    }
    pub fn ty(&self) -> f64 {
        self.storage[1]
    }
    pub fn rz(&self) -> f64 {
        self.storage[2]
    }

    /// Computes the full transformation $O * A * L^{-1}$ that brings the test geometry into the
    /// target's coordinate system, including the local origin and the working offset. If the
    /// problem has converged, this is the final result of the alignment.
    ///
    /// This is a convenience wrapper over [`AlignParams2::compute_values`] for callers that only
    /// need the transform; it does the same work, so prefer `compute_values` if you also need the
    /// partial derivative directions.
    pub fn compute_transform(&self) -> Iso2 {
        self.compute_values().transform
    }

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

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

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

        AlignValues2 {
            transform,
            align,
            dof: self.dof,
            dtx,
            dty,
            m_drz: post_rot * SK2,
            pre_rot,
        }
    }

    pub fn storage(&self) -> AlignStorage2 {
        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.rz {
            self.storage[2] = 0.0;
        }
    }

    pub fn set_storage(&mut self, storage: AlignStorage2) {
        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) -> AlignParams2 {
        self.with_index(0, tx)
    }

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

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

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

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

/// Composes the `tx`, `ty`, `rz` parameters into the alignment isometry `A`.
fn align_from_storage(p: &AlignStorage2) -> Iso2 {
    Iso2::from_parts(Translation2::new(p.x, p.y), UnitComplex::new(p.z))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::random_geometry::RandomGeometry2;
    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 AlignParams2, mirroring
    // the equivalent tests for AlignParams3 in geom3::align3::params.
    // ============================================================================================

    #[test]
    fn rotation_around_origin() {
        let params = AlignParams2::from_origin(None).with_rz(PI / 2.0);
        let test_point = Point2::new(1.0, 2.0);
        let expected = Point2::new(-2.0, 1.0);

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

    #[test]
    fn rotation_around_center() {
        let rc = Point2::new(1.0, 0.0);
        let params = AlignParams2::from_center(rc, None).with_rz(PI / 2.0);

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

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

    #[test]
    fn translations_are_in_local_origin_directions() {
        // A local origin whose axes are rotated 90 degrees from world: local +x points in world
        // +y, local +y points in world -x.
        let local = Iso2::rotation(PI / 2.0);
        let params = AlignParams2::from_local(local, None).with_tx(1.0);

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

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

    #[test]
    fn builders_respect_locked_dof() {
        let dof = Dof3::new(false, true, true);
        let params = AlignParams2::from_origin(Some(dof));

        assert_eq!(params.with_tx(1.0).tx(), 0.0);
        assert_eq!(params.with_ty(1.0).ty(), 1.0);
        assert_eq!(params.with_rz(1.0).rz(), 1.0);

        let all = params.with_storage(AlignStorage2::new(1.0, 2.0, 3.0));
        assert_eq!(all.tx(), 0.0);
        assert_eq!(all.ty(), 2.0);
        assert_eq!(all.rz(), 3.0);
    }

    // ============================================================================================
    // 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 = AlignParams2::from_origin(None);
        let current = params.compute_values();

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

    #[test]
    fn partials_of_translations_with_rotation() {
        let params = AlignParams2::from_origin(None).with_rz(0.3);
        let test_point = Point2::new(1.0, 2.0);

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

        // Because translations are applied after rotation, the direction vectors don't change
        // when the rotation parameter is nonzero.
        assert_relative_eq!(c.dtx, Vector2::x_axis(), epsilon = 1e-12);
        assert_relative_eq!(c.dty, Vector2::y_axis(), epsilon = 1e-12);

        assert_relative_eq!(exp_x, c.dtx, epsilon = 1e-8);
        assert_relative_eq!(exp_y, c.dty, epsilon = 1e-8);
    }

    #[test]
    fn stress_partials_translations() {
        let mut rg = RandomGeometry2::new();
        for _ in 0..1000 {
            let local = rg.iso2(10.0);
            let working = rg.iso2(10.0);
            let params = AlignParams2::new(AlignOrigin2::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 c = params.compute_values();
            assert_relative_eq!(exp_x, c.dtx, epsilon = 1e-6);
            assert_relative_eq!(exp_y, c.dty, epsilon = 1e-6);
        }
    }

    // ============================================================================================
    // Partial derivative of the rotation parameter. Unlike the 3D case there's only one rotation
    // axis and no gimbal effect, so there's no analogue of `euler_partials` to verify separately;
    // the stress test below exercises `AlignValues2::drz` directly against a finite-difference
    // estimate over random local origins, offsets, and parameter values.
    // ============================================================================================

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

        let exp_rz = finite_diff(&params, &p_local, 2);
        assert_relative_eq!(exp_rz, current.drz(&p), epsilon = 1e-6);
    }

    #[test]
    fn stress_partials_rotation() {
        let mut rg = RandomGeometry2::new();
        for _ in 0..1000 {
            let local = rg.iso2(10.0);
            let working = rg.iso2(10.0);
            let params = AlignParams2::new(AlignOrigin2::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_rz = finite_diff(&params, &test_point, 2);

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

            // As in the 3D stress test, the finite-difference estimate and the analytic value can
            // diverge into the fourth decimal place for points far from the rotation center; this
            // is a plausibility check, not a tight tolerance.
            assert_relative_eq!(exp_rz, c.drz(&target_point), epsilon = 1e-3);
        }
    }

    // ============================================================================================
    // Test support methods
    // ============================================================================================

    /// 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: rz)
    fn finite_diff(params: &AlignParams2, point: &Point2, index: usize) -> Vector2 {
        let mut w0 = params.clone();
        let mut w1 = params.clone();
        let stored = params.storage();

        let eps = if index < 2 {
            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)
    }
}