ambient_occlusion.cpp 4.3 KB

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