www.cemf.ir
triSurface.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #include "triSurface.hpp"
23 #include "error.hpp"
24 #include "iOstream.hpp"
25 #include "Vectors.hpp"
26 #include "triSurfaceKernels.hpp"
27 
28 
29 
30 namespace pFlow
31 {
33 (
34  uint32 basePointIndex,
35  span<realx3x3> tris,
36  realx3Vector& points,
37  uint32x3Vector& vertices,
38  realVector& area,
40 )
41 {
42  auto nt = tris.size();
43 
44  points.clear();
45  points.reserve(nt);
46 
47  vertices.clear();
48  vertices.resize(nt);
49 
50  area.clear();
51  area.resize(nt);
52 
53  normal.clear();
54  normal.resize(nt);
55 
56  for(auto i=0; i<nt; i++)
57  {
58  uint32x3 newTri;
59  const auto& tri = tris[i];
60  if(!pFlow::triangle::valid(tri.comp1(),tri.comp2(), tri.comp3()))
61  {
63  "Invalid triangle "<< tri << endl;
64  return false;
65  }
66 
67  if(auto np = find(points, tri.comp1()); np<0 )
68  {
69  points.push_back(tri.comp1()); // new point
70  newTri.comp1() = static_cast<uint32>( basePointIndex + points.size() - 1);
71  }
72  else
73  {
74  newTri.comp1() = basePointIndex+static_cast<uint32>(np);
75  }
76 
77  if(auto np = find(points, tri.comp2()); np<0 )
78  {
79  points.push_back(tri.comp2()); // new point
80  newTri.comp2() = static_cast<uint32>( basePointIndex + points.size() - 1);
81  }
82  else
83  {
84  newTri.comp2() = basePointIndex+static_cast<uint32>(np);
85  }
86 
87  if(auto np = find(points, tri.comp3()); np<0 )
88  {
89  points.push_back(tri.comp3()); // new point
90  newTri.comp3() = static_cast<uint32>( basePointIndex + points.size() - 1);
91  }
92  else
93  {
94  newTri.comp3() = basePointIndex+static_cast<uint32>(np);
95  }
96 
97  vertices[i] = newTri;
98  normal[i] = triangle::normal(tri.comp1(), tri.comp2(), tri.comp3());
99  area[i] = triangle::surface(tri.comp1(), tri.comp2(), tri.comp3());
100  }
101 
102  return true;
103 }
104 
105 }
106 
108 {
110  points_,
111  vertices_,
112  normals_);
113 }
114 
116 {
118  points_,
119  vertices_,
120  area_);
121 }
122 
124 (
125  const realx3x3Field_H &triangles
126 )
127 {
128  uint32 basePointIndex = numPoints();
129 
130  auto triData = triangles.getSpan();
131 
132  realx3Vector points("points");
133  uint32x3Vector vertices("vertices");
134  realVector area("area");
135  realx3Vector normal("normal");
136 
138  basePointIndex,
139  triData,
140  points,
141  vertices,
142  area,
143  normal))
144  {
146  "Error in constructing triSuface from raw data "<<triangles.name()<<endl;
147  return false;
148  }
149 
150  points_.append(points);
151  vertices_.append(vertices);
152  area_.append(area);
153  normals_.append(normal);
154 
155  return true;
156 }
157 
159 {
160  uint32 basePointIndex = numPoints();
161 
162  auto triData = triangles.getSpan();
163 
164  realx3Vector points("points");
165  uint32x3Vector vertices("vertices");
166  realVector area("area");
167  realx3Vector normal("normal");
168 
170  basePointIndex,
171  triData,
172  points,
173  vertices,
174  area,
175  normal))
176  {
178  "Error in constructing triSuface from raw data "<<triangles.name()<<endl;
179  return false;
180  }
181 
182  points_.append(points);
183  vertices_.append(vertices);
184  area_.append(area);
185  normals_.append(normal);
186 
187  return true;
188 }
189 
191 (
192  const objectFile &obj,
193  repository *owner
194 )
195 :
196  IOobject
197  (
198  obj,
200  owner
201  )
202 {
203  // this constructor is used by multiTrisurface and read operation is
204  // done in that class
205 }
206 
208 (
209  const objectFile &objf,
210  repository* owner,
211  const triSurface &surf
212 )
213 :
214  IOobject
215  (
216  objectFile
217  (
218  objf.name(),
219  objf.localPath(),
221  objf.wFlag()
222  ),
224  owner
225  ),
226  points_(surf.points_),
227  vertices_(surf.vertices_),
228  area_(surf.area_),
229  normals_(surf.normals_)
230 {}
231 
233 (
234  const realx3x3Field_H &triangles,
235  repository *owner
236 )
237 :
238  IOobject
239  (
240  objectFile(
241  triangles.name(),
242  "",
246  owner
247  )
248 {
249  if( !append(triangles) )
250  {
251  fatalExit;
252  }
253 }
254 
255 
256 
258 {
259  points_.clear();
260  if(!points_.read(is, iop, true))
261  {
262  ioErrorInFile(is.name(), is.lineNumber())<<
263  " when reading field "<< points_.name()<<endl;
264  return false;
265  }
266 
267  vertices_.clear();
268  if(!vertices_.read(is, iop, true))
269  {
270  ioErrorInFile(is.name(), is.lineNumber())<<
271  " when reading field "<< vertices_.name()<<endl;
272  return false;
273  }
274 
275  normals_.reallocate(vertices_.capacity(), vertices_.size());
276  area_.reallocate(vertices_.capacity(), vertices_.size());
277  calculateArea();
279  return true;
280 }
281 
282 bool pFlow::triSurface::write(iOstream &os, const IOPattern &iop) const
283 {
284  if( !points_.write(os, iop))
285  {
286  ioErrorInFile(os.name(), os.lineNumber())<<
287  "when writing field "<<points_.name()<<endl;
288  return false;
289  }
290 
291  if( !vertices_.write(os,iop) )
292  {
293  ioErrorInFile(os.name(), os.lineNumber())<<
294  "when writing field "<<vertices_.name()<<endl;
295  return false;
296  }
297 
298  return true;
299 }
pFlow::span
Definition: span.hpp:31
pFlow::triSurfaceKernels::calculateNormals
INLINE_FUNCTION_H bool calculateNormals(const realx3Field_D &points, const uint32x3Field_D &vertices, realx3Field_D &normals)
Definition: triSurfaceKernels.hpp:57
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::triSurface::normals_
realx3Field_D normals_
normal vector of triangles
Definition: triSurface.hpp:119
pFlow::triangle::valid
INLINE_FUNCTION_HD bool valid(const realx3 &p1, const realx3 &p2, const realx3 &p3)
Definition: triangleFunctions.hpp:49
pFlow::find
int64 find(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:69
pFlow::triple::comp3
INLINE_FUNCTION_HD T & comp3()
access component
Definition: triple.hpp:192
pFlow::Vector::reserve
void reserve(size_t cap)
Reserve capacity for vector Preserve the content.
Definition: Vector.hpp:284
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::span::size
INLINE_FUNCTION_HD uint32 size() const
Returns the number of elements in the span.
Definition: span.hpp:101
pFlow::Vector::getSpan
auto getSpan()
Definition: Vector.hpp:296
pFlow::triSurface::calculateNormals
bool calculateNormals()
Definition: triSurface.cpp:107
pFlow::triangle::normal
INLINE_FUNCTION_HD realx3 normal(const realx3 &p1, const realx3 &p2, const realx3 &p3)
Definition: triangleFunctions.hpp:38
pFlow::Vector::size
auto size() const
Size of the vector.
Definition: Vector.hpp:265
pFlow::objectFile::WRITE_ALWAYS
@ WRITE_ALWAYS
Definition: objectFile.hpp:43
Vectors.hpp
pFlow::triSurface::write
bool write(iOstream &os, const IOPattern &iop) const override
Definition: triSurface.cpp:282
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::Field
Definition: Field.hpp:36
pFlow::Field::name
word name() const
Definition: Field.hpp:182
pFlow::triSurface::points_
realx3Field_D points_
points of triangles
Definition: triSurface.hpp:110
pFlow
Definition: demGeometry.hpp:27
pFlow::IOobject
Definition: IOobject.hpp:35
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
triSurfaceKernels.hpp
pFlow::VectorSingle::getSpan
INLINE_FUNCTION_H auto getSpan()
Definition: VectorSingle.cpp:569
pFlow::triSurface::vertices_
uint32x3Field_D vertices_
vectices indices of triangles
Definition: triSurface.hpp:113
pFlow::objectFile::name
virtual const word & name() const
Definition: objectFile.hpp:101
pFlow::IOPattern
Definition: IOPattern.hpp:32
pFlow::triple::comp1
INLINE_FUNCTION_HD T & comp1()
access component
Definition: triple.hpp:168
pFlow::objectFile
Definition: objectFile.hpp:30
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::objectFile::READ_NEVER
@ READ_NEVER
Definition: objectFile.hpp:37
pFlow::triSurfaceKernels::calculateArea
INLINE_FUNCTION_H bool calculateArea(const realx3Field_D &points, const uint32x3Field_D &vertices, realField_D &area)
Definition: triSurfaceKernels.hpp:32
pFlow::triSurface::read
bool read(iIstream &is, const IOPattern &iop) override
Definition: triSurface.cpp:257
pFlow::Vector::name
const word & name() const
Name of the vector.
Definition: Vector.hpp:259
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::IOPattern::AllProcessorsSimilar
@ AllProcessorsSimilar
Definition: IOPattern.hpp:55
pFlow::objectFile::localPath
virtual const fileSystem & localPath() const
Definition: objectFile.hpp:107
pFlow::repository
Definition: repository.hpp:34
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
pFlow::triSurface::area_
realField_D area_
area of each triangle
Definition: triSurface.hpp:116
iOstream.hpp
pFlow::triSurface::triSurface
triSurface(const objectFile &obj, repository *owner)
Definition: triSurface.cpp:191
pFlow::triSurface
Definition: triSurface.hpp:103
pFlow::triple
A set of 3 variables that can be used for vector variables.
Definition: triple.hpp:36
pFlow::triSurface::calculateArea
bool calculateArea()
Definition: triSurface.cpp:115
pFlow::Vector< realx3 >
pFlow::convertToTriSurfaceComponents
bool convertToTriSurfaceComponents(uint32 basePointIndex, span< realx3x3 > tris, realx3Vector &points, uint32x3Vector &vertices, realVector &area, realx3Vector &normal)
Definition: triSurface.cpp:33
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::objectFile::wFlag
writeFlag wFlag() const
Definition: objectFile.hpp:119
triSurface.hpp
pFlow::triSurface::append
bool append(const realx3x3Field_H &triangles)
Definition: triSurface.cpp:124
pFlow::triangle::surface
INLINE_FUNCTION_HD real surface(const realx3 &p1, const realx3 &p2, const realx3 &p3)
Definition: triangleFunctions.hpp:30
pFlow::triple::comp2
INLINE_FUNCTION_HD T & comp2()
access component
Definition: triple.hpp:180
error.hpp