orient_outward_ao.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include <omp.h>
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename DerivedC,
  15. typename Scalar,
  16. typename Index,
  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<Scalar,Index> & 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. // face normal
  43. PlainObjectBase<DerivedV> N;
  44. per_face_normals(V,F,N);
  45. // random number generator/distribution for each thread
  46. int max_threads = omp_get_max_threads();
  47. // prng
  48. vector<mt19937> engine(max_threads);
  49. for (int i = 0; i < max_threads; ++i)
  50. engine[i].seed(time(0) * (i + 1));
  51. // discrete distribution for random selection of faces with probability proportional to their areas
  52. Matrix<typename DerivedV::Scalar,Dynamic,1> A;
  53. doublearea(V,F,A);
  54. double minarea = A.minCoeff();
  55. Matrix<int, Dynamic, 1> A_int = (A * 100.0 / minarea).template cast<int>(); // only integer is allowed for weight
  56. auto ddist_func = [&] (double i) { return A_int(static_cast<int>(i)); };
  57. vector<discrete_distribution<int>> ddist(max_threads, discrete_distribution<int>(m, 0, m, ddist_func)); // simple ctor of (Iter, Iter) not provided by the stupid VC11 impl...
  58. // uniform real between in [0, 1]
  59. vector<uniform_real_distribution<double>> rdist(max_threads);
  60. //// occlusion count per component: +1 when front ray is occluded, -1 when back ray is occluded
  61. // occlussion count per component, per back/front: C(c,0) --> number of
  62. // front-side rays occluded, C(c,1) --> number of back-side rays occluded
  63. Matrix<int, Dynamic, 2> C_occlude_count;
  64. C_occlude_count.setZero(num_cc, 2);
  65. #pragma omp parallel for
  66. for (int i = 0; i < num_samples; ++i)
  67. {
  68. int thread_num = omp_get_thread_num();
  69. int f = ddist[thread_num](engine[thread_num]); // select face with probability proportional to face area
  70. double t0 = rdist[thread_num](engine[thread_num]);
  71. double t1 = rdist[thread_num](engine[thread_num]);
  72. double t2 = rdist[thread_num](engine[thread_num]);
  73. double t_sum = t0 + t1 + t2;
  74. t0 /= t_sum;
  75. t1 /= t_sum;
  76. t2 /= t_sum;
  77. RowVector3d p = t0 * V.row(F(f,0)) + t1 * V.row(F(f,1)) + t1 * V.row(F(f,2));
  78. RowVector3d n = N.row(f);
  79. //bool is_backside = rdist[thread_num](engine[thread_num]) < 0.5;
  80. // Loop over front or back side
  81. for(int s = 0;s<2;s++)
  82. {
  83. if(s==1)
  84. {
  85. n *= -1;
  86. }
  87. Matrix<typename DerivedV::Scalar,Dynamic,1> S;
  88. ambient_occlusion(ei, p, n, 1, S);
  89. if (S(0) > 0)
  90. {
  91. #pragma omp atomic
  92. C_occlude_count(C(f),s)++;
  93. }
  94. }
  95. }
  96. for(int c = 0;c<num_cc;c++)
  97. {
  98. //I(c) = C_occlude_count(c) > 0;
  99. I(c) = C_occlude_count(c,0) > C_occlude_count(c,1);
  100. }
  101. // flip according to I
  102. for(int f = 0;f<m;f++)
  103. {
  104. if(I(C(f)))
  105. {
  106. FF.row(f) = FF.row(f).reverse().eval();
  107. }
  108. }
  109. }
  110. // EmbreeIntersector generated on the fly
  111. template <
  112. typename DerivedV,
  113. typename DerivedF,
  114. typename DerivedC,
  115. typename DerivedFF,
  116. typename DerivedI>
  117. IGL_INLINE void igl::orient_outward_ao(
  118. const Eigen::PlainObjectBase<DerivedV> & V,
  119. const Eigen::PlainObjectBase<DerivedF> & F,
  120. const Eigen::PlainObjectBase<DerivedC> & C,
  121. const int num_samples,
  122. Eigen::PlainObjectBase<DerivedFF> & FF,
  123. Eigen::PlainObjectBase<DerivedI> & I)
  124. {
  125. using namespace igl;
  126. using namespace Eigen;
  127. // Both sides
  128. MatrixXi F2;
  129. F2.resize(F.rows()*2,F.cols());
  130. F2 << F, F.rowwise().reverse().eval();
  131. EmbreeIntersector<
  132. typename DerivedV::Scalar,
  133. typename DerivedF::Scalar > ei(V,F2);
  134. return orient_outward_ao(V, F, C, ei, num_samples, FF, I);
  135. }
  136. #ifndef IGL_HEADER_ONLY
  137. // Explicit template specialization
  138. 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> >&);
  139. #endif