From 46feb76aba24bf4af74a559bad10b2eb91c46d0a Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Thu, 15 Aug 2019 22:54:16 -0700 Subject: [PATCH] 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. --- common/base_struct.cpp | 30 +++++++++++++ include/eda_rect.h | 95 +++++++++++++++++++++++++++++++++++------- 2 files changed, 110 insertions(+), 15 deletions(-) diff --git a/common/base_struct.cpp b/common/base_struct.cpp index 63ff40a359..a121781ab7 100644 --- a/common/base_struct.cpp +++ b/common/base_struct.cpp @@ -360,6 +360,9 @@ bool EDA_RECT::Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) cons bool EDA_RECT::Intersects( const EDA_RECT& aRect ) const { + if( !m_init ) + return false; + // this logic taken from wxWidgets' geometry.cpp file: bool rc; 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 { + if( !m_init ) + return false; + /* Most rectangles will be axis aligned. * It is quicker to check for this case and pass the rect * 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 { + if( !m_init ) + return false; + wxPoint closest = ClosestPointTo( aCenter ); 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 { + if( !m_init ) + return false; + EDA_RECT me( *this ); 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 ) { + if( !m_init ) + { + if( aRect.IsValid() ) + { + m_Pos = aRect.GetPosition(); + m_Size = aRect.GetSize(); + m_init = true; + } + return; + } + Normalize(); // ensure width and height >= 0 EDA_RECT rect = aRect; 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 ) { + if( !m_init ) + { + m_Pos = aPoint; + m_Size = wxSize( 0, 0 ); + return; + } + Normalize(); // ensure width and height >= 0 wxPoint end = GetEnd(); diff --git a/include/eda_rect.h b/include/eda_rect.h index 9344530d85..69cb8efb6a 100644 --- a/include/eda_rect.h +++ b/include/eda_rect.h @@ -46,13 +46,15 @@ class EDA_RECT private: wxPoint m_Pos; // Rectangle Origin wxSize m_Size; // Rectangle Size + bool m_init; // Is the rectangle initialized public: - EDA_RECT() { }; + EDA_RECT() : m_init( false ) { }; EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) : m_Pos( aPos ), - m_Size( aSize ) + m_Size( aSize ), + m_init( true ) { } virtual ~EDA_RECT() { }; @@ -121,20 +123,83 @@ public: int GetTop() const { return m_Pos.y; } int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom - void SetOrigin( const wxPoint& pos ) { m_Pos = pos; } - void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; } - void SetSize( const wxSize& size ) { m_Size = size; } - 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 SetX( int val ) { m_Pos.x = val; } - void SetY( int val ) { m_Pos.y = val; } - void SetWidth( int val ) { m_Size.x = val; } - void SetHeight( int val ) { m_Size.y = val; } - void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); } - void SetEnd( const wxPoint& pos ) + bool IsValid() const { - m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y; + return m_init; + } + + void SetOrigin( const wxPoint &pos ) + { + m_Pos = pos; + m_init = true; + } + + 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 ) + { + m_Size.x = pos.x - m_Pos.x; + m_Size.y = pos.y - m_Pos.y; + m_init = true; } /**