ambient_occlusion.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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 "ambient_occlusion.h"
  9. #include "random_dir.h"
  10. #include "ray_mesh_intersect.h"
  11. #include "EPS.h"
  12. #include "Hit.h"
  13. #include <thread>
  14. #include <functional>
  15. #include <vector>
  16. #include <algorithm>
  17. template <
  18. typename DerivedP,
  19. typename DerivedN,
  20. typename DerivedS >
  21. IGL_INLINE void igl::ambient_occlusion(
  22. const std::function<
  23. bool(
  24. const Eigen::Vector3f&,
  25. const Eigen::Vector3f&)
  26. > & shoot_ray,
  27. const Eigen::PlainObjectBase<DerivedP> & P,
  28. const Eigen::PlainObjectBase<DerivedN> & N,
  29. const int num_samples,
  30. Eigen::PlainObjectBase<DerivedS> & S)
  31. {
  32. using namespace Eigen;
  33. const int n = P.rows();
  34. // Resize output
  35. S.resize(n,1);
  36. VectorXi hits = VectorXi::Zero(n,1);
  37. // Embree seems to be parallel when constructing but not when tracing rays
  38. const MatrixXf D = random_dir_stratified(num_samples).cast<float>();
  39. const size_t nthreads = n<1000?1:std::thread::hardware_concurrency();
  40. {
  41. std::vector<std::thread> threads(nthreads);
  42. for(int t = 0;t<nthreads;t++)
  43. {
  44. threads[t] = std::thread(std::bind(
  45. [&P,&N,&shoot_ray,&S,&num_samples,&D](const int bi, const int ei, const int t)
  46. {
  47. // loop over mesh vertices in this chunk
  48. for(int p = bi;p<ei;p++)
  49. {
  50. const Vector3f origin = P.row(p).template cast<float>();
  51. const Vector3f normal = N.row(p).template cast<float>();
  52. int num_hits = 0;
  53. for(int s = 0;s<num_samples;s++)
  54. {
  55. Vector3f d = D.row(s);
  56. if(d.dot(normal) < 0)
  57. {
  58. // reverse ray
  59. d *= -1;
  60. }
  61. if(shoot_ray(origin,d))
  62. {
  63. num_hits++;
  64. }
  65. }
  66. S(p) = (double)num_hits/(double)num_samples;
  67. }
  68. },t*n/nthreads,(t+1)==nthreads?n:(t+1)*n/nthreads,t));
  69. }
  70. std::for_each(threads.begin(),threads.end(),[](std::thread& x){x.join();});
  71. }
  72. }
  73. template <
  74. typename DerivedV,
  75. int DIM,
  76. typename DerivedF,
  77. typename DerivedP,
  78. typename DerivedN,
  79. typename DerivedS >
  80. IGL_INLINE void igl::ambient_occlusion(
  81. const igl::AABB<DerivedV,DIM> & aabb,
  82. const Eigen::PlainObjectBase<DerivedV> & V,
  83. const Eigen::PlainObjectBase<DerivedF> & F,
  84. const Eigen::PlainObjectBase<DerivedP> & P,
  85. const Eigen::PlainObjectBase<DerivedN> & N,
  86. const int num_samples,
  87. Eigen::PlainObjectBase<DerivedS> & S)
  88. {
  89. const auto & shoot_ray = [&aabb,&V,&F](
  90. const Eigen::Vector3f& _s,
  91. const Eigen::Vector3f& dir)->bool
  92. {
  93. Eigen::Vector3f s = _s+1e-4*dir;
  94. igl::Hit hit;
  95. return aabb.intersect_ray(
  96. V,
  97. F,
  98. s .cast<typename DerivedV::Scalar>().eval(),
  99. dir.cast<typename DerivedV::Scalar>().eval(),
  100. hit);
  101. };
  102. return ambient_occlusion(shoot_ray,P,N,num_samples,S);
  103. }
  104. template <
  105. typename DerivedV,
  106. typename DerivedF,
  107. typename DerivedP,
  108. typename DerivedN,
  109. typename DerivedS >
  110. IGL_INLINE void igl::ambient_occlusion(
  111. const Eigen::PlainObjectBase<DerivedV> & V,
  112. const Eigen::PlainObjectBase<DerivedF> & F,
  113. const Eigen::PlainObjectBase<DerivedP> & P,
  114. const Eigen::PlainObjectBase<DerivedN> & N,
  115. const int num_samples,
  116. Eigen::PlainObjectBase<DerivedS> & S)
  117. {
  118. if(F.rows() < 100)
  119. {
  120. // Super naive
  121. const auto & shoot_ray = [&V,&F](
  122. const Eigen::Vector3f& _s,
  123. const Eigen::Vector3f& dir)->bool
  124. {
  125. Eigen::Vector3f s = _s+1e-4*dir;
  126. igl::Hit hit;
  127. return ray_mesh_intersect(s,dir,V,F,hit);
  128. };
  129. return ambient_occlusion(shoot_ray,P,N,num_samples,S);
  130. }
  131. AABB<DerivedV,3> aabb;
  132. aabb.init(V,F);
  133. return ambient_occlusion(aabb,V,F,P,N,num_samples,S);
  134. }
  135. #ifdef IGL_STATIC_LIBRARY
  136. // Explicit template specialization
  137. // generated by autoexplicit.sh
  138. template void igl::ambient_occlusion<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::function<bool (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  139. // generated by autoexplicit.sh
  140. template void igl::ambient_occlusion<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::function<bool (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  141. // generated by autoexplicit.sh
  142. template void igl::ambient_occlusion<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::function<bool (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  143. template void igl::ambient_occlusion<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::function<bool (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  144. #endif