per_vertex_point_to_plane_quadrics.cpp 5.2 KB

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