sphere_test.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <material.h>
  12. #include <transformation.h>
  13. #include <gtest/gtest.h>
  14. TEST(SphereTest, A_ray_intersect_a_sphere_at_two_points)
  15. {
  16. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  17. Sphere s = Sphere();
  18. Intersect xs; s.intersect(r, xs);
  19. ASSERT_EQ(xs.count(), 2);
  20. ASSERT_EQ(xs[0].t, 4.0);
  21. ASSERT_EQ(xs[1].t, 6.0);
  22. }
  23. TEST(SphereTest, A_ray_intersect_a_sphere_at_a_tangent)
  24. {
  25. Ray r = Ray(Point(0, 1, -5), Vector(0, 0, 1));
  26. Sphere s = Sphere();
  27. Intersect xs; s.intersect(r, xs);
  28. ASSERT_EQ(xs.count(), 2);
  29. ASSERT_EQ(xs[0].t, 5.0);
  30. ASSERT_EQ(xs[1].t, 5.0);
  31. }
  32. TEST(SphereTest, A_ray_miss_a_sphere)
  33. {
  34. Ray r = Ray(Point(0, 2, -5), Vector(0, 0, 1));
  35. Sphere s = Sphere();
  36. Intersect xs; s.intersect(r, xs);
  37. ASSERT_EQ(xs.count(), 0);
  38. }
  39. TEST(SphereTest, A_ray_originate_inside_a_sphere)
  40. {
  41. Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
  42. Sphere s = Sphere();
  43. Intersect xs; s.intersect(r, xs);
  44. ASSERT_EQ(xs.count(), 2);
  45. ASSERT_EQ(xs[0].t, -1.0);
  46. ASSERT_EQ(xs[1].t, 1.0);
  47. }
  48. TEST(SphereTest, A_sphere_is_behind_a_ray)
  49. {
  50. Ray r = Ray(Point(0, 0, 5), Vector(0, 0, 1));
  51. Sphere s = Sphere();
  52. Intersect xs; s.intersect(r, xs);
  53. ASSERT_EQ(xs.count(), 2);
  54. ASSERT_EQ(xs[0].t, -6.0);
  55. ASSERT_EQ(xs[1].t, -4.0);
  56. }
  57. TEST(SphereTest, A_sphere_default_transformation)
  58. {
  59. Sphere s = Sphere();
  60. ASSERT_EQ(s.transformMatrix, Matrix4().identity());
  61. }
  62. TEST(SphereTest, Changing_a_sphere_transformation)
  63. {
  64. Sphere s = Sphere();
  65. Matrix t = translation(2, 3, 4);
  66. s.setTransform(t);
  67. ASSERT_EQ(s.transformMatrix, t);
  68. }
  69. TEST(SphereTest, Intersecting_a_scaled_sphere_with_a_ray)
  70. {
  71. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  72. Sphere s = Sphere();
  73. s.setTransform(scaling(2, 2, 2));
  74. Intersect xs; s.intersect(r, xs);
  75. ASSERT_EQ(xs.count(), 2);
  76. ASSERT_EQ(xs[0].t, 3.0);
  77. ASSERT_EQ(xs[1].t, 7.0);
  78. }
  79. TEST(SphereTest, Intersecting_a_translated_sphere_with_a_ray)
  80. {
  81. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  82. Sphere s = Sphere();
  83. s.setTransform(translation(5, 0, 0));
  84. Intersect xs; s.intersect(r, xs);
  85. ASSERT_EQ(xs.count(), 0);
  86. }
  87. TEST(SphereTest, The_normal_on_a_sphere_at_a_point_on_the_X_axis)
  88. {
  89. Sphere s = Sphere();
  90. Tuple n = s.normalAt(Point(1, 0, 0));
  91. ASSERT_EQ(n, Vector(1, 0, 0));
  92. }
  93. TEST(SphereTest, The_normal_on_a_sphere_at_a_point_on_the_Y_axis)
  94. {
  95. Sphere s = Sphere();
  96. Tuple n = s.normalAt(Point(0, 1, 0));
  97. ASSERT_EQ(n, Vector(0, 1, 0));
  98. }
  99. TEST(SphereTest, The_normal_on_a_sphere_at_a_point_on_the_Z_axis)
  100. {
  101. Sphere s = Sphere();
  102. Tuple n = s.normalAt(Point(0, 0, 1));
  103. ASSERT_EQ(n, Vector(0, 0, 1));
  104. }
  105. TEST(SphereTest, The_normal_on_a_sphere_at_a_nonaxial_point)
  106. {
  107. Sphere s = Sphere();
  108. Tuple n = s.normalAt(Point(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3));
  109. ASSERT_EQ(n, Vector(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3));
  110. }
  111. TEST(SphereTest, The_normal_is_a_normalise_vector)
  112. {
  113. Sphere s = Sphere();
  114. Tuple n = s.normalAt(Point(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3));
  115. ASSERT_EQ(n, n.normalise());
  116. }
  117. TEST(SphereTest, Computing_the_normal_on_a_translated_sphere)
  118. {
  119. Sphere s = Sphere();
  120. s.setTransform(translation(0, 1, 0));
  121. Tuple n = s.normalAt(Point(0, 1.70711, -0.70711));
  122. set_equal_precision(0.0001);
  123. ASSERT_EQ(n, Vector(0, 0.70711, -0.70711));
  124. /* Revert to default */
  125. set_equal_precision(FLT_EPSILON);
  126. }
  127. TEST(SphereTest, Computing_the_normal_on_a_tranformed_sphere)
  128. {
  129. Sphere s = Sphere();
  130. s.setTransform(scaling(1, 0.5, 1) * rotationZ(M_PI / 5));
  131. Tuple n = s.normalAt(Point(0, sqrt(2)/2, -sqrt(2)/2));
  132. set_equal_precision(0.0001);
  133. ASSERT_EQ(n, Vector(0, 0.97014, -0.24254));
  134. /* Revert to default */
  135. set_equal_precision(FLT_EPSILON);
  136. }
  137. TEST(SphereTest, A_sphere_have_a_default_material)
  138. {
  139. Sphere s = Sphere();
  140. Material m = Material();
  141. ASSERT_EQ(s.material, m);
  142. }
  143. TEST(SphereTest, A_sphere_may_be_assigned_a_material)
  144. {
  145. Sphere s = Sphere();
  146. Material m = Material();
  147. m.ambient = 1;
  148. s.setMaterial(m);
  149. ASSERT_EQ(s.material, m);
  150. }
  151. TEST(SphereTest, A_helper_for_producing_a_sphere_with_a_glassy_material)
  152. {
  153. GlassSphere s = GlassSphere();
  154. ASSERT_EQ(s.transformMatrix, Matrix4().identity());
  155. ASSERT_EQ(s.material.transparency, 1.0);
  156. ASSERT_EQ(s.material.refractiveIndex, 1.5);
  157. }
  158. TEST(SphereTest, The_bounding_box_of_a_sphere)
  159. {
  160. Sphere t = Sphere();
  161. Tuple sphereMin = Point(-1, -1, -1);
  162. Tuple sphereMax = Point(1, 1, 1);
  163. BoundingBox res = t.getBounds();
  164. ASSERT_EQ(res.min, sphereMin);
  165. ASSERT_EQ(res.max, sphereMax);
  166. }
  167. TEST(SphereTest, A_sphere_have_finite_bounds)
  168. {
  169. Sphere t = Sphere();
  170. ASSERT_TRUE(t.haveFiniteBounds());
  171. }