unittests_trimesh_iterators.hh 8.78 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 26 27 28 29 30 31 32 33 34 35 36 37
#ifndef INCLUDE_UNITTESTS_TRIMESH_ITERATORS_HH
#define INCLUDE_UNITTESTS_TRIMESH_ITERATORS_HH

#include <gtest/gtest.h>
#include <Unittests/unittests_common.hh>

#include <iostream>

class OpenMeshIterators : public OpenMeshBase {

    protected:

        // This function is called before each test is run
        virtual void SetUp() {
        }

        // This function is called after all tests are through
        virtual void TearDown() {

            // Do some final stuff with the member data here...
        }

    // Member already defined in OpenMeshBase
    //Mesh mesh_;  
};

/*
 * ====================================================================
 * Define tests below
 * ====================================================================
 */

/*
 * Small VertexIterator Test
 */
TEST_F(OpenMeshIterators, VertexIter) {

38
  mesh_.clear();
Jan Möbius's avatar
Jan Möbius committed
39

40 41
  // Add some vertices
  Mesh::VertexHandle vhandle[4];
Jan Möbius's avatar
Jan Möbius committed
42

43 44 45 46
  vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
  vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
  vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
  vhandle[3] = mesh_.add_vertex(Mesh::Point(1, 0, 0));
Jan Möbius's avatar
Jan Möbius committed
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
  // Add two faces
  std::vector<Mesh::VertexHandle> face_vhandles;

  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[0]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[3]);
  mesh_.add_face(face_vhandles);

  // Test setup:
  //  1 === 2
  //  |   / |    
  //  |  /  |
  //  | /   |
  //  0 === 3

  Mesh::VertexIter v_it=mesh_.vertices_begin(); 
  Mesh::VertexIter  v_end=mesh_.vertices_end(); 


  EXPECT_EQ(0, v_it.handle().idx()) << "Index wrong for vertex iterator vertices_begin()";
  ++v_it;
  EXPECT_EQ(1, v_it.handle().idx()) << "Index wrong in vertex iterator";
  ++v_it;
  EXPECT_EQ(2, v_it.handle().idx()) << "Index wrong in vertex iterator";
  ++v_it;
  EXPECT_EQ(3, v_it.handle().idx()) << "Index wrong in vertex iterator";
  ++v_it;
  EXPECT_EQ(4, v_it.handle().idx()) << "Index wrong in vertex iterator";

  // Check end iterator
  EXPECT_EQ(4, v_end.handle().idx()) << "Index wrong in vertex iterator for vertices_end()";    
Jan Möbius's avatar
Jan Möbius committed
86 87 88

}

Jan Möbius's avatar
Jan Möbius committed
89 90 91 92 93
/*
 * Small EdgeIterator Test
 */
TEST_F(OpenMeshIterators, EdgeIter) {

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
  mesh_.clear();

  // Add some vertices
  Mesh::VertexHandle vhandle[4];

  vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
  vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
  vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
  vhandle[3] = mesh_.add_vertex(Mesh::Point(1, 0, 0));

  // Add two faces
  std::vector<Mesh::VertexHandle> face_vhandles;

  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[0]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[3]);
  mesh_.add_face(face_vhandles);

  // Test setup:
  //  1 === 2
  //  |   / |    
  //  |  /  |
  //  | /   |
  //  0 === 3

Jan Möbius's avatar
Jan Möbius committed
126

127 128 129 130 131
  Mesh::EdgeIter e_it  = mesh_.edges_begin(); 
  Mesh::EdgeIter e_end = mesh_.edges_end(); 
  
  EXPECT_EQ(0, e_it.handle().idx()) << "Wrong start index in edge iterator";
  EXPECT_EQ(5, e_end.handle().idx()) << "Wrong end index in edge iterator";
Jan Möbius's avatar
Jan Möbius committed
132

