unittests_tutorials.cc 28.7 KB
Newer Older
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 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

#include <gtest/gtest.h>
#include <Unittests/unittests_common.hh>
#include <string>
#include <map>
#include "generate_cube.hh"
#include "fill_props.hh"

/*
 * ====================================================================
 * Definition of custom properties related classes
 * ====================================================================
 */

struct MyData
{
  int             ival;
  double          dval;
  bool            bval;
  OpenMesh::Vec4f vec4fval;

  MyData()
    : ival(0), dval(0.0), bval(false)
  { }

  MyData( const MyData& _cpy )
    : ival(_cpy.ival), dval(_cpy.dval), bval(_cpy.bval),
      vec4fval(_cpy.vec4fval)
  { }

  // ---------- assignment

  MyData& operator = (const MyData& _rhs)
  {
    ival = _rhs.ival;
    dval = _rhs.dval;
    bval = _rhs.bval;
    vec4fval = _rhs.vec4fval;
    return *this;
  }

  MyData& operator = (int    _rhs) { ival = _rhs; return *this; }
  MyData& operator = (double _rhs) { dval = _rhs; return *this; }
  MyData& operator = (bool   _rhs) { bval = _rhs; return *this; }
  MyData& operator = (const OpenMesh::Vec4f& _rhs)
  { vec4fval = _rhs; return *this; }

  // ---------- comparison

  bool operator == (const MyData& _rhs) const
  {
    return ival == _rhs.ival
      &&   dval == _rhs.dval
      &&   bval == _rhs.bval
      &&   vec4fval == _rhs.vec4fval;
  }
  bool operator != (const MyData& _rhs) const { return !(*this == _rhs); }
};

typedef std::map< std::string, unsigned int > MyMap;

namespace OpenMesh {
  namespace IO {
    // support persistence for struct MyData
    template <> struct binary<MyData>
    {
      typedef MyData value_type;
      static const bool is_streamable = true;

      // return binary size of the value
      static size_t size_of(void)
      {
        return sizeof(int)+sizeof(double)+sizeof(bool)+sizeof(OpenMesh::Vec4f);
      }

      static size_t size_of(const value_type&)
      {
        return size_of();
      }

      static size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
      {
        size_t bytes;
        bytes  = IO::store( _os, _v.ival, _swap );
        bytes += IO::store( _os, _v.dval, _swap );
        bytes += IO::store( _os, _v.bval, _swap );
        bytes += IO::store( _os, _v.vec4fval, _swap );
        return _os.good() ? bytes : 0;
      }

      static size_t restore( std::istream& _is, value_type& _v, bool _swap=false)
      {
        size_t bytes;
        bytes  = IO::restore( _is, _v.ival, _swap );
        bytes += IO::restore( _is, _v.dval, _swap );
        bytes += IO::restore( _is, _v.bval, _swap );
        bytes += IO::restore( _is, _v.vec4fval, _swap );
        return _is.good() ? bytes : 0;
      }
    };

    template <> struct binary< MyMap >
    {
      typedef MyMap value_type;
      static const bool is_streamable = true;

      // return generic binary size of self, if known
      static size_t size_of(void) { return UnknownSize; }

      // return binary size of the value
      static size_t size_of(const value_type& _v)
      {
        if (_v.empty())
          return sizeof(unsigned int);

        value_type::const_iterator it = _v.begin();
        unsigned int   N     = _v.size();
        size_t         bytes = IO::size_of(N);
        for(;it!=_v.end(); ++it)
        {
          bytes += IO::size_of( it->first );
          bytes += IO::size_of( it->second );
        }
        return bytes;
      }

      static
      size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
      {
        size_t   bytes = 0;
        unsigned int N = _v.size();
        value_type::const_iterator it = _v.begin();
        bytes += IO::store( _os, N, _swap );
        for (; it != _v.end() && _os.good(); ++it)
        {
          bytes += IO::store( _os, it->first, _swap );
          bytes += IO::store( _os, it->second, _swap );
        }
        return _os.good() ? bytes : 0;
      }

      static
      size_t restore( std::istream& _is, value_type& _v, bool _swap=false)
      {
        size_t   bytes = 0;
        unsigned int N = 0;
        _v.clear();
        bytes += IO::restore( _is, N, _swap );
Matthias Möller's avatar
Matthias Möller committed
149 150
        value_type::key_type key;
        value_type::mapped_type  val;
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
        for (size_t i=0; i<N && _is.good(); ++i)
        {
          bytes += IO::restore( _is, key, _swap );
          bytes += IO::restore( _is, val, _swap );
          _v[key] = val;
        }
        return _is.good() ? bytes : 0;
      }
    };

  }
}

