KiCad PCB EDA Suite
Loading...
Searching...
No Matches
label_manager.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 The KiCad Developers.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <gal/color4d.h>
22#include <view/view_overlay.h>
23#include <string>
24
25#include "label_manager.h"
26
27using KIGFX::GAL;
28using KIGFX::COLOR4D;
30
32{
33};
34
35
39
40
41void LABEL_MANAGER::Add( VECTOR2I target, wxString msg, COLOR4D color )
42{
43 LABEL lbl;
44
45 lbl.m_target = target;
46 lbl.m_msg = msg;
47 lbl.m_color = color;
48 m_canvas->SetGlyphSize( VECTOR2D( m_textSize, m_textSize ) );
49
50 KIFONT::FONT* strokeFont = KIFONT::FONT::GetFont( wxEmptyString );
51 UTF8 text( msg );
53 m_textSize / 8, false, false,
55
56 lbl.m_bbox.SetOrigin( lbl.m_target - textDims - VECTOR2I( m_textSize, m_textSize ) );
57 lbl.m_bbox.SetSize( textDims );
58 m_labels.push_back( lbl );
59}
60
61
63{
64 for( int i = 0; i < aL.PointCount(); i++ )
65 {
66 Add( aL.CPoint( i ), std::to_string( i ), color );
67 }
68}
69
71{
73
74 for( auto& lbl : m_labels )
75 {
76 aOvl->SetIsFill( false );
77 aOvl->SetIsStroke( true );
78 aOvl->SetLineWidth( 10000 );
79 aOvl->SetStrokeColor( lbl.m_color.Brighten( 0.7 ) );
80 aOvl->Rectangle( lbl.m_bbox.GetOrigin(), lbl.m_bbox.GetEnd() );
81 aOvl->BitmapText( lbl.m_msg, lbl.m_bbox.Centre(), ANGLE_HORIZONTAL );
82 VECTOR2I nearest = nearestBoxCorner( lbl.m_bbox, lbl.m_target );
83 aOvl->Line( lbl.m_target, nearest );
84 }
85}
86
87
89{
90 VECTOR2I ptest[4] = { b.GetPosition(), b.GetPosition() + VECTOR2I( b.GetWidth(), 0 ),
91 b.GetPosition() + VECTOR2I( b.GetWidth(), b.GetHeight() ),
92 b.GetPosition() + VECTOR2I( 0, b.GetHeight() ) };
93
94 int bestDist = INT_MAX;
95 VECTOR2I rv;
96
97 for( int i = 0; i < 4; i++ )
98 {
99 int dist = ( ptest[i] - p ).EuclideanNorm();
100
101 if( dist < bestDist )
102 {
103 bestDist = dist;
104 rv = ptest[i];
105 }
106 }
107
108 return rv;
109}
110
111
113{
114 VECTOR2I rv( 0, 0 );
115
116 b1.Normalize();
117 b2.Normalize();
118
119 if( !b1.Intersects( b2 ) )
120 return rv;
121
122 int bestDist = INT_MAX;
123
124 VECTOR2I p[4] = { b2.GetPosition(), b2.GetPosition() + VECTOR2I( b2.GetWidth(), 0 ),
125 b2.GetPosition() + VECTOR2I( b2.GetWidth(), b2.GetHeight() ),
126 b2.GetPosition() + VECTOR2I( 0, b2.GetHeight() ) };
127
128 for( int i = 0; i < 4; i++ )
129 {
130 if( b1.Contains( p[i] ) )
131 {
132 // printf("CONT %d\n", i );
133 VECTOR2I dp[4] = { VECTOR2I( b1.GetEnd().x - p[i].x + 1, 0 ),
134 VECTOR2I( b1.GetPosition().x - p[i].x - 1, 0 ),
135 VECTOR2I( 0, b1.GetEnd().y - p[i].y + 1 ),
136 VECTOR2I( 0, b1.GetPosition().y - p[i].y - 1 ) };
137
138 for( int j = 0; j < 4; j++ )
139 {
140 BOX2I btest( b2 );
141 btest.Move( dp[j] );
142
143 if( !b1.Intersects( btest ) )
144 {
145 int dist = dp[j].EuclideanNorm();
146
147 if( dist < bestDist )
148 {
149 bestDist = dist;
150 rv = dp[j];
151 }
152 }
153 }
154 }
155 }
156
157 return rv;
158}
159
160
162{
163 int iterLimit = 5;
164
165 return;
166 while( iterLimit > 0 )
167 {
168
169 bool collisionsFound = false;
170
171 for( int i = 0; i < m_labels.size(); i++ )
172 {
173 for( int j = 0; j < m_labels.size(); j++ )
174 {
175 if( i == j )
176 continue;
177
178 auto bb_i = m_labels[i].m_bbox;
179 auto bb_j = m_labels[j].m_bbox;
180
181 bb_i.Inflate( 100000 );
182 bb_j.Inflate( 100000 );
183 VECTOR2I mtv = boxMtv( bb_i, bb_j );
184
185 if( mtv.x || mtv.y )
186 {
187 m_labels[i].m_bbox.Move( -mtv );
188 collisionsFound = true;
189 }
190 }
191 }
192
193 if( !collisionsFound )
194 break;
195
196 iterLimit--;
197 }
198}
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr const Vec & GetPosition() const
Definition box2.h:207
constexpr const Vec GetEnd() const
Definition box2.h:208
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:233
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:142
constexpr size_type GetWidth() const
Definition box2.h:210
constexpr void SetSize(const SizeVec &size)
Definition box2.h:244
constexpr size_type GetHeight() const
Definition box2.h:211
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:164
constexpr void Move(const Vec &aMoveVector)
Move the rectangle by the aMoveVector.
Definition box2.h:134
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:307
FONT is an abstract base class for both outline and stroke fonts.
Definition font.h:94
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition font.cpp:143
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic, const METRICS &aFontMetrics) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition font.cpp:447
static const METRICS & Default()
Definition font.cpp:48
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Abstract interface for drawing on a 2D-surface.
void SetLineWidth(double aLineWidth)
void Rectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
void BitmapText(const wxString &aText, const VECTOR2I &aPosition, const EDA_ANGLE &aAngle)
void SetIsFill(bool aIsFillEnabled)
void Line(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
void SetIsStroke(bool aIsStrokeEnabled)
void SetStrokeColor(const COLOR4D &aColor)
VECTOR2I boxMtv(BOX2I b1, BOX2I b2)
KIGFX::GAL * m_canvas
std::vector< LABEL > m_labels
void Add(VECTOR2I target, wxString msg, KIGFX::COLOR4D color)
void Redraw(KIGFX::VIEW_OVERLAY *aOvl)
LABEL_MANAGER(KIGFX::GAL *aGal)
VECTOR2I nearestBoxCorner(BOX2I b, VECTOR2I p)
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:279
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:407
KIGFX::COLOR4D m_color
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682