14        #define REAL_infinity 1.0e30 
   15        #define REAL_ZERO 1.0e-5 
   17        #define REAL_EQUAL(a,b)  (((a < b + REAL_EPS) && (a > b - REAL_EPS)) ? true : false) 
   18        #define REAL_GREAT(a,b) ((a > REAL_EPS + b)? true: false)  
   19        #define REAL_LESS(a,b) ((a + REAL_EPS < b)? true: false)  
   22        DYN_FUNC 
void Swap(
T& a, 
T& b) { 
T c = a; a = b; b = c;}
 
   25            float dot = U[0] * 
V[0] + U[1] * 
V[1] + U[2] * 
V[2];
 
 
   31            float dot = U[0] * 
V[0] + U[1] * 
V[1];
 
 
   39                U[1] * 
V[2] - U[2] * 
V[1],
 
   40                U[2] * 
V[0] - U[0] * 
V[2],
 
   41                U[0] * 
V[1] - U[1] * 
V[0]
 
 
   55            return Vec2f(v[1], -v[0]);
 
 
   70        DYN_FUNC 
bool isOverLap(
float& c0, 
float& c1, 
float a0, 
float a1, 
float b0, 
float b1)
 
 
   89            float a00 = e0.dot(e0);
 
   90            float a01 = e0.dot(e1);
 
   91            float a11 = e1.dot(e1);
 
   95            float det = fmax(a00 * a11 - a01 * a01, 0.f);
 
   96            float s = a01 * b1 - a11 * b0;
 
   97            float t = a01 * b0 - a00 * b1;
 
  158                float tmp0 = 0.f; 
float tmp1 = 0.f; 
float numer = 0.f; 
float denom = 0.f;
 
  165                        denom = a00 - 2.f * a01 + a11;
 
  195                        denom = a00 - 2.f * a01 + a11;
 
  221                    numer = a11 + b1 - a01 - b0;
 
  227                        denom = a00 - 2.f * a01 + a11;
 
  240            Vec3f v = (a0 + s * e0 + t * e1);
 
 
  282            if (n_a == 0 || n_b == 0) 
return false;
 
  283            auto project = [&](
Vec3f axis, 
Vec3f* p, 
int n, 
float& pmin, 
float& pmax)
 
  287                for (
int i = 0; i < n; i++)
 
  290                    pmin = fmin(pmin, 
dot);
 
  291                    pmax = fmax(pmax, 
dot);
 
  296                float amin, amax, bmin, bmax, cmin, cmax;
 
  297                project(axis, a, n_a, amin, amax);
 
  298                project(axis, b, n_b, bmin, bmax);
 
  299                if(!
isOverLap(cmin, cmax, amin, amax, bmin, bmax))
 
  307            for (
int i = 0; i < n_a; i++)
 
  309                j = (i + 1 == n_a) ? 0 : i + 1;
 
  325            for (
int i = 0; i < n_b; i++)
 
  327                j = (i + 1 == n_b) ? 0 : i + 1;
 
 
  357            if (n_a == 0 || n_b == 0) 
return 0.f;
 
  358            auto project = [&](
Vec3f axis, 
Vec3f* p, 
int n, 
float& pmin, 
float& pmax)
 
  362                for (
int i = 0; i < n; i++)
 
  365                    pmin = fmin(pmin, 
dot);
 
  366                    pmax = fmax(pmax, 
dot);
 
  370            auto cal_box_area = [&](
Vec3f axis)
 
  372                float amin, amax, bmin, bmax, cmin, cmax;
 
  373                project(axis, a, n_a, amin, amax);
 
  374                project(axis, b, n_b, bmin, bmax);
 
  375                if(!
isOverLap(cmin, cmax, amin, amax, bmin, bmax))
 
  380                float dx = cmax - cmin;
 
  383                project(axis_y, a, n_a, amin, amax);
 
  384                project(axis_y, b, n_b, bmin, bmax);
 
  385                if(!
isOverLap(cmin, cmax, amin, amax, bmin, bmax))
 
  389                float dy = cmax - cmin;
 
  396            for (
int i = 0; i < n_a; i++)
 
  398                j = (i + 1 == n_a) ? 0 : i + 1;
 
  401                float area = cal_box_area(axis);
 
  402                res = fmin(res, area);
 
  417            for (
int i = 0; i < n_b; i++)
 
  419                j = (i + 1 == n_b) ? 0 : i + 1;
 
  422                float area = cal_box_area(axis);
 
  423                res = fmin(res, area);
 
 
  451            Vec3f t[3] = {b1, b2, b3};
 
  455            float d[3]; 
