cylinder_test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Cylinder 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 <cylinder.h>
  12. #include <transformation.h>
  13. #include <gtest/gtest.h>
  14. TEST(CylinderTest, A_ray_miss_a_cylinder)
  15. {
  16. Cylinder cyl = Cylinder();
  17. Point Origins[] = {
  18. Point(1, 0, 0),
  19. Point(0, 0, 0),
  20. Point(0, 0, -5),
  21. };
  22. Vector Directions[] = {
  23. Vector(0, 1, 0),
  24. Vector(0, 1, 0),
  25. Vector(1, 1, 1),
  26. };
  27. int i;
  28. for(i = 0; i < 3; i++)
  29. {
  30. Tuple direction = Directions[i].normalise();
  31. Ray r = Ray(Origins[i], direction);
  32. Intersect xs; cyl.intersect(r, xs);
  33. ASSERT_EQ(xs.count(), 0);
  34. }
  35. }
  36. TEST(CylinderTest, A_ray_hit_a_cylinder)
  37. {
  38. Cylinder cyl = Cylinder();
  39. Point Origins[] = {
  40. Point(1, 0, -5),
  41. Point(0, 0, -5),
  42. Point(0.5, 0, -5),
  43. };
  44. Vector Directions[] = {
  45. Vector(0, 0, 1),
  46. Vector(0, 0, 1),
  47. Vector(0.1, 1, 1),
  48. };
  49. double t0s[] = { 5, 4, 6.80798 };
  50. double t1s[] = { 5, 6, 7.08872 };
  51. int i;
  52. for(i = 0; i < 3; i++)
  53. {
  54. Tuple direction = Directions[i].normalise();
  55. Ray r = Ray(Origins[i], direction);
  56. Intersect xs; cyl.intersect(r, xs);
  57. /* Temporary lower the precision */
  58. set_equal_precision(0.00001);
  59. ASSERT_EQ(xs.count(), 2);
  60. EXPECT_TRUE(double_equal(xs[0].t, t0s[i]));
  61. EXPECT_TRUE(double_equal(xs[1].t, t1s[i]));
  62. set_equal_precision(FLT_EPSILON);
  63. }
  64. }
  65. TEST(CylinderTest, Normal_vector_on_a_cylinder)
  66. {
  67. Cylinder cyl = Cylinder();
  68. Point HitPointss[] = {
  69. Point(1, 0, 0),
  70. Point(0, 5, -1),
  71. Point(0, -2, 1),
  72. Point(-1, 1, 0),
  73. };
  74. Vector Normals[] = {
  75. Vector(1, 0, 0),
  76. Vector(0, 0, -1),
  77. Vector(0, 0, 1),
  78. Vector(-1, 0, 0),
  79. };
  80. int i;
  81. for(i = 0; i < 4; i++)
  82. {
  83. ASSERT_EQ(cyl.normalAt(HitPointss[i]), Normals[i]);
  84. }
  85. }
  86. TEST(CylinderTest, The_default_minimum_and_maximum_for_a_cylinder)
  87. {
  88. Cylinder cyl = Cylinder();
  89. ASSERT_EQ(cyl.minCap, -INFINITY);
  90. ASSERT_EQ(cyl.maxCap, INFINITY);
  91. }
  92. TEST(CylinderTest, Intersecting_a_constrained_cylinder)
  93. {
  94. Point Origins[] = {
  95. Point(0, 1.5, 0),
  96. Point(0, 3, -5),
  97. Point(0, 0, -5),
  98. Point(0, 2, -5),
  99. Point(0, 1, -5),
  100. Point(0, 1.5, -2),
  101. };
  102. Vector Directions[] = {
  103. Vector(0.1, 1, 0),
  104. Vector(0, 0, 1),
  105. Vector(0, 0, 1),
  106. Vector(0, 0, 1),
  107. Vector(0, 0, 1),
  108. Vector(0., 0, 1),
  109. };
  110. uint32_t Counts[] = { 0, 0, 0, 0, 0, 2 };
  111. Cylinder cyl = Cylinder();
  112. cyl.minCap = 1;
  113. cyl.maxCap = 2;
  114. int i;
  115. for(i = 0; i < 6; i++)
  116. {
  117. Tuple direction = Directions[i].normalise();
  118. Ray r = Ray(Origins[i], direction);
  119. Intersect xs; cyl.intersect(r, xs);
  120. ASSERT_EQ(xs.count(), Counts[i]);
  121. }
  122. }
  123. TEST(CylinderTest, The_default_closed_value_for_a_cylinder)
  124. {
  125. Cylinder cyl = Cylinder();
  126. ASSERT_EQ(cyl.isClosed, false);
  127. }
  128. TEST(CylinderTest, Intersecting_the_caps_of_a_close_cylinder)
  129. {
  130. Point Origins[] = {
  131. Point(0, 3, 0),
  132. Point(0, 3, -2),
  133. Point(0, 4, -2), /* Edge case */
  134. Point(0, 0, -5),
  135. Point(0, -1, -2), /* Edge case */
  136. };
  137. Vector Directions[] = {
  138. Vector(0, -1, 0),
  139. Vector(0, -1, 2),
  140. Vector(0, -1, 1),
  141. Vector(0, 1, 2),
  142. Vector(0, 1, 1),
  143. };
  144. uint32_t Counts[] = { 2, 2, 2, 2, 2 };
  145. Cylinder cyl = Cylinder();
  146. cyl.minCap = 1;
  147. cyl.maxCap = 2;
  148. cyl.isClosed = true;
  149. int i;
  150. for(i = 0; i < 5; i++)
  151. {
  152. Tuple direction = Directions[i].normalise();
  153. Ray r = Ray(Origins[i], direction);
  154. Intersect xs; cyl.intersect(r, xs);
  155. ASSERT_EQ(xs.count(), Counts[i]);
  156. }
  157. }
  158. TEST(CylinderTest, The_normal_on_a_cylinder_end_cap)
  159. {
  160. Cylinder cyl = Cylinder();
  161. Point HitPointss[] = {
  162. Point(0, 1, 0),
  163. Point(0.5, 1, 0),
  164. Point(0, 1, 0.5),
  165. Point(0, 2, 0),
  166. Point(0.5, 2, 0),
  167. Point(0, 2, 0.5),
  168. };
  169. Vector Normals[] = {
  170. Vector(0, -1, 0),
  171. Vector(0, -1, 0),
  172. Vector(0, -1, 0),
  173. Vector(0, 1, 0),
  174. Vector(0, 1, 0),
  175. Vector(0, 1, 0),
  176. };
  177. cyl.minCap = 1;
  178. cyl.maxCap = 2;
  179. cyl.isClosed = true;
  180. int idx;
  181. for(idx = 0; idx < 6; idx++)
  182. {
  183. ASSERT_EQ(cyl.normalAt(HitPointss[idx]), Normals[idx]);
  184. }
  185. }
  186. TEST(CylinderTest, The_bounding_box_of_a_cut_cylinder)
  187. {
  188. Cylinder t = Cylinder();
  189. BoundingBox b = BoundingBox(Point(-1, -10000, -1), Point(1, 10000, 1));
  190. t.minCap = -10000;
  191. t.maxCap = 10000;
  192. BoundingBox res = t.getBounds();
  193. ASSERT_EQ(res.min, b.min);
  194. ASSERT_EQ(res.max, b.max);
  195. }
  196. TEST(CylinderTest, The_bounding_box_of_a_uncut_cylinder)
  197. {
  198. /* This one is tricky. Infinite size don't cope well with transformations */
  199. Cylinder t = Cylinder();
  200. BoundingBox res = t.getBounds();
  201. ASSERT_FALSE(res.min.isRepresentable());
  202. ASSERT_FALSE(res.max.isRepresentable());
  203. }
  204. TEST(CylinderTest, An_uncut_cylinder_have_infinite_bounds)
  205. {
  206. Cylinder t = Cylinder();
  207. ASSERT_FALSE(t.haveFiniteBounds());
  208. }
  209. TEST(CylinderTest, A_cut_cylinder_have_finite_bounds)
  210. {
  211. Cylinder t = Cylinder();
  212. t.minCap = -5;
  213. t.maxCap = 3;
  214. BoundingBox res = t.getBounds();
  215. ASSERT_TRUE(t.haveFiniteBounds());
  216. ASSERT_EQ(res.min, Point(-1, -5, -1));
  217. ASSERT_EQ(res.max, Point(1, 3, 1));
  218. }