namespace {

class OpenMeshTutorials: public OpenMeshBase {

    protected:

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

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

        // 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_;
};

/*
 * ====================================================================
 * Classes for unittests
 * ====================================================================
 */

template <class Mesh> class SmootherT
{
public:
  typedef typename Mesh::Point            cog_t;
  typedef OpenMesh::VPropHandleT< cog_t > Property_cog;

public:
  // construct with a given mesh
  SmootherT(Mesh& _mesh)
    : mesh_(_mesh)
  {
    mesh_.add_property( cog_ );
  }
  ~SmootherT()
  {
    mesh_.remove_property( cog_ );
  }
  // smooth mesh _iterations times
  void smooth(unsigned int _iterations)
  {
    for (unsigned int i=0; i < _iterations; ++i)
    {
      std::for_each(mesh_.vertices_begin(),
                    mesh_.vertices_end(),
                    ComputeCOG(mesh_, cog_));
      std::for_each(mesh_.vertices_begin(),
                    mesh_.vertices_end(),
                    SetCOG(mesh_, cog_));
    }
  }

private:
  //--- private classes ---
  class ComputeCOG
  {
  public:
    ComputeCOG(Mesh& _mesh, Property_cog& _cog)
      : mesh_(_mesh), cog_(_cog)
    {}
    void operator()(const typename Mesh::VertexHandle& _vh)
    {
      typename Mesh::VertexVertexIter  vv_it;
      typename Mesh::Scalar            valence(0.0);

      mesh_.property(cog_, _vh) = typename Mesh::Point(0.0, 0.0, 0.0);
      for (vv_it=mesh_.vv_iter(_vh); vv_it.is_valid(); ++vv_it)
      {
        mesh_.property(cog_, _vh) += mesh_.point( *vv_it );
        ++valence;
      }
      mesh_.property(cog_, _vh ) /= valence;
    }
  private:
    Mesh&         mesh_;
    Property_cog& cog_;
  };

  class SetCOG
  {
  public:
    SetCOG(Mesh& _mesh, Property_cog& _cog)
      : mesh_(_mesh), cog_(_cog)
    {}
    void operator()(const typename Mesh::VertexHandle& _vh)
    {
      if (!mesh_.is_boundary(_vh))
        mesh_.set_point( _vh, mesh_.property(cog_, _vh) );
    }
  private:
    Mesh&         mesh_;
    Property_cog& cog_;
  };

  //--- private elements ---
  Mesh&        mesh_;
  Property_cog cog_;
};


/*
 * ====================================================================
 * Specify our traits
 * ====================================================================
 */

struct MyTraits : public OpenMesh::DefaultTraits
{
  HalfedgeAttributes(OpenMesh::Attributes::PrevHalfedge);
};

// Define my personal fancy traits
struct MyFancyTraits : OpenMesh::DefaultTraits
{
  // Let Point and Normal be a vector of doubles
  typedef OpenMesh::Vec3d Point;
  typedef OpenMesh::Vec3d Normal;
  // Already defined in OpenMesh::DefaultTraits
  // HalfedgeAttributes( OpenMesh::Attributes::PrevHalfedge );

