123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /*
- * DoRayMe - a quick and dirty Raytracer
- * Cone unit tests
- *
- * Created by Manoël Trapier
- * Copyright (c) 2020 986-Studio.
- *
- */
- #include <intersect.h>
- #include <intersection.h>
- #include <cone.h>
- #include <transformation.h>
- #include <gtest/gtest.h>
- class ConeTest : public Cone
- {
- public:
- Tuple doLocalNormalAt(Tuple point)
- {
- return localNormalAt(point);
- }
- };
- TEST(ConeTest, Intersecting_a_cone_with_a_ray)
- {
- Cone cone = Cone();
- Point Origins[] = {
- Point(0, 0, -5),
- Point(0, 0, -5),
- Point(1, 1, -5),
- };
- Vector Directions[] = {
- Vector(0, 0, 1),
- Vector(1, 1, 1),
- Vector(-0.5, -1, 1),
- };
- double t0s[] = { 5, 8.66025, 4.55006 };
- double t1s[] = { 5, 8.66025, 49.44994 };
- int i;
- for(i = 0; i < 3; i++)
- {
- Tuple direction = Directions[i].normalise();
- Ray r = Ray(Origins[i], direction);
- Intersect xs = cone.intersect(r);
- /* Temporary lower the precision */
- set_equal_precision(0.00001);
- ASSERT_EQ(xs.count(), 2);
- EXPECT_TRUE(double_equal(xs[0].t, t0s[i]));
- EXPECT_TRUE(double_equal(xs[1].t, t1s[i]));
- set_equal_precision(FLT_EPSILON);
- }
- }
- TEST(ConeTest, Intersecting_a_cone_with_a_ray_parall_to_one_of_its_halves)
- {
- Cone cone = Cone();
- Tuple direction = Vector(0, 1, 1).normalise();
- Ray r = Ray(Point(0, 0, -1), direction);
- Intersect xs = cone.intersect(r);
- ASSERT_EQ(xs.count(), 1);
- /* Temporary lower the precision */
- set_equal_precision(0.00001);
- ASSERT_TRUE(double_equal(xs[0].t, 0.35355));
- set_equal_precision(FLT_EPSILON);
- }
- TEST(ConeTest, Intersecting_a_cone_end_cap)
- {
- Point Origins[] = {
- Point(0, 0, -5),
- Point(0, 0, -0.25),
- Point(0, 0, -0.25),
- };
-
- Vector Directions[] = {
- Vector(0, 1, 0),
- Vector(0, 1, 1),
- Vector(0, 1, 0),
- };
-
- uint32_t Counts[] = { 0, 2, 4 };
-
- Cone cone = Cone();
- cone.minCap = -0.5;
- cone.maxCap = 0.5;
- cone.isClosed = true;
-
- int i;
- for(i = 0; i < 3; i++)
- {
- Tuple direction = Directions[i].normalise();
- Ray r = Ray(Origins[i], direction);
- Intersect xs = cone.intersect(r);
- ASSERT_EQ(xs.count(), Counts[i]);
- }
- }
- TEST(ConeTest, Computing_the_normal_vector_on_a_cone)
- {
- ConeTest cone = ConeTest();
- Point HitPointss[] = {
- Point(0, 0, 0),
- Point(1, 1, 1),
- Point(-1, -1, 0),
- };
- Vector Normals[] = {
- Vector(0, 0, 0),
- Vector(1, -sqrt(2), 1),
- Vector(-1, 1, 0),
- };
- int i;
- for(i = 0; i < 3; i++)
- {
- ASSERT_EQ(cone.doLocalNormalAt(HitPointss[i]), Normals[i]);
- }
- }
|