[bb90203] | 1 | /* > cavern.c |
---|
| 2 | * SURVEX Cave surveying software: data reduction main and related functions |
---|
[932f7e9] | 3 | * Copyright (C) 1991-2001 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
[bb90203] | 18 | */ |
---|
| 19 | |
---|
| 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | #include <config.h> |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | #include <time.h> |
---|
| 25 | |
---|
[5853657] | 26 | #include "cavern.h" |
---|
| 27 | #include "cmdline.h" |
---|
| 28 | #include "commands.h" |
---|
[bb90203] | 29 | #include "datain.h" |
---|
| 30 | #include "debug.h" |
---|
| 31 | #include "message.h" |
---|
| 32 | #include "filename.h" |
---|
| 33 | #include "filelist.h" |
---|
| 34 | #include "img.h" |
---|
| 35 | #include "listpos.h" |
---|
[5853657] | 36 | #include "netbits.h" |
---|
| 37 | #include "netskel.h" |
---|
| 38 | #include "osdepend.h" |
---|
[bb90203] | 39 | #include "out.h" |
---|
[8e8057c] | 40 | #include "str.h" |
---|
[5853657] | 41 | #include "validate.h" |
---|
[bb90203] | 42 | |
---|
[647407d] | 43 | #ifdef NEW3DFORMAT |
---|
[407084d] | 44 | #include <unistd.h> /* for getcwd() */ |
---|
| 45 | |
---|
[647407d] | 46 | #include "new3dout.h" |
---|
| 47 | #ifndef MAXPATHLEN |
---|
| 48 | #define MAXPATHLEN 1024 |
---|
| 49 | #endif |
---|
| 50 | #endif |
---|
| 51 | |
---|
[bb90203] | 52 | /* For funcs which want to be immune from messing around with different |
---|
| 53 | * calling conventions */ |
---|
| 54 | #ifndef CDECL |
---|
| 55 | # define CDECL |
---|
| 56 | #endif |
---|
| 57 | |
---|
| 58 | /* Globals */ |
---|
| 59 | node *stnlist = NULL; |
---|
| 60 | settings *pcs; |
---|
| 61 | prefix *root; |
---|
| 62 | long cLegs, cStns; |
---|
| 63 | long cComponents; |
---|
[d1878c51] | 64 | bool fExportUsed = fFalse; |
---|
[bb90203] | 65 | |
---|
| 66 | FILE *fhErrStat = NULL; |
---|
| 67 | img *pimgOut = NULL; |
---|
| 68 | #ifndef NO_PERCENTAGE |
---|
| 69 | bool fPercent = fFalse; |
---|
| 70 | #endif |
---|
| 71 | bool fAscii = fFalse; |
---|
[647407d] | 72 | bool fQuiet = fFalse; /* just show brief summary + errors */ |
---|
| 73 | bool fMute = fFalse; /* just show errors */ |
---|
| 74 | bool fSuppress = fFalse; /* only output 3d(3dx) file */ |
---|
| 75 | |
---|
| 76 | nosurveylink *nosurveyhead; |
---|
[bb90203] | 77 | |
---|
| 78 | real totadj, total, totplan, totvert; |
---|
| 79 | real min[3], max[3]; |
---|
| 80 | prefix *pfxHi[3], *pfxLo[3]; |
---|
| 81 | |
---|
| 82 | char *survey_title = NULL; |
---|
| 83 | int survey_title_len; |
---|
| 84 | |
---|
| 85 | bool fExplicitTitle = fFalse; |
---|
| 86 | |
---|
[8e8057c] | 87 | char *fnm_output_base = NULL; |
---|
| 88 | int fnm_output_base_is_dir = 0; |
---|
| 89 | |
---|
[bb90203] | 90 | static void do_stats(void); |
---|
| 91 | |
---|
| 92 | static const struct option long_opts[] = { |
---|
| 93 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
| 94 | #ifdef NO_PERCENTAGE |
---|
| 95 | {"percentage", no_argument, 0, 0}, |
---|
| 96 | {"no-percentage", no_argument, 0, 0}, |
---|
| 97 | #else |
---|
| 98 | {"percentage", no_argument, 0, 'p'}, |
---|
| 99 | {"no-percentage", no_argument, (int*)&fPercent, 0}, |
---|
| 100 | #endif |
---|
[8e8057c] | 101 | {"output", required_argument, 0, 'o'}, |
---|
[647407d] | 102 | {"quiet", no_argument, 0, 'q'}, |
---|
| 103 | {"no-auxiliary-files", no_argument, 0, 's'}, |
---|
| 104 | #ifdef NEW3DFORMAT |
---|
| 105 | {"new-format", no_argument, 0, 'x'}, |
---|
| 106 | #endif |
---|
[bb90203] | 107 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 108 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 109 | {0, 0, 0, 0} |
---|
| 110 | }; |
---|
| 111 | |
---|
[647407d] | 112 | #ifdef NEW3DFORMAT |
---|
| 113 | #define short_opts "pxao:qsz:" |
---|
| 114 | #else |
---|
| 115 | #define short_opts "pao:qsz:" |
---|
| 116 | #endif |
---|
[bb90203] | 117 | |
---|
[647407d] | 118 | /* TRANSLATE extract help messages to message file */ |
---|
[bb90203] | 119 | static struct help_msg help[] = { |
---|
| 120 | /* <-- */ |
---|
| 121 | {HLP_ENCODELONG(0), "display percentage progress"}, |
---|
| 122 | {'a', "output ascii variant of .3d file"}, |
---|
[8e8057c] | 123 | {HLP_ENCODELONG(2), "set location for output files"}, |
---|
[647407d] | 124 | {HLP_ENCODELONG(3), "only show brief summary (-qq for errors only)"}, |
---|
| 125 | {HLP_ENCODELONG(4), "do not create .pos, .inf, or .err files"}, |
---|
| 126 | #ifdef NEW3DFORMAT |
---|
| 127 | {HLP_ENCODELONG(5), "output data in 3dx format"}, |
---|
| 128 | #endif |
---|
[8e8057c] | 129 | /*{'z', "set optimizations for network reduction"},*/ |
---|
[bb90203] | 130 | {0, 0} |
---|
| 131 | }; |
---|
| 132 | |
---|
| 133 | extern CDECL int |
---|
| 134 | main(int argc, char **argv) |
---|
| 135 | { |
---|
| 136 | int d; |
---|
| 137 | static clock_t tmCPUStart; |
---|
| 138 | static time_t tmUserStart; |
---|
| 139 | static double tmCPU, tmUser; |
---|
| 140 | |
---|
| 141 | tmUserStart = time(NULL); |
---|
| 142 | tmCPUStart = clock(); |
---|
| 143 | init_screen(); |
---|
| 144 | |
---|
[b85e20f] | 145 | msg_init(argv[0]); |
---|
[bb90203] | 146 | |
---|
| 147 | pcs = osnew(settings); |
---|
| 148 | pcs->next = NULL; |
---|
| 149 | pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1; |
---|
| 150 | |
---|
| 151 | /* Set up root of prefix hierarchy */ |
---|
| 152 | root = osnew(prefix); |
---|
| 153 | root->up = root->right = root->down = NULL; |
---|
| 154 | root->stn = NULL; |
---|
| 155 | root->pos = NULL; |
---|
| 156 | root->ident = "\\"; |
---|
[932f7e9] | 157 | root->min_export = root->max_export = 0; |
---|
[647407d] | 158 | root->fSuspectTypo = fFalse; |
---|
| 159 | |
---|
| 160 | nosurveyhead = NULL; |
---|
[bb90203] | 161 | |
---|
| 162 | stnlist = NULL; |
---|
| 163 | cLegs = cStns = cComponents = 0; |
---|
| 164 | totadj = total = totplan = totvert = 0.0; |
---|
| 165 | |
---|
| 166 | for (d = 0; d <= 2; d++) { |
---|
| 167 | min[d] = REAL_BIG; |
---|
| 168 | max[d] = -REAL_BIG; |
---|
| 169 | pfxHi[d] = pfxLo[d] = NULL; |
---|
| 170 | } |
---|
| 171 | |
---|
[b85e20f] | 172 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, -1); |
---|
[bb90203] | 173 | while (1) { |
---|
[b85e20f] | 174 | /* at least one argument must be given */ |
---|
| 175 | int opt = cmdline_getopt(); |
---|
[bb90203] | 176 | if (opt == EOF) break; |
---|
| 177 | switch (opt) { |
---|
| 178 | case 'a': |
---|
| 179 | fAscii = fTrue; |
---|
| 180 | break; |
---|
| 181 | case 'p': |
---|
| 182 | #ifndef NO_PERCENTAGE |
---|
| 183 | fPercent = 1; |
---|
| 184 | #endif |
---|
| 185 | break; |
---|
[8e8057c] | 186 | case 'o': { |
---|
| 187 | /* can be a directory (in which case use basename of leaf input) |
---|
| 188 | * or a file (in which case just trim the extension off) */ |
---|
| 189 | if (fDirectory(optarg)) { |
---|
| 190 | /* this is a little tricky - we need to note the path here, |
---|
| 191 | * and then add the leaf later on (in datain.c) */ |
---|
| 192 | fnm_output_base = base_from_fnm(optarg); |
---|
| 193 | fnm_output_base_is_dir = 1; |
---|
| 194 | } else { |
---|
| 195 | osfree(fnm_output_base); /* in case of multiple -o options */ |
---|
| 196 | fnm_output_base = base_from_fnm(optarg); |
---|
| 197 | } |
---|
| 198 | break; |
---|
| 199 | } |
---|
[647407d] | 200 | #ifdef NEW3DFORMAT |
---|
| 201 | case 'x': { |
---|
| 202 | fUseNewFormat = 1; |
---|
| 203 | break; |
---|
| 204 | } |
---|
| 205 | #endif |
---|
| 206 | case 'q': |
---|
| 207 | if (fQuiet) fMute = 1; |
---|
| 208 | fQuiet = 1; |
---|
| 209 | break; |
---|
| 210 | case 's': |
---|
| 211 | fSuppress = 1; |
---|
| 212 | break; |
---|
[8e8057c] | 213 | case 'z': { |
---|
| 214 | /* Control which network optimisations are used (development tool) */ |
---|
| 215 | static int first_opt_z = 1; |
---|
[e917a13] | 216 | char ch; |
---|
[8e8057c] | 217 | if (first_opt_z) { |
---|
[bb90203] | 218 | optimize = 0; |
---|
[8e8057c] | 219 | first_opt_z = 0; |
---|
[bb90203] | 220 | } |
---|
[c50391b8] | 221 | /* Lollipops, Parallel legs, Iterate mx, Delta* */ |
---|
[e917a13] | 222 | while ((ch = *optarg++) != '\0') |
---|
| 223 | if (islower(ch)) optimize |= BITA(ch); |
---|
[bb90203] | 224 | break; |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | out_puts(PACKAGE" "VERSION); |
---|
| 230 | out_puts(COPYRIGHT_MSG); |
---|
| 231 | putnl(); |
---|
| 232 | |
---|
| 233 | /* end of options, now process data files */ |
---|
| 234 | while (argv[optind]) { |
---|
| 235 | const char *fnm = argv[optind]; |
---|
| 236 | |
---|
| 237 | if (!fExplicitTitle) { |
---|
| 238 | char *lf; |
---|
[8e8057c] | 239 | lf = baseleaf_from_fnm(fnm); |
---|
[bb90203] | 240 | if (survey_title) s_catchar(&survey_title, &survey_title_len, ' '); |
---|
| 241 | s_cat(&survey_title, &survey_title_len, lf); |
---|
| 242 | osfree(lf); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | /* Select defaults settings */ |
---|
| 246 | default_all(pcs); |
---|
[647407d] | 247 | #ifdef NEW3DFORMAT |
---|
| 248 | /* we need to get the filename of the first one for our base_source */ |
---|
| 249 | /* and also run_file */ |
---|
| 250 | if (fUseNewFormat) { |
---|
| 251 | create_twig(root, fnm); |
---|
| 252 | rhizome = root->twig_link; |
---|
| 253 | limb = get_twig(root); |
---|
| 254 | firstfilename = osstrdup(fnm); |
---|
| 255 | startingdir = osmalloc(MAXPATHLEN); |
---|
[49090c02] | 256 | #if (OS==RISCOS) |
---|
| 257 | strcpy(startingdir, "@"); |
---|
| 258 | #else |
---|
[647407d] | 259 | getcwd(startingdir, MAXPATHLEN); |
---|
[49090c02] | 260 | #endif |
---|
[647407d] | 261 | } |
---|
| 262 | #endif |
---|
[bb90203] | 263 | data_file("", fnm); /* first argument is current path */ |
---|
[cb3d1e2] | 264 | |
---|
[bb90203] | 265 | optind++; |
---|
| 266 | } |
---|
[cb3d1e2] | 267 | |
---|
[bb90203] | 268 | validate(); |
---|
| 269 | |
---|
| 270 | solve_network(/*stnlist*/); /* Find coordinates of all points */ |
---|
| 271 | validate(); |
---|
[647407d] | 272 | |
---|
[bb90203] | 273 | #ifdef NEW3DFORMAT |
---|
[647407d] | 274 | if (fUseNewFormat) { |
---|
| 275 | cave_close(pimgOut); /* this actually does all the writing */ |
---|
| 276 | } else { |
---|
[bb90203] | 277 | #endif |
---|
[647407d] | 278 | img_close(pimgOut); /* close .3d file */ |
---|
| 279 | #ifdef NEW3DFORMAT |
---|
| 280 | } |
---|
| 281 | #endif |
---|
| 282 | if (fhErrStat) fclose(fhErrStat); |
---|
[bb90203] | 283 | |
---|
[647407d] | 284 | if (!fSuppress) list_pos(root); /* produce .pos file */ |
---|
[bb90203] | 285 | |
---|
| 286 | out_current_action(msg(/*Calculating statistics*/120)); |
---|
| 287 | do_stats(); |
---|
[647407d] | 288 | if (!fQuiet) { |
---|
| 289 | tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC; |
---|
| 290 | tmUser = difftime(time(NULL), tmUserStart); |
---|
| 291 | |
---|
| 292 | /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */ |
---|
| 293 | if (tmCPU + 1 > tmUser) { |
---|
| 294 | out_printf((msg(/*CPU time used %5.2fs*/140), tmCPU)); |
---|
| 295 | } else if (tmCPU == 0) { |
---|
| 296 | if (tmUser == 0.0) { |
---|
| 297 | out_printf((msg(/*Time used %5.2fs*/141), tmUser)); |
---|
| 298 | } else { |
---|
| 299 | out_puts(msg(/*Time used unavailable*/142)); |
---|
| 300 | } |
---|
[bb90203] | 301 | } else { |
---|
[647407d] | 302 | out_printf((msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), |
---|
| 303 | tmUser, tmCPU)); |
---|
[bb90203] | 304 | } |
---|
| 305 | |
---|
[647407d] | 306 | out_puts(msg(/*Done.*/144)); |
---|
| 307 | } |
---|
[bb90203] | 308 | return error_summary(); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | static void |
---|
| 312 | do_range(FILE *fh, int d, int msg1, int msg2, int msg3) |
---|
| 313 | { |
---|
[647407d] | 314 | char buf[1024]; |
---|
[bb90203] | 315 | sprintf(buf, msg(msg1), max[d] - min[d]); |
---|
| 316 | strcat(buf, sprint_prefix(pfxHi[d])); |
---|
| 317 | sprintf(buf + strlen(buf), msg(msg2), max[d]); |
---|
| 318 | strcat(buf, sprint_prefix(pfxLo[d])); |
---|
| 319 | sprintf(buf + strlen(buf), msg(msg3), min[d]); |
---|
[647407d] | 320 | if (!fMute) out_puts(buf); |
---|
[407084d] | 321 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 322 | } |
---|
| 323 | |
---|
| 324 | static void |
---|
| 325 | do_stats(void) |
---|
| 326 | { |
---|
[407084d] | 327 | FILE *fh = NULL; |
---|
[bb90203] | 328 | long cLoops = cComponents + cLegs - cStns; |
---|
| 329 | char buf[1024]; |
---|
| 330 | |
---|
[647407d] | 331 | if (!fSuppress) |
---|
| 332 | fh = safe_fopen_with_ext(fnm_output_base, EXT_SVX_STAT, "w"); |
---|
[bb90203] | 333 | |
---|
| 334 | out_puts(""); |
---|
| 335 | |
---|
| 336 | if (cStns == 1) |
---|
| 337 | sprintf(buf, msg(/*Survey contains 1 survey station,*/172)); |
---|
| 338 | else |
---|
| 339 | sprintf(buf, |
---|
| 340 | msg(/*Survey contains %ld survey stations,*/173), cStns); |
---|
| 341 | |
---|
| 342 | if (cLegs == 1) |
---|
| 343 | sprintf(buf + strlen(buf), msg(/* joined by 1 leg.*/174)); |
---|
| 344 | else |
---|
| 345 | sprintf(buf + strlen(buf), |
---|
| 346 | msg(/* joined by %ld legs.*/175), cLegs); |
---|
| 347 | |
---|
[647407d] | 348 | if (!fMute) out_puts(buf); |
---|
[407084d] | 349 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 350 | |
---|
| 351 | if (cLoops == 1) |
---|
| 352 | sprintf(buf, msg(/*There is 1 loop.*/138)); |
---|
| 353 | else |
---|
| 354 | sprintf(buf, msg(/*There are %ld loops.*/139), cLoops); |
---|
| 355 | |
---|
[647407d] | 356 | if (!fMute) out_puts(buf); |
---|
[407084d] | 357 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 358 | |
---|
| 359 | if (cComponents != 1) { |
---|
| 360 | sprintf(buf, |
---|
| 361 | msg(/*Survey has %ld connected components.*/178), cComponents); |
---|
[647407d] | 362 | if (!fMute) out_puts(buf); |
---|
[407084d] | 363 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 364 | } |
---|
| 365 | |
---|
| 366 | sprintf(buf, |
---|
| 367 | msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132), |
---|
| 368 | total, totadj); |
---|
[647407d] | 369 | if (!fMute) out_puts(buf); |
---|
[407084d] | 370 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 371 | |
---|
| 372 | sprintf(buf, |
---|
| 373 | msg(/*Total plan length of survey legs = %7.2fm*/133), totplan); |
---|
[647407d] | 374 | if (!fMute) out_puts(buf); |
---|
[407084d] | 375 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 376 | |
---|
| 377 | sprintf(buf, msg(/*Total vertical length of survey legs = %7.2fm*/134), |
---|
| 378 | totvert); |
---|
[647407d] | 379 | if (!fMute) out_puts(buf); |
---|
[407084d] | 380 | if (fh) fputsnl(buf, fh); |
---|
[bb90203] | 381 | |
---|
| 382 | do_range(fh, 2, /*Vertical range = %4.2fm (from */135, |
---|
| 383 | /* at %4.2fm to */136, /* at %4.2fm)*/137); |
---|
| 384 | do_range(fh, 1, /*North-South range = %4.2fm (from */148, |
---|
| 385 | /* at %4.2fm to */196, /* at %4.2fm)*/197); |
---|
| 386 | do_range(fh, 0, /*East-West range = %4.2fm (from */149, |
---|
| 387 | /* at %4.2fm to */196, /* at %4.2fm)*/197); |
---|
| 388 | |
---|
| 389 | print_node_stats(fh); |
---|
| 390 | /* Also, could give: |
---|
| 391 | * # nodes stations (ie have other than two references or are fixed) |
---|
| 392 | * # fixed stations (list of?) |
---|
| 393 | */ |
---|
| 394 | |
---|
[407084d] | 395 | if (fh) fclose(fh); |
---|
[bb90203] | 396 | } |
---|