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 #ifndef GEOMETRYKERNEL_HH_
36 #define GEOMETRYKERNEL_HH_
37 
38 #include <cassert>
39 #include <iostream>
40 
41 #include "../Geometry/VectorT.hh"
42 #include "TopologyKernel.hh"
43 
44 namespace OpenVolumeMesh {
45 
46 template <class VecT, class TopologyKernelT = TopologyKernel>
47 class GeometryKernel : public TopologyKernelT {
48 public:
49 
50  typedef VecT PointT;
51  typedef TopologyKernelT KernelT;
52 
54  GeometryKernel() = default;
55 
57  ~GeometryKernel() override = default;
58 
59  template<class OtherTopoKernel>
60  void assign(const GeometryKernel<VecT, OtherTopoKernel> *other) {
61  TopologyKernelT::assign(other);
62  other->clone_vertices(vertices_);
63  }
64 
66  VertexHandle add_vertex() override { return add_vertex(VecT()); }
67 
69  VertexHandle add_vertex(const VecT& _p) {
70 
71  // Store vertex in list
72  vertices_.push_back(_p);
73 
74  // Get handle of recently created vertex
75  return KernelT::add_vertex();
76  }
77 
79  void set_vertex(const VertexHandle& _vh, const VecT& _p) {
80 
81  assert(_vh.idx() < (int)vertices_.size());
82 
83  vertices_[_vh.idx()] = _p;
84  }
85 
87  const VecT& vertex(const VertexHandle& _vh) const {
88  return vertices_[_vh.idx()];
89  }
90 
91  VertexIter delete_vertex(const VertexHandle& _h) override {
92  assert(_h.idx() < (int)TopologyKernelT::n_vertices());
93 
94  VertexIter nV = TopologyKernelT::delete_vertex(_h);
95 
96  if (TopologyKernelT::deferred_deletion_enabled())
97  {
98 
99  }
100  else
101  vertices_.erase(vertices_.begin() + _h.idx());
102 
103  return nV;
104  }
105 
106  void collect_garbage() override
107  {
108  if (!TopologyKernelT::needs_garbage_collection())
109  return;
110 
111  if (TopologyKernelT::fast_deletion_enabled()) {
112  TopologyKernelT::collect_garbage();
113  vertices_.resize(TopologyKernel::n_vertices());
114  } else {
115  for (int i = (int)vertices_.size(); i > 0; --i)
116  if (TopologyKernelT::is_deleted(VertexHandle(i-1)))
117  {
118  vertices_.erase(vertices_.begin() + (i-1));
119  }
120  TopologyKernelT::collect_garbage();
121  }
122 
123  }
124 
125  void swap_vertex_indices(VertexHandle _h1, VertexHandle _h2) override
126  {
127  assert(_h1.idx() >= 0 && _h1.idx() < (int)vertices_.size());
128  assert(_h2.idx() >= 0 && _h2.idx() < (int)vertices_.size());
129 
130  if (_h1 == _h2)
131  return;
132 
133  std::swap(vertices_[_h1.idx()], vertices_[_h2.idx()]);
134 
135  TopologyKernelT::swap_vertex_indices(_h1, _h2);
136  }
137 
138 protected:
139 
140  void delete_multiple_vertices(const std::vector<bool>& _tag) override{
141 
142  assert(_tag.size() == TopologyKernelT::n_vertices());
143 
144  std::vector<VecT> newVertices;
145 
146  typename std::vector<VecT>::const_iterator v_it = vertices_.begin();
147 
148  for(std::vector<bool>::const_iterator t_it = _tag.begin(),
149  t_end = _tag.end(); t_it != t_end; ++t_it, ++v_it) {
150 
151  if(!(*t_it)) {
152  // Not marked as deleted
153 
154  newVertices.push_back(*v_it);
155  }
156  }
157 
158  // Swap vertices
159  vertices_.swap(newVertices);
160 
161  TopologyKernelT::delete_multiple_vertices(_tag);
162  }
163 
164 public:
165 
166  void clear(bool _clearProps = true) override {
167 
168  vertices_.clear();
169  TopologyKernelT::clear(_clearProps);
170  }
171 
172  typename PointT::value_type length(const HalfEdgeHandle& _heh) const {
173  return vector(_heh).length();
174  }
175 
176  typename PointT::value_type length(const EdgeHandle& _eh) const {
177  return vector(_eh).length();
178  }
179 
180  PointT vector(const HalfEdgeHandle& _heh) const {
181 
182  const typename TopologyKernelT::Edge& e = TopologyKernelT::halfedge(_heh);
183  return (vertex(e.to_vertex()) - vertex(e.from_vertex()));
184  }
185 
186  PointT vector(const EdgeHandle& _eh) const {
187 
188  const typename TopologyKernelT::Edge& e = TopologyKernelT::edge(_eh);
189  return (vertex(e.to_vertex()) - vertex(e.from_vertex()));
190  }
191 
192  PointT barycenter(const EdgeHandle& _eh) const {
193  return PointT(0.5 * vertex(TopologyKernelT::edge(_eh).from_vertex()) +
194  0.5 * vertex(TopologyKernelT::edge(_eh).to_vertex()));
195  }
196 
197  PointT barycenter(const FaceHandle& _fh) const {
198  PointT p(typename PointT::value_type(0));
199  typename PointT::value_type valence = 0;
200  HalfFaceVertexIter hfv_it =
201  TopologyKernelT::hfv_iter(TopologyKernelT::halfface_handle(_fh, 0));
202  for(; hfv_it.valid(); ++hfv_it, valence += 1) {
203  p += vertex(*hfv_it);
204  }
205  p /= valence;
206  return p;
207  }
208 
209  PointT barycenter(const CellHandle& _ch) const {
210  PointT p(typename PointT::value_type(0));
211  typename PointT::value_type valence = 0;
212  CellVertexIter cv_it = TopologyKernelT::cv_iter(_ch);
213  for(; cv_it.valid(); ++cv_it, valence += 1) {
214  p += vertex(*cv_it);
215  }
216  p /= valence;
217  return p;
218  }
219 
220  void clone_vertices(std::vector<VecT>& _copy) const {
221  _copy.clear();
222  _copy.reserve(vertices_.size());
223  std::copy(vertices_.begin(), vertices_.end(), std::back_inserter(_copy));
224  }
225 
226  void swap_vertices(std::vector<VecT>& _copy) {
227  if(_copy.size() != vertices_.size()) {
228  std::cerr << "Vertex vectors differ in size! The size of the copy " <<
229  "is artificially set to the correct one. Some values may not be correctly initialized." << std::endl;
230  _copy.resize(vertices_.size());
231  }
232  std::swap(vertices_, _copy);
233  }
234 
235 private:
236 
237  std::vector<VecT> vertices_;
238 };
239 
240 } // Namespace OpenVolumeMesh
241 
242 #endif /* GEOMETRYKERNEL_HH_ */
VertexHandle add_vertex(const VecT &_p)
Add a geometric point to the mesh.
GeometryKernel()=default
Constructor.
~GeometryKernel() override=default
Destructor.
size_t n_vertices() const override
Get number of vertices in mesh.
void set_vertex(const VertexHandle &_vh, const VecT &_p)
Set the coordinates of point _vh.
const VecT & vertex(const VertexHandle &_vh) const
Get point _vh&#39;s coordinates.
VertexHandle add_vertex() override
Override of empty add_vertex function.