[WIP] sart intergration of cylinders

This commit is contained in:
NADAL Jean-Baptiste
2024-03-18 16:35:12 +01:00
parent b2ba503d24
commit e17ac42b4a
7 changed files with 228 additions and 5 deletions

View File

@@ -35,6 +35,7 @@ add_library(raytracing
src/renderer/world.cpp
src/shapes/cube.cpp
src/shapes/cylinder.cpp
src/shapes/plane.cpp
src/shapes/shape.cpp
src/shapes/sphere.cpp

View File

@@ -47,5 +47,6 @@
#include "renderer/world.h"
#include "shapes/cube.h"
#include "shapes/cylinder.h"
#include "shapes/plane.h"
#include "shapes/sphere.h"

View File

@@ -208,7 +208,7 @@ Canvas Camera::render(const World &a_world)
{
double the_temp_perc;
the_cpt++;
the_temp_perc = the_cpt * 100 / the_loop_size;
the_temp_perc = (double)the_cpt * 100 / the_loop_size;
if (the_percent != (int8_t)the_temp_perc)
{
the_percent = (int8_t)the_temp_perc;

View File

@@ -1,5 +1,5 @@
/*!
* sphere.cpp
* cube.cpp
*
* Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved.
*
@@ -43,10 +43,12 @@ Intersections Cube::local_intersect(const Ray &a_ray)
{
double the_tmin, the_tmax;
Intersections the_intersections;
const Tuple &the_ray_direction = a_ray.direction();
const Tuple &the_ray_origin = a_ray.origin();
const auto [the_xtmin, the_xtmax] = check_axis(a_ray.origin().x(), a_ray.direction().x());
const auto [the_ytmin, the_ytmax] = check_axis(a_ray.origin().y(), a_ray.direction().y());
const auto [the_ztmin, the_ztmax] = check_axis(a_ray.origin().z(), a_ray.direction().z());
const auto [the_xtmin, the_xtmax] = check_axis(the_ray_origin.x(), the_ray_direction.x());
const auto [the_ytmin, the_ytmax] = check_axis(the_ray_origin.y(), the_ray_direction.y());
const auto [the_ztmin, the_ztmax] = check_axis(the_ray_origin.z(), the_ray_direction.z());
the_tmin = std::max({the_xtmin, the_ytmin, the_ztmin});
the_tmax = std::min({the_xtmax, the_ytmax, the_ztmax});

View File

@@ -0,0 +1,83 @@
/*!
* cylinder.cpp
*
* Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 18/03/2024
*
*/
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
/* ------------------------------------------------------------------------- */
#include <cmath>
#include "core/common.h"
#include "core/intersections.h"
#include "cylinder.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
Intersections Cylinder::local_intersect(const Ray &a_ray)
{
double the_a, the_b, the_c, the_disc;
double the_t0, the_t1;
Intersections the_intersections;
const Tuple &the_ray_direction = a_ray.direction();
const Tuple &the_ray_origin = a_ray.origin();
the_a = std::pow(the_ray_direction.x(), 2) + std::pow(the_ray_direction.z(), 2);
// Ray is parallel to the y axis
if (double_equal(the_a, 0))
{
return the_intersections;
}
the_b = 2 * the_ray_origin.x() * the_ray_direction.x() +
2 * the_ray_origin.z() * the_ray_direction.z();
the_c = std::pow(the_ray_origin.x(), 2) + std::pow(the_ray_origin.z(), 2) - 1;
the_disc = std::pow(the_b, 2) - 4 * the_a * the_c;
if (the_disc < 0)
{
return the_intersections;
}
the_t0 = (-the_b - std::sqrt(the_disc)) / (2 * the_a);
the_t1 = (-the_b + std::sqrt(the_disc)) / (2 * the_a);
the_intersections.add(Intersection(the_t0, this));
the_intersections.add(Intersection(the_t1, this));
return the_intersections;
}
/* ------------------------------------------------------------------------- */
Tuple Cylinder::local_normal_at(const Tuple &a_local_point) const
{
return Tuple::Vector(0, 0, 0);
}

View File

@@ -0,0 +1,46 @@
/*!
* cylinder.h
*
* Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 18/03/2024
*
*/
#ifndef _RAYTRACER_CYLINDER_H
#define _RAYTRACER_CYLINDER_H
/* ------------------------------------------------------------------------- */
#include "shapes/shape.h"
/* ------------------------------------------------------------------------- */
namespace Raytracer
{
class Cylinder : public Shape
{
public:
Cylinder(void) = default;
Intersections local_intersect(const Ray &a_ray) override;
Tuple local_normal_at(const Tuple &a_local_point) const override;
};
}; // namespace Raytracer
#endif // _RAYTRACER_CYLINDER_H

View File

@@ -32,3 +32,93 @@
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
class CylinderTestIntersect
{
public:
Tuple origin;
Tuple direction;
double t0 = -1;
double t1 = -1;
};
/* ------------------------------------------------------------------------- */
SCENARIO("A Ray misses a cylinder", "[features/cylinders.feature]")
{
// | origin | direction |
// | point(1, 0, 0) | vector(0, 1, 0) |
// | point(0, 0, 0) | vector(0, 1, 0) |
// | point(0, 0, -5) | vector(1, 1, 1) |
CylinderTestIntersect the_test[] = {
{Tuple::Point(1, 0, 0), Tuple::Vector(0, 1, 0)},
{Tuple::Point(0, 0, 0), Tuple::Vector(0, 1, 0)},
{Tuple::Point(0, 0, -5), Tuple::Vector(1, 1, 1)}
};
GIVEN("cyl <- cylinder()")
{
Cylinder cyl;
AND_GIVEN("r <- ray(<origin>, <direction>)")
{
WHEN("xs <- local_intersect(cyl,r)")
{
for (int i = 0; i < 3; i++)
{
Ray r(the_test[i].origin, the_test[i].direction);
Intersections xs = cyl.local_intersect(r);
THEN("xs.count = 0")
{
REQUIRE(xs.count() == 0);
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("A Ray strikes a cylinder", "[features/cylinders.feature]")
{
// | origin | direction | t0 | t1 |
// | point(1, 0, -5) | vector(0, 0, 1) | 5 | 5 |
// | point(0, 0, -5) | vector(0, 0, 1) | 4 | 6 |
// | point(0.5, 0, -5) | vector(0.1, 1, 1) | 6.80798 | 7.08872 |
CylinderTestIntersect the_test[] = {
{ Tuple::Point(1, 0, -5), Tuple::Vector(0, 0, 1), 5, 5},
{ Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1), 4, 6},
{Tuple::Point(0.5, 0, -5), Tuple::Vector(0.1, 1, 1), 6.80798, 7.08872}
};
GIVEN("cyl <- cylinder()")
{
Cylinder cyl;
AND_GIVEN("direction <- normalize(<direction>)")
{
AND_GIVEN("r <- ray(<origin>, direction)")
{
WHEN("xs <- local_intersect(cyl,r)")
{
for (int i = 0; i < 3; i++)
{
Tuple direction = the_test[i].direction.normalize();
Ray r(the_test[i].origin, direction);
Intersections xs = cyl.local_intersect(r);
THEN("xs.count = 2")
{
REQUIRE(xs.count() == 2);
}
AND_THEN("xs[0].t = <t1>")
{
REQUIRE(xs[0].distance_t() == the_test[i].t0);
}
AND_THEN("xs[1].t = <t2>")
{
REQUIRE(xs[1].distance_t() == the_test[i].t1);
}
}
}
}
}
}
}