KiCad PCB EDA Suite
marker_base.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2018-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
33 #include "fctsys.h"
34 #include "base_screen.h"
35 #include "common.h"
36 #include "macros.h"
37 #include "marker_base.h"
40 
41 
42 /* The graphic shape of markers is a polygon.
43  * MarkerShapeCorners contains the coordinates of corners of the polygonal default shape
44  * they are arbitrary units to make coding shape easy.
45  * internal units coordinates are these values scaled by .m_ScalingFactor
46  */
47 static const VECTOR2I MarkerShapeCorners[] =
48 {
49  VECTOR2I( 0, 0 ),
50  VECTOR2I( 8, 1 ),
51  VECTOR2I( 4, 3 ),
52  VECTOR2I( 13, 8 ),
53  VECTOR2I( 9, 9 ),
54  VECTOR2I( 8, 13 ),
55  VECTOR2I( 3, 4 ),
56  VECTOR2I( 1, 8 ),
57  VECTOR2I( 0, 0 )
58 };
60 
61 
62 MARKER_BASE::MARKER_BASE( int aScalingFactor, RC_ITEM* aItem, TYPEMARKER aType ) :
63  m_markerType( aType ),
64  m_excluded( false ),
65  m_rcItem( aItem ),
66  m_scalingFactor( aScalingFactor )
67 {
68  const VECTOR2I* point_shape = MarkerShapeCorners;
69  wxPoint start( point_shape->x, point_shape->y );
70  wxPoint end = start;
71 
72  for( int ii = 1; ii < CORNERS_COUNT; ii++ )
73  {
74  ++point_shape;
75  start.x = std::min( start.x, point_shape->x);
76  start.y = std::min( start.y, point_shape->y);
77  end.x = std::max( end.x, point_shape->x);
78  end.y = std::max( end.y, point_shape->y);
79  }
80 
83 }
84 
85 
87 {
88 }
89 
90 
91 void MARKER_BASE::SetData( EDA_UNITS aUnits, int aErrorCode, const wxPoint& aMarkerPos,
92  EDA_ITEM* aItem, const wxPoint& aPos,
93  EDA_ITEM* bItem, const wxPoint& bPos )
94 {
95  m_Pos = aMarkerPos;
96  m_rcItem->SetData( aUnits, aErrorCode, aItem, aPos, bItem, bPos );
97  m_rcItem->SetParent( this );
98 }
99 
100 
101 void MARKER_BASE::SetData( int aErrorCode, const wxPoint& aMarkerPos,
102  const wxString& aText, const wxPoint& aPos,
103  const wxString& bText, const wxPoint& bPos )
104 {
105  m_Pos = aMarkerPos;
106  m_rcItem->SetData( aErrorCode, aText, aPos, bText, bPos );
107  m_rcItem->SetParent( this );
108 }
109 
110 
111 void MARKER_BASE::SetData( EDA_UNITS aUnits, int aErrorCode, const wxPoint& aMarkerPos,
112  EDA_ITEM* aItem,
113  EDA_ITEM* bItem )
114 {
115  m_Pos = aMarkerPos;
116  m_rcItem->SetData( aUnits, aErrorCode, aItem, bItem );
117  m_rcItem->SetParent( this );
118 }
119 
120 
121 void MARKER_BASE::SetData( int aErrorCode, const wxPoint& aMarkerPos,
122  const wxString& aText,
123  const wxString& bText )
124 {
125  m_Pos = aMarkerPos;
126  m_rcItem->SetData( aErrorCode, aText, bText );
127  m_rcItem->SetParent( this );
128 }
129 
130 
131 void MARKER_BASE::SetData( int aErrorCode, const wxPoint& aMarkerPos,
132  const wxString& aText, const KIID& aID,
133  const wxString& bText, const KIID& bID )
134 {
135  m_Pos = aMarkerPos;
136  m_rcItem->SetData( aErrorCode, aText, aID, bText, bID );
137  m_rcItem->SetParent( this );
138 }
139 
140 
141 bool MARKER_BASE::HitTestMarker( const wxPoint& aHitPosition, int aAccuracy ) const
142 {
144  bbox.Inflate( aAccuracy );
145 
146  // Fast hit test using boundary box. A finer test will be made if requested
147  bool hit = bbox.Contains( aHitPosition );
148 
149  if( hit ) // Fine test
150  {
151  SHAPE_LINE_CHAIN polygon;
152  ShapeToPolygon( polygon );
153  VECTOR2I rel_pos( aHitPosition - m_Pos );
154  hit = polygon.PointInside( rel_pos, aAccuracy );
155  }
156 
157  return hit;
158 }
159 
160 
162 {
163  for( const VECTOR2I& corner : MarkerShapeCorners )
164  aPolygon.Append( corner * MarkerScale() );
165 
166  // Be sure aPolygon is seen as a closed polyline:
167  aPolygon.SetClosed( true );
168 }
169 
170 
172 {
173  wxSize size_iu = m_shapeBoundingBox.GetSize();
174  wxPoint position_iu = m_shapeBoundingBox.GetPosition();
175  size_iu.x *= m_scalingFactor;
176  size_iu.y *= m_scalingFactor;
177  position_iu.x *= m_scalingFactor;
178  position_iu.y *= m_scalingFactor;
179  position_iu += m_Pos;
180 
181  return EDA_RECT( position_iu, size_iu );
182 }
183 
184 
185 void MARKER_BASE::PrintMarker( wxDC* aDC, const wxPoint& aOffset )
186 {
187  // Build the marker shape polygon in internal units:
188  std::vector<wxPoint> shape;
189  shape.reserve( CORNERS_COUNT );
190 
191  for( const VECTOR2I& corner : MarkerShapeCorners )
192  shape.emplace_back( corner * MarkerScale() + m_Pos + aOffset );
193 
194  GRClosedPoly( nullptr, aDC, CORNERS_COUNT, &shape[0], true, 0, getColor(), getColor() );
195 }
EDA_UNITS
Definition: common.h:184
MARKER_BASE(int aScalingFactor, RC_ITEM *aItem, TYPEMARKER aType=MARKER_UNSPEC)
Definition: marker_base.cpp:62
void SetData(EDA_UNITS aUnits, int aErrorCode, EDA_ITEM *aMainItem, EDA_ITEM *bAuxItem=nullptr)
Function SetData initialize all data in item.
Definition: rc_item.h:128
virtual ~MARKER_BASE()
Definition: marker_base.cpp:86
RC_ITEM is a holder for a DRC (in Pcbnew) or ERC (in Eeschema) error item.
Definition: rc_item.h:82
wxPoint m_Pos
position of the marker
Definition: marker_base.h:51
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
virtual KIGFX::COLOR4D getColor() const =0
void SetParent(MARKER_BASE *aMarker)
Definition: rc_item.h:255
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
bool Contains(const wxPoint &aPoint) const
Function Contains.
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
This file contains miscellaneous commonly used macros and functions.
EDA_RECT GetBoundingBoxMarker() const
Function GetBoundingBoxMarker returns the orthogonal, bounding box of this object for display purpose...
RC_ITEM * m_rcItem
Definition: marker_base.h:56
Definition: common.h:65
void SetClosed(bool aClosed)
Function SetClosed()
bool HitTestMarker(const wxPoint &aHitPosition, int aAccuracy) const
Tests if the given wxPoint is within the bounds of this object.
const unsigned CORNERS_COUNT
Definition: marker_base.cpp:59
void SetEnd(int x, int y)
Definition: eda_rect.h:192
EDA_RECT m_shapeBoundingBox
Definition: marker_base.h:60
const wxPoint GetPosition() const
Definition: eda_rect.h:115
void ShapeToPolygon(SHAPE_LINE_CHAIN &aPolygon) const
Returns the shape polygon in internal units in a SHAPE_LINE_CHAIN the coordinates are relatives to th...
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:108
int m_scalingFactor
Definition: marker_base.h:58
int MarkerScale() const
The scaling factor to convert polygonal shape coordinates to internal units.
Definition: marker_base.h:72
void PrintMarker(wxDC *aDC, const wxPoint &aOffset)
Function PrintMarker Prints the shape is the polygon defined in m_Corners (array of wxPoints).
SHAPE_LINE_CHAIN.
static const VECTOR2I MarkerShapeCorners[]
Definition: marker_base.cpp:47
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
The common library.
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
void SetData(EDA_UNITS aUnits, int aErrorCode, const wxPoint &aMarkerPos, EDA_ITEM *aItem, const wxPoint &aPos, EDA_ITEM *bItem=nullptr, const wxPoint &bPos=wxPoint())
Function SetData fills in all the reportable data associated with a MARKER.
Definition: marker_base.cpp:91
BASE_SCREEN class implementation.
void GRClosedPoly(EDA_RECT *ClipBox, wxDC *DC, int n, const wxPoint *Points, bool Fill, COLOR4D Color, COLOR4D BgColor)
Function GRClosedPoly draws a closed polygon onto the drawing context aDC and optionally fills and/or...
Definition: gr_basic.cpp:552
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
const wxSize GetSize() const
Definition: eda_rect.h:103