classes MARKER rework: remove duplicate code. Better HitTest code (HitTest takes in account the actual shape)

This commit is contained in:
jean-pierre charras 2018-12-19 19:53:27 +01:00
parent 15843ae01a
commit 83d851956c
13 changed files with 97 additions and 72 deletions

View File

@ -259,6 +259,12 @@ void CAIRO_GAL_BASE::DrawPolygon( const SHAPE_POLY_SET& aPolySet )
} }
void CAIRO_GAL_BASE::DrawPolygon( const SHAPE_LINE_CHAIN& aPolygon )
{
drawPoly( aPolygon );
}
void CAIRO_GAL_BASE::DrawCurve( const VECTOR2D& aStartPoint, const VECTOR2D& aControlPointA, void CAIRO_GAL_BASE::DrawCurve( const VECTOR2D& aStartPoint, const VECTOR2D& aControlPointA,
const VECTOR2D& aControlPointB, const VECTOR2D& aEndPoint ) const VECTOR2D& aControlPointB, const VECTOR2D& aEndPoint )
{ {

View File

@ -971,24 +971,30 @@ void OPENGL_GAL::DrawPolygon( const SHAPE_POLY_SET& aPolySet )
for( int j = 0; j < aPolySet.OutlineCount(); ++j ) for( int j = 0; j < aPolySet.OutlineCount(); ++j )
{ {
const SHAPE_LINE_CHAIN& outline = aPolySet.COutline( j ); const SHAPE_LINE_CHAIN& outline = aPolySet.COutline( j );
DrawPolygon( outline );
}
}
if( outline.SegmentCount() == 0 )
continue;
const int pointCount = outline.SegmentCount() + 1;
void OPENGL_GAL::DrawPolygon( const SHAPE_LINE_CHAIN& aPolygon )
{
if( aPolygon.SegmentCount() == 0 )
return;
const int pointCount = aPolygon.SegmentCount() + 1;
std::unique_ptr<GLdouble[]> points( new GLdouble[3 * pointCount] ); std::unique_ptr<GLdouble[]> points( new GLdouble[3 * pointCount] );
GLdouble* ptr = points.get(); GLdouble* ptr = points.get();
for( int i = 0; i < pointCount; ++i ) for( int i = 0; i < pointCount; ++i )
{ {
const VECTOR2I& p = outline.CPoint( i ); const VECTOR2I& p = aPolygon.CPoint( i );
*ptr++ = p.x; *ptr++ = p.x;
*ptr++ = p.y; *ptr++ = p.y;
*ptr++ = layerDepth; *ptr++ = layerDepth;
} }
drawPolygon( points.get(), pointCount ); drawPolygon( points.get(), pointCount );
}
} }

View File

