Chaste Commit::8b5d759ac2eb95e67ae57699734101efccb0a0a9
MemfemMeshReader.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "MemfemMeshReader.hpp"
37#include "Exception.hpp"
38
39#include <sstream>
40
42// Implementation
44
45template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
47{
48 // Open node file and store the lines as a vector of strings (minus the comments)
49 std::string node_file_name = rPathBaseName + ".pts";
50 this->mNodeRawData = this->GetRawDataFromFile(node_file_name);
51
52 // Read single line header which is the number of nodes */
53 std::stringstream node_header_stream(this->mNodeRawData[0]);
54 unsigned num_nodes;
55 node_header_stream >> num_nodes;
56
57 // All Memfem data is in 3D
58 if (SPACE_DIM != 3 || ELEMENT_DIM != 3)
59 {
60 EXCEPTION("You have asked to read non-3D data. All Memfem data is in 3D.");
61 }
62
63 // Read the rest of the node data using TokenizeStringsToDoubles method
64 this->mNodeData = TokenizeStringsToDoubles(this->mNodeRawData);
65
66 // Initialise iterator for public GetNextNode method
67 this->mpNodeIterator = this->mNodeData.begin();
68
69 // Check that the size of the data matches the information in the header
70 if (num_nodes != this->mNodeData.size())
71 {
72 // ignored from coverage because otherwise would have to create files
73 // for a bad mesh just to test this line
74// LCOV_EXCL_START
75 EXCEPTION("Number of nodes does not match expected number declared in header");
76// LCOV_EXCL_STOP
77 }
78
79 // Open element file and store the lines as a vector of strings (minus the comments)
80 std::string element_file_name = rPathBaseName + ".tetras";
81 this->mElementRawData = this->GetRawDataFromFile(element_file_name);
82
83 // Read single line header which is the number of elements
84 std::stringstream element_header_stream(this->mElementRawData[0]);
85 unsigned num_elements;
86 element_header_stream >> num_elements;
87
88 // Read the rest of the element data using TokenizeStringsToInts method
89 this->mElementData = TokenizeStringsToInts(this->mElementRawData, SPACE_DIM+1, true);
90 this->mpElementIterator = this->mElementData.begin();
91
92 // Check that the size of the data matches the information in the header
93 if (num_elements != this->mElementData.size())
94 {
95 // Ignored from coverage because otherwise we would have to create files
96 // for a bad mesh just to test this line
97// LCOV_EXCL_START
98 EXCEPTION("Number of elements does not match expected number declared in header");
99// LCOV_EXCL_STOP
100 }
101
102 // Open boundary face file and store the lines as a vector of strings (minus the comments)
103 std::string face_file_name = rPathBaseName + ".tri";
104 this->mFaceRawData = this->GetRawDataFromFile(face_file_name);
105
106 // There is no header
107
108 // Read the face/edge data using TokenizeStringsToInts method
109 this->mFaceData = TokenizeStringsToInts(this->mFaceRawData, SPACE_DIM, false);
110 this->mpFaceIterator = this->mFaceData.begin();
111}
112
113template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
116
117template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
119 const std::vector<std::string>& rRawData)
120{
121 std::vector<std::vector<double> > tokenized_data; // Output
122
123 // Iterate over the lines of input
124 std::vector<std::string>::const_iterator the_iterator;
125 for (the_iterator = rRawData.begin(); the_iterator != rRawData.end(); the_iterator++ )
126 {
127 const std::string& r_line_of_data = *the_iterator;
128 std::stringstream line_stream(r_line_of_data);
129
130 if (the_iterator != rRawData.begin()) // Ignore the header string
131 {
132 std::vector<double> current_coords;
133
134 // Form the vector which represents the position of this item
135 for (unsigned i=0; i<SPACE_DIM; i++)
136 {
137 double item_coord;
138 line_stream >> item_coord;
139 current_coords.push_back(item_coord);
140 }
141
142 // Put item onto main output vector
143 tokenized_data.push_back(current_coords);
144 }
145 }
146
147 return tokenized_data;
148}
149
150template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
152 const std::vector<std::string>& rRawData,
153 unsigned dimensionOfObject,
154 bool readHeader)
155{
156 std::vector<std::vector<unsigned> > tokenized_data;
157
158 std::vector<std::string>::const_iterator the_iterator;
159 for (the_iterator = rRawData.begin(); the_iterator != rRawData.end(); the_iterator++ )
160 {
161 const std::string& r_line_of_data = *the_iterator;
162 std::stringstream line_stream(r_line_of_data);
163
164 if (readHeader == false || the_iterator != rRawData.begin())
165 {
166 std::vector<unsigned> current_indices;
167
168 for (unsigned i=0; i<dimensionOfObject; i++)
169 {
170 unsigned item_index;
171 line_stream >> item_index;
172 // The nodes have been indexed from one so we need to shift the indices
173 item_index -= 1;
174 current_indices.push_back(item_index);
175 }
176
177 tokenized_data.push_back(current_indices);
178 }
179 }
180
181 return tokenized_data;
182}
183
184// Explicit instantiation
185template class MemfemMeshReader<1,1>;
186template class MemfemMeshReader<1,2>;
187template class MemfemMeshReader<1,3>;
188template class MemfemMeshReader<2,2>;
189template class MemfemMeshReader<2,3>;
190template class MemfemMeshReader<3,3>;
#define EXCEPTION(message)
std::vector< std::vector< double > > TokenizeStringsToDoubles(const std::vector< std::string > &rRawData)
std::vector< std::vector< unsigned > > TokenizeStringsToInts(const std::vector< std::string > &rRawData, unsigned dimensionOfObject, bool readHeader)
MemfemMeshReader(const std::string &rPathBaseName)