[FEAT] cylinder is now ok

This commit is contained in:
2024-03-19 23:06:30 +01:00
parent 38107165a7
commit fd5753efeb
3 changed files with 68 additions and 0 deletions

View File

@@ -69,6 +69,7 @@ int main(void)
the_cylinder = new Cylinder();
the_cylinder->set_minimum(0);
the_cylinder->set_maximum(0.75);
the_cylinder->set_closed(true);
the_cylinder->set_transform(Matrix::translation(-1, 0, 1) *
Matrix::scaling(0.5, 1, 0.5));
Material &the_cylinder_material = the_cylinder->material();
@@ -125,6 +126,7 @@ int main(void)
the_concentric_cylinder4 = new Cylinder();
the_concentric_cylinder4->set_minimum(0);
the_concentric_cylinder4->set_maximum(0.5);
the_concentric_cylinder4->set_closed(true);
the_concentric_cylinder4->set_transform(Matrix::translation(1, 0, 0) *
Matrix::scaling(0.2, 1, 0.2));
Material &the_concentric_cylinder4_material = the_concentric_cylinder4->material();
@@ -139,6 +141,7 @@ int main(void)
the_decorative_cylinder1 = new Cylinder();
the_decorative_cylinder1->set_minimum(0);
the_decorative_cylinder1->set_maximum(0.3);
the_decorative_cylinder1->set_closed(true);
the_decorative_cylinder1->set_transform(Matrix::translation(0, 0, -0.75) *
Matrix::scaling(0.05, 1, 0.05));
Material &the_decorative_cylinder1_material = the_decorative_cylinder1->material();
@@ -153,6 +156,7 @@ int main(void)
the_decorative_cylinder2 = new Cylinder();
the_decorative_cylinder2->set_minimum(0);
the_decorative_cylinder2->set_maximum(0.3);
the_decorative_cylinder2->set_closed(true);
the_decorative_cylinder2->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.15) *
Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05));
Material &the_decorative_cylinder2_material = the_decorative_cylinder2->material();
@@ -167,6 +171,7 @@ int main(void)
the_decorative_cylinder3 = new Cylinder();
the_decorative_cylinder3->set_minimum(0);
the_decorative_cylinder3->set_maximum(0.3);
the_decorative_cylinder3->set_closed(true);
the_decorative_cylinder3->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.3) *
Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05));
Material &the_decorative_cylinder3_material = the_decorative_cylinder3->material();
@@ -181,6 +186,7 @@ int main(void)
the_decorative_cylinder4 = new Cylinder();
the_decorative_cylinder4->set_minimum(0);
the_decorative_cylinder4->set_maximum(0.3);
the_decorative_cylinder4->set_closed(true);
the_decorative_cylinder4->set_transform(Matrix::translation(0, 0, -2.25) * Matrix::rotation_y(-0.45) *
Matrix::translation(0, 0, 1.5) * Matrix::scaling(0.05, 1, 0.05));
Material &the_decorative_cylinder4_material = the_decorative_cylinder4->material();
@@ -195,6 +201,7 @@ int main(void)
the_glass_cylinder = new Cylinder();
the_glass_cylinder->set_minimum(0.0001);
the_glass_cylinder->set_maximum(0.5);
the_glass_cylinder->set_closed(true);
the_glass_cylinder->set_transform(Matrix::translation(0, 0, -1.5) *
Matrix::scaling(0.33, 1, 0.33));
Material &the_glass_cylinder_material = the_glass_cylinder->material();

View File

@@ -95,6 +95,18 @@ Intersections Cylinder::local_intersect(const Ray &a_ray)
Tuple Cylinder::local_normal_at(const Tuple &a_local_point) const
{
double the_distance;
// Compute the sqare of the distance from the y axis
the_distance = std::pow(a_local_point.x(), 2) + std::pow(a_local_point.z(), 2);
if ((the_distance < 1) && (a_local_point.y() >= m_maximum - kEpsilon))
{
return Tuple::Vector(0, 1, 0);
}
else if ((the_distance < 1) && (a_local_point.y() <= m_maximum + kEpsilon))
{
return Tuple::Vector(0, -1, 0);
}
return Tuple::Vector(a_local_point.x(), 0, a_local_point.z());
}

View File

@@ -313,3 +313,52 @@ SCENARIO("Intersecting the caps of a closed cylinder", "[features/cylinders.feat
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The normal vector on the cylinder's end caps", "[features/cylinders.feature]")
{
// | point | normal |
// | point(0, 1, 0) | vector(0, -1, 0) |
// | point(0.5, 1, 0) | vector(0, -1, 0) |
// | point(0, 1, 0.5) | vector(0, -1, 0) |
// | point(0, 2, 0) | vector(0, 1, 0) |
// | point(0.5, 2, 0) | vector(0, 1, 0) |
// | point(0, 2, 0.5) | vector(0, 1, 0) |
CylinderTestNormal the_test[] = {
{ Tuple::Point(0, 1, 0), Tuple::Vector(0, -1, 0)},
{Tuple::Point(0.5, 1, 0), Tuple::Vector(0, -1, 0)},
{ Tuple::Point(0, 1, 0.5), Tuple::Vector(0, -1, 0)},
{ Tuple::Point(0, 2, 0), Tuple::Vector(0, 1, 0)},
{Tuple::Point(0.5, 2, 0), Tuple::Vector(0, 1, 0)},
{ Tuple::Point(0, 2, 0.5), Tuple::Vector(0, 1, 0)}
};
GIVEN("cyl <- cylinder()")
{
Cylinder cyl;
AND_GIVEN("cyl.minimum <- 1")
{
cyl.set_minimum(1);
AND_GIVEN("cyl.maximum <- 2")
{
cyl.set_maximum(2);
AND_GIVEN("cyl.closed <- true")
{
cyl.set_closed(true);
WHEN("n <- local_normal_at(cyl,<point>)")
{
for (int i = 0; i < 6; i++)
{
Tuple p = the_test[i].point;
Tuple normal = cyl.local_normal_at(p);
THEN("n = <normal>")
{
REQUIRE(normal == the_test[i].normal);
}
}
}
}
}
}
}
}