ambient_occlusion.cpp 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "ambient_occlusion.h"
  9. #include "EmbreeIntersector.h"
  10. #include <igl/random_dir.h>
  11. #include <igl/EPS.h>
  12. template <
  13. typename DerivedP,
  14. typename DerivedN,
  15. typename DerivedS >
  16. IGL_INLINE void igl::embree::ambient_occlusion(
  17. const igl::embree::EmbreeIntersector & ei,
  18. const Eigen::PlainObjectBase<DerivedP> & P,
  19. const Eigen::PlainObjectBase<DerivedN> & N,
  20. const int num_samples,
  21. Eigen::PlainObjectBase<DerivedS> & S)
  22. {
  23. using namespace Eigen;
  24. using namespace igl;
  25. const int n = P.rows();
  26. // Resize output
  27. S.resize(n,1);
  28. // Embree seems to be parallel when constructing but not when tracing rays
  29. #pragma omp parallel for
  30. // loop over mesh vertices
  31. for(int p = 0;p<n;p++)
  32. {
  33. const Vector3f origin = P.row(p).template cast<float>();
  34. const Vector3f normal = N.row(p).template cast<float>();
  35. int num_hits = 0;
  36. MatrixXf D = random_dir_stratified(num_samples).cast<float>();
  37. for(int s = 0;s<num_samples;s++)
  38. {
  39. //Vector3d d = random_dir();
  40. Vector3f d = D.row(s);
  41. if(d.dot(normal) < 0)
  42. {
  43. // reverse ray
  44. d *= -1;
  45. }
  46. igl::embree::Hit hit;
  47. const float tnear = 1e-4f;
  48. if(ei.intersectRay(origin,d,hit,tnear))
  49. {
  50. num_hits++;
  51. }
  52. }
  53. S(p) = (double)num_hits/(double)num_samples;
  54. }
  55. }
  56. template <
  57. typename DerivedV,
  58. typename DerivedF,
  59. typename DerivedP,
  60. typename DerivedN,
  61. typename DerivedS >
  62. IGL_INLINE void igl::embree::ambient_occlusion(
  63. const Eigen::PlainObjectBase<DerivedV> & V,
  64. const Eigen::PlainObjectBase<DerivedF> & F,
  65. const Eigen::PlainObjectBase<DerivedP> & P,
  66. const Eigen::PlainObjectBase<DerivedN> & N,
  67. const int num_samples,
  68. Eigen::PlainObjectBase<DerivedS> & S)
  69. {
  70. using namespace igl;
  71. using namespace Eigen;
  72. EmbreeIntersector ei;
  73. ei.init(V.template cast<float>(),F.template cast<int>());
  74. ambient_occlusion(ei,P,N,num_samples,S);
  75. }
  76. #ifdef IGL_STATIC_LIBRARY
  77. // Explicit template specialization
  78. template void igl::embree::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> >(igl::embree::EmbreeIntersector 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> >&);
  79. template void igl::embree::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> >(igl::embree::EmbreeIntersector 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> >&);
  80. template void igl::embree::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::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<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> >&);
  81. template void igl::embree::ambient_occlusion<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > 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> >&);
  82. #endif