VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
GbVector3D.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 <GbPoint3D.h>
35#include <GbVector3D.h>
36
39
40using namespace std;
41
42const GbVector3D GbVector3D::ZERO(0.0, 0.0, 0.0);
43const GbVector3D GbVector3D::UNIT_X1(1.0, 0.0, 0.0);
44const GbVector3D GbVector3D::UNIT_X2(0.0, 1.0, 0.0);
45const GbVector3D GbVector3D::UNIT_X3(0.0, 0.0, 1.0);
46
47//----------------------------------------------------------------------------
49{
50 m_afTuple[0] = 0.0;
51 m_afTuple[1] = 0.0;
52 m_afTuple[2] = 0.0;
53}
54//----------------------------------------------------------------------------
55GbVector3D::GbVector3D(const double &fX, const double &fY, const double &fZ)
56{
57 m_afTuple[0] = fX;
58 m_afTuple[1] = fY;
59 m_afTuple[2] = fZ;
60}
61//----------------------------------------------------------------------------
63{
64 m_afTuple[0] = rkV.m_afTuple[0];
65 m_afTuple[1] = rkV.m_afTuple[1];
66 m_afTuple[2] = rkV.m_afTuple[2];
67}
68//----------------------------------------------------------------------------
69
71{
72 m_afTuple[0] = point.x1;
73 m_afTuple[1] = point.x2;
74 m_afTuple[2] = point.x3;
75}
76
77//----------------------------------------------------------------------------
79{
80 std::stringstream ss;
81 ss << "GbVector3D[" << m_afTuple[0] << "," << m_afTuple[1] << "," << m_afTuple[2] << "]";
82 ss << endl;
83 return ((ss.str()).c_str());
84}
85//----------------------------------------------------------------------------
86GbVector3D::operator const double *() const { return m_afTuple; }
87//----------------------------------------------------------------------------
88GbVector3D::operator double *() { return m_afTuple; }
89//----------------------------------------------------------------------------
90double GbVector3D::operator[](int i) const
91{
92 assert(0 <= i && i <= 2);
93 if (i < 0)
94 i = 0;
95 else if (i > 2)
96 i = 2;
97
98 return m_afTuple[i];
99}
100//----------------------------------------------------------------------------
102{
103 assert(0 <= i && i <= 2);
104 if (i < 0)
105 i = 0;
106 else if (i > 2)
107 i = 2;
108
109 return m_afTuple[i];
110}
111//----------------------------------------------------------------------------
112double GbVector3D::X1() const { return m_afTuple[0]; }
113//----------------------------------------------------------------------------
114double &GbVector3D::X1() { return m_afTuple[0]; }
115//----------------------------------------------------------------------------
116double GbVector3D::X2() const { return m_afTuple[1]; }
117//----------------------------------------------------------------------------
118double &GbVector3D::X2() { return m_afTuple[1]; }
119//----------------------------------------------------------------------------
120double GbVector3D::X3() const { return m_afTuple[2]; }
121//----------------------------------------------------------------------------
122double &GbVector3D::X3() { return m_afTuple[2]; }
123//----------------------------------------------------------------------------
125{
126 if (this == &rkV)
127 return *this;
128
129 m_afTuple[0] = rkV.m_afTuple[0];
130 m_afTuple[1] = rkV.m_afTuple[1];
131 m_afTuple[2] = rkV.m_afTuple[2];
132 return *this;
133}
134//----------------------------------------------------------------------------
135int GbVector3D::CompareArrays(const GbVector3D &rkV) const
136{
137 return memcmp(m_afTuple, rkV.m_afTuple, 3 * sizeof(double));
138}
139//----------------------------------------------------------------------------
140bool GbVector3D::operator==(const GbVector3D &rkV) const { return CompareArrays(rkV) == 0; }
141//----------------------------------------------------------------------------
142bool GbVector3D::operator!=(const GbVector3D &rkV) const { return CompareArrays(rkV) != 0; }
143//----------------------------------------------------------------------------
144bool GbVector3D::operator<(const GbVector3D &rkV) const { return CompareArrays(rkV) < 0; }
145//----------------------------------------------------------------------------
146bool GbVector3D::operator<=(const GbVector3D &rkV) const { return CompareArrays(rkV) <= 0; }
147//----------------------------------------------------------------------------
148bool GbVector3D::operator>(const GbVector3D &rkV) const { return CompareArrays(rkV) > 0; }
149//----------------------------------------------------------------------------
150bool GbVector3D::operator>=(const GbVector3D &rkV) const { return CompareArrays(rkV) >= 0; }
151//----------------------------------------------------------------------------
153{
154 return GbVector3D(m_afTuple[0] + rkV.m_afTuple[0], m_afTuple[1] + rkV.m_afTuple[1],
155 m_afTuple[2] + rkV.m_afTuple[2]);
156}
157//----------------------------------------------------------------------------
159{
160 return GbVector3D(m_afTuple[0] + vector.m_afTuple[0], m_afTuple[1] + vector.m_afTuple[1],
161 m_afTuple[2] + vector.m_afTuple[2]);
162}
163
164//----------------------------------------------------------------------------
166{
167 return GbVector3D(m_afTuple[0] - rkV.m_afTuple[0], m_afTuple[1] - rkV.m_afTuple[1],
168 m_afTuple[2] - rkV.m_afTuple[2]);
169}
170//----------------------------------------------------------------------------
172{
173 return GbVector3D(m_afTuple[0] - vector.m_afTuple[0], m_afTuple[1] - vector.m_afTuple[1],
174 m_afTuple[2] - vector.m_afTuple[2]);
175}
176//----------------------------------------------------------------------------
178{
179 return GbVector3D(fScalar * m_afTuple[0], fScalar * m_afTuple[1], fScalar * m_afTuple[2]);
180}
181//----------------------------------------------------------------------------
183{
185
186 if (fScalar != 0.0) {
187 double fInvScalar = 1.0 / fScalar;
188 kQuot.m_afTuple[0] = fInvScalar * m_afTuple[0];
189 kQuot.m_afTuple[1] = fInvScalar * m_afTuple[1];
190 kQuot.m_afTuple[2] = fInvScalar * m_afTuple[2];
191 } else {
192 kQuot.m_afTuple[0] = ub_math::inf;
193 kQuot.m_afTuple[1] = ub_math::inf;
194 kQuot.m_afTuple[2] = ub_math::inf;
195 }
196
197 return kQuot;
198}
199//----------------------------------------------------------------------------
200GbVector3D GbVector3D::operator-() const { return GbVector3D(-m_afTuple[0], -m_afTuple[1], -m_afTuple[2]); }
201//----------------------------------------------------------------------------
203{
204 return GbVector3D(fScalar * rkV[0], fScalar * rkV[1], fScalar * rkV[2]);
205}
206//----------------------------------------------------------------------------
208{
209 m_afTuple[0] += rkV.m_afTuple[0];
210 m_afTuple[1] += rkV.m_afTuple[1];
211 m_afTuple[2] += rkV.m_afTuple[2];
212 return *this;
213}
214//----------------------------------------------------------------------------
216{
217 m_afTuple[0] -= rkV.m_afTuple[0];
218 m_afTuple[1] -= rkV.m_afTuple[1];
219 m_afTuple[2] -= rkV.m_afTuple[2];
220 return *this;
221}
222//----------------------------------------------------------------------------
224{
225 m_afTuple[0] *= fScalar;
226 m_afTuple[1] *= fScalar;
227 m_afTuple[2] *= fScalar;
228 return *this;
229}
230//----------------------------------------------------------------------------
232{
233 if (fScalar != (double)0.0) {
234 double fInvScalar = ((double)1.0) / fScalar;
235 m_afTuple[0] *= fInvScalar;
236 m_afTuple[1] *= fInvScalar;
237 m_afTuple[2] *= fInvScalar;
238 } else {
239 m_afTuple[0] = ub_math::inf;
240 m_afTuple[1] = ub_math::inf;
241 m_afTuple[2] = ub_math::inf;
242 }
243
244 return *this;
245}
246//----------------------------------------------------------------------------
248{
249 GbVector3D PointA(0.0, 0.0, 0.0);
250 PointA.m_afTuple[0] = x * m_afTuple[0];
251 PointA.m_afTuple[1] = x * m_afTuple[1];
252 PointA.m_afTuple[2] = x * m_afTuple[2];
253 return PointA;
254}
255
256//----------------------------------------------------------------------------
257double GbVector3D::Length() const
258{
259 return std::sqrt(m_afTuple[0] * m_afTuple[0] + m_afTuple[1] * m_afTuple[1] + m_afTuple[2] * m_afTuple[2]);
260}
261//----------------------------------------------------------------------------
263{
264 return m_afTuple[0] * m_afTuple[0] + m_afTuple[1] * m_afTuple[1] + m_afTuple[2] * m_afTuple[2];
265}
266//----------------------------------------------------------------------------
267double GbVector3D::Dot(const GbVector3D &rkV) const
268{
269 return m_afTuple[0] * rkV.m_afTuple[0] + m_afTuple[1] * rkV.m_afTuple[1] + m_afTuple[2] * rkV.m_afTuple[2];
270}
271//----------------------------------------------------------------------------
273{
274 double fLength = Length();
275
277 double fInvLength = ((double)1.0) / fLength;
278 m_afTuple[0] *= fInvLength;
279 m_afTuple[1] *= fInvLength;
280 m_afTuple[2] *= fInvLength;
281 } else {
282 fLength = 0.0;
283 m_afTuple[0] = 0.0;
284 m_afTuple[1] = 0.0;
285 m_afTuple[2] = 0.0;
286 }
287
288 return fLength;
289}
290//----------------------------------------------------------------------------
292{
293 return GbVector3D(m_afTuple[1] * rkV.m_afTuple[2] - m_afTuple[2] * rkV.m_afTuple[1],
294 m_afTuple[2] * rkV.m_afTuple[0] - m_afTuple[0] * rkV.m_afTuple[2],
295 m_afTuple[0] * rkV.m_afTuple[1] - m_afTuple[1] * rkV.m_afTuple[0]);
296}
297
298//----------------------------------------------------------------------------
299
301{
302 GbVector3D kCross(m_afTuple[1] * rkV.m_afTuple[2] - m_afTuple[2] * rkV.m_afTuple[1],
303 m_afTuple[2] * rkV.m_afTuple[0] - m_afTuple[0] * rkV.m_afTuple[2],
304 m_afTuple[0] * rkV.m_afTuple[1] - m_afTuple[1] * rkV.m_afTuple[0]);
305 kCross.Normalize();
306 return kCross;
307}
308//----------------------------------------------------------------------------
310 const GbVector3D &rkV3, double afBary[4]) const
311{
312 // compute the vectors relative to V3 of the tetrahedron
313 GbVector3D akDiff[4] = { rkV0 - rkV3, rkV1 - rkV3, rkV2 - rkV3, *this - rkV3 };
314
315 // If the vertices have large magnitude, the linear system of
316 // equations for computing barycentric coordinates can be
317 // ill-conditioned. To avoid this, uniformly scale the tetrahedron
318 // edges to be of order 1. The scaling of all differences does not
319 // change the barycentric coordinates.
320 double fMax = (double)0.0;
321 int i;
322 for (i = 0; i < 3; i++) {
323 for (int j = 0; j < 3; j++) {
324 double fValue = std::fabs(akDiff[i][j]);
325 if (fValue > fMax)
326 fMax = fValue;
327 }
328 }
329
330 // scale down only large data
331 if (fMax > (double)1.0) {
332 double fInvMax = ((double)1.0) / fMax;
333 for (i = 0; i < 4; i++)
334 akDiff[i] *= fInvMax;
335 }
336
337 double fDet = akDiff[0].Dot(akDiff[1].Cross(akDiff[2]));
341 if (std::fabs(fDet) > ub_math::Epsilon<double>::val()) {
342 double fInvDet = ((double)1.0) / fDet;
343 afBary[0] = akDiff[3].Dot(kE1cE2) * fInvDet;
344 afBary[1] = akDiff[3].Dot(kE2cE0) * fInvDet;
345 afBary[2] = akDiff[3].Dot(kE0cE1) * fInvDet;
346 afBary[3] = (double)1.0 - afBary[0] - afBary[1] - afBary[2];
347 } else {
348 // The tetrahedron is potentially flat. Determine the face of
349 // maximum area and compute barycentric coordinates with respect
350 // to that face.
355 int iMaxIndex = 3;
356 double fSqrArea = kE0cE1.SquaredLength();
357 if (fSqrArea > fMaxSqrArea) {
358 iMaxIndex = 0;
360 }
361 fSqrArea = kE1cE2.SquaredLength();
362 if (fSqrArea > fMaxSqrArea) {
363 iMaxIndex = 1;
365 }
366 fSqrArea = kE2cE0.SquaredLength();
367 if (fSqrArea > fMaxSqrArea) {
368 iMaxIndex = 2;
370 }
371
373 double fInvSqrArea = ((double)1.0) / fMaxSqrArea;
375 if (iMaxIndex == 0) {
376 kTmp = akDiff[3].Cross(akDiff[1]);
378 kTmp = akDiff[0].Cross(akDiff[3]);
379 afBary[1] = kE0cE1.Dot(kTmp) * fInvSqrArea;
380 afBary[2] = (double)0.0;
381 afBary[3] = (double)1.0 - afBary[0] - afBary[1];
382 } else if (iMaxIndex == 1) {
383 afBary[0] = (double)0.0;
384 kTmp = akDiff[3].Cross(akDiff[2]);
385 afBary[1] = kE1cE2.Dot(kTmp) * fInvSqrArea;
386 kTmp = akDiff[1].Cross(akDiff[3]);
387 afBary[2] = kE1cE2.Dot(kTmp) * fInvSqrArea;
388 afBary[3] = (double)1.0 - afBary[1] - afBary[2];
389 } else if (iMaxIndex == 2) {
390 kTmp = akDiff[2].Cross(akDiff[3]);
391 afBary[0] = kE2cE0.Dot(kTmp) * fInvSqrArea;
392 afBary[1] = (double)0.0;
393 kTmp = akDiff[3].Cross(akDiff[0]);
394 afBary[2] = kE2cE0.Dot(kTmp) * fInvSqrArea;
395 afBary[3] = (double)1.0 - afBary[0] - afBary[2];
396 } else {
397 akDiff[3] = *this - rkV2;
398 kTmp = akDiff[3].Cross(kE12);
399 afBary[0] = kE02cE12.Dot(kTmp) * fInvSqrArea;
400 kTmp = kE02.Cross(akDiff[3]);
401 afBary[1] = kE02cE12.Dot(kTmp) * fInvSqrArea;
402 afBary[2] = (double)1.0 - afBary[0] - afBary[1];
403 afBary[3] = (double)0.0;
404 }
405 } else {
406 // The tetrahedron is potentially a sliver. Determine the edge of
407 // maximum length and compute barycentric coordinates with respect
408 // to that edge.
409 double fMaxSqrLength = akDiff[0].SquaredLength();
410 iMaxIndex = 0; // <V0,V3>
411 double fSqrLength = akDiff[1].SquaredLength();
413 iMaxIndex = 1; // <V1,V3>
415 }
416 fSqrLength = akDiff[2].SquaredLength();
418 iMaxIndex = 2; // <V2,V3>
420 }
421 fSqrLength = kE02.SquaredLength();
423 iMaxIndex = 3; // <V0,V2>
425 }
426 fSqrLength = kE12.SquaredLength();
428 iMaxIndex = 4; // <V1,V2>
430 }
434 iMaxIndex = 5; // <V0,V1>
436 }
437
439 double fInvSqrLength = ((double)1.0) / fMaxSqrLength;
440 if (iMaxIndex == 0) {
441 // P-V3 = t*(V0-V3)
442 afBary[0] = akDiff[3].Dot(akDiff[0]) * fInvSqrLength;
443 afBary[1] = (double)0.0;
444 afBary[2] = (double)0.0;
445 afBary[3] = (double)1.0 - afBary[0];
446 } else if (iMaxIndex == 1) {
447 // P-V3 = t*(V1-V3)
448 afBary[0] = (double)0.0;
449 afBary[1] = akDiff[3].Dot(akDiff[1]) * fInvSqrLength;
450 afBary[2] = (double)0.0;
451 afBary[3] = (double)1.0 - afBary[1];
452 } else if (iMaxIndex == 2) {
453 // P-V3 = t*(V2-V3)
454 afBary[0] = (double)0.0;
455 afBary[1] = (double)0.0;
456 afBary[2] = akDiff[3].Dot(akDiff[2]) * fInvSqrLength;
457 afBary[3] = (double)1.0 - afBary[2];
458 } else if (iMaxIndex == 3) {
459 // P-V2 = t*(V0-V2)
460 akDiff[3] = *this - rkV2;
461 afBary[0] = akDiff[3].Dot(kE02) * fInvSqrLength;
462 afBary[1] = (double)0.0;
463 afBary[2] = (double)1.0 - afBary[0];
464 afBary[3] = (double)0.0;
465 } else if (iMaxIndex == 4) {
466 // P-V2 = t*(V1-V2)
467 akDiff[3] = *this - rkV2;
468 afBary[0] = (double)0.0;
469 afBary[1] = akDiff[3].Dot(kE12) * fInvSqrLength;
470 afBary[2] = (double)1.0 - afBary[1];
471 afBary[3] = (double)0.0;
472 } else {
473 // P-V1 = t*(V0-V1)
474 akDiff[3] = *this - rkV1;
475 afBary[0] = akDiff[3].Dot(kE01) * fInvSqrLength;
476 afBary[1] = (double)1.0 - afBary[0];
477 afBary[2] = (double)0.0;
478 afBary[3] = (double)0.0;
479 }
480 } else {
481 // tetrahedron is a nearly a point, just return equal weights
482 afBary[0] = (double)0.25;
483 afBary[1] = afBary[0];
484 afBary[2] = afBary[0];
485 afBary[3] = afBary[0];
486 }
487 }
488 }
489}
490//----------------------------------------------------------------------------
492{
493 // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
494 // orthonormalization produces vectors u0, u1, and u2 as follows,
495 //
496 // u0 = v0/|v0|
497 // u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
498 // u2 = (v2-(u0*v2)u0-(u1*v2)u1)/|v2-(u0*v2)u0-(u1*v2)u1|
499 //
500 // where |A| indicates length of vector A and A*B indicates dot
501 // product of vectors A and B.
502
503 // compute u0
504 rkU.Normalize();
505
506 // compute u1
507 double fDot0 = rkU.Dot(rkV);
508 rkV -= fDot0 * rkU;
509 rkV.Normalize();
510
511 // compute u2
512 double fDot1 = rkV.Dot(rkW);
513 fDot0 = rkU.Dot(rkW);
514 rkW -= fDot0 * rkU + fDot1 * rkV;
515 rkW.Normalize();
516}
517//----------------------------------------------------------------------------
519//----------------------------------------------------------------------------
521{
522 if (!bUnitLengthW)
523 rkW.Normalize();
524
525 double fInvLength;
526
527 if (std::fabs(rkW.m_afTuple[0]) >= std::fabs(rkW.m_afTuple[1])) {
528 // W.x or W.z is the largest magnitude component, swap them
529 fInvLength = ub_math::invSqrt(rkW.m_afTuple[0] * rkW.m_afTuple[0] + rkW.m_afTuple[2] * rkW.m_afTuple[2]);
530 rkU.m_afTuple[0] = -rkW.m_afTuple[2] * fInvLength;
531 rkU.m_afTuple[1] = (double)0.0;
532 rkU.m_afTuple[2] = +rkW.m_afTuple[0] * fInvLength;
533 } else {
534 // W.y or W.z is the largest magnitude component, swap them
535 fInvLength = ub_math::invSqrt(rkW.m_afTuple[1] * rkW.m_afTuple[1] + rkW.m_afTuple[2] * rkW.m_afTuple[2]);
536 rkU.m_afTuple[0] = (double)0.0;
537 rkU.m_afTuple[1] = +rkW.m_afTuple[2] * fInvLength;
538 rkU.m_afTuple[2] = -rkW.m_afTuple[1] * fInvLength;
539 }
540
541 rkV = rkW.Cross(rkU);
542}
543//----------------------------------------------------------------------------
544
This Class provides basic 3D point objects.
Definition GbPoint3D.h:49
This Class provides basic 3D vector objects.
Definition GbVector3D.h:47
std::shared_ptr< T > SPtr
GbVector3D & operator+=(const GbVector3D &rkV)
GbVector3D Cross(const GbVector3D &rkV) const
GbVector3D & operator-=(const GbVector3D &rkV)
GbVector3D Subtract(GbVector3D &vector)
double SquaredLength() const
bool operator>=(const GbVector3D &rkV) const
bool operator<=(const GbVector3D &rkV) const
double Length() const
GbVector3D operator-() const
double operator[](int i) const
static const GbVector3D UNIT_X1
Definition GbVector3D.h:130
static const GbVector3D ZERO
Definition GbVector3D.h:129
bool operator==(const GbVector3D &rkV) const
bool operator!=(const GbVector3D &rkV) const
std::string toString()
GbVector3D operator+(const GbVector3D &rkV) const
bool operator<(const GbVector3D &rkV) const
void GetBarycentrics(const GbVector3D &rkV0, const GbVector3D &rkV1, const GbVector3D &rkV2, const GbVector3D &rkV3, double afBary[4]) const
GbVector3D operator/(const double &fScalar) const
static void Orthonormalize(GbVector3D &rkU, GbVector3D &rkV, GbVector3D &rkW)
static void GenerateOrthonormalBasis(GbVector3D &rkU, GbVector3D &rkV, GbVector3D &rkW, bool bUnitLengthW)
static const GbVector3D UNIT_X3
Definition GbVector3D.h:132
double X1() const
double X3() const
double X2() const
static const GbVector3D UNIT_X2
Definition GbVector3D.h:131
GbVector3D & operator*=(const double &fScalar)
bool operator>(const GbVector3D &rkV) const
GbVector3D operator*(const double &fScalar) const
GbVector3D & operator/=(const double &fScalar)
GbVector3D operator*(const double &fScalar, const GbVector3D &rkV)
GbVector3D UnitCross(const GbVector3D &rkV) const
double Normalize()
GbVector3D Scale(const double &x)
GbVector3D Add(GbVector3D &vector)
double Dot(const GbVector3D &rkV) const
GbVector3D & operator=(const GbVector3D &rkV)
@ x
Definition Axis.h:42
T invSqrt(const T &fValue)
Definition UbMath.h:280