transformation_test.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Transformations unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <transformation.h>
  10. #include <tuple.h>
  11. #include <math.h>
  12. #include <gtest/gtest.h>
  13. TEST(TransformationTest, Multiplying_by_a_translation_matrix)
  14. {
  15. Matrix transform = translation(5, -3, 2);
  16. Point p = Point(-3, 4, 5);
  17. ASSERT_EQ(transform * p, Point(2, 1, 7));
  18. }
  19. TEST(TransformationTest, Multiplying_by_the_inverse_of_a_translation_matrix)
  20. {
  21. Matrix transform = translation(5, -3, 2);
  22. Matrix inv = transform.inverse();
  23. Point p = Point(-3, 4, 5);
  24. ASSERT_EQ(inv * p, Point(-8, 7, 3));
  25. }
  26. TEST(TransformationTest, Translation_does_not_affect_vectors)
  27. {
  28. Matrix transform = translation(5, -3, 2);
  29. Vector v = Vector(-3, 4, 5);
  30. ASSERT_EQ(transform * v, Vector(-3, 4, 5));
  31. }
  32. TEST(TransformationTest, A_scaling_matrix_applied_to_a_point)
  33. {
  34. Matrix transform = scaling(2, 3, 4);
  35. Point p = Point(-4, 6, 8);
  36. ASSERT_EQ(transform * p, Point(-8, 18, 32));
  37. }
  38. TEST(TransformationTest, A_scaling_matrix_applied_to_a_vector)
  39. {
  40. Matrix transform = scaling(2, 3, 4);
  41. Vector v = Vector(-4, 6, 8);
  42. ASSERT_EQ(transform * v, Vector(-8, 18, 32));
  43. }
  44. TEST(TransformationTest, Multiplaying_by_the_inverse_of_a_scaling_matrix)
  45. {
  46. Matrix transform = scaling(2, 3, 4);
  47. Matrix inv = transform.inverse();
  48. Vector v = Vector(-4, 6, 8);
  49. ASSERT_EQ(inv * v, Vector(-2, 2, 2));
  50. }
  51. TEST(TransformationTest, Reflexion_is_scaling_by_a_negative_value)
  52. {
  53. Matrix transform = scaling(-1, 1, 1);
  54. Point p = Point(2, 3, 4);
  55. ASSERT_EQ(transform * p, Point(-2, 3, 4));
  56. }
  57. TEST(TransformationTest, Rotating_a_point_around_the_X_axis)
  58. {
  59. Point p = Point(0, 1, 0);
  60. Matrix half_quarter = rotationX(M_PI / 4.);
  61. Matrix full_quarter = rotationX(M_PI / 2.);
  62. ASSERT_EQ(half_quarter * p, Point(0, sqrt(2)/2, sqrt(2)/2));
  63. ASSERT_EQ(full_quarter * p, Point(0, 0, 1));
  64. }
  65. TEST(TransformationTest, The_inverse_of_an_x_rotation_rotates_in_the_opposite_direction)
  66. {
  67. Point p = Point(0, 1, 0);
  68. Matrix half_quarter = rotationX(M_PI / 4.);
  69. Matrix inv = half_quarter.inverse();
  70. ASSERT_EQ(inv * p, Point(0, sqrt(2)/2, -sqrt(2)/2));
  71. }
  72. TEST(TransformationTest, Rotating_a_point_around_the_Y_axis)
  73. {
  74. Point p = Point(0, 0, 1);
  75. Matrix half_quarter = rotationY(M_PI / 4.);
  76. Matrix full_quarter = rotationY(M_PI / 2.);
  77. ASSERT_EQ(half_quarter * p, Point(sqrt(2)/2, 0, sqrt(2)/2));
  78. ASSERT_EQ(full_quarter * p, Point(1, 0, 0));
  79. }
  80. TEST(TransformationTest, Rotating_a_point_around_the_Z_axis)
  81. {
  82. Point p = Point(0, 1, 0);
  83. Matrix half_quarter = rotationZ(M_PI / 4.);
  84. Matrix full_quarter = rotationZ(M_PI / 2.);
  85. ASSERT_EQ(half_quarter * p, Point(-sqrt(2)/2, sqrt(2)/2, 0));
  86. ASSERT_EQ(full_quarter * p, Point(-1, 0, 0));
  87. }
  88. TEST(TransformationTest, A_shearing_transformation_moves_x_in_proportion_to_y)
  89. {
  90. Matrix transform = shearing(1, 0, 0, 0, 0, 0);
  91. Point p = Point(2, 3, 4);
  92. ASSERT_EQ(transform * p, Point(5, 3, 4));
  93. }
  94. TEST(TransformationTest, A_shearing_transformation_moves_x_in_proportion_to_z)
  95. {
  96. Matrix transform = shearing(0, 1, 0, 0, 0, 0);
  97. Point p = Point(2, 3, 4);
  98. ASSERT_EQ(transform * p, Point(6, 3, 4));
  99. }
  100. TEST(TransformationTest, A_shearing_transformation_moves_y_in_proportion_to_x)
  101. {
  102. Matrix transform = shearing(0, 0, 1, 0, 0, 0);
  103. Point p = Point(2, 3, 4);
  104. ASSERT_EQ(transform * p, Point(2, 5, 4));
  105. }
  106. TEST(TransformationTest, A_shearing_transformation_moves_y_in_proportion_to_z)
  107. {
  108. Matrix transform = shearing(0, 0, 0, 1, 0, 0);
  109. Point p = Point(2, 3, 4);
  110. ASSERT_EQ(transform * p, Point(2, 7, 4));
  111. }
  112. TEST(TransformationTest, A_shearing_transformation_moves_z_in_proportion_to_x)
  113. {
  114. Matrix transform = shearing(0, 0, 0, 0, 1, 0);
  115. Point p = Point(2, 3, 4);
  116. ASSERT_EQ(transform * p, Point(2, 3, 6));
  117. }
  118. TEST(TransformationTest, A_shearing_transformation_moves_z_in_proportion_to_y)
  119. {
  120. Matrix transform = shearing(0, 0, 0, 0, 0, 1);
  121. Point p = Point(2, 3, 4);
  122. ASSERT_EQ(transform * p, Point(2, 3, 7));
  123. }
  124. TEST(TransformationTest, Individual_trnasformations_are_applied_in_sequence)
  125. {
  126. Point p = Point(1, 0, 1);
  127. Matrix A = rotationX(M_PI / 2.);
  128. Matrix B = scaling(5, 5, 5);
  129. Matrix C = translation(10, 5, 7);
  130. Tuple p2 = A * p;
  131. ASSERT_EQ(p2, Point(1, -1, 0));
  132. Tuple p3 = B * p2;
  133. ASSERT_EQ(p3, Point(5, -5, 0));
  134. Tuple p4 = C * p3;
  135. ASSERT_EQ(p4, Point(15, 0, 7));
  136. }
  137. TEST(TransformationTest, Chained_transformation_must_be_applied_in_reverse_order)
  138. {
  139. Point p = Point(1, 0, 1);
  140. Matrix A = rotationX(M_PI / 2.);
  141. Matrix B = scaling(5, 5, 5);
  142. Matrix C = translation(10, 5, 7);
  143. Matrix T = C * B * A;
  144. ASSERT_EQ(T * p, Point(15, 0, 7));
  145. }
  146. TEST(TransformationTest, The_transformation_matrix_for_the_default_orientation)
  147. {
  148. Tuple from = Point(0, 0, 0);
  149. Tuple to = Point(0, 0, -1);
  150. Tuple up = Vector(0, 1, 0);
  151. Matrix t = viewTransform(from, to, up);
  152. ASSERT_EQ(t, Matrix4().identity());
  153. }
  154. TEST(TransformationTest, A_view_transformation_matrix_looking_in_positive_z_direction)
  155. {
  156. Tuple from = Point(0, 0, 0);
  157. Tuple to = Point(0, 0, 1);
  158. Tuple up = Vector(0, 1, 0);
  159. Matrix t = viewTransform(from, to, up);
  160. ASSERT_EQ(t, scaling(-1, 1, -1));
  161. }
  162. TEST(TransformationTest, The_view_transformation_move_the_world)
  163. {
  164. Tuple from = Point(0, 0, 8);
  165. Tuple to = Point(0, 0, 0);
  166. Tuple up = Vector(0, 1, 0);
  167. Matrix t = viewTransform(from, to, up);
  168. ASSERT_EQ(t, translation(0, 0, -8));
  169. }
  170. TEST(TransformationTest, An_arbitrary_view_transformation)
  171. {
  172. Tuple from = Point(1, 3, 2);
  173. Tuple to = Point(4, -2, 8);
  174. Tuple up = Vector(1, 1, 0);
  175. Matrix t = viewTransform(from, to, up);
  176. double values[] = {-0.50709, 0.50709, 0.67612, -2.36643,
  177. 0.76772, 0.60609, 0.12122, -2.82843,
  178. -0.35857, 0.59761, -0.71714, 0.00000,
  179. 0.00000, 0.00000, 0.00000, 1.00000};
  180. /* Temporary lower the precision */
  181. set_equal_precision(0.00001);
  182. ASSERT_EQ(t, Matrix4(values));
  183. set_equal_precision(FLT_EPSILON);
  184. }