orient_outward_ao.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "orient_outward_ao.h"
  2. #include "../per_face_normals.h"
  3. #include "../doublearea.h"
  4. #include "../random_dir.h"
  5. #include "EmbreeIntersector.h"
  6. #include <iostream>
  7. #include <random>
  8. #include <limits>
  9. template <
  10. typename DerivedV,
  11. typename DerivedF,
  12. typename DerivedC,
  13. typename DerivedFF,
  14. typename DerivedI>
  15. IGL_INLINE void igl::orient_outward_ao(
  16. const Eigen::PlainObjectBase<DerivedV> & V,
  17. const Eigen::PlainObjectBase<DerivedF> & F,
  18. const Eigen::PlainObjectBase<DerivedC> & C,
  19. const int min_num_rays_per_component,
  20. const int total_num_rays,
  21. Eigen::PlainObjectBase<DerivedFF> & FF,
  22. Eigen::PlainObjectBase<DerivedI> & I)
  23. {
  24. using namespace Eigen;
  25. using namespace std;
  26. assert(C.rows() == F.rows());
  27. assert(F.cols() == 3);
  28. assert(V.cols() == 3);
  29. // pass both sides of faces to Embree
  30. MatrixXi F2;
  31. F2.resize(F.rows()*2,F.cols());
  32. F2 << F, F.rowwise().reverse().eval();
  33. EmbreeIntersector<typename DerivedV::Scalar, typename DerivedF::Scalar> ei(V,F2);
  34. // number of faces
  35. const int m = F.rows();
  36. // number of patches
  37. const int num_cc = C.maxCoeff()+1;
  38. I.resize(num_cc);
  39. if(&FF != &F)
  40. {
  41. FF = F;
  42. }
  43. // face normal
  44. PlainObjectBase<DerivedV> N;
  45. per_face_normals(V,F,N);
  46. // face area
  47. Matrix<typename DerivedV::Scalar,Dynamic,1> A;
  48. doublearea(V,F,A);
  49. double area_min = A.minCoeff();
  50. double area_total = A.sum();
  51. // determine number of rays per component according to its area
  52. VectorXd area_per_component;
  53. area_per_component.setZero(num_cc);
  54. for (int f = 0; f < m; ++f)
  55. {
  56. area_per_component(C(f)) += A(f);
  57. }
  58. VectorXi num_rays_per_component;
  59. num_rays_per_component.setZero(num_cc);
  60. for (int c = 0; c < num_cc; ++c)
  61. {
  62. num_rays_per_component(c) = max<int>(min_num_rays_per_component, static_cast<int>(total_num_rays * area_per_component(c) / area_total));
  63. }
  64. // generate all the rays
  65. cout << "generating rays... ";
  66. uniform_real_distribution<double> rdist;
  67. mt19937 prng;
  68. prng.seed(time(0));
  69. vector<int > ray_face;
  70. vector<Vector3d> ray_ori;
  71. vector<Vector3d> ray_dir;
  72. ray_face.reserve(total_num_rays);
  73. ray_ori .reserve(total_num_rays);
  74. ray_dir .reserve(total_num_rays);
  75. for (int c = 0; c < num_cc; ++c)
  76. {
  77. vector<int> CF; // set of faces per component
  78. vector<int> CF_area;
  79. for (int f = 0; f < m; ++f)
  80. {
  81. if (C(f)==c)
  82. {
  83. CF.push_back(f);
  84. CF_area.push_back(static_cast<int>(100 * A(f) / area_min));
  85. }
  86. }
  87. // discrete distribution for random selection of faces with probability proportional to their areas
  88. auto ddist_func = [&] (double i) { return CF_area[static_cast<int>(i)]; };
  89. discrete_distribution<int> ddist(CF.size(), 0, CF.size(), ddist_func); // simple ctor of (Iter, Iter) not provided by the stupid VC11 impl...
  90. for (int i = 0; i < num_rays_per_component[c]; ++i)
  91. {
  92. int f = CF[ddist(prng)]; // select face with probability proportional to face area
  93. double t0 = rdist(prng); // random barycentric coordinate
  94. double t1 = rdist(prng);
  95. double t2 = rdist(prng);
  96. double t_sum = t0 + t1 + t2;
  97. t0 /= t_sum;
  98. t1 /= t_sum;
  99. t2 /= t_sum;
  100. Vector3d p = t0 * V.row(F(f,0)) // be careful with the index!!!
  101. + t1 * V.row(F(f,1))
  102. + t2 * V.row(F(f,2));
  103. Vector3d n = N.row(f);
  104. Vector3d d = random_dir();
  105. if (n.dot(d) < 0)
  106. {
  107. d *= -1;
  108. }
  109. ray_face.push_back(f);
  110. ray_ori .push_back(p);
  111. ray_dir .push_back(d);
  112. }
  113. }
  114. // per component accumulation of occlusion distance
  115. double dist_large = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm() * 1000;
  116. vector<double> C_occlude_dist_front(num_cc, 0);
  117. vector<double> C_occlude_dist_back (num_cc, 0);
  118. auto get_dist = [&] (Hit hit, const Vector3d& origin) {
  119. Vector3d p0 = V.row(F2(hit.id, 0));
  120. Vector3d p1 = V.row(F2(hit.id, 1));
  121. Vector3d p2 = V.row(F2(hit.id, 2));
  122. Vector3d p = (1 - hit.u - hit.v) * p0 + hit.u * p1 + hit.v * p2;
  123. return (p - origin).norm();
  124. };
  125. cout << "shooting rays... ";
  126. #pragma omp parallel for
  127. for (int i = 0; i < (int)ray_face.size(); ++i)
  128. {
  129. int f = ray_face[i];
  130. Vector3d o = ray_ori [i];
  131. Vector3d d = ray_dir [i];
  132. int c = C(f);
  133. Hit hit_front;
  134. Hit hit_back;
  135. double dist_front = ei.intersectRay(o, d, hit_front) ? get_dist(hit_front, o) : dist_large;
  136. double dist_back = ei.intersectRay(o, -d, hit_back ) ? get_dist(hit_back , o) : dist_large;
  137. #pragma omp atomic
  138. C_occlude_dist_front[c] += dist_front;
  139. #pragma omp atomic
  140. C_occlude_dist_back [c] += dist_back;
  141. }
  142. for(int c = 0;c<num_cc;c++)
  143. {
  144. I(c) = C_occlude_dist_front[c] < C_occlude_dist_back[c];
  145. }
  146. // flip according to I
  147. for(int f = 0;f<m;f++)
  148. {
  149. if(I(C(f)))
  150. {
  151. FF.row(f) = FF.row(f).reverse().eval();
  152. }
  153. }
  154. cout << "done!\n";
  155. }
  156. // Call with default parameters
  157. template <
  158. typename DerivedV,
  159. typename DerivedF,
  160. typename DerivedC,
  161. typename DerivedFF,
  162. typename DerivedI>
  163. IGL_INLINE void igl::orient_outward_ao(
  164. const Eigen::PlainObjectBase<DerivedV> & V,
  165. const Eigen::PlainObjectBase<DerivedF> & F,
  166. const Eigen::PlainObjectBase<DerivedC> & C,
  167. Eigen::PlainObjectBase<DerivedFF> & FF,
  168. Eigen::PlainObjectBase<DerivedI> & I)
  169. {
  170. return orient_outward_ao(V, F, C, 100, F.rows() * 100, FF, I);
  171. }
  172. #ifndef IGL_HEADER_ONLY
  173. // Explicit template specialization
  174. 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, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  175. #endif