133 134 135 136
  EXPECT_EQ(1, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() )   << "1: Wrong to vertex handle of halfedge 0";
  EXPECT_EQ(2, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() ) << "1: Wrong from vertex handle of halfedge 0";
  EXPECT_EQ(2, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() )   << "1: Wrong to vertex handle of halfedge 1";
  EXPECT_EQ(1, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() ) << "1: Wrong from vertex handle of halfedge 1";
Jan Möbius's avatar
Jan Möbius committed
137

138 139
  ++e_it;
  EXPECT_EQ(1, e_it.handle().idx()) << "Wrong index in edge iterator";
Jan Möbius's avatar
Jan Möbius committed
140

141 142 143 144
  EXPECT_EQ(0, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() )   << "2: Wrong to vertex handle of halfedge 0";
  EXPECT_EQ(1, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() ) << "2: Wrong from vertex handle of halfedge 0";
  EXPECT_EQ(1, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() )   << "2: Wrong to vertex handle of halfedge 1";
  EXPECT_EQ(0, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() ) << "2: Wrong from vertex handle of halfedge 1";
Jan Möbius's avatar
Jan Möbius committed
145 146


147 148
  ++e_it;
  EXPECT_EQ(2, e_it.handle().idx()) << "Wrong index in edge iterator";
Jan Möbius's avatar
Jan Möbius committed
149

150 151 152 153
  EXPECT_EQ(2, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() )   << "3: Wrong to vertex handle of halfedge 0";
  EXPECT_EQ(0, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() ) << "3: Wrong from vertex handle of halfedge 0";
  EXPECT_EQ(0, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() )   << "3: Wrong to vertex handle of halfedge 1";
  EXPECT_EQ(2, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() ) << "3: Wrong from vertex handle of halfedge 1";
Jan Möbius's avatar
Jan Möbius committed
154 155


156 157
  ++e_it;
  EXPECT_EQ(3, e_it.handle().idx()) << "Wrong index in edge iterator";
Jan Möbius's avatar
Jan Möbius committed
158

159 160 161 162
  EXPECT_EQ(3, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() )   << "4: Wrong to vertex handle of halfedge 0";
  EXPECT_EQ(0, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() ) << "4: Wrong from vertex handle of halfedge 0";
  EXPECT_EQ(0, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() )   << "4: Wrong to vertex handle of halfedge 1";
  EXPECT_EQ(3, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() ) << "4: Wrong from vertex handle of halfedge 1";
Jan Möbius's avatar
Jan Möbius committed
163 164


165 166
  ++e_it;
  EXPECT_EQ(4, e_it.handle().idx()) << "Wrong index in edge iterator";
Jan Möbius's avatar
Jan Möbius committed
167

168 169 170 171
  EXPECT_EQ(2, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() )   << "5: Wrong to vertex handle of halfedge 0";
  EXPECT_EQ(3, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,0)).idx() ) << "5: Wrong from vertex handle of halfedge 0";
  EXPECT_EQ(3, mesh_.to_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() )   << "5: Wrong to vertex handle of halfedge 1";
  EXPECT_EQ(2, mesh_.from_vertex_handle(mesh_.halfedge_handle(e_it,1)).idx() ) << "5: Wrong from vertex handle of halfedge 1";
Jan Möbius's avatar
Jan Möbius committed
172 173 174 175


}

176
/*
177
 * Test with a mesh with one deleted face
178
 */
179
TEST_F(OpenMeshIterators, FaceIterEmptyMeshOneDeletedFace) {
180 181 182

  mesh_.clear();

183 184 185 186
  // request delete_face capability
  mesh_.request_vertex_status();
  mesh_.request_edge_status();
  mesh_.request_face_status();
187 188

  // Add some vertices
189
  Mesh::VertexHandle vhandle[4];
190

191 192 193
  vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
  vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
  vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
194 195 196 197 198 199 200

  // Add two faces
  std::vector<Mesh::VertexHandle> face_vhandles;

  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[0]);
201
  Mesh::FaceHandle fh = mesh_.add_face(face_vhandles);
202

