cube_test.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Cube 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 <cube.h>
  12. #include <transformation.h>
  13. #include <gtest/gtest.h>
  14. TEST(CubeTest, A_ray_intersects_a_cube)
  15. {
  16. Cube c = Cube();
  17. Point Origins[] = {
  18. Point(5, 0.5, 0),
  19. Point(-5, 0.5, 0),
  20. Point(0.5, 5, 0),
  21. Point(0.5, -5, 0),
  22. Point(0.5, 0, 5),
  23. Point(0.5, 0, -5),
  24. Point(0, 0.5, 0),
  25. };
  26. Vector Directions[] = {
  27. Vector(-1, 0, 0),
  28. Vector(1, 0, 0),
  29. Vector(0, -1, 0),
  30. Vector(0, 1, 0),
  31. Vector(0, 0, -1),
  32. Vector(0, 0, 1),
  33. Vector(0, 0, 1),
  34. };
  35. double t1[] = { 4, 4, 4, 4, 4, 4, -1 };
  36. double t2[] = { 6, 6, 6, 6, 6, 6, 1 };
  37. int i;
  38. for(i = 0; i < 7; i++)
  39. {
  40. Ray r = Ray(Origins[i], Directions[i]);
  41. Intersect xs; c.intersect(r, xs);
  42. ASSERT_EQ(xs.count(), 2);
  43. EXPECT_EQ(xs[0].t, t1[i]);
  44. EXPECT_EQ(xs[1].t, t2[i]);
  45. }
  46. }
  47. TEST(CubeTest, A_ray_miss_a_cube)
  48. {
  49. Cube c = Cube();
  50. Point Origins[] = {
  51. Point(-2, 0, 0),
  52. Point(0, -2, 0),
  53. Point(0, 0, -2),
  54. Point(2, 0, 2),
  55. Point(0, 2, 2),
  56. Point(2, 2, 0),
  57. };
  58. Vector Directions[] = {
  59. Vector(0.2673, 0.5345, 0.8018),
  60. Vector(0.8018, 0.2673, 0.5345),
  61. Vector(0.5345, 0.8018, 0.2673),
  62. Vector(0, 0, -1),
  63. Vector(0, -1, 0),
  64. Vector(-1, 0, 0),
  65. };
  66. int i;
  67. for(i = 0; i < 6; i++)
  68. {
  69. Ray r = Ray(Origins[i], Directions[i]);
  70. Intersect xs; c.intersect(r, xs);
  71. ASSERT_EQ(xs.count(), 0);
  72. }
  73. }
  74. TEST(CubeTest, The_normal_on_the_surface_of_a_cube)
  75. {
  76. Cube c = Cube();
  77. Point HitPoints[] = {
  78. Point(1, 0.5, -0.8),
  79. Point(-1, -0.2, 0.9),
  80. Point(-0.4, 1, -0.1),
  81. Point(0.3, -1, -0.7),
  82. Point(-0.6, 0.3, 1),
  83. Point(0.4, 0.4, -1),
  84. Point(1, 1, 1),
  85. Point(-1, -1, -1),
  86. };
  87. Vector ExpectedNormals[] = {
  88. Vector(1, 0, 0),
  89. Vector(-1, 0, 0),
  90. Vector(0, 1, 0),
  91. Vector(0, -1, 0),
  92. Vector(0, 0, 1),
  93. Vector(0, 0, -1),
  94. Vector(1, 0, 0),
  95. Vector(-1, 0, 0),
  96. };
  97. int i;
  98. for(i = 0; i < 8; i++)
  99. {
  100. ASSERT_EQ(c.normalAt(HitPoints[i]), ExpectedNormals[i]);
  101. }
  102. }
  103. TEST(CubeTest, The_bounding_box_of_a_cube)
  104. {
  105. Cube t = Cube();
  106. Tuple boxMin = Point(-1, -1, -1);
  107. Tuple boxMax = Point(1, 1, 1);
  108. BoundingBox res = t.getBounds();
  109. ASSERT_EQ(res.min, boxMin);
  110. ASSERT_EQ(res.max, boxMax);
  111. }
  112. TEST(CubeTest, A_cube_have_finite_bounds)
  113. {
  114. Cube t = Cube();
  115. ASSERT_TRUE(t.haveFiniteBounds());
  116. }