OpenVolumeMesh
 All Classes Functions Variables Typedefs 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: 222 $ *
38  * $Date: 2012-07-30 10:44:54 +0200 (Mo, 30 Jul 2012) $ *
39  * $LastChangedBy: kremer $ *
40  * *
41 \*===========================================================================*/
42 
43 #ifndef GEOMETRYKERNEL_HH_
44 #define GEOMETRYKERNEL_HH_
45 
46 #include <cassert>
47 
48 #include "../Geometry/VectorT.hh"
49 #include "TopologyKernel.hh"
50 
51 namespace OpenVolumeMesh {
52 
53 template <class VecT, class TopologyKernelT = TopologyKernel>
54 class GeometryKernel : public TopologyKernelT {
55 public:
56 
57  typedef VecT PointT;
58  typedef TopologyKernelT KernelT;
59 
62 
65 
67  virtual VertexHandle add_vertex() { return add_vertex(VecT()); }
68 
70  VertexHandle add_vertex(const VecT& _p) {
71 
72  // Store vertex in list
73  vertices_.push_back(_p);
74 
75  // Get handle of recently created vertex
76  return KernelT::add_vertex();
77  }
78 
80  void set_vertex(const VertexHandle& _vh, const VecT& _p) {
81 
82  assert(_vh.idx() < (int)vertices_.size());
83 
84  vertices_[_vh.idx()] = _p;
85  }
86 
88  const VecT& vertex(const VertexHandle& _vh) const {
89  return vertices_[_vh.idx()];
90  }
91 
92  virtual VertexIter delete_vertex(const VertexHandle& _h) {
93  assert(_h.idx() < (int)TopologyKernel::n_vertices());
94 
95  VertexIter nV = TopologyKernelT::delete_vertex(_h);
96 
97  vertices_.erase(vertices_.begin() + _h.idx());
98 
99  return nV;
100  }
101 
102 protected:
103 
104  virtual void delete_multiple_vertices(const std::vector<bool>& _tag) {
105 
106  assert(_tag.size() == TopologyKernelT::n_vertices());
107 
108  std::vector<VecT> newVertices;
109 
110  typename std::vector<VecT>::const_iterator v_it = vertices_.begin();
111 
112  for(std::vector<bool>::const_iterator t_it = _tag.begin(),
113  t_end = _tag.end(); t_it != t_end; ++t_it, ++v_it) {
114 
115  if(!(*t_it)) {
116  // Not marked as deleted
117 
118  newVertices.push_back(*v_it);
119  }
120  }
121 
122  // Swap vertices
123  vertices_.swap(newVertices);
124 
125  TopologyKernelT::delete_multiple_vertices(_tag);
126  }
127 
128 public:
129 
130  virtual void clear(bool _clearProps = true) {
131 
132  vertices_.clear();
133  TopologyKernelT::clear(_clearProps);
134  }
135 
136  typename PointT::value_type length(const EdgeHandle& _eh) const {
137 
138  const typename TopologyKernelT::Edge& e = TopologyKernelT::edge(_eh);
139  return (vertex(e.to_vertex()) - vertex(e.from_vertex())).length();
140  }
141 
142  PointT vector(const EdgeHandle& _eh) const {
143 
144  const typename TopologyKernelT::Edge& e = TopologyKernelT::edge(_eh);
145  return (vertex(e.to_vertex()) - vertex(e.from_vertex()));
146  }
147 
148  PointT barycenter(const EdgeHandle& _eh) const {
149  return PointT(0.5 * vertex(TopologyKernelT::edge(_eh).from_vertex()) +
150  0.5 * vertex(TopologyKernelT::edge(_eh).to_vertex()));
151  }
152 
153  PointT barycenter(const FaceHandle& _fh) const {
154  PointT p(typename PointT::value_type(0));
155  typename PointT::value_type valence = 0;
156  HalfFaceVertexIter hfv_it =
157  TopologyKernelT::hfv_iter(TopologyKernelT::halfface_handle(_fh, 0));
158  for(; hfv_it.valid(); ++hfv_it, valence += 1) {
159  p += vertex(*hfv_it);
160  }
161  p /= valence;
162  return p;
163  }
164 
165  PointT barycenter(const CellHandle& _ch) const {
166  PointT p(typename PointT::value_type(0));
167  typename PointT::value_type valence = 0;
168  CellVertexIter cv_it = TopologyKernelT::cv_iter(_ch);
169  for(; cv_it.valid(); ++cv_it, valence += 1) {
170  p += vertex(*cv_it);
171  }
172  p /= valence;
173  return p;
174  }
175 
176  void clone_vertices(std::vector<VecT>& _copy) const {
177  _copy.clear();
178  _copy.reserve(vertices_.size());
179  std::copy(vertices_.begin(), vertices_.end(), std::back_inserter(_copy));
180  }
181 
182  void swap_vertices(std::vector<VecT>& _copy) {
183  if(_copy.size() != vertices_.size()) {
184  std::cerr << "Vertex vectors differ in size!" << std::endl;
185  return;
186  }
187  std::swap(vertices_, _copy);
188  }
189 
190 private:
191 
192  std::vector<VecT> vertices_;
193 };
194 
195 } // Namespace OpenVolumeMesh
196 
197 #endif /* GEOMETRYKERNEL_HH_ */