File size: 23,213 Bytes
2409829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! Defines the `PathSegment` enum and related functionality for representing and
//! manipulating path segments in 2D space.
//!
//! This module provides implementations for various types of path segments including
//! lines, cubic and quadratic Bézier curves, and elliptical arcs. It also includes
//! utility functions for operations such as bounding box calculation, segment splitting,
//! and arc-to-cubic conversion.
//!
//! The implementations in this module closely follow the SVG path specification,
//! making it suitable for use in vector graphics applications.

use crate::EPS;
use crate::aabb::{Aabb, bounding_box_around_point, expand_bounding_box, extend_bounding_box, merge_bounding_boxes};
use crate::math::{lerp, vector_angle};
use glam::{DMat2, DMat3, DVec2};
use std::f64::consts::{PI, TAU};

/// Represents a segment of a path in a 2D space, based on the SVG path specification.
///
/// This enum closely follows the path segment types defined in the SVG 2 specification.
/// For more details, see: <https://www.w3.org/TR/SVG2/paths.html>
///
/// Each variant of this enum corresponds to a different type of path segment:
/// - Line: A straight line between two points.
/// - Cubic: A cubic Bézier curve.
/// - Quadratic: A quadratic Bézier curve.
/// - Arc: An elliptical arc.
///
/// # Examples
///
/// Creating a line segment:
/// ```
/// use path_bool::PathSegment;
/// use glam::DVec2;
///
/// let line = PathSegment::Line(DVec2::new(0., 0.), DVec2::new(1., 1.));
/// ```
///
/// Creating a cubic Bézier curve:
/// ```
/// use path_bool::PathSegment;
/// use glam::DVec2;
///
/// let cubic = PathSegment::Cubic(
///     DVec2::new(0., 0.),
///     DVec2::new(1., 0.),
///     DVec2::new(1., 1.),
///     DVec2::new(2., 1.)
/// );
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PathSegment {
	/// A line segment from the first point to the second.
	/// Corresponds to the SVG "L" command.
	Line(DVec2, DVec2),

	/// A cubic Bézier curve with start point, two control points, and end point.
	/// Corresponds to the SVG "C" command.
	Cubic(DVec2, DVec2, DVec2, DVec2),

	/// A quadratic Bézier curve with start point, control point, and end point.
	/// Corresponds to the SVG "Q" command.
	Quadratic(DVec2, DVec2, DVec2),

	/// An elliptical arc.
	/// Corresponds to the SVG "A" command.
	///
	/// Parameters:
	/// - Start point
	/// - X-axis radius
	/// - Y-axis radius
	/// - X-axis rotation (in radians)
	/// - Large arc flag (true if the arc should be greater than or equal to 180 degrees)
	/// - Sweep flag (true if the arc should be drawn in a "positive-angle" direction)
	/// - End point
	Arc(DVec2, f64, f64, f64, bool, bool, DVec2),
}

impl PathSegment {
	/// Calculates the angle of the tangent at the start point of the segment.
	///
	/// This method computes the angle (in radians) of the tangent vector at the
	/// beginning of the path segment. The angle is measured clockwise
	/// from the positive x-axis.
	///
	/// # Returns
	///
	/// A float representing the angle in radians, normalized to the range [0, 2π).
	///
	/// # Examples
	///
	/// ```
	/// use path_bool::PathSegment;
	/// use glam::DVec2;
	/// use std::f64::consts::{TAU, FRAC_PI_4};
	///
	/// let line = PathSegment::Line(DVec2::new(0., 0.), DVec2::new(1., 1.));
	/// assert_eq!(line.start_angle(), TAU  - (FRAC_PI_4));
	/// ```
	pub fn start_angle(&self) -> f64 {
		let angle = match *self {
			PathSegment::Line(start, end) => (end - start).angle_to(DVec2::X),
			PathSegment::Cubic(start, control1, control2, _) => {
				let diff = control1 - start;
				if diff.abs_diff_eq(DVec2::ZERO, EPS.point) {
					// if this diff were empty too, the segments would have been converted to a line
					(control2 - start).angle_to(DVec2::X)
				} else {
					diff.angle_to(DVec2::X)
				}
			}
			// Apply same logic as for cubic bezier
			PathSegment::Quadratic(start, control, _) => (control - start).to_angle(),
			PathSegment::Arc(..) => self.arc_segment_to_cubics(0.001)[0].start_angle(),
		};
		use std::f64::consts::TAU;
		(angle + TAU) % TAU
	}

