[825e97]: src / plugins / automatic_router / cells.cpp Maximize Restore History

Download this file

cells.cpp    507 lines (422 with data), 15.4 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
/***************************************************************************
* Copyright (C) 2003-2006 by David Saxton <david@bluehaze.org> *
* Copyright (C) 2010 by Julian B채ume <julian@svg4all.de> *
* *
* 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 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "cells.h"
#include "utils.h"
#include <cmath>
#include <interfaces/idocumentscene.h>
#include <interfaces/component/icomponentitem.h>
#include <interfaces/component/connectoritem.h>
#include <QGraphicsSvgItem>
#include <QPainter>
#include <KDebug>
//BEGIN class Cells
Cells::Cells(KTechLab::IDocumentScene* scene, QObject* parent)
: KTechLab::IRoutingInformation(scene,parent)
{
init(scene->sceneRect().toRect());
update();
}
Cells::~Cells() {
unsigned w = unsigned(m_cellsRect.width());
for (uint i = 0; i < w; i++)
delete [] m_cells[i];
delete [] m_cells;
}
Cells::Cells(const Cells &c) {
init(c.m_sceneRect);
unsigned w = unsigned(m_cellsRect.width());
unsigned h = unsigned(m_cellsRect.height());
for (uint i = 0; i < w; i++) {
for (uint j = 0; j < h; j++) {
m_cells[i][j] = c.cell(i, j);
}
}
}
void Cells::init(const QRect &canvasRect) {
m_sceneRect = canvasRect;
m_visualizedData = QImage(m_sceneRect.size() + QSize(4,4),QImage::Format_ARGB32);
m_visualizedData.fill(QColor(Qt::transparent).rgba());
m_cellsRect = QRect(mapToCells(m_sceneRect.topLeft()), mapToCells(m_sceneRect.size()));
m_cellsRect.adjust(-1,-1,2,2);
unsigned w = unsigned(m_cellsRect.width());
unsigned h = unsigned(m_cellsRect.height());
typedef Cell *cellptr;
m_cells = new cellptr[w];
for (uint i = 0; i < w; ++i) {
m_cells[i] = new Cell[h];
}
}
void Cells::reset() {
unsigned w = unsigned(m_cellsRect.width());
unsigned h = unsigned(m_cellsRect.height());
for (uint i = 0; i < w; i++) {
for (uint j = 0; j < h; j++)
m_cells[i][j].reset();
}
}
QPointF Cells::alignToGrid(const QPointF& point)
{
QPoint result(mapToCells(point.toPoint()));
return mapFromCells(result);
}
void Cells::update()
{
QRectF updateRegion = m_documentScene->sceneRect();
foreach (QGraphicsItem* item, m_documentScene->items(updateRegion)) {
//TODO: remove this, once updateSceneRect got fixed
//work around the fact, that items get added twice, when update is
//called and we are moving items around, because they are "removed"
//when moving starts and added again after being dropped
if (m_documentScene->movingItems().contains(item)) {
kDebug() << "not adding moving item: " << item;
continue;
}
KTechLab::IComponentItem* component = 0;
KTechLab::ConnectorItem* connector = 0;
int score = Cells::ScoreNone;
if ((component = qgraphicsitem_cast<KTechLab::IComponentItem*>(item))) {
score = Cells::ScoreItem;
addCIPenalty(component, score);
} else if ((connector = qgraphicsitem_cast<KTechLab::ConnectorItem*>(item))) {
score = Cells::ScoreConnector;
addCIPenalty(connector->path(),score);
}
}
updateVisualization();
}
void Cells::updateVisualization(const QRectF &region)
{
QRect dataRegion(region.toRect());
if (region.isNull())
dataRegion = m_sceneRect;
dataRegion &= m_sceneRect;
QImage& i = m_visualizedData;
for (int y = dataRegion.y(); y < dataRegion.y()+dataRegion.height(); ++y)
for (int x = dataRegion.x(); x < dataRegion.x()+dataRegion.width(); ++x) {
QPoint poi = QPoint(x,y);
QColor c = colorForScenePoint(poi);
i.setPixel(poi-m_sceneRect.topLeft(),c.rgba());
}
}
void Cells::updateSceneRect(const QRectF& rect)
{
if (m_documentScene->isRouting())
return;
//FIXME: this doesn't scale, takes very long for larger scenes
unsigned w = unsigned(m_cellsRect.width());
for (uint i = 0; i < w; i++)
delete [] m_cells[i];
delete [] m_cells;
QRect canvasRect(rect.toRect());
if (canvasRect.isEmpty())
canvasRect = m_documentScene->sceneRect().toRect();
init(canvasRect);
update();
}
Cell& Cells::cell(int i, int j) const {
Q_ASSERT(i < m_cellsRect.x()+m_cellsRect.width());
Q_ASSERT(j < m_cellsRect.y()+m_cellsRect.height());
i -= m_cellsRect.left();
j -= m_cellsRect.top();
return m_cells[i][j];
}
bool Cells::haveCell(const int i, const int j) const {
if ((i < m_cellsRect.left()) || (i >= m_cellsRect.x()+m_cellsRect.width()))
return false;
if ((j < m_cellsRect.top()) || (j >= m_cellsRect.y()+m_cellsRect.height()))
return false;
return true;
}
bool Cells::haveCellContaing(int x, int y) const {
QPoint p(mapToCells(QPoint(x,y)));
return haveCell(p.x(), p.y());
}
Cell& Cells::cellContaining(const int x, const int y) const {
QPoint p(mapToCells(QPoint(x,y)));
return cell(p.x(), p.y());
}
QRect Cells::cellsRect() const {
return m_cellsRect;
}
QColor Cells::colorForScenePoint(QPointF p) const {
if (!haveCellContaing(p.x(),p.y()))
return QColor(Qt::transparent);
int penalty = cellContaining(p.x(),p.y()).getCIPenalty();
QColor c;
if(penalty < 0){
c.setBlue(255);
penalty *= -1;
} else {
c.setRed(255);
}
c.setAlpha(qMax(-200,qMin(penalty,200)));
return c;
}
void Cells::paintRaster(QPainter* p, const QRectF& region) const
{
//region is not aligned to the grid, so we need to adjust
int offX = (int)region.x() % 8 + 4;
int offY = (int)region.y() % 8 + 4;
for (int y = 0; y < region.height()+12; y+=8)
for (int x = 0; x < region.width()+12; x+=8)
p->drawPoint(QPoint(x-offX,y-offY));
}
void Cells::paintRoutingInfo(QPainter* p, const QRectF& target, const QRectF& source) const
{
p->drawImage(target, m_visualizedData, source);
}
void Cells::mapRoute(QPointF p1, QPointF p2)
{
p1 = mapToCells(p1).toPoint();
p2 = mapToCells(p2).toPoint();
m_route.clear();
if ( !haveCell(p1.x(), p1.y()) || !haveCell(p2.x(), p2.y()) ) {
return;
}
m_lcx = p2.x();
m_lcy = p2.y();
// First, lets try some common connector routes (which will not necesssarily
// be shortest, but they will be neat, and cut down on overall CPU usage)
// If that fails, we will resort to a shortest-route algorithm to find an
// appropriate route.
// Connector configuration: Line
if (checkLineRoute(p1.x(), p1.y(), p2.x(), p2.y(), 4 * Cells::ScoreConnector)) {
return;
} else m_route.clear();
if (checkLineRoute(p1.x(), p1.y(), p2.x(), p2.y(), 2 * Cells::ScoreConnector)) {
if (checkLineRoute(p1.x(), p1.y(), p2.x(), p2.y(), Cells::ScoreConnector - 1)) {
return;
} else m_route.clear();
} else m_route.clear();
// more basic routes??? (TODO)
// It seems we must resort to brute-force route-checking
{
reset();
// Now to map out the shortest routes to the cells
Cell *startCell = &cell(p2.x(), p2.y());
startCell->makePermanent();
startCell->resetBestScore();
startCell->setPrevXY(startCellPos, startCellPos);
m_tempLabels.clear();
checkCell(p2.x(), p2.y());
// Daniel: I changed it from a do while to a while otherwise
// in rare cases the iterator can end up as end().
while (m_tempLabels.size() > 0 && !cell(p1.x(), p1.y()).isPermanent()) {
TempLabelMap::iterator it = m_tempLabels.begin();
checkCell(it->second.x(), it->second.y());
m_tempLabels.erase(it);
}
// Now, retrace the shortest route from the endcell to get out points :)
int x = p1.x(), y = p1.y();
bool ok = true;
do {
m_route.append(mapFromCells(QPointF(x,y)));
int newx = cell(x, y).getPrevX();
int newy = cell(x, y).getPrevY();
if (newx == x && newy == y)
ok = false;
else {
x = newx;
y = newy;
}
} while (haveCell(x, y)
&& (x != startCellPos)
&& (y != startCellPos)
&& ok);
// And append the last point...
m_route.append(mapFromCells(p2));
}
removeDuplicatePoints();
}
void Cells::mapRoute(qreal sx, qreal sy, qreal ex, qreal ey)
{
mapRoute(QPointF(sx,sy),QPointF(ex,ey));
}
void Cells::checkACell(int x, int y, Cell *prev, int prevX, int prevY, int nextScore) {
// if ( !p_icnDocument->isValidCellReference(x,y) ) return;
if (!haveCell(x, y))
return;
Cell *c = &cell(x, y);
if (c->isPermanent())
return;
int newScore = nextScore + c->getCIPenalty(); // + c->Cpenalty;
// Check for changing direction
if ((x != prevX && prev->comparePrevX(prevX)) ||
(y != prevY && prev->comparePrevY(prevY))) newScore += 5;
if (c->scoreIsWorse(newScore))
return;
// We only want to change the previous cell if the score is different,
// or the score is the same but this cell allows the connector
// to travel in the same direction
if (c->sameScoreAs(newScore) &&
x != prevX &&
y != prevY) return;
c->setBestScore(newScore);
c->setPrevXY(prevX, prevY);
if (!c->getAddedToLabels()) {
c->setAddedToLabels();
QPointF point(x,y);
TempLabelMap::iterator it = m_tempLabels.insert(std::make_pair(newScore, point));
}
}
void Cells::checkCell(int x, int y) {
Cell *c = &cell(x, y);
c->makePermanent();
int nextScore = c->incBestScore();
// Check the surrounding cells (up, left, right, down)
checkACell(x - 1, y, c, x, y, nextScore);
checkACell(x + 1, y, c, x, y, nextScore);
checkACell(x, y + 1, c, x, y, nextScore);
checkACell(x, y - 1, c, x, y, nextScore);
}
bool Cells::checkLineRoute(int scx, int scy, int ecx, int ecy, int maxCIScore)
{
//no straight route possible -> return false
if ((scx != ecx) || (scy != ecy))
return false;
const bool isHorizontal = scy == ecy;
int start = 0, end = 0, x = 0, y = 0, dd = 0;
if (isHorizontal) {
dd = (scx < ecx) ? 1 : -1;
start = scx;
end = ecx + dd;
y = scy;
} else {
dd = (scy < ecy) ? 1 : -1;
start = scy;
end = ecy + dd;
x = scx;
}
if (isHorizontal) {
for (qreal x = start; x != end; x += dd) {
if (std::abs(x - start) > 1 && std::abs(x - end) > 1
&& (cell(x, y).getCIPenalty() > maxCIScore)) {
return false;
} else m_route.append(mapFromCells(QPoint(x, y)));
}
} else {
for (qreal y = start; y != end; y += dd) {
if (std::abs(y - start) > 1 && std::abs(y - end) > 1
&& (cell(x, y).getCIPenalty() > maxCIScore)) {
return false;
} else m_route.append(mapFromCells(QPointF(x, y)));
}
}
removeDuplicatePoints();
return true;
}
void Cells::removeDuplicatePoints() {
const QPointF invalid(-(1 << 30), -(1 << 30));
QList<QPointF>::const_iterator end = m_route.constEnd();
for (QList<QPointF>::iterator it = m_route.begin(); it != end; ++it) {
if (m_route.count(*it) > 1)
*it = invalid;
}
m_route.removeAll(invalid);
}
inline void Cells::addCIPenalty(const QPainterPath& path, int score)
{
for (int i=0; i < path.elementCount(); ++i) {
Q_ASSERT((path.elementAt(i).isLineTo() || i==0) && (path.elementAt(i).isMoveTo() || i!=0));
QPointF p(path.elementAt(i).x,path.elementAt(i).y);
cellContaining(p.x(),p.y()).addCIPenalty(score);
}
updateVisualization(path.boundingRect());
}
inline void Cells::addCIPenalty(const KTechLab::IComponentItem* item, int score)
{
// score < 0 -> we actually don't add, but subtract the score
int factor = score < 0 ? -1 : 1;
QPainterPath shape = item->mapToScene(item->shape());
QRect rect = shape.boundingRect().toRect();
//transform to internal raster
rect.setSize(mapToCells(rect.size())-QSize(1,1));
rect.moveTopLeft(mapToCells(rect.topLeft())+QPoint(1,1));
//blur the surroundings of an item
for (int x = rect.x()-1; x < rect.x()+rect.width()+1; ++x) {
//update above rect
int y = rect.y()-1;
cell(x,y).addCIPenalty(5*factor*Cells::ScoreConnector);
//update below rect
y += rect.height()+1;
cell(x,y).addCIPenalty(5*factor*Cells::ScoreConnector);
}
for (int y = rect.y(); y < rect.y()+rect.height(); ++y) {
//update left of rect
int x = rect.x()-1;
cell(x,y).addCIPenalty(5*factor*Cells::ScoreConnector);
//update right of rect
x += rect.width()+1;
cell(x,y).addCIPenalty(5*factor*Cells::ScoreConnector);
}
//update item-rect
for (int x = rect.x(); x < rect.x()+rect.width(); ++x)
for (int y = rect.y(); y < rect.y()+rect.height(); ++y) {
cell(x,y).addCIPenalty(score);
}
updateVisualization(item->mapRectToScene(item->boundingRect()));
}
void Cells::addComponents(QList< KTechLab::IComponentItem* > components)
{
int score = Cells::ScoreItem;
foreach(KTechLab::IComponentItem* item, components) {
addCIPenalty(item,score);
}
}
void Cells::removeComponents(QList< KTechLab::IComponentItem* > components)
{
int score = -Cells::ScoreItem;
foreach(KTechLab::IComponentItem* item, components) {
addCIPenalty(item,score);
}
}
void Cells::addConnectors(QList< KTechLab::ConnectorItem* > connectors)
{
int score = Cells::ScoreConnector;
foreach(KTechLab::ConnectorItem* connector, connectors) {
addCIPenalty(connector->path(),score);
}
}
void Cells::removeConnectors(QList< KTechLab::ConnectorItem* > connectors)
{
int score = -Cells::ScoreConnector;
foreach(const KTechLab::ConnectorItem* c,connectors) {
addCIPenalty(c->path(),score);
}
}
void Cells::removeGraphicsItem(QGraphicsItem* item)
{
KTechLab::IComponentItem* component = 0;
KTechLab::ConnectorItem* connector = 0;
int score = Cells::ScoreNone;
if ((component = qgraphicsitem_cast<KTechLab::IComponentItem*>(item))) {
score = -Cells::ScoreItem;
addCIPenalty(component, score);
}
if ((connector = qgraphicsitem_cast<KTechLab::ConnectorItem*>(item))) {
score = -Cells::ScoreConnector;
addCIPenalty(connector->path(),score);
}
}
//END class Cells
//BEGIN class Cell
Cell::Cell() {
reset();
CIpenalty = 0;
numCon = 0;
}
void Cell::reset() {
addedToLabels = false;
permanent = false;
resetBestScore();
}
//END class Cell
#include "cells.moc"
// kate: indent-mode cstyle; space-indent on; indent-width 0;