KiCad PCB EDA Suite
POLYGON_GEOM_MANAGER Class Reference

Class that handles the drawing of a polygon, including management of last corner deletion and drawing of leader lines with various constraints (eg 45 deg only). More...

#include <polygon_geom_manager.h>

Classes

class  CLIENT
 "Listener" interface for a class that wants to be updated about polygon geometry changes More...
 

Public Types

enum  LEADER_MODE { LEADER_MODE::DIRECT, LEADER_MODE::DEG45 }
 The kind of the leader line. More...
 

Public Member Functions

 POLYGON_GEOM_MANAGER (CLIENT &aClient)
 
bool AddPoint (const VECTOR2I &aPt)
 Lock in a polygon point. More...
 
void SetFinished ()
 Mark the polygon finished and update the client. More...
 
void Reset ()
 Clear the manager state and start again. More...
 
void SetLeaderMode (LEADER_MODE aMode)
 Set the leader mode to use when calculating the leader/returner lines. More...
 
LEADER_MODE GetLeaderMode () const
 
void AllowIntersections (bool aEnabled)
 Enables/disables self-intersecting polygons. More...
 
bool IntersectionsAllowed () const
 Checks whether self-intersecting polygons are enabled. More...
 
bool IsSelfIntersecting (bool aIncludeLeaderPts) const
 Checks whether the locked points constitute a self-intersecting outline. More...
 
void SetCursorPosition (const VECTOR2I &aPos, LEADER_MODE aModifier)
 Set the current cursor position. More...
 
bool IsPolygonInProgress () const
 
bool NewPointClosesOutline (const VECTOR2I &aPt) const
 
void DeleteLastCorner ()
 Remove the last-added point from the polygon. More...
 
const SHAPE_LINE_CHAINGetLockedInPoints () const
 Get the "locked-in" points that describe the polygon itself. More...
 
const SHAPE_LINE_CHAINGetLeaderLinePoints () const
 Get the points comprising the leader line (the line from the last locked-in point to the current cursor position. More...
 

Private Member Functions

void updateLeaderPoints (const VECTOR2I &aEndPoint, LEADER_MODE aModifier=LEADER_MODE::DIRECT)
 Update the leader line points based on a new endpoint (probably a cursor position) More...
 

Private Attributes

CLIENTm_client
 

The "user" of the polygon data that is informed when the geometry changes

More...
 
LEADER_MODE m_leaderMode
 

The current mode of the leader line

More...
 
bool m_intersectionsAllowed
 

Flag enabling self-intersecting polygons

More...
 
SHAPE_LINE_CHAIN m_lockedPoints
 

Point that have been "locked in"

More...
 
SHAPE_LINE_CHAIN m_leaderPts
 

Points in the temporary "leader" line(s)

More...
 

Detailed Description

Class that handles the drawing of a polygon, including management of last corner deletion and drawing of leader lines with various constraints (eg 45 deg only).

This class handles only the geometry of the process.

Definition at line 36 of file polygon_geom_manager.h.

Member Enumeration Documentation

◆ LEADER_MODE

The kind of the leader line.

Enumerator
DIRECT 
DEG45 

Unconstrained point-to-point

Definition at line 66 of file polygon_geom_manager.h.

67  {
68  DIRECT,
69  DEG45,
70  };

Constructor & Destructor Documentation

◆ POLYGON_GEOM_MANAGER()

POLYGON_GEOM_MANAGER::POLYGON_GEOM_MANAGER ( CLIENT aClient)
Parameters
aClientis the client to pass the results onto

Definition at line 30 of file polygon_geom_manager.cpp.

30  :
31  m_client( aClient ),
34 {}
LEADER_MODE m_leaderMode
The current mode of the leader line
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
bool m_intersectionsAllowed
Flag enabling self-intersecting polygons

Member Function Documentation

◆ AddPoint()

bool POLYGON_GEOM_MANAGER::AddPoint ( const VECTOR2I aPt)

Lock in a polygon point.

Definition at line 37 of file polygon_geom_manager.cpp.

38 {
39  // if this is the first point, make sure the client is happy
40  // for us to continue
41  if( !IsPolygonInProgress() && !m_client.OnFirstPoint( *this ) )
42  return false;
43 
44  if( m_leaderPts.PointCount() > 1 )
45  {
46  // there are enough leader points - the next
47  // locked-in point is the end of the first leader
48  // segment
50  }
51  else
52  {
53  // no leader lines, directly add the cursor
54  m_lockedPoints.Append( aPt );
55  }
56 
57  // check for self-intersections
59  {
61  return false;
62  }
63 
64  m_client.OnGeometryChange( *this );
65  return true;
66 }
virtual void OnGeometryChange(const POLYGON_GEOM_MANAGER &aMgr)=0
Sent when the polygon geometry changes
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
int PointCount() const
Function PointCount()
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
virtual bool OnFirstPoint(POLYGON_GEOM_MANAGER &aMgr)=0
Called before the first point is added - clients can do initialisation here, and can veto the start o...
bool IsSelfIntersecting(bool aIncludeLeaderPts) const
Checks whether the locked points constitute a self-intersecting outline.
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
bool m_intersectionsAllowed
Flag enabling self-intersecting polygons
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)

