-
Notifications
You must be signed in to change notification settings - Fork 64
/
intersections.rs
1985 lines (1735 loc) · 73.3 KB
/
intersections.rs
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{collections::VecDeque, f32::EPSILON};
use crate::sketch::{Arc2, Circle2, IncrementingMap, Line2, Point2, Sketch};
use itertools::Itertools;
use std::f64::consts::{PI, TAU};
use serde::{Deserialize, Serialize};
use tsify::Tsify;
#[derive(Tsify, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub enum Shape {
Circle(Circle2),
Arc(Arc2),
Line(Line2),
}
impl Shape {
pub fn split_at_point_id(&self, new_point_id: u64) -> (Shape, Shape) {
match self {
Shape::Line(line) => {
let new_line_1 = Line2 {
start: line.start,
end: new_point_id,
};
let new_line_2 = Line2 {
start: new_point_id,
end: line.end,
};
(Shape::Line(new_line_1), Shape::Line(new_line_2))
}
Shape::Circle(circle) => todo!(),
Shape::Arc(_) => todo!(),
}
}
}
#[derive(Tsify, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Collision {
point: Point2,
shape_a: u64,
shape_b: u64,
shape_a_degeneracy: Degeneracy,
shape_b_degeneracy: Degeneracy,
}
impl Collision {
pub fn new(point: Point2, shape_a: u64, shape_b: u64) -> Self {
Collision {
point,
shape_a,
shape_b,
shape_a_degeneracy: Degeneracy::None,
shape_b_degeneracy: Degeneracy::None,
}
}
pub fn degenerate(shape_a: u64, shape_b: u64) -> Self {
Collision {
point: Point2::new(f64::NAN, f64::NAN),
shape_a,
shape_b,
shape_a_degeneracy: Degeneracy::Complete,
shape_b_degeneracy: Degeneracy::Complete,
}
}
}
#[derive(Tsify, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub enum Degeneracy {
None,
IsStart,
IsEnd,
Complete,
}
use Degeneracy::*;
#[derive(Tsify, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub enum Intersection {
None,
OnePoint(Point2, bool),
TwoPoints(Point2, bool, Point2, bool),
Line(Point2, Point2),
Arc(Arc2),
Circle(Circle2),
}
impl Sketch {
pub fn identify_collisions(
&self,
temp_sketch: &Sketch,
all_shapes: &IncrementingMap<Shape>,
shape_a_id: u64,
shape_b_id: u64,
debug: bool,
) -> Vec<Collision> {
let shape_a = all_shapes.items.get(&(shape_a_id)).unwrap();
let shape_b = all_shapes.items.get(&(shape_b_id)).unwrap();
if (debug) {
println!("Shape A ({}): {:?}", shape_a_id, shape_a);
println!("Shape B ({}): {:?}", shape_b_id, shape_b);
}
match (shape_a, shape_b) {
(Shape::Circle(circle_a), Shape::Circle(circle_b)) => {
temp_sketch.circle_circle_collisions(circle_a, shape_a_id, circle_b, shape_b_id)
}
(Shape::Circle(circle_a), Shape::Arc(arc_b)) => {
temp_sketch.circle_arc_collisions(circle_a, shape_a_id, arc_b, shape_b_id)
}
(Shape::Circle(circle_a), Shape::Line(line_b)) => {
temp_sketch.line_circle_collisions(line_b, shape_b_id, circle_a, shape_a_id)
}
(Shape::Arc(arc_a), Shape::Circle(circle_b)) => {
temp_sketch.circle_arc_collisions(circle_b, shape_b_id, arc_a, shape_a_id)
}
(Shape::Arc(arc_a), Shape::Arc(arc_b)) => {
temp_sketch.arc_arc_collisions(arc_a, shape_a_id, arc_b, shape_b_id)
}
(Shape::Arc(arc_a), Shape::Line(line_b)) => {
temp_sketch.line_arc_collisions(line_b, shape_b_id, arc_a, shape_a_id)
}
(Shape::Line(line_a), Shape::Circle(circle_b)) => {
temp_sketch.line_circle_collisions(line_a, shape_a_id, circle_b, shape_b_id)
}
(Shape::Line(line_a), Shape::Arc(arc_b)) => {
temp_sketch.line_arc_collisions(line_a, shape_a_id, arc_b, shape_b_id)
}
(Shape::Line(line_a), Shape::Line(line_b)) => {
temp_sketch.line_line_collisions(line_a, shape_a_id, line_b, shape_b_id, false)
}
}
}
pub fn process_collision(
&self,
temp_sketch: &mut Sketch,
all_shapes: &mut IncrementingMap<Shape>,
possible_shape_collisions: &mut Vec<u64>,
new_shapes: &mut Vec<u64>,
recently_deleted: &mut Vec<u64>,
collision: Collision,
debug: bool,
) {
if (debug) {
println!("Processing collision: {:?}", collision);
}
let shape_a_id = collision.shape_a;
let shape_b_id = collision.shape_b;
let point = collision.point;
let shape_a = all_shapes.get_item(shape_a_id).unwrap().clone();
let shape_b = all_shapes.get_item(shape_b_id).unwrap().clone();
match (shape_a, shape_b) {
(Shape::Circle(circle_a), Shape::Circle(circle_b)) => {
let new_point_id = temp_sketch.add_point(point.x, point.y);
let arc_a = temp_sketch.split_circle_at_point(&circle_a, &new_point_id, &point);
let arc_b = temp_sketch.split_circle_at_point(&circle_b, &new_point_id, &point);
// this is a unique case. We're making substitutions here, not deleting or creating
all_shapes.items.insert(shape_a_id, Shape::Arc(arc_a));
all_shapes.items.insert(shape_b_id, Shape::Arc(arc_b));
if (debug) {
println!("Replaced two circles with two arcs, keeping the same IDs");
}
}
(Shape::Circle(circle_a), Shape::Arc(arc_b)) => {
let new_point_id = temp_sketch.add_point(point.x, point.y);
// the circle can be converted to an arc
let arc_a = temp_sketch.split_circle_at_point(&circle_a, &new_point_id, &point);
// the arc must be split into two arcs
let (arc_b1, arc_b2) =
temp_sketch.split_arc_at_point(&arc_b, &new_point_id, &point);
// the circle -> arc amounts to a substitution not a deletion + creation
all_shapes.items.insert(shape_a_id, Shape::Arc(arc_a));
// but the arc -> 2 arcs is a deletion + creation
let new_arc_b1_id = all_shapes.add_item(Shape::Arc(arc_b1));
new_shapes.push(new_arc_b1_id);
let new_arc_b2_id = all_shapes.add_item(Shape::Arc(arc_b2));
new_shapes.push(new_arc_b2_id);
recently_deleted.push(all_shapes.remove_item(shape_b_id));
if (debug) {
println!("Replaced a circle with an arc ({})", shape_a_id);
println!(
"AND replaced an arc ({}) with 2 arcs ({}), ({})",
shape_b_id, new_arc_b1_id, new_arc_b2_id
);
}
}
(Shape::Circle(_), Shape::Line(_)) => todo!(),
(Shape::Arc(_), Shape::Circle(_)) => todo!(),
(Shape::Arc(arc_a), Shape::Arc(arc_b)) => {
let new_point_id = temp_sketch.add_point(point.x, point.y);
let (arc_a1, arc_a2) =
temp_sketch.split_arc_at_point(&arc_a, &new_point_id, &point);
let (arc_b1, arc_b2) =
temp_sketch.split_arc_at_point(&arc_b, &new_point_id, &point);
new_shapes.push(all_shapes.add_item(Shape::Arc(arc_a1)));
new_shapes.push(all_shapes.add_item(Shape::Arc(arc_a2)));
new_shapes.push(all_shapes.add_item(Shape::Arc(arc_b1)));
new_shapes.push(all_shapes.add_item(Shape::Arc(arc_b2)));
recently_deleted.push(all_shapes.remove_item(shape_a_id));
recently_deleted.push(all_shapes.remove_item(shape_b_id));
if (debug) {
println!("replaced two arcs with four arcs");
println!(
"Replaced arc {} with arcs {} and {}",
shape_a_id, new_shapes[0], new_shapes[1]
);
println!(
"Replaced arc {} with arcs {} and {}",
shape_b_id, new_shapes[2], new_shapes[3]
);
}
}
(Shape::Arc(_), Shape::Line(_)) => todo!(),
(Shape::Line(line_a), Shape::Circle(circle_b)) => {
let new_point_id = temp_sketch.add_point(point.x, point.y);
let (line_a1, line_a2) =
temp_sketch.split_line_at_point(&line_a, &new_point_id, &point);
let arc_b = temp_sketch.split_circle_at_point(&circle_b, &new_point_id, &point);
// convert the circle into an arc in place
all_shapes.items.insert(shape_b_id, Shape::Arc(arc_b));
// delete the old line and replace with two smaller lines
new_shapes.push(all_shapes.add_item(Shape::Line(line_a1)));
new_shapes.push(all_shapes.add_item(Shape::Line(line_a2)));
recently_deleted.push(all_shapes.remove_item(shape_a_id));
if (debug) {
println!(
"Broke line {} into {} and {}",
shape_a_id, new_shapes[0], new_shapes[1]
);
println!("Replaced circle {} with arc in place", shape_b_id);
}
}
(Shape::Line(line_a), Shape::Arc(arc_b)) => {
let new_point_id = temp_sketch.add_point(point.x, point.y);
let (line_a1, line_a2) =
temp_sketch.split_line_at_point(&line_a, &new_point_id, &point);
let (arc_b1, arc_b2) =
temp_sketch.split_arc_at_point(&arc_b, &new_point_id, &point);
new_shapes.push(all_shapes.add_item(Shape::Line(line_a1)));
new_shapes.push(all_shapes.add_item(Shape::Line(line_a2)));
new_shapes.push(all_shapes.add_item(Shape::Arc(arc_b1)));
new_shapes.push(all_shapes.add_item(Shape::Arc(arc_b2)));
recently_deleted.push(all_shapes.remove_item(shape_a_id));
recently_deleted.push(all_shapes.remove_item(shape_b_id));
if (debug) {
println!(
"Replaced line {} with {} and {}",
recently_deleted[0], new_shapes[0], new_shapes[1]
);
println!(
"Replaced arc {} with {} and {}",
recently_deleted[1], new_shapes[2], new_shapes[3]
);
}
}
(Shape::Line(line_a), Shape::Line(line_b)) => {
let collision_point_id =
match (&collision.shape_a_degeneracy, &collision.shape_b_degeneracy) {
(IsStart, None) => line_a.start,
(IsEnd, None) => line_a.end,
(None, IsStart) => line_b.start,
(None, IsEnd) => line_b.end,
(None, None) => temp_sketch.add_point(point.x, point.y),
(Complete, Complete) => {
if (debug) {
println!("COMPLETE degeneracy found. Removing line {}", shape_b_id);
}
all_shapes.remove_item(shape_b_id);
recently_deleted.push(shape_b_id);
return;
}
(_, _) => {
if (debug) {
println!("One line continues the other. Nothing to be done!");
}
return;
}
};
if collision.shape_a_degeneracy == None {
let (line_a1, line_a2) =
temp_sketch.split_line_at_point(&line_a, &collision_point_id, &point);
new_shapes.push(all_shapes.add_item(Shape::Line(line_a1)));
new_shapes.push(all_shapes.add_item(Shape::Line(line_a2)));
recently_deleted.push(all_shapes.remove_item(shape_a_id));
if (debug) {
println!(
"Replaced line {} with lines {} and {}",
shape_a_id, new_shapes[0], new_shapes[1]
);
}
}
if collision.shape_b_degeneracy == None {
let (line_b1, line_b2) =
temp_sketch.split_line_at_point(&line_b, &collision_point_id, &point);
new_shapes.push(all_shapes.add_item(Shape::Line(line_b1)));
new_shapes.push(all_shapes.add_item(Shape::Line(line_b2)));
recently_deleted.push(all_shapes.remove_item(shape_b_id));
if (debug) {
println!(
"Replaced line {} with lines {} and {}",
shape_b_id,
new_shapes[new_shapes.len() - 2],
new_shapes[new_shapes.len() - 1]
);
}
}
if collision.shape_a_degeneracy == IsEnd || collision.shape_a_degeneracy == IsStart
{
if !possible_shape_collisions.contains(&collision.shape_a) {
possible_shape_collisions.push(collision.shape_a);
}
}
if collision.shape_b_degeneracy == IsEnd || collision.shape_b_degeneracy == IsStart
{
if !possible_shape_collisions.contains(&collision.shape_b) {
possible_shape_collisions.push(collision.shape_b);
}
}
}
}
}
pub fn step_process(
&self,
temp_sketch: &mut Sketch,
all_shapes: &mut IncrementingMap<Shape>,
pairs_to_check: &mut VecDeque<(u64, u64)>,
collisions: &mut VecDeque<Collision>,
possible_shape_collisions: &mut Vec<u64>,
new_shapes: &mut Vec<u64>,
recently_deleted: &mut Vec<u64>,
debug: bool,
) -> bool {
// the bool we return indicates whether any work was done
if (debug) {
println!("----- Okay let's process:");
}
if !recently_deleted.is_empty() {
println!("Something was recently deleted, let's fix it");
// any collisions with the old shape are simply deleted
let mut indices_to_delete: Vec<usize> = vec![];
for (i, c) in collisions.iter().enumerate() {
if recently_deleted.contains(&c.shape_a) {
indices_to_delete.push(i);
if !possible_shape_collisions.contains(&c.shape_b) {
possible_shape_collisions.push(c.shape_b);
println!("Pushed a possible shape collision against {}", c.shape_b);
}
}
if recently_deleted.contains(&c.shape_b) {
indices_to_delete.push(i);
if !possible_shape_collisions.contains(&c.shape_a) {
possible_shape_collisions.push(c.shape_a);
println!("Pushed a possible shape collision against {}", c.shape_a);
}
}
}
for i in indices_to_delete.iter().rev() {
collisions.remove(*i);
}
println!("I removed {} collisions", indices_to_delete.len());
// We also need to comb through pairs_to_check to remove anything that
// references these recently removed shape IDs
indices_to_delete.clear();
for (i, (shape_a_id, shape_b_id)) in pairs_to_check.iter().enumerate() {
let shape_a_deleted = recently_deleted.contains(shape_a_id);
let shape_b_deleted = recently_deleted.contains(shape_b_id);
if !shape_a_deleted && !shape_b_deleted {
//nothing to do, move on
continue;
}
if shape_a_deleted && !shape_b_deleted {
// shape A was deleted but B wasn't, we we'll need to consider whether any of the
// new shapes are colliding with shape B
if !possible_shape_collisions.contains(shape_b_id) {
possible_shape_collisions.push(*shape_b_id);
}
} else if shape_b_deleted && !shape_a_deleted {
if !possible_shape_collisions.contains(shape_a_id) {
possible_shape_collisions.push(*shape_a_id);
}
} else if shape_a_deleted && shape_b_deleted {
panic!("I didn't think both shapes could be deleted at once!");
}
indices_to_delete.push(i);
}
for i in indices_to_delete.iter().rev() {
pairs_to_check.remove(*i);
}
println!("I removed {} pairs to check", indices_to_delete.len());
recently_deleted.clear();
return true;
}
if !possible_shape_collisions.is_empty() || !new_shapes.is_empty() {
if (debug) {
println!("Possible shape collisions + new shapes! Let's add some pairs to check");
}
for possible_shape_id in possible_shape_collisions.iter() {
for new_shape_id in new_shapes.iter() {
pairs_to_check.push_front((*possible_shape_id, *new_shape_id));
}
}
possible_shape_collisions.clear();
new_shapes.clear();
return true;
}
if !collisions.is_empty() {
if (debug) {
println!("We have a collision to process");
}
let collision = collisions.pop_front().unwrap();
self.process_collision(
temp_sketch,
all_shapes,
possible_shape_collisions,
new_shapes,
recently_deleted,
collision,
debug,
);
return true;
}
if !pairs_to_check.is_empty() {
if (debug) {
println!("We have pairs to check!");
}
let (shape_a_id, shape_b_id) = pairs_to_check.pop_front().unwrap();
let new_collisions =
self.identify_collisions(temp_sketch, all_shapes, shape_a_id, shape_b_id, debug);
if (debug) {
println!("Adding {} collisions", new_collisions.len());
}
for c in new_collisions {
// println!("Adding a collision");
collisions.push_back(c);
}
return true;
}
if (debug) {
println!("There was nothing to do!\n");
}
return false;
}
pub fn split_intersections(&self, debug: bool) -> Self {
let mut temp_sketch = self.clone();
// set up the necessary data structures:
// First put all segments: Arcs, Lines, Circles into one big collection called all_shapes
let mut all_shapes: IncrementingMap<Shape> = IncrementingMap::new();
let line_ids: Vec<u64> = temp_sketch.line_segments.keys().cloned().sorted().collect();
for line_id in line_ids {
let line = temp_sketch.line_segments.get(&line_id).unwrap();
all_shapes.add_item(Shape::Line(line.clone()));
}
let circle_ids: Vec<u64> = temp_sketch.circles.keys().cloned().sorted().collect();
for circle_id in circle_ids {
let circle = temp_sketch.circles.get(&circle_id).unwrap();
all_shapes.add_item(Shape::Circle(circle.clone()));
}
let arc_ids: Vec<u64> = temp_sketch.arcs.keys().cloned().sorted().collect();
for arc_id in arc_ids {
let arc = temp_sketch.arcs.get(&arc_id).unwrap();
all_shapes.add_item(Shape::Arc(arc.clone()));
}
let mut pairs_to_check: VecDeque<(u64, u64)> = VecDeque::new();
let mut collisions: VecDeque<Collision> = VecDeque::new();
let mut possible_shape_collisions: Vec<u64> = vec![];
let mut new_shapes: Vec<u64> = vec![];
let mut recently_deleted: Vec<u64> = vec![];
// inject all the pairs of shapes that need to be checked:
for shape_id_a in all_shapes.items.keys() {
for shape_id_b in all_shapes.items.keys() {
if shape_id_a < shape_id_b {
pairs_to_check.push_back((*shape_id_a, *shape_id_b))
}
}
}
// While there's anything to do, step the process forward
let mut count = 0;
loop {
if (debug) {
println!("\nstep: {} Here's the setup:", count);
}
count += 1;
if (debug) {
println!("Pairs to check: {:?}", pairs_to_check);
println!("Collisions: {:?}", collisions);
println!("Possible shape collisions: {:?}", possible_shape_collisions);
println!("New shapes: {:?}", new_shapes);
println!("Recently deleted: {:?}", recently_deleted);
}
// let mut input = String::new();
// io::stdin()
// .read_line(&mut input)
// .expect("error: unable to read user input");
// println!("{}", input);
let result = self.step_process(
&mut temp_sketch,
&mut all_shapes,
&mut pairs_to_check,
&mut collisions,
&mut possible_shape_collisions,
&mut new_shapes,
&mut recently_deleted,
debug,
);
// let mut input = String::new();
// io::stdin()
// .read_line(&mut input)
// .expect("error: unable to read user input");
// println!("{}", input);
if result == false {
break;
}
}
// Lastly, consolidate all the shapes into a final sketch and return it
let mut final_sketch = Sketch::new();
final_sketch.points = temp_sketch.points;
final_sketch.highest_point_id = temp_sketch.highest_point_id;
for shape in all_shapes.items.iter() {
match shape {
(id, Shape::Line(line)) => {
final_sketch.add_segment(line.start, line.end);
}
(id, Shape::Circle(circle)) => {
final_sketch.add_circle(circle.center, circle.radius);
}
(id, Shape::Arc(arc)) => {
final_sketch.add_arc(arc.center, arc.start, arc.end, arc.clockwise);
}
_ => {}
}
}
if (debug) {
println!("So, in summary I've generated these shapes:");
for shape in all_shapes.items.iter() {
println!("{:?}", shape);
}
}
final_sketch
}
pub fn line_line_collisions(
&self,
line_a: &Line2,
line_a_id: u64,
line_b: &Line2,
line_b_id: u64,
debug: bool,
) -> Vec<Collision> {
// catch the case where the lines are completely degenerate-their start and end points are the same
if line_a.start == line_b.start || line_a.start == line_b.end {
if line_a.end == line_b.start || line_a.end == line_b.end {
return vec![Collision::degenerate(line_a_id, line_b_id)];
}
}
let a_start = self.points.get(&line_a.start).unwrap();
let a_end = self.points.get(&line_a.end).unwrap();
let b_start = self.points.get(&line_b.start).unwrap();
let b_end = self.points.get(&line_b.end).unwrap();
fn normal_form(start: &Point2, end: &Point2) -> (f64, f64, f64) {
let a = start.y - end.y;
let b = end.x - start.x;
let c = (start.x - end.x) * start.y + (end.y - start.y) * start.x;
return (a, b, c);
}
let (a1, b1, c1) = normal_form(&a_start, &a_end);
let (a2, b2, c2) = normal_form(&b_start, &b_end);
if (debug) {
println!("a1, b1, c1: {} {} {}", a1, b1, c1);
println!("a2, b2, c2: {} {} {}", a2, b2, c2);
}
let x_intercept = (b1 * c2 - b2 * c1) / (a1 * b2 - a2 * b1);
let y_intercept = (c1 * a2 - c2 * a1) / (a1 * b2 - a2 * b1);
if (debug) {
println!("Intercept: {} {}", x_intercept, y_intercept);
}
if x_intercept.is_nan() || y_intercept.is_nan() {
// something degenerate happened. The lines may be parallel.
if (debug) {
println!("NAN intercept, so lines are be parallel");
}
let tol = 1e-8;
let a_start_colinear_with_b = (a2 * a_start.x + b2 * a_start.y + c2).abs() < tol;
let a_end_colinear_with_b = (a2 * a_end.x + b2 * a_end.y + c2).abs() < tol;
if a_start_colinear_with_b && a_end_colinear_with_b {
if (debug) {
println!("The lines are perfectly colinear!");
}
let mut collisions: Vec<Collision> = vec![];
// it is possible these lines overlap somewhat.
if within_range(a_start.x, b_start.x, b_end.x, -tol)
&& within_range(a_start.y, b_start.y, b_end.y, -tol)
{
// a_start is WITHIN b!
if (debug) {
println!("A start is within B");
}
let mut collision = Collision::new(a_start.clone(), line_a_id, line_b_id);
collision.shape_a_degeneracy = IsStart;
collisions.push(collision);
}
if within_range(a_end.x, b_start.x, b_end.x, -tol)
&& within_range(a_end.y, b_start.y, b_end.y, -tol)
{
// a_end is WITHIN b!
if (debug) {
println!("A end is within B");
}
let mut collision = Collision::new(a_end.clone(), line_a_id, line_b_id);
collision.shape_a_degeneracy = IsEnd;
collisions.push(collision);
}
if within_range(b_start.x, a_start.x, a_end.x, -tol)
&& within_range(b_start.y, a_start.y, a_end.y, -tol)
{
// b_start is WITHIN a!
if (debug) {
println!("B start is within A");
}
let mut collision = Collision::new(b_start.clone(), line_a_id, line_b_id);
collision.shape_b_degeneracy = IsStart;
collisions.push(collision);
}
if within_range(b_end.x, a_start.x, a_end.x, -tol)
&& within_range(b_end.y, a_start.y, a_end.y, -tol)
{
// b_end is WITHIN a!
if (debug) {
println!("B end is within A");
}
let mut collision = Collision::new(b_end.clone(), line_a_id, line_b_id);
collision.shape_b_degeneracy = IsEnd;
collisions.push(collision);
}
return collisions;
} else {
// If the lines are parallel but not colinear, then they can have no collisions
return vec![];
}
}
if x_intercept.is_infinite() || y_intercept.is_infinite() {
if (debug) {
println!("Infinite intercept, so lines are parallel");
}
return vec![];
}
// it only counts as an intersection if it falls within both the segments
// Check that the x-intercept is within the x-range of the first segment
let epsilon = 1e-12;
if within_range(x_intercept, a_start.x, a_end.x, epsilon)
&& within_range(y_intercept, a_start.y, a_end.y, epsilon)
{
if within_range(x_intercept, b_start.x, b_end.x, epsilon)
&& within_range(y_intercept, b_start.y, b_end.y, epsilon)
{
let collision_point = Point2::new(x_intercept, y_intercept);
let mut collision = Collision::new(collision_point.clone(), line_a_id, line_b_id);
if points_almost_equal(&a_start, &collision_point) {
collision.shape_a_degeneracy = Degeneracy::IsStart;
}
if points_almost_equal(&a_end, &collision_point) {
collision.shape_a_degeneracy = Degeneracy::IsEnd;
}
if points_almost_equal(&b_start, &collision_point) {
collision.shape_b_degeneracy = Degeneracy::IsStart;
}
if points_almost_equal(&b_end, &collision_point) {
collision.shape_b_degeneracy = Degeneracy::IsEnd;
}
return vec![collision];
}
}
vec![]
}
pub fn line_arc_collisions(
&self,
line_a: &Line2,
line_a_id: u64,
arc_b: &Arc2,
arc_b_id: u64,
) -> Vec<Collision> {
// treat this is circle/circle collision, then just do some checks
// afterwards to make sure the collision points really do fall within
// the bounds of the arc
println!("LINE AGAINST ARC");
let arc_center = self.points.get(&arc_b.center).unwrap();
// println!("Getting arc start: {}", &arc.start);
let arc_start = self.points.get(&arc_b.start).unwrap();
let arc_dx = arc_center.x - arc_start.x;
let arc_dy = arc_center.y - arc_start.y;
let arc_radius = arc_dx.hypot(arc_dy);
let fake_circle = Circle2 {
center: arc_b.center,
radius: arc_radius,
top: arc_b.start,
};
println!("Fake circle: {:?}", &fake_circle);
println!("Line Details:");
let line_start = &self.points[&line_a.start];
println!("start: {:?}", line_start);
let line_end = &self.points[&line_a.end];
println!("end: {:?}", line_end);
let fake_collisions =
self.line_circle_collisions(line_a, line_a_id, &fake_circle, arc_b_id);
println!("Fake collisions: {:?}", fake_collisions);
let mut real_collisions: Vec<Collision> = vec![];
for c in fake_collisions {
// check to make sure the point falls within the arc.
if self.point_within_arc(arc_b, &c.point) {
real_collisions.push(c);
}
}
real_collisions
}
pub fn line_circle_collisions(
&self,
line_a: &Line2,
line_a_id: u64,
circle_b: &Circle2,
circle_b_id: u64,
) -> Vec<Collision> {
// to make the math easier, let's assume the circle's center point is the origin
// let's translate the line's start and end points
let mut start = self.points[&line_a.start].clone();
let mut end = self.points[&line_a.end].clone();
let center = self.points[&circle_b.center].clone();
let r = circle_b.radius;
println!("Radius as reported by circle: {}", r);
start.x -= center.x;
start.y -= center.y;
end.x -= center.x;
end.y -= center.y;
// get the line in normal form
let (a, b, c) = normal_form(&start, &end);
println!("Line from {:?} to {:?}", &start, &end);
println!("In normal form: {} {} {}", a, b, c);
let (mut y1, mut y2, mut x1, mut x2);
let dy = (end.y - start.y).abs();
if a == 0.0 || dy < 1e-10 {
println!("It's a horizontal line");
// oh, it's a horizontal line! that makes the math easier
y1 = -c / b;
y2 = -c / b;
println!("Y1 and Y2: {} {}", y1, y2);
x1 = (r * r - y1 * y1).sqrt();
x2 = -(r * r - y1 * y1).sqrt();
// println!("X1 and X2: {} {}", x1, x2);
} else {
println!("It's not a special case");
let det = a * a + b * b;
let d = (a * a * ((r * r * det) - c * c)).sqrt();
y1 = (-d - b * c) / det;
// println!("y1 {}", y1);
y2 = (d - b * c) / det;
// println!("y2 {}", y2);
x1 = -(b * y1 + c) / a;
// println!("x1 {}", x1);
x2 = -(b * y2 + c) / a;
// println!("x2 {}", x2);
}
println!("In shifted coordinates:");
println!("x1 y1 {} {}", x1, y1);
println!("x2 y2 {} {}", x2, y2);
if (x1.hypot(y1) - r).abs() > 1e-10 {
panic!(
"Somehow the radius is not equal! Got {} expected {}",
x1.hypot(y1),
r
);
}
if (x2.hypot(y2) - r).abs() > 1e-10 {
panic!(
"Somehow the radius is not equal! Got {} expected {}",
x2.hypot(y2),
r
);
}
y1 += center.y;
y2 += center.y;
x1 += center.x;
x2 += center.x;
start.x += center.x;
start.y += center.y;
end.x += center.x;
end.y += center.y;
println!("In real coordinates:");
println!("x1 y1 {} {}", x1, y1);
println!("x2 y2 {} {}", x2, y2);
// println!("X1 Y1: {} {}", x1, y1);
// println!("X2 Y2: {} {}", x2, y2);
let mut valid_collisions: Vec<Collision> = vec![];
let start = &self.points[&line_a.start];
let end = &self.points[&line_a.end];
let epsilon = 1e-10;
println!("Checking that X {} is within {} {}", x1, start.x, end.x);
println!("Checking that Y {} is within {} {}", y1, start.y, end.y);
if within_range(x1, start.x, end.x, epsilon) && within_range(y1, start.y, end.y, epsilon) {
println!("Added that point!");
valid_collisions.push(Collision::new(Point2::new(x1, y1), line_a_id, circle_b_id));
}
println!("Checking that X {} is within {} {}", x2, start.x, end.x);
println!("Checking that Y {} is within {} {}", y2, start.y, end.y);
if within_range(x2, start.x, end.x, epsilon) && within_range(y2, start.y, end.y, epsilon) {
println!("Added that point!");
valid_collisions.push(Collision::new(Point2::new(x2, y2), line_a_id, circle_b_id));
}
valid_collisions
}
pub fn circle_circle_collisions(
&self,
circle_a: &Circle2,
circle_a_id: u64,
circle_b: &Circle2,
circle_b_id: u64,
) -> Vec<Collision> {
let center_a = self.points.get(&circle_a.center).unwrap();
let center_b = self.points.get(&circle_b.center).unwrap();
let r_a = circle_a.radius;
let r_b = circle_b.radius;
// compute distance between centers
let center_dx = center_b.x - center_a.x;
let center_dy = center_b.y - center_a.y;
let center_dist = center_dx.hypot(center_dy);
// if the circles are too far away OR too close, they don't intersect
if center_dist > r_a + r_b {
return vec![];
}
if center_dist < (r_a - r_b).abs() {
return vec![];
}
let epsilon = 1e-10;
if center_dist > r_a + r_b - epsilon && center_dist < r_a + r_b + epsilon {
// draw a straight line from a to b, of length r_a
let collision = Collision::new(
Point2::new(
center_a.x + r_a * center_dx / center_dist,
center_a.y + r_a * center_dy / center_dist,
),
circle_a_id,
circle_b_id,
);
return vec![collision];
}
let r_2 = center_dist * center_dist;
let r_4 = r_2 * r_2;
let a = (r_a * r_a - r_b * r_b) / (2.0 * r_2);
let r_2_r_2 = r_a * r_a - r_b * r_b;
let c = (2.0 * (r_a * r_a + r_b * r_b) / r_2 - r_2_r_2 * r_2_r_2 / r_4 - 1.0).sqrt();
let fx = (center_a.x + center_b.x) / 2.0 + a * (center_b.x - center_a.x);
let gx = c * (center_b.y - center_a.y) / 2.0;
let ix1 = fx + gx;
let ix2 = fx - gx;
let fy = (center_a.y + center_b.y) / 2.0 + a * (center_b.y - center_a.y);
let gy = c * (center_a.x - center_b.x) / 2.0;
let iy1 = fy + gy;
let iy2 = fy - gy;
let collision_a = Collision::new(Point2::new(ix1, iy1), circle_a_id, circle_b_id);
let collision_b = Collision::new(Point2::new(ix2, iy2), circle_a_id, circle_b_id);
return vec![collision_a, collision_b];
}
pub fn circle_circle_intersection(
&self,
circle_a: &Circle2,
circle_b: &Circle2,
) -> Intersection {
let center_a = self.points.get(&circle_a.center).unwrap();
let center_b = self.points.get(&circle_b.center).unwrap();
let r_a = circle_a.radius;
let r_b = circle_b.radius;
// compute distance between centers
let center_dx = center_b.x - center_a.x;
let center_dy = center_b.y - center_a.y;
let center_dist = center_dx.hypot(center_dy);
// if the circles are too far away OR too close, they don't intersect
if center_dist > r_a + r_b {
return Intersection::None;
}
if center_dist < (r_a - r_b).abs() {
return Intersection::None;
}