Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #include "primitives\square-primitive.h"
  2.  
  3. using namespace raytracer;
  4. using namespace math;
  5.  
  6. namespace
  7. {
  8.  
  9. class CoordinateSquareImplementation : public raytracer::primitives::_private_::PrimitiveImplementation
  10. {
  11. protected:
  12. const Vector3D m_normal;
  13.  
  14. CoordinateSquareImplementation(const Vector3D& normal)
  15. : m_normal(normal)
  16. {
  17. assert(normal.is_unit());
  18. }
  19.  
  20. virtual void initialize_hit(Hit* hit, const Ray& ray, double t) const = 0;
  21.  
  22. public:
  23. std::vector<std::shared_ptr<Hit>> find_all_hits(const math::Ray& ray) const override
  24. {
  25. std::vector<std::shared_ptr<Hit>> hits;
  26. double denom = ray.direction.dot(m_normal);
  27.  
  28. if (denom != approx(0.0))
  29. {
  30. // Compute numerator
  31. double numer = -((ray.origin - Point3D(0, 0, 0)).dot(m_normal));
  32.  
  33. // Compute t
  34. double t = numer / denom;
  35.  
  36. // Create hit object
  37. auto hit = std::make_shared<Hit>();
  38.  
  39. // shared_ptr<T>::get() returns the T* inside the shared pointer
  40. initialize_hit(hit.get(), ray, t);
  41.  
  42. // Put hit in list
  43. hits.push_back(hit);
  44. }
  45.  
  46. return hits;
  47. }
  48. };
  49.  
  50. class SquareXYImplementation : public CoordinateSquareImplementation
  51. {
  52. public:
  53. SquareXYImplementation()
  54. : CoordinateSquareImplementation(Vector3D(0, 0, 1))
  55. {
  56.  
  57. }
  58.  
  59. math::Box bounding_box() const override
  60. {
  61. return Box(interval(-1.0, 1.0), interval(-1.0, 1.0), interval(-0.01, 0.01));
  62. }
  63.  
  64. std::vector<std::shared_ptr<Hit>> find_all_hits(const math::Ray& ray) const override
  65. {
  66. std::vector<std::shared_ptr<Hit>> hits;
  67. double denom = ray.direction.dot(m_normal);
  68.  
  69. if (denom != approx(0.0))
  70. {
  71. // Compute numerator
  72. double numer = -((ray.origin - Point3D(0, 0, 0)).dot(m_normal));
  73.  
  74. // Compute t
  75. double t = numer / denom;
  76.  
  77. // Create hit object
  78. auto hit = std::make_shared<Hit>();
  79.  
  80. // shared_ptr<T>::get() returns the T* inside the shared pointer
  81. initialize_hit(hit.get(), ray, t);
  82.  
  83. if (hit->position.x() >= -1 && hit->position.x() <= 1) {
  84. if (hit->position.y() >= -1 && hit->position.y() <= 1) {
  85. // Put hit in list
  86. hits.push_back(hit);
  87. }
  88. }
  89.  
  90.  
  91. }
  92.  
  93. return hits;
  94. }
  95.  
  96. protected:
  97. void initialize_hit(Hit* hit, const Ray& ray, double t) const override
  98. {
  99. hit->t = t;
  100. hit->position = ray.at(hit->t);
  101. hit->local_position.xyz = hit->position;
  102. hit->local_position.uv = Point2D(hit->position.x(), hit->position.y());
  103. hit->normal = ray.origin.z() > 0 ? m_normal : -m_normal;
  104. }
  105. };
  106.  
  107.  
  108. }
  109.  
  110. Primitive raytracer::primitives::xy_square()
  111. {
  112. return Primitive(std::make_shared<SquareXYImplementation>());
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement