orient_outward_ao.cpp 6.0 KB

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