And cones !
This commit is contained in:
132
tests/cone_test.cpp
Normal file
132
tests/cone_test.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user