1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2015 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 
23 #ifndef __PNS_MEANDER_PLACER_BASE_H
24 #define __PNS_MEANDER_PLACER_BASE_H
25 
26 #include <math/vector2d.h>
27 
28 #include <geometry/shape_line_chain.h>
29 
30 #include "pns_node.h"
31 #include "pns_line.h"
32 #include "pns_placement_algo.h"
33 #include "pns_meander.h"
34 
35 namespace PNS {
36 
37 class ROUTER;
38 class SHOVE;
39 class OPTIMIZER;
40 
41 /**
42  * Base class for Single trace & Differential pair meandering tools, as both of them share a
43  * lot of code.
44  */
45 class MEANDER_PLACER_BASE : public PLACEMENT_ALGO
46 {
47 public:
48     ///< Result of the length tuning operation
49     enum TUNING_STATUS {
50         TOO_SHORT = 0,
51         TOO_LONG,
52         TUNED
53     };
54 
55     MEANDER_PLACER_BASE( ROUTER* aRouter );
56     virtual ~MEANDER_PLACER_BASE();
57 
58     /**
59      * Return a string describing the status and length of the tuned traces.
60      */
61     virtual const wxString TuningInfo( EDA_UNITS aUnits ) const = 0;
62 
63     /**
64      * Return the tuning status (too short, too long, etc.) of the trace(s) being tuned.
65      */
66     virtual TUNING_STATUS TuningStatus() const = 0;
67 
68     /**
69      * Increase/decreases the current meandering amplitude by one step.
70      *
71      * @param aSign direction (negative = decrease, positive = increase).
72      */
73     virtual void AmplitudeStep( int aSign );
74 
75     /**
76      * Increase/decrease the current meandering spacing by one step.
77      *
78      * @param aSign direction (negative = decrease, positive = increase).
79      */
80     virtual void SpacingStep( int aSign );
81 
82     /**
83      * Return the clearance of the track(s) being length tuned
84      *
85      * @return clearance value in internal units
86      */
87     virtual int Clearance();
88 
89     /**
90      * Return the current meandering configuration.
91      *
92      * @return the settings
93      */
94     virtual const MEANDER_SETTINGS& MeanderSettings() const;
95 
96     /*
97      * Set the current meandering configuration.
98      *
99      * @param aSettings the settings.
100      */
101     virtual void UpdateSettings( const MEANDER_SETTINGS& aSettings);
102 
103     /**
104      * Checks if it's OK to place the shape aShape (i.e. if it doesn't cause DRC violations
105      * or collide with other meanders).
106      *
107      * @param aShape the shape to check.
108      * @return true if the shape fits.
109      */
CheckFit(MEANDER_SHAPE * aShape)110     virtual bool CheckFit( MEANDER_SHAPE* aShape )
111     {
112         return false;
113     }
114 
115     int GetTotalPadToDieLength( const LINE& aLine ) const;
116 
117 protected:
118     /**
119      * Extract the part of a track to be meandered, depending on the starting point and the
120      * cursor position.
121      *
122      * @param aOrigin the original line.
123      * @param aTuneStart point where we start meandering (start click coordinates).
124      * @param aCursorPos current cursor position.
125      * @param aPre part before the beginning of meanders.
126      * @param aTuned part to be meandered.
127      * @param aPost part after the end of meanders.
128      */
129     void cutTunedLine( const SHAPE_LINE_CHAIN& aOrigin, const VECTOR2I& aTuneStart,
130                        const VECTOR2I& aCursorPos, SHAPE_LINE_CHAIN& aPre, SHAPE_LINE_CHAIN& aTuned,
131                        SHAPE_LINE_CHAIN& aPost );
132 
133     /**
134      * Take a set of meanders in \a aTuned and tunes their length to extend the original line
135      * length by \a aElongation.
136      */
137     void tuneLineLength( MEANDERED_LINE& aTuned, long long int aElongation );
138 
139     /**
140      * Compare \a aValue against \a aExpected with given tolerance.
141      */
142     int compareWithTolerance( long long int aValue, long long int aExpected,
143                               long long int aTolerance = 0 ) const;
144 
145     VECTOR2I getSnappedStartPoint( LINKED_ITEM* aStartItem, VECTOR2I aStartPoint );
146 
147     /**
148      * Calculate the total length of the line represented by an item set (tracks and vias)
149      * @param aLine
150      * @return
151      */
152     long long int lineLength( const ITEM_SET& aLine ) const;
153 
154     ///< Pointer to world to search colliding items.
155     NODE* m_world;
156 
157     ///< Total length added by pad to die size.
158     int m_padToDieLength;
159 
160     ///< Width of the meandered trace(s).
161     int m_currentWidth;
162 
163     ///< Meander settings.
164     MEANDER_SETTINGS m_settings;
165 
166     ///< The current end point.
167     VECTOR2I m_currentEnd;
168 };
169 
170 }
171 
172 #endif    // __PNS_MEANDER_PLACER_BASE_H
173