VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
TriangularMesh.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 "TriangularMesh.h"
35
36#include <stdexcept>
37
38#include "Timer/Timer.h"
39
41
44
47
48#include "grid/GridImp.h"
49#include "grid/NodeValues.h"
50
52
53namespace vf::gpu {
54
55TriangularMesh* TriangularMesh::make(const std::string& fileName, const std::vector<uint> ignorePatches)
56{
58 return triangularMesh;
59}
60
62{
64 initalizeDataFromTriangles();
65 this->findNeighbors();
66}
67
68TriangularMesh::TriangularMesh(const std::string& inputPath, const std::vector<uint> ignorePatches)
69{
71
72 try {
74 } catch (const std::exception& e) {
75 VF_LOG_WARNING("Falling back to binary STL reader for {}: {}", inputPath, e.what());
76 this->triangleVec = STLReader::readSTL(inputPath);
77 }
78 //this->triangleVec = STLReader::readSTL(inputPath);
79 initalizeDataFromTriangles();
80 this->findNeighbors();
81}
82
83
88
90{
91 auto mesh = std::make_shared<TriangularMesh>();
92 mesh->setTriangles(this->triangleVec);
93 return mesh;
94}
95
96
98{
99 return (uint)triangleVec.size();
100}
101
102
104{
105 VF_LOG_INFO("start finding neighbors ...");
106
108 t.start();
109
111 finder.fillWithNeighborAngles(this);
112
113 t.end();
114 VF_LOG_INFO("time finding neighbors = {}", t.getTimeInSeconds());
115}
116
117void TriangularMesh::setTriangles(std::vector<Triangle> triangles)
118{
119 this->triangleVec = triangles;
120 initalizeDataFromTriangles();
121}
122
124{
125 this->minmax = minmax;
126}
127
128void TriangularMesh::initalizeDataFromTriangles()
129{
130 this->triangles = triangleVec.data();
131 this->size = long(triangleVec.size());
132
133 for (std::size_t i = 0; i < (size_t)this->size; i++) {
134 this->minmax.setMinMax(this->triangleVec[i]);
135 }
136}
137
139{
140 if (!(minmax == geometry.minmax))
141 return false;
142
143 if (size != geometry.size)
144 return false;
145
146 for (int i = 0; i < size ; i++)
147 if (!(triangleVec[i] == geometry.triangleVec[i]))
148 return false;
149 return true;
150}
151
152
157
159{
160 if( this->VF_GbTriFaceMesh3D ) return;
161
162 VF_LOG_INFO("Start generating GbTriFaceMesh3D");
163
164 std::vector<GbTriFaceMesh3D::Vertex> *gbVertices = new std::vector<GbTriFaceMesh3D::Vertex>(this->triangleVec.size() * 3);
165 std::vector<GbTriFaceMesh3D::TriFace> *gbTriangles = new std::vector<GbTriFaceMesh3D::TriFace>(this->triangleVec.size());
166
167 for (int i = 0; i < (int)this->triangleVec.size(); i++)
168 {
169 (*gbVertices)[i * 3] = GbTriFaceMesh3D::Vertex(triangles[i].v1.x, triangles[i].v1.y, triangles[i].v1.z);
170 (*gbVertices)[i * 3 + 1] = GbTriFaceMesh3D::Vertex(triangles[i].v2.x, triangles[i].v2.y, triangles[i].v2.z);
171 (*gbVertices)[i * 3 + 2] = GbTriFaceMesh3D::Vertex(triangles[i].v3.x, triangles[i].v3.y, triangles[i].v3.z);
172
173 (*gbTriangles)[i] = GbTriFaceMesh3D::TriFace(i * 3, i * 3 + 1, i * 3 + 2);
174 }
175
176 this->VF_GbTriFaceMesh3D = std::make_shared<GbTriFaceMesh3D>( "stl", gbVertices, gbTriangles, GbTriFaceMesh3D::KDTREE_SAHPLIT, false );
177
178 VF_LOG_INFO("Done generating GbTriFaceMesh3D");
179}
180
181
183{
184 // assuming vectors are all normalized
185 real denom = normal * directionLine;
186 if (denom > 1e-6) {
188 real distance = p0l0 * normal / denom;
190 return (distance >= 0);
191 }
192
193 return false;
194}
195
197{
198 std::vector<Triangle> triangles = this->triangleVec;
199
200 TriangleNeighborFinder finder(this->triangles, this->size);
201
202 auto trianglesPerVertex = finder.getTrianglesPerVertex();
203 auto averrageNormals = getAverrageNormalsPerVertex(trianglesPerVertex);
204
205
206 for (uint vertexID = 0; vertexID < this->getNumberOfTriangles() * 3; vertexID++)
207 {
208 int coordinatedID = finder.sortedToTriangles[vertexID][IDS::coordinateID];
210
211 //auto triangleIds = finder.getTriangleIDsWithCommonVertex(vertexID);
212 //for(auto index : triangleIds)
213 //{
214 // auto triangle = origin->triangleVec[index];
215 // Vertex intersection;
216 // real d = 10;
217 // triangle.normal.normalize();
218 // Vertex p = triangle.v2 + triangle.normal * d;
219 // // p.normalize();
220 // Vertex lineOrigin = origin->triangleVec[triangleID].get(vertexTriangleID);
221 // // lineOrigin.normalize();
222 // //averrageNormal.normalize();
223 // //triangle.print();
224
225 // //printf("average: \n");
226 // //averrageNormal.print();
227
228 // //printf("line origin: \n");
229 // //lineOrigin.print();
230
231 // //printf("plane normal: \n");
232 // //triangle.normal.print();
233
234 // //printf("plane point: \n");
235 // //p.print();
236
237 // bool b = intersectPlane(triangle.normal, p, lineOrigin, averrageNormal, intersection);
238 // //intersection.print();
239 // //printf("\n");
240 //}
241 //printf("\n\n");
242
244 const int triangleID = (int)vertexID / 3;
245 const int vertexTriangleID = (int)vertexID % 3;
246
248 // Vertex p = this->triangleVec[triangleID].v1 + this->triangleVec[triangleID].normal * offset; // TODO: https://git.rz.tu-bs.de/irmb/VirtualFluids_dev/-/issues/85
249 Vertex lineOrigin = this->triangleVec[triangleID].get(vertexTriangleID);
250 // bool b = intersectPlane(this->triangleVec[triangleID].normal, p, lineOrigin, averrageNormal, intersection);
253
255 }
256
257 this->setTriangles(triangles);
258}
259
260std::vector<Vertex> TriangularMesh::getAverrageNormalsPerVertex(std::vector<std::vector<Triangle>> trianglesPerVertex)
261{
262 std::vector<Vertex> averrageNormals;
263 for (auto triangles : trianglesPerVertex)
264 {
265 eliminateTriangleswithIdenticialNormal(triangles);
266
268 for (auto triangle : triangles)
269 {
270 triangle.calcNormal();
271 sumNormal = sumNormal + triangle.normal;
272 }
274 averrageNormals.push_back(Vertex(sumNormal / magnitude));
275 }
276 return averrageNormals;
277}
278
279void TriangularMesh::eliminateTriangleswithIdenticialNormal(std::vector<Triangle> &triangles)
280{
281 for (std::size_t i = 0; i < triangles.size() - 1; i++) {
282 for (std::size_t j = i + 1; j < triangles.size(); j++) {
283 if (triangles[i].normal == triangles[j].normal)
284 triangles.erase(triangles.begin() + i);
285 }
286 }
287}
288
290{
291 grid->getTriangularMeshDiscretizationStrategy()->discretize(this, grid.get(), FLUID, INVALID_OUT_OF_GRID);
292}
293
294}
295
#define VF_LOG_INFO(...)
Definition Logger.h:50
#define VF_LOG_WARNING(...)
Definition Logger.h:51
void setMinMax(const Triangle &t)
static BoundingBox makeInvalidMinMaxBox()
static std::vector< Triangle > readSTL(const std::string &name)
Definition STLReader.cpp:53
void setMinMax(BoundingBox minmax)
static TriangularMesh * make(const std::string &fileName, const std::vector< uint > ignorePatches=std::vector< uint >())
void findInnerNodes(SPtr< GridImp > grid) override
SPtr< GbTriFaceMesh3D > VF_GbTriFaceMesh3D
SPtr< Object > clone() const override
GbTriFaceMesh3D * getGbTriFaceMesh3D() const
std::vector< Triangle > triangleVec
uint getNumberOfTriangles() const
void setTriangles(std::vector< Triangle > triangles)
void changeSizeByDelta(double delta) override
bool operator==(const TriangularMesh &geometry) const
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
unsigned int uint
Definition DataTypes.h:47
bool intersectPlane(const Vertex &normal, const Vertex &pointOnPlane, const Vertex &originLine, const Vertex &directionLine, Vertex &intersectionPoint)
void set(const Vertex &v1, const Vertex &v2, const Vertex &v3)
Definition Triangle.cpp:47
real getMagnitude() const
Definition Vertex.cpp:99
void normalize()
Definition Vertex.cpp:86