intersect_test.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <sphere.h>
  11. #include <gtest/gtest.h>
  12. TEST(IntersectTest, Creating_an_intersect_and_do_some_check)
  13. {
  14. Intersect i;
  15. ASSERT_EQ(i.count(), 0);
  16. i.add(Intersection(1.0, nullptr));
  17. i.add(Intersection(4.2, nullptr));
  18. ASSERT_EQ(i.count(), 2);
  19. ASSERT_EQ(i[0].t, 1.0);
  20. ASSERT_EQ(i[1].t, 4.2);
  21. }
  22. TEST(IntersectTest, An_intersection_encapsulate_t_and_object)
  23. {
  24. Sphere s = Sphere();
  25. Intersection i = Intersection(3.5, &s);
  26. ASSERT_EQ(i.t, 3.5);
  27. ASSERT_EQ(i.object, (Object *)&s);
  28. }
  29. TEST(IntersectTest, Aggregating_intersections)
  30. {
  31. Sphere s = Sphere();
  32. Intersection i1 = Intersection(1, &s);
  33. Intersection i2 = Intersection(2, &s);
  34. Intersect xs = Intersect();
  35. xs.add(i1);
  36. xs.add(i2);
  37. ASSERT_EQ(xs.count(), 2);
  38. ASSERT_EQ(xs[0].t, 1);
  39. ASSERT_EQ(xs[1].t, 2);
  40. }
  41. TEST(IntersectTest, Intersect_sets_the_object_on_the_intersection)
  42. {
  43. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  44. Sphere s = Sphere();
  45. Intersect xs = s.intersect(r);
  46. ASSERT_EQ(xs.count(), 2);
  47. ASSERT_EQ(xs[0].object, (Object *)&s);
  48. ASSERT_EQ(xs[1].object, (Object *)&s);
  49. }
  50. TEST(IntersectTest, The_hit_when_all_intersection_have_positive_t)
  51. {
  52. Sphere s = Sphere();
  53. Intersect xs = Intersect();
  54. Intersection i1 = Intersection(1, &s);
  55. Intersection i2 = Intersection(2, &s);
  56. xs.add(i1);
  57. xs.add(i2);
  58. Intersection i = xs.hit();
  59. ASSERT_EQ(i, i1);
  60. }
  61. TEST(IntersectTest, The_hit_when_some_intersection_have_negative_t)
  62. {
  63. Sphere s = Sphere();
  64. Intersect xs = Intersect();
  65. Intersection i1 = Intersection(-1, &s);
  66. Intersection i2 = Intersection(2, &s);
  67. Intersection i3 = Intersection(12, &s);
  68. xs.add(i1);
  69. xs.add(i2);
  70. xs.add(i3);
  71. Intersection i = xs.hit();
  72. ASSERT_EQ(i, i2);
  73. }
  74. TEST(IntersectTest, The_hit_when_all_intersection_have_negative_t)
  75. {
  76. Sphere s = Sphere();
  77. Intersect xs = Intersect();
  78. Intersection i1 = Intersection(-2, &s);
  79. Intersection i2 = Intersection(-1, &s);
  80. xs.add(i1);
  81. xs.add(i2);
  82. Intersection i = xs.hit();
  83. ASSERT_TRUE(i.nothing());
  84. }
  85. TEST(IntersectTest, The_hit_is_always_the_lowest_nonnegative_intersection)
  86. {
  87. Sphere s = Sphere();
  88. Intersect xs = Intersect();
  89. Intersection i1 = Intersection(5, &s);
  90. Intersection i2 = Intersection(7, &s);
  91. Intersection i3 = Intersection(-3, &s);
  92. Intersection i4 = Intersection(2, &s);
  93. xs.add(i1);
  94. xs.add(i2);
  95. xs.add(i3);
  96. xs.add(i4);
  97. Intersection i = xs.hit();
  98. ASSERT_EQ(i, i4);
  99. }