References SHAPE_LINE_CHAIN::Append(), SHAPE_LINE_CHAIN::CPoint(), IsPolygonInProgress(), IsSelfIntersecting(), m_client, m_intersectionsAllowed, m_leaderPts, m_lockedPoints, POLYGON_GEOM_MANAGER::CLIENT::OnFirstPoint(), POLYGON_GEOM_MANAGER::CLIENT::OnGeometryChange(), SHAPE_LINE_CHAIN::PointCount(), and SHAPE_LINE_CHAIN::Remove().

Referenced by DRAWING_TOOL::DrawZone().

◆ AllowIntersections()

void POLYGON_GEOM_MANAGER::AllowIntersections ( bool  aEnabled)
inline

Enables/disables self-intersecting polygons.

Parameters
aEnabledtrue if self-intersecting polygons are enabled.

Definition at line 107 of file polygon_geom_manager.h.

108  {
109  m_intersectionsAllowed = true;
110  }
bool m_intersectionsAllowed
Flag enabling self-intersecting polygons

References m_intersectionsAllowed.

◆ DeleteLastCorner()

void POLYGON_GEOM_MANAGER::DeleteLastCorner ( )

Remove the last-added point from the polygon.

Definition at line 118 of file polygon_geom_manager.cpp.

119 {
120  if( m_lockedPoints.PointCount() > 0 )
121  {
123  }
124 
125  // update the new last segment (was previously
126  // locked in), reusing last constraints
127  if( m_lockedPoints.PointCount() > 0 )
128  {
130  }
131 
132  m_client.OnGeometryChange( *this );
133 }
virtual void OnGeometryChange(const POLYGON_GEOM_MANAGER &aMgr)=0
Sent when the polygon geometry changes
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
int PointCount() const
Function PointCount()
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
const VECTOR2I & CLastPoint() const
Returns the last point in the line chain.
void updateLeaderPoints(const VECTOR2I &aEndPoint, LEADER_MODE aModifier=LEADER_MODE::DIRECT)
Update the leader line points based on a new endpoint (probably a cursor position)
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)

References SHAPE_LINE_CHAIN::CLastPoint(), m_client, m_leaderPts, m_lockedPoints, POLYGON_GEOM_MANAGER::CLIENT::OnGeometryChange(), SHAPE_LINE_CHAIN::PointCount(), SHAPE_LINE_CHAIN::Remove(), and updateLeaderPoints().

Referenced by DRAWING_TOOL::DrawZone().

◆ GetLeaderLinePoints()

const SHAPE_LINE_CHAIN& POLYGON_GEOM_MANAGER::GetLeaderLinePoints ( ) const
inline

