wire_mesh.cpp 6.8 KB

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