int s[3];
 
  456            for (
int i = 0; i < 3; i++) {
 
  457                d[i] = 
Dot(nA, t[i] - oA);
 
  462            if (s[0] == 0 && s[1] == 0 && s[2] == 0) 
return false;
 
  464            for (
int i = 0; i < 3; i++)
 
  466                if(s[i] == 0) p[p_num++] = t[i];
 
  469                    for (
int j = i + 1; j < 3; j++)
 
  470                    if (s[i] * s[j] < 0) p[p_num++] = t[j] + (t[i] - t[j]) * d[j] / (d[j] - d[i]);
 
  473            if (p_num > 2) printf(
"[geo] Error: p_num > 2 %d %d %d %f %f %f\n", s[0], s[1], s[2], d[0], d[1], d[2]);
 
  474            if (p_num != 2) 
return false;
 
  476            Vec3f vec_a1 = (a1 - oA);
 
  477            Vec3f vec_a2 = (a2 - oA);
 
  478            Vec3f vec_p1 = (p[0] - oA);
 
  479            Vec3f vec_p2 = (p[1] - oA);
 
  482            Vec3f mid_a = (vec_a1 + vec_a2) * 0.5f;
 
  484            Real cos_a = 
Dot(vec_a1, mid_a);
 
  485            Real cos_p1 = 
Dot(vec_p1, mid_a);
 
  486            Real cos_p2 = 
Dot(vec_p2, mid_a);
 
 
  510            Vec3f t[4] = {b0, b1, b2, b3};
 
  514            float d[4]; 
int s[4];
 
  515            for (
int i = 0; i < 4; i++) {
 
  516                d[i] = 
Dot(nA, t[i] - oA);
 
  518                printf(
"[geo] Error d nan nA(%f %f %f) oA(%f %f %f) t(%f %f %f)\n", nA[0], nA[1], nA[2], oA[0], oA[1], oA[2], t[i][0], t[i][1], t[i][2]);
 
  521            for (
int i = 0; i < 4; i++)
 
  523                if(s[i] == 0) p[p_num++] = t[i];
 
  526                    for (
int j = i + 1; j < 4; j++)
 
  527                    if (s[i] * s[j] < 0) {
 
  531                            p[p_num++] = t[j] + (t[i] - t[j]) * d[j] / (d[j] - d[i]);
 
  539            if (p_num > 4) printf(
"[geo] Error: p_num > 4 [%d %d %d %d]\n", s[0], s[1], s[2], s[3]);
 
  543                bool s0 = 
Sign(nA, p[0], p[1], p[2]);
 
  544                bool s1 = 
Sign(nA, p[0], p[1], p[3]);
 
  545                if(s1 != s0) 
Swap(p[0], p[3]);
 
  547                    bool s2 = 
Sign(nA, p[0], p[2], p[3]);
 
  548                    if(s2 != s0) 
Swap(p[2], p[3]);
 
  555                for(
int i = 0; i < p_num; i++)
 
  563                        printf(
"[geo] (isIntrTri2Tet) Error outside plane %f (%f %f %f) s[%d %d %d %d] d(%f %f %f %f)\n", 
Dot(nA, p[i] - oA), nA[0], p[i][0], oA[0], s[0], s[1], s[2], s[3], d[0], d[1], d[2], d[3]);
 
  569            Vec3f a[3] = {a0, a1, a2};
 
 
  577            Vec3f edge20, edge10, edge30, edge21, edge31;
 
  578            Vec3f diffP0, diffP1;
 
  579            float tsp = 0.f, zero = 0.f;
 
  586            tps0 = 
DotCross(edge30, edge10, edge20); 
 
  589            tsp = 
DotCross(diffP0, edge10, edge20);
 
  596            tsp = 
DotCross(diffP0, edge30, edge10);
 
  603            tsp = 
DotCross(diffP0, edge20,  edge30);
 
  613            tsp = 
DotCross(diffP1, edge31, edge21);
 
 
  629            Vec3f edge20, edge10, edge30;
 
  633            return DotCross(edge30, edge10, edge20) / 6.f;
 
 
  640            Vec3f diff[4] = { b0 - b3, b1 - b3, b2 - b3, p - b3 };
 
  641            float det = 
DotCross(diff[0], diff[1], diff[2]);
 
  642            if (det < -EPSILON || det > 
EPSILON) 
 
  644                bary[0] = 
DotCross(diff[3], diff[1], diff[2]) / det;
 
  645                bary[1] = 
DotCross(diff[3], diff[2], diff[0]) / det;
 
  646                bary[2] = 
DotCross(diff[3], diff[0], diff[1]) / det;
 
 
  663            float const zero = 0.f;
 
  665            Vec3f s( zero, zero, zero );
 
  666            int numPositive = 0, numNegative = 0, numZero = 0;
 
  667            Vec2f v[3] = { b0, b1, b2 };
 
  668            Vec2f diff[3] = { b0 - a0, b1 - a0, b2 - a0 };
 
  669            Vec2f direction = a1 - a0;
 
  671            for (
size_t i = 0; i < 3; ++i)
 
  673                s[i] = 
DotPerp(direction, diff[i]);
 
  678                else if (s[i] < zero)
 
  688            if (numZero == 0 && numPositive > 0 && numNegative > 0)
 
  696                float sign = (3 > numPositive * 2 ? 1.f : -1.f);
 
  697                for (
size_t i0 = 1, i1 = 2, i2 = 0; i2 < 3; i0 = i1, i1 = i2++)
 
  699                    if (
sign * s[i2] > zero)
 
  701                        Vec2f diffVi0P0 = v[i0] - origin;
 
  702                        Vec2f diffVi2Vi0 = v[i2] - v[i0];
 
  703                        float lambda0 = s[i0] / (s[i0] - s[i2]);
 
  704                        Vec2f q0 = diffVi0P0 + lambda0 * diffVi2Vi0;
 
  705                        t0 = 
Dot(direction, q0);
 
  706                        Vec2f diffVi1P0 = v[i1] - origin;
 
  707                        Vec2f diffVi2Vi1 = v[i2] - v[i1];
 
  708                        float lambda1 = s[i1] / (s[i1] - s[i2]);
 
  709                        Vec2f q1 = diffVi1P0 + lambda1 * diffVi2Vi1;
 
  710                        t1 = 
Dot(direction, q1);
 
  715            else if (numZero == 1)
 
  718                for (
size_t i0 = 1, i1 = 2, i2 = 0; i2 < 3; i0 = i1, i1 = i2++)
 
  722                        Vec2f diffVi2P0 = v[i2] - origin;
 
  723                        t0 = 
Dot(direction, diffVi2P0);
 
  724                        if (numPositive == 2 || numNegative == 2)
 
  734                            Vec2f diffVi0P0 = v[i0] - origin;
 
  735                            Vec2f diffVi1Vi0 = v[i1] - v[i0];
 
  736                            float lambda0 = s[i0] / (s[i0] - s[i1]);
 
  737                            Vec2f q = diffVi0P0 + lambda0 * diffVi1Vi0;
 
  738                            t1 = 
Dot(direction, q);
 
  744            else if (numZero == 2)
 
  748                for (
size_t i0 = 1, i1 = 2, i2 = 0; i2 < 3; i0 = i1, i1 = i2++)
 
  752                        Vec2f diffVi0P0 = v[i0] - origin;
 
  753                        t0 = 
Dot(direction, diffVi0P0);
 
  754                        Vec2f diffVi1P0 = v[i1] - origin;
 
  755                        t1 = 
Dot(direction, diffVi1P0);
 
  766                float directionSqrLength = 
Dot(direction, direction);
 
  767                t0 /= directionSqrLength;
 
  768                t1 /= directionSqrLength;
 
 
  787            Vec3f nA = 
Cross(a1 - a0, a2 - a0); nA.normalize();
 
  788            Vec3f nB = 
Cross(b1 - b0, b2 - b0); nB.normalize();
 
  800            float d0 = 
Dot(nA, b0 - oA);
 
  801            float d1 = 
Dot(nA, b1 - oA);
 
  802            float d2 = 
Dot(nA, b2 - oA);
 
  809            if (s0 == 0 && s1 == 0 && s2 == 0) 
return 0;
 
  812            if (s0 == 0) q[seg_res++] = b0;
 
  813            if (s1 == 0) q[seg_res++] = b1;
 
  814            if (s2 == 0) q[seg_res++] = b2;
 
  817            if (s0 * s1 < 0 && (!
REAL_EQUAL(d0 - d1, 0.f))) q[seg_res++] = b0 + (b1 - b0) * d0 / (d0 - d1);
 
  818            if (s1 * s2 < 0 && (!
REAL_EQUAL(d1 - d2, 0.f))) q[seg_res++] = b1 + (b2 - b1) * d1 / (d1 - d2);
 
  819            if (s0 * s2 < 0 && (!
REAL_EQUAL(d2 - d0, 0.f))) q[seg_res++] = b2 + (b0 - b2) * d2 / (d2 - d0);
 
  821            if (seg_res > 2) printf(
"[geo] Error: res > 2 %d %d %d %f %f %f\n", s0, s1, s2, d0, d1, d2);
 
  822            for (
int _ = 0; _ < seg_res; ++_)
 
  823                if (q[_] != q[_]) printf(
"[geo] Error in nan\n");
 
  830                float cmax = std::fabs(nA[0]);
 
  831                float cvalue = std::fabs(nA[1]);
 
  837                cvalue = std::fabs(nA[2]);
 
  847                    lookup = { 1, 2, 0 };
 
  849                else if (maxIndex == 1)
 
  852                    lookup = { 0, 2, 1 };
 
  857                    lookup = { 0, 1, 2 };
 
  862                Vec3f ao[3] = {a0 - oA, a1 - oA, a2 - oA};
 
  863                for (
size_t i = 0; i < 3; ++i)
 
  865                    a2d[i][0] = ao[i][lookup[0]];
 
  866                    a2d[i][1] = ao[i][lookup[1]];
 
  870                Vec3f qo[2] ={q[0] - oA, q[1] - oA};
 
  871                for (
size_t i = 0; i < 2; ++i)
 
  873                    q2d[i][0] = qo[i][lookup[0]];
 
  874                    q2d[i][1] = qo[i][lookup[1]];
 
  877                float t0 = 0.f, t1 = 0.f;
 
  878                int res2d = 
getIntersection(t0, t1, q2d[0], q2d[1], a2d[0], a2d[1], a2d[2]);
 
  884                        p0 = q[0] + (q[1] - q[0]) * t0;
 
  895                    p0 = q[0] + (q[1] - q[0]) * t0;
 
  896                    p1 = q[0] + (q[1] - q[0]) * t1;
 
  900            else if (seg_res == 1) 
 
  904                int numPositive = 0, numNegative = 0, numZero = 0;
 
  905                float s[3] = {
Dot(a0 - q[0], a1 - q[0]), 
Dot(a1 - q[0], a2 - q[0]), 
Dot(a2 - q[0], a0 - q[0])};
 
  906                for (
size_t i = 0; i < 3; ++i)
 
  921                if (!(numPositive > 0 && numNegative > 0))
 
 
  940            Vec3f t[4] = {b0, b1, b2, b3};
 
  944            float d[4]; 
int s[4];
 
  945            for (
int i = 0; i < 4; i++) {
 
  946                d[i] = 
Dot(nA, t[i] - oA);
 
  949            for (
int i = 0; i < 4; i++)
 
  951                if(s[i] == 0) p[p_num++] = t[i];
 
  954                    for (
int j = i + 1; j < 4; j++)
 
  955                    if (s[i] * s[j] < 0) p[p_num++] = t[j] + (t[i] - t[j]) * d[j] / (d[j] - d[i]);
 
  958            if (p_num > 4) printf(
"[geo] Error: p_num > 4\n");
 
  962                bool s0 = 
Sign(nA, p[0], p[1], p[2]);
 
  963                bool s1 = 
Sign(nA, p[0], p[1], p[3]);
 
  964                if(s1 != s0) 
Swap(p[0], p[3]);
 
  966                    bool s2 = 
Sign(nA, p[0], p[2], p[3]);
 
  967                    if(s2 != s0) 
Swap(p[2], p[3]);
 
  973            Vec3f a[3] = {a0, a1, a2};
 
  974            if (p_num <= 2) res = 0.f;
 
 
  992            float A = 
Dot(d0, d0);
 
  993            float B = 
Dot(d0, d1);
 
  994            float C = 
Dot(d1, d1);
 
  995            float D0 = 
Dot(d0, p);
 
  996            float D1 = 
Dot(d1, p);
 
  998            float delta = A * C - B * B;
 
 1000            float u = (C * D0 - B * D1) / delta;
 
 1001            float v = (A * D1 - B * D0) / delta;
 
 
 1012            float d0 = 
Dot(nA, b0 - oA);
 
 1013            float d1 = 
Dot(nA, b1 - oA);
 
 1019            if (s0 == 0) q[res++] = b0;
 
 1020            if (s1 == 0) q[res++] = b1;
 
 1023            if (s0 * s1 < 0 && (!
REAL_EQUAL(d0 - d1, 0.f))) q[res++] = b0 + (b1 - b0) * d0 / (d0 - d1);
 
 
 1034            float d0 = 
Dot(nA, b0 - oA);
 
 1035            float d1 = 
Dot(nA, b1 - oA);
 
 1036            float d2 = 
Dot(nA, b2 - oA);
 
 1043            if (s0 == 0) q[res++] = b0;
 
 1044            if (s1 == 0) q[res++] = b1;
 
 1045            if (s2 == 0) q[res++] = b2;
 
 1048            if (s0 * s1 < 0 && (!
REAL_EQUAL(d0 - d1, 0.f))) q[res++] = b0 + (b1 - b0) * d0 / (d0 - d1);
 
 1049            if (s1 * s2 < 0 && (!
REAL_EQUAL(d1 - d2, 0.f))) q[res++] = b1 + (b2 - b1) * d1 / (d1 - d2);
 
 1050            if (s0 * s2 < 0 && (!
REAL_EQUAL(d2 - d0, 0.f))) q[res++] = b2 + (b0 - b2) * d2 / (d2 - d0);
 
 
 1062            float d0 = 
Dot(nA, b0 - oA);
 
 1063            float d1 = 
Dot(nA, b1 - oA);
 
 1064            float d2 = 
Dot(nA, b2 - oA);
 
 1065            float d3 = 
Dot(nA, b3 - oA);
 
 1073            if (s0 == 0) q[res++] = b0;
 
 1074            if (s1 == 0) q[res++] = b1;
 
 1075            if (s2 == 0) q[res++] = b2;
 
 1076            if (s3 == 0) q[res++] = b3;
 
 1079            if (s0 * s1 < 0 && (!
REAL_EQUAL(d0 - d1, 0.f))) q[res++] = b0 + (b1 - b0) * d0 / (d0 - d1);
 
 1080            if (s0 * s2 < 0 && (!
REAL_EQUAL(d0 - d2, 0.f))) q[res++] = b0 + (b2 - b0) * d0 / (d0 - d2);
 
 1081            if (s0 * s3 < 0 && (!
REAL_EQUAL(d0 - d3, 0.f))) q[res++] = b0 + (b3 - b0) * d0 / (d0 - d3);
 
 1082            if (s1 * s2 < 0 && (!
REAL_EQUAL(d1 - d2, 0.f))) q[res++] = b1 + (b2 - b1) * d1 / (d1 - d2);
 
 1083            if (s1 * s3 < 0 && (!
REAL_EQUAL(d1 - d3, 0.f))) q[res++] = b1 + (b3 - b1) * d1 / (d1 - d3);
 
 1084            if (s2 * s3 < 0 && (!
REAL_EQUAL(d2 - d3, 0.f))) q[res++] = b2 + (b3 - b2) * d2 / (d2 - d3);
 
 1089                Vec3f q01 = q[1] - q[0];
 
 1090                Vec3f q02 = q[2] - q[0];
 
 1091                Vec3f q03 = q[3] - q[0];
 
 
 1109            Vec3f b0 = center - halfU - halfV - halfW;
 
 1110            Vec3f b1 = center + halfU - halfV - halfW;
 
 1111            Vec3f b2 = center - halfU + halfV - halfW;
 
 1112            Vec3f b3 = center + halfU + halfV - halfW;
 
 1113            Vec3f b4 = center - halfU - halfV + halfW;
 
 1114            Vec3f b5 = center + halfU - halfV + halfW;
 
 1115            Vec3f b6 = center - halfU + halfV + halfW;
 
 1116            Vec3f b7 = center + halfU + halfV + halfW;
 
 1118            float d0 = 
Dot(nA, b0 - oA);
 
 1119            float d1 = 
Dot(nA, b1 - oA);
 
 1120            float d2 = 
Dot(nA, b2 - oA);
 
 1121            float d3 = 
Dot(nA, b3 - oA);
 
 1122            float d4 = 
Dot(nA, b4 - oA);
 
 1123            float d5 = 
Dot(nA, b5 - oA);
 
 1124            float d6 = 
Dot(nA, b6 - oA);
 
 1125            float d7 = 
Dot(nA, b7 - oA);
 
 1137            if (s0 == 0) q[res++] = b0;
 
 1138            if (s1 == 0) q[res++] = b1;
 
 1139            if (s2 == 0) q[res++] = b2;
 
 1140            if (s3 == 0) q[res++] = b3;
 
 1141            if (s4 == 0) q[res++] = b4;
 
 1142            if (s5 == 0) q[res++] = b5;
 
 1143            if (s6 == 0) q[res++] = b6;
 
 1144            if (s7 == 0) q[res++] = b7;
 
 1147            if (s0 * s1 < 0 && (!
REAL_EQUAL(d0 - d1, 0.f))) q[res++] = b0 + (b1 - b0) * d0 / (d0 - d1);
 
 1148            if (s0 * s2 < 0 && (!
REAL_EQUAL(d0 - d2, 0.f))) q[res++] = b0 + (b2 - b0) * d0 / (d0 - d2);
 
 1149            if (s0 * s4 < 0 && (!
REAL_EQUAL(d0 - d4, 0.f))) q[res++] = b0 + (b4 - b0) * d0 / (d0 - d4);
 
 1151            if (s1 * s3 < 0 && (!
REAL_EQUAL(d1 - d3, 0.f))) q[res++] = b1 + (b3 - b1) * d1 / (d1 - d3);
 
 1152            if (s1 * s5 < 0 && (!
REAL_EQUAL(d1 - d5, 0.f))) q[res++] = b1 + (b5 - b1) * d1 / (d1 - d5);
 
 1154            if (s2 * s3 < 0 && (!
REAL_EQUAL(d2 - d3, 0.f))) q[res++] = b2 + (b3 - b2) * d2 / (d2 - d3);
 
 1155            if (s2 * s6 < 0 && (!
REAL_EQUAL(d2 - d6, 0.f))) q[res++] = b2 + (b6 - b2) * d2 / (d2 - d6);
 
 1157            if (s3 * s7 < 0 && (!
REAL_EQUAL(d3 - d7, 0.f))) q[res++] = b3 + (b7 - b3) * d3 / (d3 - d7);
 
 1159            if (s4 * s5 < 0 && (!
REAL_EQUAL(d4 - d5, 0.f))) q[res++] = b4 + (b5 - b4) * d4 / (d4 - d5);
 
 1160            if (s4 * s6 < 0 && (!
REAL_EQUAL(d4 - d6, 0.f))) q[res++] = b4 + (b6 - b4) * d4 / (d4 - d6);
 
 1162            if (s5 * s7 < 0 && (!
REAL_EQUAL(d5 - d7, 0.f))) q[res++] = b5 + (b7 - b5) * d5 / (d5 - d7);
 
 1164            if (s6 * s7 < 0 && (!
REAL_EQUAL(d6 - d7, 0.f))) q[res++] = b6 + (b7 - b6) * d6 / (d6 - d7);
 
 1169                Vec3f q01 = q[1] - q[0];
 
 1170                Vec3f q02 = q[2] - q[0];
 
 1171                Vec3f q03 = q[3] - q[0];
 
 
 1188            for (
int i = 0; i < n; i++)
 
 1190                int ni = (i == n - 1) ? 0 : i + 1;
 
 1192                Vec2f dB = p[ni] - oB;
 
 1196                tA = 
DotPerp(dB, oA - oB) / d;
 
 1198                tB = 
DotPerp(dA, oA - oB) / d;
 
 1202                    int nni = (ni == n - 1) ? 0 : ni + 1;
 
 1203                    Vec2f ndB = p[nni] - p[ni];
 
 1211                for (
int i = 0; i < n; i++)
 
 1213                    int ni = (i == n - 1) ? 0 : i + 1;
 
 1215                    Vec2f dB = p[ni] - oB;
 
 1219                    tA = 
DotPerp(dB, oA - oB) / d;
 
 1221                    tB = 
DotPerp(dA, oA - oB) / d;
 
 1222                    printf(
"tA tB :%f %f\n", tA, tB);
 
 1226                        int nni = (ni == n - 1) ? 0 : ni + 1;
 
 1227                        Vec2f ndB = p[nni] - p[ni];
 
 1229                        printf(
"dd %f %f\n", d, d2);
 
 1233                for (
int i = 0; i < res; ++i)
 
 1235                    Vec2f b = a0 + t[i] * (a1 - a0);
 
 1236                    printf(
" t %f p: %f %f\n", t[i], b[0], b[1]);
 
 1238                for (
int i = 0; i < n; ++i)
 
 1239                    printf(
"p: %f %f\n", p[i][0], p[i][1]);
 
 1240                printf(
"a0 %f %f\n", a0[0], a0[1]);
 
 1241                printf(
"a1 %f %f\n", a1[0], a1[1]);
 
 
 1253            Vec3f nA = 
Cross(a1 - a0, a2 - a0); nA.normalize();
 
 1256            float cmax = std::fabs(nA[0]);
 
 1257            float cvalue = std::fabs(nA[1]);
 
 1263            cvalue = std::fabs(nA[2]);
 
 1264            if (cvalue > cmax) maxIndex = 2;
 
 1267            if (maxIndex == 0)      lookup = { 1, 2, 0 };
 
 1268            else if (maxIndex == 1) lookup = { 0, 2, 1 }; 
 
 1269            else                    lookup = { 0, 1, 2 }; 
 
 1273            a2d[0] = 
Vec2f(a0[lookup[0]], a0[lookup[1]]); 
 
 1274            a2d[1] = 
Vec2f(a1[lookup[0]], a1[lookup[1]]);
 
 1275            a2d[2] = 
Vec2f(a2[lookup[0]], a2[lookup[1]]);
 
 1276            for (
int i = 0; i < n; ++i)
 
 1277                p2d[i] = 
Vec2f(p[i][lookup[0]], p[i][lookup[1]]);
 
 1282                Vec2f center = (a2d[0] + a2d[1] + a2d[2]) / 3.0f;
 
 1286                for (
int j = 0; j < num_intr; ++j)
 
 1290                if (num_inside == 1) q[res++] = p[0];
 
 1295            for (
int i = 0; i < n; ++i)
 
 1297                int ni = (i == n - 1) ? 0 : i + 1;
 
 1301                for (
int j = 0; j < num_intr; ++j)
 
 1307                            q[res++] = p[i] + (p[ni] - p[i]) * t[j];
 
 1310                if (num_inside == 1) q[res++] = p[i];
 
 1316                for (
int i = 0; i < 3; ++i)
 
 1318                    int ni = (i == 2) ? 0 : i + 1;
 
 1322                    for (
int j = 0; j < num_intr; ++j)
 
 1327                    if (num_inside == 1) q[res++] = (i == 0) ? a0 : ((i == 1) ? a1 : a2);
 
 
 1340            Vec3f nA = 
Cross(a1 - a0, a2 - a0); nA.normalize();
 
 1343            float cmax = std::fabs(nA[0]);
 
 1344            float cvalue = std::fabs(nA[1]);
 
 1350            cvalue = std::fabs(nA[2]);
 
 1351            if (cvalue > cmax) maxIndex = 2;
 
 1354            if (maxIndex == 0)      lookup = { 1, 2, 0 };
 
 1355            else if (maxIndex == 1) lookup = { 0, 2, 1 }; 
 
 1356            else                    lookup = { 0, 1, 2 }; 
 
 1360            a[0] = a0; a[1] = a1; a[2] = a2; a[3] = a3;
 
 1361            for (
int i = 0; i < 4; ++i) a2d[i] = 
Vec2f(a[i][lookup[0]], a[i][lookup[1]]);
 
 1362            for (
int i = 0; i < n; ++i) p2d[i] = 
Vec2f(p[i][lookup[0]], p[i][lookup[1]]);
 
 1367                Vec2f center = (a2d[0] + a2d[1] + a2d[2] + a2d[3]) * 0.25f;
 
 1371                for (
int j = 0; j < num_intr; ++j)
 
 1375                if (num_inside == 1) q[res++] = p[0];
 
 1379            for (
int i = 0; i < n; ++i)
 
 1381                int ni = (i == n - 1) ? 0 : i + 1;
 
 1385                for (
int j = 0; j < num_intr; ++j)
 
 1390                        if (
REAL_LESS(t[j], 1.f) && (ni != 0 || i != 1) ) 
 
 1391                            q[res++] = p[i] + (p[ni] - p[i]) * t[j];
 
 1394                if (num_inside == 1) q[res++] = p[i];
 
 1400                for (
int i = 0; i < 4; ++i)
 
 1402                    int ni = (i == 3) ? 0 : i + 1;
 
 1406                    for (
int j = 0; j < num_intr; ++j)
 
 1411                    if (num_inside == 1) q[res++] = a[i];
 
 
void check(T result, char const *const func, const char *const file, int const line)
 
DYN_FUNC int intrSegWithPlane(Vec3f *q, Vec3f oA, Vec3f nA, Vec3f b0, Vec3f b1)
 
DYN_FUNC bool isInNarrowBand(Vec3f b0, Vec3f b1, Vec3f b2, Vec3f b3, Vec3f a0, Vec3f a1, Vec3f a2, float d)
 
DYN_FUNC Vec2f projectWithParall(Vec3f p, Vec3f a0, Vec3f a1, Vec3f a2, Vec3f a3)
 
DYN_FUNC float getDistanceVF(Vec3f p, Vec3f a0, Vec3f a1, Vec3f a2)
 
DYN_FUNC float DotPerp(Vec2f const &v0, Vec2f const &v1)
 
DYN_FUNC int intrBoxWithPlane(Vec3f *q, Vec3f oA, Vec3f nA, Vec3f center, Vec3f halfU, Vec3f halfV, Vec3f halfW)
 
DYN_FUNC bool isConPolyOverLap2D(Vec3f face_n, int n_a, Vec3f *a, int n_b, Vec3f *b)
 
DYN_FUNC float DotCross(Vec3f const &U, Vec3f const &V, Vec3f const &W)
 
DYN_FUNC int intrPolyWithRect(Vec3f *q, int n, Vec3f *p, Vec3f a0, Vec3f a1, Vec3f a2, Vec3f a3)
 
DYN_FUNC Vec3f getProjectionVF(Vec3f p, Vec3f a0, Vec3f a1, Vec3f a2)
 
DYN_FUNC int intrTetWithPlane(Vec3f *q, Vec3f oA, Vec3f nA, Vec3f b0, Vec3f b1, Vec3f b2, Vec3f b3)
 
DYN_FUNC float getTriBoxAreaInTet(Vec3f a0, Vec3f a1, Vec3f a2, Vec3f b0, Vec3f b1, Vec3f b2, Vec3f b3)
 
DYN_FUNC bool isInTet(Vec3f p, Vec3f b0, Vec3f b1, Vec3f b2, Vec3f b3)
 
DYN_FUNC void Swap(T &a, T &b)
 
DYN_FUNC bool isIntrTri2Tet(Vec3f a0, Vec3f a1, Vec3f a2, Vec3f b0, Vec3f b1, Vec3f b2, Vec3f b3)
 
DYN_FUNC int intrPolyWithLine(float *t, int n, Vec2f *p, Vec2f a0, Vec2f a1)
 
DYN_FUNC int intrPolyWithTri(Vec3f *q, int n, Vec3f *p, Vec3f a0, Vec3f a1, Vec3f a2)
 
DYN_FUNC Vec3f Cross(Vec3f const &U, Vec3f const &V)
 
DYN_FUNC int intrTriWithPlane(Vec3f *q, Vec3f oA, Vec3f nA, Vec3f b0, Vec3f b1, Vec3f b2)
 
DYN_FUNC int getIntersection(float &t0, float &t1, Vec2f a0, Vec2f a1, Vec2f b0, Vec2f b1, Vec2f b2)
 
DYN_FUNC bool isOverLap(float &c0, float &c1, float a0, float a1, float b0, float b1)
 
DYN_FUNC float getOverLapBoxAreaInPoly2D(Vec3f face_n, int n_a, Vec3f *a, int n_b, Vec3f *b)
 
DYN_FUNC bool Sign(Vec3f const &n, Vec3f const &p0, Vec3f const &p1, Vec3f const &p2)
 
DYN_FUNC Vec2f Perp(Vec2f const &v)
 
DYN_FUNC Vec3f getDirectionVF(Vec3f p, Vec3f a0, Vec3f a1, Vec3f a2)
 
DYN_FUNC float Dot(Vec3f const &U, Vec3f const &V)
 
DYN_FUNC float getVolume(Vec3f b0, Vec3f b1, Vec3f b2, Vec3f b3)
 
DYN_FUNC bool getBarycentric(Vec3f &bary, Vec3f p, Vec3f b0, Vec3f b1, Vec3f b2, Vec3f b3)
 
This is an implementation of AdditiveCCD based on peridyno.
 
DYN_FUNC int sign(T const &a, T const EPS=EPSILON)
 
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 > sqrt(const Complex< Real > &)