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( false )
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 (line chain needs to be set as closed for proper checks)
57  m_lockedPoints.SetClosed( true );
58  bool selfIntersect = !!m_lockedPoints.SelfIntersecting();
59  m_lockedPoints.SetClosed( false );
60 
61  if( !m_intersectionsAllowed && selfIntersect )
62  {
64  return false;
65  }
66 
67  m_client.OnGeometryChange( *this );
68  return true;
69 }
70 
71 
73 {
74  m_client.OnComplete( *this );
75 }
76 
77 
79 {
80  m_leaderMode = aMode;
81 }
82 
83 
85 {
86  updateLeaderPoints( aPos, aModifier );
87 }
88 
89 
91 {
92  return m_lockedPoints.PointCount() > 0;
93 }
94 
95 
97 {
98  return m_lockedPoints.PointCount() > 0 && m_lockedPoints.CPoint( 0 ) == aPt;
99 }
100 
101 
103 {
104  if( m_lockedPoints.PointCount() > 0 )
105  {
107  }
108 
109  // update the new last segment (was previously
110  // locked in), reusing last constraints
111  if( m_lockedPoints.PointCount() > 0 )
112  {
114  }
115 
116  m_client.OnGeometryChange( *this );
117 }
118 
119 
121 {
123  m_leaderPts.Clear();
124 
125  m_client.OnGeometryChange( *this );
126 }
127 
128 
130 {
131  wxCHECK( m_lockedPoints.PointCount() > 0, /*void*/ );
132  const VECTOR2I& lastPt = m_lockedPoints.CLastPoint();
133 
134  if( m_leaderMode == LEADER_MODE::DEG45 || aModifier == LEADER_MODE::DEG45 )
135  {
136  // get a restricted 45/H/V line from the last fixed point to the cursor
137  DIRECTION_45 direction( lastPt - aEndPoint );
138  m_leaderPts = direction.BuildInitialTrace( lastPt, aEndPoint );
139 
140  // Can also add chain back to start, but this rearely produces
141  // usable result
142  //SHAPE_LINE_CHAIN newChain;
143  //DIRECTION_45 directionToStart( aEndPoint - m_lockedPoints.front() );
144  //newChain.Append( directionToStart.BuildInitialTrace( aEndPoint, m_lockedPoints.front() ) );
145  }
146  else
147  {
148  // direct segment
149  m_leaderPts = SHAPE_LINE_CHAIN( lastPt, aEndPoint );
150  }
151 
152  m_client.OnGeometryChange( *this );
153 }
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
const OPT< INTERSECTION > SelfIntersecting() const
Function SelfIntersecting()
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()
void SetClosed(bool aClosed)
Function SetClosed()
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
Class SHAPE_LINE_CHAIN.
bool NewPointClosesOutline(const VECTOR2I &aPt) const
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)