Chaste  Release::3.4
AbstractMeshReader.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 
81 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
82 std::vector<double> AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNode(unsigned index)
83 {
84  EXCEPTION("Random access is only implemented in mesh readers for binary mesh files.");
85 }
86 
87 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
89 {
90  EXCEPTION("Random access is only implemented in mesh readers for binary mesh files.");
91 }
92 
93 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
95 {
96  EXCEPTION("Random access is only implemented in mesh readers for binary mesh files.");
97 }
98 
99 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
101 {
102  return GetFaceData(index);
103 }
104 
105 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
107 {
108  EXCEPTION("Ncl files are only implemented in mesh readers for binary mesh files.");
109 }
110 
111 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
113 {
114  return "";
115 }
116 
117 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
119 {
120  return 1u;
121 }
122 
123 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
125 {
126  return 1u;
127 }
128 
129 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
131 {
132  return false;
133 }
134 
135 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
137 {
138  return false;
139 }
140 
141 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
143 {
144  return false;
145 }
146 
147 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
149 {
150  return false;
151 }
152 
153 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
155 {
156  EXCEPTION("Node permutations aren't supported by this reader");
157 }
158 
159 // Cable elements aren't supported in most formats
160 
161 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
163 {
164  return 0u;
165 }
166 
167 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
169 {
170  return 0u;
171 }
172 
173 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
175 {
176  EXCEPTION("Cable elements are not supported by this mesh format.");
177 }
178 
179 
180 // Iterator-related methods
181 
182 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
185 {
186  return ElementIterator(0u, this);
187 }
188 
189 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
192 {
193  return ElementIterator(rIndices, this);
194 }
195 
196 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
199 {
200  return ElementIterator(GetNumElements(), this);
201 }
202 
203 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
205  AbstractMeshReader* pReader)
206  : mpIndices(&rIndices),
207  mpReader(pReader)
208 {
209  if (mpIndices->empty())
210  {
211  mIndex = mpReader->GetNumElements();
212  }
213  else
214  {
215  mIndicesIterator = mpIndices->begin();
216  mIndex = 0;
217  CacheData(*mIndicesIterator, true);
218  }
219 }
220 
221 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
223 {
224  unsigned next_index;
225  if (mpIndices)
226  {
227  // Iterating over a subset
229  if (mIndicesIterator != mpIndices->end())
230  {
231  next_index = *mIndicesIterator;
232  }
233  else
234  {
235  // The subset is complete so skip to the end of the items so that we can be
236  // compared to GetElementIteratorEnd
237  next_index = mpReader->GetNumElements();
238  }
239  }
240  else
241  {
242  // Iterating over all items
243  next_index = mIndex + 1;
244  }
245  CacheData(next_index);
246 }
247 
248 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
250 {
251  assert(mpReader);
252  assert(mIndex < index || mIndex == 0u || index == mpReader->GetNumElements());
253  if (index < mpReader->GetNumElements())
254  {
255  if (mpReader->IsFileFormatBinary())
256  {
257  mLastDataRead = mpReader->GetElementData(index);
258  }
259  else
260  {
261  if (firstRead)
262  {
263  assert(mIndex == 0u);
264  //ASCII at construction - do an initial read to make sure the line mIndex is read
265  mLastDataRead = mpReader->GetNextElementData();
266  }
267  //ASCII generic case, where we might need to skip some unread items
268  while (mIndex < index)
269  {
270  mLastDataRead = mpReader->GetNextElementData();
271  mIndex++;
272  }
273  }
274  }
275  mIndex = index;
276 }
277 
278 
279 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
282 {
283  return NodeIterator(0u, this);
284 }
285 
286 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
289 {
290  return NodeIterator(rIndices, this);
291 }
292 
293 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
296 {
297  return NodeIterator(GetNumNodes(), this);
298 }
299 
300 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
302  AbstractMeshReader* pReader)
303  : mpIndices(&rIndices),
304  mpReader(pReader)
305 {
306  if (mpIndices->empty())
307  {
308  mIndex = mpReader->GetNumNodes();
309  }
310  else
311  {
312  mIndicesIterator = mpIndices->begin();
313  mIndex = 0;
314  CacheData(*mIndicesIterator, true);
315  }
316 }
317 
318 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
320 {
321  unsigned next_index;
322  if (mpIndices)
323  {
324  // Iterating over a subset
326  if (mIndicesIterator != mpIndices->end())
327  {
328  next_index = *mIndicesIterator;
329  }
330  else
331  {
332  // The subset is complete so skip to the end of the items so that we can be
333  // compared to GetNodeIteratorEnd
334  next_index = mpReader->GetNumNodes();
335  }
336  }
337  else
338  {
339  // Iterating over all items
340  next_index = mIndex + 1;
341  }
342  CacheData(next_index);
343 }
344 
345 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
347 {
348  assert(mpReader);
349  assert(mIndex < index || mIndex == 0u || index == mpReader->GetNumNodes());
350  if (index < mpReader->GetNumNodes())
351  {
352  if (mpReader->IsFileFormatBinary())
353  {
354  mLastDataRead = mpReader->GetNode(index);
355  }
356  else
357  {
358  if (firstRead)
359  {
360  assert(mIndex == 0u);
361  //ASCII at construction - do an initial read to make sure the line mIndex is read
362  mLastDataRead = mpReader->GetNextNode();
363  }
364  //ASCII generic case, where we might need to skip some unread items
365  while (mIndex < index)
366  {
367  mLastDataRead = mpReader->GetNextNode();
368  mIndex++;
369  }
370  }
371  }
372  mIndex = index;
373 }
374 
375 
377 // Explicit instantiation
379 
380 template class AbstractMeshReader<0,1>;
381 template class AbstractMeshReader<1,1>;
382 template class AbstractMeshReader<1,2>;
383 template class AbstractMeshReader<1,3>;
384 template class AbstractMeshReader<2,2>;
385 template class AbstractMeshReader<2,3>;
386 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 GetNodeIteratorEnd()
NodeIterator GetNodeIteratorBegin()
#define EXCEPTION(message)
Definition: Exception.hpp:143
virtual unsigned GetOrderOfElements()
const std::set< unsigned > * mpIndices
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()
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()