[bb90203] | 1 | /* > cavern.c |
---|
| 2 | * SURVEX Cave surveying software: data reduction main and related functions |
---|
| 3 | * Copyright (C) 1991-1999 Olly Betts |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | #ifdef HAVE_CONFIG_H |
---|
| 7 | #include <config.h> |
---|
| 8 | #endif |
---|
| 9 | |
---|
| 10 | #include <time.h> |
---|
| 11 | |
---|
| 12 | #include "datain.h" |
---|
| 13 | #include "debug.h" |
---|
| 14 | #include "validate.h" |
---|
| 15 | #include "cavern.h" |
---|
| 16 | #include "message.h" |
---|
| 17 | #include "filename.h" |
---|
| 18 | #include "filelist.h" |
---|
| 19 | #include "osdepend.h" |
---|
| 20 | #include "img.h" |
---|
| 21 | #include "netbits.h" |
---|
| 22 | #include "commands.h" |
---|
| 23 | #include "cmdline.h" |
---|
| 24 | #include "network.h" |
---|
| 25 | #include "listpos.h" |
---|
| 26 | #include "out.h" |
---|
| 27 | #include "s.h" |
---|
| 28 | |
---|
| 29 | /* For funcs which want to be immune from messing around with different |
---|
| 30 | * calling conventions */ |
---|
| 31 | #ifndef CDECL |
---|
| 32 | # define CDECL |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | /* Globals */ |
---|
| 36 | node *stnlist = NULL; |
---|
| 37 | settings *pcs; |
---|
| 38 | prefix *root; |
---|
| 39 | long cLegs, cStns; |
---|
| 40 | long cComponents; |
---|
| 41 | |
---|
| 42 | #ifndef OUTPUT_TO_CURRENT_DIRECTORY |
---|
| 43 | char *pthOutput; |
---|
| 44 | char *fnmInput; |
---|
| 45 | #endif |
---|
| 46 | |
---|
| 47 | FILE *fhErrStat = NULL; |
---|
| 48 | img *pimgOut = NULL; |
---|
| 49 | #ifndef NO_PERCENTAGE |
---|
| 50 | bool fPercent = fFalse; |
---|
| 51 | #endif |
---|
| 52 | bool fAscii = fFalse; |
---|
| 53 | |
---|
| 54 | real totadj, total, totplan, totvert; |
---|
| 55 | real min[3], max[3]; |
---|
| 56 | prefix *pfxHi[3], *pfxLo[3]; |
---|
| 57 | |
---|
| 58 | char *survey_title = NULL; |
---|
| 59 | int survey_title_len; |
---|
| 60 | |
---|
| 61 | bool fExplicitTitle = fFalse; |
---|
| 62 | |
---|
| 63 | static void do_stats(void); |
---|
| 64 | |
---|
| 65 | static const struct option long_opts[] = { |
---|
| 66 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
| 67 | #ifdef NO_PERCENTAGE |
---|
| 68 | {"percentage", no_argument, 0, 0}, |
---|
| 69 | {"no-percentage", no_argument, 0, 0}, |
---|
| 70 | #else |
---|
| 71 | {"percentage", no_argument, 0, 'p'}, |
---|
| 72 | {"no-percentage", no_argument, (int*)&fPercent, 0}, |
---|
| 73 | #endif |
---|
| 74 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 75 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 76 | {0, 0, 0, 0} |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | #define short_opts "pao:" |
---|
| 80 | |
---|
| 81 | /* TRANSLATE: FIXME extract help messages to message file */ |
---|
| 82 | static struct help_msg help[] = { |
---|
| 83 | /* <-- */ |
---|
| 84 | {HLP_ENCODELONG(0), "display percentage progress"}, |
---|
| 85 | {'a', "output ascii variant of .3d file"}, |
---|
| 86 | /*{'o', "set optimizations for network reduction"},*/ |
---|
| 87 | {0, 0} |
---|
| 88 | }; |
---|
| 89 | |
---|
| 90 | extern CDECL int |
---|
| 91 | main(int argc, char **argv) |
---|
| 92 | { |
---|
| 93 | int d; |
---|
| 94 | static clock_t tmCPUStart; |
---|
| 95 | static time_t tmUserStart; |
---|
| 96 | static double tmCPU, tmUser; |
---|
| 97 | |
---|
| 98 | check_fp_ok(); /* check very early on */ |
---|
| 99 | |
---|
| 100 | tmUserStart = time(NULL); |
---|
| 101 | tmCPUStart = clock(); |
---|
| 102 | init_screen(); |
---|
| 103 | |
---|
| 104 | pthOutput = NULL; |
---|
| 105 | |
---|
| 106 | ReadErrorFile(argv[0]); |
---|
| 107 | |
---|
| 108 | pcs = osnew(settings); |
---|
| 109 | pcs->next = NULL; |
---|
| 110 | pcs->Translate = ((short*) osmalloc(ossizeof(short) * 257)) + 1; |
---|
| 111 | |
---|
| 112 | /* Set up root of prefix hierarchy */ |
---|
| 113 | root = osnew(prefix); |
---|
| 114 | root->up = root->right = root->down = NULL; |
---|
| 115 | root->stn = NULL; |
---|
| 116 | root->pos = NULL; |
---|
| 117 | root->ident = "\\"; |
---|
| 118 | |
---|
| 119 | stnlist = NULL; |
---|
| 120 | cLegs = cStns = cComponents = 0; |
---|
| 121 | totadj = total = totplan = totvert = 0.0; |
---|
| 122 | |
---|
| 123 | for (d = 0; d <= 2; d++) { |
---|
| 124 | min[d] = REAL_BIG; |
---|
| 125 | max[d] = -REAL_BIG; |
---|
| 126 | pfxHi[d] = pfxLo[d] = NULL; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | while (1) { |
---|
| 130 | extern long optimize; /* defined in network.c */ |
---|
| 131 | int opt = my_getopt_long(argc, argv, short_opts, long_opts, NULL, help, 1); |
---|
| 132 | if (opt == EOF) break; |
---|
| 133 | switch (opt) { |
---|
| 134 | case 'a': |
---|
| 135 | fAscii = fTrue; |
---|
| 136 | break; |
---|
| 137 | case 'p': |
---|
| 138 | #ifdef COMPAT |
---|
| 139 | case 'P': |
---|
| 140 | #endif |
---|
| 141 | #ifndef NO_PERCENTAGE |
---|
| 142 | fPercent = 1; |
---|
| 143 | #endif |
---|
| 144 | break; |
---|
| 145 | case 'o': {/* Optimizations level used to solve network */ |
---|
| 146 | static int first_opt_o = 0; |
---|
| 147 | int ch; |
---|
| 148 | if (first_opt_o) { |
---|
| 149 | optimize = 0; |
---|
| 150 | first_opt_o = 1; |
---|
| 151 | } |
---|
| 152 | /* Lollipops, Parallel legs, Iterate mx, Delta*, Split stnlist */ |
---|
| 153 | while ((ch = *optarg++)) if (islower(ch)) optimize |= BITA(ch); |
---|
| 154 | break; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | out_puts(PACKAGE" "VERSION); |
---|
| 160 | out_puts(COPYRIGHT_MSG); |
---|
| 161 | putnl(); |
---|
| 162 | |
---|
| 163 | /* end of options, now process data files */ |
---|
| 164 | while (argv[optind]) { |
---|
| 165 | const char *fnm = argv[optind]; |
---|
| 166 | |
---|
| 167 | if (!fExplicitTitle) { |
---|
| 168 | char *lf; |
---|
| 169 | lf = LfFromFnm(fnm); |
---|
| 170 | if (survey_title) s_catchar(&survey_title, &survey_title_len, ' '); |
---|
| 171 | s_cat(&survey_title, &survey_title_len, lf); |
---|
| 172 | osfree(lf); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /* Select defaults settings */ |
---|
| 176 | default_all(pcs); |
---|
| 177 | |
---|
| 178 | data_file("", fnm); /* first argument is current path */ |
---|
| 179 | |
---|
| 180 | optind++; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | validate(); |
---|
| 184 | |
---|
| 185 | solve_network(/*stnlist*/); /* Find coordinates of all points */ |
---|
| 186 | validate(); |
---|
| 187 | #ifdef NEW3DFORMAT |
---|
| 188 | cave_close(pimgOut); /* close .3d file */ |
---|
| 189 | #else |
---|
| 190 | img_close(pimgOut); /* close .3d file */ |
---|
| 191 | #endif |
---|
| 192 | fclose(fhErrStat); |
---|
| 193 | |
---|
| 194 | list_pos(root); /* produce .pos file */ |
---|
| 195 | |
---|
| 196 | out_current_action(msg(/*Calculating statistics*/120)); |
---|
| 197 | do_stats(); |
---|
| 198 | |
---|
| 199 | tmCPU = (clock_t)(clock() - tmCPUStart) / (double)CLOCKS_PER_SEC; |
---|
| 200 | tmUser = difftime(time(NULL), tmUserStart); |
---|
| 201 | |
---|
| 202 | /* tmCPU is integer, tmUser not - equivalent to (ceil(tmCPU) >= tmUser) */ |
---|
| 203 | if (tmCPU + 1 > tmUser) { |
---|
| 204 | out_printf((msg(/*CPU time used %5.2fs*/140), tmCPU)); |
---|
| 205 | } else if (tmCPU == 0) { |
---|
| 206 | if (tmUser == 0.0) { |
---|
| 207 | out_printf((msg(/*Time used %5.2fs*/141), tmUser)); |
---|
| 208 | } else { |
---|
| 209 | out_puts(msg(/*Time used unavailable*/142)); |
---|
| 210 | } |
---|
| 211 | } else { |
---|
| 212 | out_printf((msg(/*Time used %5.2fs (%5.2fs CPU time)*/143), |
---|
| 213 | tmUser, tmCPU)); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | out_puts(msg(/*Done.*/144)); |
---|
| 217 | return error_summary(); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | static void |
---|
| 221 | do_range(FILE *fh, int d, int msg1, int msg2, int msg3) |
---|
| 222 | { |
---|
| 223 | char buf [1024]; |
---|
| 224 | sprintf(buf, msg(msg1), max[d] - min[d]); |
---|
| 225 | strcat(buf, sprint_prefix(pfxHi[d])); |
---|
| 226 | sprintf(buf + strlen(buf), msg(msg2), max[d]); |
---|
| 227 | strcat(buf, sprint_prefix(pfxLo[d])); |
---|
| 228 | sprintf(buf + strlen(buf), msg(msg3), min[d]); |
---|
| 229 | out_puts(buf); |
---|
| 230 | fputsnl(buf, fh); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | static void |
---|
| 234 | do_stats(void) |
---|
| 235 | { |
---|
| 236 | char *fnm; |
---|
| 237 | FILE *fh; |
---|
| 238 | long cLoops = cComponents + cLegs - cStns; |
---|
| 239 | char buf[1024]; |
---|
| 240 | |
---|
| 241 | #ifdef NO_EXTENSIONS |
---|
| 242 | fnm = UsePth(pthOutput, STATS_FILE); |
---|
| 243 | #else |
---|
| 244 | fnm = AddExt(fnmInput, EXT_SVX_STAT); |
---|
| 245 | #endif |
---|
| 246 | fh = safe_fopen(fnm, "w"); |
---|
| 247 | osfree(fnm); |
---|
| 248 | |
---|
| 249 | out_puts(""); |
---|
| 250 | |
---|
| 251 | if (cStns == 1) |
---|
| 252 | sprintf(buf, msg(/*Survey contains 1 survey station,*/172)); |
---|
| 253 | else |
---|
| 254 | sprintf(buf, |
---|
| 255 | msg(/*Survey contains %ld survey stations,*/173), cStns); |
---|
| 256 | |
---|
| 257 | if (cLegs == 1) |
---|
| 258 | sprintf(buf + strlen(buf), msg(/* joined by 1 leg.*/174)); |
---|
| 259 | else |
---|
| 260 | sprintf(buf + strlen(buf), |
---|
| 261 | msg(/* joined by %ld legs.*/175), cLegs); |
---|
| 262 | |
---|
| 263 | out_puts(buf); |
---|
| 264 | fputsnl(buf, fh); |
---|
| 265 | |
---|
| 266 | if (cLoops == 1) |
---|
| 267 | sprintf(buf, msg(/*There is 1 loop.*/138)); |
---|
| 268 | else |
---|
| 269 | sprintf(buf, msg(/*There are %ld loops.*/139), cLoops); |
---|
| 270 | |
---|
| 271 | out_puts(buf); |
---|
| 272 | fputsnl(buf, fh); |
---|
| 273 | |
---|
| 274 | if (cComponents != 1) { |
---|
| 275 | sprintf(buf, |
---|
| 276 | msg(/*Survey has %ld connected components.*/178), cComponents); |
---|
| 277 | out_puts(buf); |
---|
| 278 | fputsnl(buf, fh); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | sprintf(buf, |
---|
| 282 | msg(/*Total length of survey legs = %7.2fm (%7.2fm adjusted)*/132), |
---|
| 283 | total, totadj); |
---|
| 284 | out_puts(buf); |
---|
| 285 | fputsnl(buf, fh); |
---|
| 286 | |
---|
| 287 | sprintf(buf, |
---|
| 288 | msg(/*Total plan length of survey legs = %7.2fm*/133), totplan); |
---|
| 289 | out_puts(buf); |
---|
| 290 | fputsnl(buf, fh); |
---|
| 291 | |
---|
| 292 | sprintf(buf, msg(/*Total vertical length of survey legs = %7.2fm*/134), |
---|
| 293 | totvert); |
---|
| 294 | out_puts(buf); |
---|
| 295 | fputsnl(buf, fh); |
---|
| 296 | |
---|
| 297 | do_range(fh, 2, /*Vertical range = %4.2fm (from */135, |
---|
| 298 | /* at %4.2fm to */136, /* at %4.2fm)*/137); |
---|
| 299 | do_range(fh, 1, /*North-South range = %4.2fm (from */148, |
---|
| 300 | /* at %4.2fm to */196, /* at %4.2fm)*/197); |
---|
| 301 | do_range(fh, 0, /*East-West range = %4.2fm (from */149, |
---|
| 302 | /* at %4.2fm to */196, /* at %4.2fm)*/197); |
---|
| 303 | |
---|
| 304 | print_node_stats(fh); |
---|
| 305 | /* Also, could give: |
---|
| 306 | * # nodes stations (ie have other than two references or are fixed) |
---|
| 307 | * # fixed stations (list of?) |
---|
| 308 | */ |
---|
| 309 | |
---|
| 310 | fclose(fh); |
---|
| 311 | } |
---|