	/// Computes the curvature at the start point of the segment.
	///
	/// The curvature is a measure of how sharply a curve bends. A straight line
	/// has a curvature of 0, while a tight curve has a higher curvature value.
	///
	/// # Returns
	///
	/// A float representing the curvature. Positive values indicate a left
	/// curve, while negative values indicate a right curve.
	///
	/// # Examples
	///
	/// ```
	/// use path_bool::PathSegment;
	/// use glam::DVec2;
	///
	/// let line = PathSegment::Line(DVec2::new(0., 0.), DVec2::new(1., 1.));
	/// assert_eq!(line.start_curvature(), 0.);
	///
	/// let curve = PathSegment::Cubic(
	///     DVec2::new(0., 0.),
	///     DVec2::new(0., 1.),
	///     DVec2::new(1., 1.),
	///     DVec2::new(1., 0.)
	/// );
	/// assert!(curve.start_curvature() < 0.);
	/// ```
	pub fn start_curvature(&self) -> f64 {
		match *self {
			PathSegment::Line(_, _) => 0.,
			PathSegment::Cubic(start, control1, control2, _) => {
				let a = control1 - start;
				let a = 3. * a;
				let b = start - 2. * control1 + control2;
				let b = 6. * b;
				let numerator = a.x * b.y - a.y * b.x;
				let denominator = a.length_squared() * a.length();
				if denominator == 0. { 0. } else { numerator / denominator }
			}
			PathSegment::Quadratic(start, control, end) => {
				// First derivative
				let a = 2. * (control - start);
				// Second derivative
				let b = 2. * (start - 2. * control + end);
				let numerator = a.x * b.y - a.y * b.x;
				let denominator = a.length_squared() * a.length();
				if denominator == 0. { 0. } else { numerator / denominator }
			}
			PathSegment::Arc(..) => self.arc_segment_to_cubics(0.001)[0].start_curvature(),
		}
	}
	/// Converts the segment to a cubic Bézier curve representation.
	///
	/// This method provides a uniform representation of all segment types as
	/// cubic Bézier curves. For segments that are not naturally cubic Bézier
	/// curves (like lines or quadratic Bézier curves), an equivalent cubic
	/// Bézier representation is computed.
	///
	/// # Returns
	///
	/// An array of four `DVec2` points representing the cubic Bézier curve:
	/// [start point, first control point, second control point, end point]
	///
	/// # Examples
	///
	/// ```
	/// use path_bool::PathSegment;
	/// use glam::DVec2;
	///
	/// let line = PathSegment::Line(DVec2::new(0., 0.), DVec2::new(1., 1.));
	/// let cubic = line.to_cubic();
	/// assert_eq!(cubic[0], DVec2::new(0., 0.));
	/// assert_eq!(cubic[3], DVec2::new(1., 1.));
	/// ```
	///
	/// # Panics
	///
	/// This method is not implemented for `PathSegment::Arc`. Attempting to call
	/// `to_cubic()` on an `Arc` segment will result in a panic.
	pub fn to_cubic(&self) -> [DVec2; 4] {
		match *self {
			PathSegment::Line(start, end) => [start, start, end, end],
			PathSegment::Cubic(s, c1, c2, e) => [s, c1, c2, e],
			PathSegment::Quadratic(start, control, end) => {
				// C0 = Q0
				// C1 = Q0 + (2/3) (Q1 - Q0)
				// C2 = Q2 + (2/3) (Q1 - Q2)
				// C3 = Q2
				let d1 = control - start;
				let d2 = control - end;
				[start, start + (2. / 3.) * d1, end + (2. / 3.) * d2, end]
			}
			PathSegment::Arc(..) => unimplemented!(),
		}
	}

