shape_test.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Shape unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <shape.h>
  10. #include <testshape.h>
  11. #include <matrix.h>
  12. #include <transformation.h>
  13. #include <gtest/gtest.h>
  14. TEST(ShapeTest, The_default_transformation)
  15. {
  16. TestShape s = TestShape();
  17. ASSERT_EQ(s.transformMatrix, Matrix4().identity());
  18. }
  19. TEST(ShapeTest, Assigning_a_transformation)
  20. {
  21. TestShape s = TestShape();
  22. s.setTransform(translation(2, 3, 4));
  23. ASSERT_EQ(s.transformMatrix, translation(2, 3, 4));
  24. }
  25. TEST(ShapeTest, The_default_material)
  26. {
  27. TestShape s = TestShape();
  28. ASSERT_EQ(s.material, Material());
  29. }
  30. TEST(ShapeTest, Assigning_a_material)
  31. {
  32. TestShape s = TestShape();
  33. Material m = Material();
  34. m.ambient = 1;
  35. s.material = m;
  36. ASSERT_EQ(s.material, m);
  37. }
  38. TEST(ShapeTest, Intersecting_a_scaled_shape_with_a_ray)
  39. {
  40. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  41. TestShape s = TestShape();
  42. s.setTransform(scaling(2, 2, 2));
  43. Intersect xs = s.intersect(r);
  44. ASSERT_EQ(s.localRay.origin, Point(0, 0, -2.5));
  45. ASSERT_EQ(s.localRay.direction, Vector(0, 0, 0.5));
  46. }
  47. TEST(ShapeTest, Intersecting_a_translated_shape_with_a_ray)
  48. {
  49. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  50. TestShape s = TestShape();
  51. s.setTransform(translation(5, 0, 0));
  52. Intersect xs = s.intersect(r);
  53. ASSERT_EQ(s.localRay.origin, Point(-5, 0, -5));
  54. ASSERT_EQ(s.localRay.direction, Vector(0, 0, 1));
  55. }
  56. TEST(ShapeTest, Computing_the_normal_on_a_translated_shape)
  57. {
  58. TestShape s = TestShape();
  59. s.setTransform(translation(0, 1, 0));
  60. Tuple n = s.normalAt(Point(0, 1.70711, -0.70711));
  61. /* Temporary lower the precision */
  62. set_equal_precision(0.00001);
  63. ASSERT_EQ(n, Vector(0, 0.70711, -0.70711));
  64. set_equal_precision(FLT_EPSILON);
  65. }
  66. TEST(ShapeTest, Computing_the_normal_on_a_tranformed_shape)
  67. {
  68. TestShape s = TestShape();
  69. s.setTransform(scaling(1, 0.5, 1) * rotationZ(M_PI / 5));
  70. Tuple n = s.normalAt(Point(0, sqrt(2)/2, -sqrt(2)/2));
  71. /* Temporary lower the precision */
  72. set_equal_precision(0.00001);
  73. ASSERT_EQ(n, Vector(0, 0.97014, -0.24254));
  74. set_equal_precision(FLT_EPSILON);
  75. }