smoothtriangle_test.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Smooth Triangle unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <smoothtriangle.h>
  10. #include <math.h>
  11. #include <gtest/gtest.h>
  12. class SmoothTriTest : public SmoothTriangle
  13. {
  14. public:
  15. SmoothTriTest(Point p1, Point p2, Point p3, Vector n1, Vector n2, Vector n3) : SmoothTriangle(p1, p2, p3, n1, n2, n3) {};
  16. void doLocalIntersect(Ray ray, Intersect &xs)
  17. {
  18. this->localIntersect(ray, xs);
  19. };
  20. Tuple doLocalNormalAt(Tuple point, Intersection *hit)
  21. {
  22. return this->localNormalAt(point, hit);
  23. };
  24. };
  25. Point p1 = Point(0, 1, 0);
  26. Point p2 = Point(-1, 0, 0);
  27. Point p3 = Point(1, 0, 0);
  28. Vector n1 = Vector(0, 1, 0);
  29. Vector n2 = Vector(-1, 0, 0);
  30. Vector n3 = Vector(1, 0, 0);
  31. SmoothTriTest tri = SmoothTriTest(p1, p2, p3, n1, n2, n3);
  32. TEST(SmoothTriangleTest, Construcring_a_smooth_triangle)
  33. {
  34. ASSERT_EQ(tri.p1, p1);
  35. ASSERT_EQ(tri.p2, p2);
  36. ASSERT_EQ(tri.p3, p3);
  37. ASSERT_EQ(tri.n1, n1);
  38. ASSERT_EQ(tri.n2, n2);
  39. ASSERT_EQ(tri.n3, n3);
  40. }
  41. TEST(SmoothTriangleTest, An_intersection_with_a_smooth_triangle_stores_u_v)
  42. {
  43. Ray r = Ray(Point(-0.2, 0.3, -2), Vector(0, 0, 1));
  44. Intersect xs; tri.doLocalIntersect(r, xs);
  45. ASSERT_TRUE(double_equal(xs[0].u, 0.45));
  46. ASSERT_TRUE(double_equal(xs[0].v, 0.25));
  47. }
  48. TEST(SmoothTriangleTest, A_smooth_triangle_uses_u_v_to_interpolate_the_normal)
  49. {
  50. Intersection i = Intersection(1, &tri, 0.45, 0.25);
  51. Tuple n = tri.doLocalNormalAt(Point(0, 0, 0), &i);
  52. /* Temporary lower the precision */
  53. set_equal_precision(0.00001);
  54. ASSERT_EQ(n, Vector(-0.5547, 0.83205, 0));
  55. set_equal_precision(FLT_EPSILON);
  56. }
  57. TEST(SmoothTriangleTest, Preparing_a_normal_on_a_smooth_triangle)
  58. {
  59. Intersection i = Intersection(1, &tri, 0.45, 0.25);
  60. Tuple n = tri.normalAt(Point(0, 0, 0), &i);
  61. /* Temporary lower the precision */
  62. set_equal_precision(0.00001);
  63. ASSERT_EQ(n, Vector(-0.5547, 0.83205, 0));
  64. set_equal_precision(FLT_EPSILON);
  65. }