mvc.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "mvc.h"
  9. #include <vector>
  10. #include <cassert>
  11. #include <iostream>
  12. // Broken Implementation
  13. IGL_INLINE void igl::mvc(const Eigen::MatrixXd &V, const Eigen::MatrixXd &C, Eigen::MatrixXd &W)
  14. {
  15. // at least three control points
  16. assert(C.rows()>2);
  17. // dimension of points
  18. assert(C.cols() == 3 || C.cols() == 2);
  19. assert(V.cols() == 3 || V.cols() == 2);
  20. // number of polygon points
  21. int num = C.rows();
  22. Eigen::MatrixXd V1,C1;
  23. int i_prev, i_next;
  24. // check if either are 3D but really all z's are 0
  25. bool V_flat = (V.cols() == 3) && (std::sqrt( (V.col(3)).dot(V.col(3)) ) < 1e-10);
  26. bool C_flat = (C.cols() == 3) && (std::sqrt( (C.col(3)).dot(C.col(3)) ) < 1e-10);
  27. // if both are essentially 2D then ignore z-coords
  28. if((C.cols() == 2 || C_flat) && (V.cols() == 2 || V_flat))
  29. {
  30. // ignore z coordinate
  31. V1 = V.block(0,0,V.rows(),2);
  32. C1 = C.block(0,0,C.rows(),2);
  33. }
  34. else
  35. {
  36. // give dummy z coordinate to either mesh or poly
  37. if(V.rows() == 2)
  38. {
  39. V1 = Eigen::MatrixXd(V.rows(),3);
  40. V1.block(0,0,V.rows(),2) = V;
  41. }
  42. else
  43. V1 = V;
  44. if(C.rows() == 2)
  45. {
  46. C1 = Eigen::MatrixXd(C.rows(),3);
  47. C1.block(0,0,C.rows(),2) = C;
  48. }
  49. else
  50. C1 = C;
  51. // check that C is planar
  52. // average normal around poly corners
  53. Eigen::Vector3d n = Eigen::Vector3d::Zero();
  54. // take centroid as point on plane
  55. Eigen::Vector3d p = Eigen::Vector3d::Zero();
  56. for (int i = 0; i<num; ++i)
  57. {
  58. i_prev = (i>0)?(i-1):(num-1);
  59. i_next = (i<num-1)?(i+1):0;
  60. Eigen::Vector3d vnext = (C1.row(i_next) - C1.row(i)).transpose();
  61. Eigen::Vector3d vprev = (C1.row(i_prev) - C1.row(i)).transpose();
  62. n += vnext.cross(vprev);
  63. p += C1.row(i);
  64. }
  65. p/=num;
  66. n/=num;
  67. // normalize n
  68. n /= std::sqrt(n.dot(n));
  69. // check that poly is really coplanar
  70. #ifndef NDEBUG
  71. for (int i = 0; i<num; ++i)
  72. {
  73. double dist_to_plane_C = std::abs((C1.row(i)-p.transpose()).dot(n));
  74. assert(dist_to_plane_C<1e-10);
  75. }
  76. #endif
  77. // check that poly is really coplanar
  78. for (int i = 0; i<V1.rows(); ++i)
  79. {
  80. double dist_to_plane_V = std::abs((V1.row(i)-p.transpose()).dot(n));
  81. if(dist_to_plane_V>1e-10)
  82. std::cerr<<"Distance from V to plane of C is large..."<<std::endl;
  83. }
  84. // change of basis
  85. Eigen::Vector3d b1 = C1.row(1)-C1.row(0);
  86. Eigen::Vector3d b2 = n.cross(b1);
  87. // normalize basis rows
  88. b1 /= std::sqrt(b1.dot(b1));
  89. b2 /= std::sqrt(b2.dot(b2));
  90. n /= std::sqrt(n.dot(n));
  91. //transpose of the basis matrix in the m-file
  92. Eigen::Matrix3d basis = Eigen::Matrix3d::Zero();
  93. basis.col(0) = b1;
  94. basis.col(1) = b2;
  95. basis.col(2) = n;
  96. // change basis of rows vectors by right multiplying with inverse of matrix
  97. // with basis vectors as rows
  98. Eigen::ColPivHouseholderQR<Eigen::Matrix3d> solver = basis.colPivHouseholderQr();
  99. // Throw away coordinates in normal direction
  100. V1 = solver.solve(V1.transpose()).transpose().block(0,0,V1.rows(),2);
  101. C1 = solver.solve(C1.transpose()).transpose().block(0,0,C1.rows(),2);
  102. }
  103. // vectors from V to every C, where CmV(i,j,:) is the vector from domain
  104. // vertex j to handle i
  105. double EPS = 1e-10;
  106. Eigen::MatrixXd WW = Eigen::MatrixXd(C1.rows(), V1.rows());
  107. Eigen::MatrixXd dist_C_V (C1.rows(), V1.rows());
  108. std::vector< std::pair<int,int> > on_corner(0);
  109. std::vector< std::pair<int,int> > on_segment(0);
  110. for (int i = 0; i<C1.rows(); ++i)
  111. {
  112. i_prev = (i>0)?(i-1):(num-1);
  113. i_next = (i<num-1)?(i+1):0;
  114. // distance from each corner in C to the next corner so that edge_length(i)
  115. // is the distance from C(i,:) to C(i+1,:) defined cyclically
  116. double edge_length = std::sqrt((C1.row(i) - C1.row(i_next)).dot(C1.row(i) - C1.row(i_next)));
  117. for (int j = 0; j<V1.rows(); ++j)
  118. {
  119. Eigen::VectorXd v = C1.row(i) - V1.row(j);
  120. Eigen::VectorXd vnext = C1.row(i_next) - V1.row(j);
  121. Eigen::VectorXd vprev = C1.row(i_prev) - V1.row(j);
  122. // distance from V to every C, where dist_C_V(i,j) is the distance from domain
  123. // vertex j to handle i
  124. dist_C_V(i,j) = std::sqrt(v.dot(v));
  125. double dist_C_V_next = std::sqrt(vnext.dot(vnext));
  126. double a_prev = std::atan2(vprev[1],vprev[0]) - std::atan2(v[1],v[0]);
  127. double a_next = std::atan2(v[1],v[0]) - std::atan2(vnext[1],vnext[0]);
  128. // mean value coordinates
  129. WW(i,j) = (std::tan(a_prev/2.0) + std::tan(a_next/2.0)) / dist_C_V(i,j);
  130. if (dist_C_V(i,j) < EPS)
  131. on_corner.push_back(std::make_pair(j,i));
  132. else
  133. // only in case of no-corner (no need for checking for multiple segments afterwards --
  134. // should only be on one segment (otherwise must be on a corner and we already
  135. // handled that)
  136. // domain vertex j is on the segment from i to i+1 if the distances from vj to
  137. // pi and pi+1 are about
  138. if(std::abs((dist_C_V(i,j) + dist_C_V_next) / edge_length - 1) < EPS)
  139. on_segment.push_back(std::make_pair(j,i));
  140. }
  141. }
  142. // handle degenerate cases
  143. // snap vertices close to corners
  144. for (unsigned i = 0; i<on_corner.size(); ++i)
  145. {
  146. int vi = on_corner[i].first;
  147. int ci = on_corner[i].second;
  148. for (int ii = 0; ii<C.rows(); ++ii)
  149. WW(ii,vi) = (ii==ci)?1:0;
  150. }
  151. // snap vertices close to segments
  152. for (unsigned i = 0; i<on_segment.size(); ++i)
  153. {
  154. int vi = on_segment[i].first;
  155. int ci = on_segment[i].second;
  156. int ci_next = (ci<num-1)?(ci+1):0;
  157. for (int ii = 0; ii<C.rows(); ++ii)
  158. if (ii == ci)
  159. WW(ii,vi) = dist_C_V(ci_next,vi);
  160. else
  161. {
  162. if ( ii == ci_next)
  163. WW(ii,vi) = dist_C_V(ci,vi);
  164. else
  165. WW(ii,vi) = 0;
  166. }
  167. }
  168. // normalize W
  169. for (int i = 0; i<V.rows(); ++i)
  170. WW.col(i) /= WW.col(i).sum();
  171. // we've made W transpose
  172. W = WW.transpose();
  173. }