KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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 "base_screen.h"
34#include "marker_base.h"
35#include <core/arraydim.h>
37#include <render_settings.h>
39
40
49{
50 VECTOR2I( 0, 0 ),
51 VECTOR2I( 8, 1 ),
52 VECTOR2I( 4, 3 ),
53 VECTOR2I( 13, 8 ),
54 VECTOR2I( 9, 9 ),
55 VECTOR2I( 8, 13 ),
56 VECTOR2I( 3, 4 ),
57 VECTOR2I( 1, 8 ),
58 VECTOR2I( 0, 0 )
59};
60
62
63
64MARKER_BASE::MARKER_BASE( int aScalingFactor, std::shared_ptr<RC_ITEM> aItem, MARKER_T aType ) :
65 m_markerType( aType ),
66 m_excluded( false ),
67 m_rcItem( std::move( aItem ) ),
68 m_scalingFactor( aScalingFactor )
69{
70 const VECTOR2I* point_shape = MarkerShapeCorners;
71 VECTOR2I start( point_shape->x, point_shape->y );
72 VECTOR2I end = start;
73
74 for( unsigned ii = 1; ii < CORNERS_COUNT; ii++ )
75 {
76 ++point_shape;
77 start.x = std::min( start.x, point_shape->x );
78 start.y = std::min( start.y, point_shape->y );
79 end.x = std::max( end.x, point_shape->x );
80 end.y = std::max( end.y, point_shape->y );
81 }
82
85}
86
87
89{
90}
91
92
93bool MARKER_BASE::HitTestMarker( const VECTOR2I& aHitPosition, int aAccuracy ) const
94{
95 const BOX2I bbox = GetBoundingBoxMarker().GetInflated( aAccuracy );
96
97 // Fast hit test using boundary box. A finer test will be made if requested
98 bool hit = bbox.Contains( aHitPosition );
99
100 if( hit ) // Fine test
101 {
102 SHAPE_LINE_CHAIN polygon;
103 ShapeToPolygon( polygon );
104 VECTOR2I rel_pos( aHitPosition - m_Pos );
105 hit = polygon.PointInside( rel_pos, aAccuracy );
106 }
107
108 return hit;
109}
110
111
112bool MARKER_BASE::HitTestMarker( const BOX2I& aRect, bool aContained, int aAccuracy ) const
113{
114 const BOX2I bbox = GetBoundingBoxMarker().GetInflated( aAccuracy );
115
116 if( aContained )
117 return aRect.Contains( bbox );
118
119 return aRect.Intersects( bbox );
120}
121
122
123void MARKER_BASE::ShapeToPolygon( SHAPE_LINE_CHAIN& aPolygon, int aScale ) const
124{
125 if( aScale < 0 )
126 aScale = MarkerScale();
127
128 for( const VECTOR2I& corner : MarkerShapeCorners )
129 aPolygon.Append( corner * aScale );
130
131 // Be sure aPolygon is seen as a closed polyline:
132 aPolygon.SetClosed( true );
133}
134
135
137{
139
140 VECTOR2I pos = m_Pos;
142
143 return BOX2I( pos, bbox.GetSize() * m_scalingFactor );
144}
145
146
147void MARKER_BASE::PrintMarker( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset )
148{
149 wxDC* DC = aSettings->GetPrintDC();
150
151 // Build the marker shape polygon in internal units:
152 std::vector<VECTOR2I> shape;
153 shape.reserve( CORNERS_COUNT );
154
155 for( const VECTOR2I& corner : MarkerShapeCorners )
156 shape.emplace_back( corner * MarkerScale() + m_Pos + aOffset );
157
158 GRClosedPoly( DC, CORNERS_COUNT, &shape[0], true, getColor() );
159}
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
BASE_SCREEN class implementation.
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
constexpr const Vec & GetPosition() const
Definition: box2.h:211
constexpr void SetOrigin(const Vec &pos)
Definition: box2.h:237
constexpr bool Contains(const Vec &aPoint) const
Definition: box2.h:168
constexpr BOX2< Vec > GetInflated(coord_type aDx, coord_type aDy) const
Get a new rectangle that is this one, inflated by aDx and aDy.
Definition: box2.h:638
constexpr const SizeVec & GetSize() const
Definition: box2.h:206
constexpr void SetEnd(coord_type x, coord_type y)
Definition: box2.h:297
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:311
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
wxDC * GetPrintDC() const
void PrintMarker(const RENDER_SETTINGS *aSettings, const VECTOR2I &aOffset)
Print the shape is the polygon defined in m_Corners (array of VECTOR2Is).
bool HitTestMarker(const VECTOR2I &aHitPosition, int aAccuracy) const
Test if the given VECTOR2I is within the bounds of this object.
Definition: marker_base.cpp:93
virtual KIGFX::COLOR4D getColor() const =0
int m_scalingFactor
Scaling factor to convert corners coordinates to internal units coordinates.
Definition: marker_base.h:148
int MarkerScale() const
The scaling factor to convert polygonal shape coordinates to internal units.
Definition: marker_base.h:69
VECTOR2I m_Pos
Position of the marker.
Definition: marker_base.h:140
virtual ~MARKER_BASE()
Definition: marker_base.cpp:88
BOX2I m_shapeBoundingBox
Bounding box of the graphic symbol relative to the position of the shape in marker shape units.
Definition: marker_base.h:150
void ShapeToPolygon(SHAPE_LINE_CHAIN &aPolygon, int aScale=-1) const
Return the shape polygon in internal units in a SHAPE_LINE_CHAIN the coordinates are relatives to the...
MARKER_BASE(int aScalingFactor, std::shared_ptr< RC_ITEM > aItem, MARKER_T aType=MARKER_UNSPEC)
Definition: marker_base.cpp:64
BOX2I GetBoundingBoxMarker() const
Return the orthogonal, bounding box of this object for display purposes.
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
void GRClosedPoly(wxDC *DC, int n, const VECTOR2I *Points, bool Fill, const COLOR4D &Color)
Draw a closed polyline and fill it if Fill, in object space.
Definition: gr_basic.cpp:352
static const VECTOR2I MarkerShapeCorners[]
The graphic shape of markers is a polygon.
Definition: marker_base.cpp:48
const unsigned CORNERS_COUNT
Definition: marker_base.cpp:61
STL namespace.
VECTOR2I end
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695