1use aylis_core::{Path, PathCmd, PointF, RectF};
4
5#[derive(Clone, Copy, Debug, PartialEq)]
7pub struct Affine {
8 pub a: f32,
9 pub b: f32,
10 pub c: f32,
11 pub d: f32,
12 pub e: f32,
13 pub f: f32,
14}
15
16impl Default for Affine {
17 fn default() -> Self {
18 Self::IDENTITY
19 }
20}
21
22impl Affine {
23 pub const IDENTITY: Affine = Affine { a: 1.0, b: 0.0, c: 0.0, d: 1.0, e: 0.0, f: 0.0 };
24
25 pub fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
26 Self { a, b, c, d, e, f }
27 }
28
29 pub fn translate(x: f32, y: f32) -> Self {
30 Self::new(1.0, 0.0, 0.0, 1.0, x, y)
31 }
32
33 pub fn scale(x: f32, y: f32) -> Self {
34 Self::new(x, 0.0, 0.0, y, 0.0, 0.0)
35 }
36
37 pub fn rotate(degrees: f32) -> Self {
39 let (s, c) = degrees.to_radians().sin_cos();
40 Self::new(c, s, -s, c, 0.0, 0.0)
41 }
42
43 pub fn skew_x(degrees: f32) -> Self {
44 Self::new(1.0, 0.0, degrees.to_radians().tan(), 1.0, 0.0, 0.0)
45 }
46
47 pub fn skew_y(degrees: f32) -> Self {
48 Self::new(1.0, degrees.to_radians().tan(), 0.0, 1.0, 0.0, 0.0)
49 }
50
51 pub fn then(self, other: Affine) -> Affine {
53 let m = self;
54 let o = other;
55 Affine {
56 a: m.a * o.a + m.c * o.b,
57 b: m.b * o.a + m.d * o.b,
58 c: m.a * o.c + m.c * o.d,
59 d: m.b * o.c + m.d * o.d,
60 e: m.a * o.e + m.c * o.f + m.e,
61 f: m.b * o.e + m.d * o.f + m.f,
62 }
63 }
64
65 pub fn apply(&self, p: PointF) -> PointF {
66 PointF::new(self.a * p.x + self.c * p.y + self.e, self.b * p.x + self.d * p.y + self.f)
67 }
68
69 pub fn scale_factor(&self) -> f32 {
71 (self.a * self.d - self.b * self.c).abs().sqrt()
72 }
73
74 pub fn path(&self, path: &Path) -> Path {
75 path.map_points(|p| self.apply(p))
76 }
77
78 pub fn rect_to_rect(from: RectF, to: RectF) -> Affine {
80 let sx = if from.width > 0.0 { to.width / from.width } else { 1.0 };
81 let sy = if from.height > 0.0 { to.height / from.height } else { 1.0 };
82 Affine::new(sx, 0.0, 0.0, sy, to.x - from.x * sx, to.y - from.y * sy)
83 }
84}
85
86pub fn rect_path(m: &Affine, r: RectF) -> Path {
88 let mut p = Path::new();
89 let pts = [PointF::new(r.x, r.y), PointF::new(r.right(), r.y), PointF::new(r.right(), r.bottom()), PointF::new(r.x, r.bottom())];
90 let pts: Vec<PointF> = pts.iter().map(|q| m.apply(*q)).collect();
91 p.polygon(&pts, true);
92 p
93}
94
95pub fn trim(path: &Path, start: f32, end: f32, offset: f32) -> Path {
99 let (mut s, mut e) = (start.min(end), start.max(end));
100 if e - s >= 0.9999 {
101 return path.clone();
102 }
103 if e - s <= 0.0001 {
104 return Path::new();
105 }
106 s += offset;
107 e += offset;
108 let shift = s.floor();
109 s -= shift;
110 e -= shift;
111 let mut out = Path::new();
112 for (mut points, closed) in path.flatten(24) {
113 if closed && points.len() > 1 {
114 points.push(points[0]);
115 }
116 let lengths: Vec<f32> = points.windows(2).map(|w| dist(w[0], w[1])).collect();
117 let total: f32 = lengths.iter().sum();
118 if total <= 0.0 {
119 continue;
120 }
121 let mut pieces = vec![(s, e.min(1.0))];
123 if e > 1.0 {
124 pieces.push((0.0, e - 1.0));
125 }
126 for (a, b) in pieces {
127 let (from, to) = (a * total, b * total);
128 let mut acc = 0.0;
129 let mut started = false;
130 for (i, len) in lengths.iter().enumerate() {
131 let (p0, p1) = (points[i], points[i + 1]);
132 let (seg_start, seg_end) = (acc, acc + len);
133 acc = seg_end;
134 if seg_end < from || seg_start > to || *len <= 0.0 {
135 continue;
136 }
137 let at = |d: f32| {
138 let t = ((d - seg_start) / len).clamp(0.0, 1.0);
139 PointF::new(p0.x + (p1.x - p0.x) * t, p0.y + (p1.y - p0.y) * t)
140 };
141 if !started {
142 let p = at(from.max(seg_start));
143 out.move_to(p.x, p.y);
144 started = true;
145 }
146 let p = at(to.min(seg_end));
147 out.line_to(p.x, p.y);
148 }
149 }
150 }
151 out
152}
153
154pub fn dist(a: PointF, b: PointF) -> f32 {
155 ((a.x - b.x).powi(2) + (a.y - b.y).powi(2)).sqrt()
156}
157
158pub fn has_figures(path: &Path) -> bool {
160 path.cmds.iter().any(|c| !matches!(c, PathCmd::MoveTo(_) | PathCmd::Close))
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 #[test]
168 fn compose_and_apply() {
169 let m = Affine::translate(10.0, 0.0).then(Affine::scale(2.0, 2.0));
170 assert_eq!(m.apply(PointF::new(1.0, 1.0)), PointF::new(12.0, 2.0));
171 let r = Affine::rotate(90.0).apply(PointF::new(1.0, 0.0));
172 assert!((r.x).abs() < 1e-5 && (r.y - 1.0).abs() < 1e-5, "{r:?}");
173 assert!((Affine::scale(2.0, 8.0).scale_factor() - 4.0).abs() < 1e-5);
174 }
175
176 #[test]
177 fn trim_keeps_part_of_a_line() {
178 let mut p = Path::new();
179 p.move_to(0.0, 0.0).line_to(100.0, 0.0);
180 let t = trim(&p, 0.25, 0.5, 0.0);
181 let b = t.bounds();
182 assert!((b.x - 25.0).abs() < 0.5 && (b.right() - 50.0).abs() < 0.5, "{b:?}");
183 assert!(trim(&p, 0.3, 0.3, 0.0).is_empty());
184 }
185}