Get the points comprising the leader line (the line from the last locked-in point to the current cursor position.

How this is drawn will depend on the LEADER_MODE

Definition at line 168 of file polygon_geom_manager.h.

169  {
170  return m_leaderPts;
171  }
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)

References m_leaderPts.

Referenced by ZONE_CREATE_HELPER::OnComplete(), and ZONE_CREATE_HELPER::OnGeometryChange().

◆ GetLeaderMode()

LEADER_MODE POLYGON_GEOM_MANAGER::GetLeaderMode ( ) const
inline

Definition at line 98 of file polygon_geom_manager.h.

99  {
100  return m_leaderMode;
101  }
LEADER_MODE m_leaderMode
The current mode of the leader line

References m_leaderMode.

Referenced by ZONE_CREATE_HELPER::OnComplete().

◆ GetLockedInPoints()

const SHAPE_LINE_CHAIN& POLYGON_GEOM_MANAGER::GetLockedInPoints ( ) const
inline

Get the "locked-in" points that describe the polygon itself.

Definition at line 157 of file polygon_geom_manager.h.

158  {
159  return m_lockedPoints;
160  }
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"

References m_lockedPoints.

Referenced by ZONE_CREATE_HELPER::OnComplete(), and ZONE_CREATE_HELPER::OnGeometryChange().

◆ IntersectionsAllowed()

bool POLYGON_GEOM_MANAGER::IntersectionsAllowed ( ) const
inline

Checks whether self-intersecting polygons are enabled.

Returns
true if self-intersecting polygons are enabled.

Definition at line 116 of file polygon_geom_manager.h.

117  {
118  return m_intersectionsAllowed;
119  }
bool m_intersectionsAllowed
Flag enabling self-intersecting polygons

References m_intersectionsAllowed.

◆ IsPolygonInProgress()

bool POLYGON_GEOM_MANAGER::IsPolygonInProgress ( ) const
Returns
true if the polygon in "in progress", i.e. it has at least one locked-in point

Definition at line 106 of file polygon_geom_manager.cpp.

107 {
108  return m_lockedPoints.PointCount() > 0;
109 }
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
int PointCount() const
Function PointCount()

References m_lockedPoints, and SHAPE_LINE_CHAIN::PointCount().

Referenced by AddPoint(), and DRAWING_TOOL::DrawZone().

◆ IsSelfIntersecting()

bool POLYGON_GEOM_MANAGER::IsSelfIntersecting ( bool  aIncludeLeaderPts) const

Checks whether the locked points constitute a self-intersecting outline.

Parameters
aIncludeLeaderPtswhen true, also the leading points (not placed ones) will be tested.
Returns
True when the outline is self-intersecting.

Definition at line 82 of file polygon_geom_manager.cpp.

83 {
84  auto pts( m_lockedPoints );
85 
86  if( aIncludeLeaderPts )
87  {
88  for( int i = 0; i < m_leaderPts.PointCount(); ++i )
89  if( m_leaderPts.CPoint( i ) != pts.CPoint( 0 ) )
90  pts.Append( m_leaderPts.CPoint( i ) );
91  }
92 
93  // line chain needs to be set as closed for proper checks
94  pts.SetClosed( true );
95 
96  return !!pts.SelfIntersecting();
97 }
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
int PointCount() const
Function PointCount()
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
size_t i
Definition: json11.cpp:597
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)

References SHAPE_LINE_CHAIN::CPoint(), i, m_leaderPts, m_lockedPoints, and SHAPE_LINE_CHAIN::PointCount().

Referenced by AddPoint(), and DRAWING_TOOL::DrawZone().

◆ NewPointClosesOutline()

bool POLYGON_GEOM_MANAGER::NewPointClosesOutline ( const VECTOR2I aPt) const
Returns
true if locking in the given point would close the current polygon

Definition at line 112 of file polygon_geom_manager.cpp.

