wire_mesh.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "wire_mesh.h"
  2. #include "../../list_to_matrix.h"
  3. #include "../../slice.h"
  4. #include "convex_hull.h"
  5. #include "mesh_boolean.h"
  6. #include <Eigen/Geometry>
  7. #include <vector>
  8. template <
  9. typename DerivedWV,
  10. typename DerivedWE,
  11. typename DerivedV,
  12. typename DerivedF,
  13. typename DerivedJ>
  14. IGL_INLINE void igl::copyleft::cgal::wire_mesh(
  15. const Eigen::MatrixBase<DerivedWV> & WV,
  16. const Eigen::MatrixBase<DerivedWE> & WE,
  17. const double th,
  18. const int poly_size,
  19. Eigen::PlainObjectBase<DerivedV> & V,
  20. Eigen::PlainObjectBase<DerivedF> & F,
  21. Eigen::PlainObjectBase<DerivedJ> & J)
  22. {
  23. typedef typename DerivedWV::Scalar Scalar;
  24. // Canonical polygon to place at each endpoint
  25. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3> MatrixX3S;
  26. MatrixX3S PV(poly_size,3);
  27. for(int p =0;p<PV.rows();p++)
  28. {
  29. const Scalar phi = (Scalar(p)/Scalar(PV.rows()))*2.*M_PI;
  30. PV(p,0) = 0.5*cos(phi);
  31. PV(p,1) = 0.5*sin(phi);
  32. PV(p,2) = 0;
  33. }
  34. V.resize(WV.rows() + PV.rows() * 2 * WE.rows(),3);
  35. V.topLeftCorner(WV.rows(),3) = WV;
  36. // Signed adjacency list
  37. std::vector<std::vector<std::pair<int,int> > > A(WV.rows());
  38. // Inputs:
  39. // e index of edge
  40. // c index of endpoint [0,1]
  41. // p index of polygon vertex
  42. // Returns index of corresponding vertex in V
  43. const auto index =
  44. [&PV,&WV](const int e, const int c, const int p)->int
  45. {
  46. return WV.rows() + e*2*PV.rows() + PV.rows()*c + p;
  47. };
  48. const auto unindex =
  49. [&PV,&WV](int v, int & e, int & c, int & p)
  50. {
  51. assert(v>=WV.rows());
  52. v = v-WV.rows();
  53. e = v/(2*PV.rows());
  54. v = v-e*(2*PV.rows());
  55. c = v/(PV.rows());
  56. v = v-c*(PV.rows());
  57. p = v;
  58. };
  59. // loop over all edges
  60. for(int e = 0;e<WE.rows();e++)
  61. {
  62. // Fill in adjacency list as we go
  63. A[WE(e,0)].emplace_back(e,0);
  64. A[WE(e,1)].emplace_back(e,1);
  65. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  66. const RowVector3S ev = WV.row(WE(e,1))-WV.row(WE(e,0));
  67. const RowVector3S uv = ev.normalized();
  68. Eigen::Quaternion<Scalar> q;
  69. q = q.FromTwoVectors(RowVector3S(0,0,1),uv);
  70. // loop over polygon vertices
  71. for(int p = 0;p<PV.rows();p++)
  72. {
  73. RowVector3S qp = q*(PV.row(p)*th);
  74. // loop over endpoints
  75. for(int c = 0;c<2;c++)
  76. {
  77. // Move to endpoint, offset by factor of thickness
  78. V.row(index(e,c,p)) =
  79. qp+WV.row(WE(e,c)) + 1.*th*Scalar(1-2*c)*uv;
  80. }
  81. }
  82. }
  83. std::vector<std::vector<typename DerivedF::Index> > vF;
  84. std::vector<int> vJ;
  85. const auto append_hull =
  86. [&V,&vF,&vJ,&unindex,&WV](const Eigen::VectorXi & I, const int j)
  87. {
  88. MatrixX3S Vv;
  89. igl::slice(V,I,1,Vv);
  90. Eigen::MatrixXi Fv;
  91. convex_hull(Vv,Fv);
  92. for(int f = 0;f<Fv.rows();f++)
  93. {
  94. const Eigen::Array<int,1,3> face(I(Fv(f,0)), I(Fv(f,1)), I(Fv(f,2)));
  95. //const bool on_vertex = (face<WV.rows()).any();
  96. //if(!on_vertex)
  97. //{
  98. // // This correctly prunes fcaes on the "caps" of convex hulls around
  99. // // edges, but for convex hulls around vertices this will only work if
  100. // // the incoming edges are not overlapping.
  101. // //
  102. // // Q: For convex hulls around vertices, is the correct thing to do:
  103. // // check if all corners of face lie *on or _outside_* of plane of "cap"?
  104. // //
  105. // // H: Maybe, but if there's an intersection then the boundary of the
  106. // // incoming convex hulls around edges is still not going to match up
  107. // // with the boundary on the convex hull around the vertices.
  108. // //
  109. // // Might have to bite the bullet and always call self-union.
  110. // bool all_same = true;
  111. // int e0,c0,p0;
  112. // unindex(face(0),e0,c0,p0);
  113. // for(int i = 1;i<3;i++)
  114. // {
  115. // int ei,ci,pi;
  116. // unindex(face(i),ei,ci,pi);
  117. // all_same = all_same && (e0==ei && c0==ci);
  118. // }
  119. // if(all_same)
  120. // {
  121. // // don't add this face
  122. // continue;
  123. // }
  124. //}
  125. vF.push_back( { face(0),face(1),face(2)});
  126. vJ.push_back(j);
  127. }
  128. };
  129. // loop over each vertex
  130. for(int v = 0;v<WV.rows();v++)
  131. {
  132. // Gather together this vertex and the polygon vertices of all incident
  133. // edges
  134. Eigen::VectorXi I(1+A[v].size()*PV.rows());
  135. // This vertex
  136. I(0) = v;
  137. for(int n = 0;n<A[v].size();n++)
  138. {
  139. for(int p = 0;p<PV.rows();p++)
  140. {
  141. const int e = A[v][n].first;
  142. const int c = A[v][n].second;
  143. I(1+n*PV.rows()+p) = index(e,c,p);
  144. }
  145. }
  146. append_hull(I,v);
  147. }
  148. // loop over each edge
  149. for(int e = 0;e<WE.rows();e++)
  150. {
  151. // Gether together polygon vertices of both endpoints
  152. Eigen::VectorXi I(PV.rows()*2);
  153. for(int c = 0;c<2;c++)
  154. {
  155. for(int p = 0;p<PV.rows();p++)
  156. {
  157. I(c*PV.rows()+p) = index(e,c,p);
  158. }
  159. }
  160. append_hull(I,WV.rows()+e);
  161. }
  162. list_to_matrix(vF,F);
  163. // Self-union to clean up
  164. igl::copyleft::cgal::mesh_boolean(
  165. Eigen::MatrixXd(V),Eigen::MatrixXi(F),Eigen::MatrixXd(),Eigen::MatrixXi(),
  166. "union",
  167. V,F,J);
  168. for(int j=0;j<J.size();j++) J(j) = vJ[J(j)];
  169. }
  170. #ifdef IGL_STATIC_LIBRARY
  171. // Explicit template instantiation
  172. template void igl::copyleft::cgal::wire_mesh<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<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, double, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  173. #endif