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

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 ( 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

bool POLYGON_GEOM_MANAGER::AddPoint ( const VECTOR2I aPt)

Lock in a polygon point.

Definition at line 36 of file polygon_geom_manager.cpp.

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().

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"
int PointCount() const
Function PointCount()
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
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...
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
bool m_intersectionsAllowed
Flag enabling self-intersecting polygons
bool IsSelfIntersecting(bool aIncludeLeaderPts) const
Checks whether the locked points constitute a self-intersecting outline.
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)
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.

References m_intersectionsAllowed.

103  {
104  m_intersectionsAllowed = true;
105  }
bool m_intersectionsAllowed
Flag enabling self-intersecting polygons
void POLYGON_GEOM_MANAGER::DeleteLastCorner ( )

Remove the last-added point from the polygon.

Definition at line 116 of file polygon_geom_manager.cpp.

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(), and IntersectionsAllowed().

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"
int PointCount() const
Function PointCount()
const VECTOR2I & CLastPoint() const
Returns the last point in the line chain.
CLIENT & m_client
The "user" of the polygon data that is informed when the geometry changes
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
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)
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.

References DIRECT, m_leaderPts, and updateLeaderPoints().

Referenced by ZONE_CREATE_HELPER::OnGeometryChange().

164  {
165  return m_leaderPts;
166  }
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)
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.

References m_lockedPoints.

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

153  {
154  return m_lockedPoints;
155  }
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
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.

References DeleteLastCorner(), IsPolygonInProgress(), IsSelfIntersecting(), m_intersectionsAllowed, NewPointClosesOutline(), and SetCursorPosition().

112  {
113  return m_intersectionsAllowed;
114  }
bool m_intersectionsAllowed
Flag enabling self-intersecting polygons
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.

References m_lockedPoints, and SHAPE_LINE_CHAIN::PointCount().

Referenced by AddPoint(), DRAWING_TOOL::drawZone(), and IntersectionsAllowed().

105 {
106  return m_lockedPoints.PointCount() > 0;
107 }
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
int PointCount() const
Function PointCount()
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.

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

Referenced by AddPoint(), DRAWING_TOOL::drawZone(), and IntersectionsAllowed().

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()
size_t i
Definition: json11.cpp:597
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)
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.

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

Referenced by DRAWING_TOOL::drawZone(), and IntersectionsAllowed().

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()
void POLYGON_GEOM_MANAGER::Reset ( )

Clear the manager state and start again.

Definition at line 134 of file polygon_geom_manager.cpp.

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

Referenced by DRAWING_TOOL::drawZone().

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)
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.

References updateLeaderPoints().

Referenced by DRAWING_TOOL::drawZone(), and IntersectionsAllowed().

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) ...
void POLYGON_GEOM_MANAGER::SetFinished ( )

Mark the polygon finished and update the client.

Definition at line 68 of file polygon_geom_manager.cpp.

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

Referenced by DRAWING_TOOL::drawZone().

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
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.

References m_leaderMode.

Referenced by ZONE_CREATE_HELPER::OnFirstPoint().

75 {
76  m_leaderMode = aMode;
77 }
LEADER_MODE m_leaderMode
The current mode of the leader line
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.

References DIRECTION_45::BuildInitialTrace(), SHAPE_LINE_CHAIN::CLastPoint(), DEG45, m_client, m_leaderMode, m_leaderPts, m_lockedPoints, POLYGON_GEOM_MANAGER::CLIENT::OnGeometryChange(), and SHAPE_LINE_CHAIN::PointCount().

Referenced by DeleteLastCorner(), GetLeaderLinePoints(), and SetCursorPosition().

144 {
145  wxCHECK( m_lockedPoints.PointCount() > 0, /*void*/ );
146  const VECTOR2I& lastPt = m_lockedPoints.CLastPoint();
147 
148  if( m_leaderMode == LEADER_MODE::DEG45 || aModifier == LEADER_MODE::DEG45 )
149  {
150  // get a restricted 45/H/V line from the last fixed point to the cursor
151  DIRECTION_45 direction( lastPt - aEndPoint );
152  m_leaderPts = direction.BuildInitialTrace( lastPt, aEndPoint );
153 
154  // Can also add chain back to start, but this rearely produces
155  // usable result
156  //SHAPE_LINE_CHAIN newChain;
157  //DIRECTION_45 directionToStart( aEndPoint - m_lockedPoints.front() );
158  //newChain.Append( directionToStart.BuildInitialTrace( aEndPoint, m_lockedPoints.front() ) );
159  }
160  else
161  {
162  // direct segment
163  m_leaderPts = SHAPE_LINE_CHAIN( lastPt, aEndPoint );
164  }
165 
166  m_client.OnGeometryChange( *this );
167 }
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"
int PointCount() const
Function PointCount()
const VECTOR2I & CLastPoint() const
Returns the last point in the line chain.
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
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
Unconstrained point-to-point
Class DIRECTION_45.
Definition: direction45.h:36
Class SHAPE_LINE_CHAIN.
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)

Member Data Documentation

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().

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().

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().

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().

SHAPE_LINE_CHAIN POLYGON_GEOM_MANAGER::m_lockedPoints
private

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