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...
 
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 29 of file polygon_geom_manager.cpp.

29  :
30  m_client( aClient ),
33 {}
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 36 of file polygon_geom_manager.cpp.

37 {
38  // if this is the first point, make sure the client is happy
39  // for us to continue
40  if( !IsPolygonInProgress() && !m_client.OnFirstPoint( *this ) )
41  return false;
42 
43  if( m_leaderPts.PointCount() > 1 )
44  {
45  // there are enough leader points - the next
46  // locked-in point is the end of the first leader
47  // segment
49  }
50  else
51  {
52  // no leader lines, directly add the cursor
53  m_lockedPoints.Append( aPt );
54  }
55 
56  // check for self-intersections
58  {
60  return false;
61  }
62 
63  m_client.OnGeometryChange( *this );
64  return true;
65 }
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 102 of file polygon_geom_manager.h.

103  {
104  m_intersectionsAllowed = true;
105  }
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 116 of file polygon_geom_manager.cpp.

117 {
118  if( m_lockedPoints.PointCount() > 0 )
119  {
121  }
122 
123  // update the new last segment (was previously
124  // locked in), reusing last constraints
125  if( m_lockedPoints.PointCount() > 0 )
126  {
128  }
129 
130  m_client.OnGeometryChange( *this );
131 }
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 163 of file polygon_geom_manager.h.

164  {
165  return m_leaderPts;
166  }
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)

References m_leaderPts.

Referenced by ZONE_CREATE_HELPER::OnGeometryChange().

◆ GetLockedInPoints()

const SHAPE_LINE_CHAIN& POLYGON_GEOM_MANAGER::GetLockedInPoints ( ) const
inline

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

Definition at line 152 of file polygon_geom_manager.h.

153  {
154  return m_lockedPoints;
155  }
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 111 of file polygon_geom_manager.h.

112  {
113  return m_intersectionsAllowed;
114  }
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 104 of file polygon_geom_manager.cpp.

105 {
106  return m_lockedPoints.PointCount() > 0;
107 }
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 80 of file polygon_geom_manager.cpp.

81 {
82  auto pts( m_lockedPoints );
83 
84  if( aIncludeLeaderPts )
85  {
86  for( int i = 0; i < m_leaderPts.PointCount(); ++i )
87  if( m_leaderPts.CPoint( i ) != pts.CPoint( 0 ) )
88  pts.Append( m_leaderPts.CPoint( i ) );
89  }
90 
91  // line chain needs to be set as closed for proper checks
92  pts.SetClosed( true );
93 
94  return !!pts.SelfIntersecting();
95 }
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 110 of file polygon_geom_manager.cpp.

111 {
112  return m_lockedPoints.PointCount() > 0 && m_lockedPoints.CPoint( 0 ) == aPt;
113 }
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 134 of file polygon_geom_manager.cpp.

135 {
137  m_leaderPts.Clear();
138 
139  m_client.OnGeometryChange( *this );
140 }
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 98 of file polygon_geom_manager.cpp.

99 {
100  updateLeaderPoints( aPos, aModifier );
101 }
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 68 of file polygon_geom_manager.cpp.

69 {
70  m_client.OnComplete( *this );
71 }
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 74 of file polygon_geom_manager.cpp.

75 {
76  m_leaderMode = aMode;
77 }
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 143 of file polygon_geom_manager.cpp.

144 {
145  wxCHECK( m_lockedPoints.PointCount() > 0, /*void*/ );
146  const VECTOR2I& lastPt = m_lockedPoints.CLastPoint();
147  auto newEnd = VECTOR2I( aEndPoint );
148 
149  if( m_leaderMode == LEADER_MODE::DEG45 || aModifier == LEADER_MODE::DEG45 )
150  {
151  const VECTOR2I lineVector( aEndPoint - lastPt );
152  // get a restricted 45/H/V line from the last fixed point to the cursor
153  newEnd = lastPt + GetVectorSnapped45( lineVector );
154  }
155 
156  // direct segment
157  m_leaderPts = SHAPE_LINE_CHAIN( lastPt, newEnd );
158  m_client.OnGeometryChange( *this );
159 }
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"
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
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
int PointCount() const
Function PointCount()
Unconstrained point-to-point
Class SHAPE_LINE_CHAIN.
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)

References SHAPE_LINE_CHAIN::CLastPoint(), DEG45, GetVectorSnapped45(), m_client, m_leaderMode, m_leaderPts, m_lockedPoints, 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 178 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 184 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 181 of file polygon_geom_manager.h.

Referenced by SetLeaderMode(), and updateLeaderPoints().

◆ m_leaderPts

SHAPE_LINE_CHAIN POLYGON_GEOM_MANAGER::m_leaderPts
private

Points in the temporary "leader" line(s)

Definition at line 190 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: