MeshCheckerT.cc 6.37 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//=============================================================================
//                                                                            
//                               OpenMesh                                     
//        Copyright (C) 2003 by Computer Graphics Group, RWTH Aachen          
//                           www.openmesh.org                                 
//                                                                            
//-----------------------------------------------------------------------------
//                                                                            
//                                License                                     
//                                                                            
//   This library is free software; you can redistribute it and/or modify it 
//   under the terms of the GNU Lesser General Public License as published   
//   by the Free Software Foundation, version 2.1.                           
//                                                                             
//   This library is distributed in the hope that it will be useful, but       
//   WITHOUT ANY WARRANTY; without even the implied warranty of                
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
//   Lesser General Public License for more details.                           
//                                                                            
//   You should have received a copy of the GNU Lesser General Public          
//   License along with this library; if not, write to the Free Software       
//   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 
//                                                                            
//-----------------------------------------------------------------------------
//                                                                            
Jan Möbius's avatar
Jan Möbius committed
26
27
//   $Revision$
//   $Date$
Jan Möbius's avatar
Jan Möbius committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//                                                                            
//=============================================================================


#define OPENMESH_MESHCHECKER_C


//== INCLUDES =================================================================


#include <OpenMesh/Tools/Utils/MeshCheckerT.hh>


//== NAMESPACES ============================================================== 


namespace OpenMesh {
namespace Utils {

//== IMPLEMENTATION ========================================================== 


template <class Mesh>
bool 
MeshCheckerT<Mesh>::
check(unsigned int _targets, std::ostream& _os)
{
  bool  ok(true);



  //--- vertex checks ---

  if (_targets & CHECK_VERTICES)
  {
    typename Mesh::ConstVertexIter v_it(mesh_.vertices_begin()), 
                                   v_end(mesh_.vertices_end());
    typename Mesh::VertexHandle    vh;
    typename Mesh::ConstVertexVertexIter vv_it;
    typename Mesh::HalfedgeHandle  heh;
    unsigned int                   count;
    const unsigned int             max_valence(10000);


    for (; v_it != v_end; ++v_it)
    {
      if (!is_deleted(v_it))
      {
	vh = v_it.handle();


      /* The outgoing halfedge of a boundary vertex has to be a 
	 boundary halfedge */
	heh = mesh_.halfedge_handle(vh);
	if (heh.is_valid() && !mesh_.is_boundary(heh))
	{
	  for (typename Mesh::ConstVertexOHalfedgeIter vh_it(mesh_, vh);
	       vh_it; ++vh_it)
	  {
	    if (mesh_.is_boundary(vh_it.handle()))
	    {
	      _os << "MeshChecker: vertex " << vh
		  << ": outgoing halfedge not on boundary error\n";
	      ok = false;
	    }
	  }
	}

      
      
	// outgoing halfedge has to refer back to vertex
	if (mesh_.halfedge_handle(vh).is_valid() &&
	    mesh_.from_vertex_handle(mesh_.halfedge_handle(vh)) != vh)
	{
	  _os << "MeshChecker: vertex " << vh
	      << ": outgoing halfedge does not reference vertex\n";
	  ok = false;
	}


	// check whether circulators are still in order
	vv_it = mesh_.cvv_iter(vh);
	for (count=0; vv_it && (count < max_valence); ++vv_it, ++count) {};
	if (count == max_valence)
	{
	  _os << "MeshChecker: vertex " << vh
	      << ": ++circulator problem, one ring corrupt\n";
	  ok = false;
	}
	vv_it = mesh_.cvv_iter(vh);
	for (count=0; vv_it && (count < max_valence); --vv_it, ++count) {};
	if (count == max_valence)
	{
	  _os << "MeshChecker: vertex " << vh
	      << ": --circulator problem, one ring corrupt\n";
	  ok = false;
	}
      }
    }
  }



  //--- halfedge checks ---

  if (_targets & CHECK_EDGES)
  {
    typename Mesh::ConstHalfedgeIter  h_it(mesh_.halfedges_begin()), 
                                      h_end(mesh_.halfedges_end());
    typename Mesh::HalfedgeHandle     hh, hstart, hhh;
    unsigned int                      count, n_halfedges = 2*mesh_.n_edges();

    for (; h_it != h_end; ++h_it)
    {
      if (!is_deleted(mesh_.edge_handle(h_it.handle())))
      {
	hh = h_it.handle();


	// degenerated halfedge ?
	if (mesh_.from_vertex_handle(hh) == mesh_.to_vertex_handle(hh))
	{
	  _os << "MeshChecker: halfedge " << hh
	      << ": to-vertex == from-vertex\n";
	  ok = false;
	}


	// next <-> prev check
	if (mesh_.next_halfedge_handle(mesh_.prev_halfedge_handle(hh)) != hh)
	{
	  _os << "MeshChecker: halfedge " << hh
	      << ": prev->next != this\n";
	  ok = false;
	}


	// halfedges should form a cycle
	count=0; hstart=hhh=hh;
	do 
	{
	  hhh = mesh_.next_halfedge_handle(hhh);
	  ++count;
	} while (hhh != hstart && count < n_halfedges);

	if (count == n_halfedges)
	{
	  _os << "MeshChecker: halfedges starting from " << hh
	      << " do not form a cycle\n";
	  ok = false;
	}
      }
    }
  }



  //--- face checks ---

  if (_targets & CHECK_FACES)
  {
    typename Mesh::ConstFaceIter          f_it(mesh_.faces_begin()), 
                                          f_end(mesh_.faces_end());
    typename Mesh::FaceHandle             fh;
    typename Mesh::ConstFaceHalfedgeIter  fh_it;
    
    for (; f_it != f_end; ++f_it)
    {
      if (!is_deleted(f_it))
      {
	fh = f_it.handle();

	for (fh_it=mesh_.cfh_iter(fh); fh_it; ++fh_it)
	{
	  if (mesh_.face_handle(fh_it.handle()) != fh)
	  {
	    _os << "MeshChecker: face " << fh
		<< ": its halfedge does not reference face\n";
	    ok = false;
	  }
	}
      }
    }
  }



  return ok;
}


//=============================================================================
} // naespace Utils
} // namespace OpenMesh
//=============================================================================