kicad/pcbnew/router/pns_line_placer.h

478 lines
12 KiB
C
Raw Normal View History

/*
* KiRouter - a push-and-(sometimes-)shove PCB router
*
* Copyright (C) 2013-2017 CERN
* Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
2013-09-26 21:53:54 +00:00
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
2013-09-26 21:53:54 +00:00
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
2013-09-26 21:53:54 +00:00
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __PNS_LINE_PLACER_H
#define __PNS_LINE_PLACER_H
#include <math/vector2d.h>
#include <geometry/shape.h>
#include <geometry/shape_line_chain.h>
#include "pns_sizes_settings.h"
#include "pns_node.h"
#include "pns_via.h"
#include "pns_line.h"
#include "pns_placement_algo.h"
2013-09-26 21:53:54 +00:00
namespace PNS {
class ROUTER;
class SHOVE;
class OPTIMIZER;
class VIA;
class SIZES_SETTINGS;
class NODE;
class FIXED_TAIL {
public:
FIXED_TAIL ( int aLineCount = 1);
~FIXED_TAIL();
struct FIX_POINT
{
int layer;
bool placingVias;
VECTOR2I p;
DIRECTION_45 direction;
};
struct STAGE {
NODE *commit;
std::vector<FIX_POINT> pts;
};
void Clear();
void AddStage( VECTOR2I aStart, int aLayer, bool placingVias, DIRECTION_45 direction, NODE *aNode );
bool PopStage( STAGE& aStage );
int StageCount() const;
private:
std::vector<STAGE> m_stages;
};
class POSTURE_SOLVER
{
public:
POSTURE_SOLVER();
~POSTURE_SOLVER();
void Clear();
void AddTrailPoint( const VECTOR2I& aP );
void SetTolerance( int toll ) { m_tolerance = toll; }
void SetDefaultDirections( DIRECTION_45 aInitDirection, DIRECTION_45 aLastSegDir )
{
m_direction = aInitDirection;
m_lastSegDirection = aLastSegDir;
}
DIRECTION_45 GetPosture( const VECTOR2I& aP );
void FlipPosture();
private:
const double areaRatioThreshold = 1.5;
const double areaRatioEpsilon = 0.2;
SHAPE_LINE_CHAIN m_trail;
int m_tolerance;
DIRECTION_45 m_direction;
DIRECTION_45 m_lastSegDirection;
bool m_forced;
};
/**
* LINE_PLACER
*
2014-11-14 19:19:00 +00:00
* Single track placement algorithm. Interactively routes a track.
* Applies shove and walkaround algorithms when needed.
*/
class LINE_PLACER : public PLACEMENT_ALGO
{
2013-09-26 21:53:54 +00:00
public:
LINE_PLACER( ROUTER* aRouter );
~LINE_PLACER();
2013-09-26 21:53:54 +00:00
/**
* Function Start()
2014-11-14 19:19:00 +00:00
*
* Starts routing a single track at point aP, taking item aStartItem as anchor
* (unless NULL).
*/
2016-09-24 18:53:15 +00:00
bool Start( const VECTOR2I& aP, ITEM* aStartItem ) override;
/**
* Function Move()
2014-11-14 19:19:00 +00:00
*
* Moves the end of the currently routed trace to the point aP, taking
* aEndItem as anchor (if not NULL).
* (unless NULL).
*/
2016-09-24 18:53:15 +00:00
bool Move( const VECTOR2I& aP, ITEM* aEndItem ) override;
/**
* Function FixRoute()
2014-11-14 19:19:00 +00:00
*
* Commits the currently routed track to the parent node, taking
* aP as the final end point and aEndItem as the final anchor (if provided).
* @return true, if route has been commited. May return false if the routing
* result is violating design rules - in such case, the track is only committed
* if Settings.CanViolateDRC() is on.
*/
bool FixRoute( const VECTOR2I& aP, ITEM* aEndItem, bool aForceFinish ) override;
2014-11-14 19:19:00 +00:00
bool UnfixRoute() override;
bool CommitPlacement() override;
bool AbortPlacement() override;
bool HasPlacedAnything() const override;
/**
* Function ToggleVia()
2014-11-14 19:19:00 +00:00
*
* Enables/disables a via at the end of currently routed trace.
*/
2016-09-24 18:53:15 +00:00
bool ToggleVia( bool aEnabled ) override;
/**
* Function SetLayer()
*
* Sets the current routing layer.
*/
2016-09-24 18:53:15 +00:00
bool SetLayer( int aLayer ) override;
/**
* Function Head()
*
* Returns the "head" of the line being placed, that is the volatile part
* that has not "settled" yet.
*/
const LINE& Head() const { return m_head; }
2014-11-14 19:19:00 +00:00
/**
* Function Tail()
*
* Returns the "tail" of the line being placed, the part which has already wrapped around
* and shoved some obstacles.
*/
const LINE& Tail() const { return m_tail; }
/**
* Function Trace()
*
* Returns the complete routed line.
*/
const LINE Trace() const;
/**
* Function Traces()
*
* Returns the complete routed line, as a single-member ITEM_SET.
*/
2016-09-24 18:53:15 +00:00
const ITEM_SET Traces() override;
/**
* Function CurrentEnd()
*
* Returns the current end of the line being placed. It may not be equal
* to the cursor position due to collisions.
2014-11-14 19:19:00 +00:00
*/
2016-09-25 17:06:49 +00:00
const VECTOR2I& CurrentEnd() const override
2013-09-26 21:53:54 +00:00
{
return m_currentEnd;
2013-09-26 21:53:54 +00:00
}
/**
* Function CurrentNet()
*
* Returns the net code of currently routed track.
2014-11-14 19:19:00 +00:00
*/
2016-09-25 17:06:49 +00:00
const std::vector<int> CurrentNets() const override
{
return std::vector<int>( 1, m_currentNet );
}
/**
* Function CurrentLayer()
*
* Returns the layer of currently routed track.
2014-11-14 19:19:00 +00:00
*/
2016-09-25 17:06:49 +00:00
int CurrentLayer() const override
{
return m_currentLayer;
}
/**
* Function CurrentNode()
*
* Returns the most recent world state.
*/
2016-09-24 18:53:15 +00:00
NODE* CurrentNode( bool aLoopsRemoved = false ) const override;
2014-11-14 19:19:00 +00:00
/**
* Function FlipPosture()
*
* Toggles the current posture (straight/diagonal) of the trace head.
*/
2016-09-24 18:53:15 +00:00
void FlipPosture() override;
2014-11-14 19:19:00 +00:00
/**
* Function UpdateSizes()
*
* Performs on-the-fly update of the width, via diameter & drill size from
* a settings class. Used to dynamically change these parameters as
* the track is routed.
*/
2016-09-24 18:53:15 +00:00
void UpdateSizes( const SIZES_SETTINGS& aSizes ) override;
2013-09-26 21:53:54 +00:00
2016-09-24 18:53:15 +00:00
void SetOrthoMode( bool aOrthoMode ) override;
2016-09-25 17:06:49 +00:00
bool IsPlacingVia() const override { return m_placingVia; }
2016-09-24 18:53:15 +00:00
void GetModifiedNets( std::vector<int>& aNets ) const override;
/**
* Function SplitAdjacentSegments()
*
* Checks if point aP lies on segment aSeg. If so, splits the segment in two,
* forming a joint at aP and stores updated topology in node aNode.
*/
bool SplitAdjacentSegments( NODE* aNode, ITEM* aSeg, const VECTOR2I& aP );
private:
2013-09-26 21:53:54 +00:00
/**
* Function route()
2013-09-26 21:53:54 +00:00
*
* Re-routes the current track to point aP. Returns true, when routing has
* completed successfully (i.e. the trace end has reached point aP), and false
* if the trace was stuck somewhere on the way. May call routeStep()
2013-09-26 21:53:54 +00:00
* repetitively due to mouse smoothing.
* @param aP ending point of current route.
* @return true, if the routing is complete.
*/
bool route( const VECTOR2I& aP );
2013-09-26 21:53:54 +00:00
/**
* Function updateLeadingRatLine()
*
* Draws the "leading" ratsnest line, which connects the end of currently
2014-11-14 19:19:00 +00:00
* routed track and the nearest yet unrouted item. If the routing for
* current net is complete, draws nothing.
*/
void updateLeadingRatLine();
2014-11-14 19:19:00 +00:00
/**
* Function setWorld()
*
* Sets the board to route.
*/
void setWorld( NODE* aWorld );
2014-11-14 19:19:00 +00:00
/**
* Function startPlacement()
*
* Initializes placement of a new line with given parameters.
*/
2016-08-15 15:16:53 +00:00
void initPlacement();
2013-09-26 21:53:54 +00:00
/**
* Function setInitialDirection()
*
* Sets preferred direction of the very first track segment to be laid.
* Used by posture switching mechanism.
*/
void setInitialDirection( const DIRECTION_45& aDirection );
2013-09-26 21:53:54 +00:00
/**
* Function removeLoops()
*
* Searches aNode for traces concurrent to aLatest and removes them. Updated
* topology is stored in aNode.
*/
void removeLoops( NODE* aNode, LINE& aLatest );
2013-09-26 21:53:54 +00:00
/**
* Function simplifyNewLine()
*
* Assembles a line starting from segment aLatest, removes collinear segments
2014-11-14 19:19:00 +00:00
* and redundant vertexes. If a simplification bhas been found, replaces the
* old line with the simplified one in aNode.
*/
void simplifyNewLine( NODE* aNode, SEGMENT* aLatest );
2013-09-26 21:53:54 +00:00
/**
* Function checkObtusity()
*
* Helper function, checking if segments a and b form an obtuse angle
2013-09-26 21:53:54 +00:00
* (in 45-degree regime).
* @return true, if angle (aA, aB) is obtuse
2013-09-26 21:53:54 +00:00
*/
bool checkObtusity( const SEG& aA, const SEG& aB ) const;
2013-09-26 21:53:54 +00:00
/**
* Function handleSelfIntersections()
*
* Checks if the head of the track intersects its tail. If so, cuts the
2013-09-26 21:53:54 +00:00
* tail up to the intersecting segment and fixes the head direction to match
* the last segment before the cut.
* @return true if the line has been changed.
*/
bool handleSelfIntersections();
2013-09-26 21:53:54 +00:00
/**
* Function handlePullback()
*
* Deals with pull-back: reduces the tail if head trace is moved backwards
2013-09-26 21:53:54 +00:00
* wrs to the current tail direction.
* @return true if the line has been changed.
*/
bool handlePullback();
/**
* Function mergeHead()
*
* Moves "estabished" segments from the head to the tail if certain
2013-09-26 21:53:54 +00:00
* conditions are met.
* @return true, if the line has been changed.
*/
bool mergeHead();
/**
* Function reduceTail()
*
* Attempts to reduce the numer of segments in the tail by trying to replace a
* certain number of latest tail segments with a direct trace leading to aEnd
2013-09-26 21:53:54 +00:00
* that does not collide with anything.
* @param aEnd: current routing destination point.
* @return true if the line has been changed.
*/
bool reduceTail( const VECTOR2I& aEnd );
2013-09-26 21:53:54 +00:00
/**
* Function optimizeTailHeadTransition()
*
* Tries to reduce the corner count of the most recent part of tail/head by
2013-09-26 21:53:54 +00:00
* merging obtuse/collinear segments.
* @return true, if the line has been changed.
*/
bool optimizeTailHeadTransition();
/**
* Function routeHead()
*
* Computes the head trace between the current start point (m_p_start) and
* point aP, starting with direction defined in m_direction. The trace walks
* around all colliding solid or non-movable items. Movable segments are
2013-09-26 21:53:54 +00:00
* ignored, as they'll be handled later by the shove algorithm.
*/
bool routeHead( const VECTOR2I& aP, LINE& aNewHead);
2013-09-26 21:53:54 +00:00
/**
* Function routeStep()
*
* Performs a single routing alorithm step, for the end point aP.
* @param aP ending point of current route
* @return true, if the line has been changed.
*/
void routeStep( const VECTOR2I& aP );
const LINE reduceToNearestObstacle( const LINE& aOriginalLine );
bool rhStopAtNearestObstacle( const VECTOR2I& aP, LINE& aNewHead );
///> route step, walkaround mode
bool rhWalkOnly( const VECTOR2I& aP, LINE& aNewHead);
2013-09-26 21:53:54 +00:00
///> route step, shove mode
bool rhShoveOnly( const VECTOR2I& aP, LINE& aNewHead);
2013-09-26 21:53:54 +00:00
///> route step, mark obstacles mode
bool rhMarkObstacles( const VECTOR2I& aP, LINE& aNewHead );
const VIA makeVia( const VECTOR2I& aP );
2015-02-18 16:53:46 +00:00
bool buildInitialLine( const VECTOR2I& aP, LINE& aHead );
2015-02-18 16:53:46 +00:00
2013-09-26 21:53:54 +00:00
///> current routing direction
DIRECTION_45 m_direction;
///> routing direction for new traces
DIRECTION_45 m_initial_direction;
///> routing "head": volatile part of the track from the previously
/// analyzed point to the current routing destination
LINE m_head;
2013-09-26 21:53:54 +00:00
///> routing "tail": part of the track that has been already fixed due to collisions with obstacles
LINE m_tail;
2013-09-26 21:53:54 +00:00
///> pointer to world to search colliding items
NODE* m_world;
2013-09-26 21:53:54 +00:00
///> current routing start point (end of tail, beginning of head)
VECTOR2I m_p_start;
///> The shove engine
std::unique_ptr< SHOVE > m_shove;
2013-09-26 21:53:54 +00:00
///> Current world state
NODE* m_currentNode;
2013-09-26 21:53:54 +00:00
///> Postprocessed world state (including marked collisions & removed loops)
NODE* m_lastNode;
SIZES_SETTINGS m_sizes;
2013-09-26 21:53:54 +00:00
///> Are we placing a via?
bool m_placingVia;
int m_currentNet;
int m_currentLayer;
VECTOR2I m_currentEnd, m_currentStart;
LINE m_currentTrace;
PNS_MODE m_currentMode;
ITEM* m_startItem;
bool m_idle;
bool m_chainedPlacement;
bool m_orthoMode;
bool m_placementCorrect;
FIXED_TAIL m_fixedTail;
POSTURE_SOLVER m_postureSolver;
};
2013-09-26 21:53:54 +00:00
}
2013-09-26 21:53:54 +00:00
#endif // __PNS_LINE_PLACER_H