5 #define REAL_infinity 1.0e30
6 #define REAL_EQUAL(a,b) (((a < b + EPSILON) && (a > b - EPSILON)) ? true : false)
7 #define REAL_GREAT(a,b) ((a > EPSILON + b)? true: false)
8 #define REAL_LESS(a,b) ((a + EPSILON < b)? true: false)
15 T d0 = (p - v0).norm();
16 T d1 = (p - v1).norm();
18 if ((v1 - v0).norm() < 1e-7)
return dv;
21 if ((p-v0).
dot(dir) * (p-v1).
dot(dir) <= 0.0) {
34 auto n = (v1 - v0).
cross(v2 - v0);
39 auto d0 = (p - v0).
cross(v1 - v0);
40 auto d1 = (p - v1).
cross(v2 - v1);
41 auto d2 = (p - v2).
cross(v0 - v2);
65 if (
inTri(p_inPlane, y0, y1, y2)) {
66 return p_ortho.norm();
91 T det =
max(a00 * a11 - a01 * a01, 0.0);
92 T s = a01 * b1 - a11 * b0;
93 T t = a01 * b0 - a00 * b1;
154 T tmp0 = 0.0;
T tmp1 = 0.0;
T numer = 0.0;
T denom = 0.0;
161 denom = a00 - 2.0 * a01 + a11;
191 denom = a00 - 2.0 * a01 + a11;
217 numer = a11 + b1 - a01 - b0;
223 denom = a00 - 2.0 * a01 + a11;
237 if (para !=
nullptr) {
261 T a = P1mP0.dot(P1mP0);
262 T b = P1mP0.dot(Q1mQ0);
263 T c = Q1mQ0.dot(Q1mQ0);
264 T d = P1mP0.dot(P0mQ0);
265 T e = Q1mQ0.dot(P0mQ0);
266 T det = a * c - b * b;
267 T s, t, nd, bmd, bte, ctd, bpe, ate, btd;
451 T d = signedD.normSquared();
461 T d = signedD.normSquared();
481 x[0] = x0, x[1] = x1; x[2] = x2; x[3] = x3;
484 for (
int i = 0; i < 4; ++i) {
487 T lp =
max(
max(p[0].norm(), p[1].norm()), p[2].norm()) + p[3].norm();
488 if (lp == 0.0)
return false;
492 T g = this->
s * (dsqr - glm::pow(this->
xi * invL, 2)) / (glm::sqrt(dsqr) + this->
xi * invL);
494 T tL = (1 - this->
s) * (dsqr - glm::pow(this->
xi * invL, 2)) / ((glm::sqrt(dsqr) + this->
xi* invL) * lp);
504 tL = (1 - this->
s) * (dsqr - glm::pow(this->
xi * invL, 2)) / ((glm::sqrt(dsqr) + this->
xi * invL) * lp);
505 for (
int i = 0; i < 4; ++i)
509 if (time > 0.0 && ((dsqr - glm::pow(this->
xi * invL, 2)) / (glm::sqrt(dsqr) + this->
xi * invL) <= g))
539 T L1 =
pow((p1 - q1).norm(),2);
540 T L2 =
pow((p1 - q2).norm(),2);
541 T L3 =
pow((p2 - q1).norm(), 2);
542 T L4 =
pow((p2 - q2).norm(), 2);
547 for (
int i = 0; i < 4; ++i) {
551 T lp =
max(p[0].norm(), p[1].norm()) +
552 max(p[2].norm(), p[3].norm());
554 if (lp ==0.0)
return false;
564 T g = this->
s * (dsqr -
pow(this->
xi * invL, 2)) / (
sqrt(dsqr) + this->
xi * invL);
566 T tL = (1 - this->
s) * (dsqr -
pow(this->
xi * invL, 2)) / ((
sqrt(dsqr) + this->
xi * invL) * lp);
575 tL = (1 - this->
s) * (dsqr -
pow(this->
xi * invL, 2)) / ((
sqrt(dsqr) + this->
xi * invL) * lp);
577 for (
int i = 0; i < 4; ++i) {
586 auto r = (dsqr -
pow(this->
xi * invL, 2)) / (
sqrt(dsqr) + this->
xi * invL);
587 if (time >0.0 && r<g)
612 Real invL = 1 / lmax;
615 p[0] = invL * s0.
v[0];
616 p[1] = invL * s0.
v[1];
617 p[2] = invL * s0.
v[2];
620 pp[0] = invL * s1.
v[0];
621 pp[1] = invL * s1.
v[1];
622 pp[2] = invL * s1.
v[2];
625 q[0] = invL * t0.
v[0];
626 q[1] = invL * t0.
v[1];
627 q[2] = invL * t0.
v[2];
630 qq[0] = invL * t1.
v[0];
631 qq[1] = invL * t1.
v[1];
632 qq[2] = invL * t1.
v[2];
637 for (
int st = 0; st < 3; st++)
641 q[0], q[1], q[2],p[st],
642 qq[0], qq[1], qq[2],pp[st],
646 toi = collided ?
minimum(t, toi) : toi;
651 for (
int st = 0; st < 3; st++)
655 p[0], p[1], p[2],q[st],
656 pp[0], pp[1], pp[2],qq[st],
658 toi = collided ?
minimum(t, toi) : toi;
663 for (
int st = 0; st < 3; st++)
666 int ind1 = (st + 1) % 3;
667 for (
int ss = 0; ss < 3; ss++)
670 int ind3 = (ss + 1) % 3;
674 p[ind0], p[ind1], q[ind2], q[ind3],
675 pp[ind0], pp[ind1], qq[ind2], qq[ind3],
678 toi = collided ?
minimum(t, toi) : toi;
693 Real l0 =
s.maximumEdgeLength();
699 first[0] = 1.0, first[1] = 0.0, first[2] = 0.0,
700 second[0] = 1.0, second[1] = 0.0, second[2] = 0.0;
704 Real invL = 1 / lmax;
707 p[0] = invL *
s.v[0];
708 p[1] = invL *
s.v[1];
709 p[2] = invL *
s.v[2];
712 q[0] = invL * t.
v[0];
713 q[1] = invL * t.
v[1];
714 q[2] = invL * t.
v[2];
719 for (
int st = 0; st < 3; st++)
723 p[st], q[0], q[1], q[2], para);
724 auto dis = disVector.norm();
728 first[(st + 1) % 3] = 0.0;
729 first[(st + 2) % 3] = 0.0;
737 for (
int st = 0; st < 3; st++)
741 q[st], p[0], p[1], p[2], para);
742 auto dis = disVector.norm();
745 second[st] = para[0];
746 second[(st + 1) % 3] = 0.0;
747 second[(st + 2) % 3] = 0.0;
754 for (
int st = 0; st < 3; st++)
757 int ind1 = (st + 1) % 3;
758 for (
int ss = 0; ss < 3; ss++)
761 int ind3 = (ss + 1) % 3;
765 p[ind0], p[ind1], q[ind2], q[ind3], para);
766 auto dis = disVector.norm();
769 first[ind0] = para[0];
770 first[ind1] = para[1];
771 first[(ind1 + 1) % 3] = 0.0;
772 second[ind2] = para[2];
773 second[ind3] = para[3];
774 second[(ind3 + 1) % 3] = 0.0;
DYN_FUNC Vector< T, 3 > DistanceVF_v(const Vector< T, 3 > &x, const Vector< T, 3 > &y0, const Vector< T, 3 > &y1, const Vector< T, 3 > &y2, T *para)
DYN_FUNC T DistanceVF(const Vector< T, 3 > &x, const Vector< T, 3 > &y0, const Vector< T, 3 > &y1, const Vector< T, 3 > &y2)
DYN_FUNC bool VertexFaceCCD(const Vector< T, 3 > &x0, const Vector< T, 3 > &x1, const Vector< T, 3 > &x2, const Vector< T, 3 > &x3, const Vector< T, 3 > &y0, const Vector< T, 3 > &y1, const Vector< T, 3 > &y2, const Vector< T, 3 > &y3, T &time, T invL)
Do a continuous collision detection between a vertex and a triangle.
DYN_FUNC Vector< T, 3 > DistanceEE(const Vector< T, 3 > &x0, const Vector< T, 3 > &x1, const Vector< T, 3 > &y0, const Vector< T, 3 > &y1, T *para)
DYN_FUNC bool TriangleCCD(TTriangle3D< Real > &s0, TTriangle3D< Real > &s1, TTriangle3D< Real > &t0, TTriangle3D< Real > &t1, Real &toi)
Do a continuous collision detection between two triangles.
DYN_FUNC bool EdgeEdgeCCD(const Vector< T, 3 > &x0, const Vector< T, 3 > &x1, const Vector< T, 3 > &x2, const Vector< T, 3 > &x3, const Vector< T, 3 > &y0, const Vector< T, 3 > &y1, const Vector< T, 3 > &y2, const Vector< T, 3 > &y3, T &time, T invL)
Do a continuous collision detection between two edges.
DYN_FUNC void projectClosePoint(const TTriangle3D< Real > &s, const TTriangle3D< Real > &t, Vector< T, 3 > &first, Vector< T, 3 > &second)
find the close point between two triangles, store their barycentric coordinates ordered as vertex.
DYN_FUNC T SquareDistanceEE(const Vector< T, 3 > &x0, const Vector< T, 3 > &x1, const Vector< T, 3 > &x2, const Vector< T, 3 > &x3)
DYN_FUNC T SquareDistanceVF(const Vector< T, 3 > &x0, const Vector< T, 3 > &x1, const Vector< T, 3 > &x2, const Vector< T, 3 > &x3)
DYN_FUNC Real maximumEdgeLength() const
This is an implementation of AdditiveCCD based on peridyno.
DYN_FUNC T getPoint2SegmentDistance(const Vector< T, 3 > &p, const Vector< T, 3 > &v0, const Vector< T, 3 > &v1)
DYN_FUNC Vector< T, 3 > cross(Vector< T, 3 > const &U, Vector< T, 3 > const &V)
DYN_FUNC T dot(Vector< T, 2 > const &U, Vector< T, 2 > const &V)
DYN_FUNC Complex< Real > pow(const Complex< Real > &, const Real &)
DYN_FUNC bool inTri(const Vector< T, 3 > &p, const Vector< T, 3 > &v0, const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
DYN_FUNC T minimum(const T &v0, const T &v1)
constexpr Real REAL_EPSILON
DYN_FUNC Complex< Real > sqrt(const Complex< Real > &)
DYN_FUNC T maximum(const T &v0, const T &v1)