From a14a7a0ccc9de76aeab0b2e4bbf58f1a79deedc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Wed, 3 Jul 2024 10:19:30 +0200 Subject: New upstream version 5.3.0GA --- app/bin/elev.c | 513 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 288 insertions(+), 225 deletions(-) (limited to 'app/bin/elev.c') diff --git a/app/bin/elev.c b/app/bin/elev.c index ff987cc..98fcbab 100644 --- a/app/bin/elev.c +++ b/app/bin/elev.c @@ -1,5 +1,5 @@ /** \file elev.c - * + * */ /* XTrkCad - Model Railroad CAD @@ -17,7 +17,7 @@ * * 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. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "ccurve.h" @@ -40,10 +40,10 @@ static char * elevPrefix; static void SetTrkOnElevPath( track_p trk, int mode, DIST_T elev ); typedef struct { - track_p trk; - EPINX_T ep; - DIST_T len; - } elist_t; + track_p trk; + EPINX_T ep; + DIST_T len; +} elist_t; static dynArr_t elist_da; #define elist(N) DYNARR_N( elist_t, elist_da, N ) #define elistAppend( T, E, L ) \ @@ -52,33 +52,35 @@ static dynArr_t elist_da; EPINX_T GetNextTrkOnPath( track_p trk, EPINX_T ep ) { -/* Get next track on Path: - 1 - there is only 1 connected (not counting ep) - 2 - there are >1 but only 1 on Path - 3 - one of them is PST:PSE or PET:PEE -*/ + /* Get next track on Path: + 1 - there is only 1 connected (not counting ep) + 2 - there are >1 but only 1 on Path + 3 - one of them is PST:PSE or PET:PEE + */ EPINX_T ep2; track_p trkN; int epCnt = GetTrkEndPtCnt(trk); for ( ep2=0; ep2trk = trk; @@ -226,7 +233,8 @@ static void FindDefElev( void ) dep->elev = GetTrkEndElevHeight(trk,ep); } } -LOG( log_fillElev, 1, ( "%s: findDefElev [%d] (%ld)\n", elevPrefix, defelev_da.cnt, wGetTimer()-time0 ) ) + LOG( log_fillElev, 1, ( "%s: findDefElev [%d] (%ld)\n", elevPrefix, + defelev_da.cnt, wGetTimer()-time0 ) ) } @@ -250,15 +258,17 @@ static void FindAttachedDefElev( track_p trk, BOOL_T remove ) DYNARR_RESET( elist_t, elist_da ); elistAppend( trk, 0, 0 ); SetTrkBits( trk, TB_PROCESSED ); - if (remove) + if (remove) { ClrTrkBits( trk, TB_ELEVPATH ); + } for ( i1=0; i1trk = trk; @@ -286,8 +296,9 @@ static void FindAttachedDefElev( track_p trk, BOOL_T remove ) if ( !(GetTrkBits(trk1)&TB_PROCESSED) ) { elistAppend( trk1, 0, 0 ); SetTrkBits( trk1, TB_PROCESSED ); - if (remove) + if (remove) { ClrTrkBits( trk1, TB_ELEVPATH ); + } } } } @@ -311,7 +322,8 @@ static int FindObsoleteElevs( void ) FindAttachedDefElev( trk, TRUE ); } } -LOG( log_fillElev, 1, ( "%s: findObsoleteElevs [%d] (%ld)\n", elevPrefix, cnt, wGetTimer()-time0 ) ) + LOG( log_fillElev, 1, ( "%s: findObsoleteElevs [%d] (%ld)\n", elevPrefix, cnt, + wGetTimer()-time0 ) ) return cnt; } @@ -325,22 +337,22 @@ LOG( log_fillElev, 1, ( "%s: findObsoleteElevs [%d] (%ld)\n", elevPrefix, cnt, w */ typedef struct { - track_p trk; - EPINX_T ep; - EPINX_T ep2; - DIST_T dist; - DIST_T elev; - } fork_t; + track_p trk; + EPINX_T ep; + EPINX_T ep2; + DIST_T dist; + DIST_T elev; +} fork_t; static dynArr_t fork_da; #define fork(N) DYNARR_N( fork_t, fork_da, N ) static int FillElevShortestPathFunc( - SPTF_CMD cmd, - track_p trk, - EPINX_T ep, - EPINX_T ep2, - DIST_T dist, - void * data ) + SPTF_CMD cmd, + track_p trk, + EPINX_T ep, + EPINX_T ep2, + DIST_T dist, + void * data ) { defelev_t * dep = (defelev_t *)data; track_p trk1; @@ -351,10 +363,11 @@ static int FillElevShortestPathFunc( case SPTC_MATCH: /*if ( (GetTrkBits(trk)&TB_PROCESSED) ) epRc = 0; - else*/ if ( (dep->trk!=trk || dep->ep!=ep) && EndPtIsDefinedElev(trk,ep)) + else*/ if ( (dep->trk!=trk || dep->ep!=ep) && EndPtIsDefinedElev(trk,ep)) { epRc = 1; - else + } else { epRc = 0; + } break; case SPTC_MATCHANY: @@ -374,12 +387,18 @@ static int FillElevShortestPathFunc( if (!(GetTrkBits(trk)&TB_PROCESSED)) { SetTrkBits(trk, TB_PROCESSED); if ( EndPtIsDefinedElev( trk, ep ) ) { -if (log_shortPath<=0||logTable(log_shortPath).level<4) LOG( log_fillElev, 5, ( " ADD_TRK: T%d:%d D=%0.1f -> DefElev\n", GetTrkIndex(trk), ep, dist ) ) -LOG( log_shortPath, 4, ( "DefElev " ) ) - } else if ( GetNextTrk( trk, ep, &trk1, &ep1, GNTignoreIgnore ) < 0 && trk1 != NULL ) { -if (log_shortPath<=0||logTable(log_shortPath).level<4) LOG( log_fillElev, 4, ( " ADD_TRK: T%d:%d D=%0.3f E=%0.3f -> Fork[%d]\n", GetTrkIndex(trk), ep, dist, dep->elev, fork_da.cnt ) ) -LOG( log_shortPath, 4, ( "E:%0.3f Fork[%d] ", dep->elev, fork_da.cnt ) ) - DYNARR_APPEND( fork_t, fork_da, 10 ); + if (log_shortPath<=0 + ||logTable(log_shortPath).level<4) LOG( log_fillElev, 5, + ( " ADD_TRK: T%d:%d D=%0.1f -> DefElev\n", GetTrkIndex(trk), ep, dist ) ) + LOG( log_shortPath, 4, ( "DefElev " ) ) + } else if ( GetNextTrk( trk, ep, &trk1, &ep1, GNTignoreIgnore ) < 0 + && trk1 != NULL ) { + if (log_shortPath<=0 + ||logTable(log_shortPath).level<4) LOG( log_fillElev, 4, + ( " ADD_TRK: T%d:%d D=%0.3f E=%0.3f -> Fork[%d]\n", GetTrkIndex(trk), ep, + dist, dep->elev, fork_da.cnt ) ) + LOG( log_shortPath, 4, ( "E:%0.3f Fork[%d] ", dep->elev, fork_da.cnt ) ) + DYNARR_APPEND( fork_t, fork_da, 10 ); n = &fork(fork_da.cnt-1); n->trk = trk; n->ep = ep; @@ -391,10 +410,10 @@ LOG( log_shortPath, 4, ( "E:%0.3f Fork[%d] ", dep->elev, fork_da.cnt ) ) n->nep = dep->ep; #endif } else { -LOG( log_shortPath, 4, ( "Normal " ) ) + LOG( log_shortPath, 4, ( "Normal " ) ) } } else { -LOG( log_shortPath, 4, ( "Processed " ) ) + LOG( log_shortPath, 4, ( "Processed " ) ) } return 0; @@ -404,10 +423,10 @@ LOG( log_shortPath, 4, ( "Processed " ) ) case SPTC_IGNNXTTRK: if ( EndPtIsIgnoredElev(trk,ep2) ) { -LOG( log_shortPath, 4, ( "2 Ignore " ) ) + LOG( log_shortPath, 4, ( "2 Ignore " ) ) epRc = 1; } else if ( (!EndPtIsDefinedElev(trk,ep)) && GetTrkEndTrk(trk,ep)==NULL ) { -LOG( log_shortPath, 4, ( "1 Null && !DefElev " ) ) + LOG( log_shortPath, 4, ( "1 Null && !DefElev " ) ) epRc = 1; } else { epRc = 0; @@ -436,19 +455,20 @@ static void FindForks( void ) { int i; defelev_t * dep; - int rc; long time0 = wGetTimer(); DYNARR_RESET( fork_t, fork_da ); for ( i=0; itrk), dep->ep ) ) - rc = FindShortestPath( dep->trk, dep->ep, FALSE, FillElevShortestPathFunc, dep ); + LOG( log_fillElev, 3, ( " findForks from T%d:%d\n", GetTrkIndex(dep->trk), + dep->ep ) ) + FindShortestPath( dep->trk, dep->ep, FALSE, FillElevShortestPathFunc, dep ); } ClrAllTrkBits( TB_PROCESSED ); -LOG( log_fillElev, 1, ( "%s: findForks [%d] (%ld)\n", elevPrefix, fork_da.cnt, wGetTimer()-time0 ) ) + LOG( log_fillElev, 1, ( "%s: findForks [%d] (%ld)\n", elevPrefix, fork_da.cnt, + wGetTimer()-time0 ) ) } @@ -462,9 +482,9 @@ LOG( log_fillElev, 1, ( "%s: findForks [%d] (%ld)\n", elevPrefix, fork_da.cnt, w */ typedef struct { - DIST_T elev; - DIST_T dist; - } elevdist_t; + DIST_T elev; + DIST_T dist; +} elevdist_t; static dynArr_t elevdist_da; #define elevdist(N) DYNARR_N( elevdist_t, elevdist_da, N ); @@ -488,7 +508,8 @@ static DIST_T ComputeWeightedElev( DIST_T totalDist ) w = &elevdist(i2); if (w->dist < 0.001) { e = w->elev; -LOG( log_fillElev, 3, ( " computeWeightedElev: close! D%0.3f E%0.3f\n", w->dist, e ) ) + LOG( log_fillElev, 3, ( " computeWeightedElev: close! D%0.3f E%0.3f\n", + w->dist, e ) ) return e; } d2 += totalDist/w->dist; @@ -501,13 +522,13 @@ LOG( log_fillElev, 3, ( " computeWeightedElev: close! D%0.3f E%0.3f\n", w- e += ((totalDist/w->dist)/d2)*w->elev; } - if (log_fillElev >= 4) { - for ( i2=0; i2elev, w->dist ); - } + if (log_fillElev >= 4) { + for ( i2=0; i2elev, w->dist ); } -LOG( log_fillElev, 3, ( " computeWeightedElev: E%0.3f\n", e ) ) + } + LOG( log_fillElev, 3, ( " computeWeightedElev: E%0.3f\n", e ) ) return e; } @@ -536,8 +557,9 @@ static void ComputeForkElev( void ) n1 = &fork(i1); if ((trk=n1->trk)) { cnt = GetTrkEndPtCnt(n1->trk); - if (cnt<=0) + if (cnt<=0) { continue; + } /* collect dist/elev to connected DefElev points */ d1 = 0; @@ -554,21 +576,25 @@ static void ComputeForkElev( void ) n2->trk = NULL; d1 += w->dist; if ( ! ( ( n1->ep == n2->ep && n1->ep2 == n2->ep2 ) || - ( n1->ep == n2->ep2 && n1->ep2 == n2->ep ) ) ) + ( n1->ep == n2->ep2 && n1->ep2 == n2->ep ) ) ) { singlePath = FALSE; + } } } /* Also check my EPs */ for (ep=0; eptrk && ep==n2->ep) + if (trk==n2->trk && ep==n2->ep) { break; + } } if (i2 >= fork_da.cnt) { DYNARR_APPEND( elevdist_t, elevdist_da, 10 ); @@ -591,12 +617,13 @@ static void ComputeForkElev( void ) SetTrkOnElevPath( trk, ELEV_FORK, e ); /* 3 or more EPs are to DefElevs */ n3->ep = -1; -LOG( log_fillElev, 2, ( " 1 T%d E%0.3f\n", GetTrkIndex(trk), e ) ) + LOG( log_fillElev, 2, ( " 1 T%d E%0.3f\n", GetTrkIndex(trk), e ) ) } forkCnt++; } } -LOG( log_fillElev, 1, ( "%s: computeForkElev [%d] (%ld)\n", elevPrefix, forkCnt, wGetTimer()-time0 ) ) + LOG( log_fillElev, 1, ( "%s: computeForkElev [%d] (%ld)\n", elevPrefix, forkCnt, + wGetTimer()-time0 ) ) } @@ -618,8 +645,7 @@ static void RedrawCompGradeElev( track_p trk, EPINX_T ep ) pos = GetTrkEndPos( trk, ep ); if (!OFF_MAIND( pos, pos ) ) { trk1 = GetTrkEndTrk( trk, ep ); - if ( (trk1=GetTrkEndTrk(trk,ep)) && GetTrkIndex(trk1)trk && n1->ep >= 0 ) { - /* no: make sure we are on the path */ - if ( n1->ep == ep1 ) - ep2 = n1->ep2; - else if ( n1->ep2 == ep1 ) - ep2 = n1->ep; - else - return; - trkN = GetTrkEndTrk(trk1,ep2); - epN = GetEndPtConnectedToMe( trkN, trk1 ); - break; - } - } - if ( i2 >= forkCnt ) - return; + DYNARR_RESET( elist_t, elist_da ); + while (trk1) { + if ( GetTrkIndex(trk1) == checkTrk ) { + printf( "found btw forks\n" ); + } + if ( GetTrkOnElevPath( trk1, &e1 ) ) { + d1 += GetTrkLength( trk1, ep1, -1 ); + goto nextStep; + } + cnt2 = GetTrkEndPtCnt(trk1); + for ( ep2=0; ep2trk && n1->ep >= 0 ) { + /* no: make sure we are on the path */ + if ( n1->ep == ep1 ) { + ep2 = n1->ep2; + } else if ( n1->ep2 == ep1 ) { + ep2 = n1->ep; + } else { + return; } - d2 = GetTrkLength( trk1, ep1, ep2 )/2.0; - d1 += d2; - elistAppend( trk1, ep1, d1 ); - d1 += d2; - trk1 = trkN; - ep1 = epN; + trkN = GetTrkEndTrk(trk1,ep2); + epN = GetEndPtConnectedToMe( trkN, trk1 ); + break; } + } + if ( i2 >= forkCnt ) { + return; + } + } + d2 = GetTrkLength( trk1, ep1, ep2 )/2.0; + d1 += d2; + elistAppend( trk1, ep1, d1 ); + d1 += d2; + trk1 = trkN; + ep1 = epN; + } nextStep: - ASSERT(d1>0.0); - e1 = (e1-e)/d1; - trk1 = NULL; - i3 = elist_da.cnt; - for (i2=0; i20.0); + e1 = (e1-e)/d1; + trk1 = NULL; + i3 = elist_da.cnt; + for (i2=0; i2ep >= 0 ) + if ( n1->ep >= 0 ) { continue; + } trk = n1->trk; GetTrkOnElevPath( trk, &e ); cnt = GetTrkEndPtCnt(trk); @@ -762,7 +792,8 @@ static void PropogateForkElevs( void ) } } } -LOG( log_fillElev, 1, ( "%s: propogateForkElev (%ld)\n", elevPrefix, wGetTimer()-time0 ) ) + LOG( log_fillElev, 1, ( "%s: propogateForkElev (%ld)\n", elevPrefix, + wGetTimer()-time0 ) ) } static void PropogateDefElevs( void ) @@ -783,11 +814,14 @@ static void PropogateDefElevs( void ) dep = &defelev(i1); if (GetTrkOnElevPath( dep->trk, &e )) /* propogateForkElevs beat us to it */ + { continue; + } e = GetTrkEndElevHeight( dep->trk, dep->ep ); PropogateForkElev( dep->trk, dep->ep, 0, e ); } -LOG( log_fillElev, 1, ( "%s: propogateDefElevs [%d] (%ld)\n", elevPrefix, defelev_da.cnt, wGetTimer()-time0 ) ) + LOG( log_fillElev, 1, ( "%s: propogateDefElevs [%d] (%ld)\n", elevPrefix, + defelev_da.cnt, wGetTimer()-time0 ) ) } @@ -803,16 +837,16 @@ LOG( log_fillElev, 1, ( "%s: propogateDefElevs [%d] (%ld)\n", elevPrefix, defele */ typedef struct { - track_p trk; - coOrd pos; - DIST_T elev; - } pivot_t; + track_p trk; + coOrd pos; + DIST_T elev; +} pivot_t; static dynArr_t pivot_da; #define pivot(N) DYNARR_N(pivot_t, pivot_da, N) static void SurveyIsland( - track_p trk, - BOOL_T stopAtElevPath ) + track_p trk, + BOOL_T stopAtElevPath ) /* Find the tracks in this island and the pivots of this island * Outputs: * elist_da - tracks in the island @@ -833,8 +867,9 @@ static void SurveyIsland( SetTrkBits( trk, TB_PROCESSED ); for ( i1=0; i1 < elist_da.cnt; i1++ ) { trk = elist(i1).trk; - if ( GetTrkIndex(trk) == checkTrk ) + if ( GetTrkIndex(trk) == checkTrk ) { printf( "found in island\n" ); + } cnt = GetTrkEndPtCnt(trk); for ( ep=0; ep 0 ) { track_p trk; DIST_T elev; for ( trk=NULL; TrackIterate( &trk ); ) { printf( "T%4.4d = ", GetTrkIndex(trk) ); - if ( GetTrkOnElevPath( trk, &elev ) ) + if ( GetTrkOnElevPath( trk, &elev ) ) { printf( "%d:%0.2f\n", GetTrkElevMode(trk), elev ); - else + } else { printf( "noelev\n" ); + } EPINX_T ep; int mode; for ( ep=0; ep0.1) + if (diff>0.1) { ErrorMessage( MSG_JOIN_DIFFER_ELEV, PutDim(diff) ); + } } else if (mode0 == ELEV_DEF) { mode0 = GetTrkEndElevUnmaskedMode( trk0, ep0 ); elev = GetTrkEndElevHeight( trk0, ep0 ); @@ -1168,18 +1219,18 @@ EXPORT void SetTrkElevModes( BOOL_T connect, track_p trk0, EPINX_T ep0, track_p EXPORT void UpdateTrkEndElev( - track_p trk, - EPINX_T ep, - int newMode, - DIST_T newElev, - char * newStation ) + track_p trk, + EPINX_T ep, + int newMode, + DIST_T newElev, + char * newStation ) { int oldMode; DIST_T oldElev; char * oldStation; BOOL_T changed = TRUE; track_p trk1; - EPINX_T ep1; +// EPINX_T ep1; oldMode = GetTrkEndElevUnmaskedMode( trk, ep ); if ( (oldMode&ELEV_MASK) == (newMode&ELEV_MASK) ) { @@ -1187,8 +1238,9 @@ EXPORT void UpdateTrkEndElev( case ELEV_DEF: oldElev = GetTrkEndElevHeight( trk, ep ); if ( oldElev == newElev ) { - if ( oldMode == newMode ) + if ( oldMode == newMode ) { return; + } changed = FALSE; } break; @@ -1204,8 +1256,9 @@ EXPORT void UpdateTrkEndElev( } else { changed = TRUE; if ( (newMode&ELEV_MASK)==ELEV_DEF || (oldMode&ELEV_MASK)==ELEV_DEF || - (newMode&ELEV_MASK)==ELEV_IGNORE || (oldMode&ELEV_MASK)==ELEV_IGNORE ) + (newMode&ELEV_MASK)==ELEV_IGNORE || (oldMode&ELEV_MASK)==ELEV_IGNORE ) { changed = TRUE; + } } UndoModify( trk ); if ( (trk1 = GetTrkEndTrk( trk, ep )) ) { @@ -1217,7 +1270,7 @@ EXPORT void UpdateTrkEndElev( if ( changed ) { ClrTrkElev( trk ); if ( trk1 ) { - ep1 = GetEndPtConnectedToMe( trk1, trk ); +// ep1 = GetEndPtConnectedToMe( trk1, trk ); ClrTrkElev( trk1 ); } UpdateAllElevations(); @@ -1231,23 +1284,28 @@ static void SetTrkOnElevPath( track_p trk, int mode, DIST_T elev ) DIST_T oldElev; coOrd hi, lo; - if ( !GetTrkOnElevPath( trk, &oldElev ) ) + if ( !GetTrkOnElevPath( trk, &oldElev ) ) { oldElev = 0.0; + } GetBoundingBox( trk, &hi, &lo ); if ((labelEnable&LABELENABLE_TRACK_ELEV) && - labelScale >= mainD.scale && - (! OFF_MAIND( lo, hi ) ) && - (GetTrkVisible(trk) || drawTunnel!=0/*DRAW_TUNNEL_NONE*/) && - GetLayerVisible(GetTrkLayer(trk)) ) + labelScale >= mainD.scale && + (! OFF_MAIND( lo, hi ) ) && + (GetTrkVisible(trk) || drawTunnel!=0/*DRAW_TUNNEL_NONE*/) && + GetLayerVisible(GetTrkLayer(trk)) ) { redraw = TRUE; - - if ( (GetTrkBits(trk)&TB_ELEVPATH) && (oldElev == elev && oldMode == mode) ) + } + + if ( (GetTrkBits(trk)&TB_ELEVPATH) && (oldElev == elev && oldMode == mode) ) { return; - if ( redraw && (GetTrkBits(trk)&TB_ELEVPATH)) + } + if ( redraw && (GetTrkBits(trk)&TB_ELEVPATH)) { DrawTrackElev( trk, &mainD, FALSE ); + } SetTrkElev( trk, mode, elev ); - if ( redraw ) + if ( redraw ) { DrawTrackElev( trk, &mainD, TRUE ); + } } @@ -1260,40 +1318,45 @@ EXPORT void DrawTrackElev( track_cp trk, drawCmd_p d, BOOL_T drawIt ) coOrd lo, hi; if ( (!IsTrack(trk)) || - (!(labelEnable&LABELENABLE_TRACK_ELEV)) || - (d == &mapD) || - (labelScale < d->scale) || - (!GetTrkOnElevPath( trk, &elev )) || - ((GetTrkBits(trk)&TB_ELEVPATH) == 0) || - (d->options & DC_SIMPLE) != 0 ) + (!(labelEnable&LABELENABLE_TRACK_ELEV)) || + (d == &mapD) || + (labelScale < d->scale) || + (!GetTrkOnElevPath( trk, &elev )) || + ((GetTrkBits(trk)&TB_ELEVPATH) == 0) || + (d->options & DC_SIMPLE) != 0 ) { return; + } - if ( !GetCurveMiddle( trk, &pos ) && !GetCornuMiddle(trk, &pos) && !GetBezierMiddle(trk, &pos)) { + if ( !GetCurveMiddle( trk, &pos ) && !GetCornuMiddle(trk, &pos) + && !GetBezierMiddle(trk, &pos)) { GetBoundingBox( trk, &hi, &lo ); pos.x = (hi.x+lo.x)/2.0; pos.y = (hi.y+lo.y)/2.0; } - if ( d==&mainD && OFF_MAIND( pos, pos ) ) + if ( d==&mainD && OFF_MAIND( pos, pos ) ) { return; + } switch ( GetTrkElevMode(trk) ) { case ELEV_FORK: - color = drawColorBlue; + color = drawColorDkBlue; break; case ELEV_BRANCH: color = drawColorPurple; break; case ELEV_ISLAND: - color = drawColorGold; + color = drawColorDkAqua; break; case ELEV_ALONE: return; } - if ( !drawIt ) + if ( !drawIt ) { color = wDrawColorWhite; + } sprintf( message, "%s", FormatDistance(elev)); fp = wStandardFont( F_HELV, FALSE, FALSE ); - DrawBoxedString( BOX_INVERT, d, pos, message, fp, (wFontSize_t)descriptionFontSize, color, 0 ); + DrawBoxedString( BOX_INVERT, d, pos, message, fp, + (wFontSize_t)descriptionFontSize, color, 0 ); } -- cgit v1.2.3