wire_mesh.cpp 6.6 KB

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