CSGTree.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_COPYLEFT_BOOLEAN_CSG_TREE_H
  9. #define IGL_COPYLEFT_BOOLEAN_CSG_TREE_H
  10. #include "string_to_mesh_boolean_type.h"
  11. #include "MeshBooleanType.h"
  12. #include "mesh_boolean.h"
  13. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  14. #include <CGAL/number_utils.h>
  15. namespace igl
  16. {
  17. namespace copyleft
  18. {
  19. namespace boolean
  20. {
  21. // Class for defining and computing a constructive solid geometry result
  22. // out of a tree of boolean operations on "solid" triangle meshes.
  23. //
  24. //template <typename DerivedF>
  25. class CSGTree
  26. {
  27. private:
  28. typedef CGAL::Epeck::FT ExactScalar;
  29. typedef Eigen::Matrix<ExactScalar,Eigen::Dynamic,3> MatrixX3E;
  30. //typedef Eigen::PlainObjectBase<DerivedF> POBF;
  31. typedef Eigen::MatrixXi POBF;
  32. typedef POBF::Index FIndex;
  33. typedef Eigen::Matrix<FIndex,Eigen::Dynamic,1> VectorJ;
  34. // Resulting mesh
  35. MatrixX3E m_V;
  36. POBF m_F;
  37. VectorJ m_J;
  38. // Number of birth faces in A + those in B. I.e. sum of original "leaf"
  39. // faces involved in result.
  40. size_t m_number_of_birth_faces;
  41. public:
  42. CSGTree()
  43. {
  44. }
  45. //typedef Eigen::MatrixXd MatrixX3E;
  46. //typedef Eigen::MatrixXi POBF;
  47. // http://stackoverflow.com/a/3279550/148668
  48. CSGTree(const CSGTree & other)
  49. :
  50. // copy things
  51. m_V(other.m_V),
  52. // This is an issue if m_F is templated
  53. // https://forum.kde.org/viewtopic.php?f=74&t=128414
  54. m_F(other.m_F),
  55. m_J(other.m_J),
  56. m_number_of_birth_faces(other.m_number_of_birth_faces)
  57. {
  58. }
  59. // copy-swap idiom
  60. friend void swap(CSGTree& first, CSGTree& second)
  61. {
  62. using std::swap;
  63. // swap things
  64. swap(first.m_V,second.m_V);
  65. // This is an issue if m_F is templated, similar to
  66. // https://forum.kde.org/viewtopic.php?f=74&t=128414
  67. swap(first.m_F,second.m_F);
  68. swap(first.m_J,second.m_J);
  69. swap(first.m_number_of_birth_faces,second.m_number_of_birth_faces);
  70. }
  71. // Pass-by-value (aka copy)
  72. CSGTree& operator=(CSGTree other)
  73. {
  74. swap(*this,other);
  75. return *this;
  76. }
  77. CSGTree(CSGTree&& other):
  78. // initialize via default constructor
  79. CSGTree()
  80. {
  81. swap(*this,other);
  82. }
  83. // Construct and compute a boolean operation on existing CSGTree nodes.
  84. //
  85. // Inputs:
  86. // A Solid result of previous CSG operation (or identity, see below)
  87. // B Solid result of previous CSG operation (or identity, see below)
  88. // type type of mesh boolean to compute
  89. CSGTree(
  90. const CSGTree & A,
  91. const CSGTree & B,
  92. const MeshBooleanType & type)
  93. {
  94. // conduct boolean operation
  95. mesh_boolean(A.V(),A.F(),B.V(),B.F(),type,m_V,m_F,m_J);
  96. // reindex m_J
  97. std::for_each(m_J.data(),m_J.data()+m_J.size(),
  98. [&](typename VectorJ::Scalar & j)
  99. {
  100. if(j < A.F().rows())
  101. {
  102. j = A.J()(j);
  103. }else
  104. {
  105. assert(j<(A.F().rows()+B.F().rows()));
  106. j = A.number_of_birth_faces()+(B.J()(j-A.F().rows()));
  107. }
  108. });
  109. m_number_of_birth_faces =
  110. A.number_of_birth_faces() + B.number_of_birth_faces();
  111. }
  112. // Overload using string for type
  113. CSGTree(
  114. const CSGTree & A,
  115. const CSGTree & B,
  116. const std::string & s):
  117. CSGTree(A,B,string_to_mesh_boolean_type(s))
  118. {
  119. // do nothing (all done in constructor).
  120. }
  121. // "Leaf" node with identity operation on assumed "solid" mesh (V,F)
  122. //
  123. // Inputs:
  124. // V #V by 3 list of mesh vertices (in any precision, will be
  125. // converted to exact)
  126. // F #F by 3 list of mesh face indices into V
  127. template <typename DerivedV>
  128. CSGTree(const Eigen::PlainObjectBase<DerivedV> & V, const POBF & F)//:
  129. // Possible Eigen bug:
  130. // https://forum.kde.org/viewtopic.php?f=74&t=128414
  131. //m_V(V.template cast<ExactScalar>()),m_F(F)
  132. {
  133. m_V = V.template cast<ExactScalar>();
  134. m_F = F;
  135. // number of faces
  136. m_number_of_birth_faces = m_F.rows();
  137. // identity birth index
  138. m_J = VectorJ::LinSpaced(
  139. m_number_of_birth_faces,0,m_number_of_birth_faces-1);
  140. }
  141. // Returns reference to resulting mesh vertices m_V in exact scalar
  142. // representation
  143. const MatrixX3E & V() const
  144. {
  145. return m_V;
  146. }
  147. // Returns mesh vertices in the desired output type, casting when
  148. // appropriate to floating precision.
  149. template <typename DerivedV>
  150. Eigen::PlainObjectBase<DerivedV> cast_V() const
  151. {
  152. Eigen::PlainObjectBase<DerivedV> dV;
  153. dV.resize(m_V.rows(),m_V.cols());
  154. for(int i = 0;i<m_V.rows();i++)
  155. {
  156. for(int j = 0;j<m_V.cols();j++)
  157. {
  158. dV(i,j) = CGAL::to_double(m_V(i,j));
  159. }
  160. }
  161. return dV;
  162. }
  163. // Returns reference to resulting mesh faces m_F
  164. const POBF & F() const
  165. {
  166. return m_F;
  167. }
  168. // Returns reference to "birth parents" indices into [F1;F2;...;Fn]
  169. // where F1, ... , Fn are the face lists of the leaf ("original") input
  170. // meshes.
  171. const VectorJ & J() const
  172. {
  173. return m_J;
  174. }
  175. // The number of leaf faces = #F1 + #F2 + ... + #Fn
  176. const size_t & number_of_birth_faces() const
  177. {
  178. return m_number_of_birth_faces;
  179. }
  180. };
  181. }
  182. }
  183. }
  184. #endif