Horizon
pns_item.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_ITEM_H
23 #define __PNS_ITEM_H
24 
25 #include <memory>
26 #include <math/vector2d.h>
27 
28 #include <geometry/shape.h>
29 #include <geometry/shape_line_chain.h>
30 
31 #include "pns_layerset.h"
32 
33 class BOARD_CONNECTED_ITEM;
34 
35 namespace PNS {
36 
37 class NODE;
38 class PNS_HORIZON_PARENT_ITEM;
39 
40 enum LineMarker {
41  MK_HEAD = ( 1 << 0 ),
42  MK_VIOLATION = ( 1 << 3 ),
43  MK_LOCKED = ( 1 << 4 ),
44  MK_DP_COUPLED = ( 1 << 5 )
45 };
46 
47 
54 class ITEM
55 {
56 public:
57  static const int UnusedNet = INT_MAX;
58 
60  enum PnsKind
61  {
62  SOLID_T = 1,
63  LINE_T = 2,
64  JOINT_T = 4,
65  SEGMENT_T = 8,
66  VIA_T = 16,
67  DIFF_PAIR_T = 32,
68  ANY_T = 0xff
69  };
70 
71  ITEM( PnsKind aKind )
72  {
73  m_net = UnusedNet;
74  m_movable = true;
75  m_kind = aKind;
76  m_parent = 0;
77  m_owner = NULL;
78  m_marker = 0;
79  m_rank = -1;
80  }
81 
82  ITEM( const ITEM& aOther )
83  {
84  m_layers = aOther.m_layers;
85  m_net = aOther.m_net;
86  m_movable = aOther.m_movable;
87  m_kind = aOther.m_kind;
88  m_parent = aOther.m_parent;
89  m_owner = NULL;
90  m_marker = aOther.m_marker;
91  m_rank = aOther.m_rank;
92  }
93 
94  virtual ~ITEM();
95 
101  virtual ITEM* Clone() const = 0;
102 
103  /*
104  * Function Hull()
105  *
106  * Returns a convex polygon "hull" of a the item, that is used as the walk-around
107  * path.
108  * @param aClearance defines how far from the body of the item the hull should be,
109  * @param aWalkaroundThickness is the width of the line that walks around this hull.
110  */
111  virtual const SHAPE_LINE_CHAIN Hull( int aClearance = 0, int aWalkaroundThickness = 0 ) const
112  {
113  return SHAPE_LINE_CHAIN();
114  }
115 
121  PnsKind Kind() const
122  {
123  return m_kind;
124  }
125 
131  bool OfKind( int aKindMask ) const
132  {
133  return ( aKindMask & m_kind ) != 0;
134  }
135 
141  const std::string KindStr() const;
142 
148  void SetParent( const PNS_HORIZON_PARENT_ITEM *aParent )
149  {
150  m_parent = aParent;
151  }
152 
158  auto Parent() const
159  {
160  return m_parent;
161  }
162 
168  void SetNet( int aNet )
169  {
170  m_net = aNet;
171  }
172 
178  int Net() const
179  {
180  return m_net;
181  }
182 
188  void SetLayers( const LAYER_RANGE& aLayers )
189  {
190  m_layers = aLayers;
191  }
192 
198  void SetLayer( int aLayer )
199  {
200  m_layers = LAYER_RANGE( aLayer, aLayer );
201  }
202 
208  const LAYER_RANGE& Layers() const
209  {
210  return m_layers;
211  }
212 
218  virtual int Layer() const
219  {
220  return Layers().Start();
221  }
222 
229  bool LayersOverlap( const ITEM* aOther ) const
230  {
231  return Layers().Overlaps( aOther->Layers() );
232  }
233 
240  void SetOwner( NODE* aOwner )
241  {
242  m_owner = aOwner;
243  }
244 
250  bool BelongsTo( NODE* aNode ) const
251  {
252  return m_owner == aNode;
253  }
254 
260  NODE* Owner() const { return m_owner; }
261 
276  virtual bool Collide( const ITEM* aOther, int aClearance, bool aNeedMTV,
277  VECTOR2I& aMTV, bool aDifferentNetsOnly = true ) const;
278 
284  bool Collide( const ITEM* aOther, int aClearance, bool aDifferentNetsOnly = true ) const
285  {
286  VECTOR2I dummy;
287 
288  return Collide( aOther, aClearance, false, dummy, aDifferentNetsOnly );
289  }
290 
297  virtual const SHAPE* Shape() const
298  {
299  return NULL;
300  }
301 
302  virtual void Mark( int aMarker )
303  {
304  m_marker = aMarker;
305  }
306 
307  virtual void Unmark( int aMarker = -1 )
308  {
309  m_marker &= ~aMarker;
310  }
311 
312  virtual int Marker() const
313  {
314  return m_marker;
315  }
316 
317  virtual void SetRank( int aRank )
318  {
319  m_rank = aRank;
320  }
321 
322  virtual int Rank() const
323  {
324  return m_rank;
325  }
326 
327  virtual VECTOR2I Anchor( int n ) const
328  {
329  return VECTOR2I();
330  }
331 
332  virtual int AnchorCount() const
333  {
334  return 0;
335  }
336 
337  bool IsLocked() const
338  {
339  return Marker() & MK_LOCKED;
340  }
341 
342 private:
343  bool collideSimple( const ITEM* aOther, int aClearance, bool aNeedMTV,
344  VECTOR2I& aMTV, bool aDifferentNetsOnly ) const;
345 
346 protected:
347  PnsKind m_kind;
348 
349  const PNS_HORIZON_PARENT_ITEM* m_parent;
350  NODE* m_owner;
351  LAYER_RANGE m_layers;
352 
353  bool m_movable;
354  int m_net;
355  int m_marker;
356  int m_rank;
357 };
358 
359 template< typename T, typename S >
360 std::unique_ptr< T > ItemCast( std::unique_ptr< S > aPtr )
361 {
362  static_assert(std::is_base_of< ITEM, S >::value, "Need to be handed a ITEM!");
363  static_assert(std::is_base_of< ITEM, T >::value, "Need to cast to an ITEM!");
364  return std::unique_ptr< T >( static_cast<T*>(aPtr.release()) );
365 }
366 
367 template< typename T >
368 std::unique_ptr< typename std::remove_const< T >::type > Clone( const T& aItem )
369 {
370  static_assert(std::is_base_of< ITEM, T >::value, "Need to be handed an ITEM!");
371  return std::unique_ptr< typename std::remove_const< T >::type >( aItem.Clone() );
372 }
373 
374 }
375 
376 #endif // __PNS_ITEM_H
Class ITEM.
Definition: pns_item.h:54
virtual ITEM * Clone() const =0
Function Clone()
void SetOwner(NODE *aOwner)
Functon SetOwner()
Definition: pns_item.h:240
virtual int Layer() const
Function Layer()
Definition: pns_item.h:218
Class NODE.
Definition: pns_node.h:138
void SetLayer(int aLayer)
Function SetLayer()
Definition: pns_item.h:198
Definition: pns_horizon_iface.hpp:26
void SetNet(int aNet)
Function SetNet()
Definition: pns_item.h:168
bool LayersOverlap(const ITEM *aOther) const
Function LayersOverlap()
Definition: pns_item.h:229
bool BelongsTo(NODE *aNode) const
Function BelongsTo()
Definition: pns_item.h:250
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:297
int Net() const
Function Net()
Definition: pns_item.h:178
void SetParent(const PNS_HORIZON_PARENT_ITEM *aParent)
Function SetParent()
Definition: pns_item.h:148
Class SHAPE.
Definition: shape.h:57
void SetLayers(const LAYER_RANGE &aLayers)
Function SetLayers()
Definition: pns_item.h:188
PnsKind
Supported item types
Definition: pns_item.h:60
const std::string KindStr() const
Function KindStr()
Definition: pns_item.cpp:63
Class SHAPE_LINE_CHAIN.
Definition: shape_line_chain.h:47
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:131
PnsKind Kind() const
Function Kind()
Definition: pns_item.h:121
auto Parent() const
Function Parent()
Definition: pns_item.h:158
virtual bool Collide(const ITEM *aOther, int aClearance, bool aNeedMTV, VECTOR2I &aMTV, bool aDifferentNetsOnly=true) const
Function Collide()
Definition: pns_item.cpp:44
bool Collide(const ITEM *aOther, int aClearance, bool aDifferentNetsOnly=true) const
Function Collide()
Definition: pns_item.h:284
NODE * Owner() const
Function Owner()
Definition: pns_item.h:260
Definition: pns_algo_base.cpp:26
Class LAYER_RANGE.
Definition: pns_layerset.h:32
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:208