BinaryWindingNumberOperations.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Qingnan Zhou <qnzhou@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. //
  9. #ifndef IGL_BINARY_WINDING_NUMBER_OPERATIONS
  10. #define IGL_BINARY_WINDING_NUMBER_OPERATIONS
  11. #include <stdexcept>
  12. #include <igl/igl_inline.h>
  13. #include "MeshBooleanType.h"
  14. #include <Eigen/Core>
  15. namespace igl
  16. {
  17. namespace boolean
  18. {
  19. template <igl::boolean::MeshBooleanType Op>
  20. class BinaryWindingNumberOperations {
  21. public:
  22. template<typename DerivedW>
  23. typename DerivedW::Scalar operator()(
  24. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  25. throw (std::runtime_error("not implemented!"));
  26. }
  27. };
  28. template <>
  29. class BinaryWindingNumberOperations<igl::boolean::MESH_BOOLEAN_TYPE_UNION> {
  30. public:
  31. template<typename DerivedW>
  32. typename DerivedW::Scalar operator()(
  33. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  34. return win_nums(0) > 0 || win_nums(1) > 0;
  35. }
  36. };
  37. template <>
  38. class BinaryWindingNumberOperations<igl::boolean::MESH_BOOLEAN_TYPE_INTERSECT> {
  39. public:
  40. template<typename DerivedW>
  41. typename DerivedW::Scalar operator()(
  42. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  43. return win_nums(0) > 0 && win_nums(1) > 0;
  44. }
  45. };
  46. template <>
  47. class BinaryWindingNumberOperations<igl::boolean::MESH_BOOLEAN_TYPE_MINUS> {
  48. public:
  49. template<typename DerivedW>
  50. typename DerivedW::Scalar operator()(
  51. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  52. return win_nums(0) > 0 && win_nums(1) <= 0;
  53. }
  54. };
  55. template <>
  56. class BinaryWindingNumberOperations<igl::boolean::MESH_BOOLEAN_TYPE_XOR> {
  57. public:
  58. template<typename DerivedW>
  59. typename DerivedW::Scalar operator()(
  60. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  61. return (win_nums(0) > 0 && win_nums(1) <= 0) ||
  62. (win_nums(0) <= 0 && win_nums(1) > 0);
  63. }
  64. };
  65. template <>
  66. class BinaryWindingNumberOperations<igl::boolean::MESH_BOOLEAN_TYPE_RESOLVE> {
  67. public:
  68. template<typename DerivedW>
  69. typename DerivedW::Scalar operator()(
  70. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  71. return true;
  72. }
  73. };
  74. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_UNION> BinaryUnion;
  75. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_INTERSECT> BinaryIntersect;
  76. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_MINUS> BinaryMinus;
  77. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_XOR> BinaryXor;
  78. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_RESOLVE> BinaryResolve;
  79. enum KeeperType {
  80. KEEP_INSIDE,
  81. KEEP_ALL
  82. };
  83. template<KeeperType T>
  84. class WindingNumberFilter {
  85. public:
  86. template<typename DerivedW>
  87. short operator()(
  88. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  89. throw std::runtime_error("Not implemented");
  90. }
  91. };
  92. template<>
  93. class WindingNumberFilter<KEEP_INSIDE> {
  94. public:
  95. template<typename T>
  96. short operator()(T out_w, T in_w) const {
  97. if (in_w > 0 && out_w <= 0) return 1;
  98. else if (in_w <= 0 && out_w > 0) return -1;
  99. else return 0;
  100. }
  101. };
  102. template<>
  103. class WindingNumberFilter<KEEP_ALL> {
  104. public:
  105. template<typename T>
  106. short operator()(T out_w, T in_w) const {
  107. return 1;
  108. }
  109. };
  110. typedef WindingNumberFilter<KEEP_INSIDE> KeepInside;
  111. typedef WindingNumberFilter<KEEP_ALL> KeepAll;
  112. }
  113. }
  114. #endif