CSGTree.h 5.7 KB

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