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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/bin/shrtpath.c,v 1.1 2005-12-07 15:46:54 rc-flyer Exp $
*/
/* XTrkCad - Model Railroad CAD
* Copyright (C) 2005 Dave Bullis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include "shrtpath.h"
#include "track.h"
EXPORT int log_shortPath;
static int log_shortPathInitted;
/**************************************************************************
*
* Dijkstra's shortest path
*
**************************************************************************/
typedef enum { Unknown, Working, Final } pathState_e;
typedef struct {
pathState_e state;
DIST_T dist; /* Distance from root to entry */
track_p contTrk; /* continuation */
EPINX_T contEP;
int inxBack; /* Previous node on shortest path */
int inxTracks; /* List of tracks along this path */
int numTracks;
} pathNode_t, *pathNode_p;
static dynArr_t pathNode_da;
#define pathNode(N) DYNARR_N( pathNode_t, pathNode_da, N )
typedef struct {
track_p trk;
EPINX_T ep1, ep2;
DIST_T dist;
} trackep_t, *trackep_p;
static dynArr_t trackep_da;
#define trackep(N) DYNARR_N( trackep_t, trackep_da, N )
static track_p shortPathTrk0, shortPathTrk1;
static EPINX_T shortPathEP0, shortPathEP1;
static int DoShortPathFunc( shortestPathFunc_p func, char * title, SPTF_CMD cmd, track_p trk, EPINX_T ep1, EPINX_T ep2, DIST_T dist, void * data )
{
int rc;
LOG( log_shortPath, 4, ( " %s: T%d:%d.%d D:%0.3f ", title, trk?GetTrkIndex(trk):-1, ep1, ep2, dist ) )
rc = func( cmd, trk, ep1, ep2, dist, data );
LOG( log_shortPath, 4, ( "-> %d\n", rc ) )
return rc;
}
static void DumpPaths( int pinx )
{
pathNode_p pPath;
trackep_p pTrackep;
int tinx;
lprintf(" Current = %d\n", pinx );
for (pinx=0; pinx<pathNode_da.cnt; pinx++) {
pPath = &pathNode(pinx);
lprintf( " %3d: S%c T%d:%d D%0.3f T%d:%d",
pinx,
pPath->state==Unknown?'U':pPath->state==Working?'W':pPath->state==Final?'F':'?',
(pPath->contTrk?GetTrkIndex(pPath->contTrk):-1),
pPath->contEP,
pPath->dist,
pPath->inxTracks,
pPath->numTracks );
if (pPath->inxBack>=0) {
lprintf(" B%d", pPath->inxBack );
}
lprintf("\n ");
for (tinx=0; tinx<pPath->numTracks; tinx++) {
pTrackep = &trackep(pPath->inxTracks+tinx);
lprintf( " T%d:%d-%d=%0.1f", GetTrkIndex(pTrackep->trk), pTrackep->ep1, pTrackep->ep2, pTrackep->dist );
}
lprintf("\n");
}
}
static void AddTracksToPath(
int inxCurr,
shortestPathFunc_p func,
void * data )
{
pathNode_p pPath;
int tinx;
trackep_p pTrackep;
while (inxCurr>=0) {
pPath = &pathNode(inxCurr);
for (tinx=pPath->numTracks-1;tinx>=0;tinx--) {
pTrackep = &trackep(pPath->inxTracks+tinx);
DoShortPathFunc( func, "ADDTRK", SPTC_ADD_TRK, pTrackep->trk, pTrackep->ep1, pTrackep->ep2, pTrackep->dist, data );
}
inxCurr = pPath->inxBack;
}
}
static void AddTrackToNode(
track_p trk,
EPINX_T ep1,
EPINX_T ep2,
DIST_T dist)
{
DYNARR_APPEND( trackep_t, trackep_da, 10 );
trackep(trackep_da.cnt-1).trk = trk;
trackep(trackep_da.cnt-1).ep1 = ep1;
trackep(trackep_da.cnt-1).ep2 = ep2;
trackep(trackep_da.cnt-1).dist = dist;
}
static BOOL_T AddPath(
int inxCurr,
track_p trk0,
EPINX_T ep1,
EPINX_T ep2,
DIST_T dist,
shortestPathFunc_p func,
void * data )
{
EPINX_T epN;
track_p trk=trk0, trkN;
EPINX_T epCnt;
pathNode_p pNode;
int startTrack;
char * msg=NULL;
LOG( log_shortPath, 2, ( " AddPath( T%d:%d.%d D=%0.3f B%d ) -> \n", GetTrkIndex(trk), ep1, ep2, dist, inxCurr ) )
startTrack = trackep_da.cnt;
while (1) {
if ( ep2>=0 ) {
AddTrackToNode( trk, ep1, ep2, dist );
dist += GetTrkLength( trk, ep1, -1 ) + GetTrkLength( trk, ep2, -1 );
if ( DoShortPathFunc( func, "MATCH", SPTC_MATCH, trk, ep2, ep1, dist, data ) ) {
trk = NULL;
ep1 = -1;
msg = "";
goto makeNode;
}
trkN = GetTrkEndTrk(trk,ep2);
if ( trkN == NULL ) {
/* dead end */
msg = "dead end";
goto skipNode;
}
if ( DoShortPathFunc( func, "IGNORE", SPTC_IGNNXTTRK, trk, ep2, ep1, dist, data ) ) {
msg = "ignore end";
goto skipNode;
}
ep1 = GetEndPtConnectedToMe( trkN, trk );
trk = trkN;
if ( (trk==shortPathTrk0 && ep1==shortPathEP0) || (trk==shortPathTrk1 && ep1==shortPathEP1) ) {
msg = "wrap around";
goto skipNode;
}
}
epCnt = GetTrkEndPtCnt(trk);
if ( epCnt < 2 ) {
msg = "bumper track";
goto skipNode;
}
if ( epCnt > 2 ) {
if ( (epN=DoShortPathFunc( func, "MATCHANY", SPTC_MATCHANY, trk, ep1, -1, dist, data )) >= 0 ) {
/* special match */
/*dist += GetTrkLength( trk, ep1, epN );*/
AddTrackToNode( trk, ep1, epN, dist );
trk = NULL;
ep1 = -1;
msg = "ANY";
}
goto makeNode;
}
ep2 = 1-ep1;
}
makeNode:
if ( trk ) {
LOG( log_shortPath, 2, ( " -> FORK: [%d] T%d:%d", pathNode_da.cnt, GetTrkIndex(trk), ep1 ) )
} else {
LOG( log_shortPath, 2, ( " -> MATCH%s: [%d]", msg, pathNode_da.cnt ) )
}
LOG( log_shortPath, 2, ( " t%d D=%0.3f\n", startTrack, dist ) )
DYNARR_APPEND( pathNode_t, pathNode_da, 10 );
pNode = &pathNode(pathNode_da.cnt-1);
pNode->state = Working;
pNode->dist = dist;
pNode->contTrk = trk;
pNode->contEP = ep1;
pNode->inxBack = inxCurr;
pNode->inxTracks = startTrack;
pNode->numTracks = trackep_da.cnt-startTrack;
if ( trk )
SetTrkBits( trk, TB_SHRTPATH );
return TRUE;
skipNode:
LOG( log_shortPath, 2, ( " -> FAIL: %s @ T%d:%d.%d\n", msg, GetTrkIndex(trk), ep1, ep2 ) )
trackep_da.cnt = startTrack;
return FALSE;
}
int FindShortestPath(
track_p trkN,
EPINX_T epN,
BOOL_T bidirectional,
shortestPathFunc_p func,
void * data )
{
int inxCurr = 0;
pathNode_p pCurr;
pathNode_p pNext;
int pinx=0;
DIST_T minDist;
int count;
int rc = 0;
EPINX_T ep2, epCnt, ep3;
static dynArr_t ep_da;
#define ep(N) DYNARR_N( pathNode_p, ep_da, N )
DYNARR_RESET( pathNode_t, pathNode_da );
DYNARR_RESET( trackep_t, trackep_da );
count = 0;
if ( !log_shortPathInitted ) {
log_shortPath = LogFindIndex( "shortPath" );
log_shortPathInitted = TRUE;
}
LOG( log_shortPath, 1, ( "FindShortestPath( T%d:%d, %s, ... )\n", GetTrkIndex(trkN), epN, bidirectional?"bidir":"unidir" ) )
ClrAllTrkBits( TB_SHRTPATH );
/* Note: trkN:epN is not tested for MATCH */
shortPathTrk0 = trkN;
shortPathEP0 = epN;
shortPathTrk1 = GetTrkEndTrk( trkN, epN );
if ( shortPathTrk1 != NULL )
shortPathEP1 = GetEndPtConnectedToMe( shortPathTrk1, shortPathTrk0 );
AddPath( -1, shortPathTrk0, shortPathEP0, -1, 0.0, func, data );
if ( bidirectional && shortPathTrk1 != NULL )
AddPath( -1, shortPathTrk1, shortPathEP1, -1, 0.0, func, data );
while (1) {
InfoMessage( "%d", ++count );
/* select next final node */
minDist = 0.0;
inxCurr = -1;
for (pinx=0; pinx<pathNode_da.cnt; pinx++) {
pNext = &pathNode(pinx);
if (pNext->state == Working &&
(inxCurr < 0 || pNext->dist < minDist) ) {
minDist = pathNode(pinx).dist;
inxCurr = pinx;
}
}
if ( inxCurr < 0 )
break;
if (log_shortPath>=4) DumpPaths(inxCurr);
pCurr = &pathNode(inxCurr);
pCurr->state = Final;
if ( pCurr->contTrk == NULL ) {
if ( !DoShortPathFunc( func, "VALID", SPTC_VALID, trackep(pCurr->inxTracks+pCurr->numTracks-1).trk, trackep(pCurr->inxTracks+pCurr->numTracks-1).ep2, -1, 0.0, data ) )
continue;
AddTracksToPath( inxCurr, func, data );
rc++;
if ( DoShortPathFunc( func, "TERMINATE", SPTC_TERMINATE, trackep(pCurr->inxTracks+pCurr->numTracks-1).trk, trackep(pCurr->inxTracks+pCurr->numTracks-1).ep2, -1, 0.0, data ) )
break;
} else {
epCnt = GetTrkEndPtCnt(pCurr->contTrk);
DYNARR_SET( pathNode_p, ep_da, epCnt );
memset( ep_da.ptr, 0, epCnt * sizeof pNext );
if ( (GetTrkBits(pCurr->contTrk) & TB_SHRTPATH) ) {
for ( pinx=0; pinx<pathNode_da.cnt; pinx++ ) {
pNext = &pathNode(pinx);
if ( pNext->contTrk == pCurr->contTrk ) {
ep(pNext->contEP) = pNext;
}
}
}
for ( ep2=0; ep2<epCnt; ep2++ ) {
pCurr = &pathNode(inxCurr);
/* don't point back at myself */
if ( pCurr->contEP == ep2 ) continue;
/* no route to ep */
if ( DoShortPathFunc( func, "IGNORE", SPTC_IGNNXTTRK, pCurr->contTrk, pCurr->contEP, ep2, pCurr->dist, data ) ) continue;
/* somebody got here first */
if ( ep(ep2) ) continue;
/* there is already a path out via ep2 */
for ( ep3=0; ep3<epCnt; ep3++ ) {
if ( ep3==pCurr->contEP || ep3==ep2 ) continue;
if ( ep(ep3) == NULL ) continue;
if ( DoShortPathFunc( func, "IGNORE", SPTC_IGNNXTTRK, pCurr->contTrk, ep2, ep3, pCurr->dist, data ) ) continue;
if ( ep(ep3)->state == Final ) break;
}
if ( ep3 < epCnt ) continue;
AddPath( inxCurr, pCurr->contTrk, pCurr->contEP, ep2, pCurr->dist, func, data );
}
}
}
if (log_shortPath>=1) DumpPaths(inxCurr);
ClrAllTrkBits( TB_SHRTPATH );
return rc;
}
|