PeriDyno 1.0.0
Loading...
Searching...
No Matches
Primitive2D.inl
Go to the documentation of this file.
1//#include "Primitive3D.h"
2#include "Math/SimpleMath.h"
3#include "Interval.h"
4#include <glm/glm.hpp>
5
6namespace dyno
7{
8 template<typename Real>
10 {
11 origin = Coord2D(0);
12 }
13
14 template<typename Real>
15 DYN_FUNC TPoint2D<Real>::TPoint2D(const Real& val)
16 {
17 origin = Coord2D(val);
18 }
19
20 template<typename Real>
21 DYN_FUNC TPoint2D<Real>::TPoint2D(const Real& c0, const Real& c1)
22 {
23 origin = Coord2D(c0, c1);
24 }
25
26 template<typename Real>
27 DYN_FUNC TPoint2D<Real>::TPoint2D(const Coord2D& pos)
28 {
29 origin = pos;
30 }
31
32 template<typename Real>
34 {
35 origin = pt.origin;
36 }
37
38 template<typename Real>
40 {
41 origin = p;
42 return *this;
43 }
44
45 template<typename Real>
47 {
48 Coord2D u = origin - line.origin;
49 Real tNum = u.dot(line.direction);
50 Real a = line.direction.normSquared();
51 Real t = a < REAL_EPSILON_SQUARED ? 0 : tNum / a;
52
53 return TPoint2D(line.origin + t * line.direction);
54 }
55
56 template<typename Real>
58 {
59 Coord2D u = origin - ray.origin;
60
61 Real tNum = u.dot(ray.direction);
62 Real a = ray.direction.normSquared();
63 Real t = a < REAL_EPSILON_SQUARED ? 0 : tNum / a;
64
65 t = t < 0 ? 0 : t;
66
67 return TPoint2D<Real>(ray.origin + t * ray.direction);
68 }
69
70 template<typename Real>
72 {
73 Coord2D l = origin - segment.v0;
74 Coord2D dir = segment.v1 - segment.v0;
75 if (dir.normSquared() < REAL_EPSILON_SQUARED)
76 {
77 return TPoint2D<Real>(segment.v0);
78 }
79
80 Real t = l.dot(dir) / dir.normSquared();
81
82 Coord2D q = segment.v0 + t * dir;
83 q = t < 0 ? segment.v0 : q;
84 q = t > 1 ? segment.v1 : q;
85 //printf("T: %.3lf\n", t);
86 return TPoint2D<Real>(q);
87 }
88
89 template<typename Real>
91 {
92 Coord2D cp = origin - circle.center;
93 Coord2D q = circle.center + circle.radius * cp.normalize();
94
95 return TPoint2D<Real>(q);
96 }
97
98 template<typename Real>
100 {
101 return (origin - pt.origin).norm();
102 }
103
104 template<typename Real>
105 DYN_FUNC Real TPoint2D<Real>::distance(const TLine2D<Real>& line) const
106 {
107 return (origin - project(line).origin).norm();
108 }
109
110 template<typename Real>
111 DYN_FUNC Real TPoint2D<Real>::distance(const TRay2D<Real>& ray) const
112 {
113 return (origin - project(ray).origin).norm();
114 }
115
116 template<typename Real>
117 DYN_FUNC Real TPoint2D<Real>::distance(const TSegment2D<Real>& segment) const
118 {
119 return (origin - project(segment).origin).norm();
120 }
121
122 template<typename Real>
123 DYN_FUNC Real TPoint2D<Real>::distance(const TCircle2D<Real>& circle) const
124 {
125 return (origin - circle.center).norm() - circle.radius;
126 }
127
128 template<typename Real>
129 DYN_FUNC Real TPoint2D<Real>::distanceSquared(const TPoint2D& pt) const
130 {
131 return (origin - pt.origin).normSquared();
132 }
133
134 template<typename Real>
136 {
137 return (origin - project(line).origin).normSquared();
138 }
139
140 template<typename Real>
142 {
143 return (origin - project(ray).origin).normSquared();
144 }
145
146 template<typename Real>
148 {
149 return (origin - project(segment).origin).normSquared();
150 }
151
152 template<typename Real>
154 {
155 return (origin - project(circle).origin).normSquared();
156 }
157
158 template<typename Real>
159 DYN_FUNC bool TPoint2D<Real>::inside(const TLine2D<Real>& line) const
160 {
161 if (!line.isValid())
162 {
163 return false;
164 }
165
166 return (origin - line.origin).cross(line.direction) < REAL_EPSILON_SQUARED;
167 }
168
169 template<typename Real>
170 DYN_FUNC bool TPoint2D<Real>::inside(const TRay2D<Real>& ray) const
171 {
172 if (!inside(TLine2D<Real>(ray.origin, ray.direction)))
173 {
174 return false;
175 }
176
177 Coord2D offset = origin - ray.origin;
178 Real t = offset.dot(ray.direction);
179
180 return t > Real(0);
181 }
182
183 template<typename Real>
184 DYN_FUNC bool TPoint2D<Real>::inside(const TSegment2D<Real>& segment) const
185 {
186 Coord2D dir = segment.direction();
187 if (!inside(TLine2D<Real>(segment.startPoint(), dir)))
188 {
189 return false;
190 }
191
192 Coord2D offset = origin - segment.startPoint();
193 Real t = offset.dot(dir) / dir.normSquared();
194
195 return t > Real(0) && t < Real(1);
196 }
197
198 template<typename Real>
199 DYN_FUNC bool TPoint2D<Real>::inside(const TCircle2D<Real>& circle) const
200 {
201 return (origin - circle.center).normSquared() < circle.radius * circle.radius;
202 }
203
204 template<typename Real>
206 {
207 return TSegment2D<Real>(pt.origin, origin);
208 }
209
210 template<typename Real>
212 {
213 origin = Coord2D(0);
214 direction = Coord2D(1, 0, 0);
215 }
216
217 template<typename Real>
218 DYN_FUNC TLine2D<Real>::TLine2D(const Coord2D& pos, const Coord2D& dir)
219 {
220 origin = pos;
221 direction = dir;
222 }
223
224 template<typename Real>
226 {
227 origin = line.origin;
228 direction = line.direction;
229 }
230
231 template<typename Real>
233 {
234 Coord2D u = origin - line.origin;
235 Real a = direction.normSquared();
236 Real b = direction.dot(line.direction);
237 Real c = line.direction.normSquared();
238 Real d = u.dot(direction);
239 Real e = u.dot(line.direction);
240 Real f = u.normSquared();
241 Real det = a * c - b * b;
242
243 if (det < REAL_EPSILON)
244 {
246 return c < REAL_EPSILON ? p - p.project(*this) : TSegment2D<Real>(origin, line.origin + e / c * line.direction);
247 }
248 else
249 {
250 Real invDet = 1 / det;
251 Real s = (b * e - c * d) * invDet;
252 Real t = (a * e - b * d) * invDet;
253 return TSegment2D<Real>(origin + s * direction, line.origin + t * line.direction);
254 }
255 }
256
257 template<typename Real>
259 {
260 Coord2D u = origin - ray.origin;
261 Real a = direction.normSquared();
262 Real b = direction.dot(ray.direction);
263 Real c = ray.direction.normSquared();
264 Real d = u.dot(direction);
265 Real e = u.dot(ray.direction);
266 Real det = a * c - b * b;
267
268 if (det < REAL_EPSILON)
269 {
271 TPoint2D<Real> p1(ray.origin);
272
273 return a < REAL_EPSILON ? p0.project(*this) - p0 : p1 - p1.project(*this);
274 }
275
276 Real sNum = b * e - c * d;
277 Real tNum = a * e - b * d;
278
279 Real sDenom = det;
280 Real tDenom = det;
281
282 if (tNum < 0) {
283 tNum = 0;
284 sNum = -d;
285 sDenom = a;
286 }
287 // Parameters of nearest points on restricted domain
288 Real s = sNum / sDenom;
289 Real t = tNum / tDenom;
290
291 return TSegment2D<Real>(origin + (s * direction), ray.origin + (t * ray.direction));
292 }
293
294 template<typename Real>
296 {
297 Coord2D u = origin - segment.startPoint();
298 Coord2D dir1 = segment.endPoint() - segment.startPoint();
299 Real a = direction.normSquared();
300 Real b = direction.dot(dir1);
301 Real c = dir1.dot(dir1);
302 Real d = u.dot(direction);
303 Real e = u.dot(dir1);
304 Real det = a * c - b * b;
305
306 if (det < REAL_EPSILON)
307 {
309 TPoint2D<Real> p1(segment.startPoint());
310
311 return a < REAL_EPSILON ? p0.project(*this) - p0 : p1 - p1.project(*this);
312 }
313
314 Real sNum = b * e - c * d;
315 Real tNum = a * e - b * d;
316
317 Real sDenom = det;
318 Real tDenom = det;
319
320 // Check t
321 if (tNum < 0) {
322 tNum = 0;
323 sNum = -d;
324 sDenom = a;
325 }
326 else if (tNum > tDenom) {
327 tNum = tDenom;
328 sNum = -d + b;
329 sDenom = a;
330 }
331 // Parameters of nearest points on restricted domain
332 Real s = sNum / sDenom;
333 Real t = tNum / tDenom;
334
335 return TSegment2D<Real>(origin + (s * direction), segment.startPoint() + (t * dir1));
336 }
337
338 template<typename Real>
340 {
341 Coord2D offset = circle.center - origin;
342 Real d2 = direction.normSquared();
343 if (d2 < REAL_EPSILON)
344 {
346 }
347
348 Coord2D p = origin + offset.dot(direction) / d2 * direction;
349
350 return TPoint2D<Real>(p).project(circle) - TPoint2D<Real>(p);
351 }
352
353 template<typename Real>
355 {
356 return pt.distance(*this);
357 }
358
359 template<typename Real>
360 DYN_FUNC Real TLine2D<Real>::distance(const TLine2D<Real>& line) const
361 {
362 return proximity(line).length();
363 }
364
365 template<typename Real>
366 DYN_FUNC Real TLine2D<Real>::distance(const TRay2D<Real>& ray) const
367 {
368 return proximity(ray).length();
369 }
370
371 template<typename Real>
372 DYN_FUNC Real TLine2D<Real>::distance(const TSegment2D<Real>& segment) const
373 {
374 return proximity(segment).length();
375 }
376
377 template<typename Real>
379 {
380 return pt.distanceSquared(*this);
381 }
382
383 template<typename Real>
385 {
386 return proximity(line).lengthSquared();
387 }
388
389 template<typename Real>
391 {
392 return proximity(ray).lengthSquared();
393 }
394
395 template<typename Real>
397 {
398 return proximity(segment).lengthSquared();
399 }
400
401 template<typename Real>
402 DYN_FUNC int TLine2D<Real>::intersect(const TCircle2D<Real>& circle, TSegment2D<Real>& interSeg) const
403 {
404 Coord2D diff = origin - circle.center;
405 Real a0 = diff.dot(diff) - circle.radius * circle.radius;
406 Real a1 = direction.dot(diff);
407
408 // Intersection occurs when Q(t) has real roots.
409 Real discr = a1 * a1 - a0;
410 if (discr > (Real)0)
411 {
412 Real root = glm::sqrt(discr);
413 interSeg.startPoint() = origin + (-a1 - root) * direction;
414 interSeg.endPoint() = origin + (-a1 + root) * direction;
415 return 2;
416 }
417 else if (discr < (Real)0)
418 {
419 return 0;
420 }
421 else
422 {
423 interSeg.startPoint() = origin - a1 * direction;
424 return 1;
425 }
426 }
427
428 template<typename Real>
429 DYN_FUNC Real TLine2D<Real>::parameter(const Coord2D& pos) const
430 {
431 Coord2D l = pos - origin;
432 Real d2 = direction.normSquared();
433
434 return d2 < REAL_EPSILON_SQUARED ? Real(0) : l.dot(direction) / d2;
435 }
436
437 template<typename Real>
438 DYN_FUNC bool TLine2D<Real>::isValid() const
439 {
440 return direction.normSquared() > REAL_EPSILON_SQUARED;
441 }
442
443 template<typename Real>
445 {
446 origin = Coord2D(0);
447 direction = Coord2D(1, 0);
448 }
449
450 template<typename Real>
451 DYN_FUNC TRay2D<Real>::TRay2D(const Coord2D& pos, const Coord2D& dir)
452 {
453 origin = pos;
454 direction = dir;
455 }
456
457 template<typename Real>
459 {
460 origin = ray.origin;
461 direction = ray.direction;
462 }
463
464 template<typename Real>
466 {
467 Coord2D u = origin - ray.origin;
468 Real a = direction.normSquared();
469 Real b = direction.dot(ray.direction);
470 Real c = ray.direction.normSquared();
471 Real d = u.dot(direction);
472 Real e = u.dot(ray.direction);
473 Real det = a * c - b * b;
474
475 if (det < REAL_EPSILON)
476 {
478 TPoint2D<Real> p1(ray.origin);
479
480 TPoint2D<Real> q0 = p0.project(ray);
481 TPoint2D<Real> q1 = p1.project(*this);
482
483 return (q0 - p0).lengthSquared() < (q1 - p1).lengthSquared() ? q0 - p0 : p1 - q1;
484 }
485
486 Real sNum = b * e - c * d;
487 Real tNum = a * e - b * d;
488
489 Real sDenom = det;
490 Real tDenom = det;
491
492 if (sNum < 0) {
493 sNum = 0;
494 tNum = e;
495 tDenom = c;
496 }
497 // Check t
498 if (tNum < 0) {
499 tNum = 0;
500 if (-d < 0) {
501 sNum = 0;
502 }
503 else {
504 sNum = -d;
505 sDenom = a;
506 }
507 }
508 // Parameters of nearest points on restricted domain
509 Real s = sNum / sDenom;
510 Real t = tNum / tDenom;
511
512 return TSegment2D<Real>(origin + (s * direction), ray.origin + (t * direction));
513 }
514
515 template<typename Real>
517 {
518 Coord2D u = origin - segment.startPoint();
519 Real a = direction.normSquared();
520 Real b = direction.dot(segment.direction());
521 Real c = segment.lengthSquared();
522 Real d = u.dot(direction);
523 Real e = u.dot(segment.direction());
524 Real det = a * c - b * b;
525
526 if (det < REAL_EPSILON)
527 {
528 if (a < REAL_EPSILON_SQUARED)
529 {
531 return p0.project(segment) - p0;
532 }
533 else
534 {
535 TPoint2D<Real> p1(segment.startPoint());
536 TPoint2D<Real> p2(segment.endPoint());
537
538 TPoint2D<Real> q1 = p1.project(*this);
539 TPoint2D<Real> q2 = p2.project(*this);
540
541 return (p1 - q1).lengthSquared() < (p2 - q2).lengthSquared() ? (p1 - q1) : (p2 - q2);
542 }
543 }
544
545 Real sNum = b * e - c * d;
546 Real tNum = a * e - b * d;
547
548 Real sDenom = det;
549 Real tDenom = det;
550
551 if (sNum < 0) {
552 sNum = 0;
553 tNum = e;
554 tDenom = c;
555 }
556
557 // Check t
558 if (tNum < 0) {
559 tNum = 0;
560 if (-d < 0) {
561 sNum = 0;
562 }
563 else {
564 sNum = -d;
565 sDenom = a;
566 }
567 }
568 else if (tNum > tDenom) {
569 tNum = tDenom;
570 if ((-d + b) < 0) {
571 sNum = 0;
572 }
573 else {
574 sNum = -d + b;
575 sDenom = a;
576 }
577 }
578
579 Real s = sNum / sDenom;
580 Real t = tNum / tDenom;
581
582 return TSegment2D<Real>(origin + (s * direction), segment.startPoint() + (t * segment.direction()));
583 }
584
585 template<typename Real>
587 {
588 return pt.distance(*this);
589 }
590
591 template<typename Real>
592 DYN_FUNC Real TRay2D<Real>::distance(const TSegment2D<Real>& segment) const
593 {
594 return proximity(segment).length();
595 }
596
597 template<typename Real>
599 {
600 return pt.distanceSquared(*this);
601 }
602
603 template<typename Real>
605 {
606 return proximity(segment).lengthSquared();
607 }
608
609 template<typename Real>
610 DYN_FUNC int TRay2D<Real>::intersect(const TCircle2D<Real>& sphere, TSegment2D<Real>& interSeg) const
611 {
612 Coord2D diff = origin - sphere.center;
613 Real a0 = diff.dot(diff) - sphere.radius * sphere.radius;
614 Real a1 = direction.dot(diff);
615
616 // Intersection occurs when Q(t) has real roots.
617 Real discr = a1 * a1 - a0;
618 if (discr > (Real)0)
619 {
620 Real root = glm::sqrt(discr);
621
622 if (-a1 + root < Real(0))
623 {
624 return 0;
625 }
626 else if (-a1 + root < Real(0))
627 {
628 interSeg.startPoint() = origin + (-a1 + root) * direction;
629 return 1;
630 }
631 else
632 {
633 interSeg.startPoint() = origin + (-a1 - root) * direction;
634 interSeg.endPoint() = origin + (-a1 + root) * direction;
635 return 2;
636 }
637 }
638 else if (discr < Real(0))
639 {
640 return 0;
641 }
642 else
643 {
644 if (a1 > Real(0))
645 {
646 return 0;
647 }
648 interSeg.startPoint() = origin - a1 * direction;
649 return 1;
650 }
651 }
652
653 template<typename Real>
654 DYN_FUNC Real TRay2D<Real>::parameter(const Coord2D& pos) const
655 {
656 Coord2D l = pos - origin;
657 Real d2 = direction.normSquared();
658
659 return d2 < REAL_EPSILON_SQUARED ? Real(0) : l.dot(direction) / d2;
660 }
661
662 template<typename Real>
663 DYN_FUNC bool TRay2D<Real>::isValid() const
664 {
665 return direction.normSquared() > REAL_EPSILON_SQUARED;
666 }
667
668 template<typename Real>
670 {
671 v0 = Coord2D(0, 0);
672 v1 = Coord2D(1, 0);
673 }
674
675 template<typename Real>
676 DYN_FUNC TSegment2D<Real>::TSegment2D(const Coord2D& p0, const Coord2D& p1)
677 {
678 v0 = p0;
679 v1 = p1;
680 }
681
682 template<typename Real>
684 {
685 v0 = segment.v0;
686 v1 = segment.v1;
687 }
688
689 template<typename Real>
691 {
692 Coord2D u = v0 - segment.v0;
693 Coord2D dir0 = v1 - v0;
694 Coord2D dir1 = segment.v1 - segment.v0;
695 Real a = dir0.normSquared();
696 Real b = dir0.dot(dir1);
697 Real c = dir1.normSquared();
698 Real d = u.dot(dir0);
699 Real e = u.dot(dir1);
700 Real det = a * c - b * b;
701
702 // Check for (near) parallelism
703 if (det < REAL_EPSILON) {
704 // Arbitrary choice
705 Real l0 = lengthSquared();
706 Real l1 = segment.lengthSquared();
707 TPoint2D<Real> p0 = l0 < l1 ? TPoint2D<Real>(v0) : TPoint2D<Real>(segment.v0);
708 TPoint2D<Real> p1 = l0 < l1 ? TPoint2D<Real>(v1) : TPoint2D<Real>(segment.v1);
709 TSegment2D<Real> longerSeg = l0 < l1 ? segment : *this;
710 bool bOpposite = l0 < l1 ? false : true;
711 TPoint2D<Real> q0 = p0.project(longerSeg);
712 TPoint2D<Real> q1 = p1.project(longerSeg);
713 TSegment2D<Real> ret = p0.distance(q0) < p1.distance(q1) ? (q0 - p0) : (q1 - p1);
714 return bOpposite ? -ret : ret;
715 }
716
717 // Find parameter values of closest points
718 // on each segment��s infinite line. Denominator
719 // assumed at this point to be ����det����,
720 // which is always positive. We can check
721 // value of numerators to see if we��re outside
722 // the [0, 1] x [0, 1] domain.
723 Real sNum = b * e - c * d;
724 Real tNum = a * e - b * d;
725
726 Real sDenom = det;
727 Real tDenom = det;
728
729 if (sNum < 0) {
730 sNum = 0;
731 tNum = e;
732 tDenom = c;
733 }
734 else if (sNum > det) {
735 sNum = det;
736 tNum = e + b;
737 tDenom = c;
738 }
739
740 // Check t
741 if (tNum < 0) {
742 tNum = 0;
743 if (-d < 0) {
744 sNum = 0;
745 }
746 else if (-d > a) {
747 sNum = sDenom;
748 }
749 else {
750 sNum = -d;
751 sDenom = a;
752 }
753 }
754 else if (tNum > tDenom) {
755 tNum = tDenom;
756 if ((-d + b) < 0) {
757 sNum = 0;
758 }
759 else if ((-d + b) > a) {
760 sNum = sDenom;
761 }
762 else {
763 sNum = -d + b;
764 sDenom = a;
765 }
766 }
767
768 Real s = sNum / sDenom;
769 Real t = tNum / tDenom;
770
771 return TSegment2D<Real>(v0 + (s * dir0), segment.v0 + (t * dir1));
772 }
773
774 template<typename Real>
775 DYN_FUNC Real TSegment2D<Real>::distance(const TSegment2D<Real>& segment) const
776 {
777 return proximity(segment).length();
778 }
779
780 template<typename Real>
782 {
783 return proximity(segment).lengthSquared();
784 }
785
786 template<typename Real>
788 {
789 return (v1 - v0).norm();
790 }
791
792 template<typename Real>
794 {
795 return (v1 - v0).normSquared();
796 }
797
798 template<typename Real>
799 DYN_FUNC int TSegment2D<Real>::intersect(const TCircle2D<Real>& circle, TSegment2D<Real>& interSeg) const
800 {
801 Coord2D diff = v0 - circle.center;
802 Coord2D dir = direction();
803 Real a0 = diff.dot(diff) - circle.radius * circle.radius;
804 Real a1 = dir.dot(diff);
805
806 // Intersection occurs when Q(t) has real roots.
807 Real discr = a1 * a1 - a0;
808 if (discr > (Real)0)
809 {
810 Real root = glm::sqrt(discr);
811 Real t1 = maximum(-a1 - root, Real(0));
812 Real t2 = minimum(-a1 + root, Real(1));
813 if (t1 < t2)
814 {
815 interSeg.startPoint() = v0 + t1 * dir;
816 interSeg.endPoint() = v0 + t2 * dir;
817 return 2;
818 }
819 else if (t1 > t2)
820 {
821 return 0;
822 }
823 else
824 {
825 interSeg.startPoint() = v0 + t1 * dir;
826 return 1;
827 }
828 }
829 else if (discr < (Real)0)
830 {
831 return 0;
832 }
833 else
834 {
835 Real t = -a1;
836 if (t >= Real(0) && t <= Real(1))
837 {
838 interSeg.startPoint() = v0 - a1 * dir;
839 return 1;
840 }
841 return 0;
842 }
843 }
844
845 template<typename Real>
846 DYN_FUNC Real TSegment2D<Real>::parameter(const Coord2D& pos) const
847 {
848 Coord2D l = pos - v0;
849 Coord2D dir = direction();
850 Real d2 = dir.normSquared();
851
852 return d2 < REAL_EPSILON_SQUARED ? Real(0) : l.dot(dir) / d2;
853 }
854
855 template<typename Real>
856 DYN_FUNC bool TSegment2D<Real>::isValid() const
857 {
859 }
860
861 template<typename Real>
863 {
865 seg.v0 = v1;
866 seg.v1 = v0;
867
868 return seg;
869 }
870
871 template<typename Real>
873 {
874 center = Coord2D(0);
875 radius = Real(1);
876 theta = Real(0);
877 }
878
879 template<typename Real>
880 DYN_FUNC TCircle2D<Real>::TCircle2D(const Coord2D& c, const Real& r)
881 {
882 center = c;
883 radius = r;
884 theta = Real(0);
885 }
886
887 template<typename Real>
889 {
890 center = circle.center;
891 radius = circle.radius;
892 theta = circle.theta;
893 }
894
895 template<typename Real>
897 {
898 v0 = Coord2D(0);
899 v1 = Coord2D(1);
900 }
901
902 template<typename Real>
904 {
905 v0 = p0;
906 v1 = p1;
907 }
908
909 template<typename Real>
911 {
912 v0 = box.v0;
913 v1 = box.v1;
914 }
915
916 template<typename Real>
921
922 template<typename Real>
927
928
929 template<typename Real>
931 {
932 size = 4;
933
934 _vertices[0] = Coord2D(-hx, -hy);
935 _vertices[1] = Coord2D(hx, -hy);
936 _vertices[2] = Coord2D(hx, hy);
937 _vertices[3] = Coord2D(-hx, hy);
938
939 _normals[0] = Coord2D(Real(0), -Real(1));
940 _normals[1] = Coord2D(Real(1), Real(0));
941 _normals[2] = Coord2D(Real(0), Real(1));
942 _normals[3] = Coord2D(-Real(1), Real(0));
943
944 _center = Coord2D(0);
945 }
946
947 template<typename Real>
948 void TPolygon2D<Real>::setAsPentagon(const Coord2D& v0, const Coord2D& v1, const Coord2D& v2, const Coord2D& v3, const Coord2D& v4)
949 {
950 size = 5;
951
952 _vertices[0] = v0;
953 _vertices[1] = v1;
954 _vertices[2] = v2;
955 _vertices[3] = v3;
956 _vertices[4] = v4;
957
958 _center = Coord2D(0);
959 }
960
961 template<typename Real>
962 void TPolygon2D<Real>::setAsTriangle(const Coord2D& v0, const Coord2D& v1, const Coord2D& v2)
963 {
964 size = 3;
965
966 _vertices[0] = v0;
967 _vertices[1] = v1;
968 _vertices[2] = v2;
969
970 _center = Coord2D(0);
971 }
972
973 template<typename Real>
975 {
976 size = 2;
977
978 _vertices[0] = v0;
979 _vertices[1] = v1;
980
981 _center = Coord2D(0);
982 }
983
984 template<typename Real>
986 {
987 Coord2D v0(REAL_MAX);
988 Coord2D v1(-REAL_MAX);
989 for (uint i = 0; i < size; i++)
990 {
991 Coord2D v = _vertices[i] + _center;
992 v0 = v0.minimum(v);
993 v1 = v1.maximum(v);
994 }
995
996 return TAlignedBox2D<Real>(v0 - _radius, v1 + _radius);
997 }
998}
double Real
Definition Typedef.inl:23
Vector< Real, 2 > Coord2D
DYN_FUNC TCircle2D()
Vector< Real, 2 > Coord2D
1D geometric primitives in two-dimensional space
DYN_FUNC bool isValid() const
DYN_FUNC Real distance(const TPoint2D< Real > &pt) const
DYN_FUNC Real distanceSquared(const TPoint2D< Real > &pt) const
Vector< Real, 2 > Coord2D
Coord2D direction
DYN_FUNC int intersect(const TCircle2D< Real > &circle, TSegment2D< Real > &interSeg) const
DYN_FUNC TLine2D()
DYN_FUNC TSegment2D< Real > proximity(const TLine2D< Real > &line) const
DYN_FUNC Real parameter(const Coord2D &pos) const
0D geometric primitive in two-dimensional space
Definition Primitive2D.h:51
DYN_FUNC TPoint2D()
DYN_FUNC TPoint2D< Real > project(const TLine2D< Real > &line) const
project a point onto linear components – lines, rays and segments
DYN_FUNC bool inside(const TLine2D< Real > &line) const
check whether a point strictly lies inside (excluding boundary) a 1D geometric primitive
DYN_FUNC Real distance(const TPoint2D< Real > &pt) const
DYN_FUNC TPoint2D & operator=(const Coord2D &p)
Vector< Real, 2 > Coord2D
Definition Primitive2D.h:53
DYN_FUNC Real distanceSquared(const TPoint2D< Real > &pt) const
DYN_FUNC const TSegment2D< Real > operator-(const TPoint2D< Real > &pt) const
Coord2D _normals[MAX_POLYGON_VERTEX_NUM]
TAlignedBox2D< Real > aabb()
void setAsLine(const Coord2D &v0, const Coord2D &v1)
Vector< Real, 2 > Coord2D
void setAsTriangle(const Coord2D &v0, const Coord2D &v1, const Coord2D &v2)
void setAsPentagon(const Coord2D &v0, const Coord2D &v1, const Coord2D &v2, const Coord2D &v3, const Coord2D &v4)
void setAsBox(Real hx, Real hy)
Coord2D _vertices[MAX_POLYGON_VERTEX_NUM]
DYN_FUNC TSegment2D< Real > proximity(const TRay2D< Real > &ray) const
Coord2D origin
DYN_FUNC Real parameter(const Coord2D &pos) const
Vector< Real, 2 > Coord2D
DYN_FUNC Real distance(const TPoint2D< Real > &pt) const
DYN_FUNC bool isValid() const
DYN_FUNC TRay2D()
DYN_FUNC int intersect(const TCircle2D< Real > &sphere, TSegment2D< Real > &interSeg) const
Coord2D direction
DYN_FUNC Real distanceSquared(const TPoint2D< Real > &pt) const
DYN_FUNC Coord2D & startPoint()
DYN_FUNC Real parameter(const Coord2D &pos) const
DYN_FUNC Coord2D direction() const
DYN_FUNC Real length() const
DYN_FUNC bool isValid() const
DYN_FUNC TSegment2D< Real > proximity(const TSegment2D< Real > &segment) const
DYN_FUNC int intersect(const TCircle2D< Real > &circle, TSegment2D< Real > &interSeg) const
DYN_FUNC TSegment2D()
DYN_FUNC Coord2D & endPoint()
DYN_FUNC Real lengthSquared() const
DYN_FUNC Real distanceSquared(const TSegment2D< Real > &segment) const
DYN_FUNC TSegment2D< Real > operator-(void) const
DYN_FUNC Real distance(const TSegment2D< Real > &segment) const
Vector< Real, 2 > Coord2D
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
constexpr Real REAL_EPSILON_SQUARED
Definition Typedef.inl:44
constexpr Real REAL_MAX
Definition Typedef.inl:45
DYN_FUNC T minimum(const T &v0, const T &v1)
Definition SimpleMath.h:120
constexpr Real REAL_EPSILON
Definition Typedef.inl:43
unsigned int uint
Definition VkProgram.h:14
DYN_FUNC T maximum(const T &v0, const T &v1)
Definition SimpleMath.h:160