Chaste  Release::2018.1
FemlabMeshReader.cpp
1 /*
2 
3 Copyright (c) 2005-2018, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "FemlabMeshReader.hpp"
37 #include "Exception.hpp"
38 
39 #include <sstream>
40 
42 // Implementation
44 
45 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
47  const std::string& rNodeFileName,
48  const std::string& rElementFileName,
49  const std::string& rEdgeFileName)
50 {
51 
52  // Open node file and store the lines as a vector of strings (minus the comments)
53  std::string node_file_name = rPathBaseName + rNodeFileName;
54  this->mNodeRawData = this->GetRawDataFromFile(node_file_name);
55 
56  // Read the node data using TokenizeStringsToDoubles method
57  this->mNodeData = TokenizeStringsToDoubles(this->mNodeRawData);
58 
59  // Initialise iterator for public GetNextNode method
60  this->mpNodeIterator = this->mNodeData.begin();
61 
62 
63  // Open element file and store the lines as a vector of strings (minus the comments)
64  std::string element_file_name = rPathBaseName + rElementFileName;
65  this->mElementRawData = this->GetRawDataFromFile(element_file_name);
66 
67  // Read the rest of the element data using TokenizeStringsToInts method
68  this->mElementData = TokenizeStringsToInts(this->mElementRawData, SPACE_DIM + 1);
69  this->mpElementIterator = this->mElementData.begin();
70 
71  /*
72  * Open edge file and store the lines as a vector of strings (minus the comments)
73  * We store edges as "faces" but the superclass
74  * provides a GetNextEdgeData method which queries this data.
75  */
76 
77  std::string edge_file_name = rPathBaseName + rEdgeFileName;
78  this->mFaceRawData = this->GetRawDataFromFile(edge_file_name);
79 
80  // Read the rest of the face/edge data using TokenizeStringsToInts method
81  this->mFaceData = TokenizeStringsToInts(this->mFaceRawData, SPACE_DIM);
82  this->mpFaceIterator = this->mFaceData.begin();
83 }
84 
85 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
87 {}
88 
89 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
90 std::vector < std::vector<double> >
92 {
93  std::vector < std::vector < double > >tokenized_data; // Output
94 
95  // Iterate over the lines of input
96  unsigned dimension_count = 0;
97  std::vector < std::string >::const_iterator the_iterator;
98  for (the_iterator = rRawData.begin(); the_iterator != rRawData.end();
99  the_iterator++)
100  {
101  const std::string& r_line_of_data = *the_iterator;
102  std::stringstream line_stream (r_line_of_data);
103 
104  if (dimension_count == 0)
105  {
106  // First iteration, build the tokenized_data vector and push in x coordinates
107  while (!line_stream.eof())
108  {
109  double item_coord;
110 
111  std::vector < double >x_coord;
112  line_stream >> item_coord;
113  x_coord.push_back (item_coord);
114  tokenized_data.push_back (x_coord);
115  }
116  }
117  else
118  {
119  unsigned current_node = 0;
120 
121  // Other iterations, push in coordinates other than x
122  while (!line_stream.eof())
123  {
124  double item_coord;
125  line_stream >> item_coord;
126  tokenized_data[current_node].push_back (item_coord);
127  current_node++;
128  }
129  }
130  // Dimension of mesh is the same as the line of rawData
131  dimension_count++;
132  }
133 
134  if (SPACE_DIM != dimension_count)
135  {
136  EXCEPTION("SPACE_DIM != dimension read from file");
137  }
138  return (tokenized_data);
139 }
140 
141 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
142 std::vector < std::vector < unsigned > >
143  FemlabMeshReader<ELEMENT_DIM, SPACE_DIM>::TokenizeStringsToInts(const std::vector<std::string>& rRawData, unsigned dimensionOfObject)
144 {
145  std::vector < std::vector < unsigned > >tokenized_data;
146 
147  // There are dimensionOfObject lines to be read
148  for (unsigned i = 0; i < dimensionOfObject; i++)
149  {
150  const std::string& r_line_of_data = rRawData[i];
151  std::stringstream line_stream (r_line_of_data);
152 
153  if (i == 0)
154  {
155  // First iteration, build the tokenized_data vector and push in x coordinates
156  while (!line_stream.eof())
157  {
158  double item_index;
159 
160  std::vector < unsigned >first_index;
161  line_stream >> item_index;
162  first_index.push_back ((unsigned) (item_index - 0.5)); // item indices should be minus 1
163  tokenized_data.push_back (first_index);
164  }
165  }
166  else
167  {
168  unsigned current_node = 0;
169 
170  // Other iterations, push in coordinates other than x.
171  while (!line_stream.eof())
172  {
173  double item_index;
174  line_stream >> item_index;
175  tokenized_data[current_node].
176  push_back ((unsigned) (item_index - 0.5));
177  current_node++;
178  }
179  }
180  }
181  return (tokenized_data);
182 }
183 
184 
185 // Explicit instantiation
186 template class FemlabMeshReader<1,1>;
187 template class FemlabMeshReader<1,2>;
188 template class FemlabMeshReader<1,3>;
189 template class FemlabMeshReader<2,2>;
190 template class FemlabMeshReader<2,3>;
191 template class FemlabMeshReader<3,3>;
std::vector< std::vector< double > > TokenizeStringsToDoubles(const std::vector< std::string > &rRawData)
FemlabMeshReader(const std::string &rPathBaseName, const std::string &rNodeFileName, const std::string &rElementFileName, const std::string &rEdgeFileName)
#define EXCEPTION(message)
Definition: Exception.hpp:143
std::vector< std::vector< unsigned > > TokenizeStringsToInts(const std::vector< std::string > &rRawData, unsigned dimensionOfObject)
virtual ~FemlabMeshReader()