VertexMeshReader.cpp

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 #include "VertexMeshReader.hpp"
00029 #include "Exception.hpp"
00030 
00031 #include <sstream>
00032 
00033 
00034 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00035 VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::VertexMeshReader(std::string pathBaseName)
00036     : mFilesBaseName(pathBaseName),
00037       mIndexFromZero(false), // initially assume that nodes are not numbered from zero
00038       mNumNodes(0),
00039       mNumElements(0),
00040       mNodesRead(0),
00041       mElementsRead(0),
00042       mNumElementAttributes(0)
00043 {
00044     OpenFiles();
00045     ReadHeaders();
00046 }
00047 
00048 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00049 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumElements() const
00050 {
00051     return mNumElements;
00052 }
00053 
00054 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00055 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumNodes() const
00056 {
00057     return mNumNodes;
00058 }
00059 
00060 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00061 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumElementAttributes() const
00062 {
00063     return mNumElementAttributes;
00064 }
00065 
00066 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00067 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumFaces() const
00068 {
00070     return 0;
00071 }
00072 
00073 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00074 ElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextFaceData()
00075 {
00077     ElementData ret;
00078     ret.NodeIndices = std::vector<unsigned>();
00079     ret.AttributeValue = 0;
00080     return ret;
00081 }
00082 
00083 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00084 unsigned VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumEdges() const
00085 {
00087     return 0;
00088 }
00089 
00090 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00091 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::Reset()
00092 {
00093     CloseFiles();
00094     OpenFiles();
00095     ReadHeaders();
00096 
00097     mNodesRead = 0;
00098     mElementsRead = 0;
00099 }
00100 
00101 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00102 std::vector<double> VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextNode()
00103 {
00104     std::vector<double> node_data;
00105 
00106     std::string buffer;
00107     GetNextLineFromStream(mNodesFile, buffer);
00108 
00109     std::stringstream buffer_stream(buffer);
00110 
00111     unsigned index;
00112     buffer_stream >> index;
00113 
00114     unsigned offset = mIndexFromZero ? 0 : 1;
00115     if (index != mNodesRead + offset)
00116     {
00117         EXCEPTION("Data for node " << mNodesRead << " missing");
00118     }
00119 
00120     double node_value;
00121     for (unsigned i=0; i<SPACE_DIM+1; i++)
00122     {
00123         buffer_stream >> node_value;
00124         node_data.push_back(node_value);
00125     }
00126 
00127     mNodesRead++;
00128     return node_data;
00129 }
00130 
00131 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00132 ElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementData()
00133 {
00134     // Create data structure for this element
00135     ElementData element_data;
00136 
00137     std::string buffer;
00138     GetNextLineFromStream(mElementsFile, buffer);
00139 
00140     std::stringstream buffer_stream(buffer);
00141 
00142     unsigned element_index;
00143     buffer_stream >> element_index;
00144 
00145     unsigned offset = mIndexFromZero ? 0 : 1;
00146     if (element_index != mElementsRead + offset)
00147     {
00148         EXCEPTION("Data for element " << mElementsRead << " missing");
00149     }
00150 
00151     unsigned num_nodes_in_element;
00152     buffer_stream >> num_nodes_in_element;
00153 
00154     // Store node indices owned by this element
00155     unsigned node_index;
00156     for (unsigned i=0; i<num_nodes_in_element; i++)
00157     {
00158         buffer_stream >> node_index;
00159         element_data.NodeIndices.push_back(node_index - offset);
00160     }
00161 
00162     if (mNumElementAttributes > 0)
00163     {
00164         assert(mNumElementAttributes == 1);
00165 
00166         unsigned attribute_value;
00167         buffer_stream >> attribute_value;
00168         element_data.AttributeValue = attribute_value;
00169     }
00170     else
00171     {
00172         element_data.AttributeValue = 0;
00173     }
00174 
00175     mElementsRead++;
00176     return element_data;
00177 }
00178 
00179 
00180 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00181 VertexElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementDataWithFaces()
00182 {
00183     // Create data structure for this element
00184     VertexElementData element_data;
00185 
00186     std::string buffer;
00187     GetNextLineFromStream(mElementsFile, buffer);
00188 
00189     std::stringstream buffer_stream(buffer);
00190 
00191     unsigned element_index;
00192     buffer_stream >> element_index;
00193 
00194     unsigned offset = mIndexFromZero ? 0 : 1;
00195     if (element_index != mElementsRead + offset)
00196     {
00197         EXCEPTION("Data for element " << mElementsRead << " missing");
00198     }
00199 
00200     // Get number of nodes owned by this element
00201     unsigned num_nodes_in_element;
00202     buffer_stream >> num_nodes_in_element;
00203 
00204     // Store node indices owned by this element
00205     unsigned node_index;
00206     for (unsigned i=0; i<num_nodes_in_element; i++)
00207     {
00208         buffer_stream >> node_index;
00209         element_data.NodeIndices.push_back(node_index - offset);
00210     }
00211 
00212     // Get number of faces owned by this element
00213     unsigned num_faces_in_element;
00214     buffer_stream >> num_faces_in_element;
00215 
00216     element_data.Faces.resize(num_faces_in_element);
00217     for (unsigned j=0; j<num_faces_in_element; j++)
00218     {
00219         // Create data structure for this face
00220         ElementData face_data;
00221 
00222         // Get face index
00223         unsigned face_index;
00224         buffer_stream >> face_index;
00225         face_data.AttributeValue = face_index;
00226 
00227         // Get number of nodes owned by this face
00228         unsigned num_nodes_in_face;
00229         buffer_stream >> num_nodes_in_face;
00230 
00231         // Store node indices owned by this face
00232         unsigned node_index;
00233         for (unsigned i=0; i<num_nodes_in_face; i++)
00234         {
00235             buffer_stream >> node_index;
00236             face_data.NodeIndices.push_back(node_index - offset);
00237         }
00238 
00240 
00241         element_data.Faces[j] = face_data;
00242     }
00243 
00244     if (mNumElementAttributes > 0)
00245     {
00246         assert(mNumElementAttributes==1);
00247 
00248         unsigned attribute_value;
00249         buffer_stream >> attribute_value;
00250         element_data.AttributeValue = attribute_value;
00251     }
00252     else
00253     {
00254         element_data.AttributeValue = 0;
00255     }
00256 
00257     mElementsRead++;
00258     return element_data;
00259 }
00260 
00261 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00262 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenFiles()
00263 {
00264     OpenNodeFile();
00265     OpenElementsFile();
00266 }
00267 
00268 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00269 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenNodeFile()
00270 {
00271     // Nodes definition
00272     std::string file_name = mFilesBaseName + ".node";
00273     mNodesFile.open(file_name.c_str());
00274     if (!mNodesFile.is_open())
00275     {
00276         EXCEPTION("Could not open data file: " + file_name);
00277     }
00278 }
00279 
00280 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00281 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenElementsFile()
00282 {
00283     // Elements definition
00284     std::string file_name;
00285     file_name = mFilesBaseName + ".cell";
00286 
00287     mElementsFile.open(file_name.c_str());
00288     if (!mElementsFile.is_open())
00289     {
00290         EXCEPTION("Could not open data file: " + file_name);
00291     }
00292 }
00293 
00294 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00295 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::ReadHeaders()
00296 {
00297     std::string buffer;
00298 
00299     GetNextLineFromStream(mNodesFile, buffer);
00300     std::stringstream buffer_stream(buffer);
00301     buffer_stream >> mNumNodes >> mNumNodeAttributes;
00302 
00303     // Get the next line to see if nodes are indexed from zero or not
00304     GetNextLineFromStream(mNodesFile, buffer);
00305     std::stringstream node_buffer_stream(buffer);
00306 
00307     unsigned first_index;
00308     node_buffer_stream >> first_index;
00309     assert(first_index == 0 || first_index == 1);
00310     mIndexFromZero = (first_index == 0);
00311 
00312     // Close, reopen, skip header
00313     mNodesFile.close();
00314     OpenNodeFile();
00315     GetNextLineFromStream(mNodesFile, buffer);
00316 
00317     GetNextLineFromStream(mElementsFile, buffer);
00318     std::stringstream element_buffer_stream(buffer);
00319 
00320     element_buffer_stream >> mNumElements >> mNumElementAttributes;
00321 }
00322 
00323 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00324 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::CloseFiles()
00325 {
00326     mNodesFile.close();
00327     mElementsFile.close();
00328 }
00329 
00330 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00331 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextLineFromStream(std::ifstream& fileStream, std::string& rawLine)
00332 {
00333     bool line_is_blank;
00334 
00335     do
00336     {
00337         getline(fileStream, rawLine);
00338 
00339         if (fileStream.eof())
00340         {
00341             EXCEPTION("Cannot get the next line from node or element file due to incomplete data");
00342         }
00343 
00344         // Get rid of any comment
00345         rawLine = rawLine.substr(0,rawLine.find('#', 0));
00346 
00347         line_is_blank = (rawLine.find_first_not_of(" \t", 0) == std::string::npos);
00348     }
00349     while (line_is_blank);
00350 }
00351 
00353 // Explicit instantiation
00355 
00356 template class VertexMeshReader<1,1>;
00357 template class VertexMeshReader<1,2>;
00358 template class VertexMeshReader<1,3>;
00359 template class VertexMeshReader<2,2>;
00360 template class VertexMeshReader<2,3>;
00361 template class VertexMeshReader<3,3>;
Generated on Thu Dec 22 13:00:16 2011 for Chaste by  doxygen 1.6.3