VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
GbMeshTools3D.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef GBMESHTOOLS3D_H
35#define GBMESHTOOLS3D_H
36
37#include <iostream>
38#include <sstream>
39#include <vector>
40
42
44{
45inline int planeBoxOverlap(float normal[3], float vert[3], float maxbox[3]) // -NJMP-
46{
47 int q;
48 float vmin[3], vmax[3], v;
49
50 for (q = 0; q <= 2; q++) {
51 v = vert[q]; // -NJMP-
52 if (normal[q] > 0.0f) {
53 vmin[q] = -maxbox[q] - v; // -NJMP-
54 vmax[q] = maxbox[q] - v; // -NJMP-
55 } else {
56 vmin[q] = maxbox[q] - v; // -NJMP-
57 vmax[q] = -maxbox[q] - v; // -NJMP-
58 }
59 }
60 if ((normal[0] * vmin[0] + normal[1] * vmin[1] + normal[2] * vmin[2]) > 0.0f)
61 return 0; // -NJMP-
62 if ((normal[0] * vmax[0] + normal[1] * vmax[1] + normal[2] * vmax[2]) >= 0.0f)
63 return 1; // -NJMP-
64 return 0;
65}
66
67// Testet auf schnittpunkt Box <-> Dreieck
68// boxcenter = Mittelpunkt der Box
69// boxhalfsize = Halbe Laenge/Hoehe/Breite der Box
70// triverts = Koordinaten der Deickspunkte
71inline int triBoxOverlap(float boxcenter[3], float boxhalfsize[3], float triverts[3][3])
72{
73 /* use separating axis theorem to test overlap between triangle and box */
74 /* need to test for overlap in these directions: */
75 /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
76 /* we do not even need to test these) */
77 /* 2) normal of the triangle */
78 /* 3) crossproduct(edge from tri, {x,y,z}-directin) */
79 /* this gives 3x3=9 more tests */
80
81 float v0[3], v1[3], v2[3];
82
83 // float axis[3];
84
85 float min, max, p0, p1, p2, rad, fex, fey, fez; // -NJMP- "d" local variable removed
86 float normal[3], e0[3], e1[3], e2[3];
87
88 /* This is the fastest branch on Sun */
89 /* move everything so that the boxcenter is in (0,0,0) */
90 // SUB(v0,triverts[0],boxcenter);
91 //#define SUB(dest,v1,v2)
92 v0[0] = triverts[0][0] - boxcenter[0];
93 v0[1] = triverts[0][1] - boxcenter[1];
94 v0[2] = triverts[0][2] - boxcenter[2];
95
96 // SUB(v1,triverts[1],boxcenter);
97 //#define SUB(dest,v1,v2)
98 v1[0] = triverts[1][0] - boxcenter[0];
99 v1[1] = triverts[1][1] - boxcenter[1];
100 v1[2] = triverts[1][2] - boxcenter[2];
101
102 // SUB(v2,triverts[2],boxcenter);
103 //#define SUB(dest,v1,v2)
104 v2[0] = triverts[2][0] - boxcenter[0];
105 v2[1] = triverts[2][1] - boxcenter[1];
106 v2[2] = triverts[2][2] - boxcenter[2];
107
108 /* compute triangle edges */
109 // SUB(e0,v1,v0); /* tri edge 0 */
110 //#define SUB(dest,v1,v2)
111 e0[0] = v1[0] - v0[0];
112 e0[1] = v1[1] - v0[1];
113 e0[2] = v1[2] - v0[2];
114
115 // SUB(e1,v2,v1); /* tri edge 1 */
116 //#define SUB(dest,v1,v2)
117 e1[0] = v2[0] - v1[0];
118 e1[1] = v2[1] - v1[1];
119 e1[2] = v2[2] - v1[2];
120
121 // SUB(e2,v0,v2); /* tri edge 2 */
122 //#define SUB(dest,v1,v2)
123 e2[0] = v0[0] - v2[0];
124 e2[1] = v0[1] - v2[1];
125 e2[2] = v0[2] - v2[2];
126
127 /* Bullet 3: */
128 /* test the 9 tests first (this was faster) */
129 fex = fabsf(e0[0]);
130 fey = fabsf(e0[1]);
131 fez = fabsf(e0[2]);
132
133 // AXISTEST_X01(e0[2], e0[1], fez, fey);
134 //#define AXISTEST_X01(a, b, fa, fb)
135 p0 = e0[2] * v0[1] - e0[1] * v0[2];
136 p2 = e0[2] * v2[1] - e0[1] * v2[2];
137 if (p0 < p2) {
138 min = p0;
139 max = p2;
140 } else {
141 min = p2;
142 max = p0;
143 }
144 rad = fez * boxhalfsize[1] + fey * boxhalfsize[2];
145 if (min > rad || max < -rad)
146 return 0;
147
148 // AXISTEST_Y02(e0[2], e0[0], fez, fex);
149 //#define AXISTEST_Y02(a, b, fa, fb)
150 p0 = -e0[2] * v0[0] + e0[0] * v0[2];
151 p2 = -e0[2] * v2[0] + e0[0] * v2[2];
152 if (p0 < p2) {
153 min = p0;
154 max = p2;
155 } else {
156 min = p2;
157 max = p0;
158 }
159 rad = fez * boxhalfsize[0] + fex * boxhalfsize[2];
160 if (min > rad || max < -rad)
161 return 0;
162
163 // AXISTEST_Z12(e0[1], e0[0], fey, fex);
164 //#define AXISTEST_Z12(a, b, fa, fb)
165 p1 = e0[1] * v1[0] - e0[0] * v1[1];
166 p2 = e0[1] * v2[0] - e0[0] * v2[1];
167 if (p2 < p1) {
168 min = p2;
169 max = p1;
170 } else {
171 min = p1;
172 max = p2;
173 }
174 rad = fey * boxhalfsize[0] + fex * boxhalfsize[1];
175 if (min > rad || max < -rad)
176 return 0;
177
178 fex = fabsf(e1[0]);
179 fey = fabsf(e1[1]);
180 fez = fabsf(e1[2]);
181
182 // AXISTEST_X01(e1[2], e1[1], fez, fey);
183 //#define AXISTEST_X01(a, b, fa, fb)
184 p0 = e1[2] * v0[1] - e1[1] * v0[2];
185 p2 = e1[2] * v2[1] - e1[1] * v2[2];
186 if (p0 < p2) {
187 min = p0;
188 max = p2;
189 } else {
190 min = p2;
191 max = p0;
192 }
193 rad = fez * boxhalfsize[1] + fey * boxhalfsize[2];
194 if (min > rad || max < -rad)
195 return 0;
196
197 // AXISTEST_Y02(e1[2], e1[0], fez, fex);
198 //#define AXISTEST_Y02(a, b, fa, fb)
199 p0 = -e1[2] * v0[0] + e1[0] * v0[2];
200 p2 = -e1[2] * v2[0] + e1[0] * v2[2];
201 if (p0 < p2) {
202 min = p0;
203 max = p2;
204 } else {
205 min = p2;
206 max = p0;
207 }
208 rad = fez * boxhalfsize[0] + fex * boxhalfsize[2];
209 if (min > rad || max < -rad)
210 return 0;
211
212 // AXISTEST_Z0(e1[1], e1[0], fey, fex);
213 //#define AXISTEST_Z0(a, b, fa, fb)
214 p0 = e1[1] * v0[0] - e1[0] * v0[1];
215 p1 = e1[1] * v1[0] - e1[0] * v1[1];
216 if (p0 < p1) {
217 min = p0;
218 max = p1;
219 } else {
220 min = p1;
221 max = p0;
222 }
223 rad = fey * boxhalfsize[0] + fex * boxhalfsize[2];
224 if (min > rad || max < -rad)
225 return 0;
226
227 fex = fabsf(e2[0]);
228 fey = fabsf(e2[1]);
229 fez = fabsf(e2[2]);
230 // AXISTEST_X2(e2[2], e2[1], fez, fey);
231 //#define AXISTEST_X2(a, b, fa, fb)
232 p0 = e2[2] * v0[1] - e2[1] * v0[2];
233 p1 = e2[2] * v1[1] - e2[1] * v1[2];
234 if (p0 < p1) {
235 min = p0;
236 max = p1;
237 } else {
238 min = p1;
239 max = p0;
240 }
241 rad = fez * boxhalfsize[1] + fey * boxhalfsize[2];
242 if (min > rad || max < -rad)
243 return 0;
244
245 // AXISTEST_Y1(e2[2], e2[0], fez, fex);
246 //#define AXISTEST_Y1(a, b, fa, fb)
247 p0 = -e2[2] * v0[0] + e2[0] * v0[2];
248 p1 = -e2[2] * v1[0] + e2[0] * v1[2];
249 if (p0 < p1) {
250 min = p0;
251 max = p1;
252 } else {
253 min = p1;
254 max = p0;
255 }
256 rad = fez * boxhalfsize[0] + fex * boxhalfsize[2];
257 if (min > rad || max < -rad)
258 return 0;
259
260 // AXISTEST_Z12(e2[1], e2[0], fey, fex);
261 //#define AXISTEST_Z12(a, b, fa, fb)
262 p1 = e2[1] * v1[0] - e2[0] * v1[1];
263 p2 = e2[1] * v2[0] - e2[0] * v2[1];
264 if (p2 < p1) {
265 min = p2;
266 max = p1;
267 } else {
268 min = p1;
269 max = p2;
270 }
271 rad = fey * boxhalfsize[0] + fex * boxhalfsize[1];
272 if (min > rad || max < -rad)
273 return 0;
274
275 /* Bullet 1: */
276 /* first test overlap in the {x,y,z}-directions */
277 /* find min, max of the triangle each direction, and test for overlap in */
278 /* that direction -- this is equivalent to testing a minimal AABB around */
279 /* the triangle against the AABB */
280 /* test in X-direction */
281 // FINDMINMAX(v0[0],v1[0],v2[0],min,max);
282 min = (float)ub_math::min(v0[0], v1[0], v2[0]);
283 max = (float)ub_math::max(v0[0], v1[0], v2[0]);
284 if (min > boxhalfsize[0] || max < -boxhalfsize[0])
285 return 0;
286
287 /* test in Y-direction */
288 // FINDMINMAX(v0[1],v1[1],v2[1],min,max);
289 min = (float)ub_math::min(v0[1], v1[1], v2[1]);
290 max = (float)ub_math::max(v0[1], v1[1], v2[1]);
291 if (min > boxhalfsize[1] || max < -boxhalfsize[1])
292 return 0;
293
294 /* test in Z-direction */
295 // FINDMINMAX(v0[2],v1[2],v2[2],min,max);
296 min = (float)ub_math::min(v0[2], v1[2], v2[2]);
297 max = (float)ub_math::max(v0[2], v1[2], v2[2]);
298
299 if (min > boxhalfsize[2] || max < -boxhalfsize[2])
300 return 0;
301
302 /* Bullet 2: */
303 /* test if the box intersects the plane of the triangle */
304 /* compute plane equation of triangle: normal*x+d=0 */
305 // CROSS(normal,e0,e1);
306 //#define CROSS(dest,v1,v2)
307 normal[0] = e0[1] * e1[2] - e0[2] * e1[1];
308 normal[1] = e0[2] * e1[0] - e0[0] * e1[2];
309 normal[2] = e0[0] * e1[1] - e0[1] * e1[0];
310
311 // -NJMP- (line removed here)
312 if (!planeBoxOverlap(normal, v0, boxhalfsize))
313 return 0; // -NJMP-
314 return 1; /* box and triangle overlaps */
315}
316
317} // namespace gb_mesh_tools_3d
318
319#endif
320
321// original - NICHT LOESCHEN - von kroete lynn
322
323//#define X 0
324//#define Y 1
325//#define Z 2
326//
327//#define CROSS(dest,v1,v2)
328// dest[0]=v1[1]*v2[2]-v1[2]*v2[1];
329// dest[1]=v1[2]*v2[0]-v1[0]*v2[2];
330// dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
331//
332//#define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
333//
334//#define SUB(dest,v1,v2)
335// dest[0]=v1[0]-v2[0];
336// dest[1]=v1[1]-v2[1];
337// dest[2]=v1[2]-v2[2];
338//
339//#define FINDMINMAX(x0,x1,x2,min,max)
340// min = max = x0;
341// if(x1<min) min=x1;
342// if(x1>max) max=x1;
343// if(x2<min) min=x2;
344// if(x2>max) max=x2;
345//
346// int planeBoxOverlap(float normal[3], float vert[3], float maxbox[3]) // -NJMP-
347//{
348// int q;
349// float vmin[3],vmax[3],v;
350//
351// for(q=X;q<=Z;q++)
352// {
353// v=vert[q]; // -NJMP-
354// if(normal[q]>0.0f)
355// {
356// vmin[q]=-maxbox[q] - v; // -NJMP-
357// vmax[q]= maxbox[q] - v; // -NJMP-
358// }
359// else
360// {
361// vmin[q]= maxbox[q] - v; // -NJMP-
362// vmax[q]=-maxbox[q] - v; // -NJMP-
363// }
364// }
365// if(DOT(normal,vmin)>0.0f) return 0; // -NJMP-
366// if(DOT(normal,vmax)>=0.0f) return 1; // -NJMP-
367// return 0;
368//}
369//
371//
372//#define AXISTEST_X01(a, b, fa, fb)
373// p0 = a*v0[Y] - b*v0[Z];
374// p2 = a*v2[Y] - b*v2[Z];
375// if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
376// rad = fa * boxhalfsize[Y] + fb * boxhalfsize[Z];
377// if(min>rad || max<-rad) return 0;
378//
379//#define AXISTEST_X2(a, b, fa, fb)
380// p0 = a*v0[Y] - b*v0[Z];
381// p1 = a*v1[Y] - b*v1[Z];
382// if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
383// rad = fa * boxhalfsize[Y] + fb * boxhalfsize[Z];
384// if(min>rad || max<-rad) return 0;
385//
386//
387//
389//
390//#define AXISTEST_Y02(a, b, fa, fb)
391// p0 = -a*v0[X] + b*v0[Z];
392// p2 = -a*v2[X] + b*v2[Z];
393// if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
394// rad = fa * boxhalfsize[X] + fb * boxhalfsize[Z];
395// if(min>rad || max<-rad) return 0;
396//
397//#define AXISTEST_Y1(a, b, fa, fb)
398// p0 = -a*v0[X] + b*v0[Z];
399// p1 = -a*v1[X] + b*v1[Z];
400// if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
401// rad = fa * boxhalfsize[X] + fb * boxhalfsize[Z];
402// if(min>rad || max<-rad) return 0;
403//
404//======================== Z-tests ========================
405//
406//#define AXISTEST_Z12(a, b, fa, fb)
407// p1 = a*v1[X] - b*v1[Y];
408// p2 = a*v2[X] - b*v2[Y];
409// if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;}
410// rad = fa * boxhalfsize[X] + fb * boxhalfsize[Y];
411// if(min>rad || max<-rad) return 0;
412//
413//#define AXISTEST_Z0(a, b, fa, fb)
414// p0 = a*v0[X] - b*v0[Y];
415// p1 = a*v1[X] - b*v1[Y];
416// if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
417// rad = fa * boxhalfsize[X] + fb * boxhalfsize[Y];
418// if(min>rad || max<-rad) return 0;
419//
420// int triBoxOverlap(float boxcenter[3],float boxhalfsize[3],float triverts[3][3])
421//{
422// //use separating axis theorem to test overlap between triangle and box
423// //need to test for overlap in these directions:
424// //1) the {x,y,z}-directions (actually, since we use the AABB of the triangle
425// // we do not even need to test these)
426// //2) normal of the triangle
427// //3) crossproduct(edge from tri, {x,y,z}-directin)
428// // this gives 3x3=9 more tests
429//
430// float v0[3],v1[3],v2[3];
431//
432// // float axis[3];
433//
434// float min,max,p0,p1,p2,rad,fex,fey,fez; // -NJMP- "d" local variable removed
435// float normal[3],e0[3],e1[3],e2[3];
436//
437// /* This is the fastest branch on Sun */
438// /* move everything so that the boxcenter is in (0,0,0) */
439// SUB(v0,triverts[0],boxcenter);
440// SUB(v1,triverts[1],boxcenter);
441// SUB(v2,triverts[2],boxcenter);
442//
443// /* compute triangle edges */
444// SUB(e0,v1,v0); /* tri edge 0 */
445// SUB(e1,v2,v1); /* tri edge 1 */
446// SUB(e2,v0,v2); /* tri edge 2 */
447//
448// /* Bullet 3: */
449// /* test the 9 tests first (this was faster) */
450// fex = fabsf(e0[X]);
451// fey = fabsf(e0[Y]);
452// fez = fabsf(e0[Z]);
453//
454// AXISTEST_X01(e0[Z], e0[Y], fez, fey);
455// AXISTEST_Y02(e0[Z], e0[X], fez, fex);
456// AXISTEST_Z12(e0[Y], e0[X], fey, fex);
457// fex = fabsf(e1[X]);
458// fey = fabsf(e1[Y]);
459// fez = fabsf(e1[Z]);
460//
461// AXISTEST_X01(e1[Z], e1[Y], fez, fey);
462// AXISTEST_Y02(e1[Z], e1[X], fez, fex);
463// AXISTEST_Z0(e1[Y], e1[X], fey, fex);
464//
465// fex = fabsf(e2[X]);
466// fey = fabsf(e2[Y]);
467// fez = fabsf(e2[Z]);
468// AXISTEST_X2(e2[Z], e2[Y], fez, fey);
469// AXISTEST_Y1(e2[Z], e2[X], fez, fex);
470// AXISTEST_Z12(e2[Y], e2[X], fey, fex);
471//
472// /* Bullet 1: */
473// /* first test overlap in the {x,y,z}-directions */
474// /* find min, max of the triangle each direction, and test for overlap in */
475// /* that direction -- this is equivalent to testing a minimal AABB around */
476// /* the triangle against the AABB */
477// /* test in X-direction */
478// FINDMINMAX(v0[X],v1[X],v2[X],min,max);
479// if(min>boxhalfsize[X] || max<-boxhalfsize[X]) return 0;
480//
481// /* test in Y-direction */
482// FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max);
483// if(min>boxhalfsize[Y] || max<-boxhalfsize[Y]) return 0;
484//
485// /* test in Z-direction */
486// FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max);
487// if(min>boxhalfsize[Z] || max<-boxhalfsize[Z]) return 0;
488//
489// /* Bullet 2: */
490// /* test if the box intersects the plane of the triangle */
491// /* compute plane equation of triangle: normal*x+d=0 */
492// CROSS(normal,e0,e1);
493//
494// // -NJMP- (line removed here)
495// if(!planeBoxOverlap(normal,v0,boxhalfsize)) return 0; // -NJMP-
496// return 1; /* box and triangle overlaps */
497//}
498
std::shared_ptr< T > SPtr
int triBoxOverlap(float boxcenter[3], float boxhalfsize[3], float triverts[3][3])
int planeBoxOverlap(float normal[3], float vert[3], float maxbox[3])
const T & min(const T &a1, const T &a2)
Definition UbMath.h:389
const T & max(const T &a1, const T &a2)
Definition UbMath.h:371