reorient_facets_raycast.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "reorient_facets_raycast.h"
  9. #include "../per_face_normals.h"
  10. #include "../doublearea.h"
  11. #include "../random_dir.h"
  12. #include "../boost/bfs_orient.h"
  13. #include "orient_outward_ao.h"
  14. #include "EmbreeIntersector.h"
  15. #include <iostream>
  16. #include <random>
  17. #include <ctime>
  18. #include <limits>
  19. template <
  20. typename DerivedV,
  21. typename DerivedF,
  22. typename DerivedI>
  23. IGL_INLINE void igl::reorient_facets_raycast(
  24. const Eigen::PlainObjectBase<DerivedV> & V,
  25. const Eigen::PlainObjectBase<DerivedF> & F,
  26. int num_rays,
  27. bool use_parity,
  28. bool is_verbose,
  29. Eigen::PlainObjectBase<DerivedI> & I)
  30. {
  31. using namespace Eigen;
  32. using namespace std;
  33. assert(F.cols() == 3);
  34. assert(V.cols() == 3);
  35. // number of faces
  36. const int m = F.rows();
  37. if (is_verbose) cout << "extracting patches... ";
  38. VectorXi C;
  39. MatrixXi FF = F;
  40. bfs_orient(F,FF,C);
  41. if (is_verbose) cout << (C.maxCoeff() + 1) << " components. ";
  42. // number of patches
  43. const int num_cc = C.maxCoeff()+1;
  44. // Init Embree
  45. EmbreeIntersector ei;
  46. ei.init(V.template cast<float>(),FF);
  47. // face normal
  48. MatrixXd N;
  49. per_face_normals(V,FF,N);
  50. // face area
  51. Matrix<typename DerivedV::Scalar,Dynamic,1> A;
  52. doublearea(V,FF,A);
  53. double area_min = numeric_limits<double>::max();
  54. for (int f = 0; f < m; ++f)
  55. {
  56. area_min = A(f) != 0 && A(f) < area_min ? A(f) : area_min;
  57. }
  58. double area_total = A.sum();
  59. // determine number of rays per component according to its area
  60. VectorXd area_per_component;
  61. area_per_component.setZero(num_cc);
  62. for (int f = 0; f < m; ++f)
  63. {
  64. area_per_component(C(f)) += A(f);
  65. }
  66. VectorXi num_rays_per_component(num_cc);
  67. num_rays_per_component.fill(100); // Minimum number of rays per component (predefined)
  68. for (int c = 0; c < num_cc; ++c)
  69. {
  70. num_rays_per_component(c) += static_cast<int>(num_rays * area_per_component(c) / area_total);
  71. }
  72. num_rays = num_rays_per_component.sum();
  73. // generate all the rays
  74. if (is_verbose) cout << "generating rays... ";
  75. uniform_real_distribution<float> rdist;
  76. mt19937 prng;
  77. prng.seed(time(nullptr));
  78. vector<int > ray_face;
  79. vector<Vector3f> ray_ori;
  80. vector<Vector3f> ray_dir;
  81. ray_face.reserve(num_rays);
  82. ray_ori .reserve(num_rays);
  83. ray_dir .reserve(num_rays);
  84. for (int c = 0; c < num_cc; ++c)
  85. {
  86. if (area_per_component[c] == 0)
  87. {
  88. continue;
  89. }
  90. vector<int> CF; // set of faces per component
  91. vector<unsigned long long> CF_area;
  92. for (int f = 0; f < m; ++f)
  93. {
  94. if (C(f)==c)
  95. {
  96. CF.push_back(f);
  97. CF_area.push_back(static_cast<unsigned long long>(100 * A(f) / area_min));
  98. }
  99. }
  100. // discrete distribution for random selection of faces with probability proportional to their areas
  101. auto ddist_func = [&] (double i) { return CF_area[static_cast<int>(i)]; };
  102. discrete_distribution<int> ddist(CF.size(), 0, CF.size(), ddist_func); // simple ctor of (Iter, Iter) not provided by the stupid VC11 impl...
  103. for (int i = 0; i < num_rays_per_component[c]; ++i)
  104. {
  105. int f = CF[ddist(prng)]; // select face with probability proportional to face area
  106. float s = rdist(prng); // random barycentric coordinate (reference: Generating Random Points in Triangles [Turk, Graphics Gems I 1990])
  107. float t = rdist(prng);
  108. float sqrt_t = sqrtf(t);
  109. float a = 1 - sqrt_t;
  110. float b = (1 - s) * sqrt_t;
  111. float c = s * sqrt_t;
  112. Vector3f p = a * V.row(FF(f,0)).template cast<float>().eval() // be careful with the index!!!
  113. + b * V.row(FF(f,1)).template cast<float>().eval()
  114. + c * V.row(FF(f,2)).template cast<float>().eval();
  115. Vector3f n = N.row(f).cast<float>();
  116. if (n.isZero()) continue;
  117. // random direction in hemisphere around n (avoid too grazing angle)
  118. Vector3f d;
  119. while (true) {
  120. d = random_dir().cast<float>();
  121. float ndotd = n.dot(d);
  122. if (fabsf(ndotd) < 0.1f)
  123. {
  124. continue;
  125. }
  126. if (ndotd < 0)
  127. {
  128. d *= -1.0f;
  129. }
  130. break;
  131. }
  132. ray_face.push_back(f);
  133. ray_ori .push_back(p);
  134. ray_dir .push_back(d);
  135. if (is_verbose && ray_face.size() % (num_rays / 10) == 0) cout << ".";
  136. }
  137. }
  138. if (is_verbose) cout << ray_face.size() << " rays. ";
  139. // per component voting: first=front, second=back
  140. vector<pair<float, float>> C_vote_distance(num_cc, make_pair(0, 0)); // sum of distance between ray origin and intersection
  141. vector<pair<int , int >> C_vote_infinity(num_cc, make_pair(0, 0)); // number of rays reaching infinity
  142. vector<pair<int , int >> C_vote_parity(num_cc, make_pair(0, 0)); // sum of parity count for each ray
  143. if (is_verbose) cout << "shooting rays... ";
  144. #pragma omp parallel for
  145. for (int i = 0; i < (int)ray_face.size(); ++i)
  146. {
  147. int f = ray_face[i];
  148. Vector3f o = ray_ori [i];
  149. Vector3f d = ray_dir [i];
  150. int c = C(f);
  151. // shoot ray toward front & back
  152. vector<Hit> hits_front;
  153. vector<Hit> hits_back;
  154. int num_rays_front;
  155. int num_rays_back;
  156. ei.intersectRay(o, d, hits_front, num_rays_front);
  157. ei.intersectRay(o, -d, hits_back , num_rays_back );
  158. if (!hits_front.empty() && hits_front[0].id == f) hits_front.erase(hits_front.begin());
  159. if (!hits_back .empty() && hits_back [0].id == f) hits_back .erase(hits_back .begin());
  160. if (use_parity) {
  161. #pragma omp atomic
  162. C_vote_parity[c].first += hits_front.size() % 2;
  163. #pragma omp atomic
  164. C_vote_parity[c].second += hits_back .size() % 2;
  165. } else {
  166. if (hits_front.empty())
  167. {
  168. #pragma omp atomic
  169. C_vote_infinity[c].first++;
  170. } else {
  171. #pragma omp atomic
  172. C_vote_distance[c].first += hits_front[0].t;
  173. }
  174. if (hits_back.empty())
  175. {
  176. #pragma omp atomic
  177. C_vote_infinity[c].second++;
  178. } else {
  179. #pragma omp atomic
  180. C_vote_distance[c].second += hits_back[0].t;
  181. }
  182. }
  183. }
  184. I.resize(m);
  185. for(int f = 0; f < m; ++f)
  186. {
  187. int c = C(f);
  188. if (use_parity) {
  189. I(f) = C_vote_parity[c].first > C_vote_parity[c].second ? 1 : 0; // Ideally, parity for the front/back side should be 1/0 (i.e., parity sum for all rays should be smaller on the front side)
  190. } else {
  191. I(f) = (C_vote_infinity[c].first == C_vote_infinity[c].second && C_vote_distance[c].first < C_vote_distance[c].second) ||
  192. C_vote_infinity[c].first < C_vote_infinity[c].second
  193. ? 1 : 0;
  194. }
  195. }
  196. if (is_verbose) cout << "done!" << endl;
  197. }
  198. #ifndef IGL_HEADER_ONLY
  199. // Explicit template specialization
  200. #endif