Horizon
pns_line.h
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2017 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef __PNS_LINE_H
23 #define __PNS_LINE_H
24 
25 #include <math/vector2d.h>
26 
27 #include <geometry/direction45.h>
28 #include <geometry/seg.h>
29 #include <geometry/shape.h>
30 #include <geometry/shape_line_chain.h>
31 
32 #include "pns_item.h"
33 #include "pns_via.h"
34 
35 namespace PNS {
36 
37 class NODE;
38 class SEGMENT;
39 class VIA;
40 
58 #define PNS_HULL_MARGIN 10
59 
60 class LINE : public ITEM
61 {
62 public:
63  typedef std::vector<SEGMENT*> SEGMENT_REFS;
64 
69  LINE() : ITEM( LINE_T )
70  {
71  m_hasVia = false;
72  m_width = 1; // Dummy value
73  }
74 
75  LINE( const LINE& aOther );
76 
82  LINE( const LINE& aBase, const SHAPE_LINE_CHAIN& aLine ) :
83  ITEM( aBase ),
84  m_line( aLine ),
85  m_width( aBase.m_width )
86  {
87  m_net = aBase.m_net;
88  m_layers = aBase.m_layers;
89  m_hasVia = false;
90  }
91 
92  ~LINE();
93 
94  static inline bool ClassOf( const ITEM* aItem )
95  {
96  return aItem && LINE_T == aItem->Kind();
97  }
98 
100  virtual LINE* Clone() const override;
101 
102  LINE& operator=( const LINE& aOther );
103 
105  void SetShape( const SHAPE_LINE_CHAIN& aLine )
106  {
107  m_line = aLine;
108  m_line.SetWidth( m_width );
109  }
110 
112  const SHAPE* Shape() const override
113  {
114  return &m_line;
115  }
116 
119  {
120  return m_line;
121  }
122 
124  const SHAPE_LINE_CHAIN& CLine() const
125  {
126  return m_line;
127  }
128 
130  int SegmentCount() const
131  {
132  return m_line.SegmentCount();
133  }
134 
136  int PointCount() const
137  {
138  return m_line.PointCount();
139  }
140 
142  const VECTOR2I& CPoint( int aIdx ) const
143  {
144  return m_line.CPoint( aIdx );
145  }
146 
148  const SEG CSegment( int aIdx ) const
149  {
150  return m_line.CSegment( aIdx );
151  }
152 
154  void SetWidth( int aWidth )
155  {
156  m_width = aWidth;
157  m_line.SetWidth( aWidth );
158  }
159 
161  int Width() const
162  {
163  return m_width;
164  }
165 
167  bool CompareGeometry( const LINE& aOther );
168 
170  void Reverse();
171 
172 
173  /* Linking functions */
174 
176  void LinkSegment( SEGMENT* aSeg )
177  {
178  m_segmentRefs.push_back( aSeg );
179  }
180 
183  SEGMENT_REFS& LinkedSegments()
184  {
185  return m_segmentRefs;
186  }
187 
188  bool IsLinked() const
189  {
190  return m_segmentRefs.size() != 0;
191  }
192 
193  bool IsLinkedChecked() const
194  {
195  return IsLinked() && LinkCount() == SegmentCount();
196  }
197 
199  bool ContainsSegment( SEGMENT* aSeg ) const
200  {
201  return std::find( m_segmentRefs.begin(), m_segmentRefs.end(),
202  aSeg ) != m_segmentRefs.end();
203  }
204 
205  SEGMENT* GetLink( int aIndex ) const
206  {
207  return m_segmentRefs[aIndex];
208  }
209 
212 
214  int LinkCount() const
215  {
216  return m_segmentRefs.size();
217  }
218 
221  const LINE ClipToNearestObstacle( NODE* aNode ) const;
222 
224  void ClipVertexRange ( int aStart, int aEnd );
225 
227  int CountCorners( int aAngles ) const;
228 
235  bool Walkaround( SHAPE_LINE_CHAIN aObstacle,
236  SHAPE_LINE_CHAIN& aPre,
237  SHAPE_LINE_CHAIN& aWalk,
238  SHAPE_LINE_CHAIN& aPost,
239  bool aCw ) const;
240 
241  bool Walkaround( const SHAPE_LINE_CHAIN& aObstacle,
242  SHAPE_LINE_CHAIN& aPath,
243  bool aCw ) const;
244 
245  bool Is45Degree() const;
246 
248  void ShowLinks() const;
249 
250  bool EndsWithVia() const { return m_hasVia; }
251 
252  void AppendVia( const VIA& aVia );
253  void RemoveVia() { m_hasVia = false; }
254 
255  const VIA& Via() const { return m_via; }
256 
257  virtual void Mark( int aMarker ) override;
258  virtual void Unmark( int aMarker = -1 ) override;
259  virtual int Marker() const override;
260 
261  void DragSegment( const VECTOR2I& aP, int aIndex, int aSnappingThreshold = 0, bool aFreeAngle = false );
262  void DragCorner( const VECTOR2I& aP, int aIndex, int aSnappingThreshold = 0, bool aFreeAngle = false );
263 
264  void SetRank( int aRank ) override;
265  int Rank() const override;
266 
267  bool HasLoops() const;
268  bool HasLockedSegments() const;
269 
270  OPT_BOX2I ChangedArea( const LINE* aOther ) const;
271 
272 private:
273 
274  void dragSegment45( const VECTOR2I& aP, int aIndex, int aSnappingThreshold );
275  void dragCorner45( const VECTOR2I& aP, int aIndex, int aSnappingThreshold );
276  void dragSegmentFree( const VECTOR2I& aP, int aIndex, int aSnappingThreshold );
277  void dragCornerFree( const VECTOR2I& aP, int aIndex, int aSnappingThreshold );
278 
279  VECTOR2I snapToNeighbourSegments( const SHAPE_LINE_CHAIN& aPath, const VECTOR2I &aP,
280  int aIndex, int aThreshold) const;
281 
282  VECTOR2I snapDraggedCorner( const SHAPE_LINE_CHAIN& aPath, const VECTOR2I &aP,
283  int aIndex, int aThreshold ) const;
284 
286  void copyLinks( const LINE* aParent ) ;
287 
290  SEGMENT_REFS m_segmentRefs;
291 
293  SHAPE_LINE_CHAIN m_line;
294 
296  int m_width;
297 
299  bool m_hasVia;
300 
302  VIA m_via;
303 };
304 
305 }
306 
307 #endif // __PNS_LINE_H
PNS::LINE
Definition: pns_line.h:61
SHAPE_LINE_CHAIN::SetWidth
void SetWidth(int aWidth)
Sets the width of all segments in the chain.
Definition: shape_line_chain.h:183
PNS::LINE::Walkaround
bool Walkaround(SHAPE_LINE_CHAIN aObstacle, SHAPE_LINE_CHAIN &aPre, SHAPE_LINE_CHAIN &aWalk, SHAPE_LINE_CHAIN &aPost, bool aCw) const
Calculates a line thightly wrapping a convex hull of an obstacle object (aObstacle).
PNS::LINE::LinkSegment
void LinkSegment(SEGMENT *aSeg)
Adds a reference to a segment registered in a NODE that is a part of this line.
Definition: pns_line.h:176
PNS::LINE::CountCorners
int CountCorners(int aAngles) const
Returns the number of corners of angles specified by mask aAngles.
PNS::LINE::CLine
const SHAPE_LINE_CHAIN & CLine() const
Const accessor to the underlying shape
Definition: pns_line.h:124
PNS::SEGMENT
Definition: pns_segment.h:39
SHAPE_LINE_CHAIN::SegmentCount
int SegmentCount() const
Function SegmentCount()
Definition: shape_line_chain.h:203
SHAPE_LINE_CHAIN::CSegment
const SEG CSegment(int aIndex) const
Function CSegment()
Definition: shape_line_chain.h:250
PNS::LINE::CPoint
const VECTOR2I & CPoint(int aIdx) const
Returns the aIdx-th point of the line
Definition: pns_line.h:142
VECTOR2< int >
PNS::VIA
Definition: pns_via.h:38
PNS::LINE::SetShape
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Assigns a shape to the line (a polyline/line chain)
Definition: pns_line.h:105
PNS::LINE::Clone
virtual LINE * Clone() const override
Function Clone()
PNS::LINE::Reverse
void Reverse()
Reverses the point/vertex order
PNS::LINE::ClipToNearestObstacle
const LINE ClipToNearestObstacle(NODE *aNode) const
Clips the line to the nearest obstacle, traversing from the line's start vertex (0).
PNS::LINE::SetWidth
void SetWidth(int aWidth)
Sets line width
Definition: pns_line.h:154
PNS::LINE::ClipVertexRange
void ClipVertexRange(int aStart, int aEnd)
Clips the line to a given range of vertices.
PNS::LINE::PointCount
int PointCount() const
Returns the number of points in the line
Definition: pns_line.h:136
PNS::LINE::Shape
const SHAPE * Shape() const override
Returns the shape of the line
Definition: pns_line.h:112
SHAPE_LINE_CHAIN::PointCount
int PointCount() const
Function PointCount()
Definition: shape_line_chain.h:218
SHAPE_LINE_CHAIN::CPoint
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
Definition: shape_line_chain.h:285
PNS::LINE::ContainsSegment
bool ContainsSegment(SEGMENT *aSeg) const
Checks if the segment aSeg is a part of the line.
Definition: pns_line.h:199
SEG
Definition: seg.h:37
PNS::ITEM
Class ITEM.
Definition: pns_item.h:55
PNS::LINE::ClearSegmentLinks
void ClearSegmentLinks()
Erases the linking information. Used to detach the line from the owning node.
PNS::LINE::LinkedSegments
SEGMENT_REFS & LinkedSegments()
Returns the list of segments from the owning node that constitute this line (or NULL if the line is n...
Definition: pns_line.h:183
PNS::LINE::Line
SHAPE_LINE_CHAIN & Line()
Modifiable accessor to the underlying shape
Definition: pns_line.h:118
PNS::LINE::ShowLinks
void ShowLinks() const
Prints out all linked segments
PNS::NODE
Class NODE.
Definition: pns_node.h:138
SHAPE_LINE_CHAIN
Class SHAPE_LINE_CHAIN.
Definition: shape_line_chain.h:50
PNS::ITEM::Kind
PnsKind Kind() const
Function Kind()
Definition: pns_item.h:123
SHAPE
Class SHAPE.
Definition: shape.h:59
PNS::LINE::LINE
LINE(const LINE &aBase, const SHAPE_LINE_CHAIN &aLine)
Constructor Copies properties (net, layers, etc.) from a base line and replaces the shape by another.
Definition: pns_line.h:82
PNS::LINE::LinkCount
int LinkCount() const
Returns the number of segments that were assembled together to form this line.
Definition: pns_line.h:214
PNS::LINE::CompareGeometry
bool CompareGeometry(const LINE &aOther)
Returns true if the line is geometrically identical as line aOther
PNS::LINE::LINE
LINE()
Constructor Makes an empty line.
Definition: pns_line.h:69
PNS::LINE::Width
int Width() const
Returns line width
Definition: pns_line.h:161
PNS::LINE::SegmentCount
int SegmentCount() const
Returns the number of segments in the line
Definition: pns_line.h:130
PNS::LINE::CSegment
const SEG CSegment(int aIdx) const
Returns the aIdx-th segment of the line
Definition: pns_line.h:148