	#[must_use]
	/// Retrieves the start point of a path segment.
	pub fn start(&self) -> DVec2 {
		match self {
			PathSegment::Line(start, _) => *start,
			PathSegment::Cubic(start, _, _, _) => *start,
			PathSegment::Quadratic(start, _, _) => *start,
			PathSegment::Arc(start, _, _, _, _, _, _) => *start,
		}
	}

	#[must_use]
	/// Retrieves the end point of a path segment.
	pub fn end(&self) -> DVec2 {
		match self {
			PathSegment::Line(_, end) => *end,
			PathSegment::Cubic(_, _, _, end) => *end,
			PathSegment::Quadratic(_, _, end) => *end,
			PathSegment::Arc(_, _, _, _, _, _, end) => *end,
		}
	}

	#[must_use]
	/// Reverses the direction of the path segment.
	///
	/// This method creates a new `PathSegment` that represents the same geometric shape
	/// but in the opposite direction.
	///
	/// # Examples
	///
	/// ```
	/// use path_bool::PathSegment;
	/// use glam::DVec2;
	///
	/// let line = PathSegment::Line(DVec2::new(0., 0.), DVec2::new(1., 1.));
	/// let reversed = line.reverse();
	/// assert_eq!(reversed.start(), DVec2::new(1., 1.));
	/// assert_eq!(reversed.end(), DVec2::new(0., 0.));
	/// ```
	pub fn reverse(&self) -> PathSegment {
		match *self {
			PathSegment::Line(start, end) => PathSegment::Line(end, start),
			PathSegment::Cubic(p1, p2, p3, p4) => PathSegment::Cubic(p4, p3, p2, p1),
			PathSegment::Quadratic(p1, p2, p3) => PathSegment::Quadratic(p3, p2, p1),
			PathSegment::Arc(start, rx, ry, phi, fa, fs, end) => PathSegment::Arc(end, rx, ry, phi, fa, !fs, start),
		}
	}

	#[must_use]
	/// Converts an arc segment to its center parameterization.
	///
	/// This method is only meaningful for `Arc` segments. For other segment types,
	/// it returns `None`.
	///
	/// # Returns
	///
	/// An `Option` containing `PathArcSegmentCenterParametrization` if the segment
	/// is an `Arc`, or `None` otherwise.
	pub fn arc_segment_to_center(&self) -> Option<PathArcSegmentCenterParametrization> {
		if let PathSegment::Arc(xy1, rx, ry, phi, fa, fs, xy2) = *self {
			if rx == 0. || ry == 0. {
				return None;
			}

			let rotation_matrix = DMat2::from_angle(-phi.to_radians());
			let xy1_prime = rotation_matrix * (xy1 - xy2) * 0.5;

			let mut rx2 = rx * rx;
			let mut ry2 = ry * ry;
			let x1_prime2 = xy1_prime.x * xy1_prime.x;
			let y1_prime2 = xy1_prime.y * xy1_prime.y;

			let mut rx = rx.abs();
			let mut ry = ry.abs();
			let lambda = x1_prime2 / rx2 + y1_prime2 / ry2 + 1e-12;
			if lambda > 1. {
				let lambda_sqrt = lambda.sqrt();
				rx *= lambda_sqrt;
				ry *= lambda_sqrt;
				let lambda_abs = lambda.abs();
				rx2 *= lambda_abs;
				ry2 *= lambda_abs;
			}

			let sign = if fa == fs { -1. } else { 1. };
			let multiplier = ((rx2 * ry2 - rx2 * y1_prime2 - ry2 * x1_prime2) / (rx2 * y1_prime2 + ry2 * x1_prime2)).sqrt();
			let cx_prime = sign * multiplier * ((rx * xy1_prime.y) / ry);
			let cy_prime = sign * multiplier * ((-ry * xy1_prime.x) / rx);

			let cxy = rotation_matrix.transpose() * DVec2::new(cx_prime, cy_prime) + (xy1 + xy2) * 0.5;

			let vec1 = DVec2::new((xy1_prime.x - cx_prime) / rx, (xy1_prime.y - cy_prime) / ry);
			let theta1 = vector_angle(DVec2::new(1., 0.), vec1);
			let mut delta_theta = vector_angle(vec1, DVec2::new((-xy1_prime.x - cx_prime) / rx, (-xy1_prime.y - cy_prime) / ry));

			if !fs && delta_theta > 0. {
				delta_theta -= TAU;
			} else if fs && delta_theta < 0. {
				delta_theta += TAU;
			}

			Some(PathArcSegmentCenterParametrization {
				center: cxy,
				theta1,
				delta_theta,
				rx,
				ry,
				phi,
			})
		} else {
			None
		}
	}

