Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
TrianglesMeshReader.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36
37#ifndef _TRIANGLESMESHREADER_HPP_
38#define _TRIANGLESMESHREADER_HPP_
39
40#include <vector>
41#include <string>
42#include <fstream>
43#include "AbstractMeshReader.hpp"
44
51template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
52class TrianglesMeshReader : public AbstractMeshReader<ELEMENT_DIM,SPACE_DIM>
53{
54
55 friend class TestTrianglesMeshReader;//for testing
56
57private:
58
61 std::string mFilesBaseName;
63 std::ifstream mNodesFile;
64 std::ifstream mElementsFile;
65 std::ifstream mFacesFile;
66 std::ifstream mNclFile;
67 std::ifstream mCableElementsFile;
69 std::streampos mNodeFileDataStart;
70 std::streamoff mNodeItemWidth;
71 std::streampos mElementFileDataStart;
72 std::streamoff mElementItemWidth;
73 std::streampos mFaceFileDataStart;
74 std::streamoff mFaceItemWidth;
75 std::streampos mNclFileDataStart;
76 std::streamoff mNclItemWidth;
78 unsigned mNumNodes;
79 unsigned mNumElements;
80 unsigned mNumFaces;
83 unsigned mNodesRead;
84 unsigned mElementsRead;
86 unsigned mFacesRead;
88 unsigned mNclItemsRead;
89 std::vector<unsigned> mOneDimBoundary;
92 std::vector<double> mNodeAttributes;
118 std::vector<unsigned> mPermutationVector;
119 std::vector<unsigned> mInversePermutationVector;
121// /** The containing element for each boundary element (obtaining by doing tetgen with the -nn flag).
122// * In a std::vector rather than the struct to save space if not read.
123// */
124// std::vector<unsigned> mContainingElementsOfBoundaryElement;
125//
126// unsigned mIndexIntoContainingElementsVector; /**< Which index to use when GetNextContainingElementOfBoundaryElement() is called */
127
128public:
129
142 TrianglesMeshReader(std::string pathBaseName,
143 unsigned orderOfElements=1,
144 unsigned orderOfBoundaryElements=1,
145 bool readContainingElementsForBoundaryElements=false);
146
151
153 unsigned GetNumElements() const;
154
156 unsigned GetNumNodes() const;
157
159 unsigned GetNumFaces() const;
160
162 unsigned GetNumCableElements() const;
163
165 unsigned GetNumElementAttributes() const;
166
168 unsigned GetNumFaceAttributes() const;
169
171 unsigned GetNumCableElementAttributes() const;
172
174 void Reset();
175
177 std::vector<double> GetNextNode();
178
181
184
187
188
193 {
194 return mOrderOfElements;
195 }
200 {
202 }
203
211
215 std::vector<double> GetNodeAttributes();
216
223 std::vector<double> GetNode(unsigned index);
224
231 ElementData GetElementData(unsigned index);
232
239 ElementData GetFaceData(unsigned index);
240
248 std::vector<unsigned> GetContainingElementIndices(unsigned index);
249
250
251 /*** @return true if reading binary files, false if reading ascii files */
252 bool IsFileFormatBinary();
253
259 bool HasNclFile();
260
266 void SetReadBufferSize(unsigned bufferSize);
267
275 void SetNodePermutation(std::vector<unsigned>& rPermutationVector);
276
280 bool HasNodePermutation();
281
285 const std::vector<unsigned>& rGetNodePermutation();
286
287
288private:
289
291 void OpenFiles();
292
294 void OpenNodeFile();
295
297 void OpenElementsFile();
298
300 void OpenFacesFile();
301
303 void OpenNclFile();
304
307
309 void ReadHeaders();
310
312 void CloseFiles();
313
320 void GetNextLineFromStream(std::ifstream& rFileStream, std::string& rRawLine);
321
332 template<class T_DATA>
333 void GetNextItemFromStream(std::ifstream& rFileStream, unsigned expectedItemNumber,
334 std::vector<T_DATA>& rDataPacket, const unsigned& rNumAttributes,
335 std::vector<double>& rAttributes);
336
338 std::string GetMeshFileBaseName();
339
341 void GetOneDimBoundary();
342
350 void EnsureIndexingFromZero(std::vector<unsigned>& rNodeIndices);
351};
352
353#endif //_TRIANGLESMESHREADER_HPP_
ElementData GetFaceData(unsigned index)
std::vector< double > mNodeAttributes
unsigned GetNumElementAttributes() const
std::vector< double > GetNextNode()
unsigned GetNumFaceAttributes() const
ElementData GetNextCableElementData()
std::streamoff mElementItemWidth
std::vector< double > GetNodeAttributes()
std::streampos mNodeFileDataStart
std::vector< unsigned > GetContainingElementIndices(unsigned index)
std::vector< double > GetNode(unsigned index)
unsigned GetNumElements() const
unsigned GetNumCableElements() const
std::streampos mFaceFileDataStart
void GetNextItemFromStream(std::ifstream &rFileStream, unsigned expectedItemNumber, std::vector< T_DATA > &rDataPacket, const unsigned &rNumAttributes, std::vector< double > &rAttributes)
void EnsureIndexingFromZero(std::vector< unsigned > &rNodeIndices)
std::vector< unsigned > mPermutationVector
unsigned GetNumCableElementAttributes() const
std::vector< unsigned > mInversePermutationVector
std::vector< unsigned > mOneDimBoundary
void SetNodePermutation(std::vector< unsigned > &rPermutationVector)
const std::vector< unsigned > & rGetNodePermutation()
std::streampos mElementFileDataStart
void GetNextLineFromStream(std::ifstream &rFileStream, std::string &rRawLine)
void SetReadBufferSize(unsigned bufferSize)
ElementData GetElementData(unsigned index)
std::streampos mNclFileDataStart