Skip to main content

cadmpeg_ir/
transform.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Rigid transforms.
3
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7/// A 4×4 row-major affine transform applied to a body's geometry.
8///
9/// The explicit matrix preserves source coefficients. Validation checks the
10/// affine bottom row.
11#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
12pub struct Transform {
13    /// Row-major 4×4 matrix; `rows[3]` is normally `[0, 0, 0, 1]`.
14    pub rows: [[f64; 4]; 4],
15}
16
17impl Transform {
18    /// The identity transform.
19    pub fn identity() -> Self {
20        Transform {
21            rows: [
22                [1.0, 0.0, 0.0, 0.0],
23                [0.0, 1.0, 0.0, 0.0],
24                [0.0, 0.0, 1.0, 0.0],
25                [0.0, 0.0, 0.0, 1.0],
26            ],
27        }
28    }
29
30    /// Whether every matrix coefficient is finite.
31    pub fn is_finite(&self) -> bool {
32        self.rows.iter().flatten().all(|value| value.is_finite())
33    }
34}
35
36impl Default for Transform {
37    fn default() -> Self {
38        Transform::identity()
39    }
40}