per_vertex_point_to_plane_quadrics.cpp 5.1 KB

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