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, std::string msg, COLOR4D color)
 
void Add (const SHAPE_LINE_CHAIN &aL, 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 9 of file label_manager.h.

Constructor & Destructor Documentation

◆ LABEL_MANAGER()

LABEL_MANAGER::LABEL_MANAGER ( KIGFX::GAL aGal)

Definition at line 50 of file pns_log_viewer.cpp.

50  : m_gal( aGal )
51 {
52 
53 };
KIGFX::GAL * m_gal
Definition: label_manager.h:33

◆ ~LABEL_MANAGER()

LABEL_MANAGER::~LABEL_MANAGER ( )

Definition at line 56 of file pns_log_viewer.cpp.

57 {
58 }

Member Function Documentation

◆ Add() [1/2]

void LABEL_MANAGER::Add ( VECTOR2I  target,
std::string  msg,
COLOR4D  color 
)

Definition at line 61 of file pns_log_viewer.cpp.

62 {
63  LABEL lbl;
64 
65  lbl.m_target = target;
66  lbl.m_msg = msg;
67  lbl.m_color = color;
69 
70  VECTOR2I textDims = m_gal->GetTextLineSize( msg );
71 
72  lbl.m_bbox.SetOrigin( lbl.m_target - textDims - VECTOR2I( m_textSize, m_textSize ) );
73  lbl.m_bbox.SetSize( textDims );
74  m_labels.push_back( lbl );
75 }
int color
Definition: DXF_plotter.cpp:57
Define a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2D GetTextLineSize(const UTF8 &aText) const
Compute the X and Y size of a given text.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
void SetGlyphSize(const VECTOR2D &aSize)
Set the font glyph size.
KIGFX::GAL * m_gal
Definition: label_manager.h:33
std::vector< LABEL > m_labels
Definition: label_manager.h:35

References color, KIGFX::GAL::GetTextLineSize(), 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(), and BOX2< Vec >::SetSize().

Referenced by Add().

◆ Add() [2/2]

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

Definition at line 78 of file pns_log_viewer.cpp.

79 {
80  for( int i = 0; i < aL.PointCount(); i++ )
81  {
82  char msg[1024];
83  snprintf( msg, sizeof( msg ), "%d", i );
84  Add( aL.CPoint( i ), msg, color );
85  }
86 }
int color
Definition: DXF_plotter.cpp:57
void Add(VECTOR2I target, std::string msg, 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().

◆ boxMtv()

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

Definition at line 130 of file pns_log_viewer.cpp.

131 {
132  VECTOR2I rv( 0, 0 );
133 
134  b1.Normalize();
135  b2.Normalize();
136 
137  if( !b1.Intersects( b2 ) )
138  return rv;
139 
140  int bestDist = INT_MAX;
141 
142  VECTOR2I p[4] = { b2.GetPosition(), b2.GetPosition() + VECTOR2I( b2.GetWidth(), 0 ),
143  b2.GetPosition() + VECTOR2I( b2.GetWidth(), b2.GetHeight() ),
144  b2.GetPosition() + VECTOR2I( 0, b2.GetHeight() ) };
145 
146  for( int i = 0; i < 4; i++ )
147  {
148  if( b1.Contains( p[i] ) )
149  {
150  // printf("CONT %d\n", i );
151  VECTOR2I dp[4] = { VECTOR2I( b1.GetEnd().x - p[i].x + 1, 0 ),
152  VECTOR2I( b1.GetPosition().x - p[i].x - 1, 0 ),
153  VECTOR2I( 0, b1.GetEnd().y - p[i].y + 1 ),
154  VECTOR2I( 0, b1.GetPosition().y - p[i].y - 1 ) };
155 
156  for( int j = 0; j < 4; j++ )
157  {
158  BOX2I btest( b2 );
159  btest.Move( dp[j] );
160 
161  if( !b1.Intersects( btest ) )
162  {
163  int dist = dp[j].EuclideanNorm();
164 
165  if( dist < bestDist )
166  {
167  bestDist = dist;
168  rv = dp[j];
169  }
170  }
171  }
172  }
173  }
174 
175  return rv;
176 }
const Vec GetEnd() const
Definition: box2.h:178
Define a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
bool Intersects(const BOX2< Vec > &aRect) const
Definition: box2.h:217
coord_type GetWidth() const
Definition: box2.h:180
BOX2< Vec > & Normalize()
Ensure that the height ant width are positive.
Definition: box2.h:112
bool Contains(const Vec &aPoint) const
Definition: box2.h:134
const Vec & GetPosition() const
Definition: box2.h:177
coord_type GetHeight() const
Definition: box2.h:181
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:293

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 106 of file pns_log_viewer.cpp.

107 {
108  VECTOR2I ptest[4] = { b.GetPosition(), b.GetPosition() + VECTOR2I( b.GetWidth(), 0 ),
109  b.GetPosition() + VECTOR2I( b.GetWidth(), b.GetHeight() ),
110  b.GetPosition() + VECTOR2I( 0, b.GetHeight() ) };
111 
112  int bestDist = INT_MAX;
113  VECTOR2I rv;
114 
115  for( int i = 0; i < 4; i++ )
116  {
117  int dist = ( ptest[i] - p ).EuclideanNorm();
118 
119  if( dist < bestDist )
120  {
121  bestDist = dist;
122  rv = ptest[i];
123  }
124  }
125 
126  return rv;
127 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:146
Define a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
coord_type GetWidth() const
Definition: box2.h:180
const Vec & GetPosition() const
Definition: box2.h:177
coord_type GetHeight() const
Definition: box2.h:181

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

Referenced by Redraw().

◆ recalculate()

void LABEL_MANAGER::recalculate ( )
private

Definition at line 179 of file pns_log_viewer.cpp.

180 {
181  int iterLimit = 5;
182 
183  while( iterLimit > 0 )
184  {
185  printf( "Iter %d\n", iterLimit );
186  bool collisionsFound = false;
187 
188  for( int i = 0; i < m_labels.size(); i++ )
189  {
190  for( int j = 0; j < m_labels.size(); j++ )
191  {
192  if( i == j )
193  continue;
194 
195  auto bb_i = m_labels[i].m_bbox;
196  auto bb_j = m_labels[j].m_bbox;
197 
198  bb_i.Inflate( 100000 );
199  bb_j.Inflate( 100000 );
200  VECTOR2I mtv = boxMtv( bb_i, bb_j );
201 
202  if( mtv.x || mtv.y )
203  {
204  m_labels[i].m_bbox.Move( -mtv );
205  collisionsFound = true;
206  }
207  }
208  }
209 
210  if( !collisionsFound )
211  break;
212 
213  iterLimit--;
214  }
215 }
Define a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2I boxMtv(BOX2I b1, BOX2I b2)
std::vector< LABEL > m_labels
Definition: label_manager.h:35

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 88 of file pns_log_viewer.cpp.

89 {
90  recalculate();
91 
92  for( auto& lbl : m_labels )
93  {
94  aOvl->SetIsFill( false );
95  aOvl->SetIsStroke( true );
96  aOvl->SetLineWidth( 10000 );
97  aOvl->SetStrokeColor( lbl.m_color.Brighten( 0.7 ) );
98  aOvl->Rectangle( lbl.m_bbox.GetOrigin(), lbl.m_bbox.GetEnd() );
99  aOvl->BitmapText( lbl.m_msg, lbl.m_bbox.Centre(), 0.0 );
100  VECTOR2I nearest = nearestBoxCorner( lbl.m_bbox, lbl.m_target );
101  aOvl->Line( lbl.m_target, nearest );
102  }
103 }
void BitmapText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Define a general 2D-vector/point.
Definition: vector2d.h:61
void SetIsStroke(bool aIsStrokeEnabled)
VECTOR2I nearestBoxCorner(BOX2I b, VECTOR2I p)
void Line(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
void SetStrokeColor(const COLOR4D &aColor)
void SetIsFill(bool aIsFillEnabled)
void SetLineWidth(double aLineWidth)
std::vector< LABEL > m_labels
Definition: label_manager.h:35
void Rectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)

References 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 33 of file label_manager.h.

Referenced by Add().

◆ m_labels

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

Definition at line 35 of file label_manager.h.

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

◆ m_textSize

int LABEL_MANAGER::m_textSize = 100000
private

Definition at line 34 of file label_manager.h.

Referenced by Add().


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