| ... | ... |
@@ -388,193 +388,193 @@ |
| 388 | 388 |
void nextInc(Edge& edge, bool& dir) const {
|
| 389 | 389 |
int nid = edge._id; |
| 390 | 390 |
if (dir) {
|
| 391 | 391 |
if (nid >= _edge_limit) {
|
| 392 | 392 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 393 | 393 |
(nid - _edge_limit) / (_width - 1) * _width; |
| 394 | 394 |
if (nid < _node_num - _width) {
|
| 395 | 395 |
edge._id = nid; |
| 396 | 396 |
return; |
| 397 | 397 |
} |
| 398 | 398 |
} |
| 399 | 399 |
if (nid % _width > 0) {
|
| 400 | 400 |
edge._id = _edge_limit + nid % _width + |
| 401 | 401 |
(nid / _width) * (_width - 1) - 1; |
| 402 | 402 |
dir = false; |
| 403 | 403 |
return; |
| 404 | 404 |
} |
| 405 | 405 |
if (nid >= _width) {
|
| 406 | 406 |
edge._id = nid - _width; |
| 407 | 407 |
dir = false; |
| 408 | 408 |
return; |
| 409 | 409 |
} |
| 410 | 410 |
} else {
|
| 411 | 411 |
if (nid >= _edge_limit) {
|
| 412 | 412 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 413 | 413 |
(nid - _edge_limit) / (_width - 1) * _width + 1; |
| 414 | 414 |
if (nid >= _width) {
|
| 415 | 415 |
edge._id = nid - _width; |
| 416 | 416 |
return; |
| 417 | 417 |
} |
| 418 | 418 |
} |
| 419 | 419 |
} |
| 420 | 420 |
edge._id = -1; |
| 421 | 421 |
dir = true; |
| 422 | 422 |
} |
| 423 | 423 |
|
| 424 | 424 |
Arc right(Node n) const {
|
| 425 | 425 |
if (n._id % _width < _width - 1) {
|
| 426 | 426 |
return Arc(((_edge_limit + n._id % _width + |
| 427 | 427 |
(n._id / _width) * (_width - 1)) << 1) | 1); |
| 428 | 428 |
} else {
|
| 429 | 429 |
return INVALID; |
| 430 | 430 |
} |
| 431 | 431 |
} |
| 432 | 432 |
|
| 433 | 433 |
Arc left(Node n) const {
|
| 434 | 434 |
if (n._id % _width > 0) {
|
| 435 | 435 |
return Arc((_edge_limit + n._id % _width + |
| 436 | 436 |
(n._id / _width) * (_width - 1) - 1) << 1); |
| 437 | 437 |
} else {
|
| 438 | 438 |
return INVALID; |
| 439 | 439 |
} |
| 440 | 440 |
} |
| 441 | 441 |
|
| 442 | 442 |
Arc up(Node n) const {
|
| 443 | 443 |
if (n._id < _edge_limit) {
|
| 444 | 444 |
return Arc((n._id << 1) | 1); |
| 445 | 445 |
} else {
|
| 446 | 446 |
return INVALID; |
| 447 | 447 |
} |
| 448 | 448 |
} |
| 449 | 449 |
|
| 450 | 450 |
Arc down(Node n) const {
|
| 451 | 451 |
if (n._id >= _width) {
|
| 452 | 452 |
return Arc((n._id - _width) << 1); |
| 453 | 453 |
} else {
|
| 454 | 454 |
return INVALID; |
| 455 | 455 |
} |
| 456 | 456 |
} |
| 457 | 457 |
|
| 458 | 458 |
private: |
| 459 | 459 |
int _width, _height; |
| 460 | 460 |
int _node_num, _edge_num; |
| 461 | 461 |
int _edge_limit; |
| 462 | 462 |
}; |
| 463 | 463 |
|
| 464 | 464 |
|
| 465 | 465 |
typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase; |
| 466 | 466 |
|
| 467 | 467 |
/// \ingroup graphs |
| 468 | 468 |
/// |
| 469 | 469 |
/// \brief Grid graph class |
| 470 | 470 |
/// |
| 471 | 471 |
/// This class implements a special graph type. The nodes of the |
| 472 | 472 |
/// graph can be indexed by two integer \c (i,j) value where \c i is |
| 473 | 473 |
/// in the \c [0..width()-1] range and j is in the \c |
| 474 | 474 |
/// [0..height()-1] range. Two nodes are connected in the graph if |
| 475 | 475 |
/// the indexes differ exactly on one position and exactly one is |
| 476 | 476 |
/// the difference. The nodes of the graph can be indexed by position |
| 477 | 477 |
/// with the \c operator()() function. The positions of the nodes can be |
| 478 | 478 |
/// get with \c pos(), \c col() and \c row() members. The outgoing |
| 479 | 479 |
/// arcs can be retrieved with the \c right(), \c up(), \c left() |
| 480 | 480 |
/// and \c down() functions, where the bottom-left corner is the |
| 481 | 481 |
/// origin. |
| 482 | 482 |
/// |
| 483 | 483 |
/// \image html grid_graph.png |
| 484 |
/// \image latex grid_graph.eps "Grid graph" |
|
| 484 |
/// \image latex grid_graph.eps "Grid graph" width=\textwidth |
|
| 485 | 485 |
/// |
| 486 | 486 |
/// A short example about the basic usage: |
| 487 | 487 |
///\code |
| 488 | 488 |
/// GridGraph graph(rows, cols); |
| 489 | 489 |
/// GridGraph::NodeMap<int> val(graph); |
| 490 | 490 |
/// for (int i = 0; i < graph.width(); ++i) {
|
| 491 | 491 |
/// for (int j = 0; j < graph.height(); ++j) {
|
| 492 | 492 |
/// val[graph(i, j)] = i + j; |
| 493 | 493 |
/// } |
| 494 | 494 |
/// } |
| 495 | 495 |
///\endcode |
| 496 | 496 |
/// |
| 497 | 497 |
/// This graph type is fully conform to the \ref concepts::Graph |
| 498 | 498 |
/// "Graph" concept, and it also has an important extra feature |
| 499 | 499 |
/// that its maps are real \ref concepts::ReferenceMap |
| 500 | 500 |
/// "reference map"s. |
| 501 | 501 |
class GridGraph : public ExtendedGridGraphBase {
|
| 502 | 502 |
public: |
| 503 | 503 |
|
| 504 | 504 |
typedef ExtendedGridGraphBase Parent; |
| 505 | 505 |
|
| 506 | 506 |
/// \brief Map to get the indices of the nodes as dim2::Point<int>. |
| 507 | 507 |
/// |
| 508 | 508 |
/// Map to get the indices of the nodes as dim2::Point<int>. |
| 509 | 509 |
class IndexMap {
|
| 510 | 510 |
public: |
| 511 | 511 |
/// \brief The key type of the map |
| 512 | 512 |
typedef GridGraph::Node Key; |
| 513 | 513 |
/// \brief The value type of the map |
| 514 | 514 |
typedef dim2::Point<int> Value; |
| 515 | 515 |
|
| 516 | 516 |
/// \brief Constructor |
| 517 | 517 |
/// |
| 518 | 518 |
/// Constructor |
| 519 | 519 |
IndexMap(const GridGraph& graph) : _graph(graph) {}
|
| 520 | 520 |
|
| 521 | 521 |
/// \brief The subscript operator |
| 522 | 522 |
/// |
| 523 | 523 |
/// The subscript operator. |
| 524 | 524 |
Value operator[](Key key) const {
|
| 525 | 525 |
return _graph.pos(key); |
| 526 | 526 |
} |
| 527 | 527 |
|
| 528 | 528 |
private: |
| 529 | 529 |
const GridGraph& _graph; |
| 530 | 530 |
}; |
| 531 | 531 |
|
| 532 | 532 |
/// \brief Map to get the column of the nodes. |
| 533 | 533 |
/// |
| 534 | 534 |
/// Map to get the column of the nodes. |
| 535 | 535 |
class ColMap {
|
| 536 | 536 |
public: |
| 537 | 537 |
/// \brief The key type of the map |
| 538 | 538 |
typedef GridGraph::Node Key; |
| 539 | 539 |
/// \brief The value type of the map |
| 540 | 540 |
typedef int Value; |
| 541 | 541 |
|
| 542 | 542 |
/// \brief Constructor |
| 543 | 543 |
/// |
| 544 | 544 |
/// Constructor |
| 545 | 545 |
ColMap(const GridGraph& graph) : _graph(graph) {}
|
| 546 | 546 |
|
| 547 | 547 |
/// \brief The subscript operator |
| 548 | 548 |
/// |
| 549 | 549 |
/// The subscript operator. |
| 550 | 550 |
Value operator[](Key key) const {
|
| 551 | 551 |
return _graph.col(key); |
| 552 | 552 |
} |
| 553 | 553 |
|
| 554 | 554 |
private: |
| 555 | 555 |
const GridGraph& _graph; |
| 556 | 556 |
}; |
| 557 | 557 |
|
| 558 | 558 |
/// \brief Map to get the row of the nodes. |
| 559 | 559 |
/// |
| 560 | 560 |
/// Map to get the row of the nodes. |
| 561 | 561 |
class RowMap {
|
| 562 | 562 |
public: |
| 563 | 563 |
/// \brief The key type of the map |
| 564 | 564 |
typedef GridGraph::Node Key; |
| 565 | 565 |
/// \brief The value type of the map |
| 566 | 566 |
typedef int Value; |
| 567 | 567 |
|
| 568 | 568 |
/// \brief Constructor |
| 569 | 569 |
/// |
| 570 | 570 |
/// Constructor |
| 571 | 571 |
RowMap(const GridGraph& graph) : _graph(graph) {}
|
| 572 | 572 |
|
| 573 | 573 |
/// \brief The subscript operator |
| 574 | 574 |
/// |
| 575 | 575 |
/// The subscript operator. |
| 576 | 576 |
Value operator[](Key key) const {
|
| 577 | 577 |
return _graph.row(key); |
| 578 | 578 |
} |
| 579 | 579 |
|
| 580 | 580 |
private: |
0 comments (0 inline)