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 (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#include <string>
28
29#include "label_manager.h"
30
31using KIGFX::GAL;
32using KIGFX::COLOR4D;
34
35LABEL_MANAGER::LABEL_MANAGER( GAL* aGal ) : m_gal( aGal )
36{
37};
38
39
41{
42}
43
44
45void LABEL_MANAGER::Add( VECTOR2I target, wxString msg, COLOR4D color )
46{
47 LABEL lbl;
48
49 lbl.m_target = target;
50 lbl.m_msg = msg;
51 lbl.m_color = color;
53
54 KIFONT::FONT* strokeFont = KIFONT::FONT::GetFont( wxEmptyString );
55 UTF8 text( msg );
57 m_textSize / 8, false, false,
59
60 lbl.m_bbox.SetOrigin( lbl.m_target - textDims - VECTOR2I( m_textSize, m_textSize ) );
61 lbl.m_bbox.SetSize( textDims );
62 m_labels.push_back( lbl );
63}
64
65
67{
68 for( int i = 0; i < aL.PointCount(); i++ )
69 {
70 Add( aL.CPoint( i ), std::to_string( i ), 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 return;
170 while( iterLimit > 0 )
171 {
172
173 bool collisionsFound = false;
174
175 for( int i = 0; i < m_labels.size(); i++ )
176 {
177 for( int j = 0; j < m_labels.size(); j++ )
178 {
179 if( i == j )
180 continue;
181
182 auto bb_i = m_labels[i].m_bbox;
183 auto bb_j = m_labels[j].m_bbox;
184
185 bb_i.Inflate( 100000 );
186 bb_j.Inflate( 100000 );
187 VECTOR2I mtv = boxMtv( bb_i, bb_j );
188
189 if( mtv.x || mtv.y )
190 {
191 m_labels[i].m_bbox.Move( -mtv );
192 collisionsFound = true;
193 }
194 }
195 }
196
197 if( !collisionsFound )
198 break;
199
200 iterLimit--;
201 }
202}
int color
Definition: DXF_plotter.cpp:58
constexpr const Vec & GetPosition() const
Definition: box2.h:211
constexpr const Vec GetEnd() const
Definition: box2.h:212
constexpr void SetOrigin(const Vec &pos)
Definition: box2.h:237
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:146
constexpr size_type GetWidth() const
Definition: box2.h:214
constexpr void SetSize(const SizeVec &size)
Definition: box2.h:248
constexpr size_type GetHeight() const
Definition: box2.h:215
constexpr bool Contains(const Vec &aPoint) const
Definition: box2.h:168
constexpr void Move(const Vec &aMoveVector)
Move the rectangle by the aMoveVector.
Definition: box2.h:138
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:311
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
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:146
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:435
static const METRICS & Default()
Definition: font.cpp:52
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:72
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:283
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition: eda_angle.h:397
KIGFX::COLOR4D m_color
Definition: label_manager.h:42
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690