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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use angles::{signed_angle_to, WithUniqueOrthogonal};
use rough_eq::THICKNESS;
use {P2, Rotation2, V2, N, PI};

pub const MIN_LINE_LENGTH: N = 0.01;
pub const MIN_ARC_LENGTH: N = MIN_LINE_LENGTH;

pub trait Segment {
    fn start(&self) -> P2;
    fn end(&self) -> P2;
    fn length(&self) -> N;
    fn start_direction(&self) -> V2;
    fn end_direction(&self) -> V2;
    fn subdivisions_without_end(&self, max_angle: N) -> Vec<P2>;
}

#[derive(Copy, Clone, Debug)]
pub struct LineSegment {
    start: P2,
    end: P2,
}

impl LineSegment {
    pub fn new(start: P2, end: P2) -> Self {
        LineSegment { start, end }
    }

    pub fn direction(&self) -> V2 {
        (self.end - self.start).normalize()
    }
}

impl Segment for LineSegment {
    fn start(&self) -> P2 {
        self.start
    }

    fn end(&self) -> P2 {
        self.end
    }

    fn length(&self) -> N {
        (self.start - self.end).norm()
    }

    fn start_direction(&self) -> V2 {
        self.direction()
    }

    fn end_direction(&self) -> V2 {
        self.direction()
    }

    fn subdivisions_without_end(&self, _: N) -> Vec<P2> {
        vec![self.start]
    }
}

impl LineSegment {
    pub fn along(&self, distance: N) -> P2 {
        self.start + distance * self.direction()
    }

    pub fn midpoint(&self) -> P2 {
        P2::from_coordinates((self.start.coords + self.end.coords) / 2.0)
    }

    pub fn project_with_tolerance(&self, point: P2, tolerance: N) -> Option<(N, P2)> {
        if (point - self.start).norm() < tolerance {
            Some((0.0, self.start))
        } else if (point - self.end).norm() < tolerance {
            Some((self.length(), self.end))
        } else {
            let direction = self.direction();
            let line_offset = direction.dot(&(point - self.start));
            if line_offset >= 0.0 && line_offset <= self.length() {
                Some((line_offset, self.start + line_offset * direction))
            } else {
                None
            }
        }
    }

    pub fn project_with_max_distance(
        &self,
        point: P2,
        tolerance: N,
        max_distance: N,
    ) -> Option<(N, P2)> {
        self.project_with_tolerance(point, tolerance)
            .and_then(|(along, projected_point)| {
                if (projected_point - point).norm() <= max_distance {
                    Some((along, projected_point))
                } else {
                    None
                }
            })
    }

    pub fn winding_angle(&self, point: P2) -> N {
        signed_angle_to(self.start - point, self.end - point)
    }

    pub fn side_of(&self, point: P2) -> N {
        self.winding_angle(point).signum()
    }

    pub fn is_point_left_of(&self, point: P2) -> bool {
        self.winding_angle(point) > 0.0
    }

    pub fn is_point_right_of(&self, point: P2) -> bool {
        self.winding_angle(point) < 0.0
    }

    pub fn signed_distance_of(&self, point: P2) -> N {
        let direction_orth = self.direction().orthogonal_left();
        (point - self.start).dot(&direction_orth)
    }
}

use bbox::{BoundingBox, HasBoundingBox};