	#[must_use]
	/// Samples a point on the path segment at a given parameter value.
	///
	/// # Arguments
	///
	/// * `t` - A value between 0. and 1. representing the position along the segment.
	///
	/// # Examples
	///
	/// ```
	/// use path_bool::PathSegment;
	/// use glam::DVec2;
	///
	/// let line = PathSegment::Line(DVec2::new(0., 0.), DVec2::new(2., 2.));
	/// assert_eq!(line.sample_at(0.5), DVec2::new(1., 1.));
	/// ```
	pub fn sample_at(&self, t: f64) -> DVec2 {
		match *self {
			PathSegment::Line(start, end) => start.lerp(end, t),
			PathSegment::Cubic(p1, p2, p3, p4) => {
				let p01 = p1.lerp(p2, t);
				let p12 = p2.lerp(p3, t);
				let p23 = p3.lerp(p4, t);
				let p012 = p01.lerp(p12, t);
				let p123 = p12.lerp(p23, t);
				p012.lerp(p123, t)
			}
			PathSegment::Quadratic(p1, p2, p3) => {
				let p01 = p1.lerp(p2, t);
				let p12 = p2.lerp(p3, t);
				p01.lerp(p12, t)
			}
			PathSegment::Arc(start, rx, ry, phi, _, _, end) => {
				if let Some(center_param) = self.arc_segment_to_center() {
					let theta = center_param.theta1 + t * center_param.delta_theta;
					let p = DVec2::new(rx * theta.cos(), ry * theta.sin());
					let rotation_matrix = DMat2::from_angle(phi);
					rotation_matrix * p + center_param.center
				} else {
					start.lerp(end, t)
				}
			}
		}
	}

	#[must_use]
	/// Approximates an arc segment with a series of cubic Bézier curves.
	///
	/// This method is primarily used for `Arc` segments, converting them into
	/// a series of cubic Bézier curves for easier rendering or manipulation.
	/// For non-`Arc` segments, it returns a vector containing only the original segment.
	///
	/// # Arguments
	///
	/// * `max_delta_theta` - The maximum angle (in radians) that each cubic Bézier
	///   curve approximation should span.
	///
	/// # Returns
	///
	/// A vector of `PathSegment::Cubic` approximating the original segment.
	pub fn arc_segment_to_cubics(&self, max_delta_theta: f64) -> Vec<PathSegment> {
		if let PathSegment::Arc(start, rx, ry, phi, _, _, end) = *self {
			if let Some(center_param) = self.arc_segment_to_center() {
				let count = ((center_param.delta_theta.abs() / max_delta_theta).ceil() as usize).max(1);

				let from_unit = DMat3::from_translation(center_param.center) * DMat3::from_angle(phi.to_radians()) * DMat3::from_scale(DVec2::new(rx, ry));

				let theta = center_param.delta_theta / count as f64;
				let k = (4. / 3.) * (theta / 4.).tan();
				let sin_theta = theta.sin();
				let cos_theta = theta.cos();

				(0..count)
					.map(|i| {
						let start = DVec2::new(1., 0.);
						let control1 = DVec2::new(1., k);
						let control2 = DVec2::new(cos_theta + k * sin_theta, sin_theta - k * cos_theta);
						let end = DVec2::new(cos_theta, sin_theta);

						let matrix = DMat3::from_angle(center_param.theta1 + i as f64 * theta) * from_unit;
						let start = (matrix * start.extend(1.)).truncate();
						let control1 = (matrix * control1.extend(1.)).truncate();
						let control2 = (matrix * control2.extend(1.)).truncate();
						let end = (matrix * end.extend(1.)).truncate();

						PathSegment::Cubic(start, control1, control2, end)
					})
					.collect()
			} else {
				vec![PathSegment::Line(start, end)]
			}
		} else {
			vec![*self]
		}
	}
}