  // Uncomment next line to disable attribute PrevHalfedge
  // HalfedgeAttributes( OpenMesh::Attributes::None );
  //
  // or
  //
  // HalfedgeAttributes( 0 );
};

struct MyTraitsWithCOG : public OpenMesh::DefaultTraits
{
  // store barycenter of neighbors in this member
  VertexTraits
  {
  private:
    Point  cog_;
  public:
    VertexT() : cog_( Point(0.0f, 0.0f, 0.0f ) ) { }
    const Point& cog() const { return cog_; }
    void set_cog(const Point& _p) { cog_ = _p; }
  };
};

struct MyTraitsWithStatus : public OpenMesh::DefaultTraits
{
  VertexAttributes(OpenMesh::Attributes::Status);
  FaceAttributes(OpenMesh::Attributes::Status);
  EdgeAttributes(OpenMesh::Attributes::Status);
};

/*
 * ====================================================================
 * Specify our meshes
 * ====================================================================
 */
typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;
typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits> MyMeshWithTraits;
typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits> MyTriMesh;
typedef OpenMesh::TriMesh_ArrayKernelT<MyFancyTraits>  MyFancyTriMesh;
typedef OpenMesh::TriMesh_ArrayKernelT<MyTraitsWithCOG>  MyTriMeshWithCOG;
typedef OpenMesh::PolyMesh_ArrayKernelT<MyTraitsWithStatus> MyMeshWithStatus;

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

/*
 */
TEST_F(OpenMeshTutorials, building_a_cube) {

  MyMesh mesh;

  // generate vertices
  MyMesh::VertexHandle vhandle[8];
  vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1,  1));
  vhandle[1] = mesh.add_vertex(MyMesh::Point( 1, -1,  1));
  vhandle[2] = mesh.add_vertex(MyMesh::Point( 1,  1,  1));
  vhandle[3] = mesh.add_vertex(MyMesh::Point(-1,  1,  1));
  vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
  vhandle[5] = mesh.add_vertex(MyMesh::Point( 1, -1, -1));
  vhandle[6] = mesh.add_vertex(MyMesh::Point( 1,  1, -1));
  vhandle[7] = mesh.add_vertex(MyMesh::Point(-1,  1, -1));

  // generate (quadrilateral) faces
  std::vector<MyMesh::VertexHandle>  face_vhandles;
  face_vhandles.clear();
  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[3]);
  mesh.add_face(face_vhandles);

  face_vhandles.clear();
  face_vhandles.push_back(vhandle[7]);
  face_vhandles.push_back(vhandle[6]);
  face_vhandles.push_back(vhandle[5]);
  face_vhandles.push_back(vhandle[4]);
  mesh.add_face(face_vhandles);
  face_vhandles.clear();
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[4]);
  face_vhandles.push_back(vhandle[5]);
  mesh.add_face(face_vhandles);
  face_vhandles.clear();
  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[5]);
  face_vhandles.push_back(vhandle[6]);
  mesh.add_face(face_vhandles);
  face_vhandles.clear();
  face_vhandles.push_back(vhandle[3]);
  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[6]);
  face_vhandles.push_back(vhandle[7]);
  mesh.add_face(face_vhandles);
  face_vhandles.clear();
  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[3]);
  face_vhandles.push_back(vhandle[7]);
  face_vhandles.push_back(vhandle[4]);
  mesh.add_face(face_vhandles);

  bool ok = OpenMesh::IO::write_mesh(mesh, "output.off");

  EXPECT_TRUE(ok) << "Cannot write mesh to file 'output.off'";
}

TEST_F(OpenMeshTutorials, using_iterators_and_circulators) {
  MyMesh  mesh;

  bool ok = OpenMesh::IO::read_mesh(mesh, "output.off");

  EXPECT_TRUE(ok) << "Cannot read mesh from file 'output.off'";

  // this vector stores the computed centers of gravity
  std::vector<MyMesh::Point>  cogs;
  std::vector<MyMesh::Point>::iterator cog_it;
  cogs.reserve(mesh.n_vertices());

  // smoothing mesh N times
  MyMesh::VertexIter          v_it, v_end(mesh.vertices_end());
  MyMesh::VertexVertexIter    vv_it;
  MyMesh::Point               cog;
  MyMesh::Scalar              valence;
  unsigned int                i, N(100);
  for (i=0; i < N; ++i)
  {
    cogs.clear();
    for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it)
    {
      cog[0] = cog[1] = cog[2] = valence = 0.0;

      for (vv_it = mesh.vv_iter( *v_it ); vv_it.is_valid(); ++vv_it)
      {
        cog += mesh.point( *vv_it );
        ++valence;
      }
      cogs.push_back(cog / valence);
    }

    for (v_it = mesh.vertices_begin(), cog_it = cogs.begin();
         v_it != v_end; ++v_it, ++cog_it)
      if ( !mesh.is_boundary( *v_it ) )
        mesh.set_point( *v_it, *cog_it );
  }

  // write mesh
  ok = OpenMesh::IO::write_mesh(mesh, "smoothed_output.off");

  EXPECT_TRUE(ok) << "Cannot write mesh to file 'smoothed_output.off'";
}

