reorient_facets_raycast.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "../bfs_orient.h"
  13. #include "EmbreeIntersector.h"
  14. #include <iostream>
  15. #include <random>
  16. #include <ctime>
  17. #include <limits>
  18. template <
  19. typename DerivedV,
  20. typename DerivedF,
  21. typename DerivedI,
  22. typename DerivedC>
  23. IGL_INLINE void igl::embree::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 facet_wise,
  29. bool use_parity,
  30. bool is_verbose,
  31. Eigen::PlainObjectBase<DerivedI> & I,
  32. Eigen::PlainObjectBase<DerivedC> & C)
  33. {
  34. using namespace Eigen;
  35. using namespace std;
  36. assert(F.cols() == 3);
  37. assert(V.cols() == 3);
  38. // number of faces
  39. const int m = F.rows();
  40. MatrixXi FF = F;
  41. if (facet_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_total = A.sum();
  61. // determine number of rays per component according to its area
  62. VectorXd area_per_component;
  63. area_per_component.setZero(num_cc);
  64. for (int f = 0; f < m; ++f)
  65. {
  66. area_per_component(C(f)) += A(f);
  67. }
  68. VectorXi num_rays_per_component(num_cc);
  69. for (int c = 0; c < num_cc; ++c)
  70. {
  71. num_rays_per_component(c) = max<int>(static_cast<int>(rays_total * area_per_component(c) / area_total), rays_minimum);
  72. }
  73. rays_total = num_rays_per_component.sum();
  74. // generate all the rays
  75. if (is_verbose) cout << "generating rays... ";
  76. uniform_real_distribution<float> rdist;
  77. mt19937 prng;
  78. prng.seed(time(nullptr));
  79. vector<int > ray_face;
  80. vector<Vector3f> ray_ori;
  81. vector<Vector3f> ray_dir;
  82. ray_face.reserve(rays_total);
  83. ray_ori .reserve(rays_total);
  84. ray_dir .reserve(rays_total);
  85. for (int c = 0; c < num_cc; ++c)
  86. {
  87. if (area_per_component[c] == 0)
  88. {
  89. continue;
  90. }
  91. vector<int> CF; // set of faces per component
  92. vector<double> CF_area;
  93. for (int f = 0; f < m; ++f)
  94. {
  95. if (C(f)==c)
  96. {
  97. CF.push_back(f);
  98. CF_area.push_back(A(f));
  99. }
  100. }
  101. // discrete distribution for random selection of faces with probability proportional to their areas
  102. discrete_distribution<int> ddist(CF.size(), 0, CF.size(), [&](double i){ return CF_area[static_cast<int>(i)]; }); // simple ctor of (Iter, Iter) not provided by the stupid VC11/12
  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() % (rays_total / 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. // To account for the effect of bfs_orient
  196. if (F.row(f) != FF.row(f))
  197. I(f) = 1 - I(f);
  198. }
  199. if (is_verbose) cout << "done!" << endl;
  200. }
  201. template <
  202. typename DerivedV,
  203. typename DerivedF,
  204. typename DerivedFF,
  205. typename DerivedI>
  206. IGL_INLINE void igl::embree::reorient_facets_raycast(
  207. const Eigen::PlainObjectBase<DerivedV> & V,
  208. const Eigen::PlainObjectBase<DerivedF> & F,
  209. Eigen::PlainObjectBase<DerivedFF> & FF,
  210. Eigen::PlainObjectBase<DerivedI> & I)
  211. {
  212. const int rays_total = F.rows()*100;
  213. const int rays_minimum = 10;
  214. const bool facet_wise = false;
  215. const bool use_parity = false;
  216. const bool is_verbose = false;
  217. Eigen::VectorXi C;
  218. reorient_facets_raycast(
  219. V,F,rays_total,rays_minimum,facet_wise,use_parity,is_verbose,I,C);
  220. // Conservative in case FF = F
  221. FF.conservativeResize(F.rows(),F.cols());
  222. for(int i = 0;i<I.rows();i++)
  223. {
  224. if(I(i))
  225. {
  226. FF.row(i) = (F.row(i).reverse()).eval();
  227. }else
  228. {
  229. FF.row(i) = F.row(i);
  230. }
  231. }
  232. }
  233. #ifdef IGL_STATIC_LIBRARY
  234. // Explicit template specialization
  235. template void igl::embree::reorient_facets_raycast<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::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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  236. template void igl::embree::reorient_facets_raycast<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<bool, -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&, int, int, bool, bool, bool, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  237. template void igl::embree::reorient_facets_raycast<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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int, int, bool, bool, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  238. #endif