KiCad PCB EDA Suite
polygon_geom_manager.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 Kicad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
25 
26 #include <geometry/direction45.h>
27 
28 
30  m_client( aClient ),
31  m_leaderMode( LEADER_MODE::DIRECT ),
32  m_intersectionsAllowed( true )
33 {}
34 
35 
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 }
66 
67 
69 {
70  m_client.OnComplete( *this );
71 }
72 
73 
75 {
76  m_leaderMode = aMode;
77 }
78 
79 
80 bool POLYGON_GEOM_MANAGER::IsSelfIntersecting( bool aIncludeLeaderPts ) const
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 }
96 
97 
99 {
100  updateLeaderPoints( aPos, aModifier );
101 }
102 
103 
105 {
106  return m_lockedPoints.PointCount() > 0;
107 }
108 
109 
111 {
112  return m_lockedPoints.PointCount() > 0 && m_lockedPoints.CPoint( 0 ) == aPt;
113 }
114 
115 
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 }
132 
133 
135 {
137  m_leaderPts.Clear();
138 
139  m_client.OnGeometryChange( *this );
140 }
141 
142 
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
void SetCursorPosition(const VECTOR2I &aPos, LEADER_MODE aModifier)
Set the current cursor position.
SHAPE_LINE_CHAIN m_lockedPoints
Point that have been "locked in"
LEADER_MODE
The kind of the leader line.
POLYGON_GEOM_MANAGER(CLIENT &aClient)
int PointCount() const
Function PointCount()
const VECTOR2I & CLastPoint() const
Returns the last point in the line chain.
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
void SetLeaderMode(LEADER_MODE aMode)
Set the leader mode to use when calculating the leader/returner lines.
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
Unconstrained point-to-point
"Listener" interface for a class that wants to be updated about polygon geometry changes ...
Class DIRECTION_45.
Definition: direction45.h:36
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()
void DeleteLastCorner()
Remove the last-added point from the polygon.
bool m_intersectionsAllowed
Flag enabling self-intersecting polygons
bool IsSelfIntersecting(bool aIncludeLeaderPts) const
Checks whether the locked points constitute a self-intersecting outline.
Class SHAPE_LINE_CHAIN.
bool NewPointClosesOutline(const VECTOR2I &aPt) const
size_t i
Definition: json11.cpp:597
void Reset()
Clear the manager state and start again.
void Clear()
Function Clear() Removes all points from the line chain.
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false) const
Function BuildInitialTrace()
Definition: direction45.h:202
virtual void OnComplete(const POLYGON_GEOM_MANAGER &aMgr)=0
Called when the polygon is complete
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) ...
bool AddPoint(const VECTOR2I &aPt)
Lock in a polygon point.
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
void SetFinished()
Mark the polygon finished and update the client.
SHAPE_LINE_CHAIN m_leaderPts
Points in the temporary "leader" line(s)