per_vertex_point_to_plane_quadrics.cpp 5.6 KB

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