point_areas.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "point_areas.h"
  2. #include "delaunay_triangulation.h"
  3. #include "../../colon.h"
  4. #include "../../slice.h"
  5. #include "../../slice_mask.h"
  6. #include "../../parallel_for.h"
  7. #include "CGAL/Exact_predicates_inexact_constructions_kernel.h"
  8. #include "CGAL/Triangulation_vertex_base_with_info_2.h"
  9. #include "CGAL/Triangulation_data_structure_2.h"
  10. #include "CGAL/Delaunay_triangulation_2.h"
  11. typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
  12. typedef CGAL::Triangulation_vertex_base_with_info_2<unsigned int, Kernel> Vb;
  13. typedef CGAL::Triangulation_data_structure_2<Vb> Tds;
  14. typedef CGAL::Delaunay_triangulation_2<Kernel, Tds> Delaunay;
  15. typedef Kernel::Point_2 Point;
  16. namespace igl {
  17. namespace copyleft{
  18. namespace cgal{
  19. template <typename DerivedP, typename DerivedI, typename DerivedN,
  20. typename DerivedA>
  21. IGL_INLINE void point_areas(
  22. const Eigen::MatrixBase<DerivedP>& P,
  23. const Eigen::MatrixBase<DerivedI>& I,
  24. const Eigen::MatrixBase<DerivedN>& N,
  25. Eigen::PlainObjectBase<DerivedA> & A)
  26. {
  27. Eigen::MatrixXd T;
  28. point_areas(P,I,N,A,T);
  29. }
  30. template <typename DerivedP, typename DerivedI, typename DerivedN,
  31. typename DerivedA, typename DerivedT>
  32. IGL_INLINE void point_areas(
  33. const Eigen::MatrixBase<DerivedP>& P,
  34. const Eigen::MatrixBase<DerivedI>& I,
  35. const Eigen::MatrixBase<DerivedN>& N,
  36. Eigen::PlainObjectBase<DerivedA> & A,
  37. Eigen::PlainObjectBase<DerivedT> & T)
  38. {
  39. typedef typename DerivedP::Scalar real;
  40. typedef typename DerivedN::Scalar scalarN;
  41. typedef typename DerivedA::Scalar scalarA;
  42. typedef Eigen::Matrix<real,1,3> RowVec3;
  43. typedef Eigen::Matrix<real, Eigen::Dynamic, Eigen::Dynamic> MatrixP;
  44. typedef Eigen::Matrix<scalarN, Eigen::Dynamic, Eigen::Dynamic> MatrixN;
  45. typedef Eigen::Matrix<typename DerivedI::Scalar,
  46. Eigen::Dynamic, Eigen::Dynamic> MatrixI;
  47. const int n = P.rows();
  48. assert(P.cols() == 3 && "P must have exactly 3 columns");
  49. assert(P.rows() == N.rows()
  50. && "P and N must have the same number of rows");
  51. assert(P.rows() == I.rows()
  52. && "P and I must have the same number of rows");
  53. A.setZero(n,1);
  54. T.setZero(n,3);
  55. igl::parallel_for(P.rows(),[&](int i)
  56. {
  57. MatrixI neighbor_index = I.row(i);
  58. MatrixP neighbors;
  59. igl::slice(P,neighbor_index,1,neighbors);
  60. if(N.rows() && neighbors.rows() > 1){
  61. MatrixN neighbor_normals;
  62. igl::slice(N,neighbor_index,1,neighbor_normals);
  63. Eigen::Matrix<scalarN,1,3> poi_normal = neighbor_normals.row(0);
  64. Eigen::Matrix<scalarN,Eigen::Dynamic,1> dotprod =
  65. poi_normal(0)*neighbor_normals.col(0)
  66. + poi_normal(1)*neighbor_normals.col(1)
  67. + poi_normal(2)*neighbor_normals.col(2);
  68. Eigen::Array<bool,Eigen::Dynamic,1> keep = dotprod.array() > 0;
  69. igl::slice_mask(Eigen::MatrixXd(neighbors),keep,1,neighbors);
  70. }
  71. if(neighbors.rows() <= 2){
  72. A(i) = 0;
  73. } else {
  74. //subtract the mean from neighbors, then take svd,
  75. //the scores will be U*S. This is our pca plane fitting
  76. RowVec3 mean = neighbors.colwise().mean();
  77. MatrixP mean_centered = neighbors.rowwise() - mean;
  78. Eigen::JacobiSVD<MatrixP> svd(mean_centered,
  79. Eigen::ComputeThinU | Eigen::ComputeThinV);
  80. MatrixP scores = svd.matrixU() * svd.singularValues().asDiagonal();
  81. T.row(i) = svd.matrixV().col(2).transpose();
  82. if(T.row(i).dot(N.row(i)) < 0){
  83. T.row(i) *= -1;
  84. }
  85. MatrixP plane;
  86. igl::slice(scores,igl::colon<int>(0,scores.rows()-1),
  87. igl::colon<int>(0,1),plane);
  88. std::vector< std::pair<Point,unsigned> > points;
  89. //This is where we obtain a delaunay triangulation of the points
  90. for(unsigned iter = 0; iter < plane.rows(); iter++){
  91. points.push_back( std::make_pair(
  92. Point(plane(iter,0),plane(iter,1)), iter ) );
  93. }
  94. Delaunay triangulation;
  95. triangulation.insert(points.begin(),points.end());
  96. Eigen::MatrixXi F(triangulation.number_of_faces(),3);
  97. int f_row = 0;
  98. for(Delaunay::Finite_faces_iterator fit =
  99. triangulation.finite_faces_begin();
  100. fit != triangulation.finite_faces_end(); ++fit) {
  101. Delaunay::Face_handle face = fit;
  102. F.row(f_row) = Eigen::RowVector3i((int)face->vertex(0)->info(),
  103. (int)face->vertex(1)->info(),
  104. (int)face->vertex(2)->info());
  105. f_row++;
  106. }
  107. //Here we calculate the voronoi area of the point
  108. scalarA area_accumulator = 0;
  109. for(int f = 0; f < F.rows(); f++){
  110. int X = -1;
  111. for(int face_iter = 0; face_iter < 3; face_iter++){
  112. if(F(f,face_iter) == 0){
  113. X = face_iter;
  114. }
  115. }
  116. if(X >= 0){
  117. //Triangle XYZ with X being the point we want the area of
  118. int Y = (X+1)%3;
  119. int Z = (X+2)%3;
  120. scalarA x = (plane.row(F(f,Y))-plane.row(F(f,Z))).norm();
  121. scalarA y = (plane.row(F(f,X))-plane.row(F(f,Z))).norm();
  122. scalarA z = (plane.row(F(f,Y))-plane.row(F(f,X))).norm();
  123. scalarA cosX = (z*z + y*y - x*x)/(2*y*z);
  124. scalarA cosY = (z*z + x*x - y*y)/(2*x*z);
  125. scalarA cosZ = (x*x + y*y - z*z)/(2*y*x);
  126. Eigen::Matrix<scalarA,1,3> barycentric;
  127. barycentric << x*cosX, y*cosY, z*cosZ;
  128. barycentric /= (barycentric(0)+barycentric(1)+barycentric(2));
  129. //TODO: to make numerically stable, reorder so that x≥y≥z:
  130. scalarA full_area = 0.25*std::sqrt(
  131. (x+(y+z))*(z-(x-y))*(z+(x-y))*(x+(y-z)));
  132. Eigen::Matrix<scalarA,1,3> partial_area =
  133. barycentric * full_area;
  134. if(cosX < 0){
  135. area_accumulator += 0.5*full_area;
  136. } else if (cosY < 0 || cosZ < 0){
  137. area_accumulator += 0.25*full_area;
  138. } else {
  139. area_accumulator += (partial_area(1) + partial_area(2))/2;
  140. }
  141. }
  142. }
  143. if(std::isfinite(area_accumulator)){
  144. A(i) = area_accumulator;
  145. } else {
  146. A(i) = 0;
  147. }
  148. }
  149. },1000);
  150. }
  151. }
  152. }
  153. }
  154. #ifdef IGL_STATIC_LIBRARY
  155. // Explicit template instantiation
  156. // generated by autoexplicit.sh
  157. template void igl::copyleft::cgal::point_areas<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  158. #endif