@ -31,11 +31,11 @@
#include "fctsys.h" #include "fctsys.h"
//#include "gr_basic.h"
#include "base_screen.h" #include "base_screen.h"
#include "common.h" #include "common.h"
#include "macros.h" #include "macros.h"
#include "marker_base.h" #include "marker_base.h"
#include <geometry/shape_line_chain.h>
#include "dialog_display_info_HTML_base.h" #include "dialog_display_info_HTML_base.h"
@ -96,18 +96,18 @@ MARKER_BASE::MARKER_BASE( const MARKER_BASE& aMarker )
} }
MARKER_BASE::MARKER_BASE() MARKER_BASE::MARKER_BASE( int aScalingFactor )
{ {
m_ScalingFactor = 1; m_ScalingFactor = aScalingFactor;
init(); init();
} }
MARKER_BASE::MARKER_BASE( EDA_UNITS_T aUnits, int aErrorCode, const wxPoint& aMarkerPos, MARKER_BASE::MARKER_BASE( EDA_UNITS_T aUnits, int aErrorCode, const wxPoint& aMarkerPos,
EDA_ITEM* aItem, const wxPoint& aPos, EDA_ITEM* aItem, const wxPoint& aPos,
EDA_ITEM* bItem, const wxPoint& bPos ) EDA_ITEM* bItem, const wxPoint& bPos, int aScalingFactor )
{ {
m_ScalingFactor = 1; m_ScalingFactor = aScalingFactor;
init(); init();
SetData( aUnits, aErrorCode, aMarkerPos, aItem, aPos, bItem, bPos ); SetData( aUnits, aErrorCode, aMarkerPos, aItem, aPos, bItem, bPos );
@ -116,9 +116,9 @@ MARKER_BASE::MARKER_BASE( EDA_UNITS_T aUnits, int aErrorCode, const wxPoint& aMa
MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos, MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
const wxString& aText, const wxPoint& aPos, const wxString& aText, const wxPoint& aPos,
const wxString& bText, const wxPoint& bPos ) const wxString& bText, const wxPoint& bPos, int aScalingFactor )
{ {
m_ScalingFactor = 1; m_ScalingFactor = aScalingFactor;
init(); init();
SetData( aErrorCode, aMarkerPos, aText, aPos, bText, bPos ); SetData( aErrorCode, aMarkerPos, aText, aPos, bText, bPos );
@ -126,9 +126,9 @@ MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos, MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
const wxString& aText, const wxPoint& aPos ) const wxString& aText, const wxPoint& aPos, int aScalingFactor )
{ {
m_ScalingFactor = 1; m_ScalingFactor = aScalingFactor;
init(); init();
SetData( aErrorCode, aMarkerPos, aText, aPos ); SetData( aErrorCode, aMarkerPos, aText, aPos );
@ -164,7 +164,31 @@ bool MARKER_BASE::HitTestMarker( const wxPoint& aHitPosition ) const
{ {
EDA_RECT bbox = GetBoundingBoxMarker(); EDA_RECT bbox = GetBoundingBoxMarker();
return bbox.Contains( aHitPosition ); // Fast hit test using boundary box. A finer test will be made if requested
bool hit = bbox.Contains( aHitPosition );
if( hit ) // Fine test
{
SHAPE_LINE_CHAIN polygon;
ShapeToPolygon( polygon );
VECTOR2I rel_pos( aHitPosition - m_Pos );
hit = polygon.PointInside( rel_pos );
}
return hit;
}
void MARKER_BASE::ShapeToPolygon( SHAPE_LINE_CHAIN& aPolygon) const
{
// Build the marker shape polygon in internal units:
const int ccount = GetShapePolygonCornerCount();
for( int ii = 0; ii < ccount; ii++ )
aPolygon.Append( GetShapePolygonCorner( ii ) * MarkerScale() );
// Be sure aPolygon is seen as a closed polyline:
aPolygon.SetClosed( true );
} }

View File

@ -41,17 +41,15 @@
#define SCALING_FACTOR Millimeter2iu( 0.1 ) #define SCALING_FACTOR Millimeter2iu( 0.1 )
SCH_MARKER::SCH_MARKER() : SCH_ITEM( NULL, SCH_MARKER_T ), MARKER_BASE() SCH_MARKER::SCH_MARKER() : SCH_ITEM( NULL, SCH_MARKER_T ), MARKER_BASE( SCALING_FACTOR )
{ {
m_ScalingFactor = SCALING_FACTOR;
} }
SCH_MARKER::SCH_MARKER( const wxPoint& pos, const wxString& text ) : SCH_MARKER::SCH_MARKER( const wxPoint& pos, const wxString& text ) :
SCH_ITEM( NULL, SCH_MARKER_T ), SCH_ITEM( NULL, SCH_MARKER_T ),
MARKER_BASE( 0, pos, text, pos ) MARKER_BASE( 0, pos, text, pos, SCALING_FACTOR )
{ {
m_ScalingFactor = SCALING_FACTOR;
} }

View File

@ -22,7 +22,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
/* /**
* @file sch_marker.h * @file sch_marker.h
* @brief SCH_MARKER class definition. * @brief SCH_MARKER class definition.
*/ */
@ -34,10 +34,6 @@
#include <marker_base.h> #include <marker_base.h>
/* Names for corresponding types of markers: */
extern const wxChar* NameMarqueurType[];
class SCH_MARKER : public SCH_ITEM, public MARKER_BASE class SCH_MARKER : public SCH_ITEM, public MARKER_BASE
{ {
public: public:

View File

@ -52,6 +52,7 @@
#include <view/view.h> #include <view/view.h>
#include <gal/graphics_abstraction_layer.h> #include <gal/graphics_abstraction_layer.h>
#include <colors_design_settings.h> #include <colors_design_settings.h>
#include <geometry/shape_line_chain.h>
#include "sch_painter.h" #include "sch_painter.h"
@ -1364,16 +1365,8 @@ void SCH_PAINTER::draw( SCH_BITMAP *aBitmap, int aLayer )
void SCH_PAINTER::draw( SCH_MARKER *aMarker, int aLayer ) void SCH_PAINTER::draw( SCH_MARKER *aMarker, int aLayer )
{ {
const int scale = aMarker->MarkerScale(); SHAPE_LINE_CHAIN polygon;
aMarker->ShapeToPolygon( polygon );
// Build the marker shape polygon in internal units:
const int ccount = aMarker->GetShapePolygonCornerCount();
std::vector<VECTOR2D> arrow;
arrow.reserve( ccount );
for( int ii = 0; ii < ccount; ii++ )
arrow.push_back( VECTOR2D( aMarker->GetShapePolygonCorner( ii ).x * scale,
aMarker->GetShapePolygonCorner( ii ).y * scale ) );
COLOR4D color = m_schSettings.GetLayerColor( LAYER_ERC_WARN ); COLOR4D color = m_schSettings.GetLayerColor( LAYER_ERC_WARN );
@ -1385,7 +1378,7 @@ void SCH_PAINTER::draw( SCH_MARKER *aMarker, int aLayer )
m_gal->SetFillColor( color ); m_gal->SetFillColor( color );
m_gal->SetIsFill( true ); m_gal->SetIsFill( true );
m_gal->SetIsStroke( false ); m_gal->SetIsStroke( false );
m_gal->DrawPolygon( &arrow[0], arrow.size() ); m_gal->DrawPolygon( polygon );
m_gal->Restore(); m_gal->Restore();
} }

View File

@ -107,6 +107,7 @@ public:
virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); } virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override { drawPoly( aPointList ); }
virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override { drawPoly( aPointList, aListSize ); } virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override { drawPoly( aPointList, aListSize ); }
virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override; virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override;
virtual void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
/// @copydoc GAL::DrawCurve() /// @copydoc GAL::DrawCurve()
virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA, virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,

View File

@ -168,6 +168,7 @@ public:
virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) {}; virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) {};
virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) {}; virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) {};
virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) {}; virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) {};
virtual void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) {};
/** /**
* @brief Draw a cubic bezier spline. * @brief Draw a cubic bezier spline.

View File

@ -135,6 +135,7 @@ public:
virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override; virtual void DrawPolygon( const std::deque<VECTOR2D>& aPointList ) override;
virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override; virtual void DrawPolygon( const VECTOR2D aPointList[], int aListSize ) override;
virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override; virtual void DrawPolygon( const SHAPE_POLY_SET& aPolySet ) override;
virtual void DrawPolygon( const SHAPE_LINE_CHAIN& aPolySet ) override;
/// @copydoc GAL::DrawCurve() /// @copydoc GAL::DrawCurve()
virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA, virtual void DrawCurve( const VECTOR2D& startPoint, const VECTOR2D& controlPointA,

View File

@ -548,8 +548,8 @@ public:
/** /**
* Function PointInside() * Function PointInside()
* *
* Checks if point aP lies inside a polygon (any type) defined by the line chain. For closed * Checks if point aP lies inside a polygon (any type) defined by the line chain.
* shapes only. * For closed shapes only.
* @param aP point to check * @param aP point to check
* @return true if the point is inside the shape (edge is not treated as being inside). * @return true if the point is inside the shape (edge is not treated as being inside).
*/ */

