orient_outward_ao.cpp 7.0 KB

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