Chaste Release::3.1
TrianglesMeshReader.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 
00037 #ifndef _TRIANGLESMESHREADER_HPP_
00038 #define _TRIANGLESMESHREADER_HPP_
00039 
00040 #include <vector>
00041 #include <string>
00042 #include <fstream>
00043 #include "AbstractMeshReader.hpp"
00044 
00051 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00052 class TrianglesMeshReader : public AbstractMeshReader<ELEMENT_DIM,SPACE_DIM>
00053 {
00054 
00055     friend class TestTrianglesMeshReader;//for testing
00056 
00057 private:
00058 
00059     bool mIndexFromZero;            
00061     std::string mFilesBaseName;     
00063     std::ifstream mNodesFile;       
00064     std::ifstream mElementsFile;    
00065     std::ifstream mFacesFile;       
00066     std::ifstream mNclFile;         
00067     std::ifstream mCableElementsFile; 
00069     std::streampos mNodeFileDataStart; 
00070     std::streamoff mNodeItemWidth;  
00071     std::streampos mElementFileDataStart; 
00072     std::streamoff mElementItemWidth;  
00073     std::streampos mFaceFileDataStart; 
00074     std::streamoff mFaceItemWidth;  
00075     std::streampos mNclFileDataStart; 
00076     std::streamoff mNclItemWidth;  
00078     unsigned mNumNodes;             
00079     unsigned mNumElements;          
00080     unsigned mNumFaces;             
00081     unsigned mNumCableElements;     
00083     unsigned mNodesRead;            
00084     unsigned mElementsRead;         
00085     unsigned mCableElementsRead;    
00086     unsigned mFacesRead;            
00087     unsigned mBoundaryFacesRead;    
00088     std::vector<unsigned> mOneDimBoundary; 
00090     unsigned mNumNodeAttributes;    
00091     std::vector<double> mNodeAttributes; 
00092     unsigned mMaxNodeBdyMarker;     
00093     unsigned mNumElementNodes;      
00094     unsigned mNumElementAttributes; 
00095     unsigned mNumFaceAttributes;    
00096     unsigned mNumCableElementAttributes; 
00098     unsigned mOrderOfElements;      
00099     unsigned mOrderOfBoundaryElements; 
00100     unsigned mNodesPerElement;      
00101     unsigned mNodesPerBoundaryElement; 
00103     unsigned mMaxContainingElements; 
00105     bool mEofException; 
00107     bool mReadContainingElementOfBoundaryElement; 
00108     bool mFilesAreBinary; 
00109     bool mMeshIsHexahedral; 
00110     bool mNclFileAvailable; 
00112     char* mNodeFileReadBuffer; 
00113     char* mElementFileReadBuffer; 
00114     char* mFaceFileReadBuffer; 
00116     bool mNodePermutationDefined; 
00117     std::vector<unsigned> mPermutationVector; 
00118     std::vector<unsigned> mInversePermutationVector; 
00120 //    /** The containing element for each boundary element (obtaining by doing tetgen with the -nn flag).
00121 //     *  In a std::vector rather than the struct to save space if not read.
00122 //     */
00123 //    std::vector<unsigned> mContainingElementsOfBoundaryElement;
00124 //
00125 //    unsigned mIndexIntoContainingElementsVector; /**< Which index to use when GetNextContainingElementOfBoundaryElement() is called */
00126 
00127 public:
00128 
00141     TrianglesMeshReader(std::string pathBaseName,
00142                         unsigned orderOfElements=1,
00143                         unsigned orderOfBoundaryElements=1,
00144                         bool readContainingElementsForBoundaryElements=false);
00145 
00149      ~TrianglesMeshReader();
00150 
00152     unsigned GetNumElements() const;
00153 
00155     unsigned GetNumNodes() const;
00156 
00158     unsigned GetNumFaces() const;
00159 
00161     unsigned GetNumCableElements() const;
00162 
00164     unsigned GetNumElementAttributes() const;
00165 
00167     unsigned GetNumFaceAttributes() const;
00168 
00170     unsigned GetNumCableElementAttributes() const;
00171 
00173     void Reset();
00174 
00176     std::vector<double> GetNextNode();
00177 
00179     ElementData GetNextElementData();
00180 
00182     ElementData GetNextFaceData();
00183 
00185     ElementData GetNextCableElementData();
00186 
00187 
00191     unsigned GetOrderOfElements()
00192     {
00193         return mOrderOfElements;
00194     }
00198     unsigned GetOrderOfBoundaryElements()
00199     {
00200         return mOrderOfBoundaryElements;
00201     }
00202 
00206     bool GetReadContainingElementOfBoundaryElement()
00207     {
00208         return mReadContainingElementOfBoundaryElement;
00209     }
00210 
00214     std::vector<double> GetNodeAttributes();
00215 
00222     std::vector<double> GetNode(unsigned index);
00223 
00230     ElementData GetElementData(unsigned index);
00231 
00238     ElementData GetFaceData(unsigned index);
00239 
00247     std::vector<unsigned> GetContainingElementIndices(unsigned index);
00248 
00249 
00250     /*** Returns true if reading binary files, false if reading ascii files */
00251     bool IsFileFormatBinary();
00252 
00258     bool HasNclFile();
00259 
00265     void SetReadBufferSize(unsigned bufferSize);
00266 
00272     void SetNodePermutation(std::vector<unsigned>& rPermutationVector);
00273 
00274 private:
00275 
00277     void OpenFiles();
00278 
00280     void OpenNodeFile();
00281 
00283     void OpenElementsFile();
00284 
00286     void OpenFacesFile();
00287 
00289     void OpenNclFile();
00290 
00292     void OpenCableElementsFile();
00293 
00295     void ReadHeaders();
00296 
00298     void CloseFiles();
00299 
00306     void GetNextLineFromStream(std::ifstream& rFileStream, std::string& rRawLine);
00307 
00317     template<class T_DATA, class T_ATTR>
00318     void GetNextItemFromStream(std::ifstream& rFileStream, unsigned expectedItemNumber,
00319                                std::vector<T_DATA>& rDataPacket, const unsigned& rNumAttributes,
00320                                std::vector<T_ATTR>& rAttributes);
00321 
00323     std::string GetMeshFileBaseName();
00324 
00326     void GetOneDimBoundary();
00327 
00335     void EnsureIndexingFromZero(std::vector<unsigned>& rNodeIndices);
00336 
00337 };
00338 
00339 #endif //_TRIANGLESMESHREADER_HPP_