ritk-transform 0.3.0

Spatial image transform operators for RITK
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
//! Tests for composite_io
//! Extracted to keep the 500-line structural limit.
use super::io::{CompositeTransform, TransformDescription};
use ritk_core::rejection::assert_rejects;

// -- helpers ----------------------------------------------------------

/// 3-D identity matrix in row-major order (4×4 homogeneous).
fn identity_4x4() -> Vec<f64> {
    vec![
        1.0, 0.0, 0.0, 0.0, // row 0
        0.0, 1.0, 0.0, 0.0, // row 1
        0.0, 0.0, 1.0, 0.0, // row 2
        0.0, 0.0, 0.0, 1.0, // row 3
    ]
}

/// 3-D rotation matrix: 90° about Z axis (row-major, 3×3).
///
/// R_z(π/2) = [[0, -1, 0], [1, 0, 0], [0, 0, 1]]
///
/// Analytically: cos(π/2) = 0, sin(π/2) = 1.
fn rotation_z_90_3x3() -> Vec<f64> {
    vec![
        0.0, -1.0, 0.0, // row 0
        1.0, 0.0, 0.0, // row 1
        0.0, 0.0, 1.0, // row 2
    ]
}

// -- round-trip serialization ----------------------------------------

#[test]
fn roundtrip_translation() {
    let desc = TransformDescription::Translation {
        offset: vec![1.5, -2.25, 3.0],
    };
    let json = serde_json::to_string(&desc).expect("serialize");
    let back: TransformDescription = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(desc, back);
    // Value inspection: ensure offset survived.
    if let TransformDescription::Translation { offset } = &back {
        assert_eq!(offset.len(), 3);
        assert_eq!(offset[0], 1.5);
        assert_eq!(offset[1], -2.25);
        assert_eq!(offset[2], 3.0);
    } else {
        panic!("expected Translation variant");
    }
}

#[test]
fn roundtrip_rigid() {
    let desc = TransformDescription::Rigid {
        rotation: rotation_z_90_3x3(),
        translation: vec![10.0, 20.0, 30.0],
    };
    let json = serde_json::to_string(&desc).expect("serialize");
    let back: TransformDescription = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(desc, back);

    if let TransformDescription::Rigid {
        rotation,
        translation,
    } = &back
    {
        assert_eq!(rotation.len(), 9);
        assert_eq!(translation.len(), 3);
        // Verify R[0][1] = -1 (second element).
        assert_eq!(rotation[1], -1.0);
        assert_eq!(translation[2], 30.0);
    } else {
        panic!("expected Rigid variant");
    }
}

#[test]
fn roundtrip_affine() {
    let desc = TransformDescription::Affine {
        matrix: identity_4x4(),
    };
    let json = serde_json::to_string_pretty(&desc).expect("serialize");
    let back: TransformDescription = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(desc, back);

    if let TransformDescription::Affine { matrix } = &back {
        assert_eq!(matrix.len(), 16);
        // Diagonal elements are 1.
        assert_eq!(matrix[0], 1.0);
        assert_eq!(matrix[5], 1.0);
        assert_eq!(matrix[10], 1.0);
        assert_eq!(matrix[15], 1.0);
        // Off-diagonal sample is 0.
        assert_eq!(matrix[1], 0.0);
    } else {
        panic!("expected Affine variant");
    }
}

