sphere_test.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <transformation.h>
  12. #include <gtest/gtest.h>
  13. TEST(SphereTest, A_ray_intersect_a_sphere_at_two_points)
  14. {
  15. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  16. Sphere s = Sphere();
  17. Intersect xs = s.intersect(r);
  18. ASSERT_EQ(xs.count(), 2);
  19. ASSERT_EQ(xs[0].t, 4.0);
  20. ASSERT_EQ(xs[1].t, 6.0);
  21. }
  22. TEST(SphereTest, A_ray_intersect_a_sphere_at_a_tangent)
  23. {
  24. Ray r = Ray(Point(0, 1, -5), Vector(0, 0, 1));
  25. Sphere s = Sphere();
  26. Intersect xs = s.intersect(r);
  27. ASSERT_EQ(xs.count(), 2);
  28. ASSERT_EQ(xs[0].t, 5.0);
  29. ASSERT_EQ(xs[1].t, 5.0);
  30. }
  31. TEST(SphereTest, A_ray_miss_a_sphere)
  32. {
  33. Ray r = Ray(Point(0, 2, -5), Vector(0, 0, 1));
  34. Sphere s = Sphere();
  35. Intersect xs = s.intersect(r);
  36. ASSERT_EQ(xs.count(), 0);
  37. }
  38. TEST(SphereTest, A_ray_originate_inside_a_sphere)
  39. {
  40. Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
  41. Sphere s = Sphere();
  42. Intersect xs = s.intersect(r);
  43. ASSERT_EQ(xs.count(), 2);
  44. ASSERT_EQ(xs[0].t, -1.0);
  45. ASSERT_EQ(xs[1].t, 1.0);
  46. }
  47. TEST(SphereTest, A_sphere_is_behind_a_ray)
  48. {
  49. Ray r = Ray(Point(0, 0, 5), Vector(0, 0, 1));
  50. Sphere s = Sphere();
  51. Intersect xs = s.intersect(r);
  52. ASSERT_EQ(xs.count(), 2);
  53. ASSERT_EQ(xs[0].t, -6.0);
  54. ASSERT_EQ(xs[1].t, -4.0);
  55. }
  56. TEST(SphereTest, A_sphere_default_transformation)
  57. {
  58. Sphere s = Sphere();
  59. ASSERT_EQ(s.transformMatrix, Matrix4().identity());
  60. }
  61. TEST(SphereTest, Changing_a_sphere_transformation)
  62. {
  63. Sphere s = Sphere();
  64. Matrix t = translation(2, 3, 4);
  65. s.setTransform(t);
  66. ASSERT_EQ(s.transformMatrix, t);
  67. }
  68. TEST(SphereTest, Intersecting_a_scaled_sphere_with_a_ray)
  69. {
  70. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  71. Sphere s = Sphere();
  72. s.setTransform(scaling(2, 2, 2));
  73. Intersect xs = s.intersect(r);
  74. ASSERT_EQ(xs.count(), 2);
  75. ASSERT_EQ(xs[0].t, 3.0);
  76. ASSERT_EQ(xs[1].t, 7.0);
  77. }
  78. TEST(SphereTest, Intersecting_a_translated_sphere_with_a_ray)
  79. {
  80. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  81. Sphere s = Sphere();
  82. s.setTransform(translation(5, 0, 0));
  83. Intersect xs = s.intersect(r);
  84. ASSERT_EQ(xs.count(), 0);
  85. }