wire_mesh.cpp 6.9 KB

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