makepad_vector/geometry/
affine_transformation.rs1use crate::geometry::{LinearTransformation, Point, Transform, Transformation, Vector};
2
3#[derive(Clone, Copy, Debug, PartialEq)]
4#[repr(C)]
5pub struct AffineTransformation {
6 pub xy: LinearTransformation,
7 pub z: Vector,
8}
9
10impl AffineTransformation {
11 pub fn new(xy: LinearTransformation, z: Vector) -> AffineTransformation {
12 AffineTransformation { xy, z }
13 }
14
15 pub fn identity() -> AffineTransformation {
16 AffineTransformation::new(LinearTransformation::identity(), Vector::zero())
17 }
18
19 pub fn scaling(v: Vector) -> AffineTransformation {
20 AffineTransformation::new(LinearTransformation::scaling(v), Vector::zero())
21 }
22
23 pub fn uniform_scaling(k: f64) -> AffineTransformation {
24 AffineTransformation::new(LinearTransformation::uniform_scaling(k), Vector::zero())
25 }
26
27 pub fn translation(v: Vector) -> AffineTransformation {
28 AffineTransformation::new(LinearTransformation::identity(), v)
29 }
30
31 pub fn scale(self, v: Vector) -> AffineTransformation {
32 AffineTransformation::new(self.xy.scale(v), self.z.scale(v))
33 }
34
35 pub fn uniform_scale(self, k: f64) -> AffineTransformation {
36 AffineTransformation::new(self.xy.uniform_scale(k), self.z * k)
37 }
38
39 pub fn translate(self, v: Vector) -> AffineTransformation {
40 AffineTransformation::new(self.xy, self.z + v)
41 }
42}
43
44impl Transformation for AffineTransformation {
45 fn transform_point(&self, p: Point) -> Point {
46 p.transform(&self.xy) + self.z
47 }
48
49 fn transform_vector(&self, v: Vector) -> Vector {
50 v.transform(&self.xy)
51 }
52}