0
2
0
... | ... |
@@ -155,64 +155,65 @@ |
155 | 155 |
return Arc((u._id << 1) | 1); |
156 | 156 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) { |
157 | 157 |
return Arc(((u._id / _width * (_width - 1) + |
158 | 158 |
u._id % _width + _edge_limit) << 1) | 1); |
159 | 159 |
} |
160 | 160 |
} else { |
161 | 161 |
if (u._id - v._id == _width) |
162 | 162 |
return Arc(v._id << 1); |
163 | 163 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) { |
164 | 164 |
return Arc((v._id / _width * (_width - 1) + |
165 | 165 |
v._id % _width + _edge_limit) << 1); |
166 | 166 |
} |
167 | 167 |
} |
168 | 168 |
return INVALID; |
169 | 169 |
} |
170 | 170 |
|
171 | 171 |
class Node { |
172 | 172 |
friend class GridGraphBase; |
173 | 173 |
|
174 | 174 |
protected: |
175 | 175 |
int _id; |
176 | 176 |
Node(int id) : _id(id) {} |
177 | 177 |
public: |
178 | 178 |
Node() {} |
179 | 179 |
Node (Invalid) : _id(-1) {} |
180 | 180 |
bool operator==(const Node node) const {return _id == node._id;} |
181 | 181 |
bool operator!=(const Node node) const {return _id != node._id;} |
182 | 182 |
bool operator<(const Node node) const {return _id < node._id;} |
183 | 183 |
}; |
184 | 184 |
|
185 | 185 |
class Edge { |
186 | 186 |
friend class GridGraphBase; |
187 |
friend class Arc; |
|
187 | 188 |
|
188 | 189 |
protected: |
189 | 190 |
int _id; |
190 | 191 |
|
191 | 192 |
Edge(int id) : _id(id) {} |
192 | 193 |
|
193 | 194 |
public: |
194 | 195 |
Edge() {} |
195 | 196 |
Edge (Invalid) : _id(-1) {} |
196 | 197 |
bool operator==(const Edge edge) const {return _id == edge._id;} |
197 | 198 |
bool operator!=(const Edge edge) const {return _id != edge._id;} |
198 | 199 |
bool operator<(const Edge edge) const {return _id < edge._id;} |
199 | 200 |
}; |
200 | 201 |
|
201 | 202 |
class Arc { |
202 | 203 |
friend class GridGraphBase; |
203 | 204 |
|
204 | 205 |
protected: |
205 | 206 |
int _id; |
206 | 207 |
|
207 | 208 |
Arc(int id) : _id(id) {} |
208 | 209 |
|
209 | 210 |
public: |
210 | 211 |
Arc() {} |
211 | 212 |
Arc (Invalid) : _id(-1) {} |
212 | 213 |
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; } |
213 | 214 |
bool operator==(const Arc arc) const {return _id == arc._id;} |
214 | 215 |
bool operator!=(const Arc arc) const {return _id != arc._id;} |
215 | 216 |
bool operator<(const Arc arc) const {return _id < arc._id;} |
216 | 217 |
}; |
217 | 218 |
|
218 | 219 |
static bool direction(Arc arc) { |
... | ... |
@@ -443,89 +444,89 @@ |
443 | 444 |
return Arc((n._id << 1) | 1); |
444 | 445 |
} else { |
445 | 446 |
return INVALID; |
446 | 447 |
} |
447 | 448 |
} |
448 | 449 |
|
449 | 450 |
Arc down(Node n) const { |
450 | 451 |
if (n._id >= _width) { |
451 | 452 |
return Arc((n._id - _width) << 1); |
452 | 453 |
} else { |
453 | 454 |
return INVALID; |
454 | 455 |
} |
455 | 456 |
} |
456 | 457 |
|
457 | 458 |
private: |
458 | 459 |
int _width, _height; |
459 | 460 |
int _node_num, _edge_num; |
460 | 461 |
int _edge_limit; |
461 | 462 |
}; |
462 | 463 |
|
463 | 464 |
|
464 | 465 |
typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase; |
465 | 466 |
|
466 | 467 |
/// \ingroup graphs |
467 | 468 |
/// |
468 | 469 |
/// \brief Grid graph class |
469 | 470 |
/// |
470 | 471 |
/// This class implements a special graph type. The nodes of the |
471 | 472 |
/// graph can be indexed by two integer \c (i,j) value where \c i is |
472 | 473 |
/// in the \c [0..width()-1] range and j is in the \c |
473 | 474 |
/// [0..height()-1] range. Two nodes are connected in the graph if |
474 | 475 |
/// the indexes differ exactly on one position and exactly one is |
475 |
/// the difference. The nodes of the graph be indexed by position |
|
476 |
/// with \c operator()() function. The positions of the nodes can be |
|
476 |
/// the difference. The nodes of the graph can be indexed by position |
|
477 |
/// with the \c operator()() function. The positions of the nodes can be |
|
477 | 478 |
/// get with \c pos(), \c col() and \c row() members. The outgoing |
478 | 479 |
/// arcs can be retrieved with the \c right(), \c up(), \c left() |
479 | 480 |
/// and \c down() functions, where the bottom-left corner is the |
480 | 481 |
/// origin. |
481 | 482 |
/// |
482 | 483 |
/// \image html grid_graph.png |
483 |
/// \image latex grid_graph.eps "Grid |
|
484 |
/// \image latex grid_graph.eps "Grid graph" row_num=\textrow_num |
|
484 | 485 |
/// |
485 | 486 |
/// A short example about the basic usage: |
486 | 487 |
///\code |
487 | 488 |
/// GridGraph graph(rows, cols); |
488 | 489 |
/// GridGraph::NodeMap<int> val(graph); |
489 | 490 |
/// for (int i = 0; i < graph.width(); ++i) { |
490 | 491 |
/// for (int j = 0; j < graph.height(); ++j) { |
491 | 492 |
/// val[graph(i, j)] = i + j; |
492 | 493 |
/// } |
493 | 494 |
/// } |
494 | 495 |
///\endcode |
495 | 496 |
/// |
496 |
/// |
|
497 |
/// This graph type is fully conform to the \ref concepts::Graph |
|
497 | 498 |
/// "Graph" concept, and it also has an important extra feature |
498 |
/// that its maps are real \ref concepts::ReferenceMap "reference |
|
499 |
/// map"s. |
|
499 |
/// that its maps are real \ref concepts::ReferenceMap |
|
500 |
/// "reference map"s. |
|
500 | 501 |
class GridGraph : public ExtendedGridGraphBase { |
501 | 502 |
public: |
502 | 503 |
|
503 | 504 |
typedef ExtendedGridGraphBase Parent; |
504 | 505 |
|
505 | 506 |
/// \brief Map to get the indices of the nodes as dim2::Point<int>. |
506 | 507 |
/// |
507 | 508 |
/// Map to get the indices of the nodes as dim2::Point<int>. |
508 | 509 |
class IndexMap { |
509 | 510 |
public: |
510 | 511 |
/// \brief The key type of the map |
511 | 512 |
typedef GridGraph::Node Key; |
512 | 513 |
/// \brief The value type of the map |
513 | 514 |
typedef dim2::Point<int> Value; |
514 | 515 |
|
515 | 516 |
/// \brief Constructor |
516 | 517 |
/// |
517 | 518 |
/// Constructor |
518 | 519 |
IndexMap(const GridGraph& graph) : _graph(graph) {} |
519 | 520 |
|
520 | 521 |
/// \brief The subscript operator |
521 | 522 |
/// |
522 | 523 |
/// The subscript operator. |
523 | 524 |
Value operator[](Key key) const { |
524 | 525 |
return _graph.pos(key); |
525 | 526 |
} |
526 | 527 |
|
527 | 528 |
private: |
528 | 529 |
const GridGraph& _graph; |
529 | 530 |
}; |
530 | 531 |
|
531 | 532 |
/// \brief Map to get the column of the nodes. |
... | ... |
@@ -163,66 +163,66 @@ |
163 | 163 |
|
164 | 164 |
Node |
165 | 165 |
n1 = g.addNode(), |
166 | 166 |
n2 = g.addNode(), |
167 | 167 |
n3 = g.addNode(); |
168 | 168 |
|
169 | 169 |
Edge |
170 | 170 |
e1 = g.addEdge(n1, n2), |
171 | 171 |
e2 = g.addEdge(n2, n3); |
172 | 172 |
|
173 | 173 |
check(g.valid(n1), "Wrong validity check"); |
174 | 174 |
check(g.valid(e1), "Wrong validity check"); |
175 | 175 |
check(g.valid(g.direct(e1, true)), "Wrong validity check"); |
176 | 176 |
|
177 | 177 |
g.erase(n1); |
178 | 178 |
|
179 | 179 |
check(!g.valid(n1), "Wrong validity check"); |
180 | 180 |
check(g.valid(n2), "Wrong validity check"); |
181 | 181 |
check(g.valid(n3), "Wrong validity check"); |
182 | 182 |
check(!g.valid(e1), "Wrong validity check"); |
183 | 183 |
check(g.valid(e2), "Wrong validity check"); |
184 | 184 |
|
185 | 185 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
186 | 186 |
check(!g.valid(g.edgeFromId(-1)), "Wrong validity check"); |
187 | 187 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
void checkGridGraph(int width, int height) { |
191 | 191 |
typedef GridGraph Graph; |
192 | 192 |
GRAPH_TYPEDEFS(Graph); |
193 | 193 |
Graph G(width, height); |
194 | 194 |
|
195 |
check(G.width() == width, "Wrong row number"); |
|
196 |
check(G.height() == height, "Wrong column number"); |
|
195 |
check(G.width() == width, "Wrong column number"); |
|
196 |
check(G.height() == height, "Wrong row number"); |
|
197 | 197 |
|
198 | 198 |
for (int i = 0; i < width; ++i) { |
199 | 199 |
for (int j = 0; j < height; ++j) { |
200 | 200 |
check(G.col(G(i, j)) == i, "Wrong column"); |
201 | 201 |
check(G.row(G(i, j)) == j, "Wrong row"); |
202 | 202 |
check(G.pos(G(i, j)).x == i, "Wrong column"); |
203 | 203 |
check(G.pos(G(i, j)).y == j, "Wrong row"); |
204 | 204 |
} |
205 | 205 |
} |
206 | 206 |
|
207 | 207 |
for (int j = 0; j < height; ++j) { |
208 | 208 |
for (int i = 0; i < width - 1; ++i) { |
209 | 209 |
check(G.source(G.right(G(i, j))) == G(i, j), "Wrong right"); |
210 | 210 |
check(G.target(G.right(G(i, j))) == G(i + 1, j), "Wrong right"); |
211 | 211 |
} |
212 | 212 |
check(G.right(G(width - 1, j)) == INVALID, "Wrong right"); |
213 | 213 |
} |
214 | 214 |
|
215 | 215 |
for (int j = 0; j < height; ++j) { |
216 | 216 |
for (int i = 1; i < width; ++i) { |
217 | 217 |
check(G.source(G.left(G(i, j))) == G(i, j), "Wrong left"); |
218 | 218 |
check(G.target(G.left(G(i, j))) == G(i - 1, j), "Wrong left"); |
219 | 219 |
} |
220 | 220 |
check(G.left(G(0, j)) == INVALID, "Wrong left"); |
221 | 221 |
} |
222 | 222 |
|
223 | 223 |
for (int i = 0; i < width; ++i) { |
224 | 224 |
for (int j = 0; j < height - 1; ++j) { |
225 | 225 |
check(G.source(G.up(G(i, j))) == G(i, j), "Wrong up"); |
226 | 226 |
check(G.target(G.up(G(i, j))) == G(i, j + 1), "Wrong up"); |
227 | 227 |
} |
228 | 228 |
check(G.up(G(i, height - 1)) == INVALID, "Wrong up"); |
0 comments (0 inline)