BinaryWindingNumberOperations.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_COPYLEFT_CGAL_BINARY_WINDING_NUMBER_OPERATIONS_H
  10. #define IGL_COPYLEFT_CGAL_BINARY_WINDING_NUMBER_OPERATIONS_H
  11. #include <stdexcept>
  12. #include "../../igl_inline.h"
  13. #include "../../MeshBooleanType.h"
  14. #include <Eigen/Core>
  15. // TODO: This is not written according to libigl style. These should be
  16. // function handles.
  17. //
  18. // Why is this templated on DerivedW
  19. namespace igl
  20. {
  21. namespace copyleft
  22. {
  23. namespace cgal
  24. {
  25. template <igl::MeshBooleanType Op>
  26. class BinaryWindingNumberOperations {
  27. public:
  28. template<typename DerivedW>
  29. typename DerivedW::Scalar operator()(
  30. const Eigen::PlainObjectBase<DerivedW>& /*win_nums*/) const {
  31. throw (std::runtime_error("not implemented!"));
  32. }
  33. };
  34. template <>
  35. class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_UNION> {
  36. public:
  37. template<typename DerivedW>
  38. typename DerivedW::Scalar operator()(
  39. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  40. return win_nums(0) > 0 || win_nums(1) > 0;
  41. }
  42. };
  43. template <>
  44. class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_INTERSECT> {
  45. public:
  46. template<typename DerivedW>
  47. typename DerivedW::Scalar operator()(
  48. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  49. return win_nums(0) > 0 && win_nums(1) > 0;
  50. }
  51. };
  52. template <>
  53. class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_MINUS> {
  54. public:
  55. template<typename DerivedW>
  56. typename DerivedW::Scalar operator()(
  57. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  58. return win_nums(0) > 0 && win_nums(1) <= 0;
  59. }
  60. };
  61. template <>
  62. class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_XOR> {
  63. public:
  64. template<typename DerivedW>
  65. typename DerivedW::Scalar operator()(
  66. const Eigen::PlainObjectBase<DerivedW>& win_nums) const {
  67. return (win_nums(0) > 0 && win_nums(1) <= 0) ||
  68. (win_nums(0) <= 0 && win_nums(1) > 0);
  69. }
  70. };
  71. template <>
  72. class BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_RESOLVE> {
  73. public:
  74. template<typename DerivedW>
  75. typename DerivedW::Scalar operator()(
  76. const Eigen::PlainObjectBase<DerivedW>& /*win_nums*/) const {
  77. return true;
  78. }
  79. };
  80. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_UNION> BinaryUnion;
  81. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_INTERSECT> BinaryIntersect;
  82. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_MINUS> BinaryMinus;
  83. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_XOR> BinaryXor;
  84. typedef BinaryWindingNumberOperations<MESH_BOOLEAN_TYPE_RESOLVE> BinaryResolve;
  85. enum KeeperType {
  86. KEEP_INSIDE,
  87. KEEP_ALL
  88. };
  89. template<KeeperType T>
  90. class WindingNumberFilter {
  91. public:
  92. template<typename DerivedW>
  93. short operator()(
  94. const Eigen::PlainObjectBase<DerivedW>& /*win_nums*/) const {
  95. throw std::runtime_error("Not implemented");
  96. }
  97. };
  98. template<>
  99. class WindingNumberFilter<KEEP_INSIDE> {
  100. public:
  101. template<typename T>
  102. short operator()(T out_w, T in_w) const {
  103. if (in_w > 0 && out_w <= 0) return 1;
  104. else if (in_w <= 0 && out_w > 0) return -1;
  105. else return 0;
  106. }
  107. };
  108. template<>
  109. class WindingNumberFilter<KEEP_ALL> {
  110. public:
  111. template<typename T>
  112. short operator()(T /*out_w*/, T /*in_w*/) const {
  113. return 1;
  114. }
  115. };
  116. typedef WindingNumberFilter<KEEP_INSIDE> KeepInside;
  117. typedef WindingNumberFilter<KEEP_ALL> KeepAll;
  118. }
  119. }
  120. }
  121. #endif