Chaste Release::3.1
MemfemMeshReader.cpp
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 #include "MemfemMeshReader.hpp"
00037 #include "Exception.hpp"
00038 
00039 #include <sstream>
00040 
00042 // Implementation
00044 
00045 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00046 MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::MemfemMeshReader(const std::string& rPathBaseName)
00047 {
00048     // Open node file and store the lines as a vector of strings (minus the comments)
00049     std::string node_file_name = rPathBaseName + ".pts";
00050     this->mNodeRawData = this->GetRawDataFromFile(node_file_name);
00051 
00052     // Read single line header which is the number of nodes */
00053     std::stringstream node_header_stream(this->mNodeRawData[0]);
00054     unsigned num_nodes;
00055     node_header_stream >> num_nodes;
00056 
00057     // All Memfem data is in 3D
00058     if (SPACE_DIM != 3  || ELEMENT_DIM != 3)
00059     {
00060         EXCEPTION("You have asked to read non-3D data. All Memfem data is in 3D.");
00061     }
00062 
00063     // Read the rest of the node data using TokenizeStringsToDoubles method
00064     this->mNodeData = TokenizeStringsToDoubles(this->mNodeRawData);
00065 
00066     // Initialise iterator for public GetNextNode method
00067     this->mpNodeIterator = this->mNodeData.begin();
00068 
00069     // Check that the size of the data matches the information in the header
00070     if (num_nodes != this->mNodeData.size())
00071     {
00072         // ignored from coverage because otherwise would have to create files
00073         // for a bad mesh just to test this line
00074 #define COVERAGE_IGNORE
00075         EXCEPTION("Number of nodes does not match expected number declared in header");
00076 #undef COVERAGE_IGNORE
00077     }
00078 
00079     // Open element file and store the lines as a vector of strings (minus the comments)
00080     std::string element_file_name = rPathBaseName + ".tetras";
00081     this->mElementRawData = this->GetRawDataFromFile(element_file_name);
00082 
00083     // Read single line header which is the number of elements
00084     std::stringstream element_header_stream(this->mElementRawData[0]);
00085     unsigned num_elements;
00086     element_header_stream >> num_elements;
00087 
00088     // Read the rest of the element data using TokenizeStringsToInts method
00089     this->mElementData = TokenizeStringsToInts(this->mElementRawData, SPACE_DIM+1, true);
00090     this->mpElementIterator = this->mElementData.begin();
00091 
00092     // Check that the size of the data matches the information in the header
00093     if (num_elements != this->mElementData.size())
00094     {
00095         // Ignored from coverage because otherwise we would have to create files
00096         // for a bad mesh just to test this line
00097 #define COVERAGE_IGNORE
00098         EXCEPTION("Number of elements does not match expected number declared in header");
00099 #undef COVERAGE_IGNORE
00100     }
00101 
00102     // Open boundary face file and store the lines as a vector of strings (minus the comments)
00103     std::string face_file_name = rPathBaseName + ".tri";
00104     this->mFaceRawData = this->GetRawDataFromFile(face_file_name);
00105 
00106     // There is no header
00107 
00108     // Read the face/edge data using TokenizeStringsToInts method
00109     this->mFaceData = TokenizeStringsToInts(this->mFaceRawData, SPACE_DIM, false);
00110     this->mpFaceIterator = this->mFaceData.begin();
00111 }
00112 
00113 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00114 MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::~MemfemMeshReader()
00115 {}
00116 
00117 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00118 std::vector<std::vector<double> > MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToDoubles(
00119     const std::vector<std::string>& rRawData)
00120 {
00121     std::vector<std::vector<double> > tokenized_data; // Output
00122 
00123     // Iterate over the lines of input
00124     std::vector<std::string>::const_iterator the_iterator;
00125     for (the_iterator = rRawData.begin(); the_iterator != rRawData.end(); the_iterator++ )
00126     {
00127         const std::string& line_of_data = *the_iterator;
00128         std::stringstream line_stream(line_of_data);
00129 
00130         if (the_iterator != rRawData.begin()) // Ignore the header string
00131         {
00132             std::vector<double> current_coords;
00133 
00134             // Form the vector which represents the position of this item
00135             for (unsigned i=0; i<SPACE_DIM; i++)
00136             {
00137                 double item_coord;
00138                 line_stream >> item_coord;
00139                 current_coords.push_back(item_coord);
00140             }
00141 
00142             // Put item onto main output vector
00143             tokenized_data.push_back(current_coords);
00144         }
00145     }
00146 
00147     return tokenized_data;
00148 }
00149 
00150 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00151 std::vector<std::vector<unsigned> > MemfemMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToInts(
00152     const std::vector<std::string>& rRawData,
00153     unsigned dimensionOfObject,
00154     bool readHeader)
00155 {
00156     std::vector<std::vector<unsigned> > tokenized_data;
00157 
00158     std::vector<std::string>::const_iterator the_iterator;
00159     for (the_iterator = rRawData.begin(); the_iterator != rRawData.end(); the_iterator++ )
00160     {
00161         const std::string& line_of_data = *the_iterator;
00162         std::stringstream line_stream(line_of_data);
00163 
00164         if ( readHeader == false || the_iterator != rRawData.begin() )
00165         {
00166             std::vector<unsigned> current_indices;
00167 
00168             for (unsigned i=0; i<dimensionOfObject; i++)
00169             {
00170                 unsigned item_index;
00171                 line_stream >> item_index;
00172                 // The nodes have been indexed from one so we need to shift the indices
00173                 item_index -= 1;
00174                 current_indices.push_back(item_index);
00175             }
00176 
00177             tokenized_data.push_back(current_indices);
00178         }
00179     }
00180 
00181     return tokenized_data;
00182 }
00183 
00185 // Explicit instantiation
00187 
00188 template class MemfemMeshReader<1,1>;
00189 template class MemfemMeshReader<1,2>;
00190 template class MemfemMeshReader<1,3>;
00191 template class MemfemMeshReader<2,2>;
00192 template class MemfemMeshReader<2,3>;
00193 template class MemfemMeshReader<3,3>;