Allow EDA_RECT to properly be uninitialized

The base initialization of EDA_RECT sets a 0/0/0/0 rectangle that
prevents merging properly with valid EDA_RECT.  This sets the default to
be uninitialized until the internal data are set.
This commit is contained in:
Seth Hillbrand 2019-08-15 22:54:16 -07:00
parent 3135b63976
commit 46feb76aba
2 changed files with 110 additions and 15 deletions

View File

@ -360,6 +360,9 @@ bool EDA_RECT::Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) cons
bool EDA_RECT::Intersects( const EDA_RECT& aRect ) const bool EDA_RECT::Intersects( const EDA_RECT& aRect ) const
{ {
if( !m_init )
return false;
// this logic taken from wxWidgets' geometry.cpp file: // this logic taken from wxWidgets' geometry.cpp file:
bool rc; bool rc;
EDA_RECT me(*this); EDA_RECT me(*this);
@ -388,6 +391,9 @@ bool EDA_RECT::Intersects( const EDA_RECT& aRect ) const
bool EDA_RECT::Intersects( const EDA_RECT& aRect, double aRot ) const bool EDA_RECT::Intersects( const EDA_RECT& aRect, double aRot ) const
{ {
if( !m_init )
return false;
/* Most rectangles will be axis aligned. /* Most rectangles will be axis aligned.
* It is quicker to check for this case and pass the rect * It is quicker to check for this case and pass the rect
* to the simpler intersection test * to the simpler intersection test
@ -520,6 +526,9 @@ const wxPoint EDA_RECT::FarthestPointTo( const wxPoint& aPoint ) const
bool EDA_RECT::IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const bool EDA_RECT::IntersectsCircle( const wxPoint& aCenter, const int aRadius ) const
{ {
if( !m_init )
return false;
wxPoint closest = ClosestPointTo( aCenter ); wxPoint closest = ClosestPointTo( aCenter );
double dx = aCenter.x - closest.x; double dx = aCenter.x - closest.x;
@ -533,6 +542,9 @@ bool EDA_RECT::IntersectsCircle( const wxPoint& aCenter, const int aRadius ) con
bool EDA_RECT::IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const bool EDA_RECT::IntersectsCircleEdge( const wxPoint& aCenter, const int aRadius, const int aWidth ) const
{ {
if( !m_init )
return false;
EDA_RECT me( *this ); EDA_RECT me( *this );
me.Normalize(); // ensure size is >= 0 me.Normalize(); // ensure size is >= 0
@ -630,6 +642,17 @@ EDA_RECT& EDA_RECT::Inflate( wxCoord dx, wxCoord dy )
void EDA_RECT::Merge( const EDA_RECT& aRect ) void EDA_RECT::Merge( const EDA_RECT& aRect )
{ {
if( !m_init )
{
if( aRect.IsValid() )
{
m_Pos = aRect.GetPosition();
m_Size = aRect.GetSize();
m_init = true;
}
return;
}
Normalize(); // ensure width and height >= 0 Normalize(); // ensure width and height >= 0
EDA_RECT rect = aRect; EDA_RECT rect = aRect;
rect.Normalize(); // ensure width and height >= 0 rect.Normalize(); // ensure width and height >= 0
@ -647,6 +670,13 @@ void EDA_RECT::Merge( const EDA_RECT& aRect )
void EDA_RECT::Merge( const wxPoint& aPoint ) void EDA_RECT::Merge( const wxPoint& aPoint )
{ {
if( !m_init )
{
m_Pos = aPoint;
m_Size = wxSize( 0, 0 );
return;
}
Normalize(); // ensure width and height >= 0 Normalize(); // ensure width and height >= 0
wxPoint end = GetEnd(); wxPoint end = GetEnd();

View File

@ -46,13 +46,15 @@ class EDA_RECT
private: private:
wxPoint m_Pos; // Rectangle Origin wxPoint m_Pos; // Rectangle Origin
wxSize m_Size; // Rectangle Size wxSize m_Size; // Rectangle Size
bool m_init; // Is the rectangle initialized
public: public:
EDA_RECT() { }; EDA_RECT() : m_init( false ) { };
EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) : EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
m_Pos( aPos ), m_Pos( aPos ),
m_Size( aSize ) m_Size( aSize ),
m_init( true )
{ } { }
virtual ~EDA_RECT() { }; virtual ~EDA_RECT() { };
@ -121,20 +123,83 @@ public:
int GetTop() const { return m_Pos.y; } int GetTop() const { return m_Pos.y; }
int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
void SetOrigin( const wxPoint& pos ) { m_Pos = pos; } bool IsValid() const
void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; } {
void SetSize( const wxSize& size ) { m_Size = size; } return m_init;
void SetSize( int w, int h ) { m_Size.x = w; m_Size.y = h; } }
void Offset( int dx, int dy ) { m_Pos.x += dx; m_Pos.y += dy; }
void Offset( const wxPoint& offset ) { m_Pos += offset; } void SetOrigin( const wxPoint &pos )
void SetX( int val ) { m_Pos.x = val; } {
void SetY( int val ) { m_Pos.y = val; } m_Pos = pos;
void SetWidth( int val ) { m_Size.x = val; } m_init = true;
void SetHeight( int val ) { m_Size.y = val; } }
void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); }
void SetOrigin( int x, int y )
{
m_Pos.x = x;
m_Pos.y = y;
m_init = true;
}
void SetSize( const wxSize &size )
{
m_Size = size;
m_init = true;
}
void SetSize( int w, int h )
{
m_Size.x = w;
m_Size.y = h;
m_init = true;
}
void Offset( int dx, int dy )
{
m_Pos.x += dx;
m_Pos.y += dy;
}
void Offset( const wxPoint &offset )
{
m_Pos += offset;
}
void SetX( int val )
{
m_Pos.x = val;
m_init = true;
}
void SetY( int val )
{
m_Pos.y = val;
m_init = true;
}
void SetWidth( int val )
{
m_Size.x = val;
m_init = true;
}
void SetHeight( int val )
{
m_Size.y = val;
m_init = true;
}
void SetEnd( int x, int y )
{
SetEnd( wxPoint( x, y ) );
m_init = true;
}
void SetEnd( const wxPoint &pos ) void SetEnd( const wxPoint &pos )
{ {
m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y; m_Size.x = pos.x - m_Pos.x;
m_Size.y = pos.y - m_Pos.y;
m_init = true;
} }
/** /**