/// Represents the center parameterization of an elliptical arc.
///
/// This struct is used internally to perform calculations on arc segments.
pub struct PathArcSegmentCenterParametrization {
	center: DVec2,
	theta1: f64,
	delta_theta: f64,
	rx: f64,
	ry: f64,
	phi: f64,
}

/// Converts the center parameterization back to an arc segment.
///
/// # Arguments
///
/// * `start` - Optional start point of the arc. If `None`, the start point is calculated.
/// * `end` - Optional end point of the arc. If `None`, the end point is calculated.
///
/// # Returns
///
/// A `PathSegment::Arc` representing the arc described by this parameterization.
impl PathArcSegmentCenterParametrization {
	#[must_use]
	pub fn arc_segment_from_center(&self, start: Option<DVec2>, end: Option<DVec2>) -> PathSegment {
		let rotation_matrix = DMat2::from_angle(self.phi);

		let mut xy1 = rotation_matrix * DVec2::new(self.rx * self.theta1.cos(), self.ry * self.theta1.sin()) + self.center;

		let mut xy2 = rotation_matrix * DVec2::new(self.rx * (self.theta1 + self.delta_theta).cos(), self.ry * (self.theta1 + self.delta_theta).sin()) + self.center;

		let fa = self.delta_theta.abs() > PI;
		let fs = self.delta_theta > 0.;
		xy1 = start.unwrap_or(xy1);
		xy2 = end.unwrap_or(xy2);

		PathSegment::Arc(xy1, self.rx, self.ry, self.phi, fa, fs, xy2)
	}
}

/// Evaluates a 1D cubic Bézier curve at a given parameter value.
///
/// # Arguments
///
/// * `p0`, `p1`, `p2`, `p3` - Control points of the cubic Bézier curve.
/// * `t` - Parameter value between 0 and 1.
///
/// # Returns
///
/// The value of the Bézier curve at parameter `t`.
fn eval_cubic_1d(p0: f64, p1: f64, p2: f64, p3: f64, t: f64) -> f64 {
	let p01 = lerp(p0, p1, t);
	let p12 = lerp(p1, p2, t);
	let p23 = lerp(p2, p3, t);
	let p012 = lerp(p01, p12, t);
	let p123 = lerp(p12, p23, t);
	lerp(p012, p123, t)
}

/// Computes the bounding interval of a 1D cubic Bézier curve.
///
/// This function finds the minimum and maximum values of a cubic Bézier curve
/// over the interval [0, 1].
///
/// # Arguments
///
/// * `p0`, `p1`, `p2`, `p3` - Control points of the cubic Bézier curve.
///
/// # Returns
///
/// A tuple `(min, max)` representing the bounding interval.
fn cubic_bounding_interval(p0: f64, p1: f64, p2: f64, p3: f64) -> (f64, f64) {
	let mut min = p0.min(p3);
	let mut max = p0.max(p3);

	let a = 3. * (-p0 + 3. * p1 - 3. * p2 + p3);
	let b = 6. * (p0 - 2. * p1 + p2);
	let c = 3. * (p1 - p0);
	let d = b * b - 4. * a * c;

	if d < 0. || a == 0. {
		// TODO: if a=0, solve linear
		return (min, max);
	}

	let sqrt_d = d.sqrt();

	let t0 = (-b - sqrt_d) / (2. * a);
	if 0. < t0 && t0 < 1. {
		let x0 = eval_cubic_1d(p0, p1, p2, p3, t0);
		min = min.min(x0);
		max = max.max(x0);
	}

	let t1 = (-b + sqrt_d) / (2. * a);
	if 0. < t1 && t1 < 1. {
		let x1 = eval_cubic_1d(p0, p1, p2, p3, t1);
		min = min.min(x1);
		max = max.max(x1);
	}

	(min, max)
}

