per_vertex_point_to_plane_quadrics.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "per_vertex_point_to_plane_quadrics.h"
  2. #include "quadric_binary_plus_operator.h"
  3. #include <Eigen/QR>
  4. #include <cassert>
  5. IGL_INLINE void igl::per_vertex_point_to_plane_quadrics(
  6. const Eigen::MatrixXd & V,
  7. const Eigen::MatrixXi & F,
  8. const Eigen::MatrixXi & EMAP,
  9. const Eigen::MatrixXi & EF,
  10. const Eigen::MatrixXi & EI,
  11. std::vector<
  12. std::tuple<Eigen::MatrixXd,Eigen::RowVectorXd,double> > & quadrics)
  13. {
  14. using namespace std;
  15. typedef std::tuple<Eigen::MatrixXd,Eigen::RowVectorXd,double> Quadric;
  16. const int dim = V.cols();
  17. //// Quadrics per face
  18. //std::vector<Quadric> face_quadrics(F.rows());
  19. // Initialize each vertex quadric to zeros
  20. quadrics.resize(
  21. V.rows(),{Eigen::MatrixXd::Zero(dim,dim),Eigen::RowVectorXd::Zero(dim),0});
  22. Eigen::MatrixXd I = Eigen::MatrixXd::Identity(dim,dim);
  23. // Rather initial with zeros, initial with a small amount of energy pull
  24. // toward original vertex position
  25. const double w = 1e-10;
  26. for(int v = 0;v<V.rows();v++)
  27. {
  28. std::get<0>(quadrics[v]) = w*I;
  29. Eigen::RowVectorXd Vv = V.row(v);
  30. std::get<1>(quadrics[v]) = w*-Vv;
  31. std::get<2>(quadrics[v]) = w*Vv.dot(Vv);
  32. }
  33. // Generic nD qslim from "Simplifying Surfaces with Color and Texture
  34. // using Quadric Error Metric" (follow up to original QSlim)
  35. for(int f = 0;f<F.rows();f++)
  36. {
  37. int infinite_corner = -1;
  38. for(int c = 0;c<3;c++)
  39. {
  40. if(isinf(V(F(f,c),0)) || isinf(V(F(f,c),1)) || isinf(V(F(f,c),2)))
  41. {
  42. assert(infinite_corner == -1 && "Should only be one infinite corner");
  43. infinite_corner = c;
  44. }
  45. }
  46. // Inputs:
  47. // p 1 by n row point on the subspace
  48. // S m by n matrix where rows coorespond to orthonormal spanning
  49. // vectors of the subspace to which we're measuring distance (usually
  50. // a plane, m=2)
  51. // weight scalar weight
  52. // Returns quadric triple {A,b,c} so that A-2*b+c measures the quadric
  53. const auto subspace_quadric = [&I](
  54. const Eigen::RowVectorXd & p,
  55. const Eigen::MatrixXd & S,
  56. const double weight)->Quadric
  57. {
  58. // Dimension of subspace
  59. const int m = S.rows();
  60. // Weight face's quadric (v'*A*v + 2*b'*v + c) by area
  61. // e1 and e2 should be perpendicular
  62. Eigen::MatrixXd A = I;
  63. Eigen::RowVectorXd b = -p;
  64. double c = p.dot(p);
  65. for(int i = 0;i<m;i++)
  66. {
  67. Eigen::RowVectorXd ei = S.row(i);
  68. for(int j = 0;j<i;j++) assert(std::abs(S.row(j).dot(ei)) < 1e-10);
  69. A += -ei.transpose()*ei;
  70. b += p.dot(ei)*ei;
  71. c += -pow(p.dot(ei),2);
  72. }
  73. return { weight*A, weight*b, weight*c };
  74. };
  75. if(infinite_corner == -1)
  76. {
  77. // Finite (non-boundary) face
  78. Eigen::RowVectorXd p = V.row(F(f,0));
  79. Eigen::RowVectorXd q = V.row(F(f,1));
  80. Eigen::RowVectorXd r = V.row(F(f,2));
  81. Eigen::RowVectorXd pq = q-p;
  82. Eigen::RowVectorXd pr = r-p;
  83. // Gram Determinant = squared area of parallelogram
  84. double area = sqrt(pq.squaredNorm()*pr.squaredNorm()-pow(pr.dot(pq),2));
  85. Eigen::RowVectorXd e1 = pq.normalized();
  86. Eigen::RowVectorXd e2 = (pr-e1.dot(pr)*e1).normalized();
  87. Eigen::MatrixXd S(2,V.cols());
  88. S<<e1,e2;
  89. Quadric face_quadric = subspace_quadric(p,S,area);
  90. // Throw at each corner
  91. for(int c = 0;c<3;c++)
  92. {
  93. quadrics[F(f,c)] = quadrics[F(f,c)] + face_quadric;
  94. }
  95. }else
  96. {
  97. // cth corner is infinite --> edge opposite cth corner is boundary
  98. // Boundary edge vector
  99. const Eigen::RowVectorXd p = V.row(F(f,(infinite_corner+1)%3));
  100. Eigen::RowVectorXd ev = V.row(F(f,(infinite_corner+2)%3)) - p;
  101. const double length = ev.norm();
  102. ev /= length;
  103. // Face neighbor across boundary edge
  104. int e = EMAP(f+F.rows()*infinite_corner);
  105. int opp = EF(e,0) == f ? 1 : 0;
  106. int n = EF(e,opp);
  107. int nc = EI(e,opp);
  108. assert(
  109. ((F(f,(infinite_corner+1)%3) == F(n,(nc+1)%3) &&
  110. F(f,(infinite_corner+2)%3) == F(n,(nc+2)%3)) ||
  111. (F(f,(infinite_corner+1)%3) == F(n,(nc+2)%3)
  112. && F(f,(infinite_corner+2)%3) == F(n,(nc+1)%3))) &&
  113. "Edge flaps not agreeing on shared edge");
  114. // Edge vector on opposite face
  115. const Eigen::RowVectorXd eu = V.row(F(n,nc)) - p;
  116. assert(!isinf(eu(0)));
  117. // Matrix with vectors spanning plane as columns
  118. Eigen::MatrixXd A(ev.size(),2);
  119. A<<ev.transpose(),eu.transpose();
  120. // Use QR decomposition to find basis for orthogonal space
  121. Eigen::HouseholderQR<Eigen::MatrixXd> qr(A);
  122. const Eigen::MatrixXd Q = qr.householderQ();
  123. const Eigen::MatrixXd N =
  124. Q.topRightCorner(ev.size(),ev.size()-2).transpose();
  125. assert(N.cols() == ev.size());
  126. assert(N.rows() == ev.size()-2);
  127. Eigen::MatrixXd S(N.rows()+1,ev.size());
  128. S<<ev,N;
  129. Quadric boundary_edge_quadric = subspace_quadric(p,S,length);
  130. for(int c = 0;c<3;c++)
  131. {
  132. if(c != infinite_corner)
  133. {
  134. quadrics[F(f,c)] = quadrics[F(f,c)] + boundary_edge_quadric;
  135. }
  136. }
  137. }
  138. }
  139. }