113 {
114  return m_lockedPoints.PointCount() > 0 && m_lockedPoints.CPoint( 0 ) == aPt;
115 }
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
int PointCount() const
Function PointCount()
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()

References SHAPE_LINE_CHAIN::CPoint(), m_lockedPoints, and SHAPE_LINE_CHAIN::PointCount().

Referenced by DRAWING_TOOL::DrawZone().

◆ Reset()

void POLYGON_GEOM_MANAGER::Reset ( )

Clear the manager state and start again.

Definition at line 136 of file polygon_geom_manager.cpp.

137 {
139  m_leaderPts.Clear();
140 
141  m_client.OnGeometryChange( *this );
142 }
virtual void OnGeometryChange(const POLYGON_GEOM_MANAGER &aMgr)=0
Sent when the polygon geometry changes
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
void Clear()
Function Clear() Removes all points from the line chain.
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)

References SHAPE_LINE_CHAIN::Clear(), m_client, m_leaderPts, m_lockedPoints, and POLYGON_GEOM_MANAGER::CLIENT::OnGeometryChange().

Referenced by DRAWING_TOOL::DrawZone().

◆ SetCursorPosition()

void POLYGON_GEOM_MANAGER::SetCursorPosition ( const VECTOR2I aPos,
LEADER_MODE  aModifier 
)

Set the current cursor position.

Definition at line 100 of file polygon_geom_manager.cpp.

101 {
102  updateLeaderPoints( aPos, aModifier );
103 }
void updateLeaderPoints(const VECTOR2I &aEndPoint, LEADER_MODE aModifier=LEADER_MODE::DIRECT)
Update the leader line points based on a new endpoint (probably a cursor position)

References updateLeaderPoints().

Referenced by DRAWING_TOOL::DrawZone().

◆ SetFinished()

void POLYGON_GEOM_MANAGER::SetFinished ( )

Mark the polygon finished and update the client.

Definition at line 69 of file polygon_geom_manager.cpp.

70 {
71 
72  m_client.OnComplete( *this );
73 }
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
virtual void OnComplete(const POLYGON_GEOM_MANAGER &aMgr)=0
Called when the polygon is complete

References m_client, and POLYGON_GEOM_MANAGER::CLIENT::OnComplete().

Referenced by DRAWING_TOOL::DrawZone().

◆ SetLeaderMode()

void POLYGON_GEOM_MANAGER::SetLeaderMode ( LEADER_MODE  aMode)

Set the leader mode to use when calculating the leader/returner lines.

Definition at line 76 of file polygon_geom_manager.cpp.

77 {
78  m_leaderMode = aMode;
79 }
LEADER_MODE m_leaderMode
The current mode of the leader line

References m_leaderMode.

Referenced by ZONE_CREATE_HELPER::OnFirstPoint().

◆ updateLeaderPoints()

void POLYGON_GEOM_MANAGER::updateLeaderPoints ( const VECTOR2I aEndPoint,
LEADER_MODE  aModifier = LEADER_MODE::DIRECT 
)
private

Update the leader line points based on a new endpoint (probably a cursor position)

Definition at line 145 of file polygon_geom_manager.cpp.

