summaryrefslogtreecommitdiff
path: root/gate/src/renderer/geom.rs
blob: 619555f34bdbaa5f52390da6674d83b12bc03df3 (plain)
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
// Copyright 2017-2018 Matthew D. Michelotti
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops::{Add, Mul};

// 2D Cartesian vector
#[derive(Copy, Clone)]
pub struct Vec2 { pub x: f64, pub y: f64 }

impl Vec2 {
    pub fn new(x: f64, y: f64) -> Vec2 { Vec2 { x, y } }
    pub fn zero() -> Vec2 { Vec2::new(0.0, 0.0) }
    pub fn len(&self) -> f64 { (self.x * self.x + self.y * self.y).sqrt() }
}

impl Add<Vec2> for Vec2 {
    type Output = Vec2;
    fn add(self, rhs: Vec2) -> Vec2 { Vec2::new(self.x + rhs.x, self.y + rhs.y) }
}

// Diagonal 2D matrix
#[derive(Copy, Clone)]
struct Diag2 { x: f64, y: f64 }

impl Diag2 {
    fn new(x: f64, y: f64) -> Diag2 { Diag2 { x, y } }
}

impl Mul<Vec2> for Diag2 {
    type Output = Vec2;
    fn mul(self, rhs: Vec2) -> Vec2 { Vec2::new(self.x*rhs.x, self.y*rhs.y) }
}

impl Mul<Vec2> for f64 {
    type Output = Vec2;
    fn mul(self, rhs: Vec2) -> Vec2 { Vec2::new(self * rhs.x, self * rhs.y) }
}

// General 2D matrix
#[derive(Copy, Clone)]
pub struct Mat2 { a: f64, b: f64, c: f64, d: f64 }

impl Mat2 {
    fn id() -> Mat2 { Mat2 { a: 1.0, b: 0.0, c: 0.0, d: 1.0 } }

    fn rotation(angle: f64) -> Mat2 {
        let (sin, cos) = (angle.sin(), angle.cos());
        let (sin, cos) = if sin.abs() < 1e-7 || cos.abs() < 1e-7 {
            (sin.round(), cos.round())
        } else {
            (sin, cos)
        };
        Mat2 { a: cos, b: -sin, c: sin, d: cos }
    }

    pub fn col_0(&self) -> Vec2 { Vec2::new(self.a, self.c) }
    pub fn col_1(&self) -> Vec2 { Vec2::new(self.b, self.d) }
}

impl Mul<Vec2> for Mat2 {
    type Output = Vec2;
    fn mul(self, rhs: Vec2) -> Vec2 {
        Vec2::new(self.a * rhs.x + self.b * rhs.y, self.c * rhs.x + self.d * rhs.y)
    }
}

impl Mul<Mat2> for Mat2 {
    type Output = Mat2;
    fn mul(self, rhs: Mat2) -> Mat2 {
        Mat2 {
            a: self.a * rhs.a + self.b * rhs.c,
            b: self.a * rhs.b + self.b * rhs.d,
            c: self.c * rhs.a + self.d * rhs.c,
            d: self.c * rhs.b + self.d * rhs.d,
        }
    }
}

impl Mul<Diag2> for Mat2 {
    type Output = Mat2;
    fn mul(self, rhs: Diag2) -> Mat2 {
        Mat2 { a: self.a * rhs.x, b: self.b * rhs.y, c: self.c * rhs.x, d: self.d * rhs.y }
    }
}

impl Mul<Mat2> for Diag2 {
    type Output = Mat2;
    fn mul(self, rhs: Mat2) -> Mat2 {
        Mat2 { a: self.x * rhs.a, b: self.x * rhs.b, c: self.y * rhs.c, d: self.y * rhs.d }
    }
}

impl Mul<Mat2> for f64 {
    type Output = Mat2;
    fn mul(self, rhs: Mat2) -> Mat2 {
        Mat2 { a: self * rhs.a, b: self * rhs.b, c: self * rhs.c, d: self * rhs.d }
    }
}

/// Represents an affine transformation in 2D space.
#[derive(Copy, Clone)]
pub struct Affine { mat: Mat2, offset: Vec2 }

impl Affine {
    /// Identity transformation.
    #[inline]
    pub fn id() -> Affine { Affine { mat: Mat2::id(), offset: Vec2::zero() } }

    /// Returns a translation transformation.
    pub fn translate(x_offset: f64, y_offset: f64) -> Affine {
        Affine { mat: Mat2::id(), offset: Vec2::new(x_offset, y_offset) }
    }