impl HasBoundingBox for LineSegment {
    fn bounding_box(&self) -> BoundingBox {
        BoundingBox {
            min: P2::new(
                self.start.x.min(self.end.x) - THICKNESS,
                self.start.y.min(self.end.y) - THICKNESS,
            ),
            max: P2::new(
                self.start.x.max(self.end.x) + THICKNESS,
                self.start.y.max(self.end.y) + THICKNESS,
            ),
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub struct ArcSegment {
    start: P2,
    apex: P2,
    end: P2,
}

impl ArcSegment {
    pub fn new(start: P2, apex: P2, end: P2) -> Option<ArcSegment> {
        if (start - apex).norm() < MIN_LINE_LENGTH
            || (end - apex).norm() < MIN_LINE_LENGTH
            || (start - end).norm() < MIN_ARC_LENGTH
        {
            None
        } else {
            let segment = ArcSegment { start, apex, end };
            let center = segment.center();
            if center.x.is_nan() || center.y.is_nan() || center.x.is_infinite() || center.y.is_infinite() {
                None
            } else {
                Some(segment)
            }
        }
    }

    pub fn minor_arc_with_center(start: P2, center: P2, end: P2) -> Option<ArcSegment> {
        let center_to_start = start - center;
        let center_to_end = end - center;
        let radius = center_to_start.norm();

        let sum = center_to_start + center_to_end;

        let apex = if sum.norm() > 0.01 {
            center + sum.normalize() * radius
        } else {
            let signed_angle_span = signed_angle_to(center_to_start, center_to_end);
            let center_to_apex = Rotation2::new(signed_angle_span / 2.0) * center_to_start;
            center + center_to_apex
        };

        // TODO: avoid redundant calculation of center, but still check for validity somehow
        ArcSegment::new(start, apex, end)
    }

    pub fn minor_arc_with_start_direction(
        start: P2,
        start_direction: V2,
        end: P2,
    ) -> Option<ArcSegment> {
        let signed_radius = {
            let half_chord = (end - start) / 2.0;
            half_chord.norm_squared() / start_direction.orthogonal_right().dot(&half_chord)
        };

        let center = start + signed_radius * start_direction.orthogonal_right();

        Self::minor_arc_with_center(start, center, end)
    }

    pub fn apex(&self) -> P2 {
        self.apex
    }

    #[inline]
    pub fn center(&self) -> P2 {
        // https://en.wikipedia.org/wiki/Circumscribed_circle#Circumcenter_coordinates
        let (a_abs, b_abs, c_abs) = (self.start, self.apex, self.end);
        let (b, c) = (b_abs - a_abs, c_abs - a_abs);
        let d_inv = 1.0 / (2.0 * (b.x * c.y - b.y * c.x));
        let (b_norm_sq, c_norm_sq) = (b.norm_squared(), c.norm_squared());
        let center_x = d_inv * (c.y * b_norm_sq - b.y * c_norm_sq);
        let center_y = d_inv * (b.x * c_norm_sq - c.x * b_norm_sq);
        a_abs + V2::new(center_x, center_y)
    }

    pub fn radius(&self) -> N {
        (self.start - self.center()).norm()
    }

    pub fn signed_angle_span(&self) -> N {
        let center = self.center();
        signed_angle_to(self.start - center, self.end - center)
    }

    pub fn is_minor(&self) -> bool {
        self.signed_angle_span() * LineSegment::new(self.start, self.end).side_of(self.apex()) < 0.0
    }
}

impl Segment for ArcSegment {
    fn start(&self) -> P2 {
        self.start
    }

    fn end(&self) -> P2 {
        self.end
    }

    fn start_direction(&self) -> V2 {
        let center = self.center();
        let center_to_start_orth = (self.start - center).orthogonal_right();

        if LineSegment::new(self.start, self.end).is_point_left_of(self.apex) {
            center_to_start_orth
        } else {
            -center_to_start_orth
        }
    }

    fn end_direction(&self) -> V2 {
        let center = self.center();
        let center_to_end_orth = (self.end - center).orthogonal_right();

        if LineSegment::new(self.start, self.end).is_point_left_of(self.apex) {
            center_to_end_orth
        } else {
            -center_to_end_orth
        }
    }

    fn length(&self) -> N {
        let simple_angle_span = self.signed_angle_span().abs();

        let angle_span = if self.is_minor() {
            simple_angle_span
        } else {
            2.0 * PI - simple_angle_span
        };

        self.radius() * angle_span
    }

    fn subdivisions_without_end(&self, max_angle: N) -> Vec<P2> {
        let center = self.center();
        let signed_angle_span = signed_angle_to(self.start - center, self.end - center);

        if signed_angle_span.is_nan() {
            println!("KAPUTT {:?} {:?}", self, center);
        }

        let subdivisions = (signed_angle_span.abs() / max_angle).floor() as usize;
        let subdivision_angle = signed_angle_span / (subdivisions as f32);

        let mut pointer = self.start - center;

        let mut maybe_previous_point: Option<P2> = None;

        (0..subdivisions.max(1))
            .into_iter()
            .filter_map(|_| {
                let point = center + pointer;
                pointer = Rotation2::new(subdivision_angle) * pointer;

                if let Some(previous_point) = maybe_previous_point {
                    if (point - previous_point).norm() > 2.0 * THICKNESS {
                        maybe_previous_point = Some(point);
                        Some(point)
                    } else {
                        None
                    }
                } else {
                    maybe_previous_point = Some(point);
                    Some(point)
                }
            })
            .collect::<Vec<_>>()
    }
}

#[derive(Copy, Clone, Debug)]
pub enum ArcOrLineSegment {
    Line(LineSegment),
    Arc(ArcSegment),
}

impl ArcOrLineSegment {
    pub fn line_unchecked(start: P2, end: P2) -> ArcOrLineSegment {
        ArcOrLineSegment::Line(LineSegment { start, end })
    }

    pub fn arc_unchecked(start: P2, apex: P2, end: P2) -> ArcOrLineSegment {
        ArcOrLineSegment::Arc(ArcSegment { start, apex, end })
    }
}

impl Segment for ArcOrLineSegment {
    fn start(&self) -> P2 {
        match *self {
            ArcOrLineSegment::Line(line) => line.start(),
            ArcOrLineSegment::Arc(arc) => arc.start(),
        }
    }
    fn end(&self) -> P2 {
        match *self {
            ArcOrLineSegment::Line(line) => line.end(),
            ArcOrLineSegment::Arc(arc) => arc.end(),
        }
    }
    fn length(&self) -> N {
        match *self {
            ArcOrLineSegment::Line(line) => line.length(),
            ArcOrLineSegment::Arc(arc) => arc.length(),
        }
    }
    fn start_direction(&self) -> V2 {
        match *self {
            ArcOrLineSegment::Line(line) => line.start_direction(),
            ArcOrLineSegment::Arc(arc) => arc.start_direction(),
        }
    }
    fn end_direction(&self) -> V2 {
        match *self {
            ArcOrLineSegment::Line(line) => line.end_direction(),
            ArcOrLineSegment::Arc(arc) => arc.end_direction(),
        }
    }
    fn subdivisions_without_end(&self, max_angle: N) -> Vec<P2> {
        match *self {
            ArcOrLineSegment::Line(line) => line.subdivisions_without_end(max_angle),
            ArcOrLineSegment::Arc(arc) => arc.subdivisions_without_end(max_angle),
        }
    }
}

#[cfg(test)]
const TOL: f32 = 0.0001;

#[cfg(test)]
use rough_eq::RoughEq;

#[test]
fn minor_arc_test() {
    let o = V2::new(10.0, 5.0);
    let minor_arc = ArcSegment::new(
        P2::new(0.0, 1.0) + o,
        P2::new(-3.0f32.sqrt() / 2.0, 0.5) + o,
        P2::new(-1.0, 0.0) + o,
    ).expect("should be valid");
    assert_rough_eq_by!(minor_arc.center(), P2::new(0.0, 0.0) + o, TOL);
    assert!(minor_arc.is_minor());
    assert_rough_eq_by!(minor_arc.length(), PI / 2.0, TOL);

    let minor_arc_rev = ArcSegment::new(
        P2::new(-1.0, 0.0) + o,
        P2::new(-3.0f32.sqrt() / 2.0, 0.5) + o,
        P2::new(0.0, 1.0) + o,
    ).expect("should be valid");
    assert_rough_eq_by!(minor_arc_rev.center(), P2::new(0.0, 0.0) + o, TOL);
    assert!(minor_arc_rev.is_minor());
    assert_rough_eq_by!(minor_arc_rev.length(), PI / 2.0, TOL);

    let minor_arc_by_center = ArcSegment::minor_arc_with_center(
        P2::new(0.0, 1.0) + o,
        P2::new(0.0, 0.0) + o,
        P2::new(-1.0, 0.0) + o,
    ).expect("should be valid");
    assert_rough_eq_by!(
        minor_arc_by_center.apex(),
        P2::new(-2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0) + o,
        TOL
    );
    assert_rough_eq_by!(minor_arc_by_center.length(), PI / 2.0, TOL);

    let minor_arc_by_center_rev = ArcSegment::minor_arc_with_center(
        P2::new(-1.0, 0.0) + o,
        P2::new(0.0, 0.0) + o,
        P2::new(0.0, 1.0) + o,
    ).expect("should be valid");
    assert_rough_eq_by!(
        minor_arc_by_center_rev.apex(),
        P2::new(-2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0) + o,
        TOL
    );
    assert_rough_eq_by!(minor_arc_by_center_rev.length(), PI / 2.0, TOL);

    let minor_arc_by_direction = ArcSegment::minor_arc_with_start_direction(
        P2::new(0.0, 1.0) + o,
        V2::new(-1.0, 0.0),
        P2::new(-1.0, 0.0) + o,
    ).expect("should be valid");
    assert_rough_eq_by!(
        minor_arc_by_direction.apex(),
        P2::new(-2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0) + o,
        TOL
    );
    assert_rough_eq_by!(minor_arc_by_direction.length(), PI / 2.0, TOL);

    let minor_arc_by_direction_rev = ArcSegment::minor_arc_with_start_direction(
        P2::new(-1.0, 0.0) + o,
        V2::new(0.0, 1.0),
        P2::new(0.0, 1.0) + o,
    ).expect("should be valid");
    assert_rough_eq_by!(
        minor_arc_by_direction_rev.apex(),
        P2::new(-2.0f32.sqrt() / 2.0, 2.0f32.sqrt() / 2.0) + o,
        TOL
    );
    assert_rough_eq_by!(minor_arc_by_direction_rev.length(), PI / 2.0, TOL);
}

#[test]
fn colinear_apex_test() {
    assert!(ArcSegment::new(P2::new(0.0, 0.0), P2::new(1.0, 0.0), P2::new(2.0, 0.0)).is_none());
}

#[test]
fn major_arc_test() {
    let o = V2::new(10.0, 5.0);
    let major_arc = ArcSegment::new(
        P2::new(0.0, -1.0) + o,
        P2::new(-3.0f32.sqrt() / 2.0, 0.5) + o,
        P2::new(1.0, 0.0) + o,
    ).expect("should be valid");
    assert_rough_eq_by!(major_arc.center(), P2::new(0.0, 0.0) + o, TOL);
    assert!(!major_arc.is_minor());
    assert_rough_eq_by!(major_arc.length(), 3.0 * PI / 2.0, TOL);

    let major_arc_rev = ArcSegment::new(
        P2::new(-1.0, 0.0) + o,
        P2::new(-3.0f32.sqrt() / 2.0, 0.5) + o,
        P2::new(0.0, -1.0) + o,
    ).expect("should be valid");
    assert_rough_eq_by!(major_arc_rev.center(), P2::new(0.0, 0.0) + o, TOL);
    assert!(!major_arc_rev.is_minor());
    assert_rough_eq_by!(major_arc_rev.length(), 3.0 * PI / 2.0, TOL);
}