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