Developer Documentation
LongestEdgeT.hh
Go to the documentation of this file.
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 /*==========================================================================*\
43 * *
44 * $Revision: 410 $ *
45 * $Date: 2010-06-17 12:45:58 +0200 (Do, 17. Jun 2010) $ *
46 * *
47 \*==========================================================================*/
48 
53 //=============================================================================
54 //
55 // CLASS LongestEdgeT
56 //
57 //=============================================================================
58 
59 
60 #ifndef LINEAR_H
61 #define LINEAR_H
62 
64 #include <OpenMesh/Core/Utils/vector_cast.hh>
65 #include <OpenMesh/Core/Utils/Property.hh>
66 // -------------------- STL
67 #include <vector>
68 #include <queue>
69 #if defined(OM_CC_MIPS)
70 # include <math.h>
71 #else
72 # include <cmath>
73 #endif
74 
75 
76 //== NAMESPACE ================================================================
77 
78 namespace OpenMesh { // BEGIN_NS_OPENMESH
79 namespace Subdivider { // BEGIN_NS_DECIMATER
80 namespace Uniform { // BEGIN_NS_UNIFORM
81 
82 
83 //== CLASS DEFINITION =========================================================
84 
85 template <typename MeshType, typename RealType = float>
87  public:
88 
89  typedef std::pair<typename MeshType::EdgeHandle, RealType> queueElement;
90 
91  bool operator()(const queueElement& t1, const queueElement& t2) // Returns true if t1 is smaller than t2
92  {
93  return (t1.second < t2.second);
94  }
95 };
96 
97 
104 template <typename MeshType, typename RealType = float>
105 class LongestEdgeT : public SubdividerT<MeshType, RealType>
106 {
107 public:
108 
109  typedef RealType real_t;
110  typedef MeshType mesh_t;
112 
113  typedef std::vector< std::vector<real_t> > weights_t;
114  typedef std::vector<real_t> weight_t;
115 
116  typedef std::pair< typename mesh_t::EdgeHandle, real_t > queueElement;
117 
118 public:
119 
120 
121  LongestEdgeT() : parent_t()
122  { }
123 
124 
125  LongestEdgeT( mesh_t& _m) : parent_t(_m)
126  { }
127 
128 
129  ~LongestEdgeT() {}
130 
131 
132 public:
133 
134 
135  const char *name() const { return "Longest Edge Split"; }
136 
137  void set_max_edge_length(double _value) {
138  max_edge_length_squared_ = _value * _value;
139  }
140 
141 protected:
142 
143 
144  bool prepare( mesh_t& _m )
145  {
146  return true;
147  }
148 
149 
150  bool cleanup( mesh_t& _m )
151  {
152  return true;
153  }
154 
155 
156  bool subdivide( MeshType& _m, size_t _n , const bool _update_points = true)
157  {
158 
159  // Sorted queue containing all edges sorted by their decreasing length
160  std::priority_queue< queueElement, std::vector< queueElement > , CompareLengthFunction< mesh_t, real_t > > queue;
161 
162  // Build the initial queue
163  // First element should be longest edge
164  typename mesh_t::EdgeIter edgesEnd = _m.edges_end();
165  for ( typename mesh_t::EdgeIter eit = _m.edges_begin(); eit != edgesEnd; ++eit) {
166  const typename MeshType::Point to = _m.point(_m.to_vertex_handle(_m.halfedge_handle(*eit,0)));
167  const typename MeshType::Point from = _m.point(_m.from_vertex_handle(_m.halfedge_handle(*eit,0)));
168 
169  real_t length = (to - from).sqrnorm();
170 
171  // Only push the edges that need to be split
172  if ( length > max_edge_length_squared_ )
173  queue.push( queueElement(*eit,length) );
174  }
175 
176  bool stop = false;
177  while ( !stop && ! queue.empty() ) {
178  queueElement a = queue.top();
179  queue.pop();
180 
181  if ( a.second < max_edge_length_squared_ ) {
182  stop = true;
183  break;
184  } else {
185  const typename MeshType::Point to = _m.point(_m.to_vertex_handle(_m.halfedge_handle(a.first,0)));
186  const typename MeshType::Point from = _m.point(_m.from_vertex_handle(_m.halfedge_handle(a.first,0)));
187  const typename MeshType::Point midpoint = static_cast<typename MeshType::Point::value_type>(0.5) * (to + from);
188 
189  const typename MeshType::VertexHandle newVertex = _m.add_vertex(midpoint);
190  _m.split(a.first,newVertex);
191 
192  for ( typename MeshType::VertexOHalfedgeIter voh_it(_m,newVertex); voh_it.is_valid(); ++voh_it) {
193  typename MeshType::EdgeHandle eh = _m.edge_handle(*voh_it);
194  const typename MeshType::Point to = _m.point(_m.to_vertex_handle(*voh_it));
195  const typename MeshType::Point from = _m.point(_m.from_vertex_handle(*voh_it));
196  real_t length = (to - from).sqrnorm();
197 
198  // Only push the edges that need to be split
199  if ( length > max_edge_length_squared_ )
200  queue.push( queueElement(eh,length) );
201 
202  }
203  }
204  }
205 
206 #if defined(_DEBUG) || defined(DEBUG)
207  // Now we have an consistent mesh!
208  assert( OpenMesh::Utils::MeshCheckerT<mesh_t>(_m).check() );
209 #endif
210 
211 
212  return true;
213  }
214 
215 
216 private: // data
217  real_t max_edge_length_squared_;
218 
219 };
220 
221 } // END_NS_UNIFORM
222 } // END_NS_SUBDIVIDER
223 } // END_NS_OPENMESH
224 #endif
225 
bool cleanup(mesh_t &_m)
Cleanup mesh after usage, e.g. remove added properties.
const char * name() const
Return name of subdivision algorithm.
bool subdivide(MeshType &_m, size_t _n, const bool _update_points=true)
Subdivide mesh _m _n times.
bool prepare(mesh_t &_m)
Prepare mesh, e.g. add properties.