TEST_F(OpenMeshTutorials, using_custom_properties) {
  MyMesh  mesh;

  bool ok = OpenMesh::IO::read_mesh(mesh, "output.off");
  EXPECT_TRUE(ok) << "Cannot read mesh from file 'output.off'";

  // this vertex property stores the computed centers of gravity
  OpenMesh::VPropHandleT<MyMesh::Point> cogs;
  mesh.add_property(cogs);

  // smoothing mesh N times
  MyMesh::VertexIter          v_it, v_end(mesh.vertices_end());
  MyMesh::VertexVertexIter    vv_it;
  MyMesh::Point               cog;
  MyMesh::Scalar              valence;
  unsigned int                i, N(100);

  for (i=0; i < N; ++i)
  {
    for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it)
    {
      mesh.property(cogs,*v_it).vectorize(0.0f);
      valence = 0.0;

      for (vv_it = mesh.vv_iter( *v_it ); vv_it.is_valid(); ++vv_it)
      {
        mesh.property(cogs,*v_it) += mesh.point( *vv_it );
        ++valence;
      }
      mesh.property(cogs,*v_it) /= valence;
    }

    for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it)
      if ( !mesh.is_boundary( *v_it ) )
        mesh.set_point( *v_it, mesh.property(cogs,*v_it) );
  }

  // write mesh
  ok = OpenMesh::IO::write_mesh(mesh, "smoothed_custom_properties_output.off");

  EXPECT_TRUE(ok) << "Cannot write mesh to file 'smoothed_custom_properties_output.off'";
}

TEST_F(OpenMeshTutorials, using_STL_algorithms) {
  MyMeshWithTraits mesh;

  bool ok = OpenMesh::IO::read_mesh(mesh, "output.off");
  EXPECT_TRUE(ok) << "Cannot read mesh from file 'output.off'";

  SmootherT<MyMeshWithTraits> smoother(mesh);
  smoother.smooth(100);

  // write mesh
  ok = OpenMesh::IO::write_mesh(mesh, "smoothed_STL_output.off");

  EXPECT_TRUE(ok) << "Cannot write mesh to file 'smoothed_STL_output.off'";
}

TEST_F(OpenMeshTutorials, using_standard_properties) {
  MyTriMesh mesh;

  mesh.request_vertex_normals();
  EXPECT_TRUE(mesh.has_vertex_normals()) << "Standard vertex property 'Normals' not available";

  OpenMesh::IO::Options opt;
  bool ok = OpenMesh::IO::read_mesh(mesh, "output.off", opt);
  EXPECT_TRUE(ok) << "Cannot read mesh from file 'output.off'";

  // If the file did not provide vertex normals, then calculate them
  if ( !opt.check( OpenMesh::IO::Options::VertexNormal ) )
  {
    // we need face normals to update the vertex normals
    mesh.request_face_normals();
    // let the mesh update the normals
    mesh.update_normals();
    // dispose the face normals, as we don't need them anymore
    mesh.release_face_normals();
  }

  // move all vertices one unit length along it's normal direction
  for (MyMesh::VertexIter v_it = mesh.vertices_begin();
       v_it != mesh.vertices_end(); ++v_it)
  {
    mesh.set_point( *v_it, mesh.point(*v_it)+mesh.normal(*v_it) );
  }

  // don't need the normals anymore? Remove them!
  mesh.release_vertex_normals();
  // just check if it really works
  EXPECT_FALSE(mesh.has_vertex_normals()) << "Shouldn't have any vertex normals anymore";
}

