2015-12-08 07:31:57 +00:00
|
|
|
|
/*
|
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
|
*
|
2017-01-18 22:39:02 +00:00
|
|
|
|
* Copyright (C) 2015-2017 Mario Luzeiro <mrluzeiro@ua.pt>
|
2021-05-03 20:07:16 +00:00
|
|
|
|
* Copyright (C) 2015-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +00:00
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "ray.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
|
#include "../../3d_fastmath.h"
|
2019-12-05 14:03:15 +00:00
|
|
|
|
#include <cstdio>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
#include <wx/debug.h>
|
2020-08-18 14:17:16 +00:00
|
|
|
|
#include <wx/log.h>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2017-09-24 11:06:10 +00:00
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2017-01-18 22:39:02 +00:00
|
|
|
|
//static unsigned int gs_next_rayID = 0;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
void RAY::Init( const SFVEC3F& o, const SFVEC3F& d )
|
|
|
|
|
{
|
|
|
|
|
m_Origin = o;
|
|
|
|
|
m_Dir = d;
|
|
|
|
|
m_InvDir = 1.0f / d;
|
|
|
|
|
|
2020-10-27 00:25:08 +00:00
|
|
|
|
rayID = 0; // Not used, just set to 0
|
2017-01-18 22:39:02 +00:00
|
|
|
|
//rayID = gs_next_rayID;
|
|
|
|
|
//gs_next_rayID++;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
// An Efficient and Robust Ray–Box Intersection Algorithm
|
|
|
|
|
// Amy Williams Steve Barrus R. Keith Morley Peter Shirley
|
|
|
|
|
// University of Utah
|
|
|
|
|
// http://people.csail.mit.edu/amy/papers/box-jgt.pdf
|
2018-11-13 23:56:26 +00:00
|
|
|
|
m_dirIsNeg[0] = m_Dir.x < 0.0f;
|
|
|
|
|
m_dirIsNeg[1] = m_Dir.y < 0.0f;
|
|
|
|
|
m_dirIsNeg[2] = m_Dir.z < 0.0f;
|
2017-01-18 22:39:02 +00:00
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
|
// ray slope
|
|
|
|
|
|
|
|
|
|
// "Fast Ray / Axis-Aligned Bounding Box Overlap Tests using Ray Slopes"
|
|
|
|
|
// by Martin Eisemann, Thorsten Grosch, Stefan Müller and Marcus Magnor
|
|
|
|
|
// Computer Graphics Lab, TU Braunschweig, Germany and
|
|
|
|
|
// University of Koblenz-Landau, Germany
|
|
|
|
|
// Licence: "This source code is public domain, but please mention us if you use it."
|
|
|
|
|
//
|
|
|
|
|
// https://github.com/rjw57/mcvoxel/tree/master/third-party/rayslope
|
|
|
|
|
// https://github.com/rjw57/mcvoxel/blob/master/third-party/rayslope/ray.cpp
|
|
|
|
|
|
|
|
|
|
ibyj = m_Dir.x * m_InvDir.y;
|
|
|
|
|
jbyi = m_Dir.y * m_InvDir.x;
|
|
|
|
|
jbyk = m_Dir.y * m_InvDir.z;
|
|
|
|
|
kbyj = m_Dir.z * m_InvDir.y;
|
|
|
|
|
ibyk = m_Dir.x * m_InvDir.z;
|
|
|
|
|
kbyi = m_Dir.z * m_InvDir.x;
|
|
|
|
|
c_xy = m_Origin.y - jbyi * m_Origin.x;
|
|
|
|
|
c_xz = m_Origin.z - kbyi * m_Origin.x;
|
|
|
|
|
c_yx = m_Origin.x - ibyj * m_Origin.y;
|
|
|
|
|
c_yz = m_Origin.z - kbyj * m_Origin.y;
|
|
|
|
|
c_zx = m_Origin.x - ibyk * m_Origin.z;
|
|
|
|
|
c_zy = m_Origin.y - jbyk * m_Origin.z;
|
|
|
|
|
|
|
|
|
|
// ray slope classification
|
|
|
|
|
if( m_Dir.x < 0 )
|
|
|
|
|
{
|
|
|
|
|
if( m_Dir.y < 0 )
|
|
|
|
|
{
|
|
|
|
|
if( m_Dir.z < 0 )
|
|
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MMM;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
2020-08-18 14:17:16 +00:00
|
|
|
|
else if( m_Dir.z > 0 )
|
|
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MMP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
|
else
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MMO;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
|
else
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
if( m_Dir.z < 0 )
|
|
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MPM;
|
2021-01-07 19:33:43 +00:00
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
|
if( m_Dir.y == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MOM;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
|
else
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
if( ( m_Dir.y == 0 ) && ( m_Dir.z == 0 ) )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MOO;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.z == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MPO;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.y == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MOP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::MPP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
|
else
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
if( m_Dir.y < 0 )
|
|
|
|
|
{
|
|
|
|
|
if( m_Dir.z < 0 )
|
|
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::PMM;
|
2021-01-07 19:33:43 +00:00
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
|
if( m_Dir.x == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::OMM;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
|
else
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
if( ( m_Dir.x == 0 ) && ( m_Dir.z == 0 ) )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::OMO;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.z == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::PMO;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.x == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::OMP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::PMP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
|
else
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
if( m_Dir.z < 0 )
|
|
|
|
|
{
|
|
|
|
|
if( ( m_Dir.x == 0 ) && ( m_Dir.y == 0 ) )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::OOM;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.x == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::OPM;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.y == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::POM;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::PPM;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
|
else
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
if( m_Dir.x == 0 )
|
|
|
|
|
{
|
|
|
|
|
if( m_Dir.y == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::OOP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.z == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::OPO;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::OPP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if( ( m_Dir.y == 0 ) && ( m_Dir.z == 0 ) )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::POO;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.y == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::POP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else if( m_Dir.z == 0 )
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::PPO;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else
|
2019-12-30 13:01:06 +00:00
|
|
|
|
m_Classification = RAY_CLASSIFICATION::PPP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool IntersectSegment( const SFVEC2F &aStartA, const SFVEC2F &aEnd_minus_startA,
|
|
|
|
|
const SFVEC2F &aStartB, const SFVEC2F &aEnd_minus_startB )
|
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
|
float rxs = aEnd_minus_startA.x * aEnd_minus_startB.y - aEnd_minus_startA.y *
|
2016-07-19 17:35:25 +00:00
|
|
|
|
aEnd_minus_startB.x;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2017-09-24 11:06:10 +00:00
|
|
|
|
if( std::abs( rxs ) > glm::epsilon<float>() )
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
float inv_rxs = 1.0f / rxs;
|
|
|
|
|
|
|
|
|
|
SFVEC2F pq = aStartB - aStartA;
|
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
float t = ( pq.x * aEnd_minus_startB.y - pq.y * aEnd_minus_startB.x ) * inv_rxs;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
if( ( t < 0.0f ) || ( t > 1.0f ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
float u = ( pq.x * aEnd_minus_startA.y - pq.y * aEnd_minus_startA.x ) * inv_rxs;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
if( ( u < 0.0f ) || ( u > 1.0f ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
/// @todo: not tested
|
|
|
|
|
bool RAY::IntersectSphere( const SFVEC3F &aCenter, float aRadius, float &aOutT0,
|
|
|
|
|
float &aOutT1 ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
// Ray-sphere intersection: geometric
|
|
|
|
|
SFVEC3F OC = aCenter - m_Origin;
|
|
|
|
|
float p_dot_d = glm::dot( OC, m_Dir );
|
|
|
|
|
|
|
|
|
|
if( p_dot_d < 0.0f )
|
|
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
|
|
float p_dot_p = glm::dot( OC, OC );
|
|
|
|
|
float discriminant = p_dot_p - p_dot_d * p_dot_d;
|
|
|
|
|
|
|
|
|
|
if( discriminant > aRadius*aRadius )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
discriminant = sqrtf( aRadius*aRadius - discriminant );
|
|
|
|
|
|
|
|
|
|
aOutT0 = p_dot_d - discriminant;
|
|
|
|
|
aOutT1 = p_dot_d + discriminant;
|
|
|
|
|
|
|
|
|
|
if( aOutT0 > aOutT1 )
|
|
|
|
|
{
|
|
|
|
|
float temp = aOutT0;
|
|
|
|
|
aOutT0 = aOutT1;
|
|
|
|
|
aOutT1 = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
|
RAYSEG2D::RAYSEG2D( const SFVEC2F& s, const SFVEC2F& e )
|
|
|
|
|
{
|
|
|
|
|
m_Start = s;
|
|
|
|
|
m_End = e;
|
|
|
|
|
m_End_minus_start = e - s;
|
|
|
|
|
m_Length = glm::length( m_End_minus_start );
|
|
|
|
|
m_Dir = glm::normalize( m_End_minus_start );
|
2020-12-12 17:29:11 +00:00
|
|
|
|
m_InvDir = ( 1.0f / m_Dir );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
if( fabs( m_Dir.x ) < FLT_EPSILON )
|
|
|
|
|
m_InvDir.x = NextFloatDown( FLT_MAX );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
if( fabs( m_Dir.y ) < FLT_EPSILON )
|
|
|
|
|
m_InvDir.y = NextFloatDown( FLT_MAX );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
m_DOT_End_minus_start = glm::dot( m_End_minus_start, m_End_minus_start );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
|
bool RAYSEG2D::IntersectSegment( const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start,
|
2016-07-19 17:35:25 +00:00
|
|
|
|
float *aOutT ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
|
float rxs = m_End_minus_start.x * aEnd_minus_start.y - m_End_minus_start.y *
|
2016-07-19 17:35:25 +00:00
|
|
|
|
aEnd_minus_start.x;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2017-09-24 11:06:10 +00:00
|
|
|
|
if( std::abs( rxs ) > glm::epsilon<float>() )
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
2016-07-19 17:35:25 +00:00
|
|
|
|
const float inv_rxs = 1.0f / rxs;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
|
const SFVEC2F pq = aStart - m_Start;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
const float t = ( pq.x * aEnd_minus_start.y - pq.y * aEnd_minus_start.x ) * inv_rxs;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
if( ( t < 0.0f ) || ( t > 1.0f ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
float u = ( pq.x * m_End_minus_start.y - pq.y * m_End_minus_start.x ) * inv_rxs;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
if( ( u < 0.0f ) || ( u > 1.0f ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
*aOutT = t;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// http://geomalgorithms.com/a02-_lines.html
|
|
|
|
|
float RAYSEG2D::DistanceToPointSquared( const SFVEC2F &aPoint ) const
|
|
|
|
|
{
|
|
|
|
|
SFVEC2F p = aPoint - m_Start;
|
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
|
const float c1 = glm::dot( p, m_End_minus_start );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
if( c1 < FLT_EPSILON )
|
|
|
|
|
return glm::dot( p, p );
|
|
|
|
|
|
|
|
|
|
if( m_DOT_End_minus_start <= c1 )
|
2020-12-12 17:29:11 +00:00
|
|
|
|
{
|
2015-12-08 07:31:57 +00:00
|
|
|
|
p = aPoint - m_End;
|
2020-12-12 17:29:11 +00:00
|
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2016-07-19 17:35:25 +00:00
|
|
|
|
const float b = c1 / m_DOT_End_minus_start;
|
|
|
|
|
const SFVEC2F pb = m_Start + m_End_minus_start * b;
|
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
|
p = aPoint - pb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return glm::dot( p, p );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
|
bool RAYSEG2D::IntersectCircle( const SFVEC2F &aCenter, float aRadius, float *aOutT0,
|
|
|
|
|
float *aOutT1, SFVEC2F *aOutNormalT0, SFVEC2F *aOutNormalT1 ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
|
{
|
|
|
|
|
// This code used directly from Steve Marschner's CS667 framework
|
|
|
|
|
// http://cs665pd.googlecode.com/svn/trunk/photon/sphere.cpp
|
|
|
|
|
|
|
|
|
|
// Compute some factors used in computation
|
2016-07-19 17:35:25 +00:00
|
|
|
|
const float qx = m_Start.x - aCenter.x;
|
|
|
|
|
const float qy = m_Start.y - aCenter.y;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
|
const float qd = qx * m_Dir.x + qy * m_Dir.y;
|
|
|
|
|
const float qq = qx * qx + qy * qy;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
// solving the quadratic equation for t at the pts of intersection
|
|
|
|
|
// dd*t^2 + (2*qd)*t + (qq-r^2) = 0
|
2016-07-19 17:35:25 +00:00
|
|
|
|
const float discriminantsqr = (qd * qd - (qq - aRadius * aRadius));
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
// If the discriminant is less than zero, there is no intersection
|
|
|
|
|
if( discriminantsqr < FLT_EPSILON )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Otherwise check and make sure that the intersections occur on the ray (t
|
|
|
|
|
// > 0) and return the closer one
|
2017-09-24 11:06:10 +00:00
|
|
|
|
const float discriminant = std::sqrt( discriminantsqr );
|
2020-12-12 17:29:11 +00:00
|
|
|
|
const float t1 = ( -qd - discriminant );
|
|
|
|
|
const float t2 = ( -qd + discriminant );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
if( ( ( t1 < 0.0f ) || ( t1 > m_Length ) ) && ( ( t2 < 0.0f ) || ( t2 > m_Length ) ) )
|
|
|
|
|
return false; // Neither intersection was in the ray's half line.
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
// Convert the intersection to a normalized
|
|
|
|
|
*aOutT0 = t1 / m_Length;
|
|
|
|
|
*aOutT1 = t2 / m_Length;
|
|
|
|
|
|
|
|
|
|
SFVEC2F hitPointT1 = at( t1 );
|
|
|
|
|
SFVEC2F hitPointT2 = at( t2 );
|
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
*aOutNormalT0 = ( hitPointT1 - aCenter ) / aRadius;
|
|
|
|
|
*aOutNormalT1 = ( hitPointT2 - aCenter ) / aRadius;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|