2014-11-08 12:25:29 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2018-01-29 10:37:29 +00:00
|
|
|
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2021-06-08 14:09:24 +00:00
|
|
|
* Copyright (C) 2004-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-11-08 12:25:29 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-01-29 10:37:29 +00:00
|
|
|
* @file eda_rect.h
|
2014-11-08 12:25:29 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-29 10:37:29 +00:00
|
|
|
#ifndef EDA_RECT_H
|
|
|
|
#define EDA_RECT_H
|
2014-11-08 12:25:29 +00:00
|
|
|
|
2016-04-07 11:09:31 +00:00
|
|
|
#include <wx/gdicmn.h>
|
2017-03-07 12:06:00 +00:00
|
|
|
#include <math/box2.h>
|
2016-04-07 11:09:31 +00:00
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Handle the component boundary box.
|
|
|
|
*
|
|
|
|
* This class is similar to wxRect, but some wxRect functions are very curious, and are
|
|
|
|
* working only if dimensions are >= 0 (not always the case in KiCad) and also KiCad needs
|
|
|
|
* some specific method which makes this a more suitable class.
|
2014-11-08 12:25:29 +00:00
|
|
|
*/
|
|
|
|
class EDA_RECT
|
|
|
|
{
|
|
|
|
public:
|
2019-08-16 05:54:16 +00:00
|
|
|
EDA_RECT() : m_init( false ) { };
|
2014-11-08 12:25:29 +00:00
|
|
|
|
|
|
|
EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
|
2020-11-14 18:11:28 +00:00
|
|
|
m_pos( aPos ),
|
|
|
|
m_size( aSize ),
|
|
|
|
m_init( true )
|
2014-11-08 12:25:29 +00:00
|
|
|
{ }
|
|
|
|
|
2022-04-10 18:48:45 +00:00
|
|
|
template<class T>
|
|
|
|
EDA_RECT( const BOX2<T> aBox )
|
|
|
|
{
|
|
|
|
m_pos = (wxPoint) aBox.GetPosition();
|
|
|
|
m_size.x = aBox.GetWidth();
|
|
|
|
m_size.y = aBox.GetHeight();
|
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
2018-01-06 05:02:28 +00:00
|
|
|
virtual ~EDA_RECT() { };
|
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
wxPoint Centre() const
|
|
|
|
{
|
2020-12-18 14:04:03 +00:00
|
|
|
return wxPoint( m_pos.x + ( m_size.x >> 1 ), m_pos.y + ( m_size.y >> 1 ) );
|
2014-11-08 12:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Move the rectangle by the \a aMoveVector.
|
|
|
|
*
|
|
|
|
* @param aMoveVector A wxPoint that is the value to move this rectangle.
|
2014-11-08 12:25:29 +00:00
|
|
|
*/
|
|
|
|
void Move( const wxPoint& aMoveVector );
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Ensures that the height ant width are positive.
|
2014-11-08 12:25:29 +00:00
|
|
|
*/
|
|
|
|
void Normalize();
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* @param aPoint the wxPoint to test.
|
|
|
|
* @return true if aPoint is inside the boundary box. A point on a edge is seen as inside.
|
2014-11-08 12:25:29 +00:00
|
|
|
*/
|
|
|
|
bool Contains( const wxPoint& aPoint ) const;
|
2017-03-07 12:06:00 +00:00
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* @param x the x coordinate of the point to test.
|
|
|
|
* @param y the x coordinate of the point to test.
|
2014-11-08 12:25:29 +00:00
|
|
|
* @return true if point is inside the boundary box. A point on a edge is seen as inside
|
|
|
|
*/
|
|
|
|
bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* @param aRect the EDA_RECT to test.
|
|
|
|
* @return true if aRect is Contained. A common edge is seen as contained.
|
2014-11-08 12:25:29 +00:00
|
|
|
*/
|
|
|
|
bool Contains( const EDA_RECT& aRect ) const;
|
|
|
|
|
2020-11-14 18:11:28 +00:00
|
|
|
const wxSize GetSize() const { return m_size; }
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return the max size dimension.
|
2016-07-19 17:35:25 +00:00
|
|
|
*/
|
2020-11-14 18:11:28 +00:00
|
|
|
int GetSizeMax() const { return ( m_size.x > m_size.y ) ? m_size.x : m_size.y; }
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-11-14 18:11:28 +00:00
|
|
|
int GetX() const { return m_pos.x; }
|
|
|
|
int GetY() const { return m_pos.y; }
|
2014-11-08 12:25:29 +00:00
|
|
|
|
2020-11-14 18:11:28 +00:00
|
|
|
const wxPoint GetOrigin() const { return m_pos; }
|
|
|
|
const wxPoint GetPosition() const { return m_pos; }
|
|
|
|
const wxPoint GetEnd() const { return wxPoint( m_pos.x + m_size.x, m_pos.y + m_size.y ); }
|
2020-12-18 14:04:03 +00:00
|
|
|
const wxPoint GetCenter() const
|
|
|
|
{
|
|
|
|
return wxPoint( m_pos.x + ( m_size.x / 2 ), m_pos.y + ( m_size.y / 2 ) );
|
|
|
|
}
|
2014-11-08 12:25:29 +00:00
|
|
|
|
2020-11-14 18:11:28 +00:00
|
|
|
int GetWidth() const { return m_size.x; }
|
|
|
|
int GetHeight() const { return m_size.y; }
|
|
|
|
int GetRight() const { return m_pos.x + m_size.x; }
|
|
|
|
int GetLeft() const { return m_pos.x; }
|
|
|
|
int GetTop() const { return m_pos.y; }
|
|
|
|
int GetBottom() const { return m_pos.y + m_size.y; } // Y axis from top to bottom
|
2014-11-08 12:25:29 +00:00
|
|
|
|
2019-08-16 05:54:16 +00:00
|
|
|
bool IsValid() const
|
2014-11-08 12:25:29 +00:00
|
|
|
{
|
2019-08-16 05:54:16 +00:00
|
|
|
return m_init;
|
|
|
|
}
|
|
|
|
|
2021-07-26 23:47:26 +00:00
|
|
|
void SetOrigin( const wxPoint& pos )
|
2019-08-16 05:54:16 +00:00
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_pos = pos;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetOrigin( int x, int y )
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_pos.x = x;
|
|
|
|
m_pos.y = y;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
2021-07-26 23:47:26 +00:00
|
|
|
void SetSize( const wxSize& size )
|
2019-08-16 05:54:16 +00:00
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_size = size;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSize( int w, int h )
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_size.x = w;
|
|
|
|
m_size.y = h;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Offset( int dx, int dy )
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_pos.x += dx;
|
|
|
|
m_pos.y += dy;
|
2019-08-16 05:54:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 23:47:26 +00:00
|
|
|
void Offset( const wxPoint& offset )
|
2019-08-16 05:54:16 +00:00
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_pos += offset;
|
2019-08-16 05:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetX( int val )
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_pos.x = val;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetY( int val )
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_pos.y = val;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetWidth( int val )
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_size.x = val;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHeight( int val )
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_size.y = val;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetEnd( int x, int y )
|
|
|
|
{
|
|
|
|
SetEnd( wxPoint( x, y ) );
|
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
2021-06-08 14:09:24 +00:00
|
|
|
void SetEnd( const wxPoint& pos )
|
2019-08-16 05:54:16 +00:00
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_size.x = pos.x - m_pos.x;
|
|
|
|
m_size.y = pos.y - m_pos.y;
|
2019-08-16 05:54:16 +00:00
|
|
|
m_init = true;
|
2014-11-08 12:25:29 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 14:56:08 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Mirror the rectangle from the X axis (negate Y pos and size).
|
2015-06-18 14:56:08 +00:00
|
|
|
*/
|
|
|
|
void RevertYAxis()
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
m_pos.y = -m_pos.y;
|
|
|
|
m_size.y = -m_size.y;
|
2015-06-18 14:56:08 +00:00
|
|
|
Normalize();
|
|
|
|
}
|
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Test for a common area between rectangles.
|
2014-11-08 12:25:29 +00:00
|
|
|
*
|
|
|
|
* @param aRect A rectangle to test intersection with.
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return true if the argument rectangle intersects this rectangle.
|
2014-11-08 12:25:29 +00:00
|
|
|
* (i.e. if the 2 rectangles have at least a common point)
|
|
|
|
*/
|
|
|
|
bool Intersects( const EDA_RECT& aRect ) const;
|
|
|
|
|
2017-04-23 00:06:56 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Tests for a common area between this rectangle, and a rectangle with arbitrary rotation
|
2017-04-23 00:06:56 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* @param aRect a rectangle to test intersection with.
|
|
|
|
* @param aRot rectangle rotation (in 1/10 degrees).
|
2017-04-23 00:06:56 +00:00
|
|
|
*/
|
|
|
|
bool Intersects( const EDA_RECT& aRect, double aRot ) const;
|
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Test for a common area between a segment and this rectangle.
|
2014-11-08 12:25:29 +00:00
|
|
|
*
|
|
|
|
* @param aPoint1 First point of the segment to test intersection with.
|
|
|
|
* @param aPoint2 Second point of the segment to test intersection with.
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return true if the argument segment intersects this rectangle.
|
2014-11-08 12:25:29 +00:00
|
|
|
* (i.e. if the segment and rectangle have at least a common point)
|
|
|
|
*/
|
|
|
|
bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
|
|
|
|
|
2020-09-11 23:52:45 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Test for intersection between a segment and this rectangle, returning the intersections.
|
|
|
|
*
|
|
|
|
* @param aPoint1 is the first point of the segment to test intersection with.
|
|
|
|
* @param aPoint2 is the second point of the segment to test intersection with.
|
|
|
|
* @param aIntersection1 will be filled with the first intersection point, if any.
|
|
|
|
* @param aIntersection2 will be filled with the second intersection point, if any.
|
|
|
|
* @return true if the segment intersects the rect.
|
2020-09-11 23:52:45 +00:00
|
|
|
*/
|
|
|
|
bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2,
|
|
|
|
wxPoint* aIntersection1, wxPoint* aIntersection2 ) const;
|
|
|
|
|
2017-04-20 14:53:57 +00:00
|
|
|
/**
|
|
|
|
* Return the point in this rect that is closest to the provided point
|
|
|
|
*/
|
|
|
|
const wxPoint ClosestPointTo( const wxPoint& aPoint ) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the point in this rect that is farthest from the provided point
|
|
|
|
*/
|
|
|
|
const wxPoint FarthestPointTo( const wxPoint& aPoint ) const;
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Test for a common area between a circle and this rectangle.
|
2017-04-20 14:53:57 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* @param aCenter center of the circle.
|
|
|
|
* @param aRadius radius of the circle.
|
2017-04-20 14:53:57 +00:00
|
|
|
*/
|
|
|
|
bool IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const;
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Test for intersection between this rect and the edge (radius) of a circle.
|
2017-04-20 14:53:57 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* @param aCenter center of the circle.
|
|
|
|
* @param aRadius radius of the circle.
|
|
|
|
* @param aWidth width of the circle edge.
|
2017-04-20 14:53:57 +00:00
|
|
|
*/
|
|
|
|
bool IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const;
|
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Overload the cast operator to return a wxRect.
|
|
|
|
*
|
|
|
|
* wxRect does not accept negative values for size, so ensure the wxRect size is always >= 0.
|
2014-11-08 12:25:29 +00:00
|
|
|
*/
|
|
|
|
operator wxRect() const
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
EDA_RECT rect( m_pos, m_size );
|
2014-11-08 12:25:29 +00:00
|
|
|
rect.Normalize();
|
2020-11-14 18:11:28 +00:00
|
|
|
return wxRect( rect.m_pos, rect.m_size );
|
2014-11-08 12:25:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Overload the cast operator to return a BOX2I.
|
|
|
|
*
|
|
|
|
* @return this box shaped as a BOX2I object.
|
2017-03-07 12:06:00 +00:00
|
|
|
*/
|
|
|
|
operator BOX2I() const
|
|
|
|
{
|
2020-11-14 18:11:28 +00:00
|
|
|
EDA_RECT rect( m_pos, m_size );
|
2017-03-07 12:06:00 +00:00
|
|
|
rect.Normalize();
|
2017-12-10 22:04:23 +00:00
|
|
|
return BOX2I( rect.GetOrigin(), rect.GetSize() );
|
2017-03-07 12:06:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Inflate the rectangle horizontally by \a dx and vertically by \a dy. If \a dx
|
2014-11-08 12:25:29 +00:00
|
|
|
* and/or \a dy is negative the rectangle is deflated.
|
|
|
|
*/
|
|
|
|
EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Inflate the rectangle horizontally and vertically by \a aDelta. If \a aDelta
|
2014-11-08 12:25:29 +00:00
|
|
|
* is negative the rectangle is deflated.
|
|
|
|
*/
|
|
|
|
EDA_RECT& Inflate( int aDelta );
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Modify the position and size of the rectangle in order to contain \a aRect.
|
|
|
|
*
|
|
|
|
* It is mainly used to calculate bounding boxes.
|
|
|
|
*
|
2014-11-08 12:25:29 +00:00
|
|
|
* @param aRect The rectangle to merge with this rectangle.
|
|
|
|
*/
|
|
|
|
void Merge( const EDA_RECT& aRect );
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Modify the position and size of the rectangle in order to contain the given point.
|
|
|
|
*
|
2014-11-08 12:25:29 +00:00
|
|
|
* @param aPoint The point to merge with the rectangle.
|
|
|
|
*/
|
|
|
|
void Merge( const wxPoint& aPoint );
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the area of the rectangle.
|
|
|
|
*
|
2014-11-08 12:25:29 +00:00
|
|
|
* @return The area of the rectangle.
|
|
|
|
*/
|
|
|
|
double GetArea() const;
|
|
|
|
|
2015-07-30 11:49:35 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the area that is common with another rectangle.
|
|
|
|
*
|
2015-07-30 11:49:35 +00:00
|
|
|
* @param aRect is the rectangle to find the common area with.
|
|
|
|
* @return The common area rect or 0-sized rectangle if there is no intersection.
|
|
|
|
*/
|
|
|
|
EDA_RECT Common( const EDA_RECT& aRect ) const;
|
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Useful to calculate bounding box of rotated items, when rotation if not k*90 degrees.
|
|
|
|
*
|
|
|
|
* @param aAngle the rotation angle in 0.1 deg.
|
|
|
|
* @param aRotCenter the rotation point.
|
2021-06-08 14:09:24 +00:00
|
|
|
* @return the bounding box of this, after rotation.
|
2014-11-08 12:25:29 +00:00
|
|
|
*/
|
2021-06-08 14:09:24 +00:00
|
|
|
const EDA_RECT GetBoundingBoxRotated( const wxPoint& aRotCenter, double aAngle ) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxPoint m_pos; // Rectangle Origin
|
|
|
|
wxSize m_size; // Rectangle Size
|
|
|
|
bool m_init; // Is the rectangle initialized
|
2014-11-08 12:25:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-01-29 10:37:29 +00:00
|
|
|
#endif // EDA_RECT_H
|