Chaste  Release::2017.1
TrianglesMeshReader.hpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF 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 
51 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
52 class TrianglesMeshReader : public AbstractMeshReader<ELEMENT_DIM,SPACE_DIM>
53 {
54 
55  friend class TestTrianglesMeshReader;//for testing
56 
57 private:
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;
81  unsigned mNumCableElements;
83  unsigned mNodesRead;
84  unsigned mElementsRead;
85  unsigned mCableElementsRead;
86  unsigned mFacesRead;
87  unsigned mBoundaryFacesRead;
88  unsigned mNclItemsRead;
89  std::vector<unsigned> mOneDimBoundary;
91  unsigned mNumNodeAttributes;
92  std::vector<double> mNodeAttributes;
93  unsigned mMaxNodeBdyMarker;
94  unsigned mNumElementNodes;
96  unsigned mNumFaceAttributes;
99  unsigned mOrderOfElements;
101  unsigned mNodesPerElement;
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 
128 public:
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 
208  {
210  }
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 
288 private:
289 
291  void OpenFiles();
292 
294  void OpenNodeFile();
295 
297  void OpenElementsFile();
298 
300  void OpenFacesFile();
301 
303  void OpenNclFile();
304 
306  void OpenCableElementsFile();
307 
309  void ReadHeaders();
310 
312  void CloseFiles();
313 
320  void GetNextLineFromStream(std::ifstream& rFileStream, std::string& rRawLine);
321 
331  template<class T_DATA>
332  void GetNextItemFromStream(std::ifstream& rFileStream, unsigned expectedItemNumber,
333  std::vector<T_DATA>& rDataPacket, const unsigned& rNumAttributes,
334  std::vector<double>& rAttributes);
335 
337  std::string GetMeshFileBaseName();
338 
340  void GetOneDimBoundary();
341 
349  void EnsureIndexingFromZero(std::vector<unsigned>& rNodeIndices);
350 };
351 
352 #endif //_TRIANGLESMESHREADER_HPP_
ElementData GetNextCableElementData()
std::vector< unsigned > mPermutationVector
unsigned GetNumFaceAttributes() const
unsigned GetNumNodes() const
std::ifstream mCableElementsFile
std::streampos mElementFileDataStart
std::streampos mFaceFileDataStart
void GetNextLineFromStream(std::ifstream &rFileStream, std::string &rRawLine)
TrianglesMeshReader(std::string pathBaseName, unsigned orderOfElements=1, unsigned orderOfBoundaryElements=1, bool readContainingElementsForBoundaryElements=false)
std::vector< unsigned > GetContainingElementIndices(unsigned index)
unsigned GetNumElementAttributes() const
unsigned GetNumCableElementAttributes() const
unsigned GetNumCableElements() const
unsigned GetNumElements() const
std::streamoff mElementItemWidth
ElementData GetNextElementData()
std::vector< double > mNodeAttributes
void SetNodePermutation(std::vector< unsigned > &rPermutationVector)
std::streampos mNodeFileDataStart
void SetReadBufferSize(unsigned bufferSize)
std::streamoff mFaceItemWidth
std::streampos mNclFileDataStart
std::streamoff mNodeItemWidth
std::vector< double > GetNextNode()
void EnsureIndexingFromZero(std::vector< unsigned > &rNodeIndices)
ElementData GetFaceData(unsigned index)
unsigned GetNumFaces() const
bool GetReadContainingElementOfBoundaryElement()
const std::vector< unsigned > & rGetNodePermutation()
std::vector< unsigned > mInversePermutationVector
std::streamoff mNclItemWidth
std::string GetMeshFileBaseName()
std::vector< double > GetNodeAttributes()
void GetNextItemFromStream(std::ifstream &rFileStream, unsigned expectedItemNumber, std::vector< T_DATA > &rDataPacket, const unsigned &rNumAttributes, std::vector< double > &rAttributes)
std::vector< double > GetNode(unsigned index)
ElementData GetElementData(unsigned index)
std::vector< unsigned > mOneDimBoundary