#[test]
fn roundtrip_displacement_field() {
    // 2×2×2 displacement field in 3-D.
    // Total voxels: 8.  Three component vectors of length 8.
    let dims = vec![2, 2, 2];
    let n: usize = dims.iter().product();
    assert_eq!(n, 8);

    // Analytically simple: constant displacement of (0.5, -0.5, 1.0).
    let dz = vec![0.5_f64; n];
    let dy = vec![-0.5_f64; n];
    let dx = vec![1.0_f64; n];

    let desc = TransformDescription::DisplacementField {
        dims: dims.clone(),
        origin: vec![0.0, 0.0, 0.0],
        spacing: vec![1.0, 1.0, 1.0],
        components: vec![dz.clone(), dy.clone(), dx.clone()],
    };

    let json = serde_json::to_string(&desc).expect("serialize");
    let back: TransformDescription = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(desc, back);

    if let TransformDescription::DisplacementField {
        dims: d,
        origin,
        spacing,
        components,
    } = &back
    {
        assert_eq!(d, &dims);
        assert_eq!(origin.len(), 3);
        assert_eq!(spacing.len(), 3);
        assert_eq!(components.len(), 3);
        assert_eq!(components[0].len(), n);
        assert_eq!(components[0][0], 0.5);
        assert_eq!(components[1][0], -0.5);
        assert_eq!(components[2][0], 1.0);
    } else {
        panic!("expected DisplacementField variant");
    }
}

#[test]
fn roundtrip_bspline() {
    let grid_dims = vec![3, 3, 3];
    let n: usize = grid_dims.iter().product();
    assert_eq!(n, 27);

    // Linearly increasing control-point displacement per axis.
    let comp_z: Vec<f64> = (0..n).map(|i| i as f64 * 0.1).collect();
    let comp_y: Vec<f64> = (0..n).map(|i| -(i as f64) * 0.05).collect();
    let comp_x: Vec<f64> = (0..n).map(|i| i as f64 * 0.2).collect();

    let desc = TransformDescription::BSpline {
        grid_dims: grid_dims.clone(),
        grid_origin: vec![-1.0, -1.0, -1.0],
        grid_spacing: vec![1.0, 1.0, 1.0],
        components: vec![comp_z.clone(), comp_y.clone(), comp_x.clone()],
    };

    let json = serde_json::to_string(&desc).expect("serialize");
    let back: TransformDescription = serde_json::from_str(&json).expect("deserialize");
    // Approximate comparison: some f64 values (e.g. 1.4, 1.9) are not
    // exactly representable in IEEE 754 and may differ by 1 ULP after
    // JSON text round-trip.
    if let (
        TransformDescription::BSpline {
            components: c_orig, ..
        },
        TransformDescription::BSpline {
            components: c_back, ..
        },
    ) = (&desc, &back)
    {
        assert_eq!(c_orig.len(), c_back.len());
        for (orig_vec, back_vec) in c_orig.iter().zip(c_back.iter()) {
            assert_eq!(orig_vec.len(), back_vec.len());
            for (a, b) in orig_vec.iter().zip(back_vec.iter()) {
                assert!(
                    (a - b).abs() < 1e-15,
                    "component mismatch: {a} vs {b}, diff = {}",
                    (a - b).abs()
                );
            }
        }
    } else {
        panic!("expected both to be BSpline variants");
    }

    if let TransformDescription::BSpline {
        grid_dims: gd,
        grid_origin,
        grid_spacing,
        components,
    } = &back
    {
        assert_eq!(gd, &grid_dims);
        assert_eq!(grid_origin, &[-1.0, -1.0, -1.0]);
        assert_eq!(grid_spacing, &[1.0, 1.0, 1.0]);
        assert_eq!(components.len(), 3);
        assert_eq!(components[0].len(), 27);
        // Spot-check: element 5 of Z component = 5 * 0.1 = 0.5.
        assert_eq!(components[0][5], 0.5);
        // Element 10 of Y component = -(10 * 0.05) = -0.5.
        assert_eq!(components[1][10], -0.5);
    } else {
        panic!("expected BSpline variant");
    }
}

// -- composite construction ------------------------------------------

#[test]
fn composite_construction_and_accessors() {
    let mut ct = CompositeTransform::new(3);
    assert_eq!(ct.dimensionality, 3);
    assert!(ct.is_empty());
    assert_eq!(ct.len(), 0);

    ct.push(TransformDescription::Translation {
        offset: vec![1.0, 0.0, 0.0],
    });
    assert_eq!(ct.len(), 1);
    assert!(!ct.is_empty());

    ct.push(TransformDescription::Rigid {
        rotation: rotation_z_90_3x3(),
        translation: vec![0.0, 0.0, 0.0],
    });
    assert_eq!(ct.len(), 2);

    ct.push(TransformDescription::Affine {
        matrix: identity_4x4(),
    });
    assert_eq!(ct.len(), 3);

    // Validate dimensionality consistency.
    let mismatches = ct.validate_dimensionality();
    assert!(
        mismatches.is_empty(),
        "expected no dimensionality mismatches, got indices: {:?}",
        mismatches
    );
}

