VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
GbVoxelMatrix3D.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 GBVOXELMATRIX3D_H
35#define GBVOXELMATRIX3D_H
36
37#include <cmath>
38#include <vector>
39#include <fstream>
40#include <sstream>
41
45
46#include <PointerDefinitions.h>
47
48class GbLine3D;
49class GbTriangle3D;
51
53{
54public:
56 static const float SOLID;
57 static const float FLUID;
59
61 GbVoxelMatrix3D(int nx1, int nx2, int nx3, float initVal, double lowerThreshold = 0, double upperThreshold = 0);
63 ~GbVoxelMatrix3D() override = default;
64
65 void finalize() override{};
66 GbVoxelMatrix3D *clone() override;
67
68 /*=======================================================================*/
70 const Matrix3D::size_type &x3)
71 {
72 return voxelMatrix(x1, x2, x3);
73 }
74 /*=======================================================================*/
76 const Matrix3D::size_type &x3) const
77 {
78 return voxelMatrix(x1, x2, x3);
79 }
80 /*=======================================================================*/
82 {
83 this->filename = filename;
84 this->transferViaFilename = transferViaFilename;
85 }
87 {
88 this->lowerThreshold = lowerThreshold;
89 this->upperThreshold = upperThreshold;
90 }
92
93 /*=======================================================================*/
94 void setVoxelMatrixMininum(double minX1, double minX2, double minX3)
95 {
96 this->minX1 = minX1;
97 this->minX2 = minX2;
98 this->minX3 = minX3;
99 }
100 void setVoxelMatrixMinX1(double minX1) { this->minX1 = minX1; }
101 void setVoxelMatrixMinX2(double minX2) { this->minX2 = minX2; }
102 void setVoxelMatrixMinX3(double minX3) { this->minX3 = minX3; }
103
104 /*=======================================================================*/
105 void setVoxelMatrixDelta(double deltaX1, double deltaX2, double deltaX3)
106 {
107 this->deltaX1 = deltaX1;
108 this->deltaX2 = deltaX2;
109 this->deltaX3 = deltaX3;
110 }
111 void setVoxelMatrixDeltaX1(double deltaX1) { this->deltaX1 = deltaX1; }
112 void setVoxelMatrixDeltaX2(double deltaX2) { this->deltaX2 = deltaX2; }
113 void setVoxelMatrixDeltaX3(double deltaX3) { this->deltaX3 = deltaX3; }
114
115 /*=======================================================================*/
116 double getX1Centroid() override { return 0.5 * (minX1 + this->getX1Maximum()); }
117 double getX1Minimum() override { return minX1; }
118 double getX1Maximum() override { return minX1 + deltaX1 * voxelMatrix.getNX1(); }
119
120 double getX2Centroid() override { return 0.5 * (minX2 + this->getX2Maximum()); }
121 double getX2Minimum() override { return minX2; }
122 double getX2Maximum() override { return minX2 + deltaX2 * voxelMatrix.getNX2(); }
123
124 double getX3Centroid() override { return 0.5 * (this->getX3Minimum() + this->getX3Maximum()); }
125 double getX3Minimum() override { return minX3; }
126 double getX3Maximum() override { return minX3 + deltaX3 * voxelMatrix.getNX3(); }
127
128 double getLengthX1() { return this->getX1Maximum() - minX1; }
129 double getLengthX2() { return this->getX2Maximum() - minX2; }
130 double getLengthX3() { return this->getX3Maximum() - minX3; }
131
132 void setCenterCoordinates(const double &x1, const double &x2, const double &x3) override;
133 void translate(const double &tx1, const double &tx2, const double &tx3) override;
134
135 bool isPointInGbObject3D(const double &x1p, const double &x2p, const double &x3p, bool &pointIsOnBoundary) override;
136 bool isPointInGbObject3D(const double &x1p, const double &x2p, const double &x3p) override;
137 bool isCellInsideGbObject3D(const double &x1a, const double &x2a, const double &x3a, const double &x1b,
138 const double &x2b, const double &x3b) override;
139 bool isCellCuttingGbObject3D(const double &x1a, const double &x2a, const double &x3a, const double &x1b,
140 const double &x2b, const double &x3b) override;
141 bool isCellInsideOrCuttingGbObject3D(const double &x1a, const double &x2a, const double &x3a, const double &x1b,
142 const double &x2b, const double &x3b) override;
143 // double getCellVolumeInsideGbObject3D(const double& x1a,const double& x2a,const double& x3a,const double&
144 // x1b,const double& x2b,const double& x3b);
145
147 {
148 throw UbException(__FILE__, __LINE__, UB_FUNCTION, "not implemented");
149 }
150 GbLine3D *createClippedLine3D(GbPoint3D & /*point1*/, GbPoint3D & /*point2*/) override
151 {
152 throw UbException(__FILE__, __LINE__, UB_FUNCTION, "not implemented");
153 }
154
155 std::vector<GbTriangle3D *> getSurfaceTriangleSet() override;
156 void addSurfaceTriangleSet(std::vector<UbTupleFloat3> &nodes, std::vector<UbTupleInt3> &triangles) override;
157
158 bool hasRaytracing() override { return true; }
159 /*|r| must be 1! einheitsvector!!*/
160 double getIntersectionRaytraceFactor(const double &x1, const double &x2, const double &x3, const double &rx1,
161 const double &rx2, const double &rx3) override;
162
163 std::string toString() override;
164
165 // virtuelle Methoden von UbObserver
166 void objectChanged(UbObservable * /*changedObject*/) override {}
167 void objectWillBeDeleted(UbObservable * /*objectForDeletion*/) override {}
168
169 template <class T>
171 template <class T>
173 void readMatrixFromVtiASCIIFile(std::string filename);
174 void readMatrixFromVtiAppendedFile(std::string filename);
175
176 void rotate90aroundX();
177 void rotate90aroundY();
178 void rotate90aroundZ();
179 void rotate90aroundX(double cX1, double cX2, double cX3);
180 void rotate90aroundY(double cX1, double cX2, double cX3);
181 void rotate90aroundZ(double cX1, double cX2, double cX3);
182 void mirrorX();
183 void mirrorY();
184 void mirrorZ();
185
186 void rotateAroundY(double theta);
187
188 void writeToLegacyVTKASCII(const std::string &fileName);
189 void writeToLegacyVTKBinary(const std::string &fileName);
190 void writeToVTKImageDataASCII(const std::string &fileName);
191 void writeToVTKImageDataAppended(const std::string &fileName);
192
194
196 long getNumberOfSolid();
197 long getNumberOfFluid();
198
199protected:
200 void findFluidNeighbor(int cx1, int cx2, int cx3);
203
204 std::vector<int> x1Nbr;
205 std::vector<int> x2Nbr;
206 std::vector<int> x3Nbr;
207
208 std::vector<int> x1NbrTemp;
209 std::vector<int> x2NbrTemp;
210 std::vector<int> x3NbrTemp;
211
212 using GbObject3D::isPointInGbObject3D; //Reason: This eliminates the need to program isPointInGbObject3D(GbPoint3D*)
213 // which would otherwise be "covered" here
214 using GbObject3D::setCenterCoordinates; // bring other overload into scope to avoid hiding base virtual
215 // method
216
217protected:
218 // for transfer
219 std::string filename;
220 bool transferViaFilename{ false };
221
223
224 int nodesX1{ 0 };
225 int nodesX2{ 0 };
226 int nodesX3{ 0 };
227 double lowerThreshold{ 0.0 }, upperThreshold{ 0.0 };
228
229 double minX1{ 0.0 };
230 double minX2{ 0.0 };
231 double minX3{ 0.0 };
232 double deltaX1{ 1.0 };
233 double deltaX2{ 1.0 };
234 double deltaX3{ 1.0 };
235
237
240};
241
243template <class T>
245{
246 using namespace std;
247 // UBLOG(logINFO,"GbVoxelMatrix3D::readMatrixFromFile \""<<filename<<"\"
248 // nodes("<<nodesX1<<"/"<<nodesX2<<"/"<<nodesX3<<") - start");
249 ifstream in(filename.c_str(), ios::binary);
250 if (!in)
251 throw UbException(UB_EXARGS, "could not open file " + filename);
252
253 in.seekg(0, ios::end); // Ende springen
254 fstream::off_type length = in.tellg(); // Position abfragen
255 in.seekg(0, ios::beg); // An den Anfang springen
256
257 // UBLOG(logINFO,"number of nodes = "<<nodesX1*nodesX2*nodesX3*sizeof(T)<<" file size = "<<(long)length);
258 // if( (nodesX1*nodesX2*nodesX3)*sizeof(float) != (long)length )
259 unsigned long long nofn = (unsigned long long)nodesX1 * (unsigned long long)nodesX2 * (unsigned long long)nodesX3 *
260 (unsigned long long)sizeof(T);
261 if (nofn != (unsigned long long)length) {
262 throw UbException(UB_EXARGS, "number of nodes(" + ub_system::toString(nofn) + ") doesn't match file size(" +
263 ub_system::toString((long)length) + ")");
264 }
265
266 // UBLOG(logINFO," - create GbVoxelMatrix3D");
267 // GbVoxelMatrix3D* voxelGeo = new GbVoxelMatrix3D(nodesX1,nodesX2,nodesX3,GbVoxelMatrix3D::FLUID);
269
270 // UBLOG(logINFO," - init values");
271 // float val;
272 T val;
273 for (int x3 = 0; x3 < nodesX3; x3++)
274 for (int x2 = 0; x2 < nodesX2; x2++)
275 for (int x1 = 0; x1 < nodesX1; x1++) {
276 // in.read((char*)&val,sizeof(float));
277 in.read((char *)&val, sizeof(T));
278 if (endian == BigEndian)
279 ub_system::swapByteOrder((unsigned char *)(&(val)), sizeof(T));
280 // if( UbMath::equal((double)val, threshold) )
281 // if( UbMath::greater((double)val, threshold) )
282 if ((double)val >= lowerThreshold && (double)val <= upperThreshold) {
283 (voxelMatrix)(x1, x2, x3) = GbVoxelMatrix3D::SOLID;
284 }
285 //(voxelMatrix)(x1, x2, x3) = (float)val;
286 }
287
288 // UBLOG(logINFO,"GbVoxelMatrix3D::readMatrixFromFile \""<<filename<<"\"
289 // nodes("<<nodesX1<<"/"<<nodesX2<<"/"<<nodesX3<<") - end");
290}
291
293template <class T>
295{
296 using namespace std;
297 UBLOG(logINFO, "GbVoxelMatrix3D::readMatrixFromRawFile \"" << filename << "\" nodes(" << nodesX1 << "/" << nodesX2
298 << "/" << nodesX3 << ") - start");
299
300 FILE *file;
301 file = fopen(filename.c_str(), "rb");
302 if (file == NULL) {
303 throw UbException(UB_EXARGS, "Could not open file " + filename);
304 }
305
306 // obtain file size:
307 fseek(file, 0, SEEK_END);
308 unsigned long int length = ftell(file);
309 rewind(file);
310
311 UBLOG(logINFO, "number of nodes = " << (long)nodesX1 * (long)nodesX2 * (long)nodesX3 << " file size = " << length);
312
313 unsigned long int nofn = (long)nodesX1 * (long)nodesX2 * (long)nodesX3 * (long)sizeof(T);
314 if (nofn != length) {
315 // throw UbException(UB_EXARGS, "number of nodes("+ub_system::toString(nofn)+") doesn't match file
316 // size("+ub_system::toString(length)+")");
317 }
318
319 UBLOG(logINFO, " - create GbVoxelMatrix3D");
321
323
324 UBLOG(logINFO, " - read file to matrix");
325 fread(readMatrix.getStartAdressOfSortedArray(0, 0, 0), sizeof(T), readMatrix.getDataVector().size(), file);
326 fclose(file);
327
328 UBLOG(logINFO, " - init values");
329
330 numberOfSolid = 0;
331 T val;
332 for (int x3 = 0; x3 < nodesX3; x3++)
333 for (int x2 = 0; x2 < nodesX2; x2++)
334 for (int x1 = 0; x1 < nodesX1; x1++) {
335 val = readMatrix(x1, x2, x3);
336
337 if (endian == BigEndian) {
338 ub_system::swapByteOrder((unsigned char *)(&(val)), sizeof(T));
339 }
340
341 if ((double)val >= lowerThreshold && (double)val <= upperThreshold) {
343 }
344 }
345
346 UBLOG(logINFO, "GbVoxelMatrix3D::readMatrixFromRawFile \"" << filename << "\" nodes(" << nodesX1 << "/" << nodesX2
347 << "/" << nodesX3 << ") - end");
348}
349
350#endif
351
size_type getNX3() const
Definition CbArray3D.h:346
typename std::vector< value_type >::const_reference const_reference
Definition CbArray3D.h:153
size_type getNX2() const
Definition CbArray3D.h:345
typename IndexClass::size_type size_type
Definition CbArray3D.h:151
size_type getNX1() const
Definition CbArray3D.h:344
typename std::vector< value_type >::reference reference
Definition CbArray3D.h:152
This Class provides basic 3D line objects.
Definition GbLine3D.h:59
This Interface provides basic 3D geometry objects methods.
Definition GbObject3D.h:64
virtual void setCenterCoordinates(const double &, const double &, const double &)
Definition GbObject3D.h:129
This Class provides basic 3D point objects.
Definition GbPoint3D.h:49
This Class provides basic 3D triangle objects.
void setVoxelMatrixMininum(double minX1, double minX2, double minX3)
CbArray3D< char > flagMatrix
GbLine3D * createClippedLine3D(GbPoint3D &, GbPoint3D &) override
double getX3Centroid() override
void objectChanged(UbObservable *) override
void finalize() override
std::vector< int > x1Nbr
void objectWillBeDeleted(UbObservable *) override
void setAddSurfaceTriangleSetFlag(bool flag)
void setVoxelMatrixDelta(double deltaX1, double deltaX2, double deltaX3)
~GbVoxelMatrix3D() override=default
double getX1Minimum() override
Matrix3D::reference operator()(const Matrix3D::size_type &x1, const Matrix3D::size_type &x2, const Matrix3D::size_type &x3)
CbArray3D< float > Matrix3D
double getX2Centroid() override
double getX1Maximum() override
double getX3Maximum() override
bool hasRaytracing() override
void setThreshold(double lowerThreshold, double upperThreshold)
void setVoxelMatrixDeltaX3(double deltaX3)
std::vector< int > x2NbrTemp
double getX2Maximum() override
double getX2Minimum() override
std::vector< int > x2Nbr
std::vector< int > x3Nbr
double getX1Centroid() override
void setVoxelMatrixDeltaX1(double deltaX1)
void setVoxelMatrixMinX3(double minX3)
void setVoxelMatrixDeltaX2(double deltaX2)
void setTransferViaFilename(bool transferViaFilename, std::string filename)
std::vector< int > x1NbrTemp
std::vector< int > x3NbrTemp
void setVoxelMatrixMinX1(double minX1)
std::string filename
Matrix3D::const_reference operator()(const Matrix3D::size_type &x1, const Matrix3D::size_type &x2, const Matrix3D::size_type &x3) const
GbPoint3D * calculateInterSectionPoint3D(GbPoint3D &, GbPoint3D &)
void setVoxelMatrixMinX2(double minX2)
double getX3Minimum() override
Observable object.
Observer.
Definition UbObserver.h:51
std::shared_ptr< T > SPtr
void calculateNumberOfSolidAndFluid()
std::string toString() override
void addSurfaceTriangleSet(std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleInt3 > &triangles) override
void findFluidNeighbor(int cx1, int cx2, int cx3)
std::vector< GbTriangle3D * > getSurfaceTriangleSet() override
virtual bool isPointInGbObject3D(GbPoint3D *p)
bool isCellInsideGbObject3D(const double &x1a, const double &x2a, const double &x3a, const double &x1b, const double &x2b, const double &x3b) override
static const float FLUID
void rotateAroundY(double theta)
void readMatrixFromVtiASCIIFile(std::string filename)
Reads a VTI file in ASCII format and fills the voxel matrix applying the thresholds....
void setClosedVoidSpaceToSolid()
bool isCellInsideOrCuttingGbObject3D(const double &x1a, const double &x2a, const double &x3a, const double &x1b, const double &x2b, const double &x3b) override
void writeToLegacyVTKBinary(const std::string &fileName)
bool isPointInGbObject3D(const double &x1p, const double &x2p, const double &x3p, bool &pointIsOnBoundary) override
void translate(const double &tx1, const double &tx2, const double &tx3) override
double getIntersectionRaytraceFactor(const double &x1, const double &x2, const double &x3, const double &rx1, const double &rx2, const double &rx3) override
void readBufferedMatrixFromRawFile(std::string filename, GbVoxelMatrix3D::Endian endian)
static const float SOLID
void writeToVTKImageDataAppended(const std::string &fileName)
void writeToVTKImageDataASCII(const std::string &fileName)
void writeToLegacyVTKASCII(const std::string &fileName)
void readMatrixFromRawFile(std::string filename, GbVoxelMatrix3D::Endian endian)
bool isCellCuttingGbObject3D(const double &x1a, const double &x2a, const double &x3a, const double &x1b, const double &x2b, const double &x3b) override
void readMatrixFromVtiAppendedFile(std::string filename)
Reads a VTI file in appended binary format and fills the voxel matrix applying the thresholds....
void setCenterCoordinates(const double &x1, const double &x2, const double &x3) override
GbVoxelMatrix3D * clone() override
UbTypeOp< typenameUbDuoT< N, UbDuo< A, B > >::ResultT >::RefT val(UbDuo< A, B > &d)
Definition UbTuple.h:393
#define UB_FUNCTION
UbException usage: UB_THROW( UbException("error message") ); UB_THROW( UbException(FILE,...
Definition UbException.h:69
#define UBLOG(level, logtext)
Definition UbLogger.h:327
#define UB_EXARGS
Definition UbException.h:73
@ logINFO
Definition UbLogger.h:54
std::string toString(const T &x, int precision=15)
Definition UbSystem.h:199
void swapByteOrder(unsigned char *toSwap, int length)
Definition UbSystem.h:439