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.

277 lines
7.3 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. * KiRouter - a push-and-(sometimes-)shove PCB router
  3. *
  4. * Copyright (C) 2013-2014 CERN
  5. * Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  8. *
  9. * This program is free software: you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation, either version 3 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #ifndef __PNS_DIFF_PLACER_H
  23. #define __PNS_DIFF_PLACER_H
  24. #include <math/vector2d.h>
  25. #include "pns_sizes_settings.h"
  26. #include "pns_node.h"
  27. #include "pns_via.h"
  28. #include "pns_line.h"
  29. #include "pns_algo_base.h"
  30. #include "pns_diff_pair.h"
  31. #include "pns_placement_algo.h"
  32. namespace PNS {
  33. class ROUTER;
  34. class SHOVE;
  35. class OPTIMIZER;
  36. class VIA;
  37. class SIZES_SETTINGS;
  38. /**
  39. * Single track placement algorithm.
  40. *
  41. * Interactively routes a track and applies shove and walk around algorithms when needed.
  42. */
  43. class DIFF_PAIR_PLACER : public PLACEMENT_ALGO
  44. {
  45. public:
  46. DIFF_PAIR_PLACER( ROUTER* aRouter );
  47. ~DIFF_PAIR_PLACER();
  48. static bool FindDpPrimitivePair( NODE* aWorld, const VECTOR2I& aP, ITEM* aItem,
  49. DP_PRIMITIVE_PAIR& aPair, wxString* aErrorMsg = nullptr );
  50. /**
  51. * Start routing a single track at point aP, taking item aStartItem as anchor (unless NULL).
  52. */
  53. bool Start( const VECTOR2I& aP, ITEM* aStartItem ) override;
  54. /**
  55. * Move the end of the currently routed trace to the point \a aP, taking \a aEndItem as
  56. * anchor (if not NULL).
  57. */
  58. bool Move( const VECTOR2I& aP, ITEM* aEndItem ) override;
  59. /**
  60. * Commit the currently routed track to the parent node, taking \a aP as the final end
  61. * point and \a aEndItem as the final anchor (if provided).
  62. *
  63. * @return true if route has been committed. May return false if the routing result is
  64. * violating design rules. In such cases, the track is only committed if
  65. * #Settings.CanViolateDRC() is on.
  66. */
  67. bool FixRoute( const VECTOR2I& aP, ITEM* aEndItem, bool aForceFinish ) override;
  68. /// @copydoc PLACEMENT_ALGO::CommitPlacement()
  69. bool CommitPlacement() override;
  70. /// @copydoc PLACEMENT_ALGO::AbortPlacement()
  71. bool AbortPlacement() override;
  72. /// @copydoc PLACEMENT_ALGO::HasPlacedAnything()
  73. bool HasPlacedAnything() const override;
  74. /**
  75. * Enable/disable a via at the end of currently routed trace.
  76. */
  77. bool ToggleVia( bool aEnabled ) override;
  78. /**
  79. * Set the current routing layer.
  80. */
  81. bool SetLayer( int aLayer ) override;
  82. /**
  83. * Return the complete routed line, as a single-member ITEM_SET.
  84. */
  85. const ITEM_SET Traces() override;
  86. /**
  87. * Return the current end of the line being placed. It may not be equal to the cursor
  88. * position due to collisions.
  89. */
  90. const VECTOR2I& CurrentEnd() const override
  91. {
  92. return m_currentEnd;
  93. }
  94. /**
  95. * Return the net code of currently routed track.
  96. */
  97. const std::vector<int> CurrentNets() const override;
  98. /**
  99. * Return the layer of currently routed track.
  100. */
  101. int CurrentLayer() const override
  102. {
  103. return m_currentLayer;
  104. }
  105. /**
  106. * Return the most recent world state.
  107. */
  108. NODE* CurrentNode( bool aLoopsRemoved = false ) const override;
  109. /**
  110. * Toggle the current posture (straight/diagonal) of the trace head.
  111. */
  112. void FlipPosture() override;
  113. /**
  114. * Perform on-the-fly update of the width, via diameter & drill size from a settings class.
  115. *
  116. * Used to dynamically change these parameters as the track is routed.
  117. */
  118. void UpdateSizes( const SIZES_SETTINGS& aSizes ) override;
  119. bool IsPlacingVia() const override { return m_placingVia; }
  120. void SetOrthoMode( bool aOrthoMode ) override;
  121. void GetModifiedNets( std::vector<int>& aNets ) const override;
  122. private:
  123. int viaGap() const;
  124. int gap() const;
  125. /**
  126. * Re-route the current track to point \a aP.
  127. *
  128. * Returns true, when routing has completed successfully (i.e. the trace end has reached
  129. * point aP), and false if the trace was stuck somewhere on the way. May call routeStep()
  130. * repetitively due to mouse smoothing.
  131. *
  132. * @param aP is the ending point of current route.
  133. * @return true if the routing is complete.
  134. */
  135. bool route( const VECTOR2I& aP );
  136. /**
  137. * Draw the "leading" ratsnest line, which connects the end of currently routed track and
  138. * the nearest yet unrouted item. If the routing for current net is complete, draws nothing.
  139. */
  140. void updateLeadingRatLine();
  141. /**
  142. * Set the board to route.
  143. */
  144. void setWorld( NODE* aWorld );
  145. /**
  146. * Initialize placement of a new line with given parameters.
  147. */
  148. void initPlacement( );
  149. /**
  150. * Set preferred direction of the very first track segment to be laid.
  151. *
  152. * Used by posture switching mechanism.
  153. */
  154. void setInitialDirection( const DIRECTION_45& aDirection );
  155. bool routeHead( const VECTOR2I& aP );
  156. bool tryWalkDp( NODE* aNode, DIFF_PAIR& aPair, bool aSolidsOnly );
  157. ///< route step, walk around mode
  158. bool rhWalkOnly( const VECTOR2I& aP );
  159. ///< route step, shove mode
  160. bool rhShoveOnly ( const VECTOR2I& aP );
  161. ///< route step, mark obstacles mode
  162. bool rhMarkObstacles( const VECTOR2I& aP );
  163. const VIA makeVia ( const VECTOR2I& aP, int aNet );
  164. bool attemptWalk( NODE* aNode, DIFF_PAIR* aCurrent, DIFF_PAIR& aWalk, bool aPFirst,
  165. bool aWindCw, bool aSolidsOnly );
  166. bool propagateDpHeadForces ( const VECTOR2I& aP, VECTOR2I& aNewP );
  167. enum State {
  168. RT_START = 0,
  169. RT_ROUTE = 1,
  170. RT_FINISH = 2
  171. };
  172. State m_state;
  173. bool m_chainedPlacement;
  174. bool m_initialDiagonal;
  175. bool m_startDiagonal;
  176. bool m_fitOk;
  177. int m_netP, m_netN;
  178. DP_PRIMITIVE_PAIR m_start;
  179. OPT<DP_PRIMITIVE_PAIR> m_prevPair;
  180. ///< current algorithm iteration
  181. int m_iteration;
  182. ///< pointer to world to search colliding items
  183. NODE* m_world;
  184. ///< current routing start point (end of tail, beginning of head)
  185. VECTOR2I m_p_start;
  186. ///< The shove engine
  187. SHOVE* m_shove;
  188. ///< Current world state
  189. NODE* m_currentNode;
  190. ///< Postprocessed world state (including marked collisions & removed loops)
  191. NODE* m_lastNode;
  192. SIZES_SETTINGS m_sizes;
  193. ///< Are we placing a via?
  194. bool m_placingVia;
  195. ///< current via diameter
  196. int m_viaDiameter;
  197. ///< current via drill
  198. int m_viaDrill;
  199. ///< current track width
  200. int m_currentWidth;
  201. int m_currentNet;
  202. int m_currentLayer;
  203. bool m_startsOnVia;
  204. bool m_orthoMode;
  205. bool m_snapOnTarget;
  206. VECTOR2I m_currentEnd, m_currentStart;
  207. DIFF_PAIR m_currentTrace;
  208. ITEM* m_currentEndItem;
  209. PNS_MODE m_currentMode;
  210. bool m_idle;
  211. };
  212. }
  213. #endif // __PNS_LINE_PLACER_H