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 4pt_polygon_2d.cpp
|
2016-07-19 17:35:25 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "4pt_polygon_2d.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include <wx/debug.h>
|
2021-01-05 22:19:00 +00:00
|
|
|
#include "../ray.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
POLYGON_4PT_2D::POLYGON_4PT_2D( const SFVEC2F& v1, const SFVEC2F& v2, const SFVEC2F& v3,
|
2020-12-11 13:51:42 +00:00
|
|
|
const SFVEC2F& v4, const BOARD_ITEM& aBoardItem ) :
|
2021-01-02 21:05:29 +00:00
|
|
|
OBJECT_2D( OBJECT_2D_TYPE::POLYGON4PT, aBoardItem )
|
|
|
|
{
|
2020-12-11 13:51:42 +00:00
|
|
|
m_segments[0] = v1;
|
|
|
|
m_segments[1] = v4;
|
|
|
|
m_segments[2] = v3;
|
|
|
|
m_segments[3] = v2;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
unsigned int j = 4 - 1;
|
|
|
|
|
|
|
|
for( i = 0; i < 4; j = i++ )
|
|
|
|
{
|
2020-12-11 13:51:42 +00:00
|
|
|
SFVEC2F slope = m_segments[j] - m_segments[i];
|
2016-07-19 17:35:25 +00:00
|
|
|
m_precalc_slope[i] = slope;
|
2020-12-11 13:51:42 +00:00
|
|
|
m_seg_normal[i] = glm::normalize( SFVEC2F( -slope.y, +slope.x ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_bbox.Reset();
|
|
|
|
m_bbox.Union( v1 );
|
|
|
|
m_bbox.Union( v2 );
|
|
|
|
m_bbox.Union( v3 );
|
|
|
|
m_bbox.Union( v4 );
|
|
|
|
m_bbox.ScaleNextUp();
|
|
|
|
m_bbox.ScaleNextUp();
|
|
|
|
m_bbox.ScaleNextUp();
|
|
|
|
m_bbox.ScaleNextUp();
|
|
|
|
m_bbox.ScaleNextUp();
|
|
|
|
m_centroid = m_bbox.GetCenter();
|
|
|
|
|
|
|
|
wxASSERT( m_bbox.IsInitialized() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool POLYGON_4PT_2D::Intersects( const BBOX_2D& aBBox ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
return m_bbox.Intersects( aBBox );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool POLYGON_4PT_2D::Overlaps( const BBOX_2D& aBBox ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// NOT IMPLEMENTED
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool POLYGON_4PT_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-11 13:51:42 +00:00
|
|
|
bool hited = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
unsigned int hitIndex;
|
2020-12-11 13:51:42 +00:00
|
|
|
float bestHitT;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( unsigned int i = 0; i < 4; i++ )
|
|
|
|
{
|
|
|
|
float t;
|
|
|
|
|
|
|
|
if( aSegRay.IntersectSegment( m_segments[i], m_precalc_slope[i], &t ) )
|
2020-12-11 13:51:42 +00:00
|
|
|
{
|
|
|
|
if( ( hited == false ) || ( t < bestHitT ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-11 13:51:42 +00:00
|
|
|
hited = true;
|
2016-07-19 17:35:25 +00:00
|
|
|
hitIndex = i;
|
|
|
|
bestHitT = t;
|
|
|
|
}
|
2020-12-11 13:51:42 +00:00
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( hited )
|
|
|
|
{
|
2020-12-11 13:51:42 +00:00
|
|
|
wxASSERT( ( bestHitT >= 0.0f ) && ( bestHitT <= 1.0f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-11-04 09:36:41 +00:00
|
|
|
if( aOutT )
|
|
|
|
*aOutT = bestHitT;
|
|
|
|
|
|
|
|
if( aNormalOut )
|
|
|
|
*aNormalOut = m_seg_normal[hitIndex];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
INTERSECTION_RESULT POLYGON_4PT_2D::IsBBoxInside( const BBOX_2D& aBBox ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
// !TODO:
|
|
|
|
|
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 POLYGON_4PT_2D::IsPointInside( const SFVEC2F& aPoint ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2020-12-11 13:51:42 +00:00
|
|
|
unsigned int j = 4 - 1;
|
|
|
|
bool oddNodes = false;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
for( i = 0; i < 4; j = i++ )
|
|
|
|
{
|
|
|
|
const float polyJY = m_segments[j].y;
|
|
|
|
const float polyIY = m_segments[i].y;
|
|
|
|
|
2020-12-11 13:51:42 +00:00
|
|
|
if( ( ( polyIY <= aPoint.y ) && ( polyJY >= aPoint.y ) )
|
|
|
|
|| ( ( polyJY <= aPoint.y ) && ( polyIY >= aPoint.y ) ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
const float polyJX = m_segments[j].x;
|
|
|
|
const float polyIX = m_segments[i].x;
|
|
|
|
|
2020-12-11 13:51:42 +00:00
|
|
|
if( ( polyIX <= aPoint.x ) || ( polyJX <= aPoint.x ) )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-11 13:51:42 +00:00
|
|
|
oddNodes ^= ( ( polyIX + ( ( aPoint.y - polyIY ) / ( polyJY - polyIY ) )
|
|
|
|
* ( polyJX - polyIX ) )
|
|
|
|
< aPoint.x );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return oddNodes;
|
|
|
|
}
|