View File

@ -29,7 +29,7 @@
#include <drc_item.h> #include <drc_item.h>
#include <gr_basic.h> #include <gr_basic.h>
#include <eda_rect.h> #include <eda_rect.h>
class SHAPE_LINE_CHAIN;
/* Marker are mainly used to show a DRC or ERC error or warning /* Marker are mainly used to show a DRC or ERC error or warning
*/ */
@ -67,7 +67,7 @@ protected:
public: public:
MARKER_BASE(); MARKER_BASE( int aScalingFactor );
/** /**
* Constructor * Constructor
@ -77,10 +77,11 @@ public:
* @param aPos The position of the first of two objects * @param aPos The position of the first of two objects
* @param bItem The second of the two conflicting objects * @param bItem The second of the two conflicting objects
* @param bPos The position of the second of two objects * @param bPos The position of the second of two objects
* @param aScalingFactor the scaling factor to convert the shape coordinates to IU coordinates
*/ */
MARKER_BASE( EDA_UNITS_T aUnits, int aErrorCode, const wxPoint& aMarkerPos, MARKER_BASE( EDA_UNITS_T aUnits, int aErrorCode, const wxPoint& aMarkerPos,
EDA_ITEM* aItem, const wxPoint& aPos, EDA_ITEM* aItem, const wxPoint& aPos,
EDA_ITEM* bItem, const wxPoint& bPos ); EDA_ITEM* bItem, const wxPoint& bPos, int aScalingFactor );
/** /**
* Constructor * Constructor
@ -90,10 +91,11 @@ public:
* @param aPos The position of the first of two objects * @param aPos The position of the first of two objects
* @param bText Text describing the second of the two conflicting objects * @param bText Text describing the second of the two conflicting objects
* @param bPos The position of the second of two objects * @param bPos The position of the second of two objects
* @param aScalingFactor the scaling factor to convert the shape coordinates to IU coordinates
*/ */
MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos, MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
const wxString& aText, const wxPoint& aPos, const wxString& aText, const wxPoint& aPos,
const wxString& bText, const wxPoint& bPos ); const wxString& bText, const wxPoint& bPos, int aScalingFactor );
/** /**
* Constructor * Constructor
@ -101,9 +103,10 @@ public:
* @param aMarkerPos The position of the MARKER on the BOARD * @param aMarkerPos The position of the MARKER on the BOARD
* @param aText Text describing the object * @param aText Text describing the object
* @param aPos The position of the object * @param aPos The position of the object
* @param aScalingFactor the scaling factor to convert the shape coordinates to IU coordinates
*/ */
MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos, MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
const wxString& aText, const wxPoint& aPos ); const wxString& aText, const wxPoint& aPos, int aScalingFactor );
/** /**
* Contructor * Contructor
@ -119,7 +122,13 @@ public:
*/ */
int MarkerScale() const { return m_ScalingFactor; } int MarkerScale() const { return m_ScalingFactor; }
/** @return the shape polygon corners list /** Returns the shape polygon in internal units in a SHAPE_LINE_CHAIN
* the coordinates are relatives to the marker position (are not absolute)
* @param aPolygon is the SHAPE_LINE_CHAIN to fill with the shape
*/
void ShapeToPolygon( SHAPE_LINE_CHAIN& aPolygon) const;
/** @return the shape corner list
*/ */
const VECTOR2I* GetShapePolygon() const; const VECTOR2I* GetShapePolygon() const;

