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 = NULL;
77  m_owner = NULL;
78  m_marker = 0;
79  m_rank = -1;
80  m_routable = true;
81  }
82 
83  ITEM( const ITEM& aOther )
84  {
85  m_layers = aOther.m_layers;
86  m_net = aOther.m_net;
87  m_movable = aOther.m_movable;
88  m_kind = aOther.m_kind;
89  m_parent = aOther.m_parent;
90  m_owner = NULL;
91  m_marker = aOther.m_marker;
92  m_rank = aOther.m_rank;
93  m_routable = aOther.m_routable;
94  }
95 
96  virtual ~ITEM();
97 
103  virtual ITEM* Clone() const = 0;
104 
105  /*
106  * Function Hull()
107  *
108  * Returns a convex polygon "hull" of a the item, that is used as the walk-around
109  * path.
110  * @param aClearance defines how far from the body of the item the hull should be,
111  * @param aWalkaroundThickness is the width of the line that walks around this hull.
112  */
113  virtual const SHAPE_LINE_CHAIN Hull( int aClearance = 0, int aWalkaroundThickness = 0 ) const
114  {
115  return SHAPE_LINE_CHAIN();
116  }
117 
123  PnsKind Kind() const
124  {
125  return m_kind;
126  }
127 
133  bool OfKind( int aKindMask ) const
134  {
135  return ( aKindMask & m_kind ) != 0;
136  }
137 
143  const std::string KindStr() const;
144 
150  void SetParent( const PNS_HORIZON_PARENT_ITEM *aParent )
151  {
152  m_parent = aParent;
153  }
154 
160  auto Parent() const
161  {
162  return m_parent;
163  }
164 
170  void SetNet( int aNet )
171  {
172  m_net = aNet;
173  }
174 
180  int Net() const
181  {
182  return m_net;
183  }
184 
185  bool InAnyNet() const
186  {
187  return m_net != UnusedNet;
188  }
189 
195  void SetLayers( const LAYER_RANGE& aLayers )
196  {
197  m_layers = aLayers;
198  }
199 
205  void SetLayer( int aLayer )
206  {
207  m_layers = LAYER_RANGE( aLayer, aLayer );
208  }
209 
215  const LAYER_RANGE& Layers() const
216  {
217  return m_layers;
218  }
219 
225  virtual int Layer() const
226  {
227  return Layers().Start();
228  }
229 
236  bool LayersOverlap( const ITEM* aOther ) const
237  {
238  return Layers().Overlaps( aOther->Layers() );
239  }
240 
247  void SetOwner( NODE* aOwner )
248  {
249  m_owner = aOwner;
250  }
251 
257  bool BelongsTo( NODE* aNode ) const
258  {
259  return m_owner == aNode;
260  }
261 
267  NODE* Owner() const { return m_owner; }
268 
283  virtual bool Collide( const ITEM* aOther, int aClearance, bool aNeedMTV,
284  VECTOR2I& aMTV, bool aDifferentNetsOnly = true ) const;
285 
291  bool Collide( const ITEM* aOther, int aClearance, bool aDifferentNetsOnly = true ) const
292  {
293  VECTOR2I dummy;
294 
295  return Collide( aOther, aClearance, false, dummy, aDifferentNetsOnly );
296  }
297 
304  virtual const SHAPE* Shape() const
305  {
306  return NULL;
307  }
308 
309  virtual void Mark( int aMarker )
310  {
311  m_marker = aMarker;
312  }
313 
314  virtual void Unmark( int aMarker = -1 )
315  {
316  m_marker &= ~aMarker;
317  }
318 
319  virtual int Marker() const
320  {
321  return m_marker;
322  }
323 
324  virtual void SetRank( int aRank )
325  {
326  m_rank = aRank;
327  }
328 
329  virtual int Rank() const
330  {
331  return m_rank;
332  }
333 
334  virtual VECTOR2I Anchor( int n ) const
335  {
336  return VECTOR2I();
337  }
338 
339  virtual int AnchorCount() const
340  {
341  return 0;
342  }
343 
344  bool IsLocked() const
345  {
346  return Marker() & MK_LOCKED;
347  }
348 
349  void SetRoutable( bool aRoutable )
350  {
351  m_routable = aRoutable;
352  }
353 
354  bool IsRoutable() const
355  {
356  return m_routable;
357  }
358 
359 private:
360  bool collideSimple( const ITEM* aOther, int aClearance, bool aNeedMTV,
361  VECTOR2I& aMTV, bool aDifferentNetsOnly ) const;
362 
363 protected:
364  PnsKind m_kind;
365 
366  const PNS_HORIZON_PARENT_ITEM* m_parent;
367  NODE* m_owner;
368  LAYER_RANGE m_layers;
369 
370  bool m_movable;
371  int m_net;
372  int m_marker;
373  int m_rank;
374  bool m_routable;
375 };
376 
377 template< typename T, typename S >
378 std::unique_ptr< T > ItemCast( std::unique_ptr< S > aPtr )
379 {
380  static_assert(std::is_base_of< ITEM, S >::value, "Need to be handed a ITEM!");
381  static_assert(std::is_base_of< ITEM, T >::value, "Need to cast to an ITEM!");
382  return std::unique_ptr< T >( static_cast<T*>(aPtr.release()) );
383 }
384 
385 template< typename T >
386 std::unique_ptr< typename std::remove_const< T >::type > Clone( const T& aItem )
387 {
388  static_assert(std::is_base_of< ITEM, T >::value, "Need to be handed an ITEM!");
389  return std::unique_ptr< typename std::remove_const< T >::type >( aItem.Clone() );
390 }
391 
392 }
393 
394 #endif // __PNS_ITEM_H
PNS::ITEM::Parent
auto Parent() const
Function Parent()
Definition: pns_item.h:160
PNS::ITEM::Clone
virtual ITEM * Clone() const =0
Function Clone()
PNS::ITEM::SetOwner
void SetOwner(NODE *aOwner)
Functon SetOwner()
Definition: pns_item.h:247
PNS::ITEM::Layers
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:215
LAYER_RANGE
Class LAYER_RANGE.
Definition: pns_layerset.h:33
PNS::ITEM::SetNet
void SetNet(int aNet)
Function SetNet()
Definition: pns_item.h:170
PNS::ITEM::SetLayer
void SetLayer(int aLayer)
Function SetLayer()
Definition: pns_item.h:205
PNS::ITEM::Net
int Net() const
Function Net()
Definition: pns_item.h:180
VECTOR2< int >
PNS::PNS_HORIZON_PARENT_ITEM
Definition: pns_horizon_iface.hpp:28
PNS::ITEM::LayersOverlap
bool LayersOverlap(const ITEM *aOther) const
Function LayersOverlap()
Definition: pns_item.h:236
PNS::ITEM::Shape
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:304
PNS::ITEM::BelongsTo
bool BelongsTo(NODE *aNode) const
Function BelongsTo()
Definition: pns_item.h:257
PNS::ITEM::KindStr
const std::string KindStr() const
Function KindStr()
PNS::ITEM::SetParent
void SetParent(const PNS_HORIZON_PARENT_ITEM *aParent)
Function SetParent()
Definition: pns_item.h:150
PNS::ITEM::SetLayers
void SetLayers(const LAYER_RANGE &aLayers)
Function SetLayers()
Definition: pns_item.h:195
PNS::ITEM
Class ITEM.
Definition: pns_item.h:55
PNS::ITEM::Collide
virtual bool Collide(const ITEM *aOther, int aClearance, bool aNeedMTV, VECTOR2I &aMTV, bool aDifferentNetsOnly=true) const
Function Collide()
PNS::ITEM::PnsKind
PnsKind
Supported item types
Definition: pns_item.h:61
PNS::ITEM::Collide
bool Collide(const ITEM *aOther, int aClearance, bool aDifferentNetsOnly=true) const
Function Collide()
Definition: pns_item.h:291
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::ITEM::Layer
virtual int Layer() const
Function Layer()
Definition: pns_item.h:225
PNS::ITEM::Owner
NODE * Owner() const
Function Owner()
Definition: pns_item.h:267
PNS::ITEM::Collide
virtual bool Collide(const ITEM *aOther, int aClearance, bool aNeedMTV, VECTOR2I &aMTV, bool aDifferentNetsOnly=true) const
Function Collide()
Definition: pns_item.cpp:44
PNS::ITEM::OfKind
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:133