group_test.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Group 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 <group.h>
  12. #include <testshape.h>
  13. #include <sphere.h>
  14. #include <transformation.h>
  15. #include <gtest/gtest.h>
  16. TEST(GroupTest, Creating_a_new_group)
  17. {
  18. Group g = Group();
  19. ASSERT_EQ(g.transformMatrix, Matrix4().identity());
  20. ASSERT_TRUE(g.isEmpty());
  21. }
  22. TEST(GroupTest, Adding_a_child_to_a_group)
  23. {
  24. Group g = Group();
  25. TestShape s = TestShape();
  26. g.addObject(&s);
  27. ASSERT_FALSE(g.isEmpty());
  28. ASSERT_EQ(s.parent, &g);
  29. ASSERT_EQ(g[0], &s);
  30. }
  31. TEST(GroupTest, Intersecting_a_ray_with_an_empty_group)
  32. {
  33. Group g = Group();
  34. Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
  35. Intersect xs; g.intersect(r, xs);
  36. ASSERT_EQ(xs.count(), 0);
  37. }
  38. TEST(GroupTest, Intersecting_a_ray_with_an_nonempty_group)
  39. {
  40. Group g = Group();
  41. Sphere s1 = Sphere();
  42. Sphere s2 = Sphere();
  43. Sphere s3 = Sphere();
  44. s2.setTransform(translation(0, 0, -3));
  45. s3.setTransform(translation(5, 0, 0));
  46. g.addObject(&s1);
  47. g.addObject(&s2);
  48. g.addObject(&s3);
  49. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  50. Intersect xs; g.intersect(r, xs);
  51. ASSERT_EQ(xs.count(), 4);
  52. EXPECT_EQ(xs[0].object, &s2);
  53. EXPECT_EQ(xs[1].object, &s2);
  54. EXPECT_EQ(xs[2].object, &s1);
  55. EXPECT_EQ(xs[3].object, &s1);
  56. }
  57. TEST(GroupTest, Intersecting_a_transformed_group)
  58. {
  59. Group g = Group();
  60. Sphere s = Sphere();
  61. g.setTransform(scaling(2, 2, 2));
  62. s.setTransform(translation(5, 0, 0));
  63. g.addObject(&s);
  64. Ray r = Ray(Point(10, 0, -50), Vector(0, 0, 1));
  65. Intersect xs; g.intersect(r, xs);
  66. ASSERT_EQ(xs.count(), 2);
  67. }
  68. TEST(GroupTest, Group_bounding_box)
  69. {
  70. Group g = Group();
  71. Sphere s = Sphere();
  72. g.setTransform(scaling(2, 2, 2));
  73. s.setTransform(translation(5, 0, 0));
  74. g.addObject(&s);
  75. BoundingBox b = BoundingBox(Point(8, -2, -2), Point(12, 2, 2));
  76. BoundingBox res = g.getBounds();
  77. ASSERT_EQ(res.min, b.min);
  78. ASSERT_EQ(res.max, b.max);
  79. }
  80. TEST(GroupTest, Removing_an_object)
  81. {
  82. Group g = Group();
  83. Sphere s1 = Sphere();
  84. Sphere s2 = Sphere();
  85. Sphere s3 = Sphere();
  86. s1.setTransform(translation(-1, 0, 0));
  87. s2.setTransform(scaling(0.5, 0.5, 0.5));
  88. g.addObject(&s1);
  89. g.addObject(&s2);
  90. ASSERT_EQ(g.getObjectCount(), 2);
  91. ASSERT_EQ(*(g[0]), s1);
  92. ASSERT_EQ(*(g[1]), s2);
  93. g.removeObject(&s1);
  94. ASSERT_EQ(g.getObjectCount(), 1);
  95. ASSERT_EQ(*(g[0]), s2);
  96. ASSERT_EQ(g[1], nullptr);
  97. g.removeObject(&s3);
  98. ASSERT_EQ(g.getObjectCount(), 1);
  99. g.removeObject(&s2);
  100. ASSERT_EQ(g.getObjectCount(), 0);
  101. ASSERT_EQ(g[0], nullptr);
  102. }