ray_test.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Ray unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <ray.h>
  10. #include <transformation.h>
  11. #include <shape.h>
  12. #include <gtest/gtest.h>
  13. TEST(RayTest, Creating_a_ray_and_querying_it)
  14. {
  15. Point origin = Point(1, 2, 3);
  16. Vector direction = Vector(4, 5, 6);
  17. Ray r = Ray(origin, direction);
  18. ASSERT_EQ(r.origin, origin);
  19. ASSERT_EQ(r.direction, direction);
  20. }
  21. TEST(RayTest, Computing_a_point_from_a_distance)
  22. {
  23. Ray r = Ray(Point(2, 3, 4), Vector(1, 0, 0));
  24. ASSERT_EQ(r.position(0), Point(2, 3, 4));
  25. ASSERT_EQ(r.position(1), Point(3, 3, 4));
  26. ASSERT_EQ(r.position(-1), Point(1, 3, 4));
  27. ASSERT_EQ(r.position(2.5), Point(4.5, 3, 4));
  28. }
  29. TEST(RayTest, Translating_a_ray)
  30. {
  31. Ray r = Ray(Point(1, 2, 3), Vector(0, 1, 0));
  32. Matrix m = translation(3, 4, 5);
  33. Shape o = Shape();
  34. o.setTransform(m);
  35. Ray r2 = o.transform(r);
  36. ASSERT_EQ(r2.origin, Point(4, 6, 8));
  37. ASSERT_EQ(r2.direction, Vector(0, 1, 0));
  38. }
  39. TEST(RayTest, Scaling_a_ray)
  40. {
  41. Ray r = Ray(Point(1, 2, 3), Vector(0, 1, 0));
  42. Matrix m = scaling(2, 3, 4);
  43. Shape o = Shape();
  44. o.setTransform(m);
  45. Ray r2 = o.transform(r);
  46. ASSERT_EQ(r2.origin, Point(2, 6, 12));
  47. ASSERT_EQ(r2.direction, Vector(0, 3, 0));
  48. }