| 1 |
/************************************************************************* |
| 2 |
* * |
| 3 |
* Vega FEM Simulation Library Version 3.1 * |
| 4 |
* * |
| 5 |
* "mesher" library , Copyright (C) 2016 USC * |
| 6 |
* All rights reserved. * |
| 7 |
* * |
| 8 |
* Code authors: Danyong Zhao, Yijing Li, Jernej Barbic * |
| 9 |
* http://www.jernejbarbic.com/code * |
| 10 |
* * |
| 11 |
* Funding: National Science Foundation * |
| 12 |
* * |
| 13 |
* This library is free software; you can redistribute it and/or * |
| 14 |
* modify it under the terms of the BSD-style license that is * |
| 15 |
* included with this library in the file LICENSE.txt * |
| 16 |
* * |
| 17 |
* This library is distributed in the hope that it will be useful, * |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 20 |
* LICENSE.TXT for more details. * |
| 21 |
* * |
| 22 |
*************************************************************************/ |
| 23 |
|
| 24 |
#include "triangleTetIntersection.h" |
| 25 |
|
| 26 |
bool TriangleTetIntersection::tetrahedronIntersectTriangle(const Vec3d & v1, const Vec3d & v2, const Vec3d & v3, const Vec3d & v4, const Vec3d & t1, const Vec3d & t2, const Vec3d & t3) |
| 27 |
{ |
| 28 |
Tetrahedron tet(v1, v2, v3, v4); |
| 29 |
|
| 30 |
// If any vertex of a triangle is in the tetrahedron, intersect. |
| 31 |
if (tet.contains(t1) || tet.contains(t2) || tet.contains(t3)) |
| 32 |
return true; |
| 33 |
Triangle triangle(t1, t2, t3); |
| 34 |
|
| 35 |
// If any edge of a tetrahedron intersects the triangle, intersect |
| 36 |
bool intersect = triangle.intersect(v2, v1) || triangle.intersect(v1, v3) || triangle.intersect(v1, v4) || triangle.intersect(v2, v3) || triangle.intersect(v2, v4) || triangle.intersect(v3, v4); |
| 37 |
if (intersect) |
| 38 |
return true; |
| 39 |
|
| 40 |
// If any edge of a triangle intersects any face of the tetrahedron, intersect |
| 41 |
Triangle triangle1(v1, v2, v3), triangle2(v1, v2, v4), triangle3(v1, v3, v4), triangle4(v2, v3, v4); |
| 42 |
if (triangle1.intersect(t1, t2) || triangle1.intersect(t1, t3) || triangle1.intersect(t2, t3)) |
| 43 |
return true; |
| 44 |
if (triangle2.intersect(t1, t2) || triangle2.intersect(t1, t3) || triangle2.intersect(t2, t3)) |
| 45 |
return true; |
| 46 |
if (triangle3.intersect(t1, t2) || triangle3.intersect(t1, t3) || triangle3.intersect(t2, t3)) |
| 47 |
return true; |
| 48 |
if (triangle4.intersect(t1, t2) || triangle4.intersect(t1, t3) || triangle4.intersect(t2, t3)) |
| 49 |
return true; |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
TriangleTetIntersection::Tetrahedron::Tetrahedron(const Vec3d & v0_, const Vec3d & v1_, const Vec3d & v2_, const Vec3d & v3_) |
| 54 |
: v0(v0_), v1(v1_), v2(v2_), v3(v3_) |
| 55 |
{ |
| 56 |
static const Vec3d ZERO = Vec3d(0, 0, 0); |
| 57 |
n0 = cross(v2 - v1, v3 - v1); |
| 58 |
d0 = dot(n0, v1); |
| 59 |
n1 = cross(v2 - v0, v3 - v0); |
| 60 |
d1 = dot(n1, v0); |
| 61 |
n2 = cross(v1 - v0, v3 - v0); |
| 62 |
d2 = dot(n2, v0); |
| 63 |
n3 = cross(v2 - v0, v1 - v0); |
| 64 |
d3 = dot(n3, v0); |
| 65 |
if (dot(n0, v0) < d0) |
| 66 |
{ |
| 67 |
n0 = ZERO - n0; |
| 68 |
d0 = -d0; |
| 69 |
} |
| 70 |
if (dot(n1, v1) < d1) |
| 71 |
{ |
| 72 |
n1 = ZERO - n1; |
| 73 |
d1 = -d1; |
| 74 |
} |
| 75 |
if (dot(n2, v2) < d2) |
| 76 |
{ |
| 77 |
n2 = ZERO - n2; |
| 78 |
d2 = -d2; |
| 79 |
} |
| 80 |
if (dot(n3, v3) < d3) |
| 81 |
{ |
| 82 |
n3 = ZERO - n3; |
| 83 |
d3 = -d3; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
bool TriangleTetIntersection::Tetrahedron::contains(const Vec3d & p) |
| 88 |
{ |
| 89 |
// cout << dot(p, n0) << " " << dot(p, n1) << " " << dot(p, n2) << " " << dot(p, n3) << "\n"; |
| 90 |
bool ret = dot(p, n0) > d0 + 1e-10 && dot(p, n1) > d1 + 1e-10 && dot(p, n2) > d2 + 1e-10 && dot(p, n3) > d3 + 1e-10; |
| 91 |
//printf("%d\n", ret); |
| 92 |
return ret; |
| 93 |
} |
| 94 |
|
| 95 |
TriangleTetIntersection::Triangle::Triangle(const Vec3d & v0_, const Vec3d & v1_, const Vec3d & v2_) |
| 96 |
: v0(v0_), v1(v1_), v2(v2_) |
| 97 |
{ |
| 98 |
base1 = v1 - v0; |
| 99 |
base2 = v2 - v0; |
| 100 |
n = cross(base1, base2); |
| 101 |
n.normalize(); |
| 102 |
d = dot(n, v0); |
| 103 |
} |
| 104 |
|
| 105 |
bool TriangleTetIntersection::Triangle::contains(const Vec3d & p) |
| 106 |
{ |
| 107 |
double d0 = (base1[1] * base2[2] - base1[2] * base2[1]); |
| 108 |
double d1 = (base1[0] * base2[2] - base1[2] * base2[0]); |
| 109 |
double d2 = (base1[0] * base2[1] - base1[1] * base2[0]); |
| 110 |
Vec3d v = p - v0; |
| 111 |
double t1, t2; |
| 112 |
if (fabs(d2) > fabs(d1) && fabs(d2) > fabs(d0)) |
| 113 |
{ |
| 114 |
t1 = (v[0] * base2[1] - v[1] * base2[0]) / d2; |
| 115 |
t2 = (v[1] * base1[0] - v[0] * base1[1]) / d2; |
| 116 |
} |
| 117 |
else if (fabs(d1) > fabs(d0)) |
| 118 |
{ |
| 119 |
t1 = (v[0] * base2[2] - v[2] * base2[0]) / d1; |
| 120 |
t2 = (v[2] * base1[0] - v[0] * base1[2]) / d1; |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
t1 = (v[1] * base2[2] - v[2] * base2[1]) / d0; |
| 125 |
t2 = (v[2] * base1[1] - v[1] * base1[2]) / d0; |
| 126 |
} |
| 127 |
return t1 >= 1e-10 && t2 >= 1e-10 && t1 + t2 <= 1 - 1e-10; |
| 128 |
} |
| 129 |
|
| 130 |
bool TriangleTetIntersection::Triangle::intersect(const Vec3d & l0, const Vec3d & l1) |
| 131 |
{ |
| 132 |
double t = (d - dot(n, l0)) / dot(n, l1 - l0); |
| 133 |
if (!(t > 1e-10 && t < 1 - 1e-10)) |
| 134 |
return false; |
| 135 |
return contains(l0 * (1 - t) + l1 * t); |
| 136 |
} |
| 137 |
|