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
use serde::{Deserialize, Serialize};
use traffloat_types::space::{Matrix, Vector};
use xylem::{DefaultContext, NoArgs, Xylem};

use crate::Schema;

impl Xylem<Schema> for Matrix {
    type From = Vec<TransformPrimitive>;
    type Args = NoArgs;

    fn convert_impl(
        from: Self::From,
        _: &mut DefaultContext,
        _: &Self::Args,
    ) -> anyhow::Result<Self> {
        let mut matrix = Matrix::identity();
        for primitive in from {
            matrix = primitive.to_matrix() * matrix;
        }
        Ok(matrix)
    }
}

/// A primitive linear transformation operation.
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum TransformPrimitive {
    /// A translation operation.
    Translate {
        /// The distance to translate along the X axis. Default 0.
        #[serde(default)]
        x: f64,
        /// The distance to translate along the Y axis. Default 0.
        #[serde(default)]
        y: f64,
        /// The distance to translate along the Z axis. Default 0.
        #[serde(default)]
        z: f64,
    },
    /// A scaling operation.
    Scale {
        /// The ratio to scale along the X axis. Default 1.
        #[serde(default = "serde_one")]
        x: f64,
        /// The ratio to scale along the Y axis. Default 1.
        #[serde(default = "serde_one")]
        y: f64,
        /// The ratio to scale along the Z axis. Default 1.
        #[serde(default = "serde_one")]
        z: f64,
    },
}

impl TransformPrimitive {
    /// Represent this primitive as a transformation matrix.
    fn to_matrix(&self) -> Matrix {
        match *self {
            Self::Translate { x, y, z } => Matrix::new_translation(&Vector::new(x, y, z)),
            Self::Scale { x, y, z } => Matrix::new_nonuniform_scaling(&Vector::new(x, y, z)),
        }
    }
}

fn serde_one() -> f64 { 1. }