Menu

[r168]: / plugins / simplerouter / simplerouter.cpp  Maximize  Restore  History

Download this file

557 lines (518 with data), 14.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*******************************************************************************
* Copyright (C) 2014 Pike Aerospace Research Corporation *
* Author: Mike Sharkey <mike@pikeaero.com> *
* *
* 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. *
* *
* 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 at <http://www.gnu.org/licenses/> *
* for more details. *
* *
*******************************************************************************/
#include "simplerouter.h"
#include <cspecctraobject.h>
#include <cpcb.h>
#include <cpcbnetwork.h>
#include <cpcbnet.h>
#include <cpcbstructure.h>
#include <cpcbboundary.h>
#include <cpcbpath.h>
#include <cutil.h>
#include <cgsegment.h>
#include <cgwire.h>
#include <cgpadstack.h>
#include <cpcbplacement.h>
#include <cpcbplace.h>
#include <cpcblayer.h>
#include <QPolygonF>
#include <QVector>
#include <QEventLoop>
/**
* @return plugin type
*/
CPluginInterface::tPluginType SimpleRouter::type()
{
return CPluginInterface::RouterPlugin;
}
/**
* @return A title for the plugin to display in user interface.
*/
QString SimpleRouter::title() const
{
return "Simple Router";
}
/**
* @return A version for the plugin to display in user interface.
*/
QString SimpleRouter::version() const
{
return "0.2";
}
/**
* @return A author for the plugin to display in user interface.
*/
QStringList SimpleRouter::credits() const
{
QStringList rc;
rc << "Mike Sharkey <mike@pikeaero.com>";
return rc;
}
/**
* @return A website for the plugin to display in user interface.
*/
QString SimpleRouter::website() const
{
return "http://qautorouter.sourceforge.net";
}
/**
* @return A short description of the plugin to display in user interface.
*/
QString SimpleRouter::description() const
{
return "A Simple Auto Router Skeleton";
}
/**
* @brief The license text
*/
QStringList SimpleRouter::license() const
{
QStringList rc;
rc << "GPL Version 2.0";
rc << "This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published\n"
"by the Free Software Foundation; either version 2 of the License,\n"
"or (at your option) any later version. \n"
"\n"
"This program is distributed in the hope that it will be useful, \n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \n"
"GNU General Public License for more details. \n"
"\n"
"You should have received a copy of the GNU General Public License \n"
"along with this program; if not, write to the \n"
"\n"
"Free Software Foundation, Inc.,\n"
"59 Temple Place - Suite 330,\nBoston, MA 02111-1307,\nU.S.A.\n";
return rc;
}
/**
* @return elapsed time to string
*/
QString SimpleRouter::elapsed()
{
return CUtil::elapsed(mStartTime.secsTo(QDateTime::currentDateTime()));
}
/**
* @return current state
*/
SimpleRouter::tRunState SimpleRouter::state()
{
return mState;
}
/**
* @brief set surrent run state
*/
void SimpleRouter::setState(tRunState state)
{
mState=state;
}
/**
* @return A status message for the status line
*/
QString SimpleRouter::currentStatus()
{
QString msg="[";
if ( pcb() != NULL && pcb()->network() != NULL )
{
switch(state())
{
default:
case Idle: msg+="Idle "; break;
case Selecting: msg+="Select"; break;
case Routing: msg+="Route "; break;
}
msg += QString("] ")+elapsed()+tr(" Nets: ")+QString::number(pcb()->network()->nets()) + " " + tr("Routed: ")+QString::number(pcb()->network()->routed());
}
return msg;
}
/**
* @brief perform initialization
*/
bool SimpleRouter::start(CPcb* pcb)
{
mEndPoint[0]=mEndPoint[1]=NULL;
mRatLine=NULL;
mNet=NULL;
mPcb = pcb;
setState(Idle);
mStartTime = QDateTime::currentDateTime();
if ( mPcb != NULL && mPcb->network() != NULL )
{
setState(Selecting);
QObject::connect(this,SIGNAL(status(QString)),mPcb,SIGNAL(status(QString)));
QObject::connect(this,SIGNAL(clearCache()),mPcb,SLOT(clearCache()));
return true;
}
return false;
}
/**
* @brief stop processing
*/
void SimpleRouter::stop()
{
setState(Idle);
QObject::disconnect(this,SIGNAL(status(QString)),mPcb,SIGNAL(status(QString)));
QObject::disconnect(this,SIGNAL(clearCache()),mPcb,SLOT(clearCache()));
}
/// draw a single rat line
QGraphicsPathItem* SimpleRouter::drawRatLine()
{
QEventLoop loop;
mRatLine=NULL;
if ( mNet && mEndPoint[0] && mEndPoint[1] )
{
QPainterPath painterPath;
QPointF pointA = mEndPoint[0]->origin();
QPointF pointB = mEndPoint[1]->origin();
painterPath.moveTo( pointA );
painterPath.lineTo( pointB );
mRatLine = CSpecctraObject::globalScene()->addPath(painterPath);
loop.processEvents();
}
return mRatLine;
}
/// draw a single rat line
QGraphicsPathItem* SimpleRouter::drawRatLine(QList<SimpleRouterNode> path)
{
QEventLoop loop;
QPainterPath painterPath;
if ( mRatLine )
{
delete mRatLine;
mRatLine=NULL;
}
for(int n=0; n < path.count()-1; n++)
{
SimpleRouterNode nodeA = path.at(n);
SimpleRouterNode nodeB = path.at(n+1);
painterPath.moveTo( nodeA.origin() );
painterPath.lineTo( nodeB.origin() );
CSpecctraObject::globalScene()->addPath(painterPath);
loop.processEvents();
}
}
/**
* @brief SimpleRouter::selectNet select the next net to route.
* @return
*/
CPcbNet* SimpleRouter::selectNet()
{
CPcbNet* rc=NULL;
CPcbNetwork* network = pcb()->network();
for(int n=0; !rc && n < network->nets(); n++)
{
CPcbNet* net = network->net(n);
if ( !net->routed() )
{
rc = net;
}
}
return rc;
}
/**
* @brief Collect two endpoints.
* @param points[] Array to contain the two end points as CPadStack pointers.
* @return true on success.
*/
bool SimpleRouter::endPoints()
{
bool rc=false;
if ( !mNet->routed() )
{
for( int x=0; !rc && x < mNet->padstacks(); x++)
{
mEndPoint[0] = mNet->padstack(x);
if ( !mEndPoint[0]->routed() )
{
for( int y=0; !rc && y < mNet->padstacks(); y++)
{
mEndPoint[1] = mNet->padstack(y);
if ( mEndPoint[1] != mEndPoint[0] &&
( (mEndPoint[1]->layer() != mEndPoint[0]->layer()) ||
(mEndPoint[1]->layer() == mEndPoint[0]->layer() &&
mEndPoint[1]->origin() != mEndPoint[0]->origin()) ) &&
!mEndPoint[1]->routed() )
{
rc = true;
}
}
}
}
}
return rc;
}
/**
* @brief route this net
*/
void SimpleRouter::route()
{
if ( !mNet->routed() )
{
mNet->sort();
if ( endPoints() )
{
drawRatLine();
path();
drawRatLine(mPath);
if ( mRatLine )
{
delete mRatLine;
mRatLine=NULL;
}
mEndPoint[0]=mEndPoint[1]=NULL;
}
}
}
/**
* @brief route a segment of this net.
*/
QList<SimpleRouterNode>& SimpleRouter::path()
{
QEventLoop loop;
int idx;
SimpleRouterNode* node = new SimpleRouterNode(mEndPoint[0]->origin(), 0 /* mEndPoint[0]->layer()->index() */ ); /* FIXME */
mPath.clear();
clear(mOpenList);
clear(mClosedList);
insort(mOpenList,node);
while(!mOpenList.isEmpty() && mOpenList.at(0)->origin() != mEndPoint[1]->origin() )
{
node = mOpenList.takeFirst();
loop.processEvents();
QList<SimpleRouterNode*> children = neighbours(node);
for(int n=0; n < children.count(); n++)
{
SimpleRouterNode* child = children[n];
int oIdx = mOpenList.indexOf(child); // child node already in the open list?
if ( oIdx >=0 )
{
SimpleRouterNode other = mOpenList[oIdx]; // the existing node is better?
if ( other <= child )
continue;
}
int cIdx = mClosedList.indexOf(child); // child node already in the closed list?
if ( cIdx >= 0 )
{
SimpleRouterNode other = mClosedList[cIdx]; // the existing node is better?
if ( other <= child )
continue;
}
if ( oIdx >= 0 ) // remove child from open and closed lists
{
delete mOpenList.takeAt(oIdx);
}
if ( cIdx >= 0 )
{
delete mClosedList.takeAt(cIdx);
}
open(child); // add child to open list
}
close(node);
}
do
{
mPath.append(*node);
if ( ( idx = mClosedList.indexOf( other ) ) >= 0 )
other = mClosedList.takeAt(idx);
else
if ( ( idx = mOpenList.indexOf( other ) ) >= 0 )
other = mOpenList.takeAt(idx);
else
break;
node = node->parent();
} while( node->parent() != NULL );
// FIXME FIXME was: follow parent nodes from goal back to start
do
{
SimpleRouterNode other(node->parent());
mPath.append(node);
if ( ( idx = mClosedList.indexOf( other ) ) >= 0 )
other = mClosedList.takeAt(idx);
else
if ( ( idx = mOpenList.indexOf( other ) ) >= 0 )
other = mOpenList.takeAt(idx);
else
break;
node = other;
} while( node.parent() != NULL );
return mPath;
}
/**
* @brief Retrieve a list of neighbours of the node that do not collide with uncollidable objects.
* @param node The nod ein question.
* @return A list of the potential move directions.
*/
QList<SimpleRouterNode*> SimpleRouter::neighbours(SimpleRouterNode* node)
{
QList<SimpleRouterNode> rc;
for(int x=-1; x<=1; x++)
{
for(int y=-1; y<=1; y++)
{
if ( x!=0 && y!=0 )
{
SimpleRouterNode child(QPointF(node.origin().x()+x,node.origin().y()+y));
child.setParent(&node);
child.setScore( cost( child ) );
rc.append(child);
}
}
}
return rc;
}
/**
* @brief Retrieve a list of neighbours of the node that do not collide with uncollidable objects.
* @param node The nod ein question.
* @return A list of the potential move directions.
*/
QList<SimpleRouterNode> SimpleRouter::neighbours(SimpleRouter node)
{
QList<SimpleRouterNode> rc;
/** TODO - write code here */
return rc;
}
/**
* @brief insort a node into a list in sorted by cost method.
* @param list The list to which to insort.
* @param node The insorted node.
*/
void SimpleRouter::insort(QList<SimpleRouterNode*>& list, SimpleRouterNode* node)
{
int idx = list.indexOf(node);
if ( idx < 0 ) // insorted already?
{
for(int n=0; n < list.length(); n++)
{
SimpleRouterNode other = list[n];
if ( node < other )
{
list.insert(n,node);
return;
}
}
list.append(node);
}
}
/**
* @brief Make a copy of the node into the open list
* @param node The source node to copy from
*/
void SimpleRouter::open(SimpleRouterNode* node)
{
insort(mOpenList,node);
}
/**
* @brief Make a copy of the node into the open list
* @param node The source node to copy from
*/
void SimpleRouter::close(SimpleRouterNode* node)
{
insort(mClosedList,node);
}
/**
* @brief Clear a list of node points. Deleting each node.
* @param nodes A list of node pointers.
*/
void SimpleRouter::clear(QList<SimpleRouterNode*>& nodes)
{
while(nodes.count())
{
SimpleRouterNode* node = nodes.takeLast();
delete node;
}
}
/**
* @brief Calculate the sum of the absolute values of x() and y(), traditionally known as the "Manhattan length"
* of the vector from the point A to point B.
*/
double SimpleRouter::manhattanLength(QPointF a, QPointF b)
{
QPointF delta = (b - a);
return delta.manhattanLength();
}
/**
* @brief Calculate the cost between two adjacent points.
* We will assign a cost of 10 to each horizontal or vertical square moved, and a cost of 14.14 for a diagonal move.
* We use these numbers because the actual distance to move diagonally is the square root of 2,
* or roughly 1.414 times the cost of moving horizontally or vertically.
*/
double SimpleRouter::adjacentCost(QPointF a, QPointF b)
{
double diffX = fabs(a.x()-b.x());
double diffY = fabs(a.y()-b.y());
double rc = ( diffX && diffY ) ? 14.14 : 10.0;
return rc;
}
/**
* @brief Calculate the cost of this node
* @param node
* @param parent
*/
double SimpleRouter::cost(SimpleRouterNode node)
{
double rc = g(node) + h(node);
return rc;
}
/**
* @brief Calculate G, the movement cost to move from the starting point A
* to this cell of the grid, following the path generated to get there.
*/
double SimpleRouter::g(SimpleRouterNode node)
{
double rc=0.0;
SimpleRouterNode* parent = node.parent();
while( parent != NULL && parent != parent->parent() )
{
rc += adjacentCost( parent->origin(), node.origin() );
parent = parent->parent();
}
return rc;
}
/**
* @brief The estimated movement cost to move from that given square on the grid to the final destination
*/
double SimpleRouter::h(SimpleRouterNode node)
{
double rc = manhattanLength( node.origin(), mEndPoint[1]->origin() ) * 10.0;
return rc;
}
/**
* @brief Run for a little bit.
*/
bool SimpleRouter::exec()
{
bool rc=true;
emit status(currentStatus());
switch(state())
{
default:
setState(Selecting);
break;
case Idle:
rc=false;
break;
case Selecting:
setState( (mNet = selectNet()) ? Routing : Idle );
break;
case Routing:
route();
setState(Selecting);
break;
}
return rc;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.