Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/objMesh/tribox3.cpp

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code authors: Somya Sharma, Jernej Barbic *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 #include <stdio.h>
30 #include <math.h>
31 #include <iostream>
32 #include <algorithm>
33 #include "matrixMultiplyMacros.h"
34 using namespace std;
35
36 /*
37 Tests if a 3D triangle overlaps with a 3D box.
38
39 INPUT: center of box, box half sizes (in each of the three dimensions), and the three triangle vertices v0, v1, v2.
40 OUTPUT: whether triangle intersects the box or not
41 Note: all entries in boxHalfSize must be >= 0.
42 Note: lower-left-front corner is boxCenter - boxHalfSize, upper-right-back corner is boxCenter + boxHalfSize.
43 */
44
45 bool triBoxOverlap(const double boxcenter[3], const double boxhalfsize[3], const double trivert0[3], const double trivert1[3], const double trivert2[3])
46 {
47 // translate so that box's center coincides with the origin
48 double triverts[3][3];
49 VECTOR_SUBTRACT3(trivert0, boxcenter, triverts[0]);
50 VECTOR_SUBTRACT3(trivert1, boxcenter, triverts[1]);
51 VECTOR_SUBTRACT3(trivert2, boxcenter, triverts[2]);
52
53 // test AABB against minimal AABB around the triangle
54 // 3 tests
55
56 double minx = min( min(triverts[0][0], triverts[1][0]), triverts[2][0] );
57 double miny = min( min(triverts[0][1], triverts[1][1]), triverts[2][1] );
58 double minz = min( min(triverts[0][2], triverts[1][2]), triverts[2][2] );
59 double maxx = max( max(triverts[0][0], triverts[1][0]), triverts[2][0] );
60 double maxy = max( max(triverts[0][1], triverts[1][1]), triverts[2][1] );
61 double maxz = max( max(triverts[0][2], triverts[1][2]), triverts[2][2] );
62
63 if ( ( boxhalfsize[0] < minx ) || ( maxx < -boxhalfsize[0] ) || ( boxhalfsize[1] < miny ) || ( maxy < -boxhalfsize[1] ) || ( boxhalfsize[2] < minz ) || ( maxz < -boxhalfsize[2] ) )
64 return false; //no overlap
65
66 // normals of AABB at origin
67
68 double e0[3] = { 1, 0, 0 };
69 double e1[3] = { 0, 1, 0 };
70 double e2[3] = { 0, 0, 1 };
71
72 double f0[3];
73 double f1[3];
74 double f2[3];
75
76 VECTOR_SUBTRACT3(triverts[1], triverts[0], f0);
77 VECTOR_SUBTRACT3(triverts[2], triverts[1], f1);
78 VECTOR_SUBTRACT3(triverts[0], triverts[2], f2);
79
80 double * e[3] = { e0, e1, e2 };
81 double * f[3] = { f0, f1, f2 };
82
83 // aij = ej X fj
84 // 9 tests
85 double a[3];
86 double p0, p1, p2;
87 double radius;
88 #define LOOP1(i,j)\
89 {\
90 VECTOR_CROSS_PRODUCT(e[i], f[j], a);\
91 p0 = VECTOR_DOT_PRODUCT3(a, triverts[0]);\
92 p1 = VECTOR_DOT_PRODUCT3(a, triverts[1]);\
93 p2 = VECTOR_DOT_PRODUCT3(a, triverts[2]);\
94 radius = ( boxhalfsize[0] * fabs(a[0]) ) + ( boxhalfsize[1] * fabs(a[1]) ) + ( boxhalfsize[2] * fabs(a[2]) );\
95 if ( (min( min(p0, p1), p2 ) > radius) || (max( max(p0, p1), p2 ) < -radius ) )\
96 return false;\
97 }
98
99 LOOP1(0, 0);
100 LOOP1(0, 1);
101 LOOP1(0, 2);
102 LOOP1(1, 0);
103 LOOP1(1, 1);
104 LOOP1(1, 2);
105 LOOP1(2, 0);
106 LOOP1(2, 1);
107 LOOP1(2, 2);
108 #undef LOOP1
109
110 /*
111 // aij = ej X fj
112 // 9 tests
113 for ( int i = 0; i <= 2; i ++ )
114 {
115 for ( int j = 0; j <= 2; j ++ )
116 {
117 double a[3];
118 VECTOR_CROSS_PRODUCT(e[i], f[j], a);
119 double p0 = VECTOR_DOT_PRODUCT3(a, triverts[0]);
120 double p1 = VECTOR_DOT_PRODUCT3(a, triverts[1]);
121 double p2 = VECTOR_DOT_PRODUCT3(a, triverts[2]);
122
123 double radius = ( boxhalfsize[0] * fabs(a[0]) ) + ( boxhalfsize[1] * fabs(a[1]) ) + ( boxhalfsize[2] * fabs(a[2]) );
124
125 if ( (min( min(p0, p1), p2 ) > radius) || (max( max(p0, p1), p2 ) < -radius ) )
126 {
127 overlap = false;
128 return overlap;
129 }
130 }
131 }
132 */
133
134 // plane and AABB overlap test
135 // 1 test
136
137 // triangle normal
138 double normal[3];
139 VECTOR_CROSS_PRODUCT(f0, f1, normal);
140 // no need to normalize the normal as we only check the sign of expressions in the calculations that depend on normal
141 // this also makes the code robust against degenerate triangles with zero-length normals
142 //double len2 = normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2];
143 //double invlen = 1.0 / sqrt(len2);
144 //normal[0] *= invlen;
145 //normal[1] *= invlen;
146 //normal[2] *= invlen;
147
148 // distance of plane from the origin
149 double planeDist = -VECTOR_DOT_PRODUCT3(normal, triverts[0]);
150
151 // get nearest and farthest corners
152 double nearestPoint[3];
153 double farthestPoint[3];
154
155 #define LOOP2(i)\
156 if ( normal[i] < 0 )\
157 {\
158 nearestPoint[i] = boxhalfsize[i];\
159 farthestPoint[i] = - boxhalfsize[i];\
160 }\
161 else\
162 {\
163 nearestPoint[i] = - boxhalfsize[i];\
164 farthestPoint[i] = boxhalfsize[i];\
165 }\
166
167 LOOP2(0);
168 LOOP2(1);
169 LOOP2(2);
170 #undef LOOP2
171
172 /*
173 for ( int i = 0; i <= 2; i++ )
174 {
175 if ( n[i] < 0 )
176 {
177 nearestPoint[i] = boxhalfsize[i];
178 farthestPoint[i] = - boxhalfsize[i];
179 }
180 else
181 {
182 nearestPoint[i] = - boxhalfsize[i];
183 farthestPoint[i] = boxhalfsize[i];
184 }
185 }
186 */
187
188 if ( VECTOR_DOT_PRODUCT3(normal, nearestPoint) + planeDist > 0 )
189 return false; // no overlap
190
191 bool overlap = ( VECTOR_DOT_PRODUCT3(normal, farthestPoint) + planeDist >= 0 );
192 return overlap;
193 }
194