mesh_boolean.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "mesh_boolean.h"
  9. #include "to_cork_mesh.h"
  10. #include "from_cork_mesh.h"
  11. template <
  12. typename DerivedVA,
  13. typename DerivedFA,
  14. typename DerivedVB,
  15. typename DerivedFB,
  16. typename DerivedVC,
  17. typename DerivedFC>
  18. IGL_INLINE void igl::copyleft::cork::mesh_boolean(
  19. const Eigen::PlainObjectBase<DerivedVA > & VA,
  20. const Eigen::PlainObjectBase<DerivedFA > & FA,
  21. const Eigen::PlainObjectBase<DerivedVB > & VB,
  22. const Eigen::PlainObjectBase<DerivedFB > & FB,
  23. const MeshBooleanType & type,
  24. Eigen::PlainObjectBase<DerivedVC > & VC,
  25. Eigen::PlainObjectBase<DerivedFC > & FC)
  26. {
  27. CorkTriMesh A,B,C;
  28. // pointer to output so it's easy to redirect on degenerate cases
  29. CorkTriMesh *ret = &C;
  30. to_cork_mesh(VA,FA,A);
  31. to_cork_mesh(VB,FB,B);
  32. switch(type)
  33. {
  34. case MESH_BOOLEAN_TYPE_UNION:
  35. if(A.n_triangles == 0)
  36. {
  37. ret = &B;
  38. }else if(B.n_triangles == 0)
  39. {
  40. ret = &A;
  41. }else
  42. {
  43. computeUnion(A,B,ret);
  44. }
  45. break;
  46. case MESH_BOOLEAN_TYPE_INTERSECT:
  47. if(A.n_triangles == 0 || B.n_triangles == 0)
  48. {
  49. ret->n_triangles = 0;
  50. ret->n_vertices = 0;
  51. }else
  52. {
  53. computeIntersection(A,B,ret);
  54. }
  55. break;
  56. case MESH_BOOLEAN_TYPE_MINUS:
  57. if(A.n_triangles == 0)
  58. {
  59. ret->n_triangles = 0;
  60. ret->n_vertices = 0;
  61. }else if(B.n_triangles == 0)
  62. {
  63. ret = &A;
  64. }else
  65. {
  66. computeDifference(A,B,ret);
  67. }
  68. break;
  69. case MESH_BOOLEAN_TYPE_XOR:
  70. if(A.n_triangles == 0)
  71. {
  72. ret = &B;
  73. }else if(B.n_triangles == 0)
  74. {
  75. ret = &A;
  76. }else
  77. {
  78. computeSymmetricDifference(A,B,&C);
  79. }
  80. break;
  81. case MESH_BOOLEAN_TYPE_RESOLVE:
  82. resolveIntersections(A,B,&C);
  83. break;
  84. default:
  85. assert(false && "Unknown type");
  86. return;
  87. }
  88. from_cork_mesh(*ret,VC,FC);
  89. freeCorkTriMesh(&A);
  90. freeCorkTriMesh(&B);
  91. freeCorkTriMesh(&C);
  92. }
  93. #ifdef IGL_STATIC_LIBRARY
  94. // Explicit template specialization
  95. template void igl::copyleft::cork::mesh_boolean<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> >&);
  96. template void igl::copyleft::cork::mesh_boolean<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::MeshBooleanType const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  97. #endif