1 /*
2 * KiRouter - a push-and-(sometimes-)shove PCB router
3 *
4 * Copyright (C) 2013-2015 CERN
5 * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "pns_router.h"
23 #include "pns_meander.h"
24 #include "pns_meander_placer_base.h"
25
26 namespace PNS {
27
MEANDER_PLACER_BASE(ROUTER * aRouter)28 MEANDER_PLACER_BASE::MEANDER_PLACER_BASE( ROUTER* aRouter ) :
29 PLACEMENT_ALGO( aRouter )
30 {
31 m_currentWidth = 0;
32 }
33
34
~MEANDER_PLACER_BASE()35 MEANDER_PLACER_BASE::~MEANDER_PLACER_BASE()
36 {
37 }
38
39
AmplitudeStep(int aSign)40 void MEANDER_PLACER_BASE::AmplitudeStep( int aSign )
41 {
42 int a = m_settings.m_maxAmplitude + aSign * m_settings.m_step;
43 a = std::max( a, m_settings.m_minAmplitude );
44
45 m_settings.m_maxAmplitude = a;
46 }
47
48
SpacingStep(int aSign)49 void MEANDER_PLACER_BASE::SpacingStep( int aSign )
50 {
51 int s = m_settings.m_spacing + aSign * m_settings.m_step;
52 s = std::max( s, 2 * m_currentWidth );
53
54 m_settings.m_spacing = s;
55 }
56
57
UpdateSettings(const MEANDER_SETTINGS & aSettings)58 void MEANDER_PLACER_BASE::UpdateSettings( const MEANDER_SETTINGS& aSettings )
59 {
60 m_settings = aSettings;
61 }
62
63
cutTunedLine(const SHAPE_LINE_CHAIN & aOrigin,const VECTOR2I & aTuneStart,const VECTOR2I & aCursorPos,SHAPE_LINE_CHAIN & aPre,SHAPE_LINE_CHAIN & aTuned,SHAPE_LINE_CHAIN & aPost)64 void MEANDER_PLACER_BASE::cutTunedLine( const SHAPE_LINE_CHAIN& aOrigin,
65 const VECTOR2I& aTuneStart,
66 const VECTOR2I& aCursorPos,
67 SHAPE_LINE_CHAIN& aPre,
68 SHAPE_LINE_CHAIN& aTuned,
69 SHAPE_LINE_CHAIN& aPost )
70 {
71 VECTOR2I cp ( aCursorPos );
72
73 if ( cp == aTuneStart ) // we don't like tuning segments with 0 length
74 {
75 int idx = aOrigin.FindSegment( cp );
76 if( idx >= 0 )
77 {
78 const SEG& s = aOrigin.CSegment( idx );
79 cp += (s.B - s.A).Resize(2);
80 } else
81 cp += VECTOR2I (2, 5); // some arbitrary value that is not 45 degrees oriented
82 }
83
84 VECTOR2I n = aOrigin.NearestPoint( cp );
85 VECTOR2I m = aOrigin.NearestPoint( aTuneStart );
86
87 SHAPE_LINE_CHAIN l( aOrigin );
88 l.Split( n );
89 l.Split( m );
90
91 int i_start = l.Find( m );
92 int i_end = l.Find( n );
93
94 if( i_start > i_end )
95 {
96 l = l.Reverse();
97 i_start = l.Find( m );
98 i_end = l.Find( n );
99 }
100
101 aPre = l.Slice( 0, i_start );
102 aPost = l.Slice( i_end, -1 );
103 aTuned = l.Slice( i_start, i_end );
104
105 aTuned.Simplify();
106 }
107
108
tuneLineLength(MEANDERED_LINE & aTuned,int aElongation)109 void MEANDER_PLACER_BASE::tuneLineLength( MEANDERED_LINE& aTuned, int aElongation )
110 {
111 int remaining = aElongation;
112 bool finished = false;
113
114 for( MEANDER_SHAPE* m : aTuned.Meanders() )
115 {
116 if( m->Type() != MT_CORNER )
117 {
118 if( remaining >= 0 )
119 remaining -= m->MaxTunableLength() - m->BaselineLength();
120
121 if( remaining < 0 )
122 {
123 if( !finished )
124 {
125 MEANDER_TYPE newType;
126
127 if( m->Type() == MT_START || m->Type() == MT_SINGLE )
128 newType = MT_SINGLE;
129 else
130 newType = MT_FINISH;
131
132 m->SetType( newType );
133 m->Recalculate();
134
135 finished = true;
136 } else {
137 m->MakeEmpty();
138 }
139 }
140 }
141 }
142
143 remaining = aElongation;
144 int meanderCount = 0;
145
146 for(MEANDER_SHAPE* m : aTuned.Meanders())
147 {
148 if( m->Type() != MT_CORNER && m->Type() != MT_EMPTY )
149 {
150 if(remaining >= 0)
151 {
152 remaining -= m->MaxTunableLength() - m->BaselineLength();
153 meanderCount ++;
154 }
155 }
156 }
157
158 int balance = 0;
159
160 if( meanderCount )
161 balance = -remaining / meanderCount;
162
163 if( balance >= 0 )
164 {
165 for( MEANDER_SHAPE* m : aTuned.Meanders() )
166 {
167 if( m->Type() != MT_CORNER && m->Type() != MT_EMPTY )
168 {
169 m->Resize( std::max( m->Amplitude() - balance / 2, m_settings.m_minAmplitude ) );
170 }
171 }
172 }
173 }
174
175
MeanderSettings() const176 const MEANDER_SETTINGS& MEANDER_PLACER_BASE::MeanderSettings() const
177 {
178 return m_settings;
179 }
180
181
compareWithTolerance(int aValue,int aExpected,int aTolerance) const182 int MEANDER_PLACER_BASE::compareWithTolerance( int aValue, int aExpected, int aTolerance ) const
183 {
184 if( aValue < aExpected - aTolerance )
185 return -1;
186 else if( aValue > aExpected + aTolerance )
187 return 1;
188 else
189 return 0;
190 }
191
192 }
193