Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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)TopologyKernel::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  TopologyKernelT::collect_garbage();
111 
112  for (unsigned int i = vertices_.size(); i > 0; --i)
113  if (TopologyKernelT::is_deleted(VertexHandle(i-1)))
114  {
115  vertices_.erase(vertices_.begin() + (i-1));
116  }
117 
118  }
119 
120  virtual void swap_vertices(VertexHandle _h1, VertexHandle _h2)
121  {
122  assert(_h1.idx() >= 0 && _h1.idx() < (int)vertices_.size());
123  assert(_h2.idx() >= 0 && _h2.idx() < (int)vertices_.size());
124 
125  if (_h1 == _h2)
126  return;
127 
128  std::swap(vertices_[_h1.idx()], vertices_[_h2.idx()]);
129 
130  TopologyKernelT::swap_vertices(_h1, _h2);
131  }
132 
133 protected:
134 
135  virtual void delete_multiple_vertices(const std::vector<bool>& _tag) {
136 
137  assert(_tag.size() == TopologyKernelT::n_vertices());
138 
139  std::vector<VecT> newVertices;
140 
141  typename std::vector<VecT>::const_iterator v_it = vertices_.begin();
142 
143  for(std::vector<bool>::const_iterator t_it = _tag.begin(),
144  t_end = _tag.end(); t_it != t_end; ++t_it, ++v_it) {
145 
146  if(!(*t_it)) {
147  // Not marked as deleted
148 
149  newVertices.push_back(*v_it);
150  }
151  }
152 
153  // Swap vertices
154  vertices_.swap(newVertices);
155 
156  TopologyKernelT::delete_multiple_vertices(_tag);
157  }
158 
159 public:
160 
161  virtual void clear(bool _clearProps = true) {
162 
163  vertices_.clear();
164  TopologyKernelT::clear(_clearProps);
165  }
166 
167  typename PointT::value_type length(const EdgeHandle& _eh) const {
168 
169  const typename TopologyKernelT::Edge& e = TopologyKernelT::edge(_eh);
170  return (vertex(e.to_vertex()) - vertex(e.from_vertex())).length();
171  }
172 
173  PointT vector(const EdgeHandle& _eh) const {
174 
175  const typename TopologyKernelT::Edge& e = TopologyKernelT::edge(_eh);
176  return (vertex(e.to_vertex()) - vertex(e.from_vertex()));
177  }
178 
179  PointT barycenter(const EdgeHandle& _eh) const {
180  return PointT(0.5 * vertex(TopologyKernelT::edge(_eh).from_vertex()) +
181  0.5 * vertex(TopologyKernelT::edge(_eh).to_vertex()));
182  }
183 
184  PointT barycenter(const FaceHandle& _fh) const {
185  PointT p(typename PointT::value_type(0));
186  typename PointT::value_type valence = 0;
187  HalfFaceVertexIter hfv_it =
188  TopologyKernelT::hfv_iter(TopologyKernelT::halfface_handle(_fh, 0));
189  for(; hfv_it.valid(); ++hfv_it, valence += 1) {
190  p += vertex(*hfv_it);
191  }
192  p /= valence;
193  return p;
194  }
195 
196  PointT barycenter(const CellHandle& _ch) const {
197  PointT p(typename PointT::value_type(0));
198  typename PointT::value_type valence = 0;
199  CellVertexIter cv_it = TopologyKernelT::cv_iter(_ch);
200  for(; cv_it.valid(); ++cv_it, valence += 1) {
201  p += vertex(*cv_it);
202  }
203  p /= valence;
204  return p;
205  }
206 
207  void clone_vertices(std::vector<VecT>& _copy) const {
208  _copy.clear();
209  _copy.reserve(vertices_.size());
210  std::copy(vertices_.begin(), vertices_.end(), std::back_inserter(_copy));
211  }
212 
213  void swap_vertices(std::vector<VecT>& _copy) {
214  if(_copy.size() != vertices_.size()) {
215  std::cerr << "Vertex vectors differ in size! The size of the copy " <<
216  "is artificially set to the correct one. Some values may not be correctly initialized." << std::endl;
217  _copy.resize(vertices_.size());
218  }
219  std::swap(vertices_, _copy);
220  }
221 
222 private:
223 
224  std::vector<VecT> vertices_;
225 };
226 
227 } // Namespace OpenVolumeMesh
228 
229 #endif /* GEOMETRYKERNEL_HH_ */
VertexHandle add_vertex(const VecT &_p)
Add a geometric point to the mesh.
virtual size_t n_vertices() const
Get number of vertices in mesh.
const VecT & vertex(const VertexHandle &_vh) const
Get point _vh's coordinates.
virtual VertexHandle add_vertex()
Override of empty add_vertex function.
void set_vertex(const VertexHandle &_vh, const VecT &_p)
Set the coordinates of point _vh.