KiCad PCB EDA Suite
LABEL_MANAGER Class Reference

#include <label_manager.h>

Classes

struct  LABEL
 

Public Member Functions

 LABEL_MANAGER (KIGFX::GAL *aGal)
 
 ~LABEL_MANAGER ()
 
void Add (VECTOR2I target, wxString msg, KIGFX::COLOR4D color)
 
void Add (const SHAPE_LINE_CHAIN &aL, KIGFX::COLOR4D color)
 
void Redraw (KIGFX::VIEW_OVERLAY *aOvl)
 

Private Member Functions

VECTOR2I nearestBoxCorner (BOX2I b, VECTOR2I p)
 
VECTOR2I boxMtv (BOX2I b1, BOX2I b2)
 
void recalculate ()
 

Private Attributes

KIGFX::GALm_gal
 
int m_textSize = 100000
 
std::vector< LABELm_labels
 

Detailed Description

Definition at line 37 of file label_manager.h.

Constructor & Destructor Documentation

◆ LABEL_MANAGER()

LABEL_MANAGER::LABEL_MANAGER ( KIGFX::GAL aGal)

Definition at line 34 of file label_manager.cpp.

34 : m_gal( aGal )
35{
36};
KIGFX::GAL * m_gal
Definition: label_manager.h:60

◆ ~LABEL_MANAGER()

LABEL_MANAGER::~LABEL_MANAGER ( )

Definition at line 39 of file label_manager.cpp.

40{
41}

Member Function Documentation

◆ Add() [1/2]

void LABEL_MANAGER::Add ( const SHAPE_LINE_CHAIN aL,
KIGFX::COLOR4D  color 
)

Definition at line 64 of file label_manager.cpp.

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}
int color
Definition: DXF_plotter.cpp:57
void Add(VECTOR2I target, wxString msg, KIGFX::COLOR4D color)
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.

References Add(), color, SHAPE_LINE_CHAIN::CPoint(), and SHAPE_LINE_CHAIN::PointCount().

◆ Add() [2/2]

void LABEL_MANAGER::Add ( VECTOR2I  target,
wxString  msg,
KIGFX::COLOR4D  color 
)

Definition at line 44 of file label_manager.cpp.

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}
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
void SetGlyphSize(const VECTOR2I aSize)
std::vector< LABEL > m_labels
Definition: label_manager.h:62
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617

References color, KIFONT::FONT::GetFont(), LABEL_MANAGER::LABEL::m_bbox, LABEL_MANAGER::LABEL::m_color, m_gal, m_labels, LABEL_MANAGER::LABEL::m_msg, LABEL_MANAGER::LABEL::m_target, m_textSize, KIGFX::GAL::SetGlyphSize(), BOX2< Vec >::SetOrigin(), BOX2< Vec >::SetSize(), KIFONT::FONT::StringBoundaryLimits(), and text.

Referenced by Add().

◆ boxMtv()

VECTOR2I LABEL_MANAGER::boxMtv ( BOX2I  b1,
BOX2I  b2 
)
private

Definition at line 116 of file label_manager.cpp.

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}
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
bool Contains(const Vec &aPoint) const
Definition: box2.h:141
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618

References BOX2< Vec >::Contains(), VECTOR2< T >::EuclideanNorm(), BOX2< Vec >::GetEnd(), BOX2< Vec >::GetHeight(), BOX2< Vec >::GetPosition(), BOX2< Vec >::GetWidth(), BOX2< Vec >::Intersects(), BOX2< Vec >::Move(), BOX2< Vec >::Normalize(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by recalculate().

◆ nearestBoxCorner()

VECTOR2I LABEL_MANAGER::nearestBoxCorner ( BOX2I  b,
VECTOR2I  p 
)
private

Definition at line 92 of file label_manager.cpp.

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}
double EuclideanNorm(const VECTOR2I &vector)
Definition: trigo.h:129

References EuclideanNorm(), BOX2< Vec >::GetHeight(), BOX2< Vec >::GetPosition(), and BOX2< Vec >::GetWidth().

Referenced by Redraw().

◆ recalculate()

void LABEL_MANAGER::recalculate ( )
private

Definition at line 165 of file label_manager.cpp.

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}
VECTOR2I boxMtv(BOX2I b1, BOX2I b2)

References boxMtv(), m_labels, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by Redraw().

◆ Redraw()

void LABEL_MANAGER::Redraw ( KIGFX::VIEW_OVERLAY aOvl)

Definition at line 74 of file label_manager.cpp.

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}
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 nearestBoxCorner(BOX2I b, VECTOR2I p)
static constexpr EDA_ANGLE & ANGLE_HORIZONTAL
Definition: eda_angle.h:419

References ANGLE_HORIZONTAL, KIGFX::VIEW_OVERLAY::BitmapText(), KIGFX::VIEW_OVERLAY::Line(), m_labels, nearestBoxCorner(), recalculate(), KIGFX::VIEW_OVERLAY::Rectangle(), KIGFX::VIEW_OVERLAY::SetIsFill(), KIGFX::VIEW_OVERLAY::SetIsStroke(), KIGFX::VIEW_OVERLAY::SetLineWidth(), and KIGFX::VIEW_OVERLAY::SetStrokeColor().

Member Data Documentation

◆ m_gal

KIGFX::GAL* LABEL_MANAGER::m_gal
private

Definition at line 60 of file label_manager.h.

Referenced by Add().

◆ m_labels

std::vector<LABEL> LABEL_MANAGER::m_labels
private

Definition at line 62 of file label_manager.h.

Referenced by Add(), recalculate(), and Redraw().

◆ m_textSize

int LABEL_MANAGER::m_textSize = 100000
private

Definition at line 61 of file label_manager.h.

Referenced by Add().


The documentation for this class was generated from the following files: