mesh_boolean_cork.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_NO_CORK
  9. #include "mesh_boolean_cork.h"
  10. #include "to_cork_mesh.h"
  11. #include "from_cork_mesh.h"
  12. template <
  13. typename DerivedVA,
  14. typename DerivedFA,
  15. typename DerivedVB,
  16. typename DerivedFB,
  17. typename DerivedVC,
  18. typename DerivedFC>
  19. IGL_INLINE void igl::boolean::mesh_boolean_cork(
  20. const Eigen::PlainObjectBase<DerivedVA > & VA,
  21. const Eigen::PlainObjectBase<DerivedFA > & FA,
  22. const Eigen::PlainObjectBase<DerivedVB > & VB,
  23. const Eigen::PlainObjectBase<DerivedFB > & FB,
  24. const MeshBooleanType & type,
  25. Eigen::PlainObjectBase<DerivedVC > & VC,
  26. Eigen::PlainObjectBase<DerivedFC > & FC)
  27. {
  28. CorkTriMesh A,B,C;
  29. // pointer to output so it's easy to redirect on degenerate cases
  30. CorkTriMesh *ret = &C;
  31. to_cork_mesh(VA,FA,A);
  32. to_cork_mesh(VB,FB,B);
  33. switch(type)
  34. {
  35. case MESH_BOOLEAN_TYPE_UNION:
  36. if(A.n_triangles == 0)
  37. {
  38. ret = &B;
  39. }else if(B.n_triangles == 0)
  40. {
  41. ret = &A;
  42. }else
  43. {
  44. computeUnion(A,B,ret);
  45. }
  46. break;
  47. case MESH_BOOLEAN_TYPE_INTERSECT:
  48. if(A.n_triangles == 0 || B.n_triangles == 0)
  49. {
  50. ret->n_triangles = 0;
  51. ret->n_vertices = 0;
  52. }else
  53. {
  54. computeIntersection(A,B,ret);
  55. }
  56. break;
  57. case MESH_BOOLEAN_TYPE_MINUS:
  58. if(A.n_triangles == 0)
  59. {
  60. ret->n_triangles = 0;
  61. ret->n_vertices = 0;
  62. }else if(B.n_triangles == 0)
  63. {
  64. ret = &A;
  65. }else
  66. {
  67. computeDifference(A,B,ret);
  68. }
  69. break;
  70. case MESH_BOOLEAN_TYPE_XOR:
  71. if(A.n_triangles == 0)
  72. {
  73. ret = &B;
  74. }else if(B.n_triangles == 0)
  75. {
  76. ret = &A;
  77. }else
  78. {
  79. computeSymmetricDifference(A,B,&C);
  80. }
  81. break;
  82. case MESH_BOOLEAN_TYPE_RESOLVE:
  83. resolveIntersections(A,B,&C);
  84. break;
  85. default:
  86. assert(false && "Unknown type");
  87. return;
  88. }
  89. from_cork_mesh(*ret,VC,FC);
  90. freeCorkTriMesh(&A);
  91. freeCorkTriMesh(&B);
  92. freeCorkTriMesh(&C);
  93. }
  94. #ifdef IGL_STATIC_LIBRARY
  95. // Explicit template specialization
  96. template void igl::boolean::mesh_boolean_cork<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  97. template void igl::boolean::mesh_boolean_cork<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, igl::boolean::MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  98. #endif
  99. #endif