/// Evaluates a 1D quadratic Bézier curve at a given parameter value.
///
/// # Arguments
///
/// * `p0`, `p1`, `p2` - Control points of the quadratic Bézier curve.
/// * `t` - Parameter value between 0 and 1.
///
/// # Returns
///
/// The value of the Bézier curve at parameter `t`.
fn eval_quadratic_1d(p0: f64, p1: f64, p2: f64, t: f64) -> f64 {
	let p01 = lerp(p0, p1, t);
	let p12 = lerp(p1, p2, t);
	lerp(p01, p12, t)
}

/// Computes the bounding interval of a 1D quadratic Bézier curve.
///
/// This function finds the minimum and maximum values of a quadratic Bézier curve
/// over the interval [0, 1].
///
/// # Arguments
///
/// * `p0`, `p1`, `p2` - Control points of the quadratic Bézier curve.
///
/// # Returns
///
/// A tuple `(min, max)` representing the bounding interval.
fn quadratic_bounding_interval(p0: f64, p1: f64, p2: f64) -> (f64, f64) {
	let mut min = p0.min(p2);
	let mut max = p0.max(p2);

	let denominator = p0 - 2. * p1 + p2;

	if denominator == 0. {
		return (min, max);
	}

	let t = (p0 - p1) / denominator;
	if (0.0..=1.).contains(&t) {
		let x = eval_quadratic_1d(p0, p1, p2, t);
		min = min.min(x);
		max = max.max(x);
	}

	(min, max)
}

fn in_interval(x: f64, x0: f64, x1: f64) -> bool {
	(x0..=x1).contains(&x)
}

impl PathSegment {
	/// Computes the bounding box of the path segment.
	///
	/// # Returns
	///
	/// An [`Aabb`] representing the axis-aligned bounding box of the segment.
	pub(crate) fn bounding_box(&self) -> Aabb {
		match *self {
			PathSegment::Line(start, end) => Aabb {
				top: start.y.min(end.y),
				right: start.x.max(end.x),
				bottom: start.y.max(end.y),
				left: start.x.min(end.x),
			},
			PathSegment::Cubic(p1, p2, p3, p4) => {
				let (left, right) = cubic_bounding_interval(p1.x, p2.x, p3.x, p4.x);
				let (top, bottom) = cubic_bounding_interval(p1.y, p2.y, p3.y, p4.y);
				Aabb { top, right, bottom, left }
			}
			PathSegment::Quadratic(p1, p2, p3) => {
				let (left, right) = quadratic_bounding_interval(p1.x, p2.x, p3.x);
				let (top, bottom) = quadratic_bounding_interval(p1.y, p2.y, p3.y);
				Aabb { top, right, bottom, left }
			}
			PathSegment::Arc(start, rx, ry, phi, _, _, end) => {
				if let Some(center_param) = self.arc_segment_to_center() {
					let theta2 = center_param.theta1 + center_param.delta_theta;
					let mut bounding_box = extend_bounding_box(Some(bounding_box_around_point(start, 0.)), end);

					if phi == 0. || rx == ry {
						// TODO: Fix the fact that the following gives false positives, resulting in larger boxes
						if in_interval(-PI, center_param.theta1, theta2) || in_interval(PI, center_param.theta1, theta2) {
							bounding_box = extend_bounding_box(Some(bounding_box), DVec2::new(center_param.center.x - rx, center_param.center.y));
						}
						if in_interval(-PI / 2., center_param.theta1, theta2) || in_interval(3. * PI / 2., center_param.theta1, theta2) {
							bounding_box = extend_bounding_box(Some(bounding_box), DVec2::new(center_param.center.x, center_param.center.y - ry));
						}
						if in_interval(0., center_param.theta1, theta2) || in_interval(2. * PI, center_param.theta1, theta2) {
							bounding_box = extend_bounding_box(Some(bounding_box), DVec2::new(center_param.center.x + rx, center_param.center.y));
						}
						if in_interval(PI / 2., center_param.theta1, theta2) || in_interval(5. * PI / 2., center_param.theta1, theta2) {
							bounding_box = extend_bounding_box(Some(bounding_box), DVec2::new(center_param.center.x, center_param.center.y + ry));
						}
						expand_bounding_box(&bounding_box, 1e-11) // TODO: Get rid of expansion
					} else {
						// TODO: Don't convert to cubics
						let cubics = self.arc_segment_to_cubics(PI / 16.);
						let mut bounding_box = None;
						for cubic_seg in cubics {
							bounding_box = Some(merge_bounding_boxes(bounding_box, &cubic_seg.bounding_box()));
						}
						bounding_box.unwrap_or_else(|| bounding_box_around_point(start, 0.))
					}
				} else {
					extend_bounding_box(Some(bounding_box_around_point(start, 0.)), end)
				}
			}
		}
	}

