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