intersect_test.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <transformation.h>
  13. #include <gtest/gtest.h>
  14. TEST(IntersectTest, Creating_an_intersect_and_do_some_check)
  15. {
  16. Intersect i;
  17. ASSERT_EQ(i.count(), 0);
  18. i.add(Intersection(1.0, nullptr));
  19. i.add(Intersection(4.2, nullptr));
  20. ASSERT_EQ(i.count(), 2);
  21. ASSERT_EQ(i[0].t, 1.0);
  22. ASSERT_EQ(i[1].t, 4.2);
  23. }
  24. TEST(IntersectTest, An_intersection_encapsulate_t_and_object)
  25. {
  26. Sphere s = Sphere();
  27. Intersection i = Intersection(3.5, &s);
  28. ASSERT_EQ(i.t, 3.5);
  29. ASSERT_EQ(i.object, (Shape *)&s);
  30. }
  31. TEST(IntersectTest, Aggregating_intersections)
  32. {
  33. Sphere s = Sphere();
  34. Intersection i1 = Intersection(1, &s);
  35. Intersection i2 = Intersection(2, &s);
  36. Intersect xs = Intersect();
  37. xs.add(i1);
  38. xs.add(i2);
  39. ASSERT_EQ(xs.count(), 2);
  40. ASSERT_EQ(xs[0].t, 1);
  41. ASSERT_EQ(xs[1].t, 2);
  42. }
  43. TEST(IntersectTest, Intersect_sets_the_object_on_the_intersection)
  44. {
  45. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  46. Sphere s = Sphere();
  47. Intersect xs = s.intersect(r);
  48. ASSERT_EQ(xs.count(), 2);
  49. ASSERT_EQ(xs[0].object, (Shape *)&s);
  50. ASSERT_EQ(xs[1].object, (Shape *)&s);
  51. }
  52. TEST(IntersectTest, The_hit_when_all_intersection_have_positive_t)
  53. {
  54. Sphere s = Sphere();
  55. Intersect xs = Intersect();
  56. Intersection i1 = Intersection(1, &s);
  57. Intersection i2 = Intersection(2, &s);
  58. xs.add(i1);
  59. xs.add(i2);
  60. Intersection i = xs.hit();
  61. ASSERT_EQ(i, i1);
  62. }
  63. TEST(IntersectTest, The_hit_when_some_intersection_have_negative_t)
  64. {
  65. Sphere s = Sphere();
  66. Intersect xs = Intersect();
  67. Intersection i1 = Intersection(-1, &s);
  68. Intersection i2 = Intersection(2, &s);
  69. Intersection i3 = Intersection(12, &s);
  70. xs.add(i1);
  71. xs.add(i2);
  72. xs.add(i3);
  73. Intersection i = xs.hit();
  74. ASSERT_EQ(i, i2);
  75. }
  76. TEST(IntersectTest, The_hit_when_all_intersection_have_negative_t)
  77. {
  78. Sphere s = Sphere();
  79. Intersect xs = Intersect();
  80. Intersection i1 = Intersection(-2, &s);
  81. Intersection i2 = Intersection(-1, &s);
  82. xs.add(i1);
  83. xs.add(i2);
  84. Intersection i = xs.hit();
  85. ASSERT_TRUE(i.nothing());
  86. }
  87. TEST(IntersectTest, The_hit_is_always_the_lowest_nonnegative_intersection)
  88. {
  89. Sphere s = Sphere();
  90. Intersect xs = Intersect();
  91. Intersection i1 = Intersection(5, &s);
  92. Intersection i2 = Intersection(7, &s);
  93. Intersection i3 = Intersection(-3, &s);
  94. Intersection i4 = Intersection(2, &s);
  95. xs.add(i1);
  96. xs.add(i2);
  97. xs.add(i3);
  98. xs.add(i4);
  99. Intersection i = xs.hit();
  100. ASSERT_EQ(i, i4);
  101. }
  102. TEST(IntersectTest, Precomputing_the_state_of_an_intersection)
  103. {
  104. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  105. Sphere shape = Sphere();
  106. Intersection i = Intersection(4, &shape);
  107. Computation comps = i.prepareComputation(r);
  108. ASSERT_EQ(comps.t, i.t);
  109. ASSERT_EQ(comps.object, i.object);
  110. ASSERT_EQ(comps.hitPoint, Point(0, 0, -1));
  111. ASSERT_EQ(comps.eyeVector, Vector(0, 0, -1));
  112. ASSERT_EQ(comps.normalVector, Vector(0, 0, -1));
  113. }
  114. TEST(IntersectTest, The_hit_when_an_intersection_occurs_on_the_outside)
  115. {
  116. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  117. Sphere shape = Sphere();
  118. Intersection i = Intersection(4, &shape);
  119. Computation comps = i.prepareComputation(r);
  120. ASSERT_EQ(comps.inside, false);
  121. }
  122. TEST(IntersectTest, The_hit_when_an_intersection_occurs_on_the_inside)
  123. {
  124. Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
  125. Sphere shape = Sphere();
  126. Intersection i = Intersection(1, &shape);
  127. Computation comps = i.prepareComputation(r);
  128. ASSERT_EQ(comps.hitPoint, Point(0, 0, 1));
  129. ASSERT_EQ(comps.eyeVector, Vector(0, 0, -1));
  130. ASSERT_EQ(comps.inside, true);
  131. /* Normal vector would have been (0, 0, 1); but is inverted ! */
  132. ASSERT_EQ(comps.normalVector, Vector(0, 0, -1));
  133. }
  134. TEST(IntersectTest, The_hit_should_offset_the_point)
  135. {
  136. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  137. Sphere shape = Sphere();
  138. shape.setTransform(translation(0, 0, 1));
  139. Intersection i = Intersection(5, &shape);
  140. Computation comps = i.prepareComputation(r);
  141. /* Normal vector would have been (0, 0, 1); but is inverted ! */
  142. ASSERT_LT(comps.overHitPoint.z, -getEpsilon() / 2);
  143. ASSERT_GT(comps.hitPoint.z, comps.overHitPoint.z);
  144. }