SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MeshFacadeIterators.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 /// @todo Documentation Core/Datatypes/Mesh/MeshFacadeIterators.h
30 
31 #ifndef CORE_DATATYPES_MESH_FACADE_ITERATORS_H
32 #define CORE_DATATYPES_MESH_FACADE_ITERATORS_H
33 
34 #include <boost/iterator/iterator_facade.hpp>
35 #include <Core/Utils/Exception.h>
38 
39 namespace SCIRun {
40 namespace Core {
41 namespace Datatypes {
42 
43  /*
44  IDEA: Mesh iterators should iterate over entire values, not just indexes, producing values on demand.
45  A "SmartIndex" for the mesh.
46  E.G. for LatVols:
47  class SmartNodeIterator : public std::iterator<std::forward_iterator_tag, SmartIndex<Mesh::index_type,Point> >
48  class SmartEdgeIterator : public std::iterator<std::forward_iterator_tag, SmartIndex<Mesh::index_type,Point[2]> >
49  class SmartFaceIterator : public std::iterator<std::forward_iterator_tag, SmartIndex<Mesh::index_type,Point[4],SmartIndex<Edge>[4]> >
50  */
51 
52  /// @todo: Being conservative with mesh synchronization flags until more
53  // synchronize infrastructure is implemented and tested,
54  // so clear_synchronization() is always called after synchronize and operation
55  // that requires synchronize to be called.
56  template <class VirtualMeshType, template <typename> class MeshComponent>
57  class SmartMeshIterator : public boost::iterator_facade<SmartMeshIterator<VirtualMeshType, MeshComponent>, MeshComponent<VirtualMeshType>, boost::bidirectional_traversal_tag>
58  {
59  public:
60  /// @todo: need to look up pattern for creating "end" iterators.
61  explicit SmartMeshIterator(VirtualMeshType* vmesh = 0, bool isEnd = false) : iter_(0), vmesh_(vmesh), current_(vmesh)
62  {
63  ENSURE_NOT_NULL(vmesh, "virtual mesh");
64  if (!isEnd)
65  {
66  vmesh_->begin(iter_);
67  }
68  else
69  {
70  /// @todo: need to split out that Synchronize enum
71  vmesh_->synchronize(/*Mesh::EDGES_E*/ 2);
72  vmesh_->end(iter_);
73  }
74  current_.setIndex(*iter_);
75  }
76  private:
78 
79  void increment() { ++iter_; }
80  void decrement() { --iter_; }
81 
82  bool equal(const SmartMeshIterator<VirtualMeshType, MeshComponent>& other) const
83  {
84  return this->vmesh_ == other.vmesh_
85  && this->iter_ == other.iter_;
86  }
87 
88  MeshComponent<VirtualMeshType>& dereference() const
89  {
90  current_.setIndex(*iter_);
91  return current_;
92  }
93 
94  typename MeshComponent<VirtualMeshType>::iterator iter_;
95  VirtualMeshType* vmesh_;
96  mutable MeshComponent<VirtualMeshType> current_;
97  };
98 
99  /// @todo: templatize with traits and stuff. for now, a specialized version for edges.
100  template <class VirtualMeshType>
101  class EdgeInfo
102  {
103  public:
104  typedef typename VirtualMeshType::Edge::iterator iterator;
105  enum
106  {
108  };
109  explicit EdgeInfo(VirtualMeshType* mesh) : index_(0), vmesh_(mesh)
110  {
111  /// @todo: need to split out that Synchronize enum
112  vmesh_->synchronize(/*Mesh::EDGES_E*/ sync_enum);
113  }
114  void setIndex(typename VirtualMeshType::Edge::index_type i) { index_ = i; }
115 
116  typename VirtualMeshType::Edge::index_type index() const { return index_; }
117  typename VirtualMeshType::Node::array_type nodeIndices() const
118  {
119  typename VirtualMeshType::Node::array_type nodesFromEdge(2);
120  vmesh_->get_nodes(nodesFromEdge, index_);
121 
122  return nodesFromEdge;
123  }
124 
125  std::vector<Geometry::Point> nodePoints() const
126  {
127  auto indices = nodeIndices();
128  std::vector<Geometry::Point> ps(2);
129  for (size_t i = 0; i < ps.size(); ++i)
130  vmesh_->get_point(ps[i], indices[i]);
131 
132  return ps;
133  }
134  private:
135  typename VirtualMeshType::Edge::index_type index_;
136  VirtualMeshType* vmesh_;
137  };
138 
139  template <typename VirtualMeshType>
141  {
143  };
144 
145  template <class VirtualMeshType>
146  class FaceInfo
147  {
148  public:
149  typedef typename VirtualMeshType::Face::iterator iterator;
150  enum
151  {
153  };
154  explicit FaceInfo(VirtualMeshType* mesh) : index_(0), vmesh_(mesh)
155  {
156  vmesh_->synchronize(/*Mesh::FACES_E*/ sync_enum);
157  }
158  void setIndex(typename VirtualMeshType::Face::index_type i) { index_ = i; }
159 
160  typename VirtualMeshType::Face::index_type index() const { return index_; }
161  typename VirtualMeshType::Node::array_type nodeIndices() const
162  {
163  typename VirtualMeshType::Node::array_type nodesFromFace(4);
164  vmesh_->get_nodes(nodesFromFace, index_);
165  return nodesFromFace;
166  }
167  std::vector<Geometry::Point> nodePoints() const
168  {
169  auto indices = nodeIndices();
170  std::vector<Geometry::Point> ps(4);
171  for (size_t i = 0; i < ps.size(); ++i)
172  vmesh_->get_point(ps[i], indices[i]);
173  return ps;
174  }
175  typename VirtualMeshType::Edge::array_type edgeIndices() const
176  {
177  typename VirtualMeshType::Edge::array_type edgesFromFace(4);
178  vmesh_->get_edges(edgesFromFace, index_);
179  return edgesFromFace;
180  }
181  private:
182  typename VirtualMeshType::Face::index_type index_;
183  VirtualMeshType* vmesh_;
184  };
185 
186  template <typename VirtualMeshType>
188  {
190  };
191 
192  template <class VirtualMeshType>
193  class NodeInfo
194  {
195  public:
196  typedef typename VirtualMeshType::Node::iterator iterator;
197  enum
198  {
199  sync_enum = 1 << 8
200  };
201  explicit NodeInfo(VirtualMeshType* mesh) : synched_(false), index_(0), vmesh_(mesh) {}
202  void setIndex(typename VirtualMeshType::Node::index_type i) { index_ = i; }
203 
204  typename VirtualMeshType::Node::index_type index() const { return index_; }
206  {
207  Geometry::Point p;
208  vmesh_->get_point(p, index_);
209  return p;
210  }
211  typename VirtualMeshType::Edge::array_type edgeIndices() const
212  {
213  if (!synched_)
214  {
215  /// @todo: need to split out that Synchronize enum
216  vmesh_->synchronize(/*Mesh::NODE_NEIGHBORS_E*/sync_enum);
217  synched_ = true;
218  }
219  typename VirtualMeshType::Edge::array_type edgesFromNode(6);
220  vmesh_->get_edges(edgesFromNode, index_);
221  return edgesFromNode;
222  }
223  private:
224  mutable bool synched_;
225  typename VirtualMeshType::Node::index_type index_;
226  VirtualMeshType* vmesh_;
227  };
228 
229  template <typename VirtualMeshType>
231  {
233  };
234 
235 }}}
236 
237 #endif
238 
Definition: MeshFacadeIterators.h:230
VirtualMeshType::Face::iterator iterator
Definition: MeshFacadeIterators.h:149
NodeInfo(VirtualMeshType *mesh)
Definition: MeshFacadeIterators.h:201
std::vector< Geometry::Point > nodePoints() const
Definition: MeshFacadeIterators.h:125
VirtualMeshType::Edge::array_type edgeIndices() const
Definition: MeshFacadeIterators.h:211
VirtualMeshType::Node::iterator iterator
Definition: MeshFacadeIterators.h:196
Definition: MeshFacadeIterators.h:152
#define ENSURE_NOT_NULL(var, message)
Definition: Exception.h:63
void setIndex(typename VirtualMeshType::Edge::index_type i)
Definition: MeshFacadeIterators.h:114
Definition: MeshFacadeIterators.h:101
Geometry::Point point() const
Definition: MeshFacadeIterators.h:205
SmartMeshIterator< VirtualMeshType, FaceInfo > Type
Definition: MeshFacadeIterators.h:189
FieldHandle mesh()
Definition: BuildTDCSMatrixTests.cc:56
Definition: Point.h:49
void setIndex(typename VirtualMeshType::Node::index_type i)
Definition: MeshFacadeIterators.h:202
std::vector< Geometry::Point > nodePoints() const
Definition: MeshFacadeIterators.h:167
VirtualMeshType::Node::array_type nodeIndices() const
Definition: MeshFacadeIterators.h:161
SmartMeshIterator< VirtualMeshType, EdgeInfo > Type
Definition: MeshFacadeIterators.h:142
SmartMeshIterator(VirtualMeshType *vmesh=0, bool isEnd=false)
Definition: MeshFacadeIterators.h:61
Definition: MeshFacadeIterators.h:199
Definition: MeshFacadeIterators.h:193
VirtualMeshType::Node::index_type index() const
Definition: MeshFacadeIterators.h:204
SmartMeshIterator< VirtualMeshType, NodeInfo > Type
Definition: MeshFacadeIterators.h:232
friend class boost::iterator_core_access
Definition: MeshFacadeIterators.h:77
VirtualMeshType::Edge::array_type edgeIndices() const
Definition: MeshFacadeIterators.h:175
Definition: MeshFacadeIterators.h:57
Definition: MeshFacadeIterators.h:107
VirtualMeshType::Edge::iterator iterator
Definition: MeshFacadeIterators.h:104
Definition: MeshFacadeIterators.h:187
VirtualMeshType::Face::index_type index() const
Definition: MeshFacadeIterators.h:160
VirtualMeshType::Edge::index_type index() const
Definition: MeshFacadeIterators.h:116
EdgeInfo(VirtualMeshType *mesh)
Definition: MeshFacadeIterators.h:109
void setIndex(typename VirtualMeshType::Face::index_type i)
Definition: MeshFacadeIterators.h:158
Definition: MeshFacadeIterators.h:140
long long index_type
Definition: Types.h:39
FaceInfo(VirtualMeshType *mesh)
Definition: MeshFacadeIterators.h:154
VirtualMeshType::Node::array_type nodeIndices() const
Definition: MeshFacadeIterators.h:117
Definition: MeshFacadeIterators.h:146