TEST_F(OpenMeshTutorials, using_mesh_attributes_and_traits) {
  MyFancyTriMesh mesh;

  // Just make sure that point element type is double
  EXPECT_TRUE(typeid( OpenMesh::vector_traits<MyFancyTriMesh::Point>::value_type ) ==
      typeid(double)) << "Data type is wrong";

  // Make sure that normal element type is double
  EXPECT_TRUE(typeid( OpenMesh::vector_traits<MyFancyTriMesh::Normal>::value_type ) ==
      typeid(double)) << "Data type is wrong";

  // Add vertex normals as default property (ref. previous tutorial)
  mesh.request_vertex_normals();
  // Add face normals as default property
  mesh.request_face_normals();

  // load a mesh
  OpenMesh::IO::Options opt;
  bool ok = OpenMesh::IO::read_mesh(mesh, "output.off", opt);
  EXPECT_TRUE(ok) << "Cannot read mesh from file 'output.off'";

  // If the file did not provide vertex normals, then calculate them
  if ( !opt.check( OpenMesh::IO::Options::VertexNormal ) &&
      mesh.has_face_normals() && mesh.has_vertex_normals() )
  {
    // let the mesh update the normals
    mesh.update_normals();
  }

  // move all vertices one unit length along it's normal direction
  for (MyMesh::VertexIter v_it = mesh.vertices_begin();
      v_it != mesh.vertices_end(); ++v_it)
  {
    mesh.set_point( *v_it, mesh.point(*v_it)+mesh.normal(*v_it) );
  }
}

TEST_F(OpenMeshTutorials, extending_the_mesh_using_traits) {
  MyTriMeshWithCOG mesh;

  bool ok = OpenMesh::IO::read_mesh(mesh, "output.off");
  EXPECT_TRUE(ok) << "Cannot read mesh from file 'output.off'";

  // smoothing mesh N times
  MyTriMeshWithCOG::VertexIter          v_it, v_end(mesh.vertices_end());
  MyTriMeshWithCOG::VertexVertexIter    vv_it;
  MyTriMeshWithCOG::Point               cog;
  MyTriMeshWithCOG::Scalar              valence;
  unsigned int                i, N(100);

  for (i=0; i < N; ++i)
  {
    for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it)
    {
      cog[0] = cog[1] = cog[2] = valence = 0.0;

      for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it)
      {
        cog += mesh.point( *vv_it );
        ++valence;
      }
      mesh.data(*v_it).set_cog(cog / valence);
    }

    for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it)
      if (!mesh.is_boundary(*v_it))
        mesh.set_point( *v_it, mesh.data(*v_it).cog());
  }

  // write mesh
  ok = OpenMesh::IO::write_mesh(mesh, "smoothed_extended_output.off");

  EXPECT_TRUE(ok) << "Cannot write mesh to file 'smoothed_extended_output.off'";
}

612