View File

@ -46,10 +46,9 @@
MARKER_PCB::MARKER_PCB( BOARD_ITEM* aParent ) : MARKER_PCB::MARKER_PCB( BOARD_ITEM* aParent ) :
BOARD_ITEM( aParent, PCB_MARKER_T ), BOARD_ITEM( aParent, PCB_MARKER_T ),
MARKER_BASE(), m_item( nullptr ) MARKER_BASE( SCALING_FACTOR ), m_item( nullptr )
{ {
m_Color = WHITE; m_Color = WHITE;
m_ScalingFactor = SCALING_FACTOR;
} }
@ -57,10 +56,9 @@ MARKER_PCB::MARKER_PCB( EDA_UNITS_T aUnits, int aErrorCode, const wxPoint& aMark
BOARD_ITEM* aItem, const wxPoint& aPos, BOARD_ITEM* aItem, const wxPoint& aPos,
BOARD_ITEM* bItem, const wxPoint& bPos ) : BOARD_ITEM* bItem, const wxPoint& bPos ) :
BOARD_ITEM( nullptr, PCB_MARKER_T ), // parent set during BOARD::Add() BOARD_ITEM( nullptr, PCB_MARKER_T ), // parent set during BOARD::Add()
MARKER_BASE( aUnits, aErrorCode, aMarkerPos, aItem, aPos, bItem, bPos ), m_item( nullptr ) MARKER_BASE( aUnits, aErrorCode, aMarkerPos, aItem, aPos, bItem, bPos, SCALING_FACTOR ), m_item( nullptr )
{ {
m_Color = WHITE; m_Color = WHITE;
m_ScalingFactor = SCALING_FACTOR;
} }
@ -68,10 +66,9 @@ MARKER_PCB::MARKER_PCB( int aErrorCode, const wxPoint& aMarkerPos,
const wxString& aText, const wxPoint& aPos, const wxString& aText, const wxPoint& aPos,
const wxString& bText, const wxPoint& bPos ) : const wxString& bText, const wxPoint& bPos ) :
BOARD_ITEM( nullptr, PCB_MARKER_T ), // parent set during BOARD::Add() BOARD_ITEM( nullptr, PCB_MARKER_T ), // parent set during BOARD::Add()
MARKER_BASE( aErrorCode, aMarkerPos, aText, aPos, bText, bPos ), m_item( nullptr ) MARKER_BASE( aErrorCode, aMarkerPos, aText, aPos, bText, bPos, SCALING_FACTOR ), m_item( nullptr )
{ {
m_Color = WHITE; m_Color = WHITE;
m_ScalingFactor = SCALING_FACTOR;
} }

