ambient_occlusion.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. template <
  14. typename DerivedP,
  15. typename DerivedN,
  16. typename DerivedS >
  17. IGL_INLINE void igl::ambient_occlusion(
  18. const std::function<
  19. bool(
  20. const Eigen::Vector3f&,
  21. const Eigen::Vector3f&)
  22. > & shoot_ray,
  23. const Eigen::PlainObjectBase<DerivedP> & P,
  24. const Eigen::PlainObjectBase<DerivedN> & N,
  25. const int num_samples,
  26. Eigen::PlainObjectBase<DerivedS> & S)
  27. {
  28. using namespace Eigen;
  29. const int n = P.rows();
  30. // Resize output
  31. S.resize(n,1);
  32. VectorXi hits = VectorXi::Zero(n,1);
  33. const MatrixXf D = random_dir_stratified(num_samples).cast<float>();
  34. // Embree seems to be parallel when constructing but not when tracing rays
  35. #pragma omp parallel for
  36. // loop over mesh vertices
  37. for(int p = 0;p<n;p++)
  38. {
  39. const Vector3f origin = P.row(p).template cast<float>();
  40. const Vector3f normal = N.row(p).template cast<float>();
  41. int num_hits = 0;
  42. for(int s = 0;s<num_samples;s++)
  43. {
  44. // //Vector3d d = random_dir();
  45. Vector3f d = D.row(s);
  46. if(d.dot(normal) < 0)
  47. {
  48. // reverse ray
  49. d *= -1;
  50. }
  51. if(shoot_ray(origin,d))
  52. {
  53. num_hits++;
  54. }
  55. }
  56. S(p) = (double)num_hits/(double)num_samples;
  57. }
  58. }
  59. template <
  60. typename DerivedV,
  61. int DIM,
  62. typename DerivedF,
  63. typename DerivedP,
  64. typename DerivedN,
  65. typename DerivedS >
  66. IGL_INLINE void igl::ambient_occlusion(
  67. const igl::AABB<DerivedV,DIM> & aabb,
  68. const Eigen::PlainObjectBase<DerivedV> & V,
  69. const Eigen::PlainObjectBase<DerivedF> & F,
  70. const Eigen::PlainObjectBase<DerivedP> & P,
  71. const Eigen::PlainObjectBase<DerivedN> & N,
  72. const int num_samples,
  73. Eigen::PlainObjectBase<DerivedS> & S)
  74. {
  75. const auto & shoot_ray = [&aabb,&V,&F](
  76. const Eigen::Vector3f& _s,
  77. const Eigen::Vector3f& dir)->bool
  78. {
  79. Eigen::Vector3f s = _s+1e-4*dir;
  80. igl::Hit hit;
  81. return aabb.intersect_ray(
  82. V,
  83. F,
  84. s .cast<typename DerivedV::Scalar>().eval(),
  85. dir.cast<typename DerivedV::Scalar>().eval(),
  86. hit);
  87. };
  88. return ambient_occlusion(shoot_ray,P,N,num_samples,S);
  89. }
  90. template <
  91. typename DerivedV,
  92. typename DerivedF,
  93. typename DerivedP,
  94. typename DerivedN,
  95. typename DerivedS >
  96. IGL_INLINE void igl::ambient_occlusion(
  97. const Eigen::PlainObjectBase<DerivedV> & V,
  98. const Eigen::PlainObjectBase<DerivedF> & F,
  99. const Eigen::PlainObjectBase<DerivedP> & P,
  100. const Eigen::PlainObjectBase<DerivedN> & N,
  101. const int num_samples,
  102. Eigen::PlainObjectBase<DerivedS> & S)
  103. {
  104. if(F.rows() < 100)
  105. {
  106. // Super naive
  107. const auto & shoot_ray = [&V,&F](
  108. const Eigen::Vector3f& _s,
  109. const Eigen::Vector3f& dir)->bool
  110. {
  111. Eigen::Vector3f s = _s+1e-4*dir;
  112. igl::Hit hit;
  113. return ray_mesh_intersect(s,dir,V,F,hit);
  114. };
  115. return ambient_occlusion(shoot_ray,P,N,num_samples,S);
  116. }
  117. AABB<DerivedV,3> aabb;
  118. aabb.init(V,F);
  119. return ambient_occlusion(aabb,V,F,P,N,num_samples,S);
  120. }
  121. #ifdef IGL_STATIC_LIBRARY
  122. // Explicit template specialization
  123. // generated by autoexplicit.sh
  124. 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> >&);
  125. // generated by autoexplicit.sh
  126. 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> >&);
  127. // generated by autoexplicit.sh
  128. 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> >&);
  129. #endif