ray_test.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <testshape.h>
  13. #include <gtest/gtest.h>
  14. TEST(RayTest, Creating_a_ray_and_querying_it)
  15. {
  16. Point origin = Point(1, 2, 3);
  17. Vector direction = Vector(4, 5, 6);
  18. Ray r = Ray(origin, direction);
  19. ASSERT_EQ(r.origin, origin);
  20. ASSERT_EQ(r.direction, direction);
  21. }
  22. TEST(RayTest, Computing_a_point_from_a_distance)
  23. {
  24. Ray r = Ray(Point(2, 3, 4), Vector(1, 0, 0));
  25. ASSERT_EQ(r.position(0), Point(2, 3, 4));
  26. ASSERT_EQ(r.position(1), Point(3, 3, 4));
  27. ASSERT_EQ(r.position(-1), Point(1, 3, 4));
  28. ASSERT_EQ(r.position(2.5), Point(4.5, 3, 4));
  29. }
  30. TEST(RayTest, Translating_a_ray)
  31. {
  32. Ray r = Ray(Point(1, 2, 3), Vector(0, 1, 0));
  33. Matrix m = translation(3, 4, 5);
  34. TestShape o = TestShape();
  35. o.setTransform(m);
  36. Ray r2 = o.transform(r);
  37. ASSERT_EQ(r2.origin, Point(4, 6, 8));
  38. ASSERT_EQ(r2.direction, Vector(0, 1, 0));
  39. }
  40. TEST(RayTest, Scaling_a_ray)
  41. {
  42. Ray r = Ray(Point(1, 2, 3), Vector(0, 1, 0));
  43. Matrix m = scaling(2, 3, 4);
  44. TestShape o = TestShape();
  45. o.setTransform(m);
  46. Ray r2 = o.transform(r);
  47. ASSERT_EQ(r2.origin, Point(2, 6, 12));
  48. ASSERT_EQ(r2.direction, Vector(0, 3, 0));
  49. }