2016-07-19 17:35:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
|
2020-12-11 13:51:42 +00:00
|
|
|
* Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-07-19 17:35:25 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-01-03 22:23:00 +00:00
|
|
|
* @file ring_2d.cpp
|
2016-07-19 17:35:25 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "ring_2d.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include "../../../3d_fastmath.h"
|
2021-01-05 22:19:00 +00:00
|
|
|
#include "../ray.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include <wx/debug.h>
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
RING_2D::RING_2D( const SFVEC2F& aCenter, float aInnerRadius, float aOuterRadius,
|
2020-12-11 13:51:42 +00:00
|
|
|
const BOARD_ITEM& aBoardItem ) :
|
2021-01-02 21:05:29 +00:00
|
|
|
OBJECT_2D( OBJECT_2D_TYPE::RING, aBoardItem )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
wxASSERT( aInnerRadius < aOuterRadius );
|
|
|
|
|
|
|
|
m_center = aCenter;
|
|
|
|
m_inner_radius = aInnerRadius;
|
|
|
|
m_outer_radius = aOuterRadius;
|
|
|
|
|
|
|
|
m_inner_radius_squared = aInnerRadius * aInnerRadius;
|
|
|
|
m_outer_radius_squared = aOuterRadius * aOuterRadius;
|
|
|
|
|
|
|
|
m_bbox.Reset();
|
|
|
|
m_bbox.Set( m_center - SFVEC2F( aOuterRadius, aOuterRadius ),
|
|
|
|
m_center + SFVEC2F( aOuterRadius, aOuterRadius ) );
|
|
|
|
m_bbox.ScaleNextUp();
|
|
|
|
m_centroid = m_bbox.GetCenter();
|
|
|
|
|
|
|
|
wxASSERT( m_bbox.IsInitialized() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool RING_2D::Overlaps( const BBOX_2D& aBBox ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
// NOT IMPLEMENTED, why?
|
2016-07-19 17:35:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool RING_2D::Intersects( const BBOX_2D& aBBox ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-11 13:51:42 +00:00
|
|
|
// !TODO: check the inside for a great improvement
|
2016-07-19 17:35:25 +00:00
|
|
|
return aBBox.Intersects( m_center, m_outer_radius_squared );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool RING_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
|
2016-07-19 17:35:25 +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
|
2021-01-02 21:05:29 +00:00
|
|
|
const float qx = ( aSegRay.m_Start.x - m_center.x );
|
|
|
|
const float qy = ( aSegRay.m_Start.y - m_center.y );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
const float qd = qx * aSegRay.m_Dir.x + qy * aSegRay.m_Dir.y;
|
|
|
|
const float qq = qx * qx + qy * qy;
|
|
|
|
|
|
|
|
// solving the quadratic equation for t at the pts of intersection
|
|
|
|
// dd*t^2 + (2*qd)*t + (qq-r^2) = 0
|
|
|
|
const float discriminantsqr = qd * qd - qq;
|
2019-08-20 17:02:28 +00:00
|
|
|
const float discriminantsqr_outer = discriminantsqr + m_outer_radius_squared;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// If the discriminant is less than zero, there is no intersection
|
2019-08-20 17:02:28 +00:00
|
|
|
if( discriminantsqr_outer < FLT_EPSILON )
|
2016-07-19 17:35:25 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise check and make sure that the intersections occur on the ray (t
|
|
|
|
// > 0) and return the closer one
|
2019-08-20 17:02:28 +00:00
|
|
|
const float discriminant = sqrt( discriminantsqr_outer );
|
2021-01-02 21:05:29 +00:00
|
|
|
float t = ( -qd - discriminant );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
if( ( t > FLT_EPSILON ) && ( t < aSegRay.m_Length ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-11-04 09:36:41 +00:00
|
|
|
if( aNormalOut )
|
|
|
|
{
|
|
|
|
SFVEC2F hitPoint = aSegRay.at( t );
|
|
|
|
*aNormalOut = (hitPoint - m_center) / m_outer_radius;
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const float discriminantsqr_inter = discriminantsqr + m_inner_radius_squared;
|
|
|
|
|
|
|
|
if( discriminantsqr_inter > FLT_EPSILON )
|
|
|
|
{
|
|
|
|
const float discriminant_inner = sqrt( discriminantsqr_inter );
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const float t2_inner = ( -qd + discriminant_inner );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
if( ( t2_inner > FLT_EPSILON ) && ( t2_inner < aSegRay.m_Length ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
t = t2_inner;
|
|
|
|
|
2020-11-04 09:36:41 +00:00
|
|
|
if( aNormalOut )
|
|
|
|
{
|
|
|
|
const SFVEC2F hitPoint = aSegRay.at( t2_inner );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
*aNormalOut = ( m_center - hitPoint ) / m_inner_radius;
|
2020-11-04 09:36:41 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
2020-12-11 13:51:42 +00:00
|
|
|
{
|
2016-07-19 17:35:25 +00:00
|
|
|
return false;
|
2020-12-11 13:51:42 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
2020-12-11 13:51:42 +00:00
|
|
|
{
|
2016-07-19 17:35:25 +00:00
|
|
|
return false;
|
2020-12-11 13:51:42 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( (t > 0.0f) && (t <= aSegRay.m_Length) );
|
|
|
|
|
|
|
|
// Convert the intersection to a normalized 0.0 .. 1.0
|
2020-11-04 09:36:41 +00:00
|
|
|
if( aOutT )
|
|
|
|
*aOutT = t / aSegRay.m_Length;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
INTERSECTION_RESULT RING_2D::IsBBoxInside( const BBOX_2D& aBBox ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
return INTERSECTION_RESULT::MISSES;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool RING_2D::IsPointInside( const SFVEC2F& aPoint ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
const SFVEC2F v = m_center - aPoint;
|
|
|
|
|
|
|
|
const float dot = glm::dot( v, v );
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
if( ( dot <= m_outer_radius_squared ) && ( dot >= m_inner_radius_squared ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|