613
TEST_F(OpenMeshTutorials, deleting_geometry_elements) {
614 615 616 617 618 619
  Mesh mesh;

  // the request has to be called before a vertex/face/edge can be deleted. it grants access to the status attribute
  mesh.request_face_status();
  mesh.request_edge_status();
  mesh.request_vertex_status();
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

  // generate vertices
  MyMeshWithStatus::VertexHandle vhandle[8];
  MyMeshWithStatus::FaceHandle   fhandle[6];

  vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1,  1));
  vhandle[1] = mesh.add_vertex(MyMesh::Point( 1, -1,  1));
  vhandle[2] = mesh.add_vertex(MyMesh::Point( 1,  1,  1));
  vhandle[3] = mesh.add_vertex(MyMesh::Point(-1,  1,  1));
  vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
  vhandle[5] = mesh.add_vertex(MyMesh::Point( 1, -1, -1));
  vhandle[6] = mesh.add_vertex(MyMesh::Point( 1,  1, -1));
  vhandle[7] = mesh.add_vertex(MyMesh::Point(-1,  1, -1));

  // generate (quadrilateral) faces
  std::vector<MyMesh::VertexHandle>  tmp_face_vhandles;
  tmp_face_vhandles.clear();
  tmp_face_vhandles.push_back(vhandle[0]);
  tmp_face_vhandles.push_back(vhandle[1]);
  tmp_face_vhandles.push_back(vhandle[2]);
  tmp_face_vhandles.push_back(vhandle[3]);
  fhandle[0] = mesh.add_face(tmp_face_vhandles);

  tmp_face_vhandles.clear();
  tmp_face_vhandles.push_back(vhandle[7]);
  tmp_face_vhandles.push_back(vhandle[6]);
  tmp_face_vhandles.push_back(vhandle[5]);
  tmp_face_vhandles.push_back(vhandle[4]);
  fhandle[1] = mesh.add_face(tmp_face_vhandles);

  tmp_face_vhandles.clear();
  tmp_face_vhandles.push_back(vhandle[1]);
  tmp_face_vhandles.push_back(vhandle[0]);
  tmp_face_vhandles.push_back(vhandle[4]);
  tmp_face_vhandles.push_back(vhandle[5]);
  fhandle[2] = mesh.add_face(tmp_face_vhandles);

  tmp_face_vhandles.clear();
  tmp_face_vhandles.push_back(vhandle[2]);
  tmp_face_vhandles.push_back(vhandle[1]);
  tmp_face_vhandles.push_back(vhandle[5]);
  tmp_face_vhandles.push_back(vhandle[6]);
  fhandle[3] = mesh.add_face(tmp_face_vhandles);
  tmp_face_vhandles.clear();
  tmp_face_vhandles.push_back(vhandle[3]);
  tmp_face_vhandles.push_back(vhandle[2]);
  tmp_face_vhandles.push_back(vhandle[6]);
  tmp_face_vhandles.push_back(vhandle[7]);
  fhandle[4] = mesh.add_face(tmp_face_vhandles);

  tmp_face_vhandles.clear();
  tmp_face_vhandles.push_back(vhandle[0]);
  tmp_face_vhandles.push_back(vhandle[3]);
  tmp_face_vhandles.push_back(vhandle[7]);
  tmp_face_vhandles.push_back(vhandle[4]);
  fhandle[5] = mesh.add_face(tmp_face_vhandles);

  // And now delete all faces and vertices
  // except face (vh[7], vh[6], vh[5], vh[4])
  // whose handle resides in fhandle[1]

  EXPECT_FALSE(mesh.status(fhandle[0]).deleted()) << "face shouldn't be deleted";
  EXPECT_FALSE(mesh.status(fhandle[1]).deleted()) << "face shouldn't be deleted";
  EXPECT_FALSE(mesh.status(fhandle[2]).deleted()) << "face shouldn't be deleted";
  EXPECT_FALSE(mesh.status(fhandle[3]).deleted()) << "face shouldn't be deleted";
  EXPECT_FALSE(mesh.status(fhandle[4]).deleted()) << "face shouldn't be deleted";
  EXPECT_FALSE(mesh.status(fhandle[5]).deleted()) << "face shouldn't be deleted";

  // Delete face 0
  mesh.delete_face(fhandle[0], false);
  // ... face 2
  mesh.delete_face(fhandle[2], false);
  // ... face 3
  mesh.delete_face(fhandle[3], false);
  // ... face 4
  mesh.delete_face(fhandle[4], false);
  // ... face 5
  mesh.delete_face(fhandle[5], false);

  EXPECT_TRUE(mesh.status(fhandle[0]).deleted()) << "face should be deleted";
  EXPECT_FALSE(mesh.status(fhandle[1]).deleted()) << "face shouldn't be deleted";
  EXPECT_TRUE(mesh.status(fhandle[2]).deleted()) << "face should be deleted";
  EXPECT_TRUE(mesh.status(fhandle[3]).deleted()) << "face should be deleted";
  EXPECT_TRUE(mesh.status(fhandle[4]).deleted()) << "face should be deleted";
  EXPECT_TRUE(mesh.status(fhandle[5]).deleted()) << "face should be deleted";

  // If isolated vertices result in a face deletion
  // they have to be deleted manually. If you want this
  // to happen automatically, change the second parameter
  // to true.
  // Now delete the isolated vertices 0, 1, 2 and 3

  EXPECT_FALSE(mesh.status(vhandle[0]).deleted()) << "vertex shouldn't be deleted";
  EXPECT_FALSE(mesh.status(vhandle[1]).deleted()) << "vertex shouldn't be deleted";
  EXPECT_FALSE(mesh.status(vhandle[2]).deleted()) << "vertex shouldn't be deleted";
  EXPECT_FALSE(mesh.status(vhandle[3]).deleted()) << "vertex shouldn't be deleted";

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

