ambient_occlusion.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "ambient_occlusion.h"
  2. #include "EmbreeIntersector.h"
  3. #include <igl/random_dir.h>
  4. template <
  5. typename PointMatrixType,
  6. typename FaceMatrixType,
  7. typename RowVector3,
  8. typename DerivedP,
  9. typename DerivedN,
  10. typename DerivedS >
  11. void igl::ambient_occlusion(
  12. const igl::EmbreeIntersector<PointMatrixType,FaceMatrixType,RowVector3> & ei,
  13. const Eigen::PlainObjectBase<DerivedP> & P,
  14. const Eigen::PlainObjectBase<DerivedN> & N,
  15. const int num_samples,
  16. Eigen::PlainObjectBase<DerivedS> & S)
  17. {
  18. using namespace Eigen;
  19. using namespace igl;
  20. const int n = P.rows();
  21. // Resize output
  22. S.resize(n,1);
  23. // Embree seems to be parallel when constructing but not when tracing rays
  24. #pragma omp parallel for
  25. // loop over mesh vertices
  26. for(int p = 0;p<n;p++)
  27. {
  28. const Vector3d origin = P.row(p);
  29. const Vector3d normal = N.row(p);
  30. int num_hits = 0;
  31. MatrixXd D = random_dir_stratified(num_samples);
  32. for(int s = 0;s<num_samples;s++)
  33. {
  34. //Vector3d d = random_dir();
  35. Vector3d d = D.row(s);
  36. if(d.dot(normal) < 0)
  37. {
  38. // reverse ray
  39. d *= -1;
  40. }
  41. embree::Hit hit;
  42. if(ei.intersectRay(origin,d,hit))
  43. {
  44. num_hits++;
  45. }
  46. }
  47. S(p) = (double)num_hits/(double)num_samples;
  48. }
  49. }
  50. #ifndef IGL_HEADER_ONLY
  51. // Explicit template instanciation
  52. template void igl::ambient_occlusion<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 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> >(igl::EmbreeIntersector<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, const int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  53. #endif