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