718 719 720 721 722
  mesh.delete_vertex(vhandle[0], false);
  mesh.delete_vertex(vhandle[1], false);
  mesh.delete_vertex(vhandle[2], false);
  mesh.delete_vertex(vhandle[3], false);

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

724 725 726 727 728 729 730 731 732 733 734 735 736 737
  EXPECT_TRUE(mesh.status(vhandle[0]).deleted()) << "vertex should be deleted";
  EXPECT_TRUE(mesh.status(vhandle[1]).deleted()) << "vertex should be deleted";
  EXPECT_TRUE(mesh.status(vhandle[2]).deleted()) << "vertex should be deleted";
  EXPECT_TRUE(mesh.status(vhandle[3]).deleted()) << "vertex should be deleted";

  // Delete all elements that are marked as deleted
  // from memory.
  mesh.garbage_collection();

  // write mesh
  bool ok = OpenMesh::IO::write_mesh(mesh, "deleted_output.off");

  EXPECT_TRUE(ok) << "Cannot write mesh to file 'deleted_output.off'";
}
738

739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

TEST_F(OpenMeshTutorials, storing_custom_properties) {
  MyMesh mesh;

  // generate a geometry
  generate_cube<MyMesh>(mesh);

  // define some custom properties
  OpenMesh::VPropHandleT<float>       vprop_float;
  OpenMesh::EPropHandleT<bool>        eprop_bool;
  OpenMesh::FPropHandleT<std::string> fprop_string;
  OpenMesh::HPropHandleT<MyData>      hprop_mydata;
  OpenMesh::MPropHandleT<MyMap>       mprop_map;

  // registrate them at the mesh object
  mesh.add_property(vprop_float,  "vprop_float");
  mesh.add_property(eprop_bool,   "eprop_bool");
  mesh.add_property(fprop_string, "fprop_string");
  mesh.add_property(hprop_mydata, "hprop_mydata");
  mesh.add_property(mprop_map,    "mprop_map");

  //fill the props
  fill_props(mesh, vprop_float);
  fill_props(mesh, eprop_bool);
  fill_props(mesh, fprop_string);
  fill_props(mesh, hprop_mydata);
  fill_props(mesh, mprop_map);

  EXPECT_TRUE(fill_props(mesh, vprop_float, true)) << "property not filled correctly";
  EXPECT_TRUE(fill_props(mesh, eprop_bool, true)) << "property not filled correctly";
  EXPECT_TRUE(fill_props(mesh, fprop_string, true)) << "property not filled correctly";
  EXPECT_TRUE(fill_props(mesh, hprop_mydata, true)) << "property not filled correctly";
  EXPECT_TRUE(fill_props(mesh, mprop_map, true)) << "property not filled correctly";

  //Set persistent flag
  mesh.property(vprop_float).set_persistent(true);
  EXPECT_TRUE(mesh.property(vprop_float).persistent()) << "property should be persistent";
  mesh.property(eprop_bool).set_persistent(true);
  EXPECT_TRUE(mesh.property(eprop_bool).persistent()) << "property should be persistent";
  mesh.property(fprop_string).set_persistent(true);
  EXPECT_TRUE(mesh.property(fprop_string).persistent()) << "property should be persistent";
  mesh.property(hprop_mydata).set_persistent(true);
  EXPECT_TRUE(mesh.property(hprop_mydata).persistent()) << "property should be persistent";
  mesh.mproperty(mprop_map).set_persistent(true);
  EXPECT_TRUE(mesh.mproperty(mprop_map).persistent()) << "property should be persistent";

  // write mesh
  bool ok = OpenMesh::IO::write_mesh( mesh, "persistence-check.om" );
  EXPECT_TRUE(ok) << "Cannot write mesh to file 'persistent-check.om'";

  // clear mesh
  mesh.clear();

  //Read back mesh
  ok = OpenMesh::IO::read_mesh( mesh, "persistence-check.om" );
  EXPECT_TRUE(ok) << "Cannot read mesh from file 'persistent-check.om'";

  // check props
  EXPECT_TRUE(fill_props(mesh, vprop_float, true)) << "property not filled correctly";
  EXPECT_TRUE(fill_props(mesh, eprop_bool, true)) << "property not filled correctly";
  EXPECT_TRUE(fill_props(mesh, fprop_string, true)) << "property not filled correctly";
  EXPECT_TRUE(fill_props(mesh, hprop_mydata, true)) << "property not filled correctly";
  EXPECT_TRUE(fill_props(mesh, mprop_map, true)) << "property not filled correctly";
}

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/*Testcase for code snippet from flipping edges in triangle meshes
 * */
