ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/lib/outil/src/hypergraphlib_dijkstra.cpp
Revision: 113
Committed: Wed Jun 25 18:44:12 2008 UTC (16 years, 10 months ago) by francois
Original Path: magic/lib/outil/outil/src/HypergraphLib_Dijkstra.cpp
File size: 4086 byte(s)
Error occurred while calculating annotation data.
Log Message:
pb de compatibilite windows apres le passage linux de hypergraph

File Contents

# Content
1 #include "gestionversion.h"
2 #ifdef WINDOWS_VERSION
3 #include "HypergraphLib_Dijkstra.h"
4
5 #include "HypergraphLib_Graph.h"
6 #include "HypergraphLib_Node.h"
7 #include "HypergraphLib_Arc.h"
8
9 #include <stdio.h>
10
11 using namespace HypergraphLib;
12
13 //---------------------------------------------------------------------------
14 struct DJNode {
15 int nbVisit;
16 double d;
17 Node * n;
18 Node * previous;
19 Arc * prevArc;
20 };
21
22 //---------------------------------------------------------------------------
23 //Calcule le chemin le plus court entre les noeuds a et b, selon la
24 //fonction de coût distanceFunc
25 double
26 HypergraphLib::Dijkstra(Graph * __G, Node * source, Node * destination, double (* distanceFunc) (HypergraphLib::Node*, HypergraphLib::Node*, HypergraphLib::Arc*) , std::vector<Node *> & path, std::vector<Arc*> & pathArcs)
27 {
28 int i,j,k;
29
30 #define djnode(xtmp) ((DJNode*)xtmp->GetUserData(50))
31
32
33 Graph::MapNodesById graphNodes = __G->GetNodes();
34 for (Graph::MapNodesById::iterator itNode = graphNodes.begin();
35 itNode != graphNodes.end();
36 itNode ++ )
37 {
38 Node * n = itNode->second;
39 struct DJNode * dj = new struct DJNode;
40 dj->previous = NULL;
41 dj->n = n;
42 dj->nbVisit = 0;
43 dj->d = 1E300;
44 dj->prevArc = 0;
45
46 n->SetUserData(50,dj);
47 }
48
49 if (__G->GetNode(source->Id()) != source || __G->GetNode(destination->Id()) != destination )
50 return 1E300;
51
52 djnode(source)->d = 0;
53
54 typedef std::pair < double, DJNode * > DJElement;
55 std::set < DJElement > Q;
56 std::set < DJElement >::iterator itQ;
57 DJNode* djSource=djnode(source);
58 DJNode* djDestination=djnode(destination);
59 Q.insert(DJElement(djSource->d,djSource));
60
61 while ( ! Q.empty() )
62 {
63 struct DJNode * current = NULL;
64
65 DJElement top = *Q.begin();
66 Q.erase(Q.begin());
67 current = top.second;
68
69 if ( current == NULL )
70 break;
71
72 current->nbVisit++;
73
74 for ( Node::MultimapArcsById::iterator itArc1 = current->n->IncidentArcs().begin();
75 itArc1 != current->n->IncidentArcs().end();
76 itArc1 ++ )
77 {
78 Arc * arc = itArc1->second;
79 for ( Arc::MultimapNodesById::iterator itNode = arc->Nodes().begin();
80 itNode != arc->Nodes().end();
81 itNode ++ )
82 {
83 Node * child = itNode->second;
84 if (child == current->n)
85 continue;
86
87 struct DJNode * sDJNodeChild = djnode(child);
88 double dist_child = distanceFunc(child, current->n, arc);
89
90 if ( sDJNodeChild->d > current->d + dist_child)
91 {
92 if(sDJNodeChild->d < 1E100) {
93 itQ = Q.find(DJElement(sDJNodeChild->d,sDJNodeChild));
94 if (itQ != Q.end())
95 Q.erase(itQ);
96 }
97
98 sDJNodeChild->d = current->d + dist_child;
99 sDJNodeChild->previous = current->n;
100 sDJNodeChild->prevArc = arc;
101
102 Q.insert(DJElement(sDJNodeChild->d,sDJNodeChild));
103 }
104 }
105 }
106 }
107
108 struct DJNode * n = djDestination;
109 double length = n->d;
110
111 int nb_node = __G->GetNodes().size();
112
113 for (i=0; i<nb_node ; i++)
114 {
115 path.insert(path.begin(), n->n);
116 if (n->prevArc)
117 pathArcs.insert(pathArcs.begin(), n->prevArc);
118 if ( n->n == source )
119 break;
120 else
121 {
122 if (n->previous)
123 n = djnode(n->previous);
124 else
125 break;
126 }
127 }
128 if (i == nb_node)
129 length = 1E300;
130
131 for (Graph::MapNodesById::iterator itNode = graphNodes.begin();
132 itNode != graphNodes.end();
133 itNode ++ )
134 {
135 Node * node = itNode->second;
136 struct DJNode * djtmp = djnode(node);
137 delete djtmp;
138 node->SetUserData(50,0);
139 }
140 #undef djnode
141
142 return length;
143 }
144 #endif
145