View File

@ -42,6 +42,7 @@
#include <gal/graphics_abstraction_layer.h> #include <gal/graphics_abstraction_layer.h>
#include <convert_basic_shapes_to_polygon.h> #include <convert_basic_shapes_to_polygon.h>
#include <geometry/shape_line_chain.h>
using namespace KIGFX; using namespace KIGFX;
@ -1243,16 +1244,8 @@ void PCB_PAINTER::draw( const PCB_TARGET* aTarget )
void PCB_PAINTER::draw( const MARKER_PCB* aMarker ) void PCB_PAINTER::draw( const MARKER_PCB* aMarker )
{ {
const int scale = aMarker->MarkerScale(); SHAPE_LINE_CHAIN polygon;
aMarker->ShapeToPolygon( polygon );
// Build the marker shape polygon in internal units:
const int ccount = aMarker->GetShapePolygonCornerCount();
std::vector<VECTOR2D> arrow;
arrow.reserve( ccount );
for( int ii = 0; ii < ccount; ii++ )
arrow.push_back( VECTOR2D( aMarker->GetShapePolygonCorner( ii ).x * scale,
aMarker->GetShapePolygonCorner( ii ).y * scale ) );
auto strokeColor = m_pcbSettings.GetColor( aMarker, LAYER_DRC ); auto strokeColor = m_pcbSettings.GetColor( aMarker, LAYER_DRC );
@ -1261,7 +1254,7 @@ void PCB_PAINTER::draw( const MARKER_PCB* aMarker )
m_gal->SetFillColor( strokeColor ); m_gal->SetFillColor( strokeColor );
m_gal->SetIsFill( true ); m_gal->SetIsFill( true );
m_gal->SetIsStroke( false ); m_gal->SetIsStroke( false );
m_gal->DrawPolygon( &arrow[0], arrow.size() ); m_gal->DrawPolygon( polygon );
m_gal->Restore(); m_gal->Restore();
} }