Chaste  Release::2017.1
AbstractMeshReader.hpp
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 
36 
37 #ifndef _ABSTRACTMESHREADER_HPP_
38 #define _ABSTRACTMESHREADER_HPP_
39 
40 #include <string>
41 #include <vector>
42 #include <set>
43 #include <cassert>
44 #include <boost/iterator/iterator_facade.hpp>
45 #include "Exception.hpp"
46 
52 {
53  // Constructor to initialise default values, to prevent -Werror=maybe-uninitialized on some compilers
54  ElementData()
55  : NodeIndices(std::vector<unsigned>()),
58  std::vector<unsigned> NodeIndices;
59  double AttributeValue;
60  unsigned ContainingElement;
61 };
62 
75 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
77 {
78 
79 public:
80 
81  virtual ~AbstractMeshReader()
82  {}
83 
85  virtual unsigned GetNumElements() const =0;
86 
88  virtual unsigned GetNumNodes() const =0;
89 
91  virtual unsigned GetNumFaces() const =0;
92 
94  virtual unsigned GetNumCableElements() const;
95 
97  virtual unsigned GetNumElementAttributes() const;
98 
100  virtual unsigned GetNumFaceAttributes() const;
101 
103  virtual unsigned GetNumCableElementAttributes() const;
104 
112  virtual std::vector<double> GetNodeAttributes();
113 
115  unsigned GetNumEdges() const;
116 
118  virtual std::vector<double> GetNextNode()=0;
119 
121  virtual void Reset()=0;
122 
124  virtual ElementData GetNextElementData()=0;
125 
127  virtual ElementData GetNextFaceData()=0;
128 
130  virtual ElementData GetNextCableElementData();
131 
133  ElementData GetNextEdgeData();
134 
135 
142  virtual std::vector<double> GetNode(unsigned index);
143 
150  virtual ElementData GetElementData(unsigned index);
151 
158  virtual ElementData GetFaceData(unsigned index);
159 
166  ElementData GetEdgeData(unsigned index);
167 
175  virtual std::vector<unsigned> GetContainingElementIndices(unsigned index);
176 
180  virtual std::string GetMeshFileBaseName();
181 
185  virtual unsigned GetOrderOfElements();
186 
190  virtual unsigned GetOrderOfBoundaryElements();
191 
195  virtual bool GetReadContainingElementOfBoundaryElement();
196 
197 
204  virtual bool IsFileFormatBinary();
205 
212  virtual bool HasNclFile();
213 
220  virtual bool HasNodePermutation();
221 
228  virtual const std::vector<unsigned>& rGetNodePermutation();
229 
230 
231  // Iterator classes
232 
236  class ElementIterator : public boost::iterator_facade<ElementIterator, const ElementData,
237  boost::single_pass_traversal_tag>
238  {
239  public:
251  : mIndex(index),
252  mpIndices(nullptr),
253  mpReader(pReader)
254  {
255  CacheData(mIndex, true);
256  }
257 
265  ElementIterator(const std::set<unsigned>& rIndices,
267 
271  unsigned GetIndex() const
272  {
273  return mIndex;
274  }
275 
276  private:
277  friend class boost::iterator_core_access;
278 
286  void CacheData(unsigned index, bool firstRead = false);
287 
291  void increment();
292 
297  bool equal(const ElementIterator& rOther) const
298  {
299  return mIndex == rOther.mIndex;
300  }
301 
309  const ElementData& dereference() const
310  {
311  assert(mpReader);
312  assert(mIndex < mpReader->GetNumElements());
313  // This was cached in increment()
314  return mLastDataRead;
315  }
316 
318  unsigned mIndex;
319 
321  const std::set<unsigned>* mpIndices;
322 
324  std::set<unsigned>::const_iterator mIndicesIterator;
325 
328 
331  };
332 
340  ElementIterator GetElementIteratorBegin();
341 
351  ElementIterator GetElementIteratorBegin(const std::set<unsigned>& rIndices);
352 
356  ElementIterator GetElementIteratorEnd();
357 
358 
362  class NodeIterator : public boost::iterator_facade<NodeIterator, const std::vector<double>,
364  boost::single_pass_traversal_tag>
365  {
366  public:
378  : mIndex(index),
379  mpIndices(nullptr),
380  mpReader(pReader)
381  {
382  CacheData(mIndex, true);
383  }
384 
392  NodeIterator(const std::set<unsigned>& rIndices,
394 
398  unsigned GetIndex() const
399  {
400  return mIndex;
401  }
402 
403  private:
404  friend class boost::iterator_core_access;
405 
413  void CacheData(unsigned index, bool firstRead = false);
414 
418  void increment();
419 
424  bool equal(const NodeIterator& rOther) const
425  {
426  return mIndex == rOther.mIndex;
427  }
428 
435  const std::vector<double>& dereference() const
436  {
437  assert(mpReader);
438  assert(mIndex < mpReader->GetNumNodes());
439  // This was cached in increment()
440  return mLastDataRead;
441  }
442 
444  unsigned mIndex;
445 
447  const std::set<unsigned>* mpIndices;
448 
450  std::set<unsigned>::const_iterator mIndicesIterator;
451 
454 
456  std::vector<double> mLastDataRead;
457  };
458 
466  NodeIterator GetNodeIteratorBegin();
467 
477  NodeIterator GetNodeIteratorBegin(const std::set<unsigned>& rIndices);
478 
482  NodeIterator GetNodeIteratorEnd();
483 };
484 
485 #endif //_ABSTRACTMESHREADER_HPP_
std::set< unsigned >::const_iterator mIndicesIterator
std::set< unsigned >::const_iterator mIndicesIterator
NodeIterator(unsigned index, AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > *pReader)
unsigned ContainingElement
bool equal(const NodeIterator &rOther) const
AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > * mpReader
const std::set< unsigned > * mpIndices
ElementIterator(unsigned index, AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > *pReader)
const double DOUBLE_UNSET
Definition: Exception.hpp:56
std::vector< unsigned > NodeIndices
AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > * mpReader
const ElementData & dereference() const
const unsigned UNSIGNED_UNSET
Definition: Exception.hpp:52
const std::set< unsigned > * mpIndices
const std::vector< double > & dereference() const
bool equal(const ElementIterator &rOther) const