warping.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef WARPING_HPP
  2. #define WARPING_HPP
  3. #include <Eigen/Dense>
  4. #include <cmath>
  5. #include <random>
  6. inline Eigen::Vector3f uniform_sphere(const Eigen::Vector2f& sample){
  7. using std::acos;
  8. using std::cos;
  9. using std::sin;
  10. std::uniform_real_distribution<float> dis(0, 1);
  11. float theta = 2 * M_PI * sample(0);
  12. float phi = acos(2 * sample(1) - 1);
  13. Eigen::Vector3f ret;
  14. ret(0) = sin(phi) * cos(theta);
  15. ret(1) = sin(phi) * sin(theta);
  16. ret(2) = cos(phi);
  17. return ret;
  18. }
  19. inline Eigen::Vector3f cosine_hemisphere(const Eigen::Vector3f& normal, const Eigen::Vector2f& sample) {
  20. using std::asin;
  21. using std::cos;
  22. using std::sin;
  23. using std::sqrt;
  24. //constexpr float r = 1;
  25. //float theta = asin(sqrt(sample.y()));
  26. //float phi = 2 * M_PI * sample.x();
  27. //float z = r * cos(theta);
  28. //float _r = sqrt(r * r - z * z);
  29. //float x = _r * cos(phi);
  30. //float y = _r * sin(phi);
  31. float r = sqrt(sample.x());
  32. float phi = 2 * M_PI * sample.y();
  33. float x = r * cos(phi);
  34. float y = r * sin(phi);
  35. float z = sqrt(1 - r * r);
  36. Eigen::Vector3f notnormal = normal;
  37. if(normal.y() == 0){
  38. std::swap(notnormal.x(), notnormal.z());
  39. notnormal.z() *= -1;
  40. }
  41. else{
  42. std::swap(notnormal.x(), notnormal.y());
  43. notnormal.y() *= -1;
  44. }
  45. Eigen::Vector3f notnormal2 = normal.cross(notnormal);
  46. notnormal = notnormal2.cross(normal);
  47. Eigen::Matrix3f trf;
  48. trf.col(0) = notnormal;
  49. trf.col(1) = notnormal2;
  50. trf.col(2) = normal;
  51. Eigen::Vector3f ret(x, y, z);
  52. return Eigen::Vector3f(trf * ret);
  53. }
  54. #endif