wire_mesh.cpp 6.6 KB

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