[ff6cfe1] | 1 | /* netskel.c |
---|
[045c055] | 2 | * Survex network reduction - remove trailing traverses and concatenate |
---|
| 3 | * traverses between junctions |
---|
[fdf79f4] | 4 | * Copyright (C) 1991-2004,2005,2006,2010,2011,2012,2013,2014,2015 Olly Betts |
---|
[846746e] | 5 | * |
---|
[89231c4] | 6 | * This program is free software; you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | * (at your option) any later version. |
---|
[846746e] | 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
[846746e] | 15 | * |
---|
[89231c4] | 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program; if not, write to the Free Software |
---|
[d333899] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[045c055] | 19 | */ |
---|
| 20 | |
---|
[7fda0c8] | 21 | /* #define BLUNDER_DETECTION */ |
---|
[045c055] | 22 | |
---|
| 23 | #if 0 |
---|
| 24 | #define DEBUG_INVALID 1 |
---|
| 25 | #define VALIDATE 1 |
---|
[c30a8f3] | 26 | #define DUMP_NETWORK 1 |
---|
[045c055] | 27 | #endif |
---|
| 28 | |
---|
| 29 | #ifdef HAVE_CONFIG_H |
---|
| 30 | # include <config.h> |
---|
| 31 | #endif |
---|
| 32 | |
---|
| 33 | #include "validate.h" |
---|
| 34 | #include "debug.h" |
---|
| 35 | #include "cavern.h" |
---|
| 36 | #include "filename.h" |
---|
| 37 | #include "message.h" |
---|
| 38 | #include "filelist.h" |
---|
[a405bc1] | 39 | #include "img_hosted.h" |
---|
[e329f0f] | 40 | #include "netartic.h" |
---|
[045c055] | 41 | #include "netbits.h" |
---|
| 42 | #include "netskel.h" |
---|
| 43 | #include "network.h" |
---|
| 44 | #include "out.h" |
---|
| 45 | |
---|
| 46 | #define sqrdd(X) (sqrd((X)[0]) + sqrd((X)[1]) + sqrd((X)[2])) |
---|
| 47 | |
---|
| 48 | typedef struct Stack { |
---|
| 49 | struct Link *join1, *join2; |
---|
| 50 | struct Stack *next; |
---|
| 51 | } stack; |
---|
| 52 | |
---|
| 53 | typedef struct StackTr { |
---|
| 54 | struct Link *join1; |
---|
| 55 | struct StackTr *next; |
---|
| 56 | } stackTrail; |
---|
| 57 | |
---|
| 58 | /* string used between things in traverse printouts eg \1 - \2 - \3 -...*/ |
---|
| 59 | static const char *szLink = " - "; |
---|
| 60 | static const char *szLinkEq = " = "; /* use this one for equates */ |
---|
| 61 | |
---|
| 62 | #if 0 |
---|
| 63 | #define fprint_prefix(FH, NAME) BLK((fprint_prefix)((FH), (NAME));\ |
---|
[421b7d2] | 64 | fprintf((FH), " [%p]", (void*)(NAME)); ) |
---|
[045c055] | 65 | #endif |
---|
| 66 | |
---|
| 67 | static stack *ptr; /* Ptr to TRaverse linked list for in-between travs */ |
---|
| 68 | static stackTrail *ptrTrail; /* Ptr to TRaverse linked list for trail travs*/ |
---|
| 69 | |
---|
| 70 | static void remove_trailing_travs(void); |
---|
| 71 | static void remove_travs(void); |
---|
| 72 | static void replace_travs(void); |
---|
| 73 | static void replace_trailing_travs(void); |
---|
[ee05463] | 74 | static void write_passage_models(void); |
---|
[045c055] | 75 | |
---|
| 76 | static void concatenate_trav(node *stn, int i); |
---|
| 77 | |
---|
| 78 | static void err_stat(int cLegsTrav, double lenTrav, |
---|
| 79 | double eTot, double eTotTheo, |
---|
| 80 | double hTot, double hTotTheo, |
---|
| 81 | double vTot, double vTotTheo); |
---|
| 82 | |
---|
| 83 | extern void |
---|
| 84 | solve_network(void /*node *stnlist*/) |
---|
| 85 | { |
---|
| 86 | static int first_solve = 1; |
---|
| 87 | node *stn; |
---|
| 88 | |
---|
[fdf79f4] | 89 | /* We can't average across solving to fix positions. */ |
---|
| 90 | clear_last_leg(); |
---|
| 91 | |
---|
[dfe4a520] | 92 | if (stnlist == NULL) { |
---|
| 93 | if (first_solve) fatalerror(/*No survey data*/43); |
---|
| 94 | /* We've had a *solve followed by another *solve (or the implicit |
---|
| 95 | * *solve at the end of the data. Don't moan about that. */ |
---|
| 96 | return; |
---|
| 97 | } |
---|
[045c055] | 98 | ptr = NULL; |
---|
| 99 | ptrTrail = NULL; |
---|
| 100 | dump_network(); |
---|
| 101 | |
---|
[3b066f6] | 102 | if (first_solve && !pcs->proj && !proj_out) { |
---|
| 103 | /* If we haven't already solved to find some station positions, and |
---|
| 104 | * there's no specified coordinate system, then check if there are any |
---|
| 105 | * fixed points, and if there aren't, invent one at (0,0,0). |
---|
| 106 | * |
---|
| 107 | * We do this first so the solving part is just like the standard case - |
---|
| 108 | * this avoid problems, such as sub-nodes of the invented fix having been |
---|
| 109 | * removed. It also means we can fix the "first" station, which makes |
---|
| 110 | * more sense to the user. */ |
---|
| 111 | FOR_EACH_STN(stn, stnlist) |
---|
| 112 | if (fixed(stn)) break; |
---|
| 113 | |
---|
| 114 | if (!stn) { |
---|
| 115 | /* If we've had a *solve and all the new survey since then is hanging, |
---|
| 116 | * we don't want to invent a fixed point. We want to complain but |
---|
| 117 | * the easiest way to is just to continue processing and let |
---|
| 118 | * articulate() catch this condition as it will any hanging survey |
---|
| 119 | * data. */ |
---|
| 120 | node *stnFirst = NULL; |
---|
| 121 | |
---|
| 122 | /* New stations are pushed onto the head of the list, so the |
---|
| 123 | * first station added is the last in the list. */ |
---|
| 124 | FOR_EACH_STN(stn, stnlist) { |
---|
| 125 | /* Prefer a station with legs attached when choosing one to fix |
---|
| 126 | * so that if there's a hanging station on a nosurvey leg we pick |
---|
| 127 | * the main clump of survey data. */ |
---|
| 128 | if (stnFirst && !stnFirst->leg[0]) continue; |
---|
| 129 | stnFirst = stn; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /* If we've got nosurvey legs, then the station we find to fix could |
---|
| 133 | * have no real legs attached. */ |
---|
| 134 | SVX_ASSERT2(nosurveyhead || stnFirst->leg[0], |
---|
| 135 | "no fixed stns, but we've got a zero node!"); |
---|
| 136 | SVX_ASSERT2(stnFirst, "no stations left in net!"); |
---|
| 137 | stn = stnFirst; |
---|
| 138 | printf(msg(/*Survey has no fixed points. Therefore I’ve fixed %s at (0,0,0)*/72), |
---|
| 139 | sprint_prefix(stn->name)); |
---|
| 140 | putnl(); |
---|
| 141 | POS(stn,0) = (real)0.0; |
---|
| 142 | POS(stn,1) = (real)0.0; |
---|
| 143 | POS(stn,2) = (real)0.0; |
---|
| 144 | fix(stn); |
---|
[1fa9b83] | 145 | } |
---|
[045c055] | 146 | } |
---|
| 147 | |
---|
[c30a8f3] | 148 | first_solve = 0; |
---|
| 149 | |
---|
[045c055] | 150 | remove_trailing_travs(); |
---|
| 151 | validate(); dump_network(); |
---|
| 152 | remove_travs(); |
---|
| 153 | validate(); dump_network(); |
---|
| 154 | remove_subnets(); |
---|
| 155 | validate(); dump_network(); |
---|
[7222b04] | 156 | articulate(); |
---|
[045c055] | 157 | validate(); dump_network(); |
---|
| 158 | replace_subnets(); |
---|
| 159 | validate(); dump_network(); |
---|
| 160 | replace_travs(); |
---|
| 161 | validate(); dump_network(); |
---|
| 162 | replace_trailing_travs(); |
---|
| 163 | validate(); dump_network(); |
---|
[ee05463] | 164 | |
---|
| 165 | /* Now write out any passage models. */ |
---|
| 166 | write_passage_models(); |
---|
[045c055] | 167 | } |
---|
| 168 | |
---|
| 169 | static void |
---|
| 170 | remove_trailing_travs(void) |
---|
| 171 | { |
---|
| 172 | node *stn; |
---|
[736f7df] | 173 | /* TRANSLATORS: In French, Eric chose to use the terminology used by |
---|
| 174 | * toporobot: "sequence" for the English "traverse", which makes sense |
---|
| 175 | * (although toporobot actually uses this term to mean something more |
---|
| 176 | * specific). Feel free to follow this lead if you can't think of a better |
---|
| 177 | * term - these messages mostly indicate how processing is progressing. |
---|
[b49ac56] | 178 | * |
---|
[736f7df] | 179 | * A trailing traverse is a dead end back to a junction. */ |
---|
[045c055] | 180 | out_current_action(msg(/*Removing trailing traverses*/125)); |
---|
| 181 | FOR_EACH_STN(stn, stnlist) { |
---|
| 182 | if (!fixed(stn) && one_node(stn)) { |
---|
| 183 | int i = 0; |
---|
| 184 | int j; |
---|
| 185 | node *stn2 = stn; |
---|
| 186 | stackTrail *trav; |
---|
| 187 | |
---|
| 188 | #if PRINT_NETBITS |
---|
| 189 | printf("Removed trailing trav "); |
---|
| 190 | #endif |
---|
| 191 | do { |
---|
| 192 | struct Link *leg; |
---|
| 193 | #if PRINT_NETBITS |
---|
[e27a0c3] | 194 | print_prefix(stn2->name); printf("<%p>",stn2); fputs(szLink, stdout); |
---|
[045c055] | 195 | #endif |
---|
| 196 | remove_stn_from_list(&stnlist, stn2); |
---|
| 197 | leg = stn2->leg[i]; |
---|
| 198 | j = reverse_leg_dirn(leg); |
---|
| 199 | stn2 = leg->l.to; |
---|
| 200 | i = j ^ 1; /* flip direction for other leg of 2 node */ |
---|
| 201 | /* stop if fixed or 3 or 1 node */ |
---|
| 202 | } while (two_node(stn2) && !fixed(stn2)); |
---|
[cb3d1e2] | 203 | |
---|
[045c055] | 204 | /* put traverse on stack */ |
---|
| 205 | trav = osnew(stackTrail); |
---|
| 206 | trav->join1 = stn2->leg[j]; |
---|
| 207 | trav->next = ptrTrail; |
---|
| 208 | ptrTrail = trav; |
---|
| 209 | |
---|
| 210 | /* We want to keep all 2-nodes using legs 0 and 1 and all one nodes |
---|
| 211 | * using leg 0 so we may need to swap leg j with leg 2 (for a 3 node) |
---|
| 212 | * or leg 1 (for a fixed 2 node) */ |
---|
[421b7d2] | 213 | if ((j == 0 && !one_node(stn2)) || (j == 1 && three_node(stn2))) { |
---|
[045c055] | 214 | /* i is the direction to swap with */ |
---|
[eb18f4d] | 215 | i = (three_node(stn2)) ? 2 : 1; |
---|
[045c055] | 216 | /* change the other direction of leg i to use leg j */ |
---|
| 217 | reverse_leg(stn2->leg[i])->l.reverse += j - i; |
---|
| 218 | stn2->leg[j] = stn2->leg[i]; |
---|
| 219 | j = i; |
---|
| 220 | } |
---|
| 221 | stn2->leg[j] = NULL; |
---|
| 222 | |
---|
| 223 | #if PRINT_NETBITS |
---|
| 224 | print_prefix(stn2->name); printf("<%p>",stn2); putnl(); |
---|
| 225 | #endif |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | static void |
---|
| 231 | remove_travs(void) |
---|
| 232 | { |
---|
| 233 | node *stn; |
---|
[736f7df] | 234 | /* TRANSLATORS: In French, Eric chose to use the terminology used by |
---|
| 235 | * toporobot: "sequence" for the English "traverse", which makes sense |
---|
| 236 | * (although toporobot actually uses this term to mean something more |
---|
| 237 | * specific). Feel free to follow this lead if you can't think of a better |
---|
| 238 | * term - these messages mostly indicate how processing is progressing. */ |
---|
[c20d521] | 239 | out_current_action(msg(/*Concatenating traverses*/126)); |
---|
[045c055] | 240 | FOR_EACH_STN(stn, stnlist) { |
---|
[4a0d231] | 241 | if (fixed(stn) || three_node(stn)) { |
---|
[ec8a439] | 242 | int d; |
---|
| 243 | for (d = 0; d <= 2; d++) { |
---|
| 244 | linkfor *leg = stn->leg[d]; |
---|
| 245 | if (leg && !(leg->l.reverse & FLAG_REPLACEMENTLEG)) |
---|
| 246 | concatenate_trav(stn, d); |
---|
| 247 | } |
---|
[045c055] | 248 | } |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | static void |
---|
| 253 | concatenate_trav(node *stn, int i) |
---|
| 254 | { |
---|
| 255 | int j; |
---|
| 256 | stack *trav; |
---|
| 257 | node *stn2; |
---|
| 258 | linkfor *newleg, *newleg2; |
---|
| 259 | |
---|
| 260 | stn2 = stn->leg[i]->l.to; |
---|
| 261 | /* Reject single legs as they may be already concatenated traverses */ |
---|
| 262 | if (fixed(stn2) || !two_node(stn2)) return; |
---|
| 263 | |
---|
| 264 | trav = osnew(stack); |
---|
| 265 | newleg2 = (linkfor*)osnew(linkrev); |
---|
| 266 | |
---|
| 267 | #if PRINT_NETBITS |
---|
| 268 | printf("Concatenating trav "); print_prefix(stn->name); printf("<%p>",stn); |
---|
| 269 | #endif |
---|
| 270 | |
---|
| 271 | newleg2->l.to = stn; |
---|
| 272 | newleg2->l.reverse = i | FLAG_REPLACEMENTLEG; |
---|
| 273 | trav->join1 = stn->leg[i]; |
---|
| 274 | |
---|
| 275 | j = reverse_leg_dirn(stn->leg[i]); |
---|
[4c07c51] | 276 | SVX_ASSERT(j == 0 || j == 1); |
---|
[045c055] | 277 | |
---|
| 278 | newleg = copy_link(stn->leg[i]); |
---|
| 279 | |
---|
[cb3d1e2] | 280 | while (1) { |
---|
[045c055] | 281 | stn = stn2; |
---|
| 282 | |
---|
| 283 | #if PRINT_NETBITS |
---|
[e27a0c3] | 284 | fputs(szLink, stdout); print_prefix(stn->name); printf("<%p>",stn); |
---|
[045c055] | 285 | #endif |
---|
[cb3d1e2] | 286 | |
---|
[045c055] | 287 | /* stop if fixed or 3 or 1 node */ |
---|
| 288 | if (fixed(stn) || !two_node(stn)) break; |
---|
[cb3d1e2] | 289 | |
---|
[045c055] | 290 | remove_stn_from_list(&stnlist, stn); |
---|
| 291 | |
---|
| 292 | i = j ^ 1; /* flip direction for other leg of 2 node */ |
---|
| 293 | |
---|
| 294 | stn2 = stn->leg[i]->l.to; |
---|
| 295 | j = reverse_leg_dirn(stn->leg[i]); |
---|
| 296 | |
---|
| 297 | addto_link(newleg, stn->leg[i]); |
---|
| 298 | } |
---|
[cb3d1e2] | 299 | |
---|
[045c055] | 300 | trav->join2 = stn->leg[j]; |
---|
| 301 | trav->next = ptr; |
---|
| 302 | ptr = trav; |
---|
| 303 | |
---|
| 304 | newleg->l.to = stn; |
---|
[4a0d231] | 305 | newleg->l.reverse = j | FLAG_DATAHERE | FLAG_REPLACEMENTLEG; |
---|
[045c055] | 306 | |
---|
[ec8a439] | 307 | newleg2->l.to->leg[reverse_leg_dirn(newleg2)] = newleg; |
---|
| 308 | /* i.e. stn->leg[i] = newleg; with original stn and i */ |
---|
| 309 | |
---|
[045c055] | 310 | stn->leg[j] = newleg2; |
---|
| 311 | |
---|
| 312 | #if PRINT_NETBITS |
---|
| 313 | putchar(' '); |
---|
[be97baf] | 314 | print_var(&(newleg->v)); |
---|
[045c055] | 315 | printf("\nStacked "); |
---|
| 316 | print_prefix(newleg2->l.to->name); |
---|
| 317 | printf(",%d-", reverse_leg_dirn(newleg2)); |
---|
| 318 | print_prefix(stn->name); |
---|
| 319 | printf(",%d\n", j); |
---|
| 320 | #endif |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | #ifdef BLUNDER_DETECTION |
---|
| 324 | /* expected_error is actually squared... */ |
---|
[25ab06b] | 325 | /* only called if fhErrStat != NULL */ |
---|
[045c055] | 326 | static void |
---|
[eb18f4d] | 327 | do_gross(delta e, delta v, node *stn1, node *stn2, double expected_error) |
---|
[cb3d1e2] | 328 | { |
---|
[045c055] | 329 | double hsqrd, rsqrd, s, cx, cy, cz; |
---|
| 330 | double tot; |
---|
| 331 | int i; |
---|
| 332 | int output = 0; |
---|
[647407d] | 333 | prefix *name1 = stn1->name, *name2 = stn2->name; |
---|
[045c055] | 334 | |
---|
| 335 | #if 0 |
---|
| 336 | printf( "e = ( %.2f, %.2f, %.2f )", e[0], e[1], e[2] ); |
---|
| 337 | printf( " v = ( %.2f, %.2f, %.2f )\n", v[0], v[1], v[2] ); |
---|
| 338 | #endif |
---|
| 339 | hsqrd = sqrd(v[0]) + sqrd(v[1]); |
---|
| 340 | rsqrd = hsqrd + sqrd(v[2]); |
---|
| 341 | if (rsqrd == 0.0) return; |
---|
| 342 | |
---|
| 343 | cx = v[0] + e[0]; |
---|
| 344 | cy = v[1] + e[1]; |
---|
| 345 | cz = v[2] + e[2]; |
---|
| 346 | |
---|
| 347 | s = (e[0] * v[0] + e[1] * v[1] + e[2] * v[2]) / rsqrd; |
---|
| 348 | tot = 0; |
---|
| 349 | for (i = 2; i >= 0; i--) tot += sqrd(e[i] - v[i] * s); |
---|
| 350 | |
---|
[25ab06b] | 351 | if (tot <= expected_error) { |
---|
[045c055] | 352 | if (!output) { |
---|
| 353 | fprint_prefix(fhErrStat, name1); |
---|
| 354 | fputs("->", fhErrStat); |
---|
| 355 | fprint_prefix(fhErrStat, name2); |
---|
| 356 | } |
---|
[647407d] | 357 | fprintf(fhErrStat, " L: %.2f", sqrt(tot)); |
---|
| 358 | /* checked - works */ |
---|
| 359 | fprintf(fhErrStat, " (%.2fm -> %.2fm)", sqrt(sqrdd(v)), sqrt(sqrdd(v)) * (1 - s)); |
---|
[045c055] | 360 | output = 1; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | s = sqrd(cx) + sqrd(cy); |
---|
| 364 | if (s > 0.0) { |
---|
| 365 | s = hsqrd / s; |
---|
[4c07c51] | 366 | SVX_ASSERT(s >= 0.0); |
---|
[647407d] | 367 | s = sqrt(s); |
---|
[045c055] | 368 | s = 1 - s; |
---|
| 369 | tot = sqrd(cx * s) + sqrd(cy * s) + sqrd(e[2]); |
---|
[25ab06b] | 370 | if (tot <= expected_error) { |
---|
[647407d] | 371 | double newval, oldval; |
---|
[045c055] | 372 | if (!output) { |
---|
| 373 | fprint_prefix(fhErrStat, name1); |
---|
| 374 | fputs("->", fhErrStat); |
---|
| 375 | fprint_prefix(fhErrStat, name2); |
---|
| 376 | } |
---|
[647407d] | 377 | fprintf(fhErrStat, " B: %.2f", sqrt(tot)); |
---|
| 378 | /* checked - works */ |
---|
| 379 | newval = deg(atan2(cx, cy)); |
---|
| 380 | if (newval < 0) newval += 360; |
---|
| 381 | oldval = deg(atan2(v[0], v[1])); |
---|
| 382 | if (oldval < 0) oldval += 360; |
---|
| 383 | fprintf(fhErrStat, " (%.2fdeg -> %.2fdeg)", oldval, newval); |
---|
[045c055] | 384 | output = 1; |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | if (hsqrd > 0.0) { |
---|
| 389 | double nx, ny; |
---|
| 390 | s = (e[0] * v[1] - e[1] * v[0]) / hsqrd; |
---|
| 391 | nx = cx - s * v[1]; |
---|
| 392 | ny = cy + s * v[0]; |
---|
| 393 | s = sqrd(nx) + sqrd(ny) + sqrd(cz); |
---|
| 394 | if (s > 0.0) { |
---|
[421b7d2] | 395 | s = rsqrd / s; |
---|
[4c07c51] | 396 | SVX_ASSERT(s >= 0); |
---|
[421b7d2] | 397 | s = sqrt(s); |
---|
| 398 | tot = sqrd(cx - s * nx) + sqrd(cy - s * ny) + sqrd(cz - s * cz); |
---|
[25ab06b] | 399 | if (tot <= expected_error) { |
---|
[045c055] | 400 | if (!output) { |
---|
| 401 | fprint_prefix(fhErrStat, name1); |
---|
| 402 | fputs("->", fhErrStat); |
---|
| 403 | fprint_prefix(fhErrStat, name2); |
---|
| 404 | } |
---|
[647407d] | 405 | fprintf(fhErrStat, " G: %.2f", sqrt(tot)); |
---|
| 406 | /* checked - works */ |
---|
| 407 | fprintf(fhErrStat, " (%.2fdeg -> %.2fdeg)", |
---|
| 408 | deg(atan2(v[2], sqrt(v[0] * v[0] + v[1] * v[1]))), |
---|
| 409 | deg(atan2(cz, sqrt(nx * nx + ny * ny)))); |
---|
[045c055] | 410 | output = 1; |
---|
| 411 | } |
---|
| 412 | } |
---|
| 413 | } |
---|
[25ab06b] | 414 | if (output) fputnl(fhErrStat); |
---|
[045c055] | 415 | } |
---|
| 416 | #endif |
---|
| 417 | |
---|
| 418 | static void |
---|
| 419 | replace_travs(void) |
---|
| 420 | { |
---|
| 421 | stack *ptrOld; |
---|
| 422 | node *stn1, *stn2, *stn3; |
---|
| 423 | int i, j, k; |
---|
[407084d] | 424 | double eTot = 0, lenTrav = 0, lenTot; |
---|
| 425 | double eTotTheo = 0; |
---|
| 426 | double vTot = 0, vTotTheo = 0, hTot = 0, hTotTheo = 0; |
---|
[eb18f4d] | 427 | delta e, sc; |
---|
[045c055] | 428 | bool fEquate; /* used to indicate equates in output */ |
---|
[407084d] | 429 | int cLegsTrav = 0; |
---|
[402c753] | 430 | bool fArtic; |
---|
[045c055] | 431 | |
---|
[736f7df] | 432 | /* TRANSLATORS: In French, Eric chose to use the terminology used by |
---|
| 433 | * toporobot: "sequence" for the English "traverse", which makes sense |
---|
| 434 | * (although toporobot actually uses this term to mean something more |
---|
| 435 | * specific). Feel free to follow this lead if you can't think of a better |
---|
| 436 | * term - these messages mostly indicate how processing is progressing. */ |
---|
[c20d521] | 437 | out_current_action(msg(/*Calculating traverses*/127)); |
---|
[045c055] | 438 | |
---|
[647407d] | 439 | if (!fhErrStat && !fSuppress) |
---|
[045c055] | 440 | fhErrStat = safe_fopen_with_ext(fnm_output_base, EXT_SVX_ERRS, "w"); |
---|
| 441 | |
---|
[693388e] | 442 | if (!pimg) { |
---|
[a4ae909] | 443 | char *fnm = add_ext(fnm_output_base, EXT_SVX_3D); |
---|
[06a871f] | 444 | filename_register_output(fnm); |
---|
[84d5946] | 445 | pimg = img_open_write_cs(fnm, survey_title, proj_str_out, 0); |
---|
[a4ae909] | 446 | if (!pimg) fatalerror(img_error(), fnm); |
---|
[06a871f] | 447 | osfree(fnm); |
---|
[045c055] | 448 | } |
---|
| 449 | |
---|
| 450 | /* First do all the one leg traverses */ |
---|
| 451 | FOR_EACH_STN(stn1, stnlist) { |
---|
[be97baf] | 452 | #if PRINT_NETBITS |
---|
| 453 | printf("One leg traverses from "); |
---|
| 454 | print_prefix(stn1->name); |
---|
| 455 | printf(" [%p]\n", stn1); |
---|
| 456 | #endif |
---|
[045c055] | 457 | for (i = 0; i <= 2; i++) { |
---|
| 458 | linkfor *leg = stn1->leg[i]; |
---|
| 459 | if (leg && data_here(leg) && |
---|
[693388e] | 460 | !(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) { |
---|
[4c07c51] | 461 | SVX_ASSERT(fixed(stn1)); |
---|
| 462 | SVX_ASSERT(!fZeros(&leg->v)); |
---|
[693388e] | 463 | |
---|
[e6cfe52] | 464 | stn2 = leg->l.to; |
---|
| 465 | if (TSTBIT(leg->l.flags, FLAGS_SURFACE)) { |
---|
| 466 | stn1->name->sflags |= BIT(SFLAGS_SURFACE); |
---|
| 467 | stn2->name->sflags |= BIT(SFLAGS_SURFACE); |
---|
| 468 | } else { |
---|
| 469 | stn1->name->sflags |= BIT(SFLAGS_UNDERGROUND); |
---|
| 470 | stn2->name->sflags |= BIT(SFLAGS_UNDERGROUND); |
---|
| 471 | } |
---|
[a4ae909] | 472 | img_write_item(pimg, img_MOVE, 0, NULL, |
---|
| 473 | POS(stn1, 0), POS(stn1, 1), POS(stn1, 2)); |
---|
| 474 | if (leg->meta) { |
---|
[1ee204e] | 475 | pimg->days1 = leg->meta->days1; |
---|
| 476 | pimg->days2 = leg->meta->days2; |
---|
[a4ae909] | 477 | } else { |
---|
[1ee204e] | 478 | pimg->days1 = pimg->days2 = -1; |
---|
[e6cfe52] | 479 | } |
---|
[eb5aea0] | 480 | pimg->style = (leg->l.flags >> FLAGS_STYLE_BIT0) & 0x07; |
---|
| 481 | img_write_item(pimg, img_LINE, leg->l.flags & FLAGS_MASK, |
---|
[a4ae909] | 482 | sprint_prefix(stn1->name->up), |
---|
| 483 | POS(stn2, 0), POS(stn2, 1), POS(stn2, 2)); |
---|
[e6cfe52] | 484 | if (!(leg->l.reverse & FLAG_ARTICULATION)) { |
---|
[25ab06b] | 485 | #ifdef BLUNDER_DETECTION |
---|
[e6cfe52] | 486 | delta err; |
---|
| 487 | int do_blunder; |
---|
[25ab06b] | 488 | #else |
---|
[e6cfe52] | 489 | if (fhErrStat) { |
---|
| 490 | fprint_prefix(fhErrStat, stn1->name); |
---|
| 491 | fputs(szLink, fhErrStat); |
---|
| 492 | fprint_prefix(fhErrStat, stn2->name); |
---|
| 493 | } |
---|
[402c753] | 494 | #endif |
---|
[e6cfe52] | 495 | subdd(&e, &POSD(stn2), &POSD(stn1)); |
---|
| 496 | subdd(&e, &e, &leg->d); |
---|
| 497 | if (fhErrStat) { |
---|
| 498 | eTot = sqrdd(e); |
---|
| 499 | hTot = sqrd(e[0]) + sqrd(e[1]); |
---|
| 500 | vTot = sqrd(e[2]); |
---|
[045c055] | 501 | #ifndef NO_COVARIANCES |
---|
[f795df0] | 502 | /* FIXME: what about covariances? */ |
---|
[e6cfe52] | 503 | hTotTheo = leg->v[0] + leg->v[1]; |
---|
| 504 | vTotTheo = leg->v[2]; |
---|
[0a208f9] | 505 | eTotTheo = hTotTheo + vTotTheo; |
---|
[045c055] | 506 | #else |
---|
[e6cfe52] | 507 | hTotTheo = leg->v[0] + leg->v[1]; |
---|
| 508 | vTotTheo = leg->v[2]; |
---|
[f7c455c] | 509 | eTotTheo = hTotTheo + vTotTheo; |
---|
[0a208f9] | 510 | #endif |
---|
[647407d] | 511 | #ifdef BLUNDER_DETECTION |
---|
[a8c4cfa] | 512 | memcpy(&err, &e, sizeof(delta)); |
---|
[e6cfe52] | 513 | do_blunder = (eTot > eTotTheo); |
---|
| 514 | fputs("\ntraverse ", fhErrStat); |
---|
| 515 | fprint_prefix(fhErrStat, stn1->name); |
---|
| 516 | fputs("->", fhErrStat); |
---|
| 517 | fprint_prefix(fhErrStat, stn2->name); |
---|
| 518 | fprintf(fhErrStat, " e=(%.2f, %.2f, %.2f) mag=%.2f %s\n", |
---|
| 519 | e[0], e[1], e[2], sqrt(eTot), |
---|
| 520 | (do_blunder ? "suspect:" : "OK")); |
---|
| 521 | if (do_blunder) |
---|
| 522 | do_gross(err, leg->d, stn1, stn2, eTotTheo); |
---|
| 523 | #endif |
---|
| 524 | err_stat(1, sqrt(sqrdd(leg->d)), eTot, eTotTheo, |
---|
| 525 | hTot, hTotTheo, vTot, vTotTheo); |
---|
[cb3d1e2] | 526 | } |
---|
[045c055] | 527 | } |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | while (ptr != NULL) { |
---|
| 533 | /* work out where traverse should be reconnected */ |
---|
[eb18f4d] | 534 | linkfor *leg = ptr->join1; |
---|
[045c055] | 535 | leg = reverse_leg(leg); |
---|
| 536 | stn1 = leg->l.to; |
---|
| 537 | i = reverse_leg_dirn(leg); |
---|
| 538 | |
---|
| 539 | leg = ptr->join2; |
---|
| 540 | leg = reverse_leg(leg); |
---|
| 541 | stn2 = leg->l.to; |
---|
| 542 | j = reverse_leg_dirn(leg); |
---|
| 543 | |
---|
| 544 | #if PRINT_NETBITS |
---|
| 545 | printf(" Trav "); |
---|
| 546 | print_prefix(stn1->name); |
---|
[be97baf] | 547 | printf("<%p>[%d]%s...%s", stn1, i, szLink, szLink); |
---|
[045c055] | 548 | print_prefix(stn2->name); |
---|
[be97baf] | 549 | printf("<%p>[%d]\n", stn2, j); |
---|
[045c055] | 550 | #endif |
---|
[e6cfe52] | 551 | |
---|
[4c07c51] | 552 | SVX_ASSERT(fixed(stn1)); |
---|
| 553 | SVX_ASSERT(fixed(stn2)); |
---|
[e6cfe52] | 554 | |
---|
[045c055] | 555 | /* calculate scaling factors for error distribution */ |
---|
[e6cfe52] | 556 | eTot = 0.0; |
---|
| 557 | hTot = vTot = 0.0; |
---|
[4c07c51] | 558 | SVX_ASSERT(data_here(stn1->leg[i])); |
---|
[e6cfe52] | 559 | if (fZeros(&stn1->leg[i]->v)) { |
---|
| 560 | sc[0] = sc[1] = sc[2] = 0.0; |
---|
| 561 | } else { |
---|
| 562 | subdd(&e, &POSD(stn2), &POSD(stn1)); |
---|
| 563 | subdd(&e, &e, &stn1->leg[i]->d); |
---|
| 564 | eTot = sqrdd(e); |
---|
| 565 | hTot = sqrd(e[0]) + sqrd(e[1]); |
---|
| 566 | vTot = sqrd(e[2]); |
---|
| 567 | divds(&sc, &e, &stn1->leg[i]->v); |
---|
| 568 | } |
---|
[045c055] | 569 | #ifndef NO_COVARIANCES |
---|
[f795df0] | 570 | /* FIXME: what about covariances? */ |
---|
[e6cfe52] | 571 | hTotTheo = stn1->leg[i]->v[0] + stn1->leg[i]->v[1]; |
---|
| 572 | vTotTheo = stn1->leg[i]->v[2]; |
---|
[045c055] | 573 | #else |
---|
[e6cfe52] | 574 | hTotTheo = stn1->leg[i]->v[0] + stn1->leg[i]->v[1]; |
---|
| 575 | vTotTheo = stn1->leg[i]->v[2]; |
---|
[045c055] | 576 | #endif |
---|
[f7c455c] | 577 | eTotTheo = hTotTheo + vTotTheo; |
---|
[e6cfe52] | 578 | cLegsTrav = 0; |
---|
| 579 | lenTrav = 0.0; |
---|
[a4ae909] | 580 | img_write_item(pimg, img_MOVE, 0, NULL, |
---|
| 581 | POS(stn1, 0), POS(stn1, 1), POS(stn1, 2)); |
---|
[e6cfe52] | 582 | |
---|
[402c753] | 583 | fArtic = stn1->leg[i]->l.reverse & FLAG_ARTICULATION; |
---|
[045c055] | 584 | osfree(stn1->leg[i]); |
---|
| 585 | stn1->leg[i] = ptr->join1; /* put old link back in */ |
---|
| 586 | |
---|
| 587 | osfree(stn2->leg[j]); |
---|
| 588 | stn2->leg[j] = ptr->join2; /* and the other end */ |
---|
| 589 | |
---|
| 590 | #ifdef BLUNDER_DETECTION |
---|
[e6cfe52] | 591 | delta err; |
---|
| 592 | int do_blunder; |
---|
[a8c4cfa] | 593 | memcpy(&err, &e, sizeof(delta)); |
---|
[e6cfe52] | 594 | do_blunder = (eTot > eTotTheo); |
---|
| 595 | if (fhErrStat && !fArtic) { |
---|
| 596 | fputs("\ntraverse ", fhErrStat); |
---|
| 597 | fprint_prefix(fhErrStat, stn1->name); |
---|
| 598 | fputs("->", fhErrStat); |
---|
| 599 | fprint_prefix(fhErrStat, stn2->name); |
---|
| 600 | fprintf(fhErrStat, " e=(%.2f, %.2f, %.2f) mag=%.2f %s\n", |
---|
| 601 | e[0], e[1], e[2], sqrt(eTot), |
---|
| 602 | (do_blunder ? "suspect:" : "OK")); |
---|
| 603 | } |
---|
[045c055] | 604 | #endif |
---|
[e6cfe52] | 605 | while (fTrue) { |
---|
| 606 | int reached_end; |
---|
[693388e] | 607 | prefix *leg_pfx; |
---|
[421b7d2] | 608 | |
---|
[e6cfe52] | 609 | fEquate = fTrue; |
---|
| 610 | /* get next node in traverse |
---|
| 611 | * should have stn3->leg[k]->l.to == stn1 */ |
---|
| 612 | stn3 = stn1->leg[i]->l.to; |
---|
| 613 | k = reverse_leg_dirn(stn1->leg[i]); |
---|
[4c07c51] | 614 | SVX_ASSERT2(stn3->leg[k]->l.to == stn1, |
---|
[e6cfe52] | 615 | "reverse leg doesn't reciprocate"); |
---|
[421b7d2] | 616 | |
---|
[e6cfe52] | 617 | reached_end = (stn3 == stn2 && k == j); |
---|
[421b7d2] | 618 | |
---|
[e6cfe52] | 619 | if (data_here(stn1->leg[i])) { |
---|
[693388e] | 620 | leg_pfx = stn1->name->up; |
---|
[e6cfe52] | 621 | leg = stn1->leg[i]; |
---|
[045c055] | 622 | #ifdef BLUNDER_DETECTION |
---|
[e6cfe52] | 623 | if (do_blunder && fhErrStat) |
---|
| 624 | do_gross(err, leg->d, stn1, stn3, eTotTheo); |
---|
[045c055] | 625 | #endif |
---|
[e6cfe52] | 626 | if (!reached_end) |
---|
| 627 | adddd(&POSD(stn3), &POSD(stn1), &leg->d); |
---|
| 628 | } else { |
---|
[693388e] | 629 | leg_pfx = stn3->name->up; |
---|
[e6cfe52] | 630 | leg = stn3->leg[k]; |
---|
[045c055] | 631 | #ifdef BLUNDER_DETECTION |
---|
[e6cfe52] | 632 | if (do_blunder && fhErrStat) |
---|
| 633 | do_gross(err, leg->d, stn1, stn3, eTotTheo); |
---|
[045c055] | 634 | #endif |
---|
[e6cfe52] | 635 | if (!reached_end) |
---|
| 636 | subdd(&POSD(stn3), &POSD(stn1), &leg->d); |
---|
| 637 | } |
---|
[421b7d2] | 638 | |
---|
[e6cfe52] | 639 | lenTot = sqrdd(leg->d); |
---|
[421b7d2] | 640 | |
---|
[be97baf] | 641 | if (!fZeros(&leg->v)) fEquate = fFalse; |
---|
[e6cfe52] | 642 | if (!reached_end) { |
---|
| 643 | add_stn_to_list(&stnlist, stn3); |
---|
[be97baf] | 644 | if (!fEquate) { |
---|
[e6cfe52] | 645 | mulsd(&e, &leg->v, &sc); |
---|
| 646 | adddd(&POSD(stn3), &POSD(stn3), &e); |
---|
[95c3272] | 647 | } |
---|
[e6cfe52] | 648 | fix(stn3); |
---|
| 649 | } |
---|
[421b7d2] | 650 | |
---|
[64d37a3] | 651 | if (!(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) { |
---|
| 652 | if (TSTBIT(leg->l.flags, FLAGS_SURFACE)) { |
---|
| 653 | stn1->name->sflags |= BIT(SFLAGS_SURFACE); |
---|
| 654 | stn3->name->sflags |= BIT(SFLAGS_SURFACE); |
---|
| 655 | } else { |
---|
| 656 | stn1->name->sflags |= BIT(SFLAGS_UNDERGROUND); |
---|
| 657 | stn3->name->sflags |= BIT(SFLAGS_UNDERGROUND); |
---|
| 658 | } |
---|
| 659 | |
---|
[a4ae909] | 660 | SVX_ASSERT(!fEquate); |
---|
| 661 | SVX_ASSERT(!fZeros(&leg->v)); |
---|
| 662 | if (leg->meta) { |
---|
[1ee204e] | 663 | pimg->days1 = leg->meta->days1; |
---|
| 664 | pimg->days2 = leg->meta->days2; |
---|
[a4ae909] | 665 | } else { |
---|
[1ee204e] | 666 | pimg->days1 = pimg->days2 = -1; |
---|
[64d37a3] | 667 | } |
---|
[eb5aea0] | 668 | pimg->style = (leg->l.flags >> FLAGS_STYLE_BIT0) & 0x07; |
---|
| 669 | img_write_item(pimg, img_LINE, leg->l.flags & FLAGS_MASK, |
---|
[a4ae909] | 670 | sprint_prefix(leg_pfx), |
---|
| 671 | POS(stn3, 0), POS(stn3, 1), POS(stn3, 2)); |
---|
[64d37a3] | 672 | } |
---|
[045c055] | 673 | |
---|
[e6cfe52] | 674 | /* FIXME: equate at the start of a traverse treated specially |
---|
[421b7d2] | 675 | * - what about equates at end? */ |
---|
[be97baf] | 676 | if (stn1->name != stn3->name && !(fEquate && cLegsTrav == 0)) { |
---|
[a4ae909] | 677 | /* (node not part of same stn) && |
---|
[e6cfe52] | 678 | * (not equate at start of traverse) */ |
---|
[647407d] | 679 | #ifndef BLUNDER_DETECTION |
---|
[e6cfe52] | 680 | if (fhErrStat && !fArtic) { |
---|
[be97baf] | 681 | if (!stn1->name->ident) { |
---|
[e6cfe52] | 682 | /* FIXME: not ideal */ |
---|
| 683 | fputs("<fixed point>", fhErrStat); |
---|
| 684 | } else { |
---|
[be97baf] | 685 | fprint_prefix(fhErrStat, stn1->name); |
---|
[e6cfe52] | 686 | } |
---|
| 687 | fputs(fEquate ? szLinkEq : szLink, fhErrStat); |
---|
| 688 | if (reached_end) { |
---|
[ff6cfe1] | 689 | if (!stn3->name->ident) { |
---|
[5b256a2] | 690 | /* FIXME: not ideal */ |
---|
| 691 | fputs("<fixed point>", fhErrStat); |
---|
| 692 | } else { |
---|
[e6cfe52] | 693 | fprint_prefix(fhErrStat, stn3->name); |
---|
[5b256a2] | 694 | } |
---|
[647407d] | 695 | } |
---|
[e6cfe52] | 696 | } |
---|
[045c055] | 697 | #endif |
---|
[e6cfe52] | 698 | if (!fEquate) { |
---|
| 699 | cLegsTrav++; |
---|
| 700 | lenTrav += sqrt(lenTot); |
---|
| 701 | } |
---|
| 702 | } else { |
---|
[045c055] | 703 | #if SHOW_INTERNAL_LEGS |
---|
[e6cfe52] | 704 | if (fhErrStat && !fArtic) fprintf(fhErrStat, "+"); |
---|
[045c055] | 705 | #endif |
---|
[e6cfe52] | 706 | if (lenTot > 0.0) { |
---|
[045c055] | 707 | #if DEBUG_INVALID |
---|
[e6cfe52] | 708 | fprintf(stderr, "lenTot = %8.4f ", lenTot); |
---|
[be97baf] | 709 | fprint_prefix(stderr, stn1->name); |
---|
[e6cfe52] | 710 | fprintf(stderr, " -> "); |
---|
| 711 | fprint_prefix(stderr, stn3->name); |
---|
[045c055] | 712 | #endif |
---|
[e6cfe52] | 713 | BUG("during calculation of closure errors"); |
---|
[045c055] | 714 | } |
---|
[e6cfe52] | 715 | } |
---|
| 716 | if (reached_end) break; |
---|
[421b7d2] | 717 | |
---|
[e6cfe52] | 718 | i = k ^ 1; /* flip direction for other leg of 2 node */ |
---|
[421b7d2] | 719 | |
---|
[e6cfe52] | 720 | stn1 = stn3; |
---|
| 721 | } /* endwhile */ |
---|
[421b7d2] | 722 | |
---|
[e6cfe52] | 723 | if (cLegsTrav && !fArtic && fhErrStat) |
---|
| 724 | err_stat(cLegsTrav, lenTrav, eTot, eTotTheo, |
---|
| 725 | hTot, hTotTheo, vTot, vTotTheo); |
---|
[421b7d2] | 726 | |
---|
[045c055] | 727 | ptrOld = ptr; |
---|
| 728 | ptr = ptr->next; |
---|
| 729 | osfree(ptrOld); |
---|
| 730 | } |
---|
| 731 | |
---|
[f1067a2] | 732 | /* Leave fhErrStat open in case we're asked to close loops again... */ |
---|
[045c055] | 733 | } |
---|
| 734 | |
---|
| 735 | static void |
---|
| 736 | err_stat(int cLegsTrav, double lenTrav, |
---|
| 737 | double eTot, double eTotTheo, |
---|
| 738 | double hTot, double hTotTheo, |
---|
| 739 | double vTot, double vTotTheo) |
---|
| 740 | { |
---|
[c61aa79] | 741 | double E = sqrt(eTot / eTotTheo); |
---|
| 742 | double H = sqrt(hTot / hTotTheo); |
---|
| 743 | double V = sqrt(vTot / vTotTheo); |
---|
[647407d] | 744 | if (!fSuppress) { |
---|
[c61aa79] | 745 | double sqrt_eTot = sqrt(eTot); |
---|
[cb3d1e2] | 746 | fputnl(fhErrStat); |
---|
[034141d] | 747 | fprintf(fhErrStat, msg(/*Original length %6.2fm (%3d legs), moved %6.2fm (%5.2fm/leg). */145), |
---|
[cb3d1e2] | 748 | lenTrav, cLegsTrav, sqrt_eTot, sqrt_eTot / cLegsTrav); |
---|
[736f7df] | 749 | if (lenTrav > 0.0) { |
---|
[034141d] | 750 | fprintf(fhErrStat, msg(/*Error %6.2f%%*/146), 100 * sqrt_eTot / lenTrav); |
---|
[736f7df] | 751 | } else { |
---|
| 752 | /* TRANSLATORS: Here N/A means "Not Applicable" -- it means the |
---|
| 753 | * traverse has zero length, so error per metre is meaningless. |
---|
| 754 | * |
---|
| 755 | * There should be 4 spaces between "Error" and "N/A" so that it lines |
---|
| 756 | * up with the numbers in the message above. */ |
---|
[421b7d2] | 757 | fputs(msg(/*Error N/A*/147), fhErrStat); |
---|
[736f7df] | 758 | } |
---|
[cb3d1e2] | 759 | fputnl(fhErrStat); |
---|
[c61aa79] | 760 | fprintf(fhErrStat, "%f\n", E); |
---|
| 761 | fprintf(fhErrStat, "H: %f V: %f\n", H, V); |
---|
[cb3d1e2] | 762 | fputnl(fhErrStat); |
---|
| 763 | } |
---|
[c61aa79] | 764 | img_write_errors(pimg, cLegsTrav, lenTrav, E, H, V); |
---|
[045c055] | 765 | } |
---|
| 766 | |
---|
| 767 | static void |
---|
| 768 | replace_trailing_travs(void) |
---|
| 769 | { |
---|
| 770 | stackTrail *ptrOld; |
---|
| 771 | node *stn1, *stn2; |
---|
| 772 | linkfor *leg; |
---|
| 773 | int i; |
---|
| 774 | |
---|
[736f7df] | 775 | /* TRANSLATORS: In French, Eric chose to use the terminology used by |
---|
| 776 | * toporobot: "sequence" for the English "traverse", which makes sense |
---|
| 777 | * (although toporobot actually uses this term to mean something more |
---|
| 778 | * specific). Feel free to follow this lead if you can't think of a better |
---|
| 779 | * term - these messages mostly indicate how processing is progressing. |
---|
[b49ac56] | 780 | * |
---|
[736f7df] | 781 | * A trailing traverse is a dead end back to a junction. */ |
---|
[045c055] | 782 | out_current_action(msg(/*Calculating trailing traverses*/128)); |
---|
| 783 | |
---|
| 784 | while (ptrTrail != NULL) { |
---|
| 785 | leg = ptrTrail->join1; |
---|
| 786 | leg = reverse_leg(leg); |
---|
| 787 | stn1 = leg->l.to; |
---|
| 788 | i = reverse_leg_dirn(leg); |
---|
| 789 | #if PRINT_NETBITS |
---|
| 790 | printf(" Trailing trav "); |
---|
| 791 | print_prefix(stn1->name); |
---|
| 792 | printf("<%p>", stn1); |
---|
| 793 | printf("%s...\n", szLink); |
---|
| 794 | printf(" attachment stn is at (%f, %f, %f)\n", |
---|
| 795 | POS(stn1, 0), POS(stn1, 1), POS(stn1, 2)); |
---|
| 796 | #endif |
---|
[09bfd4c] | 797 | /* We may have swapped the links round when we removed the leg. If |
---|
| 798 | * we did then stn1->leg[i] will be in use. The link we swapped |
---|
| 799 | * with is the first free leg */ |
---|
[045c055] | 800 | if (stn1->leg[i]) { |
---|
| 801 | /* j is the direction to swap with */ |
---|
| 802 | int j = (stn1->leg[1]) ? 2 : 1; |
---|
| 803 | /* change the other direction of leg i to use leg j */ |
---|
| 804 | reverse_leg(stn1->leg[i])->l.reverse += j - i; |
---|
| 805 | stn1->leg[j] = stn1->leg[i]; |
---|
| 806 | } |
---|
| 807 | stn1->leg[i] = ptrTrail->join1; |
---|
[4c07c51] | 808 | SVX_ASSERT(fixed(stn1)); |
---|
[a4ae909] | 809 | img_write_item(pimg, img_MOVE, 0, NULL, |
---|
| 810 | POS(stn1, 0), POS(stn1, 1), POS(stn1, 2)); |
---|
[045c055] | 811 | |
---|
[e6cfe52] | 812 | while (1) { |
---|
[693388e] | 813 | prefix *leg_pfx; |
---|
[e6cfe52] | 814 | int j; |
---|
[693388e] | 815 | |
---|
[e6cfe52] | 816 | leg = stn1->leg[i]; |
---|
| 817 | stn2 = leg->l.to; |
---|
| 818 | j = reverse_leg_dirn(leg); |
---|
| 819 | if (data_here(leg)) { |
---|
[693388e] | 820 | leg_pfx = stn1->name->up; |
---|
[e6cfe52] | 821 | adddd(&POSD(stn2), &POSD(stn1), &leg->d); |
---|
[045c055] | 822 | #if 0 |
---|
[e6cfe52] | 823 | printf("Adding leg (%f, %f, %f)\n", leg->d[0], leg->d[1], leg->d[2]); |
---|
[045c055] | 824 | #endif |
---|
[e6cfe52] | 825 | } else { |
---|
[693388e] | 826 | leg_pfx = stn2->name->up; |
---|
[e6cfe52] | 827 | leg = stn2->leg[j]; |
---|
| 828 | subdd(&POSD(stn2), &POSD(stn1), &leg->d); |
---|
[045c055] | 829 | #if 0 |
---|
[e6cfe52] | 830 | printf("Subtracting reverse leg (%f, %f, %f)\n", leg->d[0], leg->d[1], leg->d[2]); |
---|
[045c055] | 831 | #endif |
---|
[e6cfe52] | 832 | } |
---|
[045c055] | 833 | |
---|
[e6cfe52] | 834 | fix(stn2); |
---|
| 835 | add_stn_to_list(&stnlist, stn2); |
---|
[64d37a3] | 836 | if (!(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) { |
---|
| 837 | if (TSTBIT(leg->l.flags, FLAGS_SURFACE)) { |
---|
| 838 | stn1->name->sflags |= BIT(SFLAGS_SURFACE); |
---|
| 839 | stn2->name->sflags |= BIT(SFLAGS_SURFACE); |
---|
| 840 | } else { |
---|
| 841 | stn1->name->sflags |= BIT(SFLAGS_UNDERGROUND); |
---|
| 842 | stn2->name->sflags |= BIT(SFLAGS_UNDERGROUND); |
---|
| 843 | } |
---|
[e6cfe52] | 844 | } |
---|
[a4ae909] | 845 | if (!(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) { |
---|
| 846 | SVX_ASSERT(!fZeros(&leg->v)); |
---|
| 847 | if (leg->meta) { |
---|
[1ee204e] | 848 | pimg->days1 = leg->meta->days1; |
---|
| 849 | pimg->days2 = leg->meta->days2; |
---|
[a4ae909] | 850 | } else { |
---|
[1ee204e] | 851 | pimg->days1 = pimg->days2 = -1; |
---|
[693388e] | 852 | } |
---|
[eb5aea0] | 853 | pimg->style = (leg->l.flags >> FLAGS_STYLE_BIT0) & 0x07; |
---|
| 854 | img_write_item(pimg, img_LINE, leg->l.flags & FLAGS_MASK, |
---|
[a4ae909] | 855 | sprint_prefix(leg_pfx), |
---|
| 856 | POS(stn2, 0), POS(stn2, 1), POS(stn2, 2)); |
---|
[e6cfe52] | 857 | } |
---|
[045c055] | 858 | |
---|
[e6cfe52] | 859 | /* stop if not 2 node */ |
---|
| 860 | if (!two_node(stn2)) break; |
---|
[045c055] | 861 | |
---|
[e6cfe52] | 862 | stn1 = stn2; |
---|
| 863 | i = j ^ 1; /* flip direction for other leg of 2 node */ |
---|
[045c055] | 864 | } |
---|
| 865 | |
---|
| 866 | ptrOld = ptrTrail; |
---|
| 867 | ptrTrail = ptrTrail->next; |
---|
| 868 | osfree(ptrOld); |
---|
| 869 | } |
---|
| 870 | |
---|
[647407d] | 871 | /* write out connections with no survey data */ |
---|
[a4ae909] | 872 | while (nosurveyhead) { |
---|
| 873 | nosurveylink *p = nosurveyhead; |
---|
| 874 | SVX_ASSERT(fixed(p->fr)); |
---|
| 875 | SVX_ASSERT(fixed(p->to)); |
---|
| 876 | if (TSTBIT(p->flags, FLAGS_SURFACE)) { |
---|
| 877 | p->fr->name->sflags |= BIT(SFLAGS_SURFACE); |
---|
| 878 | p->to->name->sflags |= BIT(SFLAGS_SURFACE); |
---|
| 879 | } else { |
---|
| 880 | p->fr->name->sflags |= BIT(SFLAGS_UNDERGROUND); |
---|
| 881 | p->to->name->sflags |= BIT(SFLAGS_UNDERGROUND); |
---|
[647407d] | 882 | } |
---|
[a4ae909] | 883 | img_write_item(pimg, img_MOVE, 0, NULL, |
---|
| 884 | POS(p->fr, 0), POS(p->fr, 1), POS(p->fr, 2)); |
---|
[ee05463] | 885 | if (p->meta) { |
---|
[1ee204e] | 886 | pimg->days1 = p->meta->days1; |
---|
| 887 | pimg->days2 = p->meta->days2; |
---|
[a4ae909] | 888 | } else { |
---|
[1ee204e] | 889 | pimg->days1 = pimg->days2 = -1; |
---|
[a4ae909] | 890 | } |
---|
[eb5aea0] | 891 | pimg->style = img_STYLE_NOSURVEY; |
---|
| 892 | img_write_item(pimg, img_LINE, (p->flags & FLAGS_MASK), |
---|
[a4ae909] | 893 | sprint_prefix(p->fr->name->up), |
---|
| 894 | POS(p->to, 0), POS(p->to, 1), POS(p->to, 2)); |
---|
| 895 | nosurveyhead = p->next; |
---|
| 896 | osfree(p); |
---|
[647407d] | 897 | } |
---|
| 898 | |
---|
[045c055] | 899 | /* write stations to .3d file and free legs and stations */ |
---|
| 900 | FOR_EACH_STN(stn1, stnlist) { |
---|
[e6cfe52] | 901 | int d; |
---|
[4c07c51] | 902 | SVX_ASSERT(fixed(stn1)); |
---|
[a2c33ae] | 903 | if (stn1->name->stn == stn1) { |
---|
[a4ae909] | 904 | int sf = stn1->name->sflags; |
---|
[a2c33ae] | 905 | /* take care of unused fixed points */ |
---|
[a4ae909] | 906 | if (!TSTBIT(sf, SFLAGS_SOLVED)) { |
---|
[a2c33ae] | 907 | const char * label = NULL; |
---|
| 908 | if (TSTBIT(sf, SFLAGS_ANON)) { |
---|
| 909 | label = ""; |
---|
| 910 | } else if (stn1->name->ident) { |
---|
| 911 | label = sprint_prefix(stn1->name); |
---|
| 912 | } |
---|
| 913 | if (label) { |
---|
| 914 | /* Set flag to stop station being rewritten after *solve. */ |
---|
| 915 | stn1->name->sflags = sf | BIT(SFLAGS_SOLVED); |
---|
| 916 | sf &= SFLAGS_MASK; |
---|
| 917 | if (stn1->name->max_export) sf |= BIT(SFLAGS_EXPORTED); |
---|
| 918 | img_write_item(pimg, img_LABEL, sf, label, |
---|
| 919 | POS(stn1, 0), POS(stn1, 1), POS(stn1, 2)); |
---|
| 920 | } |
---|
[95c3272] | 921 | } |
---|
[e6cfe52] | 922 | } |
---|
| 923 | /* update coords of bounding box, ignoring the base positions |
---|
[c9eb197] | 924 | * of points fixed with error estimates and only counting stations |
---|
[a2c33ae] | 925 | * in underground surveys. |
---|
| 926 | * |
---|
| 927 | * NB We don't set SFLAGS_UNDERGROUND for the anchor station for |
---|
| 928 | * a point fixed with error estimates, so this test will exclude |
---|
| 929 | * those too, which is what we want. |
---|
| 930 | */ |
---|
| 931 | if (TSTBIT(stn1->name->sflags, SFLAGS_UNDERGROUND)) { |
---|
[e6cfe52] | 932 | for (d = 0; d < 3; d++) { |
---|
| 933 | if (POS(stn1, d) < min[d]) { |
---|
| 934 | min[d] = POS(stn1, d); |
---|
| 935 | pfxLo[d] = stn1->name; |
---|
[045c055] | 936 | } |
---|
[e6cfe52] | 937 | if (POS(stn1, d) > max[d]) { |
---|
| 938 | max[d] = POS(stn1, d); |
---|
| 939 | pfxHi[d] = stn1->name; |
---|
[045c055] | 940 | } |
---|
| 941 | } |
---|
[e6cfe52] | 942 | } |
---|
[421b7d2] | 943 | |
---|
[e6cfe52] | 944 | d = stn1->name->shape; |
---|
[f15cde77] | 945 | if (d <= 1 && !TSTBIT(stn1->name->sflags, SFLAGS_USED)) { |
---|
| 946 | bool unused_fixed_point = fFalse; |
---|
| 947 | if (d == 0) { |
---|
| 948 | /* Unused fixed point without error estimates */ |
---|
| 949 | unused_fixed_point = fTrue; |
---|
| 950 | } else if (stn1->leg[0]) { |
---|
| 951 | prefix *pfx = stn1->leg[0]->l.to->name; |
---|
[a2c33ae] | 952 | if (!pfx->ident && !TSTBIT(pfx->sflags, SFLAGS_ANON)) { |
---|
[f15cde77] | 953 | /* Unused fixed point with error estimates */ |
---|
| 954 | unused_fixed_point = fTrue; |
---|
| 955 | } |
---|
| 956 | } |
---|
| 957 | if (unused_fixed_point) { |
---|
[736f7df] | 958 | /* TRANSLATORS: fixed survey station that is not part of any survey |
---|
| 959 | */ |
---|
[1edaf8d] | 960 | warning_in_file(stn1->name->filename, stn1->name->line, |
---|
[0804fbe] | 961 | /*Unused fixed point “%s”*/73, sprint_prefix(stn1->name)); |
---|
[045c055] | 962 | } |
---|
| 963 | } |
---|
[eb04711] | 964 | |
---|
| 965 | /* For stations fixed with error estimates, we need to ignore the leg to |
---|
| 966 | * the "real" fixed point in the node stats. |
---|
| 967 | */ |
---|
[a2c33ae] | 968 | if (stn1->leg[0] && !stn1->leg[0]->l.to->name->ident && |
---|
| 969 | !TSTBIT(stn1->leg[0]->l.to->name->sflags, SFLAGS_ANON)) |
---|
[eb04711] | 970 | stn1->name->shape--; |
---|
| 971 | |
---|
[045c055] | 972 | for (i = 0; i <= 2; i++) { |
---|
| 973 | leg = stn1->leg[i]; |
---|
| 974 | /* only want to think about forwards legs */ |
---|
| 975 | if (leg && data_here(leg)) { |
---|
[eb18f4d] | 976 | linkfor *legRev; |
---|
[045c055] | 977 | node *stnB; |
---|
| 978 | int iB; |
---|
| 979 | stnB = leg->l.to; |
---|
| 980 | iB = reverse_leg_dirn(leg); |
---|
| 981 | legRev = stnB->leg[iB]; |
---|
[4c07c51] | 982 | SVX_ASSERT2(legRev->l.to == stn1, "leg doesn't reciprocate"); |
---|
| 983 | SVX_ASSERT(fixed(stn1)); |
---|
[e6cfe52] | 984 | if (!(leg->l.flags & |
---|
[95c3272] | 985 | (BIT(FLAGS_DUPLICATE)|BIT(FLAGS_SPLAY)| |
---|
| 986 | BIT(FLAGS_SURFACE)))) { |
---|
[693388e] | 987 | /* check not an equating leg, or one inside an sdfix point */ |
---|
| 988 | if (!(leg->l.reverse & (FLAG_REPLACEMENTLEG | FLAG_FAKE))) { |
---|
[045c055] | 989 | totadj += sqrt(sqrd(POS(stnB, 0) - POS(stn1, 0)) + |
---|
| 990 | sqrd(POS(stnB, 1) - POS(stn1, 1)) + |
---|
| 991 | sqrd(POS(stnB, 2) - POS(stn1, 2))); |
---|
| 992 | total += sqrt(sqrdd(leg->d)); |
---|
[27b8b59] | 993 | totplan += hypot(leg->d[0], leg->d[1]); |
---|
[045c055] | 994 | totvert += fabs(leg->d[2]); |
---|
| 995 | } |
---|
| 996 | } |
---|
| 997 | osfree(leg); |
---|
| 998 | osfree(legRev); |
---|
| 999 | stn1->leg[i] = stnB->leg[iB] = NULL; |
---|
| 1000 | } |
---|
| 1001 | } |
---|
| 1002 | } |
---|
| 1003 | |
---|
| 1004 | /* The station position is attached to the name, so we leave the names and |
---|
| 1005 | * positions in place - they can then be picked up if we have a *solve |
---|
| 1006 | * followed by more data */ |
---|
| 1007 | for (stn1 = stnlist; stn1; stn1 = stn2) { |
---|
| 1008 | stn2 = stn1->next; |
---|
| 1009 | stn1->name->stn = NULL; |
---|
| 1010 | osfree(stn1); |
---|
| 1011 | } |
---|
| 1012 | stnlist = NULL; |
---|
| 1013 | } |
---|
[ee05463] | 1014 | |
---|
| 1015 | static void |
---|
| 1016 | write_passage_models(void) |
---|
| 1017 | { |
---|
| 1018 | lrudlist * psg = model; |
---|
| 1019 | while (psg) { |
---|
| 1020 | lrudlist * oldp; |
---|
| 1021 | lrud * xsect = psg->tube; |
---|
| 1022 | int xflags = 0; |
---|
| 1023 | while (xsect) { |
---|
| 1024 | lrud *oldx; |
---|
[d333899] | 1025 | prefix *pfx; |
---|
[ee05463] | 1026 | const char *name; |
---|
| 1027 | pimg->l = xsect->l; |
---|
| 1028 | pimg->r = xsect->r; |
---|
| 1029 | pimg->u = xsect->u; |
---|
| 1030 | pimg->d = xsect->d; |
---|
[b08ad81] | 1031 | if (xsect->meta) { |
---|
[1ee204e] | 1032 | pimg->days1 = xsect->meta->days1; |
---|
| 1033 | pimg->days2 = xsect->meta->days2; |
---|
[b08ad81] | 1034 | } else { |
---|
[1ee204e] | 1035 | pimg->days1 = pimg->days2 = -1; |
---|
[b08ad81] | 1036 | } |
---|
[d333899] | 1037 | |
---|
| 1038 | pfx = xsect->stn; |
---|
| 1039 | name = sprint_prefix(pfx); |
---|
[ee05463] | 1040 | oldx = xsect; |
---|
| 1041 | xsect = xsect->next; |
---|
| 1042 | osfree(oldx); |
---|
[d333899] | 1043 | |
---|
| 1044 | if (!pfx->pos) { |
---|
[736f7df] | 1045 | /* TRANSLATORS: e.g. the user specifies a passage cross-section at |
---|
| 1046 | * station "entrance.27", but there is no station "entrance.27" in |
---|
| 1047 | * the centre-line. */ |
---|
[d333899] | 1048 | error_in_file(pfx->filename, pfx->line, |
---|
[0804fbe] | 1049 | /*Cross section specified at non-existent station “%s”*/83, |
---|
[d333899] | 1050 | name); |
---|
| 1051 | } else { |
---|
| 1052 | if (xsect == NULL) xflags = img_XFLAG_END; |
---|
| 1053 | img_write_item(pimg, img_XSECT, xflags, name, 0, 0, 0); |
---|
| 1054 | } |
---|
[ee05463] | 1055 | } |
---|
| 1056 | oldp = psg; |
---|
| 1057 | psg = psg->next; |
---|
| 1058 | osfree(oldp); |
---|
| 1059 | } |
---|
| 1060 | model = NULL; |
---|
| 1061 | } |
---|