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
use num_traits::{real::Real, FloatConst, Zero, One, AsPrimitive};
use approx::RelativeEq;
use std::ops::*;
use std::ops::Add;
use crate::ops::Clamp;
macro_rules! geom_impl_line_segment {
($LineSegment:ident $Vec:ident) => {
impl<T> From<Range<$Vec<T>>> for $LineSegment<T> {
fn from(range: Range<$Vec<T>>) -> Self {
let Range { start, end } = range;
Self { start, end }
}
}
impl<T> $LineSegment<T> {
pub fn into_range(self) -> Range<$Vec<T>> {
let Self { start, end } = self;
Range { start, end }
}
pub fn projected_point(self, p: $Vec<T>) -> $Vec<T> where T: Real + Add<T, Output=T> + RelativeEq {
let len_sq = self.start.distance_squared(self.end);
if len_sq.relative_eq(&Zero::zero(), T::default_epsilon(), T::default_max_relative()) {
self.start
} else {
let t = ((p - self.start).dot(self.end - self.start) / len_sq)
.max(Zero::zero())
.min(One::one());
self.start + (self.end - self.start) * t
}
}
pub fn distance_to_point(self, p: $Vec<T>) -> T where T: Real + Add<T, Output=T> + RelativeEq {
self.projected_point(p).distance(p)
}
pub fn as_<D>(self) -> $LineSegment<D> where T: AsPrimitive<D>, D: 'static + Copy {
let Self { start, end } = self;
$LineSegment { start: start.as_(), end: end.as_() }
}
}
};
}
macro_rules! geom_impl_rect_or_rect3 {
(
$Rect:ident $Vec:ident $Extent:ident ($(($p_s:expr) $p:ident $split_at_p:ident)+) ($($e:ident)+)
$Aab:ident $into_aab:ident
contains_rect: $contains_rect:ident
contains_aab: $contains_aab:ident
collides_with_rect: $collides_with_rect:ident
collides_with_aab: $collides_with_aab:ident
collision_vector_with_rect: $collision_vector_with_rect:ident
collision_vector_with_aab: $collision_vector_with_aab:ident
) => {
impl<P,E> $Rect<P,E> {
pub fn new($($p: P,)+ $($e: E),+) -> Self {
Self { $($p,)+ $($e),+ }
}
pub fn position(self) -> $Vec<P> {
let Self { $($p,)+ .. } = self;
$Vec { $($p,)+ }
}
pub fn extent(self) -> $Extent<E> {
let Self { $($e,)+ .. } = self;
$Extent { $($e,)+ }
}
pub fn position_extent(self) -> ($Vec<P>, $Extent<E>) {
let Self { $($p,)+ $($e,)+ } = self;
($Vec { $($p,)+ }, $Extent { $($e,)+ })
}
pub fn map<DP,DE,PF,EF>(self, pf: PF, ef: EF) -> $Rect<DP,DE>
where PF: FnMut(P) -> DP, EF: FnMut(E) -> DE
{
let Self { $($p,)+ $($e,)+ } = self;
let $Vec { $($p,)+ } = $Vec { $($p,)+ }.map(pf);
let $Extent { $($e,)+ } = $Extent { $($e,)+ }.map(ef);
$Rect { $($p,)+ $($e,)+ }
}
pub fn as_<DP,DE>(self) -> $Rect<DP,DE>
where P: AsPrimitive<DP>, DP: 'static + Copy,
E: AsPrimitive<DE>, DE: 'static + Copy
{
let Self { $($p,)+ $($e,)+ } = self;
let $Vec { $($p,)+ } = $Vec { $($p,)+ }.as_();
let $Extent { $($e,)+ } = $Extent { $($e,)+ }.as_();
$Rect { $($p,)+ $($e,)+ }
}
}
impl<T> $Rect<T,T> where T: Copy + Add<T, Output=T> {
pub fn $into_aab(self) -> $Aab<T> {
self.into()
}
pub fn contains_point(self, p: $Vec<T>) -> bool where T: PartialOrd {
self.$into_aab().contains_point(p)
}
pub fn $contains_rect(self, other: Self) -> bool where T: PartialOrd {
self.$into_aab().$contains_aab(other.into())
}
pub fn $collides_with_rect(self, other: Self) -> bool where T: PartialOrd {
self.$into_aab().$collides_with_aab(other.into())
}
pub fn center(self) -> $Vec<T> where T: One + Div<T,Output=T> {
self.$into_aab().center()
}
}
impl<T> $Rect<T,T> where T: Copy + PartialOrd + Sub<T, Output=T> + Add<T, Output=T> {
pub fn expanded_to_contain_point(self, p: $Vec<T>) -> Self where T: PartialOrd {
self.$into_aab().expanded_to_contain_point(p).into()
}
pub fn expand_to_contain_point(&mut self, p: $Vec<T>) where T: PartialOrd {
*self = self.expanded_to_contain_point(p);
}
pub fn union(self, other: Self) -> Self {
self.$into_aab().union(other.into()).into()
}
pub fn intersection(self, other: Self) -> Self {
self.$into_aab().intersection(other.into()).into()
}
pub fn expand_to_contain(&mut self, other: Self) {
*self = self.union(other);
}
pub fn intersect(&mut self, other: Self) {
*self = self.intersection(other);
}
pub fn $collision_vector_with_rect(self, other: Self) -> $Vec<T>
where T: One + Div<T,Output=T>
{
self.$into_aab().$collision_vector_with_aab(other.into())
}
$(
#[doc=$p_s]
#[doc=$p_s]
pub fn $split_at_p(self, sp: T) -> [Self; 2] {
let s = self.$into_aab().$split_at_p(sp);
[s[0].into(), s[1].into()]
}
)+
}
impl<P,E> From<($Vec<P>, $Extent<E>)> for $Rect<P,E> {
fn from(t: ($Vec<P>, $Extent<E>)) -> Self {
let ($Vec { $($p,)+ }, $Extent { $($e,)+ }) = t;
Self { $($p,)+ $($e,)+ }
}
}
impl<T> From<$Aab<T>> for $Rect<T,T>
where T: Copy + Sub<T, Output=T>
{
fn from(aab: $Aab<T>) -> Self {
let $Extent { $($e,)+ } = (aab.max - aab.min).into();
Self {
$($p: aab.min.$p,)+
$($e,)+
}
}
}
impl<T> From<$Rect<T,T>> for $Aab<T>
where T: Copy + Add<T, Output=T>
{
fn from(rect: $Rect<T,T>) -> Self {
Self {
min: rect.position(),
max: rect.position() + rect.extent(),
}
}
}
};
}
macro_rules! geom_impl_aabr_or_aabb {
(
$Aab:ident $Vec:ident $Extent:ident ($(($p_s:expr) $p:ident $split_at_p:ident)+)
$Rect:ident $into_rect:ident
contains_aab: $contains_aab:ident
collides_with_aab: $collides_with_aab:ident
collision_vector_with_aab: $collision_vector_with_aab:ident
) => {
impl<T> $Aab<T> {
pub fn is_valid(&self) -> bool where T: PartialOrd {
self.min.partial_cmple(&self.max).reduce_and()
}
pub fn make_valid(&mut self) where T: PartialOrd {
$(if self.min.$p > self.max.$p { std::mem::swap(&mut self.min.$p, &mut self.max.$p); })+
}
pub fn made_valid(mut self) -> Self where T: PartialOrd {
self.make_valid();
self
}
pub fn new_empty(p: $Vec<T>) -> Self where T: Copy {
let (min, max) = (p, p);
Self { min, max }
}
pub fn $into_rect(self) -> $Rect<T,T>
where T: Copy + Sub<T, Output=T>
{
self.into()
}
pub fn center(self) -> $Vec<T>
where T: Copy + One + Add<T,Output=T> + Div<T,Output=T>
{
(self.min + self.max) / (T::one() + T::one())
}
pub fn size(self) -> $Extent<T>
where T: Copy + Sub<T, Output=T>
{
self.$into_rect().extent()
}
pub fn half_size(self) -> $Extent<T>
where T: Copy + Sub<T, Output=T> + One + Div<T,Output=T> + Add<T, Output=T>
{
self.size() / (T::one() + T::one())
}
pub fn union(self, other: Self) -> Self where T: PartialOrd {
Self {
min: $Vec::partial_min(self.min, other.min),
max: $Vec::partial_max(self.max, other.max),
}
}
pub fn intersection(self, other: Self) -> Self where T: PartialOrd {
Self {
min: $Vec::partial_max(self.min, other.min),
max: $Vec::partial_min(self.max, other.max),
}
}
pub fn expand_to_contain(&mut self, other: Self) where T: Copy + PartialOrd {
*self = self.union(other);
}
pub fn intersect(&mut self, other: Self) where T: Copy + PartialOrd {
*self = self.intersection(other);
}
pub fn expanded_to_contain_point(self, p: $Vec<T>) -> Self where T: Copy + PartialOrd {
self.union(Self::new_empty(p))
}
pub fn expand_to_contain_point(&mut self, p: $Vec<T>) where T: Copy + PartialOrd {
*self = self.expanded_to_contain_point(p);
}
pub fn contains_point(self, p: $Vec<T>) -> bool
where T: PartialOrd
{
true $(&& self.min.$p <= p.$p && p.$p <= self.max.$p)+
}
pub fn $contains_aab(self, other: Self) -> bool
where T: PartialOrd
{
true $(&& self.min.$p <= other.min.$p && other.max.$p <= self.max.$p)+
}
pub fn $collides_with_aab(self, other: Self) -> bool
where T: PartialOrd
{
true $(&& self.max.$p > other.min.$p && self.min.$p < other.max.$p)+
}
pub fn $collision_vector_with_aab(self, other: Self) -> $Vec<T>
where T: Copy + PartialOrd + Sub<T, Output=T> + One + Add<T,Output=T> + Div<T,Output=T>
{
let (b1, b2) = (self, other);
let (c1, c2) = (b1.center(), b2.center());
$Vec { $($p: if c1.$p < c2.$p {
b1.max.$p - b2.min.$p
} else {
b1.min.$p - b2.max.$p
}),+}
}
pub fn projected_point(self, p: $Vec<T>) -> $Vec<T>
where T: Clamp
{
p.clamped(self.min, self.max)
}
pub fn distance_to_point(self, p: $Vec<T>) -> T where T: Clamp + Real + Add<T, Output=T> + RelativeEq {
self.projected_point(p).distance(p)
}
$(
#[doc=$p_s]
#[doc=$p_s]
pub fn $split_at_p(self, sp: T) -> [Self; 2] where T: Copy + PartialOrd {
debug_assert!(sp >= self.min.$p);
debug_assert!(sp <= self.max.$p);
let low = Self {
min: self.min,
max: { let mut v = self.max; v.$p = sp; v },
};
let high = Self {
min: { let mut v = self.min; v.$p = sp; v },
max: self.max,
};
[low, high]
}
)+
pub fn map<D,F>(self, mut f: F) -> $Aab<D> where F: FnMut(T) -> D
{
let Self { min, max } = self;
let $Vec { $($p,)+ } = min;
let min = $Vec { $($p: f($p),)+ };
let $Vec { $($p,)+ } = max;
let max = $Vec { $($p: f($p),)+ };
$Aab { min, max }
}
pub fn as_<D>(self) -> $Aab<D> where T: AsPrimitive<D>, D: 'static + Copy {
let Self { min, max } = self;
$Aab { min: min.as_(), max: max.as_() }
}
}
};
}
macro_rules! geom_impl_disk_or_sphere {
(
$Shape:ident ($Shape_s:expr) $Vec:ident ($($p:ident)+)
$Extent:ident
$Rect:ident $rect:ident
$Aab:ident $aab:ident
collides_with_other: $collides_with_other:ident
collision_vector_with_other: $collision_vector_with_other:ident
) => {
impl<P,E> $Shape<P,E> {
#[doc=$Shape_s]
pub fn new(center: $Vec<P>, radius: E) -> Self {
Self { center, radius }
}
#[doc=$Shape_s]
pub fn unit(center: $Vec<P>) -> Self where E: One {
Self { center, radius: One::one() }
}
#[doc=$Shape_s]
pub fn point(center: $Vec<P>) -> Self where E: Zero {
Self { center, radius: Zero::zero() }
}
pub fn diameter(self) -> E where E: Copy + Add<Output=E> {
self.radius + self.radius
}
pub fn $rect(self) -> $Rect<P,E>
where P: Sub<P,Output=P> + From<E> + Copy, E: Copy + Add<E,Output=E>
{
$Rect::from((
self.center - P::from(self.radius),
$Extent::broadcast(self.diameter())
))
}
}
impl<T> $Shape<T,T> where T: Copy + Add<T,Output=T> + Sub<T,Output=T> {
pub fn $aab(self) -> $Aab<T> {
$Aab {
min: self.center - self.radius,
max: self.center + self.radius,
}
}
}
impl<T: Real + Add<T, Output=T>> $Shape<T,T> {
pub fn contains_point(self, p: $Vec<T>) -> bool where T: PartialOrd {
self.center.distance(p) <= self.radius
}
pub fn $collides_with_other(self, other: Self) -> bool where T: PartialOrd {
self.center.distance(other.center) <= (self.radius + other.radius)
}
pub fn $collision_vector_with_other(self, other: Self) -> $Vec<T> {
let v = other.center - self.center;
let mag = self.radius + other.radius - v.magnitude();
v.normalized() * mag
}
}
};
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct FrustumPlanes<T> {
pub left: T,
pub right: T,
pub bottom: T,
pub top: T,
pub near: T,
pub far: T,
}
macro_rules! geom_complete_mod {
($mod:ident) => {
use crate::vec::$mod::*;
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub struct Rect<P, E> {
pub x: P,
pub y: P,
pub w: E,
pub h: E,
}
geom_impl_rect_or_rect3!{
Rect Vec2 Extent2 (("x") x split_at_x ("y") y split_at_y) (w h)
Aabr into_aabr
contains_rect: contains_rect
contains_aab: contains_aabr
collides_with_rect: collides_with_rect
collides_with_aab: collides_with_aabr
collision_vector_with_rect: collision_vector_with_rect
collision_vector_with_aab: collision_vector_with_aabr
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub struct Aabr<T> {
pub min: Vec2<T>,
pub max: Vec2<T>,
}
impl<T> From<Aabb<T>> for Aabr<T> {
fn from(aabb: Aabb<T>) -> Self {
Self {
min: aabb.min.into(),
max: aabb.max.into(),
}
}
}
geom_impl_aabr_or_aabb!{
Aabr Vec2 Extent2 (("x") x split_at_x ("y") y split_at_y)
Rect into_rect
contains_aab: contains_aabr
collides_with_aab: collides_with_aabr
collision_vector_with_aab: collision_vector_with_aabr
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub struct Rect3<P,E> {
pub x: P,
pub y: P,
pub z: P,
pub w: E,
pub h: E,
pub d: E,
}
geom_impl_rect_or_rect3!{
Rect3 Vec3 Extent3 (("x") x split_at_x ("y") y split_at_y ("z") z split_at_z) (w h d)
Aabb into_aabb
contains_rect: contains_rect3
contains_aab: contains_aabb
collides_with_rect: collides_with_rect3
collides_with_aab: collides_with_aabb
collision_vector_with_rect: collision_vector_with_rect3
collision_vector_with_aab: collision_vector_with_aabb
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub struct Aabb<T> {
pub min: Vec3<T>,
pub max: Vec3<T>,
}
geom_impl_aabr_or_aabb!{
Aabb Vec3 Extent3 (("x") x split_at_x ("y") y split_at_y ("z") z split_at_z)
Rect3 into_rect3
contains_aab: contains_aabb
collides_with_aab: collides_with_aabb
collision_vector_with_aab: collision_vector_with_aabb
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct Disk<P,E> {
pub center: Vec2<P>,
pub radius: E,
}
impl<P,E> Disk<P,E> {
pub fn circumference(self) -> E
where E: Copy + FloatConst + Mul<Output=E> + Add<Output=E>
{
let pi = E::PI();
(pi + pi) * self.radius
}
pub fn area(self) -> E where E: Copy + FloatConst + Mul<Output=E> {
let r = self.radius;
E::PI()*r*r
}
}
geom_impl_disk_or_sphere!{
Disk ("Disk") Vec2 (x y)
Extent2 Rect rect Aabr aabr
collides_with_other: collides_with_disk
collision_vector_with_other: collision_vector_with_disk
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct Sphere<P,E> {
pub center: Vec3<P>,
pub radius: E,
}
impl<P,E> Sphere<P,E> {
pub fn surface_area(self) -> E where E: Copy + One + FloatConst + Add<Output=E> + Mul<Output=E> {
let four = E::one() + E::one() + E::one() + E::one();
let r = self.radius;
four*E::PI()*r*r
}
pub fn volume(self) -> E where E: Copy + One + FloatConst + Add<Output=E> + Mul<Output=E> + Div<Output=E> {
let four = E::one() + E::one() + E::one() + E::one();
let three = E::one() + E::one() + E::one();
let r = self.radius;
(four*E::PI()*r*r*r)/three
}
}
geom_impl_disk_or_sphere!{
Sphere ("Sphere") Vec3 (x y z)
Extent3 Rect3 rect3 Aabb aabb
collides_with_other: collides_with_sphere
collision_vector_with_other: collision_vector_with_sphere
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct Ellipsis<P,E> {
pub center: Vec2<P>,
pub radius: Extent2<E>,
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct Potato<P,E> {
pub center: Vec3<P>,
pub radius: Extent3<E>,
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct LineSegment2<T> {
pub start: Vec2<T>,
pub end: Vec2<T>,
}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
#[allow(missing_docs)]
pub struct LineSegment3<T> {
pub start: Vec3<T>,
pub end: Vec3<T>,
}
geom_impl_line_segment!{LineSegment2 Vec2}
geom_impl_line_segment!{LineSegment3 Vec3}
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub struct Ray<T> {
pub origin: Vec3<T>,
pub direction: Vec3<T>,
}
impl<T: Real + Add<T, Output=T>> Ray<T> {
pub fn new(origin: Vec3<T>, direction: Vec3<T>) -> Self {
Self { origin, direction }
}
pub fn triangle_intersection(&self, tri: [Vec3<T>; 3]) -> Option<T> {
let (v0, v1, v2) = (tri[0], tri[1], tri[2]);
let edge1 = v1 - v0;
let edge2 = v2 - v0;
let h = self.direction.cross(edge2);
let a = edge1.dot(h);
if a > -T::epsilon() && a < T::epsilon() {
return None;
}
let f = a.recip();
let s = self.origin - v0;
let u = f * s.dot(h);
if u < T::zero() || u > T::one() {
return None;
}
let q = s.cross(edge1);
let v = f * self.direction.dot(q);
if v < T::zero() || u + v > T::one() {
return None;
}
Some(f * edge2.dot(q))
}
}
}
}
#[cfg(all(nightly, feature="repr_simd"))]
pub mod repr_simd {
use super::*;
geom_complete_mod!(repr_simd);
}
pub mod repr_c {
use super::*;
geom_complete_mod!(repr_c);
}
pub use self::repr_c::*;
#[cfg(test)]
mod tests {
use super::*;
use crate::vec::{Vec2, Vec3};
#[test] fn rect_center() {
let min = Vec2::new(-1_f32, -1.);
let max = -min;
let aabr = Aabr { min, max };
assert_relative_eq!(aabr.center(), Vec2::zero());
let rect = aabr.into_rect();
assert_relative_eq!(rect.center(), Vec2::zero());
}
#[test] fn rect3_center() {
let min = Vec3::new(-1_f32, -1., -1.);
let max = -min;
let aabb = Aabb { min, max };
assert_relative_eq!(aabb.center(), Vec3::zero());
let rect3 = aabb.into_rect3();
assert_relative_eq!(rect3.center(), Vec3::zero());
}
#[test] fn projected_point() {
let segment = LineSegment2 { start: Vec2::new(-5_f32, 5.), end: Vec2::new(5., -5.0) };
assert_relative_eq!(segment.start, segment.projected_point(Vec2::new(-4., 7.)));
assert_relative_eq!(segment.end, segment.projected_point(Vec2::new(7., -4.)));
assert_relative_eq!(Vec2::zero(), segment.projected_point(Vec2::new(1., 1.)));
let segment = LineSegment3 { start: Vec3::new(-5_f32, -0., 5.), end: Vec3::new(5., 0., -5.0) };
assert_relative_eq!(Vec3::zero(), segment.projected_point(Vec3::new(-0., -1., -0.)));
}
#[test] fn distance_to_point() {
let segment = LineSegment2 { start: Vec2::new(-5_f32, 5.), end: Vec2::new(5., -5.0) };
assert_relative_eq!(2.0f32.sqrt(), segment.distance_to_point(Vec2::new(-1., -1.)));
let segment = LineSegment3 { start: Vec3::new(-5_f32, 0., 5.), end: Vec3::new(5., 0., -5.) };
assert_relative_eq!(2.0f32.sqrt(), segment.distance_to_point(Vec3::new(-1., 0., -1.)));
}
}