    /// Returns a rotation transformation, rotating counter-clockwise by `angle` radians.
    pub fn rotate(angle: f64) -> Affine {
        Affine { mat: Mat2::rotation(angle), offset: Vec2::zero() }
    }

    /// Returns a scaling transformation, scaling x and y axes separately.
    pub fn scale_axes(scale_x: f64, scale_y: f64) -> Affine {
        Affine { mat: Mat2 { a: scale_x, b: 0., c: 0., d: scale_y }, offset: Vec2::zero() }
    }

    /// Returns a scaling transformation, scaling x and y axes identically.
    pub fn scale(scale: f64) -> Affine {
        Affine::scale_axes(scale, scale)
    }

    /// Returns a transformation that is functionally equivalent to `self` composed with `rhs`.
    ///
    /// This means that the `rhs` transformation is invoked first, and then the `self`
    /// transformation is invoked on the output of that.
    /// This is usually the desired ordering with graphics transformations.
    pub fn pre_transform(&self, rhs: &Affine) -> Affine {
        Affine {
            mat: self.mat * rhs.mat,
            offset: self.offset + self.mat * rhs.offset,
        }
    }

    /// Logically equivalent to `self.pre_transform(&Affine::scale_axes(scale_x, scale_y))`.
    pub fn pre_scale_axes(&self, scale_x: f64, scale_y: f64) -> Affine {
        Affine {
            mat: self.mat * Diag2::new(scale_x, scale_y),
            offset: self.offset,
        }
    }

    /// Logically equivalent to `self.pre_transform(&Affine::scale(scale))`.
    pub fn pre_scale(&self, scale: f64) -> Affine {
        Affine {
            mat: scale * self.mat,
            offset: self.offset,
        }
    }

    /// Logically equivalent to `self.pre_transform(&Affine::rotate(angle))`.
    pub fn pre_rotate(&self, angle: f64) -> Affine {
        Affine {
            mat: self.mat * Mat2::rotation(angle),
            offset: self.offset,
        }
    }

    /// Logically equivalent to `self.pre_transform(&Affine::translate(x_offset, y_offset))`.
    pub fn pre_translate(&self, x_offset: f64, y_offset: f64) -> Affine {
        Affine {
            mat: self.mat,
            offset: self.offset + self.mat * Vec2::new(x_offset, y_offset),
        }
    }

    /// Logically equivalent to `Affine::scale_axes(scale_x, scale_y).pre_transform(self)`.
    pub fn post_scale_axes(&self, scale_x: f64, scale_y: f64) -> Affine {
        let scale = Diag2::new(scale_x, scale_y);
        Affine {
            mat: scale * self.mat,
            offset: scale * self.offset,
        }
    }

    /// Logically equivalent to `Affine::scale(scale).pre_transform(self)`.
    pub fn post_scale(&self, scale: f64) -> Affine {
        Affine {
            mat: scale * self.mat,
            offset: scale * self.offset,
        }
    }

    /// Logically equivalent to `Affine::rotate(angle).pre_transform(self)`.
    pub fn post_rotate(&self, angle: f64) -> Affine {
        let rotation = Mat2::rotation(angle);
        Affine {
            mat: rotation * self.mat,
            offset: rotation * self.offset,
        }
    }

    /// Logically equivalent to `Affine::translate(x_offset, y_offset).pre_transform(self)`.
    pub fn post_translate(&self, x_offset: f64, y_offset: f64) -> Affine {
        Affine {
            mat: self.mat,
            offset: self.offset + Vec2::new(x_offset, y_offset),
        }
    }

    pub fn invert_translate(&self) -> Affine {
        Affine {
            mat: self.mat,
            offset: -1. * self.offset
        }
    }

    pub fn apply(&self, input: Vec2) -> Vec2 { self.mat * input + self.offset }

    pub fn apply_f32(&self, input: (f32, f32)) -> (f32, f32) {
        let input = Vec2::new(input.0 as f64, input.1 as f64);
        let result = self.apply(input);
        (result.x as f32, result.y as f32)
    }

    pub fn apply_f64(&self, input: (f64, f64)) -> (f64, f64) {
        let input = Vec2::new(input.0 as f64, input.1 as f64);
        let result = self.apply(input);
        (result.x, result.y)
    }

    pub(crate) fn mat(&self) -> &Mat2 { &self.mat }
}