	/// Splits the path segment at a given parameter value.
	///
	/// # Arguments
	///
	/// * `t` - A value between 0. and 1. representing the split point along the segment.
	///
	/// # Returns
	///
	/// A tuple of two `PathSegment`s representing the parts before and after the split point.
	///
	/// # Examples
	///
	/// ```
	/// use path_bool::PathSegment;
	/// use glam::DVec2;
	///
	/// let line = PathSegment::Line(DVec2::new(0., 0.), DVec2::new(2., 2.));
	/// let (first_half, second_half) = line.split_at(0.5);
	/// assert_eq!(first_half.end(), DVec2::new(1., 1.));
	/// assert_eq!(second_half.start(), DVec2::new(1., 1.));
	/// ```
	pub fn split_at(&self, t: f64) -> (PathSegment, PathSegment) {
		match *self {
			PathSegment::Line(start, end) => {
				let p = start.lerp(end, t);
				(PathSegment::Line(start, p), PathSegment::Line(p, end))
			}
			PathSegment::Cubic(p0, p1, p2, p3) => {
				let p01 = p0.lerp(p1, t);
				let p12 = p1.lerp(p2, t);
				let p23 = p2.lerp(p3, t);
				let p012 = p01.lerp(p12, t);
				let p123 = p12.lerp(p23, t);
				let p = p012.lerp(p123, t);

				(PathSegment::Cubic(p0, p01, p012, p), PathSegment::Cubic(p, p123, p23, p3))
			}
			PathSegment::Quadratic(p0, p1, p2) => {
				let p01 = p0.lerp(p1, t);
				let p12 = p1.lerp(p2, t);
				let p = p01.lerp(p12, t);

				(PathSegment::Quadratic(p0, p01, p), PathSegment::Quadratic(p, p12, p2))
			}
			PathSegment::Arc(start, _, _, _, _, _, end) => {
				if let Some(center_param) = self.arc_segment_to_center() {
					let mid_delta_theta = center_param.delta_theta * t;
					let seg1 = PathArcSegmentCenterParametrization {
						delta_theta: mid_delta_theta,
						..center_param
					}
					.arc_segment_from_center(Some(start), None);
					let seg2 = PathArcSegmentCenterParametrization {
						theta1: center_param.theta1 + mid_delta_theta,
						delta_theta: center_param.delta_theta - mid_delta_theta,
						..center_param
					}
					.arc_segment_from_center(None, Some(end));
					(seg1, seg2)
				} else {
					// https://svgwg.org/svg2-draft/implnote.html#ArcCorrectionOutOfRangeRadii
					let p = start.lerp(end, t);
					(PathSegment::Line(start, p), PathSegment::Line(p, end))
				}
			}
		}
	}
}