KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
length_calculation.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef PCBNEW_LENGTH_CALCULATION_H
25#define PCBNEW_LENGTH_CALCULATION_H
26
29#include <pad.h>
30#include <pcb_track.h>
31#include <layer_ids.h>
32#include <unordered_set>
34
35class BOARD;
36
42{
43public:
45 enum class TYPE
46 {
47 UNKNOWN,
48 PAD,
49 LINE,
50 VIA
51 };
52
56 enum class MERGE_STATUS
57 {
61 };
62
64 TYPE Type() const { return m_type; };
65
67 void SetPad( PAD* aPad )
68 {
69 m_type = TYPE::PAD;
70 m_pad = aPad;
71 }
72
74 PAD* GetPad() const { return m_pad; }
75
77 void SetLine( const SHAPE_LINE_CHAIN& aLine )
78 {
79 m_type = TYPE::LINE;
80 m_line = aLine;
81 }
82
84 SHAPE_LINE_CHAIN& GetLine() const { return m_line; }
85
87 void SetVia( PCB_VIA* aVia )
88 {
89 m_type = TYPE::VIA;
90 m_via = aVia;
91 }
92
94 PCB_VIA* GetVia() const { return m_via; }
95
97 void SetLayers( const PCB_LAYER_ID aStart, const PCB_LAYER_ID aEnd = PCB_LAYER_ID::UNDEFINED_LAYER )
98 {
99 m_layerStart = aStart;
100 m_layerEnd = aEnd;
101 }
102
105 void SetMergeStatus( const MERGE_STATUS aStatus ) { m_mergeStatus = aStatus; }
106
109
111 std::tuple<PCB_LAYER_ID, PCB_LAYER_ID> GetLayers() const { return { m_layerStart, m_layerEnd }; }
112
115
118
120 void CalculateViaLayers( const BOARD* aBoard );
121
122protected:
124 PAD* m_pad{ nullptr };
125
128
130 PCB_VIA* m_via{ nullptr };
131
133 PCB_LAYER_ID m_layerStart{ PCB_LAYER_ID::UNDEFINED_LAYER };
134
136 PCB_LAYER_ID m_layerEnd{ PCB_LAYER_ID::UNDEFINED_LAYER };
137
140
142 TYPE m_type{ TYPE::UNKNOWN };
143};
144
145
150{
151 int NumPads{ 0 };
152 int NumVias{ 0 };
153 int ViaLength{ 0 };
154 int64_t TrackLength{ 0 };
156 std::unique_ptr<std::map<PCB_LAYER_ID, int64_t>> LayerLengths;
157
158 int64_t TotalLength() const { return ViaLength + TrackLength + PadToDieLength; }
159};
160
161
168{
171 bool OptimiseViaLayers = false;
172
174 bool MergeTracks = false;
175
181
185 bool InferViaInPad = false;
186};
187
188
193{
194public:
196 explicit LENGTH_CALCULATION( BOARD* aBoard ) : m_board( aBoard ) {}
197
206 int64_t CalculateLength( std::vector<LENGTH_CALCULATION_ITEM>& aItems, PATH_OPTIMISATIONS aOptimisations,
207 const PAD* aStartPad = nullptr, const PAD* aEndPad = nullptr ) const;
208
218 LENGTH_DETAILS CalculateLengthDetails( std::vector<LENGTH_CALCULATION_ITEM>& aItems,
219 PATH_OPTIMISATIONS aOptimisations, const PAD* aStartPad = nullptr,
220 const PAD* aEndPad = nullptr, bool aWithLayerLengths = false ) const;
221
223 static void OptimiseTraceInPad( SHAPE_LINE_CHAIN& aLine, const PAD* aPad, PCB_LAYER_ID aPcbLayer );
224
227
228protected:
231
233 enum class MERGE_POINT
234 {
235 START,
236 END
237 };
238
244 int stackupHeight( PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer ) const;
245
252 static void optimiseTracesInPads( const std::vector<LENGTH_CALCULATION_ITEM*>& aPads,
253 const std::vector<LENGTH_CALCULATION_ITEM*>& aLines );
254
256 static void clipLineToPad( SHAPE_LINE_CHAIN& aLine, const PAD* aPad, PCB_LAYER_ID aLayer, bool aForward = true );
257
262 static void
263 optimiseViaLayers( const std::vector<LENGTH_CALCULATION_ITEM*>& aVias,
264 std::vector<LENGTH_CALCULATION_ITEM*>& aLines,
265 std::map<VECTOR2I, std::unordered_set<LENGTH_CALCULATION_ITEM*>>& aLinesPositionMap,
266 const std::map<VECTOR2I, std::unordered_set<LENGTH_CALCULATION_ITEM*>>& aPadsPositionMap );
267
271 static void mergeLines( std::vector<LENGTH_CALCULATION_ITEM*>& aLines,
272 std::map<VECTOR2I, std::unordered_set<LENGTH_CALCULATION_ITEM*>>& aLinesPositionMap );
273
279 static void mergeShapeLineChains( SHAPE_LINE_CHAIN& aPrimary, const SHAPE_LINE_CHAIN& aSecondary,
280 MERGE_POINT aMergePoint );
281
285 void inferViaInPad( const PAD* aPad, const LENGTH_CALCULATION_ITEM& aItem, LENGTH_DETAILS& aDetails ) const;
286};
287
288#endif //PCBNEW_LENGTH_CALCULATION_H
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:297
Lightweight class which holds a pad, via, or a routed trace outline.
void CalculateViaLayers(const BOARD *aBoard)
Calculates active via payers for a proxied VIA object.
PCB_LAYER_ID m_layerEnd
The end board layer for the proxied object.
MERGE_STATUS m_mergeStatus
Flags whether this item has already been merged with another.
void SetPad(PAD *aPad)
Sets the parent PAD associated with this item.
SHAPE_LINE_CHAIN & GetLine() const
Gets the SHAPE_LINE_CHAIN associated with this item.
PCB_LAYER_ID m_layerStart
The start board layer for the proxied object.
SHAPE_LINE_CHAIN m_line
A proxied SHAPE_LINE_CHAIN object. Line is empty if not proxying a SHAPE_LINE_CHAIN.
TYPE
The type of routing object this item proxies.
MERGE_STATUS GetMergeStatus() const
Gets the MERGE_STATUS of this item.
void SetLayers(const PCB_LAYER_ID aStart, const PCB_LAYER_ID aEnd=PCB_LAYER_ID::UNDEFINED_LAYER)
Sets the first and last layers associated with this item.
TYPE m_type
The routing object type of the proxied parent.
MERGE_STATUS
Whether this item is UNMERGED, it has been merged and should be used (MERGED_IN_USE),...
PAD * m_pad
A proxied PAD object. Set to nullptr if not proxying a PAD.
void SetMergeStatus(const MERGE_STATUS aStatus)
Sets the MERGE_STATUS of this item.
PCB_VIA * m_via
A proxied PVIAAD object. Set to nullptr if not proxying a VIA.
TYPE Type() const
Gets the routing item type.
void SetLine(const SHAPE_LINE_CHAIN &aLine)
Sets the source SHAPE_LINE_CHAIN of this item.
PCB_LAYER_ID GetEndLayer() const
Gets the end board layer for the proxied item.
PCB_LAYER_ID GetStartLayer() const
Gets the start board layer for the proxied item.
void SetVia(PCB_VIA *aVia)
Sets the VIA associated with this item.
PCB_VIA * GetVia() const
Gets the VIA associated with this item.
std::tuple< PCB_LAYER_ID, PCB_LAYER_ID > GetLayers() const
Gets the upper and lower layers for the proxied item.
PAD * GetPad() const
Gets the parent PAD associated with this item.
Class which calculates lengths (and associated routing statistics) in a BOARD context.
static void optimiseViaLayers(const std::vector< LENGTH_CALCULATION_ITEM * > &aVias, std::vector< LENGTH_CALCULATION_ITEM * > &aLines, std::map< VECTOR2I, std::unordered_set< LENGTH_CALCULATION_ITEM * > > &aLinesPositionMap, const std::map< VECTOR2I, std::unordered_set< LENGTH_CALCULATION_ITEM * > > &aPadsPositionMap)
Optimises the via layers.
static void optimiseTracesInPads(const std::vector< LENGTH_CALCULATION_ITEM * > &aPads, const std::vector< LENGTH_CALCULATION_ITEM * > &aLines)
Optimises the given set of items to minimise the electrical path length.
int stackupHeight(PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer) const
Returns the stackup distance between the two given layers.
static void OptimiseTraceInPad(SHAPE_LINE_CHAIN &aLine, const PAD *aPad, PCB_LAYER_ID aPcbLayer)
Optimises the given trace / line to minimise the electrical path length within the given pad.
LENGTH_CALCULATION(BOARD *aBoard)
Construct the calculator in the given BOARD context.
static void mergeLines(std::vector< LENGTH_CALCULATION_ITEM * > &aLines, std::map< VECTOR2I, std::unordered_set< LENGTH_CALCULATION_ITEM * > > &aLinesPositionMap)
Merges any lines (traces) that are contiguous, on one layer, and with no junctions.
LENGTH_DETAILS CalculateLengthDetails(std::vector< LENGTH_CALCULATION_ITEM > &aItems, PATH_OPTIMISATIONS aOptimisations, const PAD *aStartPad=nullptr, const PAD *aEndPad=nullptr, bool aWithLayerLengths=false) const
Calculates the electrical length of the given items.
BOARD * m_board
The parent board for all items.
void inferViaInPad(const PAD *aPad, const LENGTH_CALCULATION_ITEM &aItem, LENGTH_DETAILS &aDetails) const
Infers if there is a via in the given pad.
int64_t CalculateLength(std::vector< LENGTH_CALCULATION_ITEM > &aItems, PATH_OPTIMISATIONS aOptimisations, const PAD *aStartPad=nullptr, const PAD *aEndPad=nullptr) const
Calculates the electrical length of the given items.
LENGTH_CALCULATION_ITEM GetLengthCalculationItem(BOARD_CONNECTED_ITEM *aBoardItem) const
Return a LENGTH_CALCULATION_ITEM constructed from the given BOARD_CONNECTED_ITEM.
static void clipLineToPad(SHAPE_LINE_CHAIN &aLine, const PAD *aPad, PCB_LAYER_ID aLayer, bool aForward=true)
Clips the given line to the minimal direct electrical length within the pad.
MERGE_POINT
Enum to describe whether track merging is attempted from the start or end of a track segment.
static void mergeShapeLineChains(SHAPE_LINE_CHAIN &aPrimary, const SHAPE_LINE_CHAIN &aSecondary, MERGE_POINT aMergePoint)
Merges two SHAPE_LINE_CHAINs where there is a shared endpoing.
Definition: pad.h:54
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
Holds length measurement result details and statistics.
std::unique_ptr< std::map< PCB_LAYER_ID, int64_t > > LayerLengths
int64_t TotalLength() const
Struct to control which optimisations the length calculation code runs on the given path objects.
bool InferViaInPad
Determines if there is a via-in-pad present on the board but not in the item set.
bool OptimiseViaLayers
Optimise via layers for height calculations, ensuring only the distance between routed segments is co...
bool MergeTracks
Merges all contiguous (end-to-end, same layer) tracks.
bool OptimiseTracesInPads
Optimises the electrical length of tracks within pads.