Files
raytracer_challenge/raytracing/src/core/intersection.cpp

186 lines
5.2 KiB
C++

/*!
* intersection.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: 08/02/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 "core/common.h"
#include "intersection.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
Intersection::Intersection(void) :
m_is_nothing(true),
m_distance_t(0.0),
m_shape(nullptr)
{
}
/* ------------------------------------------------------------------------- */
Intersection::Intersection(double a_distance_t, Shape *a_shape) :
m_is_nothing(false),
m_distance_t(a_distance_t),
m_shape(a_shape)
{
}
/* ------------------------------------------------------------------------- */
Intersection::Intersection(Intersection &an_intersection) :
m_is_nothing(an_intersection.m_is_nothing),
m_distance_t(an_intersection.m_distance_t),
m_shape(an_intersection.m_shape)
{
}
/* ------------------------------------------------------------------------- */
Intersection::Intersection(const Intersection &an_intersection) :
m_is_nothing(an_intersection.m_is_nothing),
m_distance_t(an_intersection.m_distance_t),
m_shape(an_intersection.m_shape)
{
}
/* ------------------------------------------------------------------------- */
const Intersection &Intersection::operator=(const Intersection &an_intersection)
{
if (this == &an_intersection)
{
return *this;
}
m_is_nothing = an_intersection.m_is_nothing;
m_distance_t = an_intersection.m_distance_t;
m_shape = an_intersection.m_shape;
return *this;
}
/* ------------------------------------------------------------------------- */
bool Intersection::operator==(const Intersection &an_other) const
{
return (double_equal(m_distance_t, an_other.m_distance_t) && (m_shape == an_other.m_shape));
}
/* ------------------------------------------------------------------------- */
bool Intersection::operator<(const Intersection &an_other) const
{
return m_distance_t < an_other.m_distance_t;
}
/* ------------------------------------------------------------------------- */
bool Intersection::operator>(const Intersection &an_other) const
{
return m_distance_t > an_other.m_distance_t;
}
/* ------------------------------------------------------------------------- */
bool Intersection::operator>(double a_distance) const
{
return m_distance_t > a_distance;
}
/* ------------------------------------------------------------------------- */
bool Intersection::operator>=(double a_distance) const
{
return m_distance_t >= a_distance;
}
/* ------------------------------------------------------------------------- */
bool Intersection::operator<(double a_distance) const
{
return m_distance_t < a_distance;
}
/* ------------------------------------------------------------------------- */
bool Intersection::operator<=(double a_distance) const
{
return m_distance_t <= a_distance;
}
/* ------------------------------------------------------------------------- */
double Intersection::distance_t(void) const
{
return m_distance_t;
}
/* ------------------------------------------------------------------------- */
const Shape *Intersection::object(void) const
{
return m_shape;
}
/* ------------------------------------------------------------------------- */
bool Intersection::is_nothing(void)
{
return m_is_nothing;
}
/* ------------------------------------------------------------------------- */
bool Intersection::is_defined(void)
{
return !m_is_nothing;
}
/* ------------------------------------------------------------------------- */
IntersectionData Intersection::prepare_computations(const Ray &a_ray) const
{
IntersectionData the_data;
// Copy intersections properties for convenance
the_data.set_distance_t(m_distance_t);
the_data.set_object(m_shape);
// Precompute some useful values
the_data.set_point(a_ray.position(m_distance_t));
the_data.set_eyev(-a_ray.direction());
the_data.set_normalv(m_shape->normal_at(the_data.point()));
the_data.set_over_point(the_data.point() + the_data.normalv() * kEpsilon);
the_data.set_inside();
the_data.set_reflectv(a_ray.direction().reflect(the_data.normalv()));
return the_data;
}