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)
}
}
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum TransformPrimitive {
Translate {
#[serde(default)]
x: f64,
#[serde(default)]
y: f64,
#[serde(default)]
z: f64,
},
Scale {
#[serde(default = "serde_one")]
x: f64,
#[serde(default = "serde_one")]
y: f64,
#[serde(default = "serde_one")]
z: f64,
},
}
impl TransformPrimitive {
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. }