point_areas_and_normals.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "point_areas_and_normals.h"
  2. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  3. #include <CGAL/Triangulation_vertex_base_with_info_2.h>
  4. #include <CGAL/Triangulation_data_structure_2.h>
  5. #include <CGAL/Delaunay_triangulation_2.h>
  6. #include <igl/copyleft/cgal/delaunay_triangulation.h>
  7. #include <igl/colon.h>
  8. #include <igl/slice.h>
  9. #include <igl/slice_mask.h>
  10. #include <igl/parallel_for.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. template <typename DerivedP, typename DerivedI, typename DerivedO>
  17. IGL_INLINE void point_areas_and_normals(
  18. const Eigen::MatrixBase<DerivedP>& P,
  19. const Eigen::MatrixBase<DerivedI>& I,
  20. const Eigen::MatrixBase<DerivedO>& O,
  21. Eigen::PlainObjectBase<DerivedA> & A,
  22. Eigen::PlainObjectBase<DerivedA> & N);
  23. {
  24. //namespace igl {
  25. // void point_areas_and_normals(const Eigen::MatrixXd & P,
  26. // const Eigen::MatrixXi & I,
  27. // const Eigen::MatrixXd & O,
  28. // Eigen::VectorXd & A,
  29. // Eigen::MatrixXd & N
  30. // )
  31. // {
  32. const int n = P.rows();
  33. A.setZero(n,1);
  34. N.setZero(n,3);
  35. igl::parallel_for(P.rows(),[&](int i)
  36. {
  37. Eigen::MatrixXi neighbour_index = I.row(i);
  38. Eigen::MatrixXd neighbours;
  39. igl::slice(P,neighbour_index,1,neighbours);
  40. if(O.rows() && neighbours.rows() > 1){
  41. Eigen::MatrixXd neighbour_normals;
  42. igl::slice(O,neighbour_index,1,neighbour_normals);
  43. Eigen::Vector3d poi_normal = neighbour_normals.row(0);
  44. Eigen::VectorXd dotprod = poi_normal(0)*neighbour_normals.col(0)
  45. + poi_normal(1)*neighbour_normals.col(1)
  46. + poi_normal(2)*neighbour_normals.col(2);
  47. Eigen::Array<bool,Eigen::Dynamic,1> keep = dotprod.array() > 0;
  48. igl::slice_mask(Eigen::MatrixXd(neighbours),keep,1,neighbours);
  49. }
  50. if(neighbours.rows() <= 2){
  51. A(i) = 0;
  52. N.row(i) = Eigen::RowVector3d::Zero();
  53. } else {
  54. //subtract the mean from neighbours, then take svd, the scores will be U*S;
  55. //This is our pca plane fitting
  56. Eigen::RowVector3d mean = neighbours.colwise().mean();
  57. Eigen::MatrixXd mean_centered = neighbours.rowwise() - mean;
  58. Eigen::JacobiSVD<Eigen::MatrixXd> svd(mean_centered, Eigen::ComputeThinU | Eigen::ComputeThinV);
  59. Eigen::MatrixXd scores = svd.matrixU() * svd.singularValues().asDiagonal();
  60. N.row(i) = svd.matrixV().col(2).transpose();
  61. if(N.row(i).dot(O.row(i)) < 0){
  62. N.row(i) *= -1;
  63. }
  64. Eigen::MatrixXd plane;
  65. igl::slice(scores,igl::colon<int>(0,scores.rows()-1),igl::colon<int>(0,1),plane);
  66. std::vector< std::pair<Point,unsigned> > points;
  67. //This is where we obtain a delaunay triangulation of the points
  68. for(unsigned iter = 0; iter < plane.rows(); iter++){
  69. points.push_back( std::make_pair( Point(plane(iter,0),plane(iter,1)), iter ) );
  70. }
  71. Delaunay triangulation;
  72. triangulation.insert(points.begin(),points.end());
  73. Eigen::MatrixXi F(triangulation.number_of_faces(),3);
  74. int f_row = 0;
  75. for(Delaunay::Finite_faces_iterator fit = triangulation.finite_faces_begin();
  76. fit != triangulation.finite_faces_end(); ++fit) {
  77. Delaunay::Face_handle face = fit;
  78. F.row(f_row) = Eigen::RowVector3i((int)face->vertex(0)->info(),
  79. (int)face->vertex(1)->info(),
  80. (int)face->vertex(2)->info());
  81. f_row++;
  82. }
  83. //Here we calculate the voronoi area of the point
  84. double area_accumulator = 0;
  85. for(int f = 0; f < F.rows(); f++){
  86. int X = -1;
  87. for(int face_iter = 0; face_iter < 3; face_iter++){
  88. if(F(f,face_iter) == 0){
  89. X = face_iter;
  90. }
  91. }
  92. if(X >= 0){
  93. //Triangle XYZ with X being the point we want the area of
  94. int Y = (X+1)%3;
  95. int Z = (X+2)%3;
  96. double x = (plane.row(F(f,Y))-plane.row(F(f,Z))).norm();
  97. double y = (plane.row(F(f,X))-plane.row(F(f,Z))).norm();
  98. double z = (plane.row(F(f,Y))-plane.row(F(f,X))).norm();
  99. double cosX = (z*z + y*y - x*x)/(2*y*z);
  100. double cosY = (z*z + x*x - y*y)/(2*x*z);
  101. double cosZ = (x*x + y*y - z*z)/(2*y*x);
  102. Eigen::Vector3d barycentric;
  103. barycentric << x*cosX, y*cosY, z*cosZ;
  104. barycentric /= (barycentric(0) + barycentric(1) + barycentric(2));
  105. //TODO: to make numerically stable, reorder so that x≥y≥z:
  106. double full_area = 0.25*std::sqrt((x+(y+z))*(z-(x-y))*(z+(x-y))*(x+(y-z)));
  107. Eigen::Vector3d partial_area = barycentric * full_area;
  108. if(cosX < 0){
  109. area_accumulator += 0.5*full_area;
  110. } else if (cosY < 0 || cosZ < 0){
  111. area_accumulator += 0.25*full_area;
  112. } else {
  113. area_accumulator += (partial_area(1) + partial_area(2))/2;
  114. }
  115. }
  116. }
  117. if(std::isfinite(area_accumulator)){
  118. A(i) = area_accumulator;
  119. } else {
  120. A(i) = 0;
  121. }
  122. }
  123. },1000);
  124. }
  125. }