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
use parse_display::{Display, FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Co-ordinate axis specifier.
///
/// See [cglearn.eu] for background reading.
///
/// [cglearn.eu]: https://cglearn.eu/pub/computer-graphics/introduction-to-geometry#material-coordinate-systems-1
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum Axis {
    /// 'Y' axis.
    Y = 1,
    /// 'Z' axis.
    Z = 2,
}

/// Specifies the sign of a co-ordinate axis.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum Direction {
    /// Increasing numbers.
    Positive = 1,
    /// Decreasing numbers.
    Negative = -1,
}

impl std::ops::Mul for Direction {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        match self as i32 * rhs as i32 {
            1 => Direction::Positive,
            -1 => Direction::Negative,
            _ => unreachable!(),
        }
    }
}

/// An [`Axis`] paired with a [`Direction`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[display("({axis}, {direction})")]
pub struct AxisDirectionPair {
    /// Axis specifier.
    pub axis: Axis,

    /// Specifies which direction the axis is pointing.
    pub direction: Direction,
}

/// Co-ordinate system definition.
///
/// The `up` axis must be orthogonal to the `forward` axis.
///
/// See [cglearn.eu] for background reading.
///
/// [cglearn.eu](https://cglearn.eu/pub/computer-graphics/introduction-to-geometry#material-coordinate-systems-1)
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[display("forward: {forward}, up: {up}")]
pub struct System {
    /// Axis the front face of a model looks along.
    pub forward: AxisDirectionPair,
    /// Axis pointing up and away from a model.
    pub up: AxisDirectionPair,
}

/// KittyCAD co-ordinate system.
///
/// * Forward: -Y
/// * Up: +Z
/// * Handedness: Right
pub const KITTYCAD: &System = &System {
    // -Y
    forward: AxisDirectionPair {
        axis: Axis::Y,
        direction: Direction::Negative,
    },
    // +Z
    up: AxisDirectionPair {
        axis: Axis::Z,
        direction: Direction::Positive,
    },
};

/// OpenGL co-ordinate system.
///
/// * Forward: +Z
/// * Up: +Y
/// * Handedness: Right
pub const OPENGL: &System = &System {
    // +Z
    forward: AxisDirectionPair {
        axis: Axis::Z,
        direction: Direction::Positive,
    },
    // +Y
    up: AxisDirectionPair {
        axis: Axis::Y,
        direction: Direction::Positive,
    },
};

/// Vulkan co-ordinate system.
///
/// * Forward: +Z
/// * Up: -Y
/// * Handedness: Left
pub const VULKAN: &System = &System {
    // +Z
    forward: AxisDirectionPair {
        axis: Axis::Z,
        direction: Direction::Positive,
    },
    // -Y
    up: AxisDirectionPair {
        axis: Axis::Y,
        direction: Direction::Negative,
    },
};

/// Perform co-ordinate system transform.
///
/// # Examples
///
/// KittyCAD (+Z up, -Y forward) to OpenGL (+Y up, +Z forward):
///
/// ```
/// # use format::coord::*;
/// let a = [1.0, 2.0, 3.0];
/// let b = transform(a, KITTYCAD, OPENGL);
/// assert_eq!(b, [1.0, 3.0, -2.0]);
/// ```
///
/// OpenGL (+Y up, +Z forward) to KittyCAD (+Z up, -Y forward):
///
/// ```
/// # use format::coord::*;
/// let a = [1.0, 2.0, 3.0];
/// let b = transform(a, OPENGL, KITTYCAD);
/// assert_eq!(b, [1.0, -3.0, 2.0]);
/// ```
///
/// KittyCAD (+Z up, -Y forward) to Vulkan (-Y up, +Z forward):
///
/// ```
/// # use format::coord::*;
/// let a = [1.0, 2.0, 3.0];
/// let b = transform(a, KITTYCAD, VULKAN);
/// assert_eq!(b, [1.0, -3.0, -2.0]);
/// ```
///
/// OpenGL (+Y up, +Z forward) to Vulkan (-Y up, +Z forward):
///
/// ```
/// # use format::coord::*;
/// let a = [1.0, 2.0, 3.0];
/// let b = transform(a, OPENGL, VULKAN);
/// assert_eq!(b, [1.0, -2.0, 3.0]);
/// ```
#[inline]
pub fn transform(a: [f32; 3], from: &System, to: &System) -> [f32; 3] {
    let mut b = a;
    b[to.forward.axis as usize] =
        (from.forward.direction * to.forward.direction) as i32 as f32 * a[from.forward.axis as usize];
    b[to.up.axis as usize] = (from.up.direction * to.up.direction) as i32 as f32 * a[from.up.axis as usize];
    b
}