2014-05-14 09:47:17 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SHAPE_SEGMENT_H
|
|
|
|
#define __SHAPE_SEGMENT_H
|
|
|
|
|
|
|
|
#include <geometry/seg.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <geometry/shape.h>
|
|
|
|
#include <math/box2.h>
|
|
|
|
#include <math/vector2d.h>
|
2014-05-14 09:47:17 +00:00
|
|
|
|
2020-07-11 09:37:22 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-12-25 22:13:47 +00:00
|
|
|
class SHAPE_SEGMENT : public SHAPE
|
|
|
|
{
|
2014-05-14 09:47:17 +00:00
|
|
|
public:
|
2020-12-25 22:13:47 +00:00
|
|
|
SHAPE_SEGMENT() :
|
|
|
|
SHAPE( SH_SEGMENT ),
|
|
|
|
m_width( 0 )
|
|
|
|
{};
|
2014-05-14 09:47:17 +00:00
|
|
|
|
2020-12-25 22:13:47 +00:00
|
|
|
SHAPE_SEGMENT( const VECTOR2I& aA, const VECTOR2I& aB, int aWidth = 0 ) :
|
|
|
|
SHAPE( SH_SEGMENT ),
|
|
|
|
m_seg( aA, aB ),
|
|
|
|
m_width( aWidth )
|
|
|
|
{};
|
2014-05-14 09:47:17 +00:00
|
|
|
|
2020-12-25 22:13:47 +00:00
|
|
|
SHAPE_SEGMENT( const SEG& aSeg, int aWidth = 0 ) :
|
|
|
|
SHAPE( SH_SEGMENT ),
|
|
|
|
m_seg( aSeg ),
|
|
|
|
m_width( aWidth )
|
|
|
|
{};
|
2014-11-14 19:19:00 +00:00
|
|
|
|
|
|
|
~SHAPE_SEGMENT() {};
|
2014-05-14 09:47:17 +00:00
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
SHAPE* Clone() const override
|
2014-05-14 09:47:17 +00:00
|
|
|
{
|
|
|
|
return new SHAPE_SEGMENT( m_seg, m_width );
|
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
const BOX2I BBox( int aClearance = 0 ) const override
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
2015-08-10 15:50:10 +00:00
|
|
|
return BOX2I( m_seg.A, m_seg.B - m_seg.A ).Inflate( aClearance + ( m_width + 1 ) / 2 );
|
2014-11-14 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:48:22 +00:00
|
|
|
bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
|
|
|
|
{
|
|
|
|
return SHAPE::Collide( aShape, aClearance, aMTV );
|
|
|
|
}
|
|
|
|
|
2020-09-28 22:27:33 +00:00
|
|
|
bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override
|
2020-07-04 18:48:22 +00:00
|
|
|
{
|
2020-09-28 22:27:33 +00:00
|
|
|
return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
|
2020-07-04 18:48:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 22:27:33 +00:00
|
|
|
bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
2021-11-27 14:08:23 +00:00
|
|
|
if( aSeg.A == aSeg.B )
|
|
|
|
return Collide( aSeg.A, aClearance, aActual, aLocation );
|
|
|
|
|
2020-07-02 16:06:09 +00:00
|
|
|
int min_dist = ( m_width + 1 ) / 2 + aClearance;
|
|
|
|
ecoord dist_sq = m_seg.SquaredDistance( aSeg );
|
|
|
|
|
2020-09-28 22:27:33 +00:00
|
|
|
if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
|
2020-07-02 16:06:09 +00:00
|
|
|
{
|
2020-09-28 22:27:33 +00:00
|
|
|
if( aLocation )
|
|
|
|
*aLocation = m_seg.NearestPoint( aSeg );
|
|
|
|
|
2020-07-02 16:06:09 +00:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-11-14 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 22:27:33 +00:00
|
|
|
bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
|
|
|
|
VECTOR2I* aLocation = nullptr ) const override
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
2020-07-02 16:06:09 +00:00
|
|
|
int min_dist = ( m_width + 1 ) / 2 + aClearance;
|
|
|
|
ecoord dist_sq = m_seg.SquaredDistance( aP );
|
|
|
|
|
2020-09-28 22:27:33 +00:00
|
|
|
if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
|
2020-07-02 16:06:09 +00:00
|
|
|
{
|
2020-09-28 22:27:33 +00:00
|
|
|
if( aLocation )
|
|
|
|
*aLocation = m_seg.NearestPoint( aP );
|
|
|
|
|
2020-07-02 16:06:09 +00:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-11-14 19:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetSeg( const SEG& aSeg )
|
|
|
|
{
|
|
|
|
m_seg = aSeg;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SEG& GetSeg() const
|
|
|
|
{
|
|
|
|
return m_seg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetWidth( int aWidth )
|
|
|
|
{
|
|
|
|
m_width = aWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetWidth() const
|
|
|
|
{
|
|
|
|
return m_width;
|
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
bool IsSolid() const override
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:06:09 +00:00
|
|
|
void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
|
|
|
|
{
|
|
|
|
m_seg.A -= aCenter;
|
|
|
|
m_seg.B -= aCenter;
|
|
|
|
|
|
|
|
m_seg.A = m_seg.A.Rotate( aAngle );
|
|
|
|
m_seg.B = m_seg.B.Rotate( aAngle );
|
|
|
|
|
|
|
|
m_seg.A += aCenter;
|
|
|
|
m_seg.B += aCenter;
|
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
void Move( const VECTOR2I& aVector ) override
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
|
|
|
m_seg.A += aVector;
|
|
|
|
m_seg.B += aVector;
|
|
|
|
}
|
2014-11-14 18:18:31 +00:00
|
|
|
|
2020-10-07 13:15:16 +00:00
|
|
|
virtual const std::string Format( ) const override;
|
2020-10-06 09:22:07 +00:00
|
|
|
|
2014-05-14 09:47:17 +00:00
|
|
|
private:
|
2014-11-14 19:19:00 +00:00
|
|
|
SEG m_seg;
|
|
|
|
int m_width;
|
2014-05-14 09:47:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|