TrianglesMeshReader.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00030 #ifndef _TRIANGLESMESHREADER_HPP_
00031 #define _TRIANGLESMESHREADER_HPP_
00032 
00033 #include <vector>
00034 #include <string>
00035 #include <fstream>
00036 #include "AbstractMeshReader.hpp"
00037 
00044 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00045 class TrianglesMeshReader : public AbstractMeshReader<ELEMENT_DIM,SPACE_DIM>
00046 {
00047 
00048     friend class TestTrianglesMeshReader;//for testing
00049 
00050 private:
00051 
00052     bool mIndexFromZero;            
00054     std::string mFilesBaseName;     
00056     std::ifstream mNodesFile;       
00057     std::ifstream mElementsFile;    
00058     std::ifstream mFacesFile;       
00059     std::ifstream mNclFile;         
00061     std::streampos mNodeFileDataStart; 
00062     std::streamoff mNodeItemWidth;  
00063     std::streampos mElementFileDataStart; 
00064     std::streamoff mElementItemWidth;  
00065     std::streampos mFaceFileDataStart; 
00066     std::streamoff mFaceItemWidth;  
00067     std::streampos mNclFileDataStart; 
00068     std::streamoff mNclItemWidth;  
00070     unsigned mNumNodes;             
00071     unsigned mNumElements;          
00072     unsigned mNumFaces;             
00074     unsigned mNodesRead;            
00075     unsigned mElementsRead;         
00076     unsigned mFacesRead;            
00077     unsigned mBoundaryFacesRead;    
00078     std::vector<unsigned> mOneDimBoundary; 
00080     unsigned mNumNodeAttributes;    
00081     std::vector<double> mNodeAttributes; 
00082     unsigned mMaxNodeBdyMarker;     
00083     unsigned mNumElementNodes;      
00084     unsigned mNumElementAttributes; 
00085     unsigned mNumFaceAttributes;    
00087     unsigned mOrderOfElements;      
00088     unsigned mOrderOfBoundaryElements; 
00089     unsigned mNodesPerElement;      
00090     unsigned mNodesPerBoundaryElement; 
00092     unsigned mMaxContainingElements; 
00094     bool mEofException; 
00096     bool mReadContainingElementOfBoundaryElement; 
00097     bool mFilesAreBinary; 
00098     bool mMeshIsHexahedral; 
00099     bool mNclFileAvailable; 
00101     char* mNodeFileReadBuffer; 
00102     char* mElementFileReadBuffer; 
00103     char* mFaceFileReadBuffer; 
00105     bool mNodePermutationDefined; 
00106     std::vector<unsigned> mPermutationVector; 
00107     std::vector<unsigned> mInversePermutationVector; 
00109 //    /** The containing element for each boundary element (obtaining by doing tetgen with the -nn flag).
00110 //     *  In a std::vector rather than the struct to save space if not read.
00111 //     */
00112 //    std::vector<unsigned> mContainingElementsOfBoundaryElement;
00113 //
00114 //    unsigned mIndexIntoContainingElementsVector; /**< Which index to use when GetNextContainingElementOfBoundaryElement() is called */
00115 
00116 public:
00117 
00130     TrianglesMeshReader(std::string pathBaseName,
00131                         unsigned orderOfElements=1,
00132                         unsigned orderOfBoundaryElements=1,
00133                         bool readContainingElementsForBoundaryElements=false);
00134 
00138      ~TrianglesMeshReader();
00139 
00141     unsigned GetNumElements() const;
00142 
00144     unsigned GetNumNodes() const;
00145 
00147     unsigned GetNumFaces() const;
00148 
00150     unsigned GetNumElementAttributes() const;
00151 
00153     unsigned GetNumFaceAttributes() const;
00154 
00156     void Reset();
00157 
00159     std::vector<double> GetNextNode();
00160 
00162     ElementData GetNextElementData();
00163 
00165     ElementData GetNextFaceData();
00166 
00167 
00171     unsigned GetOrderOfElements()
00172     {
00173         return mOrderOfElements;
00174     }
00178     unsigned GetOrderOfBoundaryElements()
00179     {
00180         return mOrderOfBoundaryElements;
00181     }
00182 
00186     bool GetReadContainingElementOfBoundaryElement()
00187     {
00188         return mReadContainingElementOfBoundaryElement;
00189     }
00190 
00194     std::vector<double> GetNodeAttributes();
00195 
00202     std::vector<double> GetNode(unsigned index);
00203 
00210     ElementData GetElementData(unsigned index);
00211 
00218     ElementData GetFaceData(unsigned index);
00219 
00227     std::vector<unsigned> GetContainingElementIndices(unsigned index);
00228 
00229 
00230     /*** Returns true if reading binary files, false if reading ascii files */
00231     bool IsFileFormatBinary();
00232 
00238     bool HasNclFile();
00239 
00245     void SetReadBufferSize(unsigned bufferSize);
00246 
00252     void SetNodePermutation(std::vector<unsigned>& rPermutationVector);
00253     
00254 private:
00255 
00257     void OpenFiles();
00258 
00260     void OpenNodeFile();
00261 
00263     void OpenElementsFile();
00264 
00266     void OpenFacesFile();
00267 
00269     void OpenNclFile();
00270 
00272     void ReadHeaders();
00273 
00275     void CloseFiles();
00276 
00283     void GetNextLineFromStream(std::ifstream& rFileStream, std::string& rRawLine);
00284 
00294     template<class T>
00295     void GetNextItemFromStream(std::ifstream& rFileStream, unsigned expectedItemNumber,
00296                                std::vector<T>& rDataPacket, const unsigned& rNumAttributes, std::vector<T>& rAttributes);
00297 
00299     std::string GetMeshFileBaseName();
00300 
00302     void GetOneDimBoundary();
00303 
00311     void EnsureIndexingFromZero(std::vector<unsigned>& rNodeIndices);
00312     
00313 };
00314 
00315 #endif //_TRIANGLESMESHREADER_HPP_

Generated on Mon Apr 18 11:35:35 2011 for Chaste by  doxygen 1.5.5