TEST_F(OpenMeshTutorials, flipping_edges) {
  Mesh mesh;
  // Add some vertices
  Mesh::VertexHandle vhandle[4];
  vhandle[0] = mesh.add_vertex(MyMesh::Point(0, 0, 0));
  vhandle[1] = mesh.add_vertex(MyMesh::Point(0, 1, 0));
  vhandle[2] = mesh.add_vertex(MyMesh::Point(1, 1, 0));
  vhandle[3] = mesh.add_vertex(MyMesh::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);
  // Now the edge adjacent to the two faces connects
  // vertex vhandle[0] and vhandle[2].
  // Find this edge and then flip it
  for(Mesh::EdgeIter it = mesh.edges_begin(); it != mesh.edges_end(); ++it) {
    if(!mesh.is_boundary(*it)) {
      // Flip edge
831 832
      EXPECT_EQ(vhandle[2].idx(), mesh.to_vertex_handle(mesh.halfedge_handle(*it,0)).idx()) << "expected vertex handle 2!" ;
      EXPECT_EQ(vhandle[0].idx(), mesh.to_vertex_handle(mesh.halfedge_handle(*it,1)).idx()) << "expected vertex handle 0!" ;
833
      mesh.flip(*it);
834 835
      EXPECT_EQ(vhandle[1].idx(), mesh.to_vertex_handle(mesh.halfedge_handle(*it,0)).idx()) << "expected vertex handle 1 (did the flip work?)!" ;
      EXPECT_EQ(vhandle[3].idx(), mesh.to_vertex_handle(mesh.halfedge_handle(*it,1)).idx()) << "expected vertex handle 3 (did the flip work?)!" ;
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    }
  }
  // The edge now connects vertex vhandle[1] and vhandle[3].
}

/*Testcase for code snippet from collapsing edges in triangle meshes
 * */
TEST_F(OpenMeshTutorials, collapsing_edges) {
  PolyMesh mesh;
  mesh.request_vertex_status();
  mesh.request_edge_status();
  // Add some vertices as in the illustration above
  PolyMesh::VertexHandle vhandle[7];
  vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, 1, 0));
  vhandle[1] = mesh.add_vertex(MyMesh::Point(-1, 3, 0));
  vhandle[2] = mesh.add_vertex(MyMesh::Point(0, 0, 0));
  vhandle[3] = mesh.add_vertex(MyMesh::Point(0, 2, 0));
  vhandle[4] = mesh.add_vertex(MyMesh::Point(0, 4, 0));
  vhandle[5] = mesh.add_vertex(MyMesh::Point(1, 1, 0));
  vhandle[6] = mesh.add_vertex(MyMesh::Point(1, 3, 0));
  // Add three quad faces
  std::vector<PolyMesh::VertexHandle> face_vhandles;
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[3]);
  mesh.add_face(face_vhandles);
  face_vhandles.clear();
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[3]);
  face_vhandles.push_back(vhandle[5]);
  face_vhandles.push_back(vhandle[4]);
  mesh.add_face(face_vhandles);
  face_vhandles.clear();
  face_vhandles.push_back(vhandle[3]);
  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[6]);
  face_vhandles.push_back(vhandle[5]);
  mesh.add_face(face_vhandles);
  // Now find the edge between vertex vhandle[2]
  // and vhandle[3]
  for(PolyMesh::HalfedgeIter it = mesh.halfedges_begin(); it != mesh.halfedges_end(); ++it) {
    if( mesh.to_vertex_handle(*it) == vhandle[3] &&
        mesh.from_vertex_handle(*it) == vhandle[2])
    {
      // Collapse edge
      mesh.collapse(*it);
      break;
    }
  }
  // Our mesh now looks like in the illustration above after the collapsing.
}

889
}