203 204 205
  // Delete face but keep vertices
  bool const is_delete_isolated_vertex = false;
  mesh_.delete_face(fh, is_delete_isolated_vertex);
Jan Möbius's avatar
Jan Möbius committed
206

207 208 209 210 211 212
  // Test setup (Face deleted but vertices kept.
  //  1 === 2
  //  |   /
  //  |  /
  //  | /
  //  0
Jan Möbius's avatar
Jan Möbius committed
213

214 215
  Mesh::FaceIter f_it  = mesh_.faces_begin();
  Mesh::FaceIter f_end = mesh_.faces_end();
Jan Möbius's avatar
Jan Möbius committed
216

217
  EXPECT_EQ(0, f_it.handle().idx()) << "Wrong start index in FaceIterator";
Jan Möbius's avatar
Jan Möbius committed
218

219
  EXPECT_EQ(1, f_end.handle().idx()) << "Wrong end index in FaceIterator";
Jan Möbius's avatar
Jan Möbius committed
220

221 222 223
  ++f_it;
  EXPECT_EQ(1, f_it.handle().idx()) << "Wrong end index in FaceIterator after one step";
  EXPECT_TRUE(f_it == f_end ) << "Iterator not at end for FaceIterator after one step";
Jan Möbius's avatar
Jan Möbius committed
224

225 226
  Mesh::ConstFaceIter cf_it  = mesh_.faces_begin();
  Mesh::ConstFaceIter cf_end = mesh_.faces_end();
Jan Möbius's avatar
Jan Möbius committed
227

228
  EXPECT_EQ(0, cf_it.handle().idx()) << "Wrong start index in ConstFaceIterator";
Jan Möbius's avatar
Jan Möbius committed
229

230
  EXPECT_EQ(1, cf_end.handle().idx()) << "Wrong end index in ConstFaceIterator";
Jan Möbius's avatar
Jan Möbius committed
231

232 233 234
  ++cf_it;
  EXPECT_EQ(1, cf_it.handle().idx()) << "Wrong end index in ConstFaceIterator after one step";
  EXPECT_TRUE(cf_it == cf_end ) << "Iterator not at end for ConstFaceIterator after one step";
Jan Möbius's avatar
Jan Möbius committed
235 236


237 238 239
  // Same with skipping iterators:
  f_it  = mesh_.faces_sbegin();
  f_end = mesh_.faces_end();
Jan Möbius's avatar
Jan Möbius committed
240

241
  EXPECT_EQ(1, f_it.handle().idx()) << "Wrong start index in FaceIterator with skipping";
Jan Möbius's avatar
Jan Möbius committed
242

243
  EXPECT_EQ(1, f_end.handle().idx()) << "Wrong end index in FaceIterator with skipping";
Jan Möbius's avatar
Jan Möbius committed
244

245
  EXPECT_TRUE(f_it == f_end ) << "Iterator not at end for FaceIterator with skipping";
Jan Möbius's avatar
Jan Möbius committed
246

247 248 249
  // Same with skipping iterators:
  cf_it  = mesh_.faces_sbegin();
  cf_end = mesh_.faces_end();
Jan Möbius's avatar
Jan Möbius committed
250

251
  EXPECT_EQ(1, cf_it.handle().idx()) << "Wrong start index in ConstFaceIterator with skipping";
Jan Möbius's avatar
Jan Möbius committed
252

253
  EXPECT_EQ(1, cf_end.handle().idx()) << "Wrong end index in ConstFaceIterator with skipping";
Jan Möbius's avatar
Jan Möbius committed
254

255
  EXPECT_TRUE(cf_it == cf_end ) << "Iterator not at end for ConstFaceIterator with skipping";
Jan Möbius's avatar
Jan Möbius committed
256

257

258 259 260
  mesh_.release_vertex_status();
  mesh_.release_edge_status();
  mesh_.release_face_status();
Jan Möbius's avatar
Jan Möbius committed
261 262

}
263

Jan Möbius's avatar
Jan Möbius committed
264
#endif // INCLUDE GUARD