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
|
/**********************************************************************
* $Id: ConnectedSubgraphFinder.cpp 1820 2006-09-06 16:54:23Z mloskot $
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
* Copyright (C) 2006 Refractions Research Inc.
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#include <geos/planargraph/algorithm/ConnectedSubgraphFinder.h>
#include <geos/planargraph/Subgraph.h>
#include <geos/planargraph/Edge.h>
#include <geos/planargraph/Node.h>
#include <geos/planargraph/DirectedEdge.h>
#include <geos/planargraph/DirectedEdgeStar.h>
#include <vector>
#include <stack>
using namespace std;
namespace geos {
namespace planargraph {
namespace algorithm {
void
ConnectedSubgraphFinder::getConnectedSubgraphs(vector<Subgraph *>& subgraphs)
{
GraphComponent::setVisitedMap(graph.nodeBegin(),
graph.nodeEnd(), false);
for (PlanarGraph::EdgeIterator
it=graph.edgeBegin(),
itEnd=graph.edgeEnd();
it!=itEnd; ++it)
{
Edge *e = *it;
Node *node = e->getDirEdge(0)->getFromNode();
if (! node->isVisited()) {
subgraphs.push_back(findSubgraph(node));
}
}
}
/*private*/
Subgraph*
ConnectedSubgraphFinder::findSubgraph(Node* node)
{
Subgraph* subgraph = new Subgraph(graph);
addReachable(node, subgraph);
return subgraph;
}
/*private*/
void
ConnectedSubgraphFinder::addReachable(Node* startNode,
Subgraph* subgraph)
{
stack<Node *> nodeStack;
nodeStack.push(startNode);
while ( !nodeStack.empty() )
{
Node* node = nodeStack.top();
nodeStack.pop();
addEdges(node, nodeStack, subgraph);
}
}
/*private*/
void
ConnectedSubgraphFinder::addEdges(Node* node,
stack<Node *>& nodeStack, Subgraph* subgraph)
{
node->setVisited(true);
DirectedEdgeStar *des=node->getOutEdges();
for (DirectedEdge::Vect::iterator i=des->begin(), iEnd=des->end();
i!=iEnd; ++i)
{
DirectedEdge *de=*i;
subgraph->add(de->getEdge());
Node *toNode = de->getToNode();
if ( ! toNode->isVisited() ) nodeStack.push(toNode);
}
}
} // namespace geos.planargraph.algorithm
} // namespace geos.planargraph
} // namespace geos
/**********************************************************************
* $Log$
* Revision 1.5 2006/03/21 21:42:54 strk
* planargraph.h header split, planargraph:: classes renamed to match JTS symbols
*
**********************************************************************/
|