reorient_facets_raycast.cpp 7.0 KB

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