summaryrefslogtreecommitdiffstats
path: root/svgbob/src/buffer/fragment_buffer/fragment/arc.rs
blob: 5d7963ef5abc147b8607dcabcaca5066958195b7 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::{buffer::Cell, fragment::Bounds, util, Point};
use ncollide2d::shape::{Segment, Shape};
use sauron::{
    html::attributes::*,
    svg::{attributes::*, *},
    Node,
};
use std::{cmp::Ordering, fmt};

#[derive(Debug, Clone)]
pub struct Arc {
    pub start: Point,
    pub end: Point,
    pub radius: f32,
    major_flag: bool,
    pub sweep_flag: bool,
    rotation_flag: bool,
}

impl Arc {
    /// create an arc from start to end with a radius
    /// direction is counter clock wise
    pub(in crate) fn new(start: Point, end: Point, radius: f32) -> Self {
        let mut arc = Arc {
            start,
            end,
            radius,
            /// always false, since arcs are mostly in minor arc
            major_flag: false,
            sweep_flag: false,
            rotation_flag: false,
        };
        arc.sort_reorder_end_points();
        arc
    }

    /// check if this arcs to point a, b
    /// disregarding radius
    pub(in crate) fn arcs_to(&self, a: Point, b: Point) -> bool {
        let arc = Arc::new(a, b, 1.0);
        self.start == arc.start
            && self.end == arc.end
            && self.sweep_flag == arc.sweep_flag
    }

    pub(in crate) fn new_with_sweep(
        start: Point,
        end: Point,
        radius: f32,
        sweep_flag: bool,
    ) -> Self {
        let mut arc = Arc {
            start,
            end,
            radius,
            /// always false, since arcs are mostly in minor arc
            major_flag: false,
            sweep_flag,
            rotation_flag: false,
        };
        arc.sort_reorder_end_points();
        arc
    }

    pub(in crate) fn absolute_position(&self, cell: Cell) -> Self {
        Arc {
            start: cell.absolute_position(self.start),
            end: cell.absolute_position(self.end),
            ..*self
        }
    }

    /// reverse the order of points and also set the flag to true, to
    /// make the rotation clockwise
    pub(in crate) fn sort_reorder_end_points(&mut self) {
        if self.start > self.end {
            let tmp_start = self.start;
            self.start = self.end;
            self.end = tmp_start;
            self.sweep_flag = !self.sweep_flag;
        }
    }

    pub fn scale(&self, scale: f32) -> Self {
        Arc {
            start: self.start.scale(scale),
            end: self.end.scale(scale),
            radius: self.radius * scale,
            ..*self
        }
    }

    /// check to see of this arc is touching the other arc
    pub(in crate) fn is_touching(&self, other: &Self) -> bool {
        self.start == other.start
            || self.end == other.end
            || self.start == other.end
            || self.end == other.start
    }

    pub fn has_endpoint(&self, p: Point) -> bool {
        self.start == p || self.end == p
    }

    /// calculate the center point this arc
    pub fn center(&self) -> Point {
        let start = self.start;
        let end = self.end;
        let q = start.distance(&end);
        let y3 = (start.y + end.y) / 2.0;
        let x3 = (start.x + end.x) / 2.0;

        let rr_q22 = (self.radius.powf(2.0) - (q / 2.0).powf(2.0)).sqrt();

        let base_x = rr_q22 * (start.y - end.y) / q;
        let base_y = rr_q22 * (end.x - start.x) / q;

        if self.sweep_flag {
            let cx = x3 + base_x;
            let cy = y3 + base_y;
            Point::new(cx, cy)
        } else {
            let cx = x3 - base_x;
            let cy = y3 - base_y;
            Point::new(cx, cy)
        }
    }

    /// check to see if the arc is aabb right angle
    /// that is the center x and y coordinate is alinged to both of the end points
    /// This will be used for checking if group of fragments can be a rounded rect
    pub fn is_aabb_right_angle_arc(&self) -> bool {
        let center = self.center();
        (center.x == self.start.x && center.y == self.end.y)
            || (center.x == self.end.x && center.y == self.start.y)
    }
}

impl Bounds for Arc {
    fn bounds(&self) -> (Point, Point) {
        let aabb = Segment::new(*self.start, *self.end).local_aabb();
        (Point::from(*aabb.mins), Point::from(*aabb.maxs))
    }
}

impl fmt::Display for Arc {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "A {} {} {} -> {} {} {}",
            self.start,
            self.end,
            self.radius,
            self.rotation_flag as u8,
            self.major_flag as u8,
            self.sweep_flag as u8,
        )
    }
}

impl<MSG> Into<Node<MSG>> for Arc {
    fn into(self) -> Node<MSG> {
        let dv = format!(
            "M {},{} A {},{} {},{},{} {},{}",
            self.start.x,
            self.start.y,
            self.radius,
            self.radius,
            self.rotation_flag as u8,
            self.major_flag as u8,
            self.sweep_flag as u8,
            self.end.x,
            self.end.y
        );
        path(vec![d(dv), class("nofill")], vec![])
    }
}

impl Eq for Arc {}

impl Ord for Arc {
    fn cmp(&self, other: &Self) -> Ordering {
        self.start
            .cmp(&other.start)
            .then(self.end.cmp(&other.end))
            .then(util::ord(self.radius, other.radius))
            .then(self.rotation_flag.cmp(&other.rotation_flag))
            .then(self.major_flag.cmp(&other.major_flag))
            .then(self.sweep_flag.cmp(&other.sweep_flag))
    }
}

impl PartialOrd for Arc {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for Arc {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::buffer::CellGrid;

    #[test]
    fn test_arc_centers() {
        let e = CellGrid::e();
        let y = CellGrid::y();
        let o = CellGrid::o();
        let arc = Arc::new(e, y, 1.0);
        assert_eq!(o, arc.center());
        assert!(!arc.is_aabb_right_angle_arc());
    }

    #[test]
    fn test_arc_ke_center_a() {
        let a = CellGrid::a();
        let e = CellGrid::e();
        let k = CellGrid::k();
        let _o = CellGrid::o();
        let arc = Arc::new(k, e, 1.0);
        // 1st, up, ltr, swapped, steepup
        assert_eq!(a, arc.center());
        assert!(arc.is_aabb_right_angle_arc());
    }

    #[test]
    fn test_arc_ao_center_e() {
        let a = CellGrid::a();
        let e = CellGrid::e();
        let o = CellGrid::o();
        let _k = CellGrid::k();
        let arc = Arc::new