Chaste  Release::2017.1
AbstractMeshReader.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 #include <cassert>
36 #include "AbstractMeshReader.hpp"
37 #include "Exception.hpp"
38 
40 // Implementation
42 
43 template<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 
51 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
53 {
54  return GetNumFaces();
55 }
56 
57 template<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 
65 template<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 
74 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
76 {
77  return GetNextFaceData();
78 }
79 
80 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
81 std::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 
86 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
88 {
89  EXCEPTION("Random access is only implemented in mesh readers for binary mesh files.");
90 }
91 
92 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
94 {
95  EXCEPTION("Random access is only implemented in mesh readers for binary mesh files.");
96 }
97 
98 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
100 {
101  return GetFaceData(index);
102 }
103 
104 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
106 {
107  EXCEPTION("Ncl files are only implemented in mesh readers for binary mesh files.");
108 }
109 
110 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
112 {
113  return "";
114 }
115 
116 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
118 {
119  return 1u;
120 }
121 
122 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
124 {
125  return 1u;
126 }
127 
128 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
130 {
131  return false;
132 }
133 
134 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
136 {
137  return false;
138 }
139 
140 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
142 {
143  return false;
144 }
145 
146 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
148 {
149  return false;
150 }
151 
152 template<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 
160 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
162 {
163  return 0u;
164 }
165 
166 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
168 {
169  return 0u;
170 }
171 
172 template<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
180 
181 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
184 {
185  return ElementIterator(0u, this);
186 }
187 
188 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
191 {
192  return ElementIterator(rIndices, this);
193 }
194 
195 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
198 {
199  return ElementIterator(GetNumElements(), this);
200 }
201 
202 template<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 
220 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
222 {
223  unsigned next_index;
224  if (mpIndices)
225  {
226  // Iterating over a subset
228  if (mIndicesIterator != mpIndices->end())
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 
247 template<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();
265  }
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 
277 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
280 {
281  return NodeIterator(0u, this);
282 }
283 
284 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
287 {
288  return NodeIterator(rIndices, this);
289 }
290 
291 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
294 {
295  return NodeIterator(GetNumNodes(), this);
296 }
297 
298 template<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;
312  CacheData(*mIndicesIterator, true);
313  }
314 }
315 
316 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
318 {
319  unsigned next_index;
320  if (mpIndices)
321  {
322  // Iterating over a subset
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 
343 template<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())
351  {
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
374 template class AbstractMeshReader<0,1>;
375 template class AbstractMeshReader<1,1>;
376 template class AbstractMeshReader<1,2>;
377 template class AbstractMeshReader<1,3>;
378 template class AbstractMeshReader<2,2>;
379 template class AbstractMeshReader<2,3>;
380 template class AbstractMeshReader<3,3>;
ElementIterator GetElementIteratorBegin()
std::set< unsigned >::const_iterator mIndicesIterator
std::set< unsigned >::const_iterator mIndicesIterator
virtual ElementData GetElementData(unsigned index)
virtual ElementData GetFaceData(unsigned index)
virtual unsigned GetNumCableElementAttributes() const
NodeIterator(unsigned index, AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > *pReader)
NodeIterator GetNodeIteratorEnd()
NodeIterator GetNodeIteratorBegin()
#define EXCEPTION(message)
Definition: Exception.hpp:143
virtual unsigned GetOrderOfElements()
AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > * mpReader
const std::set< unsigned > * mpIndices
ElementIterator(unsigned index, AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > *pReader)
virtual bool HasNodePermutation()
ElementIterator GetElementIteratorEnd()
virtual ElementData GetNextCableElementData()
virtual std::vector< double > GetNode(unsigned index)
virtual unsigned GetNumElements() const =0
ElementData GetEdgeData(unsigned index)
ElementData GetNextEdgeData()
AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > * mpReader
unsigned GetNumEdges() const
virtual unsigned GetNumFaceAttributes() const
virtual unsigned GetNumElementAttributes() const
virtual bool GetReadContainingElementOfBoundaryElement()
virtual bool IsFileFormatBinary()
virtual unsigned GetOrderOfBoundaryElements()
virtual unsigned GetNumCableElements() const
void CacheData(unsigned index, bool firstRead=false)
virtual std::vector< unsigned > GetContainingElementIndices(unsigned index)
virtual std::string GetMeshFileBaseName()
void CacheData(unsigned index, bool firstRead=false)
const std::set< unsigned > * mpIndices
virtual unsigned GetNumNodes() const =0
virtual std::vector< double > GetNodeAttributes()
virtual const std::vector< unsigned > & rGetNodePermutation()