intersect_test.cpp 3.9 KB

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