-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathintersections.jl
1213 lines (1012 loc) · 48.3 KB
/
intersections.jl
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
@testitem "Point intersection" setup = [Setup] begin
p = cart(0, 0)
q = cart(-1, -1)
b = Box(cart(0, 0), cart(1, 1))
@test p ∩ p == p
@test q ∩ q == q
@test p ∩ b == b ∩ p == p
@test isnothing(p ∩ q)
@test isnothing(q ∩ b)
end
@testitem "Segment intersection" setup = [Setup] begin
# segments in 2D
s1 = Segment(cart(0, 0), cart(1, 0))
s2 = Segment(cart(0.5, 0.0), cart(2, 0))
@test s1 ∩ s2 ≈ Segment(cart(0.5, 0.0), cart(1, 0))
@test s2 ∩ s1 ≈ Segment(cart(0.5, 0.0), cart(1, 0))
s1 = Segment(cart(0, 0), cart(1, -1))
s2 = Segment(cart(0.5, -0.5), cart(1.5, -1.5))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ Segment(cart(0.5, -0.5), cart(1, -1))
s1 = Segment(cart(0, 0), cart(1, 0))
s2 = Segment(cart(0, 0), cart(0, 1))
@test s1 ∩ s2 ≈ cart(0, 0)
@test s2 ∩ s1 ≈ cart(0, 0)
s1 = Segment(cart(0, 0), cart(1, 0))
s2 = Segment(cart(0, 0), cart(-1, 0))
@test s1 ∩ s2 ≈ cart(0, 0)
@test s2 ∩ s1 ≈ cart(0, 0)
s1 = Segment(cart(0, 0), cart(0, 1))
s2 = Segment(cart(0, 0), cart(0, -1))
@test s1 ∩ s2 ≈ cart(0, 0)
@test s2 ∩ s1 ≈ cart(0, 0)
s1 = Segment(cart(1, 1), cart(1, 2))
s2 = Segment(cart(1, 1), cart(1, 0))
@test s1 ∩ s2 ≈ cart(1, 1)
@test s2 ∩ s1 ≈ cart(1, 1)
s1 = Segment(cart(1, 1), cart(2, 1))
s2 = Segment(cart(1, 0), cart(3, 0))
@test s1 ∩ s2 === nothing
@test s2 ∩ s1 === nothing
s1 = Segment(cart(0.181429364026879, 0.546811355144474), cart(0.38282226144778, 0.107781953228536))
s2 = Segment(cart(0.412498700935005, 0.212081819871479), cart(0.395936725690311, 0.252041094122474))
@test s1 ∩ s2 === nothing
@test s2 ∩ s1 === nothing
s1 = Segment(cart(1, 2), cart(1, 0))
s2 = Segment(cart(1, 0), cart(1, 1))
@test s1 ∩ s2 ≈ Segment(cart(1, 1), cart(1, 0))
@test s2 ∩ s1 ≈ Segment(cart(1, 0), cart(1, 1))
s1 = Segment(cart(0, 0), cart(2, 0))
s2 = Segment(cart(-2, 0), cart(-1, 0))
s3 = Segment(cart(-1, 0), cart(-2, 0))
@test s1 ∩ s2 === s2 ∩ s1 === nothing
@test s1 ∩ s3 === s3 ∩ s1 === nothing
s1 = Segment(cart(-1, 0), cart(0, 0))
s2 = Segment(cart(0, 0), cart(2, 0))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ cart(0, 0)
s1 = Segment(cart(-1, 0), cart(1, 0))
s2 = Segment(cart(0, 0), cart(3, 0))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ Segment(cart(0, 0), cart(1, 0))
s1 = Segment(cart(0, 0), cart(1, 0))
s2 = Segment(cart(0, 0), cart(2, 0))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ Segment(cart(0, 0), cart(1, 0))
s1 = Segment(cart(0, 0), cart(3, 0))
s2 = Segment(cart(1, 0), cart(2, 0))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ s2
s1 = Segment(cart(0, 0), cart(2, 0))
s2 = Segment(cart(1, 0), cart(2, 0))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ s2
s1 = Segment(cart(0, 0), cart(2, 0))
s2 = Segment(cart(1, 0), cart(3, 0))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ Segment(cart(1, 0), cart(2, 0))
s1 = Segment(cart(0, 0), cart(2, 0))
s2 = Segment(cart(2, 0), cart(3, 0))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ cart(2, 0)
s1 = Segment(cart(0, 0), cart(2, 0))
s2 = Segment(cart(3, 0), cart(4, 0))
@test s1 ∩ s2 === s2 ∩ s1 === nothing
s1 = Segment(cart(2, 1), cart(1, 2))
s2 = Segment(cart(1, 0), cart(1, 1))
@test s1 ∩ s2 === s2 ∩ s1 === nothing
s1 = Segment(cart(1.5, 1.5), cart(3.0, 1.5))
s2 = Segment(cart(3.0, 1.0), cart(2.0, 2.0))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ cart(2.5, 1.5)
s1 = Segment(cart(0.94495744, 0.53224397), cart(0.94798386, 0.5344541))
s2 = Segment(cart(0.94798386, 0.5344541), cart(0.9472896, 0.5340202))
@test s1 ∩ s2 ≈ s2 ∩ s1 ≈ cart(0.94798386, 0.5344541)
s₁ = Segment(cart(0, 0), cart(3, 4))
s₂ = Segment(cart(1, 2), cart(3, -2))
s₃ = Segment(cart(2, 0), cart(-2, 0))
s₄ = Segment(cart(0, 0), cart(1, 2))
s₅ = Segment(cart(1, 2), cart(3, 4))
s₆ = Segment(cart(-1, -4 / 3), cart(0, 0))
s₇ = Segment(cart(1, 2), cart(0, 4))
s₈ = Segment(cart(4, 16 / 3), cart(3, 4))
s₉ = Segment(cart(-1, 5), cart(1, 4))
s₁₀ = Segment(cart(1, 4), cart(-1, 5))
s₁₁ = Segment(cart(-2, 5.5), cart(-0.8, 4.9))
s₁₂ = Segment(cart(-0.8, 4.9), cart(-2, 5.5))
s₁₃ = Segment(cart(-0.5, 4.75), cart(0.2, 4.4))
s₁₄ = Segment(cart(0.2, 4.4), cart(-0.5, 4.75))
s₁₅ = Segment(cart(0.5, 4.25), cart(1, 4))
s₁₆ = Segment(cart(1, 4), cart(0.5, 4.25))
s₁₇ = Segment(cart(2, 3.5), cart(1.5, 3.75))
s₁₈ = Segment(cart(1.5, 3.75), cart(2, 3.5))
@test s₁ ∩ s₂ ≈ s₂ ∩ s₁ ≈ cart(1.2, 1.6) # CASE 1: Crossing Segments
@test intersection(s₁, s₂) |> type == Crossing
@test intersection(s₂, s₁) |> type == Crossing
@test s₁ ∩ s₃ ≈ s₃ ∩ s₁ ≈ cart(0, 0) # CASE 2: EdgeTouching (s₁(0))
@test intersection(s₁, s₃) |> type == EdgeTouching
@test intersection(s₃, s₁) |> type == EdgeTouching
@test s₂ ∩ s₃ ≈ s₃ ∩ s₂ ≈ cart(2, 0) # CASE 2: EdgeTouching (s₃(1))
@test intersection(s₂, s₃) |> type == EdgeTouching
@test intersection(s₃, s₂) |> type == EdgeTouching
@test s₁ ∩ s₄ ≈ s₄ ∩ s₁ ≈ cart(0, 0) # CASE 3: CornerTouching (s₁(0), s₄(0))
@test intersection(s₁, s₄) |> type == CornerTouching
@test intersection(s₄, s₁) |> type == CornerTouching
@test s₂ ∩ s₄ ≈ s₄ ∩ s₂ ≈ cart(1, 2) # CASE 3: CornerTouching (s₂(0), s₄(1))
@test intersection(s₂, s₄) |> type == CornerTouching
@test intersection(s₄, s₂) |> type == CornerTouching
@test s₁ ∩ s₅ ≈ s₅ ∩ s₁ ≈ cart(3, 4) # CASE 3: CornerTouching (s₁(1), s₅(1))
@test intersection(s₂, s₄) |> type == CornerTouching
@test intersection(s₄, s₂) |> type == CornerTouching
@test s₁ ∩ s₆ ≈ s₆ ∩ s₁ ≈ cart(0, 0) # CASE 3: CornerTouching (s₁(0), s₆(1)), collinear
@test intersection(s₁, s₆) |> type == CornerTouching
@test intersection(s₆, s₁) |> type == CornerTouching
@test s₂ ∩ s₇ ≈ s₇ ∩ s₂ ≈ cart(1, 2) # CASE 3: CornerTouching (s₂(0), s₇(0)), collinear
@test intersection(s₂, s₇) |> type == CornerTouching
@test intersection(s₇, s₂) |> type == CornerTouching
@test s₁ ∩ s₈ ≈ s₈ ∩ s₁ ≈ cart(3, 4) # CASE 3: CornerTouching (s₁(1), s₈(1)), collinear
@test intersection(s₁, s₈) |> type == CornerTouching
@test intersection(s₈, s₁) |> type == CornerTouching
@test s₉ ∩ s₉ ≈ s₉ # CASE 4: Overlapping (same segment)
@test intersection(s₉, s₉) |> type == Overlapping
@test s₉ ∩ s₁₀ ≈ s₉ # CASE 4: Overlapping (same segment, flipped points)
@test s₁₀ ∩ s₉ ≈ s₁₀
@test intersection(s₉, s₁₀) |> type == Overlapping
@test intersection(s₁₀, s₉) |> type == Overlapping
@test s₉ ∩ s₁₁ ≈ s₁₁ ∩ s₉ ≈ Segment(cart(-1, 5), cart(-0.8, 4.9)) # CASE 4: Overlapping (same alignment)
@test intersection(s₉, s₁₁) |> type == Overlapping
@test intersection(s₁₁, s₉) |> type == Overlapping
@test s₉ ∩ s₁₂ ≈ Segment(cart(-1, 5), cart(-0.8, 4.9)) # CASE 4: Overlapping (opposite alignment, λ = 0 involved)
@test s₁₂ ∩ s₉ ≈ Segment(cart(-0.8, 4.9), cart(-1, 5)) # flipped Points in Segment
@test intersection(s₉, s₁₂) |> type == Overlapping
@test intersection(s₁₂, s₉) |> type == Overlapping
@test s₁₀ ∩ s₁₁ ≈ Segment(cart(-0.8, 4.9), cart(-1, 5)) # CASE 4: Overlapping (opposite alignment, λ = 1 involved)
@test s₁₁ ∩ s₁₀ ≈ Segment(cart(-1, 5), cart(-0.8, 4.9)) # flipped Points in Segment
@test intersection(s₁₀, s₁₁) |> type == Overlapping
@test intersection(s₁₁, s₁₀) |> type == Overlapping
@test s₉ ∩ s₁₃ ≈ s₁₃ ∩ s₉ ≈ s₁₃ # CASE 4: Overlapping (same alignment)
@test intersection(s₉, s₁₃) |> type == Overlapping
@test intersection(s₁₃, s₉) |> type == Overlapping
@test s₁₄ ∩ s₉ ≈ s₁₄ # CASE 4: Overlapping (opposite alignment)
@test s₉ ∩ s₁₄ ≈ s₁₃ # flipped Points in Segment
@test intersection(s₉, s₁₄) |> type == Overlapping
@test intersection(s₁₄, s₉) |> type == Overlapping
@test s₉ ∩ s₁₅ ≈ s₁₅ ∩ s₉ ≈ s₁₅ # CASE 4: Overlapping (same alignment, corner case)
@test intersection(s₉, s₁₅) |> type == Overlapping
@test intersection(s₁₅, s₉) |> type == Overlapping
@test s₁₅ ∩ s₁₀ ≈ s₁₅ # CASE 4: Overlapping (same alignment, corner case)
@test s₁₀ ∩ s₁₅ ≈ s₁₆ # flipped Points in Segment
@test intersection(s₁₀, s₁₅) |> type == Overlapping
@test intersection(s₁₅, s₁₀) |> type == Overlapping
@test s₁₆ ∩ s₉ ≈ s₁₆ # CASE 4: Overlapping (opposite alignment, corner case)
@test s₉ ∩ s₁₆ ≈ s₁₅ # flipped Points in Segment
@test intersection(s₉, s₁₆) |> type == Overlapping
@test intersection(s₁₆, s₉) |> type == Overlapping
@test s₁₀ ∩ s₁₆ ≈ s₁₆ ∩ s₁₀ ≈ s₁₆ # CASE 4: Overlapping (same alignment, corner case)
@test intersection(s₁₀, s₁₆) |> type == Overlapping
@test intersection(s₁₆, s₁₀) |> type == Overlapping
@test s₉ ∩ s₁₇ === s₁₇ ∩ s₉ === nothing # CASE 5: NotIntersecting (collinear, same alignment)
@test intersection(s₉, s₁₇) |> type == NotIntersecting
@test intersection(s₁₇, s₉) |> type == NotIntersecting
@test s₁₀ ∩ s₁₇ === s₁₇ ∩ s₁₀ === nothing # CASE 5: NotIntersecting (collinear, opposite alignment)
@test intersection(s₁₀, s₁₇) |> type == NotIntersecting
@test intersection(s₁₇, s₁₀) |> type == NotIntersecting
@test s₉ ∩ s₁₈ === s₁₈ ∩ s₉ === nothing # CASE 5: NotIntersecting (collinear, opposite alignment)
@test intersection(s₉, s₁₈) |> type == NotIntersecting
@test intersection(s₁₈, s₉) |> type == NotIntersecting
@test s₁ ∩ s₉ === s₉ ∩ s₁ === nothing # CASE 5: NotIntersecting, one λ in range
@test intersection(s₉, s₁) |> type == NotIntersecting
@test intersection(s₁, s₉) |> type == NotIntersecting
@test s₁ ∩ s₁₀ === s₁₀ ∩ s₁ === nothing # CASE 5: NotIntersecting, one λ in range
@test intersection(s₁₀, s₁) |> type == NotIntersecting
@test intersection(s₁, s₁₀) |> type == NotIntersecting
@test s₃ ∩ s₉ === s₉ ∩ s₃ === nothing # CASE 5: NotIntersecting
@test intersection(s₉, s₁) |> type == NotIntersecting
@test intersection(s₁, s₉) |> type == NotIntersecting
@test s₃ ∩ s₁₀ === s₁₀ ∩ s₃ === nothing # CASE 5: NotIntersecting
@test intersection(s₁₀, s₃) |> type == NotIntersecting
@test intersection(s₃, s₁₀) |> type == NotIntersecting
# segments in 3D
s1 = Segment(cart(0.0, 0.0, 0.0), cart(1.0, 0.0, 0.0))
s2 = Segment(cart(0.5, 1.0, 0.0), cart(0.5, -1.0, 0.0))
s3 = Segment(cart(0.5, 0.0, 0.0), cart(1.5, 0.0, 0.0))
s4 = Segment(cart(0.0, 1.0, 0.0), cart(0.0, -2.0, 0.0))
s5 = Segment(cart(-1.0, 1.0, 0.0), cart(2.0, -2.0, 0.0))
s6 = Segment(cart(0.0, 0.0, 0.0), cart(0.0, 1.0, 0.0))
s7 = Segment(cart(-1.0, 1.0, 0.0), cart(-1.0, -1.0, 0.0))
s8 = Segment(cart(-1.0, 1.0, 1.0), cart(-1.0, -1.0, 1.0))
s9 = Segment(cart(0.5, 1.0, 1.0), cart(0.5, -1.0, 1.0))
s10 = Segment(cart(0.0, 1.0, 0.0), cart(1.0, 1.0, 0.0))
s11 = Segment(cart(1.5, 0.0, 0.0), cart(2.5, 0.0, 0.0))
s12 = Segment(cart(1.0, 0.0, 0.0), cart(2.0, 0.0, 0.0))
@test intersection(s1, s2) |> type == Crossing
@test s1 ∩ s2 ≈ cart(0.5, 0.0, 0.0)
@test intersection(s1, s3) |> type == Overlapping
@test s1 ∩ s3 ≈ Segment(cart(0.5, 0.0, 0.0), cart(1.0, 0.0, 0.0))
@test intersection(s1, s4) |> type == EdgeTouching
@test s1 ∩ s4 ≈ cart(0.0, 0.0, 0.0)
@test intersection(s1, s5) |> type == EdgeTouching
@test s1 ∩ s5 ≈ cart(0.0, 0.0, 0.0)
@test intersection(s1, s6) |> type == CornerTouching
@test s1 ∩ s6 ≈ cart(0.0, 0.0, 0.0)
@test intersection(s1, s7) |> type == NotIntersecting
@test isnothing(s1 ∩ s7)
@test intersection(s1, s8) |> type == NotIntersecting
@test isnothing(s1 ∩ s8)
@test intersection(s1, s9) |> type == NotIntersecting
@test isnothing(s1 ∩ s9)
@test intersection(s1, s10) |> type == NotIntersecting
@test isnothing(s1 ∩ s10)
@test intersection(s1, s11) |> type == NotIntersecting
@test isnothing(s1 ∩ s11)
@test intersection(s1, s12) |> type == CornerTouching
@test s1 ∩ s12 ≈ cart(1.0, 0.0, 0.0)
# precision test
s1 = Segment(cart(2.0, 2.0), cart(3.0, 1.0))
s2 = Segment(cart(2.12505, 1.87503), cart(50000.0, 30000.0))
s3 = Segment(cart(2.125005, 1.875003), cart(50000.0, 30000.0))
s4 = Segment(cart(2.125005, 1.875003), cart(50002.125005, 30001.875003))
@test s1 ∩ s2 === s2 ∩ s1 === nothing
@test s1 ∩ s3 === s3 ∩ s1 === ((T == Float32) ? cart(2.125005, 1.875003) : nothing)
@test s1 ∩ s4 === s4 ∩ s1 === ((T == Float32) ? cart(2.125005, 1.875003) : nothing)
# type stability tests
s1 = Segment(cart(0, 0), cart(1, 0))
s2 = Segment(cart(0.5, 0.0), cart(2, 0))
@inferred someornone(s1, s2)
s1 = Segment(cart(0.0, 0.0, 0.0), cart(1.0, 0.0, 0.0))
s2 = Segment(cart(0.5, 1.0, 0.0), cart(0.5, -1.0, 0.0))
@inferred someornone(s1, s2)
# rays and segments in 2D
r₁ = Ray(cart(1, 0), vector(2, 1))
s₁ = Segment(cart(0, 2), cart(2, -1)) # Crossing
s₂ = Segment(cart(0, 2), cart(1, 0.5)) # NotIntersecting
s₃ = Segment(cart(0, 2), cart(0.5, -0.5)) # NotIntersecting
s₄ = Segment(cart(0.5, 1), cart(1.5, -1)) # EdgeTouching
s₅ = Segment(cart(1.5, 0.25), cart(1.5, 2)) # EdgeTouching
s₆ = Segment(cart(1, 0), cart(1, -1)) # CornerTouching
s₇ = Segment(cart(0.5, -1), cart(1, 0)) # CornerTouching
@test intersection(r₁, s₁) |> type == Crossing #CASE 1
@test r₁ ∩ s₁ ≈ s₁ ∩ r₁ ≈ cart(1.25, 0.125)
@test intersection(r₁, s₂) |> type == NotIntersecting # CASE 5
@test r₁ ∩ s₂ === s₂ ∩ r₁ === nothing
@test intersection(r₁, s₃) |> type == NotIntersecting # CASE 5
@test r₁ ∩ s₃ === s₃ ∩ r₁ === nothing
@test intersection(r₁, s₄) |> type == EdgeTouching # CASE 2
@test r₁ ∩ s₄ ≈ s₄ ∩ r₁ ≈ r₁(0)
@test intersection(r₁, s₅) |> type == EdgeTouching # CASE 2
@test r₁ ∩ s₅ ≈ s₅ ∩ r₁ ≈ cart(1.5, 0.25)
@test intersection(r₁, s₆) |> type == CornerTouching # CASE 3
@test r₁ ∩ s₆ ≈ s₆ ∩ r₁ ≈ r₁(0)
@test intersection(r₁, s₇) |> type == CornerTouching # CASE 3
@test r₁ ∩ s₇ ≈ s₇ ∩ r₁ ≈ r₁(0)
r₂ = Ray(cart(3, 2), vector(1, 1))
s₈ = Segment(cart(4, 3), cart(5, 4)) # Overlapping
s₉ = Segment(cart(2.5, 1.5), cart(3.3, 2.3)) # Overlapping s(1)
s₁₀ = Segment(cart(3.6, 2.6), cart(2.6, 1.6)) # Overlapping s(0)
s₁₁ = Segment(cart(2.2, 1.2), cart(3, 2)) # CornerTouching, colinear, s(1)
s₁₂ = Segment(cart(3, 2), cart(2.4, 1.4)) # CornerTouching, colinear, s(0)
s₁₃ = Segment(cart(3, 2), cart(3.1, 2.1)) # Overlapping s(0) = r(0)
s₁₄ = Segment(cart(3.2, 2.2), cart(3, 2)) # Overlapping s(1) = r(0)
s₁₅ = Segment(cart(2, 1), cart(1.6, 0.6)) # No Intersection, colinear
s₁₆ = Segment(cart(3, 1), cart(4, 2)) # No Intersection, parallel
@test intersection(r₂, s₈) |> type == Overlapping # CASE 4
@test r₂ ∩ s₈ === s₈ ∩ r₂ === s₈
@test intersection(r₂, s₉) |> type == Overlapping # CASE 4
@test r₂ ∩ s₉ == s₉ ∩ r₂ == Segment(r₂(0), s₉(1))
@test intersection(r₂, s₁₀) |> type == Overlapping # CASE 4
@test r₂ ∩ s₁₀ == s₁₀ ∩ r₂ == Segment(r₂(0), s₁₀(0))
@test intersection(r₂, s₁₁) |> type == CornerTouching # CASE 3
@test r₂ ∩ s₁₁ ≈ s₁₁ ∩ r₂ ≈ r₂(0)
@test intersection(r₂, s₁₂) |> type == CornerTouching # CASE 3
@test r₂ ∩ s₁₂ ≈ s₁₂ ∩ r₂ ≈ r₂(0)
@test intersection(r₂, s₁₃) |> type == Overlapping # CASE 4
@test r₂ ∩ s₁₃ === s₁₃ ∩ r₂ === s₁₃
@test intersection(r₂, s₁₄) |> type == Overlapping # CASE 4
@test r₂ ∩ s₁₄ === s₁₄ ∩ r₂ === s₁₄
@test intersection(r₂, s₁₅) |> type == NotIntersecting # CASE 5
@test r₂ ∩ s₁₅ === s₁₅ ∩ r₂ === nothing
@test intersection(r₂, s₁₆) |> type == NotIntersecting # CASE 5
@test r₂ ∩ s₁₆ === s₁₆ ∩ r₂ === nothing
# type stability tests
r₁ = Ray(cart(0, 0), vector(1, 0))
s₁ = Segment(cart(-1, -1), cart(-1, 1))
@inferred someornone(r₁, s₁)
# 3D test
r₁ = Ray(cart(1, 2, 3), vector(1, 2, 3))
s₁ = Segment(cart(1, 3, 5), cart(3, 5, 7))
@test intersection(r₁, s₁) |> type === Crossing # CASE 1
@test r₁ ∩ s₁ ≈ s₁ ∩ r₁ ≈ cart(2, 4, 6)
s₂ = Segment(cart(0, 1, 2), cart(2, 3, 4))
@test intersection(r₁, s₂) |> type === EdgeTouching # CASE 2
@test r₁ ∩ s₂ == s₂ ∩ r₁ == r₁(0)
s₃ = Segment(cart(0.23, 1, 2.3), cart(1, 2, 3))
@test intersection(r₁, s₃) |> type === CornerTouching # CASE 3
@test r₁ ∩ s₃ == s₃ ∩ r₁ == r₁(0)
s₄ = Segment(cart(0, 0, 0), cart(2, 4, 6))
@test intersection(r₁, s₄) |> type === Overlapping # CASE 4
@test r₁ ∩ s₄ == s₄ ∩ r₁ == Segment(cart(1, 2, 3), cart(2, 4, 6))
s₅ = Segment(cart(0, 0, 0), cart(0.5, 1, 1.5))
@test intersection(r₁, s₅) |> type === NotIntersecting # CASE 5
@test r₁ ∩ s₅ === s₅ ∩ r₁ === nothing
l₁ = Line(cart(1, 0), cart(3, 1))
s₁ = Segment(cart(0, 2), cart(2, -1)) # Crossing
s₂ = Segment(cart(0.5, 1), cart(0, 0)) # NotIntersecting
s₃ = Segment(cart(0, 2), cart(-2, 1)) # NotIntersecting
s₄ = Segment(cart(0.5, -1), cart(1, 0)) # Touching
s₅ = Segment(cart(1.5, 0.25), cart(1.5, 2)) # Touching
s₆ = Segment(cart(-3, -2), cart(4, 1.5)) # Overlapping
@test intersection(l₁, s₁) |> type == Crossing #CASE 1
@test l₁ ∩ s₁ ≈ s₁ ∩ l₁ ≈ cart(1.25, 0.125)
@test intersection(l₁, s₂) |> type == NotIntersecting # CASE 4
@test l₁ ∩ s₂ === s₂ ∩ l₁ === nothing
@test intersection(l₁, s₃) |> type == NotIntersecting # CASE 4
@test l₁ ∩ s₃ === s₃ ∩ l₁ === nothing
@test intersection(l₁, s₄) |> type == Touching # CASE 2
@test l₁ ∩ s₄ ≈ s₄ ∩ l₁ ≈ s₄(1)
@test intersection(l₁, s₅) |> type == Touching # CASE 2
@test l₁ ∩ s₅ ≈ s₅ ∩ l₁ ≈ s₅(0)
@test intersection(l₁, s₆) |> type == Overlapping # CASE 3
@test l₁ ∩ s₆ ≈ s₆ ∩ l₁ ≈ s₆
# type stability tests
@inferred someornone(l₁, s₁)
@inferred someornone(l₁, s₂)
# 3d tests
l₁ = Line(cart(1, 0, 1), cart(3, 1, 1))
s₁ = Segment(cart(0, 2, 1), cart(2, -1, 1)) # Crossing
s₂ = Segment(cart(0.5, 1, 1), cart(0, 0, 1)) # NotIntersecting
s₃ = Segment(cart(0, 2, 1), cart(-2, 1, 1)) # NotIntersecting
s₄ = Segment(cart(0.5, -1, 1), cart(1, 0, 1)) # Touching
s₅ = Segment(cart(1.5, 0.25, 1), cart(1.5, 2, 1)) # Touching
s₆ = Segment(cart(-3, -2, 1), cart(4, 1.5, 1)) # Overlapping
s₇ = Segment(cart(0, 2, 1), cart(2, -1, 1.1)) # NotIntersecting
@test intersection(l₁, s₁) |> type == Crossing #CASE 1
@test l₁ ∩ s₁ ≈ s₁ ∩ l₁ ≈ cart(1.25, 0.125, 1)
@test intersection(l₁, s₂) |> type == NotIntersecting # CASE 4
@test l₁ ∩ s₂ === s₂ ∩ l₁ === nothing
@test intersection(l₁, s₃) |> type == NotIntersecting # CASE 4
@test l₁ ∩ s₃ === s₃ ∩ l₁ === nothing
@test intersection(l₁, s₄) |> type == Touching # CASE 2
@test l₁ ∩ s₄ ≈ s₄ ∩ l₁ ≈ s₄(1)
@test intersection(l₁, s₅) |> type == Touching # CASE 2
@test l₁ ∩ s₅ ≈ s₅ ∩ l₁ ≈ s₅(0)
@test intersection(l₁, s₆) |> type == Overlapping # CASE 3
@test l₁ ∩ s₆ ≈ s₆ ∩ l₁ ≈ s₆
@test intersection(l₁, s₇) |> type == NotIntersecting # CASE 4
@test l₁ ∩ s₇ === s₇ ∩ l₁ === nothing
# degenerate segments
A = cart(0.0, 0.0)
B = cart(0.5, 0.0)
C = cart(1.0, 0.0)
s₀ = Segment(A, C)
s₁ = Segment(A, A)
s₂ = Segment(B, B)
s₃ = Segment(C, C)
@test s₀ ∩ s₁ ≈ s₁ ∩ s₀ ≈ A
@test s₀ ∩ s₂ ≈ s₂ ∩ s₀ ≈ B
@test s₀ ∩ s₃ ≈ s₃ ∩ s₀ ≈ C
@test intersection(s₀, s₁) |> type == CornerTouching
@test intersection(s₀, s₂) |> type == EdgeTouching
@test intersection(s₀, s₃) |> type == CornerTouching
@test s₁ ∩ s₂ === s₂ ∩ s₁ === nothing
@test s₁ ∩ s₃ === s₃ ∩ s₁ === nothing
@test s₂ ∩ s₃ === s₃ ∩ s₂ === nothing
@test intersection(s₁, s₂) |> type == NotIntersecting
@test intersection(s₁, s₃) |> type == NotIntersecting
@test intersection(s₂, s₃) |> type == NotIntersecting
@test s₁ ∩ s₁ ≈ A
@test s₂ ∩ s₂ ≈ B
@test s₃ ∩ s₃ ≈ C
@test intersection(s₁, s₁) |> type == CornerTouching
@test intersection(s₂, s₂) |> type == CornerTouching
@test intersection(s₃, s₃) |> type == CornerTouching
# utils
@test Meshes._sort4vals(2.5, 1.4, 1.1, 2.0) == (1.4, 2.0)
@test Meshes._sort4vals(2.0, 1.1, 1.4, 2.5) == (1.4, 2.0)
@test Meshes._sort4vals(2.0, 2.5, 1.1, 1.4) == (1.4, 2.0)
end
@testitem "Ray intersection" setup = [Setup] begin
# rays in 2D
r₁ = Ray(cart(1, 0), vector(2, 1))
r₂ = Ray(cart(0, 2), vector(2, -3))
r₃ = Ray(cart(0.5, 1), vector(1, -2))
r₄ = Ray(cart(0, 2), vector(1, -3))
r₅ = Ray(cart(4, 1.5), vector(4, 2))
r₆ = Ray(cart(2, 0.5), vector(-0.5, -0.25))
r₇ = Ray(cart(4, 0), vector(0, 1))
@test intersection(r₁, r₂) |> type == Crossing #CASE 1
@test r₁ ∩ r₂ ≈ cart(1.25, 0.125)
@test r₁ ∩ r₇ ≈ cart(4, 1.5)
@test intersection(r₁, r₃) |> type == EdgeTouching #CASE 2
@test r₁ ∩ r₃ ≈ r₁(0) # origin of first ray
@test r₅ ∩ r₇ ≈ r₅(0)
@test intersection(r₃, r₁) |> type == EdgeTouching
@test r₃ ∩ r₁ ≈ r₁(0) # origin of second ray
@test r₇ ∩ r₅ ≈ r₅(0)
@test intersection(r₂, r₄) |> type == CornerTouching #CASE 3
@test r₂ ∩ r₄ ≈ r₂(0) ≈ r₄(0)
@test intersection(r₅, r₁) |> type == PosOverlapping #CASE 4
@test r₅ ∩ r₁ == r₅ # first ray
@test intersection(r₁, r₅) |> type == PosOverlapping #CASE 4
@test r₁ ∩ r₅ == r₅ # second ray
@test intersection(r₁, r₆) |> type == NegOverlapping #CASE 5
@test r₁ ∩ r₆ == Segment(r₁(0), r₆(0))
@test intersection(r₁, r₄) |> type == NotIntersecting #CASE 6
@test r₁ ∩ r₄ === r₄ ∩ r₁ === nothing
# lines and rays in 2D
l₁ = Line(cart(0, 0), cart(4, 5))
r₁ = Ray(cart(3, 4), vector(1, -2)) # crossing ray
r₂ = Ray(cart(1, 1.25), vector(1, 0.3)) # touching ray
r₃ = Ray(cart(-1, -1.25), vector(-1, -1.25)) # overlapping ray
r₄ = Ray(cart(1, 3), vector(1, 1.25)) # parallel ray
r₅ = Ray(cart(1, 1), vector(1, -1)) # no Intersection
@test l₁ ∩ r₁ ≈ r₁ ∩ l₁ ≈ cart(3.0769230769230766, 3.846153846153846) # CASE 1
@test intersection(l₁, r₁) |> type === Crossing
@test l₁ ∩ r₂ == r₂ ∩ l₁ == r₂(0) # CASE 2
@test intersection(l₁, r₂) |> type === Touching
@test l₁ ∩ r₃ == r₃ ∩ l₁ == r₃ # CASE 3
@test intersection(l₁, r₃) |> type === Overlapping
@test l₁ ∩ r₄ == r₄ ∩ l₁ === nothing # CASE 4 parallel
@test intersection(l₁, r₄) |> type === NotIntersecting
@test l₁ ∩ r₅ == r₅ ∩ l₁ === nothing # CASE 4 no intersection
@test intersection(l₁, r₅) |> type === NotIntersecting
# type stability tests
@inferred someornone(l₁, r₁)
@inferred someornone(l₁, r₅)
# 3D tests
# lines and rays in 3D
l₁ = Line(cart(0, 0, 0.1), cart(4, 5, 0.1))
r₁ = Ray(cart(3, 4, 0.1), vector(1, -2, 0)) # crossing ray
r₂ = Ray(cart(1, 1.25, 0.1), vector(1, 0.3, 0)) # touching ray
r₃ = Ray(cart(-1, -1.25, 0.1), vector(-1, -1.25, 0)) # overlapping ray
r₄ = Ray(cart(1, 3, 0.1), vector(1, 1.25, 0)) # parallel ray
r₅ = Ray(cart(1, 1, 0.1), vector(1, -1, 0)) # no Intersection
r₆ = Ray(cart(3, 4, 0), vector(1, -2, 1)) # crossing ray
@test l₁ ∩ r₁ ≈ r₁ ∩ l₁ ≈ cart(3.0769230769230766, 3.846153846153846, 0.1) # CASE 1
@test intersection(l₁, r₁) |> type === Crossing
@test l₁ ∩ r₂ == r₂ ∩ l₁ == r₂(0) # CASE 2
@test intersection(l₁, r₂) |> type === Touching
@test l₁ ∩ r₃ == r₃ ∩ l₁ == r₃ # CASE 3
@test intersection(l₁, r₃) |> type === Overlapping
@test l₁ ∩ r₄ == r₄ ∩ l₁ === nothing # CASE 4 parallel
@test intersection(l₁, r₄) |> type === NotIntersecting
@test l₁ ∩ r₅ == r₅ ∩ l₁ === nothing # CASE 4 no intersection
@test intersection(l₁, r₅) |> type === NotIntersecting
@test l₁ ∩ r₆ == r₆ ∩ l₁ === nothing # CASE 4 no intersection
@test intersection(l₁, r₆) |> type === NotIntersecting
end
@testitem "Line intersection" setup = [Setup] begin
# lines in 2D
l1 = Line(cart(0, 0), cart(1, 0))
l2 = Line(cart(-1, -1), cart(-1, 1))
@test l1 ∩ l2 ≈ l2 ∩ l1 ≈ cart(-1, 0)
l1 = Line(cart(0, 0), cart(1, 0))
l2 = Line(cart(0, 1), cart(1, 1))
@test l1 ∩ l2 === l2 ∩ l1 === nothing
l1 = Line(cart(0, 0), cart(1, 0))
l2 = Line(cart(1, 0), cart(2, 0))
@test l1 == l2
@test l1 ∩ l2 == l2 ∩ l1 == l1
# rounding errors
for k in 1:1000
δ = k * atol(T)
lo = Line(cart(3.0, 1.0), cart(2.0, 2.0))
lδ = Line(cart(1.5, 1.5 + δ), cart(3.0, 1.5 + δ))
p = cart(2.5 - δ, 1.5 + δ)
@test lo ∩ lδ ≈ lδ ∩ lo ≈ p
end
# lines in 3D
# not in same plane
l1 = Line(cart(0, 0, 0), cart(1, 0, 0))
l2 = Line(cart(1, 1, 1), cart(1, 2, 1))
@test l1 ∩ l2 == l2 ∩ l1 === nothing
# in same plane but parallel
l1 = Line(cart(0, 0, 0), cart(1, 0, 0))
l2 = Line(cart(0, 1, 1), cart(1, 1, 1))
@test l1 ∩ l2 == l2 ∩ l1 === nothing
# in same plane and colinear
l1 = Line(cart(0, 0, 0), cart(1, 0, 0))
l2 = Line(cart(2, 0, 0), cart(3, 0, 0))
@test l1 ∩ l2 == l2 ∩ l1 == l1
# crossing in one point
l1 = Line(cart(1, 2, 3), cart(2, 1, 0))
l2 = Line(cart(1, 2, 3), cart(1, 1, 1))
@test l1 ∩ l2 ≈ l2 ∩ l1 ≈ cart(1, 2, 3)
# type stability tests
l1 = Line(cart(0, 0), cart(1, 0))
l2 = Line(cart(-1, -1), cart(-1, 1))
@inferred someornone(l1, l2)
end
@testitem "Chain intersection" setup = [Setup] begin
# https://github.com/JuliaGeometry/Meshes.jl/issues/644
r = Rope(cart(0, 0), cart(1, 1))
@test r ∩ r == GeometrySet([Segment(cart(0, 0), cart(1, 1))])
@inferred someornone(r, r)
end
@testitem "Plane intersection" setup = [Setup] begin
# ---------
# SEGMENTS
# ---------
p = Plane(cart(0, 0, 1), vector(1, 0, 0), vector(0, 1, 0))
# intersecting segment and plane
s = Segment(cart(0, 0, 0), cart(0, 2, 2))
@test intersection(s, p) |> type == Crossing
@test s ∩ p == cart(0, 1, 1)
# intersecting segment and plane with λ ≈ 0
s = Segment(cart(0, 0, 1), cart(0, 2, 2))
@test intersection(s, p) |> type == Touching
@test s ∩ p == cart(0, 0, 1)
# intersecting segment and plane with λ ≈ 1
s = Segment(cart(0, 0, 2), cart(0, 2, 1))
@test intersection(s, p) |> type == Touching
@test s ∩ p == cart(0, 2, 1)
# segment contained within plane
s = Segment(cart(0, 0, 1), cart(0, -2, 1))
@test intersection(s, p) |> type == Overlapping
@test s ∩ p == s
# segment below plane, non-intersecting
s = Segment(cart(0, 0, 0), cart(0, -2, -2))
@test intersection(s, p) |> type == NotIntersecting
@test isnothing(s ∩ p)
# segment parallel to plane, offset, non-intersecting
s = Segment(cart(0, 0, -1), cart(0, -2, -1))
@test intersection(s, p) |> type == NotIntersecting
@test isnothing(s ∩ p)
# plane as first argument
p = Plane(cart(0, 0, 1), vector(1, 0, 0), vector(0, 1, 0))
s = Segment(cart(0, 0, 0), cart(0, 2, 2))
@test intersection(p, s) |> type == Crossing
@test s ∩ p == p ∩ s == cart(0, 1, 1)
# type stability tests
s = Segment(cart(0, 0, 0), cart(0, 2, 2))
p = Plane(cart(0, 0, 1), vector(1, 0, 0), vector(0, 1, 0))
@inferred someornone(s, p)
# -----
# RAYS
# -----
p = Plane(cart(0, 0, 1), vector(1, 0, 0), vector(0, 1, 0))
# intersecting ray and plane
r = Ray(cart(0, 0, 0), vector(0, 2, 2))
@test intersection(r, p) |> type == Crossing
@test r ∩ p == cart(0, 1, 1)
# intersecting ray and plane with λ ≈ 0
r = Ray(cart(0, 0, 1), vector(0, 2, 1))
@test intersection(r, p) |> type == Touching
@test r ∩ p == cart(0, 0, 1)
# intersecting ray and plane with λ ≈ 1 (only case where Ray different to Segment)
r = Ray(cart(0, 0, 2), vector(0, 2, -1))
@test intersection(r, p) |> type == Crossing
@test r ∩ p == cart(0, 2, 1)
# ray contained within plane
r = Ray(cart(0, 0, 1), vector(0, -2, 0))
@test intersection(r, p) |> type == Overlapping
@test r ∩ p == r
# ray below plane, non-intersecting
r = Ray(cart(0, 0, 0), vector(0, -2, -2))
@test intersection(r, p) |> type == NotIntersecting
@test isnothing(r ∩ p)
# ray parallel to plane, offset, non-intersecting
r = Ray(cart(0, 0, -1), vector(0, -2, 0))
@test intersection(r, p) |> type == NotIntersecting
@test isnothing(r ∩ p)
# plane as first argument
p = Plane(cart(0, 0, 1), vector(1, 0, 0), vector(0, 1, 0))
r = Ray(cart(0, 0, 0), vector(0, 2, 2))
@test intersection(p, r) |> type == Crossing
@test r ∩ p == p ∩ r == cart(0, 1, 1)
# ------
# LINES
# ------
p = Plane(cart(0, 0, 1), vector(1, 0, 0), vector(0, 1, 0))
# intersecting line and plane
l = Line(cart(0, 0, 0), cart(0, 2, 2))
@test intersection(l, p) |> type == Crossing
@test l ∩ p == cart(0, 1, 1)
# intersecting line and plane with λ ≈ 0
l = Line(cart(0, 0, 1), cart(0, 2, 2))
@test intersection(l, p) |> type == Crossing
@test l ∩ p == cart(0, 0, 1)
# intersecting line and plane with λ ≈ 1
l = Line(cart(0, 0, 2), cart(0, 2, 1))
@test intersection(l, p) |> type == Crossing
@test l ∩ p == cart(0, 2, 1)
# line contained within plane
l = Line(cart(0, 0, 1), cart(0, -2, 1))
@test intersection(l, p) |> type == Overlapping
@test l ∩ p == l
# line below plane, non-intersecting
l = Line(cart(0, 0, 0), cart(0, -2, -2))
@test intersection(l, p) |> type == Crossing
@test l ∩ p == cart(0, 1, 1)
# line parallel to plane, offset, non-intersecting
l = Line(cart(0, 0, -1), cart(0, -2, -1))
@test intersection(l, p) |> type == NotIntersecting
@test isnothing(l ∩ p)
# plane as first argument
p = Plane(cart(0, 0, 1), vector(1, 0, 0), vector(0, 1, 0))
l = Line(cart(0, 0, 0), cart(0, 2, 2))
@test intersection(p, l) |> type == Crossing
@test l ∩ p == p ∩ l == cart(0, 1, 1)
# type stability tests
l = Line(cart(0, 0, 0), cart(0, 2, 2))
p = Plane(cart(0, 0, 1), vector(1, 0, 0), vector(0, 1, 0))
@inferred someornone(l, p)
# ------
# PLANES
# ------
p1 = Plane(cart(0, 0, 0), vector(0, 0, 1))
# p1 parallel to p2
p2 = Plane(cart(0, 0, 1), vector(0, 0, 1))
@test intersection(p1, p2) |> type == NotIntersecting
@test isnothing(p1 ∩ p2)
# p1 intersects p2
p2 = Plane(cart(0, 0, 1), vector(1 / sqrt(2), 0, 1 / sqrt(2)))
@test intersection(p1, p2) |> type == Intersecting
@test p1 ∩ p2 == Line(cart(1, 0, 0), cart(1, 1, 0))
# CRS propagation
c1 = Cartesian{WGS84Latest}(T(0), T(0), T(0))
c2 = Cartesian{WGS84Latest}(T(0), T(0), T(1))
p1 = Plane(Point(c1), vector(0, 0, 1))
p2 = Plane(Point(c2), vector(1 / sqrt(2), 0, 1 / sqrt(2)))
@test crs(p1 ∩ p2) === crs(p1)
end
@testitem "Box intersection" setup = [Setup] begin
b1 = Box(cart(0, 0), cart(1, 1))
b2 = Box(cart(0.5, 0.5), cart(2, 2))
b3 = Box(cart(2, 2), cart(3, 3))
b4 = Box(cart(1, 1), cart(2, 2))
b5 = Box(cart(1.0, 0.5), cart(2, 2))
b6 = Box(cart(0, 2), cart(1, 3))
b7 = Box(cart(0, 1), cart(1, 2))
b8 = Box(cart(0, -1), cart(1, 0))
b9 = Box(cart(1, 0), cart(2, 1))
b10 = Box(cart(-1, 0), cart(0, 1))
@test intersection(b1, b2) |> type == Overlapping
@test b1 ∩ b2 == Box(cart(0.5, 0.5), cart(1, 1))
@test intersection(b1, b3) |> type == NotIntersecting
@test isnothing(b1 ∩ b3)
@test intersection(b1, b4) |> type == CornerTouching
@test b1 ∩ b4 == cart(1, 1)
@test intersection(b1, b5) |> type == Touching
@test b1 ∩ b5 == Box(cart(1.0, 0.5), cart(1, 1))
@test intersection(b1, b6) |> type == NotIntersecting
@test isnothing(b1 ∩ b6)
@test intersection(b1, b7) |> type == Touching
@test b1 ∩ b7 == Box(cart(0, 1), cart(1, 1))
@test intersection(b1, b8) |> type == Touching
@test b1 ∩ b8 == Box(cart(0, 0), cart(1, 0))
@test intersection(b1, b9) |> type == Touching
@test b1 ∩ b9 == Box(cart(1, 0), cart(1, 1))
@test intersection(b1, b10) |> type == Touching
@test b1 ∩ b10 == Box(cart(0, 0), cart(0, 1))
# more touching examples
b1 = Box(cart(0, 0), cart(1, 1))
b2 = Box(cart(1.0, 0.5), cart(2, 1))
b3 = Box(cart(-1, 0), cart(0.0, 0.5))
b4 = Box(cart(0, 1), cart(0.5, 2.0))
b5 = Box(cart(0.5, -1.0), cart(1, 0))
@test intersection(b1, b2) |> type == Touching
@test b1 ∩ b2 == Box(cart(1.0, 0.5), cart(1, 1))
@test intersection(b1, b3) |> type == Touching
@test b1 ∩ b3 == Box(cart(0.0, 0.0), cart(0.0, 0.5))
@test intersection(b1, b4) |> type == Touching
@test b1 ∩ b4 == Box(cart(0.0, 1.0), cart(0.5, 1.0))
@test intersection(b1, b5) |> type == Touching
@test b1 ∩ b5 == Box(cart(0.5, 0.0), cart(1.0, 0.0))
# tricky examples with degenerate boxes
b1 = Box(cart(0, 0, 0), cart(2, 2, 0))
b2 = Box(cart(3, 0, 0), cart(5, 2, 0))
b3 = Box(cart(1, 0, 0), cart(3, 2, 0))
@test intersection(b1, b2) |> type == NotIntersecting
@test isnothing(b1 ∩ b2)
@test intersection(b1, b3) |> type == Touching
@test b1 ∩ b3 == Box(cart(1, 0, 0), cart(2, 2, 0))
# different units
b1 = Box((T(0) * u"cm", T(0) * u"cm"), (T(100) * u"cm", T(100) * u"cm"))
b2 = Box((T(500) * u"mm", T(500) * u"mm"), (T(2000) * u"mm", T(2000) * u"mm"))
@test intersection(b1, b2) |> type == Overlapping
@test unit(Meshes.lentype(b1 ∩ b2)) == u"cm"
@test b1 ∩ b2 == Box(cart(0.5, 0.5), cart(1, 1))
# type stability tests
b1 = Box(cart(0, 0), cart(1, 1))
b2 = Box(cart(0.5, 0.5), cart(2, 2))
@inferred someornone(b1, b2)
b1 = Box((T(0) * u"cm", T(0) * u"cm"), (T(100) * u"cm", T(100) * u"cm"))
b2 = Box((T(500) * u"mm", T(500) * u"mm"), (T(2000) * u"mm", T(2000) * u"mm"))
@inferred someornone(b1, b2)
# CRS propagation
b1 = Box(merc(0, 0), merc(1, 1))
b2 = Box(merc(0.5, 0.5), merc(2, 2))
@test crs(b1 ∩ b2) === crs(b1)
# Ray-Box intersection
b = Box(cart(0, 0, 0), cart(1, 1, 1))
r = Ray(cart(0, 0, 0), vector(1, 1, 1))
@test intersection(r, b) |> type == Crossing
@test r ∩ b == Segment(cart(0, 0, 0), cart(1, 1, 1))
r = Ray(cart(-0.5, 0, 0), vector(1.0, 1.0, 1.0))
@test intersection(r, b) |> type == Crossing
@test r ∩ b == Segment(cart(0.0, 0.5, 0.5), cart(0.5, 1.0, 1.0))
r = Ray(cart(3.0, 0.0, 0.5), vector(-1.0, 1.0, 0.0))
@test intersection(r, b) |> type == NotIntersecting
r = Ray(cart(2.0, 0.0, 0.5), vector(-1.0, 1.0, 0.0))
@test intersection(r, b) |> type == Touching
@test r ∩ b == cart(1.0, 1.0, 0.5)
# the ray on a face of the box, got NaN in calculation
r = Ray(cart(1.5, 0.0, 0.0), vector(-1.0, 1.0, 0.0))
@test intersection(r, b) |> type == Crossing
@test r ∩ b == Segment(cart(1.0, 0.5, 0.0), cart(0.5, 1.0, 0.0))
end
@testitem "Triangle intersection" setup = [Setup] begin
# utility to reverse segments, to more fully
# test branches in the intersection algorithm
reverse_segment(s) = Segment(vertices(s)[2], vertices(s)[1])
# intersections with triangle lying in XY plane
t = Triangle(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0))
# intersects through t
s = Segment(cart(0.2, 0.2, 1.0), cart(0.2, 0.2, -1.0))
@test intersection(s, t) |> type == Intersecting
@test s ∩ t == cart(0.2, 0.2, 0.0)
# intersects at a vertex of t
s = Segment(cart(0.0, 0.0, 1.0), cart(0.0, 0.0, -1.0))
@test intersection(s, t) |> type == Intersecting
@test s ∩ t == cart(0.0, 0.0, 0.0)
# normal to, doesn't intersect with t
s = Segment(cart(0.9, 0.9, 1.0), cart(0.9, 0.9, -1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# coplanar, doesn't intersect with t
s = Segment(cart(-0.2, -0.2, 0.0), cart(1.2, -0.2, 0.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# parallel, above, doesn't intersect with t
s = Segment(cart(-0.2, 0.2, 1.0), cart(1.2, 0.2, 1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# parallel, below, doesn't intersect with t
s = Segment(cart(-0.2, 0.2, -1.0), cart(1.2, 0.2, -1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# coplanar, within bounding box of t, no intersection
s = Segment(cart(0.7, 0.8, 0.0), cart(0.8, 0.7, 0.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# segment above and to right of t, no intersection
s = Segment(cart(1.0, 1.0, 0.0), cart(1.0, 1.0, 1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# segment below t, no intersection
s = Segment(cart(0.5, -1.0, 0.0), cart(0.5, -1.0, 1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# segment left of t, no intersection
s = Segment(cart(-1.0, 0.5, 0.0), cart(-1.0, 0.5, 1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# segment above and to right of t, no intersection
s = Segment(cart(1.0, 1.0, 0.0), cart(1.0, 1.0, -1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
@test intersection(reverse_segment(s), t) |> type == NotIntersecting
@test isnothing(reverse_segment(s) ∩ t)
# segment below t, no intersection
s = Segment(cart(0.5, -1.0, 0.0), cart(0.5, -1.0, -1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
@test intersection(reverse_segment(s), t) |> type == NotIntersecting
@test isnothing(reverse_segment(s) ∩ t)
# segment left of t, no intersection
s = Segment(cart(-1.0, 0.5, 0.0), cart(-1.0, 0.5, -1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
@test intersection(reverse_segment(s), t) |> type == NotIntersecting
@test isnothing(reverse_segment(s) ∩ t)
# segment above and to right of t, no intersection
s = Segment(cart(1.0, 1.0, 1.0), cart(1.0, 1.0, 0.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# segment below t, no intersection
s = Segment(cart(0.5, -1.0, 1.0), cart(0.5, -1.0, 0.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# segment left of t, no intersection
s = Segment(cart(-1.0, 0.5, 1.0), cart(-1.0, 0.5, 0.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# intersections with an inclined inclined triangle t
t = Triangle(cart(0, 0, 0), cart(2, 0, 0), cart(0, 2, 2))
# doesn't reach t, no intersection
s = Segment(cart(0.5, 0.5, 1.9), cart(0.5, 0.5, 1.8))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# parallel, offset from t, no intersection
s = Segment(cart(0.0, 0.5, 1.0), cart(1.0, 0.5, 1.0))
@test intersection(s, t) |> type == NotIntersecting
@test isnothing(s ∩ t)
# triangle as first argument
t = Triangle(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0))
s = Segment(cart(0.2, 0.2, 1.0), cart(0.2, 0.2, -1.0))
@test intersection(t, s) |> type == Intersecting
@test s ∩ t == t ∩ s == cart(0.2, 0.2, 0.0)
# type stability tests
s = Segment(cart(0.2, 0.2, 1.0), cart(0.2, 0.2, -1.0))
t = Triangle(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0))
@inferred someornone(s, t)
# https://github.com/JuliaGeometry/Meshes.jl/issues/728
s = Segment(cart(0.5, 0.5, 0.0), cart(0.5, 0.5, 2.0))
t = Triangle(cart(1.0, 0.0, 0.0), cart(0.0, 1.0, 0.0), cart(0.0, 0.0, 1.0))
@test intersection(s, t) |> type == Intersecting
@test s ∩ t == t ∩ s == cart(0.5, 0.5, 0.0)
s = Segment(cart(0.5, 0.5, 2.0), cart(0.5, 0.5, 0.0))
@test intersection(s, t) |> type == Intersecting
@test s ∩ t == t ∩ s == cart(0.5, 0.5, 0.0)
# Intersection for a triangle and a ray
t = Triangle(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0))
# intersects through t
r = Ray(cart(0.2, 0.2, 1.0), vector(0.0, 0.0, -1.0))
@test intersection(r, t) |> type == Crossing
@test r ∩ t == cart(0.2, 0.2, 0.0)
# origin of ray intersects with middle of triangle
r = Ray(cart(0.2, 0.2, 0.0), vector(0.0, 0.0, -1.0))
@test intersection(r, t) |> type == Touching
@test r ∩ t == cart(0.2, 0.2, 0.0)
# Special case: the direction vector is not length enough to cross triangle