OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LongestEdgeT.hh
Go to the documentation of this file.
1 /*===========================================================================*\
2 * *
3 * OpenMesh *
4 * Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen *
5 * www.openmesh.org *
6 * *
7 *---------------------------------------------------------------------------*
8 * This file is part of OpenMesh. *
9 * *
10 * OpenMesh 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 * OpenMesh 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 OpenMesh. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*==========================================================================*/
34 
35 /*==========================================================================*\
36 * *
37 * $Revision: 410 $ *
38 * $Date: 2010-06-17 12:45:58 +0200 (Do, 17. Jun 2010) $ *
39 * *
40 \*==========================================================================*/
41 
46 //=============================================================================
47 //
48 // CLASS LongestEdgeT
49 //
50 //=============================================================================
51 
52 
53 #ifndef LINEAR_H
54 #define LINEAR_H
55 
57 #include <OpenMesh/Core/Utils/vector_cast.hh>
58 #include <OpenMesh/Core/Utils/Property.hh>
59 // -------------------- STL
60 #include <vector>
61 #include <queue>
62 #if defined(OM_CC_MIPS)
63 # include <math.h>
64 #else
65 # include <cmath>
66 #endif
67 
68 
69 //== NAMESPACE ================================================================
70 
71 namespace OpenMesh { // BEGIN_NS_OPENMESH
72 namespace Subdivider { // BEGIN_NS_DECIMATER
73 namespace Uniform { // BEGIN_NS_UNIFORM
74 
75 
76 //== CLASS DEFINITION =========================================================
77 
78 template <typename MeshType, typename RealType = float>
80  public:
81 
82  typedef std::pair<typename MeshType::EdgeHandle, RealType> queueElement;
83 
84  bool operator()(const queueElement& t1, const queueElement& t2) // Returns true if t1 is smaller than t2
85  {
86  return (t1.second < t2.second);
87  }
88 };
89 
90 
97 template <typename MeshType, typename RealType = float>
98 class LongestEdgeT : public SubdividerT<MeshType, RealType>
99 {
100 public:
101 
102  typedef RealType real_t;
103  typedef MeshType mesh_t;
105 
106  typedef std::vector< std::vector<real_t> > weights_t;
107  typedef std::vector<real_t> weight_t;
108 
109  typedef std::pair< typename mesh_t::EdgeHandle, real_t > queueElement;
110 
111 public:
112 
113 
114  LongestEdgeT() : parent_t()
115  { }
116 
117 
118  LongestEdgeT( mesh_t& _m) : parent_t(_m)
119  { }
120 
121 
122  ~LongestEdgeT() {}
123 
124 
125 public:
126 
127 
128  const char *name() const { return "Longest Edge Split"; }
129 
130  void set_max_edge_length(double _value) {
131  max_edge_length_squared_ = _value * _value;
132  }
133 
134 protected:
135 
136 
137  bool prepare( mesh_t& _m )
138  {
139  return true;
140  }
141 
142 
143  bool cleanup( mesh_t& _m )
144  {
145  return true;
146  }
147 
148 
149  bool subdivide( MeshType& _m, size_t _n , const bool _update_points = true)
150  {
151 
152  // Sorted queue containing all edges sorted by their decreasing length
153  std::priority_queue< queueElement, std::vector< queueElement > , CompareLengthFunction< mesh_t, real_t > > queue;
154 
155  // Build the initial queue
156  // First element should be longest edge
157  typename mesh_t::EdgeIter edgesEnd = _m.edges_end();
158  for ( typename mesh_t::EdgeIter eit = _m.edges_begin(); eit != edgesEnd; ++eit) {
159  const typename MeshType::Point to = _m.point(_m.to_vertex_handle(_m.halfedge_handle(*eit,0)));
160  const typename MeshType::Point from = _m.point(_m.from_vertex_handle(_m.halfedge_handle(*eit,0)));
161 
162  real_t length = (to - from).sqrnorm();
163 
164  // Only push the edges that need to be split
165  if ( length > max_edge_length_squared_ )
166  queue.push( queueElement(*eit,length) );
167  }
168 
169  bool stop = false;
170  while ( !stop && ! queue.empty() ) {
171  queueElement a = queue.top();
172  queue.pop();
173 
174  if ( a.second < max_edge_length_squared_ ) {
175  stop = true;
176  break;
177  } else {
178  const typename MeshType::Point to = _m.point(_m.to_vertex_handle(_m.halfedge_handle(a.first,0)));
179  const typename MeshType::Point from = _m.point(_m.from_vertex_handle(_m.halfedge_handle(a.first,0)));
180  const typename MeshType::Point midpoint = static_cast<typename MeshType::Point::value_type>(0.5) * (to + from);
181 
182  const typename MeshType::VertexHandle newVertex = _m.add_vertex(midpoint);
183  _m.split(a.first,newVertex);
184 
185  for ( typename MeshType::VertexOHalfedgeIter voh_it(_m,newVertex); voh_it.is_valid(); ++voh_it) {
186  typename MeshType::EdgeHandle eh = _m.edge_handle(*voh_it);
187  const typename MeshType::Point to = _m.point(_m.to_vertex_handle(*voh_it));
188  const typename MeshType::Point from = _m.point(_m.from_vertex_handle(*voh_it));
189  real_t length = (to - from).sqrnorm();
190 
191  // Only push the edges that need to be split
192  if ( length > max_edge_length_squared_ )
193  queue.push( queueElement(eh,length) );
194 
195  }
196  }
197  }
198 
199 #if defined(_DEBUG) || defined(DEBUG)
200  // Now we have an consistent mesh!
201  assert( OpenMesh::Utils::MeshCheckerT<mesh_t>(_m).check() );
202 #endif
203 
204 
205  return true;
206  }
207 
208 
209 private: // data
210  real_t max_edge_length_squared_;
211 
212 };
213 
214 } // END_NS_UNIFORM
215 } // END_NS_SUBDIVIDER
216 } // END_NS_OPENMESH
217 #endif
218 
Check integrity of mesh.
Definition: MeshCheckerT.hh:71
bool subdivide(MeshType &_m, size_t _n, const bool _update_points=true)
Subdivide mesh _m _n times.
Definition: LongestEdgeT.hh:149
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:56
const char * name() const
Return name of subdivision algorithm.
Definition: LongestEdgeT.hh:128
bool cleanup(mesh_t &_m)
Cleanup mesh after usage, e.g. remove added properties.
Definition: LongestEdgeT.hh:143
Uniform LongestEdgeT subdivision algorithm
Definition: LongestEdgeT.hh:98
Abstract base class for uniform subdivision algorithms.
Definition: SubdividerT.hh:87
bool prepare(mesh_t &_m)
Prepare mesh, e.g. add properties.
Definition: LongestEdgeT.hh:137

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .