VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
Triangle.cpp
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#include "Triangle.h"
35
36#include <cmath>
37
38#include "utilities/math/Math.h"
39
40#include "grid/NodeValues.h"
41
42namespace vf::gpu {
43
44Triangle::Triangle(Vertex &v1, Vertex &v2, Vertex &v3, Vertex &normal) : v1(v1), v2(v2), v3(v3), normal(normal), patchIndex(INVALID_INDEX) {}
45Triangle::Triangle(Vertex &v1, Vertex &v2, Vertex &v3) : v1(v1), v2(v2), v3(v3), patchIndex(INVALID_INDEX) { calcNormal(); }
46
47void Triangle::set(const Vertex &v1, const Vertex &v2, const Vertex &v3)
48{
49 this->v1 = v1;
50 this->v2 = v2;
51 this->v3 = v3;
52 this->calcNormal();
53}
54
55void Triangle::set(int index, Vertex value)
56{
57 if (index == 0)
58 v1 = value;
59 if (index == 1)
60 v2 = value;
61 if (index == 2)
62 v3 = value;
63}
64
66{
67 if (index == 0)
68 return v1;
69 if (index == 1)
70 return v2;
71 if (index == 2)
72 return v3;
73 else
74 return Vertex((real)-999999999999999, (real)-99999999999999, (real)-9999999999999);
75}
76
84
86{
87 this->layerThickness = delta * (std::abs(this->normal.x) + std::abs(this->normal.y) + std::abs(this->normal.z));
88}
89
90
92{
93 real s;
94
95 if (this->isUnterExtendedFace(point, s)) {
96 if (this->isNotNextToFace(point)) {
97 if (this->isUnderAngleToNeighbors(point)) {
98 if (this->isNegativeDirectionBorder(point)) {
99 return NEGATIVE_DIRECTION_BORDER;
100 } else {
101 return INSIDE;
102 }
103 } else {
104 return 4;
105 }
106 } else {
107 return 3;
108 }
109 }
110
111
112 if (this->isQNode(point, s))
113 return Q_DEPRECATED;
114
115 return FLUID;
116}
117
119{
120 s = this->getPerpedicularDistanceFrom(point);
122}
123
125{
126 Vertex v = P - v1;
127 return (v * -1.0f) * normal;
128}
129
134
135bool Triangle::isQNode(const Vertex & point, const real &s) const
136{
137 return (s < 0 && vf::Math::lessEqual(-s, this->layerThickness));
138 //calculateQs(actualPoint, actualTriangle);
139}
140
142{
143 return normal.x < 0.0f || normal.y < 0.0f || normal.z < 0.0f;
144 //return (sVector.x < 0.0f && sVector.y < 0.0f && sVector.z < 0.0f);
145}
146
148{
150
151 Vertex w1 = Pb - v1;
152 Vertex w2 = Pb - v2;
153 Vertex w3 = Pb - v3;
154
155 Vertex t1 = w1.crossProduct(v2 - v1);
156 Vertex t2 = w2.crossProduct(v3 - v2);
157 Vertex t3 = w3.crossProduct(v1 - v3);
158
159 real g1 = t1 * normal;
160 real g2 = t2 * normal;
161 real g3 = t3 * normal;
162
163 return vf::Math::lessEqual(g1, 0.0f) && vf::Math::lessEqual(g2, 0.0f) && vf::Math::lessEqual(g3, 0.0f);
164}
165
167{
168 Vertex Pci[3];
169 this->getClosestPointsOnEdges(Pci, point);
170 Vertex Pb = this->getPerpedicularPointFrom(point);
171
172 Vertex q[3];
173 Vertex r[3];
174
175 real betaAngles[3];
176 for (int i = 0; i < 3; i++)
177 {
178 q[i] = point - Pci[i];
179 r[i] = Pb - Pci[i];
180 betaAngles[i] = q[i].getInnerAngle(r[i]);
181 }
182
183 real eps = EPSILON * 100.0f;
185}
186
188{
189 Vertex Pc1, Pc2, Pc3;
190 Vertex v4 = P - v1;
191 Vertex v5 = P - v2;
192 Vertex v6 = P - v3;
193
194 Vertex d1 = v2 - v1;
195 Vertex d2 = v3 - v2;
196 Vertex d3 = v1 - v3;
197
198 real temp = (v4 * d1) / (d1 * d1);
199 Vertex tempV = d1 * temp;
200 Pc1 = v1 + tempV;
201
202 temp = (v5 * d2) / (d2 * d2);
203 tempV = d2 * temp;
204 Pc2 = v2 + tempV;
205
206 temp = (v6 * d3) / (d3 * d3);
207 tempV = d3 * temp;
208 Pc3 = v3 + tempV;
209
210 arr[0] = Pc1;
211 arr[1] = Pc2;
212 arr[2] = Pc3;
213}
214
216{
217 return (v1 + v2 + v3) * (1.0f / 3.0f);
218}
219
221{
222 if (isEqual(t2)) return 0.0f;
223
224 real alpha = normal.getInnerAngle(t2.normal);
225 if (alpha == 0.0f)
226 return 90.0f;
227
229 return (180.0f + alpha) / 2.0f;
230 else
231 return (180.0f - alpha) / 2.0f;
232}
233
234int Triangle::isEqual(const Triangle &t2) const
235{
236 return getNumberOfCommonEdge(t2) == 3;
237}
238
240{
243
244 Vertex s1s2 = s1 - s2;
245 real X = s1s2 * t2.normal;
246 return X > 0 ? true : false;
247}
248
250{
251 bool edgeOneCommon = false;
252 bool edgeTwoCommon = false;
253 bool edgeThreeCommon = false;
254
258
260 return 0;
261 else if (edgeTwoCommon && edgeThreeCommon)
262 return 1;
263 else if (edgeThreeCommon && edgeOneCommon)
264 return 2;
265 else
266 return -1;
267}
268
269bool Triangle::contains(const Vertex& v) const
270{
271 return (v == v1 || v == v2 || v == v3);
272}
273
274
276{
277 int commonEdge = 0;
278 if (t2.contains(v1))
279 commonEdge++;
280 if (t2.contains(v2))
281 commonEdge++;
282 if (t2.contains(v3))
283 commonEdge++;
284
285 if (commonEdge == 2 || commonEdge == 3) return commonEdge;
286 return 0;
287}
288
289
291{
293 //http://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/moller-trumbore-ray-triangle-intersection
294
295
297 float det, inv_det;
298
299 edge1 = v2 - v1;
300 edge2 = v3 - v1;
301
302 pvec = direction.crossProduct(edge2);
303 det = edge1 * pvec;
304
305 if (det < EPSILON)
306 {
307 if (std::fabs(det) < EPSILON)
308 {
311 if (normalLength > 0.0f)
312 {
313 const real invNormalLength = 1.0f / normalLength;
315
316 const Vertex pointToV1 = P - v1;
318 constexpr real planeTolerance = 1.0e-6f;
319
321 {
322 const Vertex v0 = edge1;
323 const Vertex v1Edge = edge2;
324 const real dot00 = v0 * v0;
325 const real dot01 = v0 * v1Edge;
326 const real dot11 = v1Edge * v1Edge;
327 const real dot02 = v0 * pointToV1;
328 const real dot12 = v1Edge * pointToV1;
329 const real denom = dot00 * dot11 - dot01 * dot01;
330
331 if (vf::Math::greaterEqual(std::fabs(denom), EPSILON))
332 {
333 const real invDenom = 1.0f / denom;
334 const real u = (dot11 * dot02 - dot01 * dot12) * invDenom;
335 const real v = (dot00 * dot12 - dot01 * dot02) * invDenom;
336
339 {
340 pointOnTri = P;
341 qVal = 0.0f;
342 return 0;
343 }
344 }
345 }
346 }
347 }
348
349 return 3;
350 }
351
352 inv_det = 1.0 / det;
353
354 tvec = P - v1;
355 tuv.y = (tvec * pvec) * inv_det;
356
357 if (!vf::Math::greaterEqual(tuv.y, 0.0) || !vf::Math::lessEqual(tuv.y, 1.0))
358 //if (tuv.y < 0.0 || tuv.y > 1.0)
359 return 1;
360
361 qvec = tvec.crossProduct(edge1);
362 tuv.z = (direction * qvec) * inv_det;
363
364 if ( !vf::Math::greaterEqual(tuv.z, 0.0) || !vf::Math::lessEqual((tuv.y + tuv.z), 1.0))
365 //if (tuv.z < 0.0 || (tuv.y + tuv.z) > 1.0)
366 return 2;
367
368 tuv.x = (edge2 * qvec) * inv_det;
369
370 pointOnTri.x = (1.0 - tuv.y - tuv.z) * v1.x + tuv.y * v2.x + tuv.z * v3.x;
371 pointOnTri.y = (1.0 - tuv.y - tuv.z) * v1.y + tuv.y * v2.y + tuv.z * v3.y;
372 pointOnTri.z = (1.0 - tuv.y - tuv.z) * v1.z + tuv.y * v2.z + tuv.z * v3.z;
373
374 qVal = tuv.x;
375
376 return 0;
377}
378
379void Triangle::print() const
380{
381 printf("v1: ");
382 v1.print();
383 printf("v2: ");
384 v2.print();
385 printf("v3: ");
386 v3.print();
387 printf("normal: ");
388 normal.print();
389}
390
392{
393 return v1 == t.v1 && v2 == t.v2 && v3 == t.v3
394 && vf::Math::equal(alphaAngles[0], t.alphaAngles[0]) && vf::Math::equal(alphaAngles[1], t.alphaAngles[1]) && vf::Math::equal(alphaAngles[2], t.alphaAngles[2]);
395}
396
397
398void Triangle::setMinMax(real &minX, real &maxX, real &minY, real &maxY, real &minZ, real &maxZ) const
399{
400 Vertex::setMinMax(minX, maxX, minY, maxY, minZ, maxZ, v1, v2, v3);
401}
402
403}
404
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
#define INVALID_INDEX
Definition DataTypes.h:48
#define EPSILON
Definition Math.h:40
static bool lessEqual(const real &val1, const real &val2, real maxRelDiff=EPSILON)
Definition Math.cpp:52
static bool greaterEqual(const real &val1, const real &val2, real maxRelDiff=EPSILON)
Definition Math.cpp:59
static bool equal(const real &val1, const real &val2, real maxRelDiff=EPSILON)
Definition Math.cpp:40
real alphaAngles[3]
Definition Triangle.h:49
int getTriangleIntersection(const Vertex &P, const Vertex &direction, Vertex &pointOnTri, real &qVal) const
Definition Triangle.cpp:290
Vertex getCenterOfMass() const
Definition Triangle.cpp:215
void getClosestPointsOnEdges(Vertex arr[], const Vertex &P) const
Definition Triangle.cpp:187
real getPerpedicularDistanceFrom(const Vertex &P) const
Definition Triangle.cpp:124
bool isUnderAngleToNeighbors(const Vertex &point) const
Definition Triangle.cpp:166
bool contains(const Vertex &v) const
Definition Triangle.cpp:269
bool operator==(const Triangle &t) const
Definition Triangle.cpp:391
int isEqual(const Triangle &t2) const
Definition Triangle.cpp:234
void initalLayerThickness(real delta)
Definition Triangle.cpp:85
void print() const
Definition Triangle.cpp:379
Vertex get(int index)
Definition Triangle.cpp:65
void set(const Vertex &v1, const Vertex &v2, const Vertex &v3)
Definition Triangle.cpp:47
bool isNegativeDirectionBorder(const Vertex &point) const
Definition Triangle.cpp:141
int getNumberOfCommonEdge(const Triangle &t2) const
Definition Triangle.cpp:275
bool isUnterExtendedFace(const Vertex &point, real &s) const
Definition Triangle.cpp:118
int getCommonEdge(const Triangle &t2) const
Definition Triangle.cpp:249
void setMinMax(real &minX, real &maxX, real &minY, real &maxY, real &minZ, real &maxZ) const
Definition Triangle.cpp:398
real getHalfAngleBetweenToAdjacentTriangle(const Triangle &t2) const
Definition Triangle.cpp:220
bool isNotNextToFace(const Vertex &point) const
Definition Triangle.cpp:147
bool doesNormalsShowToEachOther(const Triangle &t2) const
Definition Triangle.cpp:239
char isUnderFace(const Vertex &point) const
Definition Triangle.cpp:91
Vertex getPerpedicularPointFrom(const Vertex &P) const
Definition Triangle.cpp:130
bool isQNode(const Vertex &point, const real &s) const
Definition Triangle.cpp:135
static void setMinMax(real &minX, real &maxX, real &minY, real &maxY, real &minZ, real &maxZ, const Vertex &v1, const Vertex &v2, const Vertex &v3)
Definition Vertex.cpp:164
real length() const
Definition Vertex.cpp:81
struct Vertex crossProduct(const Vertex &w) const
Definition Vertex.cpp:73
real getInnerAngle(const Vertex &w) const
Definition Vertex.cpp:110
void normalize()
Definition Vertex.cpp:86
void print() const
Definition Vertex.cpp:124