orient_outward_ao.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "orient_outward_ao.h"
  2. #include "../per_face_normals.h"
  3. #include "../barycenter.h"
  4. #include "../doublearea.h"
  5. #include "../matlab_format.h"
  6. #include "ambient_occlusion.h"
  7. #include "EmbreeIntersector.h"
  8. #include <iostream>
  9. #include <random>
  10. template <
  11. typename DerivedV,
  12. typename DerivedF,
  13. typename DerivedC,
  14. typename PointMatrixType,
  15. typename FaceMatrixType,
  16. typename RowVector3,
  17. typename DerivedFF,
  18. typename DerivedI>
  19. IGL_INLINE void igl::orient_outward_ao(
  20. const Eigen::PlainObjectBase<DerivedV> & V,
  21. const Eigen::PlainObjectBase<DerivedF> & F,
  22. const Eigen::PlainObjectBase<DerivedC> & C,
  23. const igl::EmbreeIntersector<PointMatrixType,FaceMatrixType,RowVector3> & ei,
  24. const int num_samples,
  25. Eigen::PlainObjectBase<DerivedFF> & FF,
  26. Eigen::PlainObjectBase<DerivedI> & I)
  27. {
  28. using namespace Eigen;
  29. using namespace std;
  30. assert(C.rows() == F.rows());
  31. assert(F.cols() == 3);
  32. assert(V.cols() == 3);
  33. // number of faces
  34. const int m = F.rows();
  35. // number of patches
  36. const int num_cc = C.maxCoeff()+1;
  37. I.resize(num_cc);
  38. if(&FF != &F)
  39. {
  40. FF = F;
  41. }
  42. PlainObjectBase<DerivedV> N;
  43. Matrix<typename DerivedV::Scalar,Dynamic,1> A;
  44. per_face_normals(V,F,N);
  45. doublearea(V,F,A);
  46. double minarea = A.minCoeff();
  47. mt19937 engine;
  48. engine.seed(time(0));
  49. Matrix<int, Dynamic, 1> A_int = (A * 100.0 / minarea).template cast<int>();
  50. auto ddist_func = [&] (double i) { return A_int(static_cast<int>(i)); };
  51. discrete_distribution<int> ddist(m, 0, m, ddist_func); // simple ctor of (Iter, Iter) not provided by the stupid VC11 impl...
  52. uniform_real_distribution<double> rdist;
  53. Matrix<int, Dynamic, 1> C_occlude_count; // +1 when front ray is occluded, -1 when back ray is occluded
  54. C_occlude_count.setZero(m, 1);
  55. //#pragma omp parallel for
  56. for (int i = 0; i < num_samples; ++i)
  57. {
  58. int f = ddist(engine); // select face with probability proportional to face area
  59. double t0 = rdist(engine);
  60. double t1 = rdist(engine);
  61. double t2 = rdist(engine);
  62. double t_sum = t0 + t1 + t2;
  63. t0 /= t_sum;
  64. t1 /= t_sum;
  65. t2 /= t_sum;
  66. RowVector3d p = t0 * V.row(F(f,0)) + t1 * V.row(F(f,1)) + t1 * V.row(F(f,2));
  67. RowVector3d n = N.row(f);
  68. bool is_backside = rdist(engine) < 0.5;
  69. if (is_backside)
  70. {
  71. n *= -1;
  72. }
  73. Matrix<typename DerivedV::Scalar,Dynamic,1> S;
  74. ambient_occlusion(ei, p, n, 1, S);
  75. if (S(0) > 0)
  76. {
  77. C_occlude_count(C(f)) += is_backside ? -1 : 1;
  78. }
  79. }
  80. for(int c = 0;c<num_cc;c++)
  81. {
  82. I(c) = C_occlude_count(c) > 0;
  83. }
  84. // flip according to I
  85. for(int f = 0;f<m;f++)
  86. {
  87. if(I(C(f)))
  88. {
  89. FF.row(f) = FF.row(f).reverse().eval();
  90. }
  91. }
  92. }
  93. // EmbreeIntersector generated on the fly
  94. template <
  95. typename DerivedV,
  96. typename DerivedF,
  97. typename DerivedC,
  98. typename DerivedFF,
  99. typename DerivedI>
  100. IGL_INLINE void igl::orient_outward_ao(
  101. const Eigen::PlainObjectBase<DerivedV> & V,
  102. const Eigen::PlainObjectBase<DerivedF> & F,
  103. const Eigen::PlainObjectBase<DerivedC> & C,
  104. const int num_samples,
  105. Eigen::PlainObjectBase<DerivedFF> & FF,
  106. Eigen::PlainObjectBase<DerivedI> & I)
  107. {
  108. using namespace igl;
  109. using namespace Eigen;
  110. EmbreeIntersector<
  111. PlainObjectBase<DerivedV>,
  112. PlainObjectBase<DerivedF>,
  113. Matrix<typename DerivedV::Scalar,3,1> > ei(V,F);
  114. return orient_outward_ao(V, F, C, ei, num_samples, FF, I);
  115. }
  116. #ifndef IGL_HEADER_ONLY
  117. // Explicit template specialization
  118. template void igl::orient_outward_ao<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  119. #endif