You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.8 KiB

  1. /*
  2. * KiRouter - a push-and-(sometimes-)shove PCB router
  3. *
  4. * Copyright (C) 2013 CERN
  5. * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.or/licenses/>.
  19. */
  20. #ifndef __PNS_JOINT_H
  21. #define __PNS_JOINT_H
  22. #include <vector>
  23. #include <boost/functional/hash.hpp>
  24. #include <math/vector2d.h>
  25. #include "pns_item.h"
  26. #include "pns_segment.h"
  27. /**
  28. * Class PNS_JOINT
  29. *
  30. * Represents a 2D point on a given set of layers and belonging to a certain
  31. * net, that links together a number of board items.
  32. * A hash table of joints is used by the router to follow connectivity between
  33. * the items.
  34. **/
  35. class PNS_JOINT : public PNS_ITEM
  36. {
  37. public:
  38. typedef std::vector<PNS_ITEM*> LinkedItems;
  39. ///> Joints are hashed by their position, layers and net.
  40. /// Linked items are, obviously, not hashed
  41. struct HashTag
  42. {
  43. VECTOR2I pos;
  44. int net;
  45. };
  46. PNS_JOINT() :
  47. PNS_ITEM( JOINT ) {}
  48. PNS_JOINT( const VECTOR2I& aPos, const PNS_LAYERSET& aLayers,
  49. int aNet = -1 ) :
  50. PNS_ITEM( JOINT )
  51. {
  52. m_tag.pos = aPos;
  53. m_tag.net = aNet;
  54. m_layers = aLayers;
  55. }
  56. PNS_JOINT( const PNS_JOINT& b ) :
  57. PNS_ITEM( JOINT )
  58. {
  59. m_layers = b.m_layers;
  60. m_tag.pos = b.m_tag.pos;
  61. m_tag.net = b.m_tag.net;
  62. m_linkedItems = b.m_linkedItems;
  63. m_layers = b.m_layers;
  64. }
  65. PNS_ITEM* Clone() const
  66. {
  67. assert( false );
  68. return NULL;
  69. }
  70. ///> Returns true if the joint is a trivial line corner, connecting two
  71. /// segments of the same net, on the same layer.
  72. bool IsLineCorner() const
  73. {
  74. if( m_linkedItems.size() != 2 )
  75. return false;
  76. if( m_linkedItems[0]->GetKind() != SEGMENT ||
  77. m_linkedItems[1]->GetKind() != SEGMENT )
  78. return false;
  79. PNS_SEGMENT* seg1 = static_cast<PNS_SEGMENT*>( m_linkedItems[0] );
  80. PNS_SEGMENT* seg2 = static_cast<PNS_SEGMENT*>( m_linkedItems[1] );
  81. // joints between segments of different widths are not trivial.
  82. return seg1->GetWidth() == seg2->GetWidth();
  83. }
  84. ///> Links the joint to a given board item (when it's added to the PNS_NODE)
  85. void Link( PNS_ITEM* aItem )
  86. {
  87. LinkedItems::iterator f = std::find( m_linkedItems.begin(),
  88. m_linkedItems.end(), aItem );
  89. if( f != m_linkedItems.end() )
  90. return;
  91. m_linkedItems.push_back( aItem );
  92. }
  93. ///> Unlinks a given board item from the joint (upon its removal from a PNS_NODE)
  94. ///> Returns true if the joint became dangling after unlinking.
  95. bool Unlink( PNS_ITEM* aItem )
  96. {
  97. LinkedItems::iterator f = std::find( m_linkedItems.begin(),
  98. m_linkedItems.end(), aItem );
  99. if( f != m_linkedItems.end() )
  100. m_linkedItems.erase( f );
  101. return m_linkedItems.size() == 0;
  102. }
  103. ///> For trivial joints, returns the segment adjacent to (aCurrent). For non-trival ones, returns
  104. ///> NULL, indicating the end of line.
  105. PNS_SEGMENT* NextSegment( PNS_SEGMENT* aCurrent ) const
  106. {
  107. if( !IsLineCorner() )
  108. return NULL;
  109. return static_cast<PNS_SEGMENT*>( m_linkedItems[m_linkedItems[0] == aCurrent ? 1 : 0] );
  110. }
  111. /// trivial accessors
  112. const HashTag& GetTag() const { return m_tag; }
  113. const VECTOR2I& GetPos() const { return m_tag.pos; }
  114. int GetNet() const { return m_tag.net; }
  115. LinkedItems& GetLinkList() { return m_linkedItems; };
  116. ///> Returns the number of linked items of types listed in aMask.
  117. int LinkCount( int aMask = -1 ) const
  118. {
  119. int n = 0;
  120. for( LinkedItems::const_iterator i = m_linkedItems.begin();
  121. i != m_linkedItems.end(); ++i )
  122. if( (*i)->GetKind() & aMask )
  123. n++;
  124. return n;
  125. }
  126. void Dump() const;
  127. bool operator==( const PNS_JOINT& rhs ) const
  128. {
  129. return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
  130. }
  131. void Merge( const PNS_JOINT& aJoint )
  132. {
  133. if( !Overlaps( aJoint ) )
  134. return;
  135. m_layers.Merge( aJoint.m_layers );
  136. // fixme: duplicate links (?)
  137. for( LinkedItems::const_iterator i = aJoint.m_linkedItems.begin();
  138. i != aJoint.m_linkedItems.end(); ++i )
  139. m_linkedItems.push_back( *i );
  140. }
  141. bool Overlaps( const PNS_JOINT& rhs ) const
  142. {
  143. return m_tag.pos == rhs.m_tag.pos &&
  144. m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
  145. }
  146. private:
  147. ///> hash tag for unordered_multimap
  148. HashTag m_tag;
  149. ///> list of items linked to this joint
  150. LinkedItems m_linkedItems;
  151. };
  152. // hash function & comparison operator for boost::unordered_map<>
  153. inline bool operator==( PNS_JOINT::HashTag const& p1,
  154. PNS_JOINT::HashTag const& p2 )
  155. {
  156. return p1.pos == p2.pos && p1.net == p2.net;
  157. }
  158. inline std::size_t hash_value( PNS_JOINT::HashTag const& p )
  159. {
  160. std::size_t seed = 0;
  161. boost::hash_combine( seed, p.pos.x );
  162. boost::hash_combine( seed, p.pos.y );
  163. boost::hash_combine( seed, p.net );
  164. return seed;
  165. }
  166. #endif // __PNS_JOINT_H