Developer Documentation
GeometryKernel.hh
1 /*===========================================================================*\
2  * *
3  * OpenVolumeMesh *
4  * Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openvolumemesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenVolumeMesh. *
9  * *
10  * OpenVolumeMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenVolumeMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenVolumeMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision$ *
38  * $Date$ *
39  * $LastChangedBy$ *
40  * *
41 \*===========================================================================*/
42 
43 #ifndef GEOMETRYKERNEL_HH_
44 #define GEOMETRYKERNEL_HH_
45 
46 #include <cassert>
47 #include <iostream>
48 
49 #include "../Geometry/VectorT.hh"
50 #include "TopologyKernel.hh"
51 
52 namespace OpenVolumeMesh {
53 
54 template <class VecT, class TopologyKernelT = TopologyKernel>
55 class GeometryKernel : public TopologyKernelT {
56 public:
57 
58  typedef VecT PointT;
59  typedef TopologyKernelT KernelT;
60 
63 
66 
68  virtual VertexHandle add_vertex() { return add_vertex(VecT()); }
69 
71  VertexHandle add_vertex(const VecT& _p) {
72 
73  // Store vertex in list
74  vertices_.push_back(_p);
75 
76  // Get handle of recently created vertex
77  return KernelT::add_vertex();
78  }
79 
81  void set_vertex(const VertexHandle& _vh, const VecT& _p) {
82 
83  assert(_vh.idx() < (int)vertices_.size());
84 
85  vertices_[_vh.idx()] = _p;
86  }
87 
89  const VecT& vertex(const VertexHandle& _vh) const {
90  return vertices_[_vh.idx()];
91  }
92 
93  virtual VertexIter delete_vertex(const VertexHandle& _h) {
94  assert(_h.idx() < (int)TopologyKernelT::n_vertices());
95 
96  VertexIter nV = TopologyKernelT::delete_vertex(_h);
97 
98  if (TopologyKernelT::deferred_deletion_enabled())
99  {
100 
101  }
102  else
103  vertices_.erase(vertices_.begin() + _h.idx());
104 
105  return nV;
106  }
107 
108  virtual void collect_garbage()
109  {
110  if (!TopologyKernelT::needs_garbage_collection())
111  return;
112 
113  if (TopologyKernelT::fast_deletion_enabled()) {
114  TopologyKernelT::collect_garbage();
115  vertices_.resize(TopologyKernel::n_vertices());
116  } else {
117  for (unsigned int i = vertices_.size(); i > 0; --i)
118  if (TopologyKernelT::is_deleted(VertexHandle(i-1)))
119  {
120  vertices_.erase(vertices_.begin() + (i-1));
121  }
122  TopologyKernelT::collect_garbage();
123  }
124 
125  }
126 
127  virtual void swap_vertices(VertexHandle _h1, VertexHandle _h2)
128  {
129  assert(_h1.idx() >= 0 && _h1.idx() < (int)vertices_.size());
130  assert(_h2.idx() >= 0 && _h2.idx() < (int)vertices_.size());
131 
132  if (_h1 == _h2)
133  return;
134 
135  std::swap(vertices_[_h1.idx()], vertices_[_h2.idx()]);
136 
137  TopologyKernelT::swap_vertices(_h1, _h2);
138  }
139 
140 protected:
141 
142  virtual void delete_multiple_vertices(const std::vector<bool>& _tag) {
143 
144  assert(_tag.size() == TopologyKernelT::n_vertices());
145 
146  std::vector<VecT> newVertices;
147 
148  typename std::vector<VecT>::const_iterator v_it = vertices_.begin();
149 
150  for(std::vector<bool>::const_iterator t_it = _tag.begin(),
151  t_end = _tag.end(); t_it != t_end; ++t_it, ++v_it) {
152 
153  if(!(*t_it)) {
154  // Not marked as deleted
155 
156  newVertices.push_back(*v_it);
157  }
158  }
159 
160  // Swap vertices
161  vertices_.swap(newVertices);
162 
163  TopologyKernelT::delete_multiple_vertices(_tag);
164  }
165 
166 public:
167 
168  virtual void clear(bool _clearProps = true) {
169 
170  vertices_.clear();
171  TopologyKernelT::clear(_clearProps);
172  }
173 
174  typename PointT::value_type length(const EdgeHandle& _eh) const {
175 
176  const typename TopologyKernelT::Edge& e = TopologyKernelT::edge(_eh);
177  return (vertex(e.to_vertex()) - vertex(e.from_vertex())).length();
178  }
179 
180  PointT vector(const EdgeHandle& _eh) const {
181 
182  const typename TopologyKernelT::Edge& e = TopologyKernelT::edge(_eh);
183  return (vertex(e.to_vertex()) - vertex(e.from_vertex()));
184  }
185 
186  PointT barycenter(const EdgeHandle& _eh) const {
187  return PointT(0.5 * vertex(TopologyKernelT::edge(_eh).from_vertex()) +
188  0.5 * vertex(TopologyKernelT::edge(_eh).to_vertex()));
189  }
190 
191  PointT barycenter(const FaceHandle& _fh) const {
192  PointT p(typename PointT::value_type(0));
193  typename PointT::value_type valence = 0;
194  HalfFaceVertexIter hfv_it =
195  TopologyKernelT::hfv_iter(TopologyKernelT::halfface_handle(_fh, 0));
196  for(; hfv_it.valid(); ++hfv_it, valence += 1) {
197  p += vertex(*hfv_it);
198  }
199  p /= valence;
200  return p;
201  }
202 
203  PointT barycenter(const CellHandle& _ch) const {
204  PointT p(typename PointT::value_type(0));
205  typename PointT::value_type valence = 0;
206  CellVertexIter cv_it = TopologyKernelT::cv_iter(_ch);
207  for(; cv_it.valid(); ++cv_it, valence += 1) {
208  p += vertex(*cv_it);
209  }
210  p /= valence;
211  return p;
212  }
213 
214  void clone_vertices(std::vector<VecT>& _copy) const {
215  _copy.clear();
216  _copy.reserve(vertices_.size());
217  std::copy(vertices_.begin(), vertices_.end(), std::back_inserter(_copy));
218  }
219 
220  void swap_vertices(std::vector<VecT>& _copy) {
221  if(_copy.size() != vertices_.size()) {
222  std::cerr << "Vertex vectors differ in size! The size of the copy " <<
223  "is artificially set to the correct one. Some values may not be correctly initialized." << std::endl;
224  _copy.resize(vertices_.size());
225  }
226  std::swap(vertices_, _copy);
227  }
228 
229 private:
230 
231  std::vector<VecT> vertices_;
232 };
233 
234 } // Namespace OpenVolumeMesh
235 
236 #endif /* GEOMETRYKERNEL_HH_ */
const VecT & vertex(const VertexHandle &_vh) const
Get point _vh&#39;s coordinates.
void set_vertex(const VertexHandle &_vh, const VecT &_p)
Set the coordinates of point _vh.
virtual size_t n_vertices() const
Get number of vertices in mesh.
virtual VertexHandle add_vertex()
Override of empty add_vertex function.
VertexHandle add_vertex(const VecT &_p)
Add a geometric point to the mesh.