Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
AbstractMeshReader.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#include <cassert>
36#include "AbstractMeshReader.hpp"
37#include "Exception.hpp"
38
40// Implementation
42
43template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
45{
46 // By default returns 0. If a concrete class does read attributes
47 // it needs to overload this method.
48 return 0;
49}
50
51template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
53{
54 return GetNumFaces();
55}
56
57template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
59{
60 // By default returns 0. If a concrete class does read attributes
61 // it needs to overload this method.
62 return 0;
63}
64
65template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
67{
68 // By default returns an empty vector. If a concrete class does read node attributes
69 // it needs to overload this method.
70 std::vector<double> empty;
71 return empty;
72}
73
74template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
79
80template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
81std::vector<double> AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNode(unsigned index)
82{
83 EXCEPTION("Random access is only implemented in mesh readers for binary mesh files.");
84}
85
86template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
88{
89 EXCEPTION("Random access is only implemented in mesh readers for binary mesh files.");
90}
91
92template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
95 EXCEPTION("Random access is only implemented in mesh readers for binary mesh files.");
96}
98template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
101 return GetFaceData(index);
102}
104template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
106{
107 EXCEPTION("Ncl files are only implemented in mesh readers for binary mesh files.");
108}
109
110template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
113 return "";
114}
116template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
121
122template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
127
128template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
131 return false;
132}
134template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
139
140template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
143 return false;
144}
145
146template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
151
152template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
154{
155 EXCEPTION("Node permutations aren't supported by this reader");
156}
157
158// Cable elements aren't supported in most formats
159
160template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
165
166template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
171
172template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
174{
175 EXCEPTION("Cable elements are not supported by this mesh format.");
176}
177
178
179// Iterator-related methods
181template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
184{
185 return ElementIterator(0u, this);
186}
187
188template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
191{
192 return ElementIterator(rIndices, this);
193}
194
195template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
201
202template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
205 : mpIndices(&rIndices),
206 mpReader(pReader)
207{
208 if (mpIndices->empty())
209 {
210 mIndex = mpReader->GetNumElements();
211 }
212 else
213 {
214 mIndicesIterator = mpIndices->begin();
215 mIndex = 0;
216 CacheData(*mIndicesIterator, true);
217 }
218}
219
220template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
222{
223 unsigned next_index;
224 if (mpIndices)
225 {
226 // Iterating over a subset
229 {
230 next_index = *mIndicesIterator;
231 }
232 else
233 {
234 // The subset is complete so skip to the end of the items so that we can be
235 // compared to GetElementIteratorEnd
236 next_index = mpReader->GetNumElements();
237 }
238 }
239 else
240 {
241 // Iterating over all items
242 next_index = mIndex + 1;
243 }
244 CacheData(next_index);
245}
246
247template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
249{
250 assert(mpReader);
251 assert(mIndex < index || mIndex == 0u || index == mpReader->GetNumElements());
252 if (index < mpReader->GetNumElements())
253 {
254 if (mpReader->IsFileFormatBinary())
255 {
256 mLastDataRead = mpReader->GetElementData(index);
257 }
258 else
259 {
260 if (firstRead)
261 {
262 assert(mIndex == 0u);
263 //ASCII at construction - do an initial read to make sure the line mIndex is read
264 mLastDataRead = mpReader->GetNextElementData();
266 //ASCII generic case, where we might need to skip some unread items
267 while (mIndex < index)
268 {
269 mLastDataRead = mpReader->GetNextElementData();
270 mIndex++;
271 }
272 }
273 }
274 mIndex = index;
275}
276
277template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
283
284template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
287{
288 return NodeIterator(rIndices, this);
289}
290
291template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
297
298template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
301 : mpIndices(&rIndices),
302 mpReader(pReader)
303{
304 if (mpIndices->empty())
305 {
306 mIndex = mpReader->GetNumNodes();
307 }
308 else
309 {
310 mIndicesIterator = mpIndices->begin();
311 mIndex = 0;
313 }
314}
315
316template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
318{
319 unsigned next_index;
320 if (mpIndices)
321 {
322 // Iterating over a subset
323 ++mIndicesIterator;
324 if (mIndicesIterator != mpIndices->end())
325 {
326 next_index = *mIndicesIterator;
327 }
328 else
329 {
330 // The subset is complete so skip to the end of the items so that we can be
331 // compared to GetNodeIteratorEnd
332 next_index = mpReader->GetNumNodes();
333 }
334 }
335 else
336 {
337 // Iterating over all items
338 next_index = mIndex + 1;
339 }
340 CacheData(next_index);
341}
342
343template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
345{
346 assert(mpReader);
347 assert(mIndex < index || mIndex == 0u || index == mpReader->GetNumNodes());
348 if (index < mpReader->GetNumNodes())
349 {
350 if (mpReader->IsFileFormatBinary())
352 mLastDataRead = mpReader->GetNode(index);
353 }
354 else
355 {
356 if (firstRead)
357 {
358 assert(mIndex == 0u);
359 //ASCII at construction - do an initial read to make sure the line mIndex is read
360 mLastDataRead = mpReader->GetNextNode();
361 }
362 //ASCII generic case, where we might need to skip some unread items
363 while (mIndex < index)
364 {
365 mLastDataRead = mpReader->GetNextNode();
366 mIndex++;
367 }
368 }
369 }
370 mIndex = index;
371}
372
373// Explicit instantiation
374template class AbstractMeshReader<0,1>;
375template class AbstractMeshReader<1,1>;
376template class AbstractMeshReader<1,2>;
377template class AbstractMeshReader<1,3>;
378template class AbstractMeshReader<2,2>;
379template class AbstractMeshReader<2,3>;
380template class AbstractMeshReader<3,3>;
#define EXCEPTION(message)
AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > * mpReader
ElementIterator(unsigned index, AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > *pReader)
void CacheData(unsigned index, bool firstRead=false)
std::set< unsigned >::const_iterator mIndicesIterator
const std::set< unsigned > * mpIndices
const std::set< unsigned > * mpIndices
AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > * mpReader
std::set< unsigned >::const_iterator mIndicesIterator
NodeIterator(unsigned index, AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > *pReader)
void CacheData(unsigned index, bool firstRead=false)
ElementIterator GetElementIteratorBegin()
virtual unsigned GetNumCableElementAttributes() const
ElementData GetEdgeData(unsigned index)
ElementData GetNextEdgeData()
virtual unsigned GetNumElements() const =0
virtual std::vector< unsigned > GetContainingElementIndices(unsigned index)
virtual bool GetReadContainingElementOfBoundaryElement()
virtual unsigned GetOrderOfBoundaryElements()
virtual std::string GetMeshFileBaseName()
NodeIterator GetNodeIteratorEnd()
virtual unsigned GetNumElementAttributes() const
virtual std::vector< double > GetNode(unsigned index)
virtual const std::vector< unsigned > & rGetNodePermutation()
virtual ElementData GetNextCableElementData()
unsigned GetNumEdges() const
virtual unsigned GetNumCableElements() const
virtual std::vector< double > GetNodeAttributes()
virtual ElementData GetFaceData(unsigned index)
virtual unsigned GetNumFaceAttributes() const
virtual bool HasNodePermutation()
virtual bool IsFileFormatBinary()
virtual unsigned GetNumNodes() const =0
NodeIterator GetNodeIteratorBegin()
virtual ElementData GetElementData(unsigned index)
ElementIterator GetElementIteratorEnd()
virtual unsigned GetOrderOfElements()