sphere_test.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Sphere unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <ray.h>
  10. #include <sphere.h>
  11. #include <gtest/gtest.h>
  12. TEST(SphereTest, A_ray_intersect_a_sphere_at_two_points)
  13. {
  14. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  15. Sphere s = Sphere();
  16. Intersect xs = s.intersect(r);
  17. ASSERT_EQ(xs.count(), 2);
  18. ASSERT_EQ(xs[0]->t, 4.0);
  19. ASSERT_EQ(xs[1]->t, 6.0);
  20. }
  21. TEST(SphereTest, A_ray_intersect_a_sphere_at_a_tangent)
  22. {
  23. Ray r = Ray(Point(0, 1, -5), Vector(0, 0, 1));
  24. Sphere s = Sphere();
  25. Intersect xs = s.intersect(r);
  26. ASSERT_EQ(xs.count(), 2);
  27. ASSERT_EQ(xs[0]->t, 5.0);
  28. ASSERT_EQ(xs[1]->t, 5.0);
  29. }
  30. TEST(SphereTest, A_ray_miss_a_sphere)
  31. {
  32. Ray r = Ray(Point(0, 2, -5), Vector(0, 0, 1));
  33. Sphere s = Sphere();
  34. Intersect xs = s.intersect(r);
  35. ASSERT_EQ(xs.count(), 0);
  36. }
  37. TEST(SphereTest, A_ray_originate_inside_a_sphere)
  38. {
  39. Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
  40. Sphere s = Sphere();
  41. Intersect xs = s.intersect(r);
  42. ASSERT_EQ(xs.count(), 2);
  43. ASSERT_EQ(xs[0]->t, -1.0);
  44. ASSERT_EQ(xs[1]->t, 1.0);
  45. }
  46. TEST(SphereTest, A_sphere_is_behind_a_ray)
  47. {
  48. Ray r = Ray(Point(0, 0, 5), Vector(0, 0, 1));
  49. Sphere s = Sphere();
  50. Intersect xs = s.intersect(r);
  51. ASSERT_EQ(xs.count(), 2);
  52. ASSERT_EQ(xs[0]->t, -6.0);
  53. ASSERT_EQ(xs[1]->t, -4.0);
  54. }