[6dd4ed]: src / plugins / automatic_router / cells.h  Maximize  Restore  History

Download this file

245 lines (205 with data), 6.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
/***************************************************************************
* 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. *
***************************************************************************/
#ifndef CELLS_H
#define CELLS_H
#include <cassert>
#include <QRect>
#include <QPixmap>
#include "utils.h"
#include <QColor>
#include <interfaces/iroutinginformation.h>
class QGraphicsItem;
namespace KTechLab{
class IDocumentScene;
class ConnectorItem;
class IComponentItem;
}
/**
* @short Used for mapping out connections
* In this implementation a \class Cell represents 8x8 pixels in the scene to
* reduce complexity. An instance of this class stores the costs needed to
* route through this area of the scene.
*/
const short startCellPos = -(1 << 14);
class Cell {
public:
Cell();
/**
* Resets bestScore, prevX, prevY, addedToLabels, it, permanent for
* the cell.
*/
void reset();
short getNumCon() const {
return numCon;
}
void addConnectors(const short connectors) {
numCon += connectors;
}
short getCIPenalty() const {
return CIpenalty;
}
void addCIPenalty(const short x) {
CIpenalty += x;
}
short incBestScore() {
return ++bestScore;
};
bool scoreIsWorse(const short score) const {
return score > bestScore;
}
bool sameScoreAs(const short score) const {
return score == bestScore;
}
void resetBestScore() {
bestScore = 0xFFFF;
}
void setBestScore(const short aScore) {
bestScore = aScore;
}
short getPrevX() const {
return prevX;
}
short getPrevY() const {
return prevY;
}
bool comparePrevX(const short x) const {
return prevX == x;
}
bool comparePrevY(const short y) const {
return prevY == y;
}
void setPrevXY(const short x, const short y) {
prevX = x;
prevY = y;
}
void setAddedToLabels() {
addedToLabels = true;
}
bool getAddedToLabels() const {
return addedToLabels;
}
bool isPermanent() const {
return permanent;
}
void makePermanent() {
permanent = true;
}
private:
/**
* Best (lowest) score so far, _the_ best if it is permanent.
*/
unsigned short bestScore;
/**
* 'Penalty' of using the cell from CNItem.
*/
unsigned short CIpenalty;
/**
* Whether the score can be improved on.
*/
bool permanent;
/**
* Whether the cell has already been added to the list of cells to
* check.
*/
bool addedToLabels;
/**
* Which cell this came from, (startCellPos,startCellPos) if originating
* cell.
*/
short prevX, prevY;
/**
* Number of connectors through that point.
*/
unsigned short numCon;
};
#include <map>
// Key = cell, data = previous cell, compare = score
typedef std::multimap< unsigned short, QPointF > TempLabelMap;
/**
* This class represents a grid (2 dimensional array) of \class Cell instances.
* In this implementation a \class Cell represents 8x8 pixels in the scene to
* reduce complexity.
* @author David Saxton
*/
class Cells : public KTechLab::IRoutingInformation {
Q_OBJECT
public:
Cells(KTechLab::IDocumentScene* scene, QObject* parent = 0);
~Cells();
/**
* Scores for different type of items on the scene
*/
enum {
ScoreNone = 0,
ScoreConnector = 4,
ScoreItem = 1000
};
virtual QPointF alignToGrid(const QPointF& point);
virtual void mapRoute(QPointF p1, QPointF p2);
virtual void mapRoute(qreal sx, qreal sy, qreal ex, qreal ey);
/**
* Resets bestScore, prevX, prevY, addedToLabels, it, permanent for each cell
*/
void reset();
QRect cellsRect() const ;
/**
* Returns the cell containing the given position on the canvas.
*/
Cell &cellContaining(const int x, const int y) const ;
/**
* @return if the given cell exists.
*/
bool haveCell(const int i, const int j) const ;
/**
* @return if there is a cell containg the given canvas point.
*/
bool haveCellContaing(int x, int y) const ;
Cell &cell(int i, int j) const ;
QColor colorForScenePoint( QPointF p ) const ;
void updateVisualization(const QRectF& region = QRectF());
public slots:
void updateSceneRect(const QRectF &rect = QRectF());
void addComponents(QList< KTechLab::IComponentItem* > components);
void removeComponents(QList< KTechLab::IComponentItem* > components);
void addConnectors(QList< KTechLab::ConnectorItem* > connectors);
void removeConnectors(QList< KTechLab::ConnectorItem* > connectors);
void removeGraphicsItem(QGraphicsItem* item);
protected:
virtual void paintRaster(QPainter* p, const QRectF& region) const;
virtual void paintRoutingInfo(QPainter* p, const QRectF& target, const QRectF& source) const;
private:
/**
* Update the internal representation
*/
void update();
void init(const QRect &canvasRect);
QRect m_cellsRect;
Cell **m_cells;
QImage m_visualizedData;
QRect m_sceneRect;
Cells(const Cells &);
// Cells & operator= (const Cells &);
/**
* Check a line of the ICNDocument cells for a valid route
*/
bool checkLineRoute(int scx, int scy, int ecx, int ecy, int maxCIScore);
/**
* Remove duplicated points from the route
*/
void removeDuplicatePoints();
void checkACell(int x, int y, Cell *prev, int prevX, int prevY, int nextScore);
void checkCell(int x, int y); // Gets the shortest route from the final cell
inline void addCIPenalty(const QPainterPath& path, int score);
inline void addCIPenalty(const KTechLab::IComponentItem* item, int score);
TempLabelMap m_tempLabels;
qreal m_lcx;
qreal m_lcy;
};
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks