VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
GbTriFaceMesh3D.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 GBTRIFACEMESH3D_H
35#define GBTRIFACEMESH3D_H
36
37#include <iostream>
38#include <map>
39#include <sstream>
40#include <vector>
41#include <atomic>
42
49
50namespace kd_tree
51{
52template <typename T>
53class Tree;
54template <typename T>
55class SplitAlgorithm;
56template <typename T>
58} // namespace kd_tree
59
60class WbWriter;
61
62/*=========================================================================*/
63/* GbTriFaceMesh3D */
64/* */
71{
72public:
73 // nested class start
74 class Vertex
75 {
76 public:
77 Vertex() = default;
78 Vertex(const float &x, const float &y, const float &z) : x(x), y(y), z(z) {}
80 {
81 this->x = vert->x;
82 this->y = vert->y;
83 this->z = vert->z;
84 }
85 float operator[](const int &i) const
86 {
87 if (i == 0)
88 return x;
89 else if (i == 1)
90 return y;
91 else if (i == 2)
92 return z;
93
94 throw UbException(UB_EXARGS, "i not in [0;2]");
95 }
96 float &operator[](const int &i)
97 {
98 if (i == 0)
99 return x;
100 else if (i == 1)
101 return y;
102 else if (i == 2)
103 return z;
104
105 throw UbException(UB_EXARGS, "not in [0;2]");
106 }
107 bool operator==(const Vertex &rhs)
108 {
109 return (fabs(x - rhs.x) < 1.E-8 && fabs(y - rhs.y) < 1.E-8 && fabs(z - rhs.z) < 1.E-8);
110 }
111 friend inline bool operator<(const Vertex &lhsVert, const Vertex &rhsVert)
112 {
113 if (lhsVert.x < rhsVert.x)
114 return true;
115 if (lhsVert.x > rhsVert.x)
116 return false;
117 if (lhsVert.y < rhsVert.y)
118 return true;
119 if (lhsVert.y > rhsVert.y)
120 return false;
121 if (lhsVert.z < rhsVert.z)
122 return true;
123
124 return false;
125 }
126 friend std::ostream &operator<<(std::ostream &os, const Vertex &node)
127 {
128 return os << node.x << "," << node.y << "," << node.z;
129 }
130 Vertex *clone() { return (new Vertex(this)); }
131
132 public:
133 float x{ 0.0 }, y{ 0.0 }, z{ 0.0 };
134 };
137 {
138 public:
140
141 = default;
142 TriFace(const int &v1, const int &v2, const int &v3) : v1(v1), v2(v2), v3(v3) {}
143
144 const int &getIndexVertex1() const { return v1; }
145 const int &getIndexVertex2() const { return v2; }
146 const int &getIndexVertex3() const { return v3; }
147
148 Vertex &getNode(const int &i, std::vector<Vertex> &nodes)
149 {
150 if (i == 0)
151 return nodes[v1];
152 if (i == 1)
153 return nodes[v2];
154 if (i == 2)
155 return nodes[v3];
156 throw UbException(UB_EXARGS, "invalid i - not in range [0;2]");
157 }
158 void setNode(const int &i, const int &index)
159 {
160 if (i == 0)
161 v1 = index;
162 else if (i == 1)
163 v2 = index;
164 else if (i == 2)
165 v3 = index;
166 else
167 throw UbException(UB_EXARGS, "invalid i - not in range [0;2]");
168 }
169
170 int operator[](int index)
171 {
172 if (index == 0)
173 return v1;
174 if (index == 1)
175 return v2;
176 if (index == 2)
177 return v3;
178 throw UbException(UB_EXARGS, "invalid i - not in range [0;2]");
179 }
180
181 float &getV1x(std::vector<Vertex> &nodes) { return nodes[v1].x; }
182 float &getV1y(std::vector<Vertex> &nodes) { return nodes[v1].y; }
183 float &getV1z(std::vector<Vertex> &nodes) { return nodes[v1].z; }
184
185 float &getV2x(std::vector<Vertex> &nodes) { return nodes[v2].x; }
186 float &getV2y(std::vector<Vertex> &nodes) { return nodes[v2].y; }
187 float &getV2z(std::vector<Vertex> &nodes) { return nodes[v2].z; }
188
189 float &getV3x(std::vector<Vertex> &nodes) { return nodes[v3].x; }
190 float &getV3y(std::vector<Vertex> &nodes) { return nodes[v3].y; }
191 float &getV3z(std::vector<Vertex> &nodes) { return nodes[v3].z; }
192
193 float getMinX(std::vector<Vertex> &nodes) { return (float)ub_math::min(nodes[v1].x, nodes[v2].x, nodes[v3].x); }
194 float getMinY(std::vector<Vertex> &nodes) { return (float)ub_math::min(nodes[v1].y, nodes[v2].y, nodes[v3].y); }
195 float getMinZ(std::vector<Vertex> &nodes) { return (float)ub_math::min(nodes[v1].z, nodes[v2].z, nodes[v3].z); }
196
197 float getMaxX(std::vector<Vertex> &nodes) { return (float)ub_math::max(nodes[v1].x, nodes[v2].x, nodes[v3].x); }
198 float getMaxY(std::vector<Vertex> &nodes) { return (float)ub_math::max(nodes[v1].y, nodes[v2].y, nodes[v3].y); }
199 float getMaxZ(std::vector<Vertex> &nodes) { return (float)ub_math::max(nodes[v1].z, nodes[v2].z, nodes[v3].z); }
200
201 float getX1Centroid(std::vector<Vertex> &nodes)
202 {
203 return (float)vf::basics::constant::c1o3 * (getV1x(nodes) + getV2x(nodes) + getV3x(nodes));
204 }
205 float getX2Centroid(std::vector<Vertex> &nodes)
206 {
207 return (float)vf::basics::constant::c1o3 * (getV1y(nodes) + getV2y(nodes) + getV3y(nodes));
208 }
209 float getX3Centroid(std::vector<Vertex> &nodes)
210 {
211 return (float)vf::basics::constant::c1o3 * (getV1z(nodes) + getV2z(nodes) + getV3z(nodes));
212 }
213
214 double calculateDistanceToPoint3D(const double &x1, const double &x2, const double &x3,
215 std::vector<Vertex> &nodes);
216
217 double getArea(std::vector<Vertex> &nodes)
218 {
221 GbVector3D C(nodes[v3].x, nodes[v3].y, nodes[v3].z);
222 GbVector3D AB = B - A;
223 GbVector3D AC = C - A;
224 GbVector3D N = AB.Cross(AC);
225 return 0.5 * N.Length();
226 }
227
228 void calculateNormal(std::vector<Vertex> &nodes)
229 {
230 const float &v1x = nodes[v1].x;
231 const float &v1y = nodes[v1].y;
232 const float &v1z = nodes[v1].z;
233 const float &v2x = nodes[v2].x;
234 const float &v2y = nodes[v2].y;
235 const float &v2z = nodes[v2].z;
236 const float &v3x = nodes[v3].x;
237 const float &v3y = nodes[v3].y;
238 const float &v3z = nodes[v3].z;
239
240 nx = (v3z - v1z) * (v2y - v1y) - (v2z - v1z) * (v3y - v1y);
241 ny = (v2z - v1z) * (v3x - v1x) - (v2x - v1x) * (v3z - v1z);
242 nz = (v2x - v1x) * (v3y - v1y) - (v2y - v1y) * (v3x - v1x);
243
244 float length = std::sqrt(nx * nx + ny * ny + nz * nz);
245 if (length > 1.E-10) {
246 length = 1.0f / length;
247 nx *= length;
248 ny *= length;
249 nz *= length;
250 } else {
251 // Log only once to avoid flooding when many degenerate triangles are present.
252 static std::atomic<int> degenerateWarnings{0};
253 const int warnIndex = degenerateWarnings.fetch_add(1, std::memory_order_relaxed);
254 if (warnIndex == 0) {
255 std::cerr << "GbTriFaceMesh3D::TriFace - calculateNormal: nx=ny=nz=0 -> should not happen "
256 << "(triangle likely has duplicate nodes or is a line) "
257 << "-> removeRedundantNodes" << std::endl;
258 }
259 }
260 }
261
262 public:
263 int v1{ -1 }, v2{ -1 }, v3{ -1 };
264 float nx{ 0.0 }, ny{ 0.0 }, nz{ 0.0 };
265 };
266
267public:
269
270public:
272 GbTriFaceMesh3D(std::string name, std::vector<Vertex> *nodes, std::vector<TriFace> *triangles,
274 ~GbTriFaceMesh3D() override;
275
276 GbTriFaceMesh3D *clone() override; // { throw UbException(UB_EXARGS,"not implemented"); }
277 void finalize() override {}
278
279 // void setRegardPointInPolyhedronTest(bool value) { this->regardPiO=value; }
280
281 std::string toString() override;
282
283 // std::string getName();
284 std::vector<Vertex> *getNodes();
285 std::vector<TriFace> *getTriangles();
286
287 void setTransferViaFilename(bool transferViaFilename, std::string filename, double transX1, double transX2,
288 double transX3)
289 {
290 this->filename = filename;
291 this->transferViaFilename = transferViaFilename;
292 this->transX1 = transX1;
293 this->transX2 = transX2;
294 this->transX3 = transX3;
295 }
298
299 double getX1Minimum() override
300 {
301 if (!this->consistent)
302 this->calculateValues();
303 return this->x1min;
304 }
305 double getX1Maximum() override
306 {
307 if (!this->consistent)
308 this->calculateValues();
309 return this->x1max;
310 }
311 double getX1Centroid() override
312 {
313 if (!this->consistent)
314 this->calculateValues();
315 return this->x1center;
316 }
317
318 double getX2Minimum() override
319 {
320 if (!this->consistent)
321 this->calculateValues();
322 return this->x2min;
323 }
324 double getX2Maximum() override
325 {
326 if (!this->consistent)
327 this->calculateValues();
328 return this->x2max;
329 }
330 double getX2Centroid() override
331 {
332 if (!this->consistent)
333 this->calculateValues();
334 return this->x2center;
335 }
336
337 double getX3Minimum() override
338 {
339 if (!this->consistent)
340 this->calculateValues();
341 return this->x3min;
342 }
343 double getX3Centroid() override
344 {
345 if (!this->consistent)
346 this->calculateValues();
347 return this->x3center;
348 }
349 double getX3Maximum() override
350 {
351 if (!this->consistent)
352 this->calculateValues();
353 return this->x3max;
354 }
355
356 void calculateValues();
357
358 double getVolume();
360
363
364 void setCenterCoordinates(const double &x1, const double &x2, const double &x3) override;
365
366 void setCenterCoordinates(const UbTupleDouble3 & /*position*/) override;
367
368 void scale(const double &sx1, const double &sx2, const double &sx3) override;
369 void rotate(const double &alpha, const double &beta, const double &gamma) override;
370 void rotateAroundPoint(const double &px1, const double &px2, const double &px3, const double &alpha,
371 const double &beta, const double &gamma);
372 void translate(const double &x1, const double &x2, const double &x3) override;
373 //void reflectAcrossXYLine(const double &alpha);
374
375 bool isPointInGbObject3D(const double &x1, const double &x2, const double &x3) override;
376 bool isPointInGbObject3D(const double &x1, const double &x2, const double &x3, int counter);
377 bool isPointInGbObject3D(const double &x1, const double &x2, const double &x3, bool &pointIsOnBoundary) override;
378
380
381 std::vector<GbTriangle3D *> getSurfaceTriangleSet() override;
382 void addSurfaceTriangleSet(std::vector<UbTupleFloat3> &nodes, std::vector<UbTupleInt3> &triangles) override;
383
384 std::vector<GbTriFaceMesh3D::TriFace *> getTrianglesForVertex(Vertex *vertex);
385
390
392 bool writeNormals = false,
393 std::vector<std::string> *datanames = NULL,
394 std::vector<std::vector<double>> *nodedata = NULL);
395 void writeMeshPly(const std::string &filename);
396
397 /*======================================================================*/
398 using GbObject3D::isPointInGbObject3D; // Grund: dadurch muss man hier isPointInGbObject3D(GbPoint3D*) nicht
399 // ausprogrammieren, welche sonst hier "ueberdeckt" waere
400
401 bool intersectLine(const double &p1_x1, const double &p1_x2, const double &p1_x3, const double &p2_x1,
402 const double &p2_x2, const double &p2_x3);
403
404protected:
406 void init();
407
408 std::vector<Vertex> * nodes;
409 std::vector<TriFace> * triangles;
410 // for transfer
411 std::string filename;
412 bool transferViaFilename{ false };
413 double transX1{ 0.0 };
414 double transX2{ 0.0 };
415 double transX3{ 0.0 };
416
417 double x1min;
418 double x1max;
419 double x2min;
420 double x2max;
421 double x3min;
422 double x3max;
423 double x1center;
424 double x2center;
425 double x3center;
426
427 bool consistent{ false };
428
430 std::multimap<Vertex *, TriFace *> relationVertTris;
431
433};
434
435#endif // GBTRIFACEMESH3D_H
436
This Class provides basic 3D line objects.
Definition GbLine3D.h:59
This Interface provides basic 3D geometry objects methods.
Definition GbObject3D.h:64
This Class provides basic 3D point objects.
Definition GbPoint3D.h:49
double calculateDistanceToPoint3D(const double &x1, const double &x2, const double &x3, std::vector< Vertex > &nodes)
float & getV1y(std::vector< Vertex > &nodes)
Vertex & getNode(const int &i, std::vector< Vertex > &nodes)
float getMaxY(std::vector< Vertex > &nodes)
float & getV3z(std::vector< Vertex > &nodes)
float getMinX(std::vector< Vertex > &nodes)
float getX1Centroid(std::vector< Vertex > &nodes)
float & getV2x(std::vector< Vertex > &nodes)
float getMinY(std::vector< Vertex > &nodes)
const int & getIndexVertex2() const
void setNode(const int &i, const int &index)
const int & getIndexVertex1() const
double getArea(std::vector< Vertex > &nodes)
void calculateNormal(std::vector< Vertex > &nodes)
TriFace(const int &v1, const int &v2, const int &v3)
float & getV3x(std::vector< Vertex > &nodes)
float getX3Centroid(std::vector< Vertex > &nodes)
float getMinZ(std::vector< Vertex > &nodes)
float getMaxZ(std::vector< Vertex > &nodes)
float & getV3y(std::vector< Vertex > &nodes)
float getX2Centroid(std::vector< Vertex > &nodes)
float & getV1z(std::vector< Vertex > &nodes)
float & getV2y(std::vector< Vertex > &nodes)
const int & getIndexVertex3() const
float getMaxX(std::vector< Vertex > &nodes)
float & getV1x(std::vector< Vertex > &nodes)
float & getV2z(std::vector< Vertex > &nodes)
bool operator==(const Vertex &rhs)
Vertex(const float &x, const float &y, const float &z)
float & operator[](const int &i)
float operator[](const int &i) const
friend bool operator<(const Vertex &lhsVert, const Vertex &rhsVert)
friend std::ostream & operator<<(std::ostream &os, const Vertex &node)
KDTREE_SPLITAGORITHM kdtreeSplitAlg
double getX1Centroid() override
std::string filename
GbTriFaceMesh3D(std::string name, std::vector< Vertex > *nodes, std::vector< TriFace > *triangles, KDTREE_SPLITAGORITHM splitAlg=KDTREE_SAHPLIT, bool removeRedundantNodes=true)
kd_tree::Tree< double > * getKdTree()
double getX1Maximum() override
double getX1Minimum() override
std::multimap< Vertex *, TriFace * > relationVertTris
double getX2Maximum() override
std::vector< TriFace > * triangles
double getX3Centroid() override
kd_tree::Tree< double > * kdTree
KDTREE_SPLITAGORITHM getKdTreeSplitAlgorithm()
double getX3Minimum() override
std::vector< Vertex > * nodes
void setTransferViaFilename(bool transferViaFilename, std::string filename, double transX1, double transX2, double transX3)
void finalize() override
double getX2Centroid() override
double getX2Minimum() override
double getX3Maximum() override
This Class provides basic 3D vector objects.
Definition GbVector3D.h:47
A class implements a tuple.
Definition UbTuple.h:477
std::shared_ptr< T > SPtr
GbVector3D Cross(const GbVector3D &rkV) const
void rotate(const double &alpha, const double &beta, const double &gamma) override
virtual UbTuple< std::string, std::string > writeMesh(std::string filename, WbWriter *writer, bool writeNormals=false, std::vector< std::string > *datanames=NULL, std::vector< std::vector< double > > *nodedata=NULL)
void setKdTreeSplitAlgorithm(KDTREE_SPLITAGORITHM mode)
kd_tree::Tree< double > * ensureKdTree()
std::vector< Vertex > * getNodes()
void setCenterCoordinates(const double &x1, const double &x2, const double &x3) override
void scale(const double &sx1, const double &sx2, const double &sx3) override
UbTupleDouble6 calculateMomentOfInertia(double rhoP)
double Length() const
void readMeshFromSTLFileBinary(std::string filename, bool removeRedundantNodes)
std::string toString() override
~GbTriFaceMesh3D() override
void readMeshFromSTLFileASCII(std::string filename, bool removeRedundantNodes)
virtual bool isPointInGbObject3D(GbPoint3D *p)
std::vector< GbTriangle3D * > getSurfaceTriangleSet() override
void rotateAroundPoint(const double &px1, const double &px2, const double &px3, const double &alpha, const double &beta, const double &gamma)
void writeMeshPly(const std::string &filename)
GbTriFaceMesh3D * clone() override
GbLine3D * createClippedLine3D(GbPoint3D &point1, GbPoint3D &point2) override
std::vector< GbTriFaceMesh3D::TriFace * > getTrianglesForVertex(Vertex *vertex)
void translate(const double &x1, const double &x2, const double &x3) override
bool isPointInGbObject3D(const double &x1, const double &x2, const double &x3) override
std::vector< TriFace > * getTriangles()
void addSurfaceTriangleSet(std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt3 > &triangles) override
UbTupleDouble3 calculateCenterOfGravity()
bool intersectLine(const double &p1_x1, const double &p1_x2, const double &p1_x3, const double &p2_x1, const double &p2_x2, const double &p2_x3)
@ z
Definition Axis.h:44
@ x
Definition Axis.h:42
@ y
Definition Axis.h:43
#define UB_EXARGS
Definition UbException.h:73
UbTuple< double, double, double, double, double, double > UbTupleDouble6
Definition UbTuple.h:636
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