Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /trunk/src/superx.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 761 - (show annotations) (download) (as text)
Fri Jun 3 18:42:06 2011 UTC (13 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 14011 byte(s)
Shorten the messages when running to avoid going beyond 80 characters.

1 /***************************************
2 Super-Segment data type functions.
3
4 Part of the Routino routing software.
5 ******************/ /******************
6 This file Copyright 2008-2011 Andrew M. Bishop
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ***************************************/
21
22
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include "ways.h"
27
28 #include "nodesx.h"
29 #include "segmentsx.h"
30 #include "waysx.h"
31 #include "superx.h"
32
33 #include "files.h"
34 #include "logging.h"
35 #include "results.h"
36
37
38 /* Local functions */
39
40 static Results *FindRoutesWay(NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx,node_t start,Way *match);
41
42
43 /*++++++++++++++++++++++++++++++++++++++
44 Select the super-nodes from the list of nodes.
45
46 NodesX *nodesx The set of nodes to use.
47
48 SegmentsX *segmentsx The set of segments to use.
49
50 WaysX *waysx The set of ways to use.
51 ++++++++++++++++++++++++++++++++++++++*/
52
53 void ChooseSuperNodes(NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx)
54 {
55 index_t i;
56 int nnodes=0;
57
58 if(nodesx->number==0 || segmentsx->number==0 || waysx->number==0)
59 return;
60
61 /* Print the start message */
62
63 printf_first("Finding Super-Nodes: Nodes=0 Super-Nodes=0");
64
65 /* Map into memory / open the files */
66
67 #if !SLIM
68 nodesx->data=MapFile(nodesx->filename);
69 segmentsx->data=MapFile(segmentsx->filename);
70 waysx->data=MapFile(waysx->filename);
71 #else
72 nodesx->fd=ReOpenFile(nodesx->filename);
73 segmentsx->fd=ReOpenFile(segmentsx->filename);
74 waysx->fd=ReOpenFile(waysx->filename);
75 #endif
76
77 /* Find super-nodes */
78
79 for(i=0;i<nodesx->number;i++)
80 {
81 if(IsBitSet(nodesx->super,i))
82 {
83 int issuper=0;
84 NodeX *nodex=LookupNodeX(nodesx,i,1);
85
86 if(nodex->flags&(NODE_TURNRSTRCT|NODE_TURNRSTRCT2))
87 {
88 issuper=1;
89 }
90 else
91 {
92 int count=0,j;
93 Way segmentway[MAX_SEG_PER_NODE];
94 int segmentweight[MAX_SEG_PER_NODE];
95 SegmentX *segmentx=FirstSegmentX(segmentsx,i,1);
96
97 while(segmentx)
98 {
99 WayX *wayx=LookupWayX(waysx,segmentx->way,1);
100 int nsegments;
101
102 /* Segments that are loops count twice */
103
104 assert(count<MAX_SEG_PER_NODE); /* Only a limited amount of information stored. */
105
106 if(segmentx->node1==segmentx->node2)
107 segmentweight[count]=2;
108 else
109 segmentweight[count]=1;
110
111 segmentway[count]=wayx->way;
112
113 /* If the node allows less traffic types than any connecting way then it is super */
114
115 if((wayx->way.allow&nodex->allow)!=wayx->way.allow)
116 {
117 issuper=1;
118 break;
119 }
120
121 nsegments=segmentweight[count];
122
123 for(j=0;j<count;j++)
124 if(wayx->way.allow & segmentway[j].allow)
125 {
126 /* If two ways are different in any attribute and there is a type of traffic that can use both then it is super */
127
128 if(WaysCompare(&segmentway[j],&wayx->way))
129 {
130 issuper=1;
131 break;
132 }
133
134 /* If there are two other segments that can be used by the same types of traffic as this one then it is super */
135
136 nsegments+=segmentweight[j];
137 if(nsegments>2)
138 {
139 issuper=1;
140 break;
141 }
142 }
143
144 if(issuper)
145 break;
146
147 segmentx=NextSegmentX(segmentsx,segmentx,i,1);
148
149 count++;
150 }
151 }
152
153 /* Mark the node as super if it is. */
154
155 if(issuper)
156 nnodes++;
157 else
158 ClearBit(nodesx->super,i);
159 }
160
161 if(!((i+1)%10000))
162 printf_middle("Finding Super-Nodes: Nodes=%d Super-Nodes=%d",i+1,nnodes);
163 }
164
165 /* Unmap from memory / close the files */
166
167 #if !SLIM
168 nodesx->data=UnmapFile(nodesx->filename);
169 segmentsx->data=UnmapFile(segmentsx->filename);
170 waysx->data=UnmapFile(waysx->filename);
171 #else
172 nodesx->fd=CloseFile(nodesx->fd);
173 segmentsx->fd=CloseFile(segmentsx->fd);
174 waysx->fd=CloseFile(waysx->fd);
175 #endif
176
177 /* Print the final message */
178
179 printf_last("Found Super-Nodes: Nodes=%d Super-Nodes=%d",nodesx->number,nnodes);
180 }
181
182
183 /*++++++++++++++++++++++++++++++++++++++
184 Create the super-segments from the existing segments.
185
186 SegmentsX *CreateSuperSegments Returns the new super segments.
187
188 NodesX *nodesx The set of nodes to use.
189
190 SegmentsX *segmentsx The set of segments to use.
191
192 WaysX *waysx The set of ways to use.
193 ++++++++++++++++++++++++++++++++++++++*/
194
195 SegmentsX *CreateSuperSegments(NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx)
196 {
197 index_t i;
198 SegmentsX *supersegmentsx;
199 int sn=0,ss=0;
200
201 supersegmentsx=NewSegmentList(0);
202
203 if(segmentsx->number==0 || waysx->number==0)
204 return(supersegmentsx);
205
206 /* Print the start message */
207
208 printf_first("Creating Super-Segments: Super-Nodes=0 Super-Segments=0");
209
210 /* Map into memory / open the files */
211
212 #if !SLIM
213 segmentsx->data=MapFile(segmentsx->filename);
214 waysx->data=MapFile(waysx->filename);
215 #else
216 segmentsx->fd=ReOpenFile(segmentsx->filename);
217 waysx->fd=ReOpenFile(waysx->filename);
218 #endif
219
220 /* Create super-segments for each super-node. */
221
222 for(i=0;i<nodesx->number;i++)
223 {
224 if(IsBitSet(nodesx->super,i))
225 {
226 SegmentX *segmentx;
227 int count=0,match;
228 Way prevway[MAX_SEG_PER_NODE];
229
230 segmentx=FirstSegmentX(segmentsx,i,1);
231
232 while(segmentx)
233 {
234 WayX *wayx=LookupWayX(waysx,segmentx->way,1);
235
236 /* Check that this type of way hasn't already been routed */
237
238 match=0;
239
240 if(count>0)
241 {
242 int j;
243
244 for(j=0;j<count;j++)
245 if(!WaysCompare(&prevway[j],&wayx->way))
246 {
247 match=1;
248 break;
249 }
250 }
251
252 assert(count<MAX_SEG_PER_NODE); /* Only a limited amount of history stored. */
253
254 prevway[count++]=wayx->way;
255
256 /* Route the way and store the super-segments. */
257
258 if(!match)
259 {
260 Results *results=FindRoutesWay(nodesx,segmentsx,waysx,i,&wayx->way);
261 Result *result=FirstResult(results);
262
263 while(result)
264 {
265 if(IsBitSet(nodesx->super,result->node) && result->segment!=NO_SEGMENT)
266 {
267 if(wayx->way.type&Way_OneWay && result->node!=i)
268 AppendSegment(supersegmentsx,segmentx->way,i,result->node,DISTANCE((distance_t)result->score)|ONEWAY_1TO2);
269 else
270 AppendSegment(supersegmentsx,segmentx->way,i,result->node,DISTANCE((distance_t)result->score));
271
272 ss++;
273 }
274
275 result=NextResult(results,result);
276 }
277
278 FreeResultsList(results);
279 }
280
281 segmentx=NextSegmentX(segmentsx,segmentx,i,1);
282 }
283
284 sn++;
285
286 if(!(sn%10000))
287 printf_middle("Creating Super-Segments: Super-Nodes=%d Super-Segments=%d",sn,ss);
288 }
289 }
290
291 /* Unmap from memory / close the files */
292
293 #if !SLIM
294 segmentsx->data=UnmapFile(segmentsx->filename);
295 waysx->data=UnmapFile(waysx->filename);
296 #else
297 segmentsx->fd=CloseFile(segmentsx->fd);
298 waysx->fd=CloseFile(waysx->fd);
299 #endif
300
301 /* Print the final message */
302
303 printf_last("Created Super-Segments: Super-Nodes=%d Super-Segments=%d",sn,ss);
304
305 return(supersegmentsx);
306 }
307
308
309 /*++++++++++++++++++++++++++++++++++++++
310 Merge the segments and super-segments into a new segment list.
311
312 SegmentsX *MergeSuperSegments Returns a new set of merged segments.
313
314 SegmentsX *segmentsx The set of segments to use.
315
316 SegmentsX *supersegmentsx The set of super-segments to use.
317 ++++++++++++++++++++++++++++++++++++++*/
318
319 SegmentsX *MergeSuperSegments(SegmentsX *segmentsx,SegmentsX *supersegmentsx)
320 {
321 index_t i,j;
322 int m=0,a=0;
323 SegmentsX *mergedsegmentsx;
324
325 mergedsegmentsx=NewSegmentList(0);
326
327 if(segmentsx->number==0)
328 return(mergedsegmentsx);
329
330 /* Print the start message */
331
332 printf_first("Merging Segments: Segments=0 Super=0 Merged=0 Added=0");
333
334 /* Map into memory / open the files */
335
336 #if !SLIM
337 segmentsx->data=MapFile(segmentsx->filename);
338 if(supersegmentsx->number>0)
339 supersegmentsx->data=MapFile(supersegmentsx->filename);
340 #else
341 segmentsx->fd=ReOpenFile(segmentsx->filename);
342 if(supersegmentsx->number>0)
343 supersegmentsx->fd=ReOpenFile(supersegmentsx->filename);
344 #endif
345
346 /* Loop through and create a new list of combined segments */
347
348 for(i=0,j=0;i<segmentsx->number;i++)
349 {
350 int super=0;
351 SegmentX *segmentx=LookupSegmentX(segmentsx,i,1);
352
353 while(j<supersegmentsx->number)
354 {
355 SegmentX *supersegmentx=LookupSegmentX(supersegmentsx,j,1);
356
357 if(segmentx->node1 ==supersegmentx->node1 &&
358 segmentx->node2 ==supersegmentx->node2 &&
359 segmentx->distance==supersegmentx->distance)
360 {
361 m++;
362 j++;
363 /* mark as super-segment and normal segment */
364 super=1;
365 break;
366 }
367 else if((segmentx->node1==supersegmentx->node1 &&
368 segmentx->node2==supersegmentx->node2) ||
369 (segmentx->node1==supersegmentx->node1 &&
370 segmentx->node2>supersegmentx->node2) ||
371 (segmentx->node1>supersegmentx->node1))
372 {
373 /* mark as super-segment */
374 AppendSegment(mergedsegmentsx,supersegmentx->way,supersegmentx->node1,supersegmentx->node2,supersegmentx->distance|SEGMENT_SUPER);
375 a++;
376 j++;
377 }
378 else
379 {
380 /* mark as normal segment */
381 break;
382 }
383 }
384
385 if(super)
386 AppendSegment(mergedsegmentsx,segmentx->way,segmentx->node1,segmentx->node2,segmentx->distance|SEGMENT_SUPER|SEGMENT_NORMAL);
387 else
388 AppendSegment(mergedsegmentsx,segmentx->way,segmentx->node1,segmentx->node2,segmentx->distance|SEGMENT_NORMAL);
389
390 if(!((i+1)%10000))
391 printf_middle("Merging Segments: Segments=%d Super=%d Merged=%d Added=%d",i+1,j,m,a);
392 }
393
394 /* Unmap from memory / close the files */
395
396 #if !SLIM
397 segmentsx->data=UnmapFile(segmentsx->filename);
398 if(supersegmentsx->number>0)
399 supersegmentsx->data=UnmapFile(supersegmentsx->filename);
400 #else
401 segmentsx->fd=CloseFile(segmentsx->fd);
402 if(supersegmentsx->number>0)
403 supersegmentsx->fd=CloseFile(supersegmentsx->fd);
404 #endif
405
406 /* Print the final message */
407
408 printf_last("Merged Segments: Segments=%d Super=%d Merged=%d Added=%d",segmentsx->number,supersegmentsx->number,m,a);
409
410 return(mergedsegmentsx);
411 }
412
413
414 /*++++++++++++++++++++++++++++++++++++++
415 Find all routes from a specified super-node to any other super-node that follows a certain type of way.
416
417 Results *FindRoutesWay Returns a set of results.
418
419 NodesX *nodesx The set of nodes to use.
420
421 SegmentsX *segmentsx The set of segments to use.
422
423 WaysX *waysx The set of ways to use.
424
425 node_t start The start node.
426
427 Way *match A template for the type of way that the route must follow.
428 ++++++++++++++++++++++++++++++++++++++*/
429
430 static Results *FindRoutesWay(NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx,node_t start,Way *match)
431 {
432 Results *results;
433 Queue *queue;
434 Result *result1,*result2;
435 WayX *wayx;
436
437 /* Insert the first node into the queue */
438
439 results=NewResultsList(8);
440
441 queue=NewQueueList();
442
443 result1=InsertResult(results,start,NO_SEGMENT);
444
445 InsertInQueue(queue,result1);
446
447 /* Loop across all nodes in the queue */
448
449 while((result1=PopFromQueue(queue)))
450 {
451 index_t node1,seg1;
452 SegmentX *segmentx;
453
454 node1=result1->node;
455 seg1=result1->segment;
456
457 segmentx=FirstSegmentX(segmentsx,node1,2); /* position 1 is already used */
458
459 while(segmentx)
460 {
461 index_t node2,seg2;
462 distance_t cumulative_distance;
463
464 /* must not be one-way against the direction of travel */
465 if(IsOnewayTo(segmentx,node1))
466 goto endloop;
467
468 node2=OtherNode(segmentx,node1);
469
470 seg2=IndexSegmentX(segmentsx,segmentx);
471
472 /* must not be a u-turn */
473 if(result1->segment==seg2)
474 goto endloop;
475
476 wayx=LookupWayX(waysx,segmentx->way,2);
477
478 /* must be the right type of way */
479 if(WaysCompare(&wayx->way,match))
480 goto endloop;
481
482 cumulative_distance=(distance_t)result1->score+DISTANCE(segmentx->distance);
483
484 result2=FindResult(results,node2,seg2);
485
486 if(!result2) /* New end node */
487 {
488 result2=InsertResult(results,node2,seg2);
489 result2->prev=result1;
490 result2->score=cumulative_distance;
491 result2->sortby=cumulative_distance;
492
493 /* don't route beyond a super-node. */
494 if(!IsBitSet(nodesx->super,node2))
495 InsertInQueue(queue,result2);
496 }
497 else if(cumulative_distance<result2->score)
498 {
499 result2->prev=result1;
500 result2->score=cumulative_distance;
501 result2->sortby=cumulative_distance;
502
503 /* don't route beyond a super-node. */
504 if(!IsBitSet(nodesx->super,node2))
505 InsertInQueue(queue,result2);
506 }
507
508 endloop:
509
510 segmentx=NextSegmentX(segmentsx,segmentx,node1,2);
511 }
512 }
513
514 FreeQueueList(queue);
515
516 return(results);
517 }

Properties

Name Value
cvs:description Super-nodes and super-segments functions.