#[test]
fn composite_roundtrip_json_string() {
    let mut ct = CompositeTransform::new(3);
    ct.description = "test composite: translate then rotate".into();
    ct.push(TransformDescription::Translation {
        offset: vec![5.0, -3.0, 0.0],
    });
    ct.push(TransformDescription::Rigid {
        rotation: rotation_z_90_3x3(),
        translation: vec![0.0, 0.0, 0.0],
    });

    let json = ct.to_json().expect("to_json");
    let back = CompositeTransform::from_json(&json).expect("from_json");

    assert_eq!(ct, back);
    assert_eq!(back.dimensionality, 3);
    assert_eq!(back.description, "test composite: translate then rotate");
    assert_eq!(back.len(), 2);

    // Inspect first transform value.
    if let TransformDescription::Translation { offset } = &back.transforms[0] {
        assert_eq!(offset, &[5.0, -3.0, 0.0]);
    } else {
        panic!("expected Translation at index 0");
    }
}

#[test]
fn composite_default_description_absent_in_json() {
    // When `description` is absent from the JSON payload, it must default
    // to an empty string via `#[serde(default)]`.
    let json = r#"{"dimensionality":2,"transforms":[]}"#;
    let ct = CompositeTransform::from_json(json).expect("from_json");
    assert_eq!(ct.dimensionality, 2);
    assert_eq!(ct.description, "");
    assert!(ct.is_empty());
}

// -- file I/O --------------------------------------------------------

#[test]
fn composite_file_io_roundtrip() {
    let dir = tempfile::tempdir().expect("create tempdir");
    let file_path = dir.path().join("composite.json");

    let mut ct = CompositeTransform::new(3);
    ct.description = "file I/O test".into();
    ct.push(TransformDescription::Translation {
        offset: vec![1.0, 2.0, 3.0],
    });
    ct.push(TransformDescription::Affine {
        matrix: identity_4x4(),
    });

    ct.save_json(&file_path).expect("save_json");

    // Verify the file exists and has non-zero length.
    let metadata = std::fs::metadata(&file_path).expect("file metadata");
    assert!(metadata.len() > 0);

    let loaded = CompositeTransform::load_json(&file_path).expect("load_json");
    assert_eq!(ct, loaded);

    // Value-level check on loaded data.
    assert_eq!(loaded.dimensionality, 3);
    assert_eq!(loaded.description, "file I/O test");
    assert_eq!(loaded.len(), 2);
    if let TransformDescription::Translation { offset } = &loaded.transforms[0] {
        assert_eq!(offset, &[1.0, 2.0, 3.0]);
    } else {
        panic!("expected Translation at index 0");
    }
    if let TransformDescription::Affine { matrix } = &loaded.transforms[1] {
        assert_eq!(matrix.len(), 16);
        assert_eq!(matrix[0], 1.0);
        assert_eq!(matrix[15], 1.0);
    } else {
        panic!("expected Affine at index 1");
    }
}

#[test]
fn load_json_nonexistent_file_returns_io_error() {
    let result = CompositeTransform::load_json("/nonexistent/path/composite.json");
    let err = result.expect_err("the call must be rejected");
    assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
}

// -- invalid JSON ----------------------------------------------------

#[test]
fn from_json_invalid_json_returns_error() {
    let bad_json = r#"{"dimensionality": 3, "transforms": [{"Translation": {}}]}"#;
    let result = CompositeTransform::from_json(bad_json);
    assert_rejects(result, "missing field `offset` at line 1 column 55");
}

