ambient_occlusion.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <[email protected]>
  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 "parallel_for.h"
  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::Matrix<typename DerivedP::Scalar,3,1> &,
  25. const Eigen::Matrix<typename DerivedP::Scalar,3,1> &)
  26. > & shoot_ray,
  27. const Eigen::MatrixBase<DerivedP> & P,
  28. const Eigen::MatrixBase<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. // Embree seems to be parallel when constructing but not when tracing rays
  37. typedef typename DerivedP::Scalar Scalar;
  38. typedef Eigen::Matrix<Scalar,3,1> Vector3N;
  39. const Matrix<Scalar,Eigen::Dynamic,3> D = random_dir_stratified(num_samples).cast<Scalar>();
  40. const auto & inner = [&P,&N,&num_samples,&D,&S,&shoot_ray](const int p)
  41. {
  42. const Vector3N origin = P.row(p);
  43. const Vector3N normal = N.row(p);
  44. int num_hits = 0;
  45. for(int s = 0;s<num_samples;s++)
  46. {
  47. Vector3N d = D.row(s);
  48. if(d.dot(normal) < 0)
  49. {
  50. // reverse ray
  51. d *= -1;
  52. }
  53. if(shoot_ray(origin,d))
  54. {
  55. num_hits++;
  56. }
  57. }
  58. S(p) = (double)num_hits/(double)num_samples;
  59. };
  60. parallel_for(n,inner,1000);
  61. }
  62. template <
  63. typename DerivedV,
  64. int DIM,
  65. typename DerivedF,
  66. typename DerivedP,
  67. typename DerivedN,
  68. typename DerivedS >
  69. IGL_INLINE void igl::ambient_occlusion(
  70. const igl::AABB<DerivedV,DIM> & aabb,
  71. const Eigen::MatrixBase<DerivedV> & V,
  72. const Eigen::MatrixBase<DerivedF> & F,
  73. const Eigen::MatrixBase<DerivedP> & P,
  74. const Eigen::MatrixBase<DerivedN> & N,
  75. const int num_samples,
  76. Eigen::PlainObjectBase<DerivedS> & S)
  77. {
  78. typedef typename DerivedV::Scalar Scalar;
  79. using Vector3S = Eigen::Matrix<Scalar,3,1>;
  80. const auto & shoot_ray = [&aabb,&V,&F](
  81. const Eigen::Matrix<Scalar,3,1> & _s,
  82. const Eigen::Matrix<Scalar,3,1> & dir)->bool
  83. {
  84. Vector3S s = _s+1e-4*dir;
  85. igl::Hit<Scalar> hit;
  86. return aabb.intersect_ray(
  87. V,
  88. F,
  89. s,
  90. dir,
  91. hit);
  92. };
  93. return ambient_occlusion(shoot_ray,P,N,num_samples,S);
  94. }
  95. template <
  96. typename DerivedV,
  97. typename DerivedF,
  98. typename DerivedP,
  99. typename DerivedN,
  100. typename DerivedS >
  101. IGL_INLINE void igl::ambient_occlusion(
  102. const Eigen::MatrixBase<DerivedV> & V,
  103. const Eigen::MatrixBase<DerivedF> & F,
  104. const Eigen::MatrixBase<DerivedP> & P,
  105. const Eigen::MatrixBase<DerivedN> & N,
  106. const int num_samples,
  107. Eigen::PlainObjectBase<DerivedS> & S)
  108. {
  109. typedef typename DerivedV::Scalar Scalar;
  110. using Vector3S = Eigen::Matrix<Scalar,3,1>;
  111. if(F.rows() < 100)
  112. {
  113. // Super naive
  114. const auto & shoot_ray = [&V,&F](
  115. const Eigen::Matrix<Scalar,3,1> & _s,
  116. const Eigen::Matrix<Scalar,3,1> & dir)->bool
  117. {
  118. Vector3S s = _s+1e-4*dir;
  119. igl::Hit<Scalar> hit;
  120. return ray_mesh_intersect(s,dir,V,F,hit);
  121. };
  122. return ambient_occlusion(shoot_ray,P,N,num_samples,S);
  123. }
  124. AABB<DerivedV,3> aabb;
  125. aabb.init(V,F);
  126. return ambient_occlusion(aabb,V,F,P,N,num_samples,S);
  127. }
  128. #ifdef IGL_STATIC_LIBRARY
  129. // Explicit template instantiation
  130. template void igl::ambient_occlusion<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  131. 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<double, 3, 1, 0, 3, 1> const&, Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  132. 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<double, 3, 1, 0, 3, 1> const&, Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  133. 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<double, 3, 1, 0, 3, 1> const&, Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  134. 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<double, 3, 1, 0, 3, 1> const&, Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  135. template void igl::ambient_occlusion<Eigen::Matrix<float, 1, 3, 1, 1, 3>, Eigen::Matrix<float, 1, 3, 1, 1, 3>, Eigen::Matrix<float, -1, 1, 0, -1, 1>>(std::function<bool (Eigen::Matrix<Eigen::Matrix<float, 1, 3, 1, 1, 3>::Scalar, 3, 1, 0, 3, 1> const&, Eigen::Matrix<Eigen::Matrix<float, 1, 3, 1, 1, 3>::Scalar, 3, 1, 0, 3, 1> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3>> const&, Eigen::MatrixBase<Eigen::Matrix<float, 1, 3, 1, 1, 3>> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1>>&);
  136. template void igl::ambient_occlusion<Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<float, -1, 1, 0, -1, 1>>(std::function<bool (Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, 3, 1, 0, 3, 1> const&, Eigen::Matrix<Eigen::Matrix<float, -1, 3, 0, -1, 3>::Scalar, 3, 1, 0, 3, 1> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 0, -1, 3>> const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 0, -1, 3>> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1>>&);
  137. template void igl::ambient_occlusion<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, 1, 0, -1, 1>>(std::function<bool (Eigen::Matrix<Eigen::Matrix<float, -1, -1, 0, -1, -1>::Scalar, 3, 1, 0, 3, 1> const&, Eigen::Matrix<Eigen::Matrix<float, -1, -1, 0, -1, -1>::Scalar, 3, 1, 0, 3, 1> const&)> const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1>> const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1>> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1>>&);
  138. #endif