intersect_test.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Intersect unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <intersect.h>
  10. #include <intersection.h>
  11. #include <sphere.h>
  12. #include <plane.h>
  13. #include <transformation.h>
  14. #include <gtest/gtest.h>
  15. TEST(IntersectTest, Creating_an_intersect_and_do_some_check)
  16. {
  17. Intersect i;
  18. ASSERT_EQ(i.count(), 0);
  19. i.add(Intersection(1.0, nullptr));
  20. i.add(Intersection(4.2, nullptr));
  21. ASSERT_EQ(i.count(), 2);
  22. ASSERT_EQ(i[0].t, 1.0);
  23. ASSERT_EQ(i[1].t, 4.2);
  24. }
  25. TEST(IntersectTest, An_intersection_encapsulate_t_and_object)
  26. {
  27. Sphere s = Sphere();
  28. Intersection i = Intersection(3.5, &s);
  29. ASSERT_EQ(i.t, 3.5);
  30. ASSERT_EQ(i.object, (Shape *)&s);
  31. }
  32. TEST(IntersectTest, Aggregating_intersections)
  33. {
  34. Sphere s = Sphere();
  35. Intersection i1 = Intersection(1, &s);
  36. Intersection i2 = Intersection(2, &s);
  37. Intersect xs = Intersect();
  38. xs.add(i1);
  39. xs.add(i2);
  40. ASSERT_EQ(xs.count(), 2);
  41. ASSERT_EQ(xs[0].t, 1);
  42. ASSERT_EQ(xs[1].t, 2);
  43. }
  44. TEST(IntersectTest, Intersect_sets_the_object_on_the_intersection)
  45. {
  46. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  47. Sphere s = Sphere();
  48. Intersect xs = s.intersect(r);
  49. ASSERT_EQ(xs.count(), 2);
  50. ASSERT_EQ(xs[0].object, (Shape *)&s);
  51. ASSERT_EQ(xs[1].object, (Shape *)&s);
  52. }
  53. TEST(IntersectTest, The_hit_when_all_intersection_have_positive_t)
  54. {
  55. Sphere s = Sphere();
  56. Intersect xs = Intersect();
  57. Intersection i1 = Intersection(1, &s);
  58. Intersection i2 = Intersection(2, &s);
  59. xs.add(i1);
  60. xs.add(i2);
  61. Intersection i = xs.hit();
  62. ASSERT_EQ(i, i1);
  63. }
  64. TEST(IntersectTest, The_hit_when_some_intersection_have_negative_t)
  65. {
  66. Sphere s = Sphere();
  67. Intersect xs = Intersect();
  68. Intersection i1 = Intersection(-1, &s);
  69. Intersection i2 = Intersection(2, &s);
  70. Intersection i3 = Intersection(12, &s);
  71. xs.add(i1);
  72. xs.add(i2);
  73. xs.add(i3);
  74. Intersection i = xs.hit();
  75. ASSERT_EQ(i, i2);
  76. }
  77. TEST(IntersectTest, The_hit_when_all_intersection_have_negative_t)
  78. {
  79. Sphere s = Sphere();
  80. Intersect xs = Intersect();
  81. Intersection i1 = Intersection(-2, &s);
  82. Intersection i2 = Intersection(-1, &s);
  83. xs.add(i1);
  84. xs.add(i2);
  85. Intersection i = xs.hit();
  86. ASSERT_TRUE(i.nothing());
  87. }
  88. TEST(IntersectTest, The_hit_is_always_the_lowest_nonnegative_intersection)
  89. {
  90. Sphere s = Sphere();
  91. Intersect xs = Intersect();
  92. Intersection i1 = Intersection(5, &s);
  93. Intersection i2 = Intersection(7, &s);
  94. Intersection i3 = Intersection(-3, &s);
  95. Intersection i4 = Intersection(2, &s);
  96. xs.add(i1);
  97. xs.add(i2);
  98. xs.add(i3);
  99. xs.add(i4);
  100. Intersection i = xs.hit();
  101. ASSERT_EQ(i, i4);
  102. }
  103. TEST(IntersectTest, Precomputing_the_state_of_an_intersection)
  104. {
  105. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  106. Sphere shape = Sphere();
  107. Intersection i = Intersection(4, &shape);
  108. Computation comps = i.prepareComputation(r);
  109. ASSERT_EQ(comps.t, i.t);
  110. ASSERT_EQ(comps.object, i.object);
  111. ASSERT_EQ(comps.hitPoint, Point(0, 0, -1));
  112. ASSERT_EQ(comps.eyeVector, Vector(0, 0, -1));
  113. ASSERT_EQ(comps.normalVector, Vector(0, 0, -1));
  114. }
  115. TEST(IntersectTest, The_hit_when_an_intersection_occurs_on_the_outside)
  116. {
  117. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  118. Sphere shape = Sphere();
  119. Intersection i = Intersection(4, &shape);
  120. Computation comps = i.prepareComputation(r);
  121. ASSERT_EQ(comps.inside, false);
  122. }
  123. TEST(IntersectTest, The_hit_when_an_intersection_occurs_on_the_inside)
  124. {
  125. Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
  126. Sphere shape = Sphere();
  127. Intersection i = Intersection(1, &shape);
  128. Computation comps = i.prepareComputation(r);
  129. ASSERT_EQ(comps.hitPoint, Point(0, 0, 1));
  130. ASSERT_EQ(comps.eyeVector, Vector(0, 0, -1));
  131. ASSERT_EQ(comps.inside, true);
  132. /* Normal vector would have been (0, 0, 1); but is inverted ! */
  133. ASSERT_EQ(comps.normalVector, Vector(0, 0, -1));
  134. }
  135. TEST(IntersectTest, The_hit_should_offset_the_point)
  136. {
  137. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  138. Sphere shape = Sphere();
  139. shape.setTransform(translation(0, 0, 1));
  140. Intersection i = Intersection(5, &shape);
  141. Computation comps = i.prepareComputation(r);
  142. /* Normal vector would have been (0, 0, 1); but is inverted ! */
  143. ASSERT_LT(comps.overHitPoint.z, -getEpsilon() / 2);
  144. ASSERT_GT(comps.hitPoint.z, comps.overHitPoint.z);
  145. }
  146. TEST(IntersectTest, Precomputing_the_reflection_vector)
  147. {
  148. Plane s = Plane();
  149. Ray r = Ray(Point(0, 1, -1), Vector(0, -sqrt(2) / 2, sqrt(2) / 2));
  150. Intersection i = Intersection(sqrt(2), &s);
  151. Computation comps = i.prepareComputation(r);
  152. ASSERT_EQ(comps.reflectVector, Vector(0, sqrt(2) / 2, sqrt(2) / 2));
  153. }
  154. TEST(IntersectTest, Finding_n1_and_n2_at_various_intersections)
  155. {
  156. int i;
  157. double n1_res[6] = { 1.0, 1.5, 2.0, 2.5, 2.5, 1.5 };
  158. double n2_res[6] = { 1.5, 2.0, 2.5, 2.5, 1.5, 1.0 };
  159. GlassSphere A = GlassSphere();
  160. A.setTransform(scaling(2, 2, 2));
  161. A.material.refractiveIndex = 1.5;
  162. GlassSphere B = GlassSphere();
  163. B.setTransform(translation(0, 0, -0.25));
  164. B.material.refractiveIndex = 2.0;
  165. GlassSphere C = GlassSphere();
  166. C.setTransform(translation(0, 0, 0.25));
  167. C.material.refractiveIndex = 2.5;
  168. Ray r = Ray(Point(0, 0, -4), Vector(0, 0, 1));
  169. Intersect xs = Intersect();
  170. xs.add(Intersection(2.0, &A));
  171. xs.add(Intersection(2.75, &B));
  172. xs.add(Intersection(3.25, &C));
  173. xs.add(Intersection(4.75, &B));
  174. xs.add(Intersection(5.25, &C));
  175. xs.add(Intersection(6, &A));
  176. for(i = 0; i < xs.count(); i++)
  177. {
  178. Intersection inter = xs[i];
  179. Computation comps = inter.prepareComputation(r, &xs);
  180. ASSERT_EQ(comps.n1, n1_res[i]);
  181. ASSERT_EQ(comps.n2, n2_res[i]);
  182. }
  183. }
  184. TEST(IntersectTest, The_under_point_is_offset_below_the_surface)
  185. {
  186. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  187. GlassSphere shape = GlassSphere();
  188. shape.setTransform(translation(0, 0, 1));
  189. Intersection i = Intersection(5, &shape);
  190. Intersect xs = Intersect();
  191. xs.add(i);
  192. Computation comps = i.prepareComputation(r, &xs);
  193. ASSERT_TRUE(double_equal(comps.underHitPoint.z, getEpsilon() / 2));
  194. ASSERT_LT(comps.hitPoint.z, comps.underHitPoint.z);
  195. }
  196. TEST(IntersectTest, The_Schlick_approximation_under_total_internal_reflection)
  197. {
  198. GlassSphere shape = GlassSphere();
  199. Ray r = Ray(Point(0, 0, sqrt(2)/2), Vector(0, 1, 0));
  200. Intersect xs = Intersect();
  201. xs.add(Intersection(-sqrt(2)/2, &shape));
  202. xs.add(Intersection(sqrt(2)/2, &shape));
  203. Computation comps = xs[1].prepareComputation(r, &xs);
  204. double reflectance = comps.schlick();
  205. ASSERT_EQ(reflectance, 1.0);
  206. }
  207. TEST(IntersectTest, The_Schlick_approximation_with_a_perpendicular_viewing_angle)
  208. {
  209. GlassSphere shape = GlassSphere();
  210. Ray r = Ray(Point(0, 0, 0), Vector(0, 1, 0));
  211. Intersect xs = Intersect();
  212. xs.add(Intersection(-1, &shape));
  213. xs.add(Intersection(1, &shape));
  214. Computation comps = xs[1].prepareComputation(r, &xs);
  215. double reflectance = comps.schlick();
  216. ASSERT_TRUE(double_equal(reflectance, 0.04));
  217. }
  218. TEST(IntersectTest, The_Schlick_approximation_with_small_angle_and_n2_gt_n1)
  219. {
  220. GlassSphere shape = GlassSphere();
  221. Ray r = Ray(Point(0, 0.99, -2), Vector(0, 0, 1));
  222. Intersect xs = Intersect();
  223. xs.add(Intersection(1.8589, &shape));
  224. Computation comps = xs[0].prepareComputation(r, &xs);
  225. double reflectance = comps.schlick();
  226. /* Temporary lower the precision */
  227. set_equal_precision(0.00001);
  228. ASSERT_TRUE(double_equal(reflectance, 0.48873));
  229. set_equal_precision(FLT_EPSILON);
  230. }