#[test]
fn from_json_truncated_payload_returns_error() {
    let truncated = r#"{"dimensionality": 3, "transf"#;
    let result = CompositeTransform::from_json(truncated);
    assert_rejects(result, "EOF while parsing a string at line 1 column 29");
}

#[test]
fn from_json_wrong_type_returns_error() {
    // `dimensionality` must be an integer, not a string.
    let bad = r#"{"dimensionality": "three", "transforms": []}"#;
    let result = CompositeTransform::from_json(bad);
    assert_rejects(result, "invalid type: string");
}

// -- dimensionality validation ---------------------------------------

#[test]
fn validate_detects_mismatched_dimensionality() {
    let mut ct = CompositeTransform::new(3);
    // Correct: 3-D translation.
    ct.push(TransformDescription::Translation {
        offset: vec![1.0, 2.0, 3.0],
    });
    // Wrong: 2-D translation in a 3-D composite.
    ct.push(TransformDescription::Translation {
        offset: vec![1.0, 2.0],
    });

    let bad = ct.validate_dimensionality();
    assert_eq!(bad, vec![1]);
}

#[test]
fn implied_dimensionality_rigid_consistency() {
    let good = TransformDescription::Rigid {
        rotation: rotation_z_90_3x3(),
        translation: vec![0.0, 0.0, 0.0],
    };
    assert_eq!(good.implied_dimensionality(), Some(3));

    // Inconsistent: 9-element rotation but 2-element translation.
    let bad = TransformDescription::Rigid {
        rotation: rotation_z_90_3x3(),
        translation: vec![0.0, 0.0],
    };
    assert_eq!(bad.implied_dimensionality(), None);
}

#[test]
fn implied_dimensionality_affine() {
    // 4×4 = 16 elements → D = 3.
    assert_eq!(
        TransformDescription::Affine {
            matrix: identity_4x4()
        }
        .implied_dimensionality(),
        Some(3)
    );
    // 3×3 = 9 elements → D = 2.
    let id_3x3 = vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
    assert_eq!(
        TransformDescription::Affine { matrix: id_3x3 }.implied_dimensionality(),
        Some(2)
    );
    // Non-square element count → None.
    assert_eq!(
        TransformDescription::Affine {
            matrix: vec![1.0; 7]
        }
        .implied_dimensionality(),
        None
    );
}

// -- composite with all variant types --------------------------------

#[test]
fn composite_all_variants_roundtrip() {
    let dims = vec![2, 2, 2];
    let n: usize = dims.iter().product();

    let mut ct = CompositeTransform::new(3);
    ct.description = "all-variants composite".into();

    ct.push(TransformDescription::Translation {
        offset: vec![1.0, 2.0, 3.0],
    });
    ct.push(TransformDescription::Rigid {
        rotation: rotation_z_90_3x3(),
        translation: vec![0.0, 5.0, 0.0],
    });
    ct.push(TransformDescription::Affine {
        matrix: identity_4x4(),
    });
    ct.push(TransformDescription::DisplacementField {
        dims: dims.clone(),
        origin: vec![0.0, 0.0, 0.0],
        spacing: vec![1.0, 1.0, 1.0],
        components: vec![vec![0.0; n], vec![0.0; n], vec![0.0; n]],
    });
    ct.push(TransformDescription::BSpline {
        grid_dims: vec![3, 3, 3],
        grid_origin: vec![-1.0, -1.0, -1.0],
        grid_spacing: vec![1.0, 1.0, 1.0],
        components: vec![vec![0.0; 27], vec![0.0; 27], vec![0.0; 27]],
    });

    assert_eq!(ct.len(), 5);
    assert!(ct.validate_dimensionality().is_empty());

    let json = ct.to_json().expect("to_json");
    let back = CompositeTransform::from_json(&json).expect("from_json");
    assert_eq!(ct, back);
    assert_eq!(back.len(), 5);

    // Spot-check: second transform is Rigid with translation[1] = 5.0.
    if let TransformDescription::Rigid { translation, .. } = &back.transforms[1] {
        assert_eq!(translation[1], 5.0);
    } else {
        panic!("expected Rigid at index 1");
    }
}