KiCad PCB EDA Suite
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 (C) 2020-2022 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include <gal/color4d.h>
26#include <view/view_overlay.h>
27
28#include "label_manager.h"
29
30using KIGFX::GAL;
31using KIGFX::COLOR4D;
33
34LABEL_MANAGER::LABEL_MANAGER( GAL* aGal ) : m_gal( aGal )
35{
36};
37
38
40{
41}
42
43
44void LABEL_MANAGER::Add( VECTOR2I target, wxString msg, COLOR4D color )
45{
46 LABEL lbl;
47
48 lbl.m_target = target;
49 lbl.m_msg = msg;
50 lbl.m_color = color;
52
53 KIFONT::FONT* strokeFont = KIFONT::FONT::GetFont( wxEmptyString );
54 UTF8 text( msg );
56 m_textSize/8, false, false );
57
58 lbl.m_bbox.SetOrigin( lbl.m_target - textDims - VECTOR2I( m_textSize, m_textSize ) );
59 lbl.m_bbox.SetSize( textDims );
60 m_labels.push_back( lbl );
61}
62
63
65{
66 for( int i = 0; i < aL.PointCount(); i++ )
67 {
68 char msg[1024];
69 snprintf( msg, sizeof( msg ), "%d", i );
70 Add( aL.CPoint( i ), msg, color );
71 }
72}
73
75{
77
78 for( auto& lbl : m_labels )
79 {
80 aOvl->SetIsFill( false );
81 aOvl->SetIsStroke( true );
82 aOvl->SetLineWidth( 10000 );
83 aOvl->SetStrokeColor( lbl.m_color.Brighten( 0.7 ) );
84 aOvl->Rectangle( lbl.m_bbox.GetOrigin(), lbl.m_bbox.GetEnd() );
85 aOvl->BitmapText( lbl.m_msg, lbl.m_bbox.Centre(), ANGLE_HORIZONTAL );
86 VECTOR2I nearest = nearestBoxCorner( lbl.m_bbox, lbl.m_target );
87 aOvl->Line( lbl.m_target, nearest );
88 }
89}
90
91
93{
94 VECTOR2I ptest[4] = { b.GetPosition(), b.GetPosition() + VECTOR2I( b.GetWidth(), 0 ),
95 b.GetPosition() + VECTOR2I( b.GetWidth(), b.GetHeight() ),
96 b.GetPosition() + VECTOR2I( 0, b.GetHeight() ) };
97
98 int bestDist = INT_MAX;
99 VECTOR2I rv;
100
101 for( int i = 0; i < 4; i++ )
102 {
103 int dist = ( ptest[i] - p ).EuclideanNorm();
104
105 if( dist < bestDist )
106 {
107 bestDist = dist;
108 rv = ptest[i];
109 }
110 }
111
112 return rv;
113}
114
115
117{
118 VECTOR2I rv( 0, 0 );
119
120 b1.Normalize();
121 b2.Normalize();
122
123 if( !b1.Intersects( b2 ) )
124 return rv;
125
126 int bestDist = INT_MAX;
127
128 VECTOR2I p[4] = { b2.GetPosition(), b2.GetPosition() + VECTOR2I( b2.GetWidth(), 0 ),
129 b2.GetPosition() + VECTOR2I( b2.GetWidth(), b2.GetHeight() ),
130 b2.GetPosition() + VECTOR2I( 0, b2.GetHeight() ) };
131
132 for( int i = 0; i < 4; i++ )
133 {
134 if( b1.Contains( p[i] ) )
135 {
136 // printf("CONT %d\n", i );
137 VECTOR2I dp[4] = { VECTOR2I( b1.GetEnd().x - p[i].x + 1, 0 ),
138 VECTOR2I( b1.GetPosition().x - p[i].x - 1, 0 ),
139 VECTOR2I( 0, b1.GetEnd().y - p[i].y + 1 ),
140 VECTOR2I( 0, b1.GetPosition().y - p[i].y - 1 ) };
141
142 for( int j = 0; j < 4; j++ )
143 {
144 BOX2I btest( b2 );
145 btest.Move( dp[j] );
146
147 if( !b1.Intersects( btest ) )
148 {
149 int dist = dp[j].EuclideanNorm();
150
151 if( dist < bestDist )
152 {
153 bestDist = dist;
154 rv = dp[j];
155 }
156 }
157 }
158 }
159 }
160
161 return rv;
162}
163
164
166{
167 int iterLimit = 5;
168
169 while( iterLimit > 0 )
170 {
171 bool collisionsFound = false;
172
173 for( int i = 0; i < m_labels.size(); i++ )
174 {
175 for( int j = 0; j < m_labels.size(); j++ )
176 {
177 if( i == j )
178 continue;
179
180 auto bb_i = m_labels[i].m_bbox;
181 auto bb_j = m_labels[j].m_bbox;
182
183 bb_i.Inflate( 100000 );
184 bb_j.Inflate( 100000 );
185 VECTOR2I mtv = boxMtv( bb_i, bb_j );
186
187 if( mtv.x || mtv.y )
188 {
189 m_labels[i].m_bbox.Move( -mtv );
190 collisionsFound = true;
191 }
192 }
193 }
194
195 if( !collisionsFound )
196 break;
197
198 iterLimit--;
199 }
200}
int color
Definition: DXF_plotter.cpp:57
void SetOrigin(const Vec &pos)
Definition: box2.h:202
BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:119
const Vec & GetPosition() const
Definition: box2.h:184
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:269
coord_type GetHeight() const
Definition: box2.h:188
coord_type GetWidth() const
Definition: box2.h:187
const Vec GetEnd() const
Definition: box2.h:185
void Move(const Vec &aMoveVector)
Move the rectangle by the aMoveVector.
Definition: box2.h:111
void SetSize(const Vec &size)
Definition: box2.h:213
bool Contains(const Vec &aPoint) const
Definition: box2.h:141
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false)
Definition: font.cpp:65
VECTOR2I StringBoundaryLimits(const wxString &aText, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic) const
Compute the boundary limits of aText (the bounding box of all shapes).
Definition: font.cpp:271
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Abstract interface for drawing on a 2D-surface.
void SetGlyphSize(const VECTOR2I aSize)
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_gal
Definition: label_manager.h:60
std::vector< LABEL > m_labels
Definition: label_manager.h:62
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:71
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
static constexpr EDA_ANGLE & ANGLE_HORIZONTAL
Definition: eda_angle.h:408
KIGFX::COLOR4D m_color
Definition: label_manager.h:42
double EuclideanNorm(const VECTOR2I &vector)
Definition: trigo.h:129
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618