orient_outward_ao.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <kt84/dbg.h>
  7. #include <iostream>
  8. #include <random>
  9. #include <limits>
  10. template <
  11. typename DerivedV,
  12. typename DerivedF,
  13. typename DerivedC,
  14. typename DerivedFF,
  15. typename DerivedI>
  16. IGL_INLINE void igl::orient_outward_ao(
  17. const Eigen::PlainObjectBase<DerivedV> & V,
  18. const Eigen::PlainObjectBase<DerivedF> & F,
  19. const Eigen::PlainObjectBase<DerivedC> & C,
  20. const int min_num_rays_per_component,
  21. const int total_num_rays,
  22. Eigen::PlainObjectBase<DerivedFF> & FF,
  23. Eigen::PlainObjectBase<DerivedI> & I)
  24. {
  25. using namespace Eigen;
  26. using namespace std;
  27. assert(C.rows() == F.rows());
  28. assert(F.cols() == 3);
  29. assert(V.cols() == 3);
  30. EmbreeIntersector<float, int> ei(V.cast<float>(),F);
  31. // number of faces
  32. const int m = F.rows();
  33. // number of patches
  34. const int num_cc = C.maxCoeff()+1;
  35. I.resize(num_cc);
  36. if(&FF != &F)
  37. {
  38. FF = F;
  39. }
  40. // face normal
  41. MatrixXd N;
  42. per_face_normals(V,F,N);
  43. // face area
  44. Matrix<typename DerivedV::Scalar,Dynamic,1> A;
  45. doublearea(V,F,A);
  46. double area_min = A.minCoeff();
  47. double area_total = A.sum();
  48. // determine number of rays per component according to its area
  49. VectorXd area_per_component;
  50. area_per_component.setZero(num_cc);
  51. for (int f = 0; f < m; ++f)
  52. {
  53. area_per_component(C(f)) += A(f);
  54. }
  55. VectorXi num_rays_per_component;
  56. num_rays_per_component.setZero(num_cc);
  57. for (int c = 0; c < num_cc; ++c)
  58. {
  59. num_rays_per_component(c) = max<int>(min_num_rays_per_component, static_cast<int>(total_num_rays * area_per_component(c) / area_total));
  60. }
  61. // generate all the rays
  62. cout << "generating rays... ";
  63. uniform_real_distribution<float> rdist;
  64. mt19937 prng;
  65. prng.seed(0);
  66. vector<int > ray_face;
  67. vector<Vector3f> ray_ori;
  68. vector<Vector3f> ray_dir;
  69. ray_face.reserve(total_num_rays);
  70. ray_ori .reserve(total_num_rays);
  71. ray_dir .reserve(total_num_rays);
  72. for (int c = 0; c < num_cc; ++c)
  73. {
  74. vector<int> CF; // set of faces per component
  75. vector<int> CF_area;
  76. for (int f = 0; f < m; ++f)
  77. {
  78. if (C(f)==c)
  79. {
  80. CF.push_back(f);
  81. CF_area.push_back(static_cast<int>(100 * A(f) / area_min));
  82. }
  83. }
  84. // discrete distribution for random selection of faces with probability proportional to their areas
  85. auto ddist_func = [&] (double i) { return CF_area[static_cast<int>(i)]; };
  86. discrete_distribution<int> ddist(CF.size(), 0, CF.size(), ddist_func); // simple ctor of (Iter, Iter) not provided by the stupid VC11 impl...
  87. for (int i = 0; i < num_rays_per_component[c]; ++i)
  88. {
  89. int f = CF[ddist(prng)]; // select face with probability proportional to face area
  90. float t0 = rdist(prng); // random barycentric coordinate
  91. float t1 = rdist(prng);
  92. float t2 = rdist(prng);
  93. float t_sum = t0 + t1 + t2;
  94. t0 /= t_sum;
  95. t1 /= t_sum;
  96. t2 /= t_sum;
  97. Vector3f p = t0 * V.row(F(f,0)).cast<float>().eval() // be careful with the index!!!
  98. + t1 * V.row(F(f,1)).cast<float>().eval()
  99. + t2 * V.row(F(f,2)).cast<float>().eval();
  100. Vector3f n = N.row(f).cast<float>();
  101. assert(n != Vector3f::Zero());
  102. // random direction in hemisphere around n (avoid too grazing angle)
  103. Vector3f d;
  104. while (true) {
  105. d = random_dir().cast<float>();
  106. float ndotd = n.dot(d);
  107. if (fabsf(ndotd) < 0.1f)
  108. {
  109. continue;
  110. }
  111. if (ndotd < 0)
  112. {
  113. d *= -1.0f;
  114. }
  115. break;
  116. }
  117. ray_face.push_back(f);
  118. ray_ori .push_back(p);
  119. ray_dir .push_back(d);
  120. }
  121. }
  122. // per component voting: positive for front side, negative for back side
  123. vector<float> C_vote_distance(num_cc, 0); // sum of distance between ray origin and intersection
  124. vector<int > C_vote_infinity(num_cc, 0); // number of rays reaching infinity
  125. auto get_hit_point = [&] (Hit hit) -> Vector3f
  126. {
  127. Vector3f p0 = V.row(F(hit.id, 0)).cast<float>();
  128. Vector3f p1 = V.row(F(hit.id, 1)).cast<float>();
  129. Vector3f p2 = V.row(F(hit.id, 2)).cast<float>();
  130. return (1.0f - hit.u - hit.v) * p0 + hit.u * p1 + hit.v * p2;
  131. };
  132. cout << "shooting rays... ";
  133. #pragma omp parallel for
  134. for (int i = 0; i < (int)ray_face.size(); ++i)
  135. {
  136. int f = ray_face[i];
  137. Vector3f o = ray_ori [i];
  138. Vector3f d = ray_dir [i];
  139. int c = C(f);
  140. if (c==65)
  141. c=c;
  142. // shoot ray toward front & back
  143. Hit hit_front;
  144. bool is_hit_front;
  145. for (float offset = numeric_limits<float>::min(); ; offset *= 10.0f) {
  146. hit_front.id = -1;
  147. is_hit_front = ei.intersectRay(o + offset * d, d, hit_front);
  148. if (!is_hit_front) break;
  149. if (hit_front.id != f) break;
  150. }
  151. Hit hit_back;
  152. bool is_hit_back;
  153. for (float offset = numeric_limits<float>::min(); ; offset *= 10.0f) {
  154. hit_back.id = -1;
  155. is_hit_back = ei.intersectRay(o - offset * d, -d, hit_back);
  156. if (!is_hit_back) break;
  157. if (hit_back.id != f) break;
  158. }
  159. if (hit_front.id == f || hit_back.id == f)
  160. {
  161. // due to numerical error, ray origin was detected as intersection -> ignore this ray
  162. continue;
  163. }
  164. if (is_hit_front)
  165. {
  166. #pragma omp atomic
  167. C_vote_distance[c] += (get_hit_point(hit_front) - o).norm();
  168. } else {
  169. #pragma omp atomic
  170. C_vote_infinity[c]++;
  171. }
  172. if (is_hit_back)
  173. {
  174. #pragma omp atomic
  175. C_vote_distance[c] -= (get_hit_point(hit_back) - o).norm();
  176. } else {
  177. #pragma omp atomic
  178. C_vote_infinity[c]--;
  179. }
  180. }
  181. for(int c = 0;c<num_cc;c++)
  182. {
  183. I(c) = C_vote_infinity[c] < 0;// || C_vote_distance[c] < 0;
  184. }
  185. // flip according to I
  186. for(int f = 0;f<m;f++)
  187. {
  188. if(I(C(f)))
  189. {
  190. FF.row(f) = FF.row(f).reverse().eval();
  191. }
  192. }
  193. cout << "done!\n";
  194. }
  195. // Call with default parameters
  196. template <
  197. typename DerivedV,
  198. typename DerivedF,
  199. typename DerivedC,
  200. typename DerivedFF,
  201. typename DerivedI>
  202. IGL_INLINE void igl::orient_outward_ao(
  203. const Eigen::PlainObjectBase<DerivedV> & V,
  204. const Eigen::PlainObjectBase<DerivedF> & F,
  205. const Eigen::PlainObjectBase<DerivedC> & C,
  206. Eigen::PlainObjectBase<DerivedFF> & FF,
  207. Eigen::PlainObjectBase<DerivedI> & I)
  208. {
  209. return orient_outward_ao(V, F, C, 100, F.rows() * 100, FF, I);
  210. }
  211. #ifndef IGL_HEADER_ONLY
  212. // Explicit template specialization
  213. 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> >&);
  214. #endif