146 {
147  wxCHECK( m_lockedPoints.PointCount() > 0, /*void*/ );
148  const VECTOR2I& lastPt = m_lockedPoints.CLastPoint();
149 
150  if( m_leaderMode == LEADER_MODE::DEG45 || aModifier == LEADER_MODE::DEG45 )
151  {
152  const VECTOR2I lineVector( aEndPoint - lastPt );
153  // get a restricted 45/H/V line from the last fixed point to the cursor
154  auto newEnd = lastPt + GetVectorSnapped45( lineVector );
155 
156  SEG first( lastPt, newEnd );
157  SEG test_seg = m_lockedPoints.CSegment( 0 );
158 
159  auto pt = first.IntersectLines( m_lockedPoints.CSegment( 0 ) );
160  int dist = pt ? ( aEndPoint - *pt ).EuclideanNorm() : std::numeric_limits<int>::max();
161 
162  for( int i = 1; i < 8; i++ )
163  {
164  test_seg.B = ( test_seg.B - test_seg.A ).Rotate( M_PI_4 ) + test_seg.A;
165  auto pt2 = first.IntersectLines( test_seg );
166  if( pt2 )
167  {
168  int dist2 = ( aEndPoint - *pt2 ).EuclideanNorm();
169  if( dist2 < dist )
170  {
171  dist = dist2;
172  pt = pt2;
173  }
174  }
175  }
176 
177  m_leaderPts = SHAPE_LINE_CHAIN( lastPt, newEnd );
178 
179  if( pt )
180  m_leaderPts.Append( *pt );
181  }
182  else
183  {
184  // direct segment
185  m_leaderPts = SHAPE_LINE_CHAIN( lastPt, aEndPoint );
186  }
187 
188  m_client.OnGeometryChange( *this );
189 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:123
virtual void OnGeometryChange(const POLYGON_GEOM_MANAGER &aMgr)=0
Sent when the polygon geometry changes
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
static const int dist[10][10]
Definition: ar_matrix.cpp:320
LEADER_MODE m_leaderMode
The current mode of the leader line
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:183
int PointCount() const
Function PointCount()
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
Unconstrained point-to-point
Definition: seg.h:36
const SEG CSegment(int aIndex) const
Function CSegment()
#define max(a, b)
Definition: auxiliary.h:86
Class SHAPE_LINE_CHAIN.
size_t i
Definition: json11.cpp:597
VECTOR2I A
Definition: seg.h:44
const VECTOR2I & CLastPoint() const
Returns the last point in the line chain.
VECTOR2< T > GetVectorSnapped45(const VECTOR2< T > &aVec)
Snap a vector onto the nearest 0, 45 or 90 degree line.
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)
VECTOR2I B
Definition: seg.h:45

References SEG::A, SHAPE_LINE_CHAIN::Append(), SEG::B, SHAPE_LINE_CHAIN::CLastPoint(), SHAPE_LINE_CHAIN::CSegment(), DEG45, dist, EuclideanNorm(), GetVectorSnapped45(), i, SEG::IntersectLines(), m_client, m_leaderMode, m_leaderPts, m_lockedPoints, max, POLYGON_GEOM_MANAGER::CLIENT::OnGeometryChange(), and SHAPE_LINE_CHAIN::PointCount().

Referenced by DeleteLastCorner(), and SetCursorPosition().

Member Data Documentation

◆ m_client

CLIENT& POLYGON_GEOM_MANAGER::m_client
private

The "user" of the polygon data that is informed when the geometry changes

Definition at line 183 of file polygon_geom_manager.h.

Referenced by AddPoint(), DeleteLastCorner(), Reset(), SetFinished(), and updateLeaderPoints().

◆ m_intersectionsAllowed

bool POLYGON_GEOM_MANAGER::m_intersectionsAllowed
private

Flag enabling self-intersecting polygons

Definition at line 189 of file polygon_geom_manager.h.

Referenced by AddPoint(), AllowIntersections(), and IntersectionsAllowed().

◆ m_leaderMode

LEADER_MODE POLYGON_GEOM_MANAGER::m_leaderMode
private

The current mode of the leader line

Definition at line 186 of file polygon_geom_manager.h.

Referenced by GetLeaderMode(), SetLeaderMode(), and updateLeaderPoints().

◆ m_leaderPts

SHAPE_LINE_CHAIN POLYGON_GEOM_MANAGER::m_leaderPts
private

Points in the temporary "leader" line(s)

Definition at line 195 of file polygon_geom_manager.h.

Referenced by AddPoint(), DeleteLastCorner(), GetLeaderLinePoints(), IsSelfIntersecting(), Reset(), and updateLeaderPoints().

◆ m_lockedPoints

SHAPE_LINE_CHAIN POLYGON_GEOM_MANAGER::m_lockedPoints
private

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