VertexMeshReader.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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         std::stringstream error;
00118         error << "Data for node " << mNodesRead << " missing";
00119         EXCEPTION(error.str());
00120     }
00121 
00122     double node_value;
00123     for (unsigned i=0; i<3; i++)
00124     {
00125         buffer_stream >> node_value;
00126         node_data.push_back(node_value);
00127     }
00128 
00129     mNodesRead++;
00130     return node_data;
00131 }
00132 
00133 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00134 ElementData VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementData()
00135 {
00136     ElementData element_data;
00137 
00138     std::string buffer;
00139     GetNextLineFromStream(mElementsFile, buffer);
00140 
00141     std::stringstream buffer_stream(buffer);
00142 
00143     unsigned element_index;
00144     buffer_stream >> element_index;
00145 
00146     unsigned offset = mIndexFromZero ? 0 : 1;
00147     if (element_index != mElementsRead + offset)
00148     {
00149         std::stringstream error;
00150         error << "Data for element " << mElementsRead << " missing";
00151         EXCEPTION(error.str());
00152     }
00153 
00154     unsigned num_nodes_in_element;
00155     buffer_stream >> num_nodes_in_element;
00156 
00157     unsigned node_index;
00158     for (unsigned i=0; i<num_nodes_in_element; i++)
00159     {
00160         buffer_stream >> node_index;
00161         element_data.NodeIndices.push_back(node_index - offset);
00162     }
00163 
00164     if (mNumElementAttributes > 0)
00165     {
00166         assert(mNumElementAttributes==1);
00167 
00168         unsigned attribute_value;
00169         buffer_stream >> attribute_value;
00170         element_data.AttributeValue = attribute_value;
00171     }
00172     else
00173     {
00174         element_data.AttributeValue = 0;
00175     }
00176 
00177     mElementsRead++;
00178     return element_data;
00179 }
00180 
00181 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00182 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenFiles()
00183 {
00184     OpenNodeFile();
00185     OpenElementsFile();
00186 }
00187 
00188 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00189 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenNodeFile()
00190 {
00191     // Nodes definition
00192     std::string file_name = mFilesBaseName + ".node";
00193     mNodesFile.open(file_name.c_str());
00194     if (!mNodesFile.is_open())
00195     {
00196         EXCEPTION("Could not open data file: " + file_name);
00197     }
00198 }
00199 
00200 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00201 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::OpenElementsFile()
00202 {
00203     // Elements definition
00204     std::string file_name;
00205     file_name = mFilesBaseName + ".cell";
00206 
00207     mElementsFile.open(file_name.c_str());
00208     if (!mElementsFile.is_open())
00209     {
00210         EXCEPTION("Could not open data file: " + file_name);
00211     }
00212 }
00213 
00214 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00215 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::ReadHeaders()
00216 {
00217     std::string buffer;
00218 
00219     GetNextLineFromStream(mNodesFile, buffer);
00220     std::stringstream buffer_stream(buffer);
00221     buffer_stream >> mNumNodes >> mNumNodeAttributes;
00222 
00223     // Get the next line to see if nodes are indexed from zero or not
00224     GetNextLineFromStream(mNodesFile, buffer);
00225     std::stringstream node_buffer_stream(buffer);
00226 
00227     unsigned first_index;
00228     node_buffer_stream >> first_index;
00229     assert(first_index == 0 || first_index == 1);
00230     mIndexFromZero = (first_index == 0);
00231 
00232     // Close, reopen, skip header
00233     mNodesFile.close();
00234     OpenNodeFile();
00235     GetNextLineFromStream(mNodesFile, buffer);
00236 
00237     GetNextLineFromStream(mElementsFile, buffer);
00238     std::stringstream element_buffer_stream(buffer);
00239 
00240     element_buffer_stream >> mNumElements >> mNumElementAttributes;
00241 }
00242 
00243 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00244 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::CloseFiles()
00245 {
00246     mNodesFile.close();
00247     mElementsFile.close();
00248 }
00249 
00250 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00251 void VertexMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextLineFromStream(std::ifstream& fileStream, std::string& rawLine)
00252 {
00253     bool line_is_blank;
00254 
00255     do
00256     {
00257         getline(fileStream, rawLine);
00258 
00259         if (fileStream.eof())
00260         {
00261             EXCEPTION("Cannot get the next line from node or element file due to incomplete data");
00262         }
00263 
00264         // Get rid of any comment
00265         rawLine = rawLine.substr(0,rawLine.find('#', 0));
00266 
00267         line_is_blank = (rawLine.find_first_not_of(" \t", 0) == std::string::npos);
00268     }
00269     while (line_is_blank);
00270 }
00271 
00272 
00274 // Explicit instantiation
00276 
00277 template class VertexMeshReader<1,1>;
00278 template class VertexMeshReader<1,2>;
00279 template class VertexMeshReader<1,3>;
00280 template class VertexMeshReader<2,2>;
00281 template class VertexMeshReader<2,3>;
00282 template class VertexMeshReader<3,3>;

Generated by  doxygen 1.6.2