[57e3ad3] | 1 | /* 3dtopos.c */ |
---|
| 2 | /* Produce a .pos file from a .3d file */ |
---|
[45af761] | 3 | /* Copyright (C) 2001,2002,2011 Olly Betts |
---|
[57e3ad3] | 4 | * |
---|
| 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. |
---|
| 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 |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 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 |
---|
[ecbc6c18] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[57e3ad3] | 18 | */ |
---|
| 19 | |
---|
| 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | #include <ctype.h> |
---|
| 25 | #include <math.h> |
---|
| 26 | #include <stdio.h> |
---|
| 27 | #include <stdlib.h> |
---|
| 28 | #include <string.h> |
---|
| 29 | |
---|
| 30 | #include "cmdline.h" |
---|
| 31 | #include "filelist.h" |
---|
[8ca8c26] | 32 | #include "filename.h" |
---|
| 33 | #include "img.h" |
---|
| 34 | #include "message.h" |
---|
[a20d9b9] | 35 | #include "namecmp.h" |
---|
[8ca8c26] | 36 | #include "osalloc.h" |
---|
[57e3ad3] | 37 | |
---|
| 38 | static const struct option long_opts[] = { |
---|
| 39 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
[76bbb7c9] | 40 | {"survey", required_argument, 0, 's'}, |
---|
[57e3ad3] | 41 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 42 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 43 | {0, 0, 0, 0} |
---|
| 44 | }; |
---|
| 45 | |
---|
[76bbb7c9] | 46 | #define short_opts "s:" |
---|
[57e3ad3] | 47 | |
---|
| 48 | static struct help_msg help[] = { |
---|
[45af761] | 49 | /* <-- */ |
---|
| 50 | {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0}, |
---|
| 51 | {0, 0, 0} |
---|
[57e3ad3] | 52 | }; |
---|
| 53 | |
---|
| 54 | typedef struct { |
---|
| 55 | const char *name; |
---|
| 56 | img_point pt; |
---|
| 57 | } station; |
---|
| 58 | |
---|
[ee1ec59] | 59 | static int separator; |
---|
| 60 | |
---|
[57e3ad3] | 61 | static int |
---|
| 62 | cmp_station(const void *a, const void *b) |
---|
| 63 | { |
---|
[ee1ec59] | 64 | return name_cmp(((const station *)a)->name, ((const station *)b)->name, |
---|
| 65 | separator); |
---|
[57e3ad3] | 66 | } |
---|
| 67 | |
---|
[a20d9b9] | 68 | static station *stns; |
---|
| 69 | static OSSIZE_T c_stns = 0; |
---|
[57e3ad3] | 70 | |
---|
| 71 | int |
---|
| 72 | main(int argc, char **argv) |
---|
[421b7d2] | 73 | { |
---|
[57e3ad3] | 74 | char *fnm, *fnm_out; |
---|
| 75 | FILE *fh_out; |
---|
| 76 | img *pimg; |
---|
| 77 | int result; |
---|
[a20d9b9] | 78 | OSSIZE_T c_labels = 0; |
---|
| 79 | OSSIZE_T c_totlabel = 0; |
---|
| 80 | char *p, *p_end; |
---|
[421b7d2] | 81 | const char *survey = NULL; |
---|
[57e3ad3] | 82 | |
---|
[bdfe97f] | 83 | msg_init(argv); |
---|
[57e3ad3] | 84 | |
---|
[d8dbdff] | 85 | cmdline_set_syntax_message(/*3D_FILE [POS_FILE]*/217, 0, NULL); |
---|
[57e3ad3] | 86 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2); |
---|
| 87 | while (1) { |
---|
| 88 | int opt = cmdline_getopt(); |
---|
| 89 | if (opt == EOF) break; |
---|
[76bbb7c9] | 90 | if (opt == 's') survey = optarg; |
---|
[57e3ad3] | 91 | } |
---|
| 92 | |
---|
| 93 | fnm = argv[optind++]; |
---|
| 94 | if (argv[optind]) { |
---|
| 95 | fnm_out = argv[optind]; |
---|
| 96 | } else { |
---|
[b710403] | 97 | char *base = baseleaf_from_fnm(fnm); |
---|
[57e3ad3] | 98 | fnm_out = add_ext(base, EXT_SVX_POS); |
---|
| 99 | osfree(base); |
---|
| 100 | } |
---|
| 101 | |
---|
[a2ad284] | 102 | pimg = img_open_survey(fnm, survey); |
---|
[57e3ad3] | 103 | if (!pimg) fatalerror(img_error(), fnm); |
---|
[ee1ec59] | 104 | separator = pimg->separator; |
---|
[57e3ad3] | 105 | |
---|
| 106 | fh_out = safe_fopen(fnm_out, "w"); |
---|
| 107 | |
---|
| 108 | /* Output headings line */ |
---|
| 109 | fputsnl(msg(/*( Easting, Northing, Altitude )*/195), fh_out); |
---|
| 110 | |
---|
| 111 | do { |
---|
[a20d9b9] | 112 | img_point pt; |
---|
| 113 | result = img_read_item(pimg, &pt); |
---|
[57e3ad3] | 114 | switch (result) { |
---|
| 115 | case img_MOVE: |
---|
| 116 | case img_LINE: |
---|
[421b7d2] | 117 | break; |
---|
[57e3ad3] | 118 | case img_LABEL: |
---|
[a20d9b9] | 119 | c_labels++; |
---|
| 120 | c_totlabel += strlen(pimg->label) + 1; |
---|
[57e3ad3] | 121 | break; |
---|
| 122 | case img_BAD: |
---|
| 123 | img_close(pimg); |
---|
[b8720f8] | 124 | fatalerror(img_error(), fnm); |
---|
[a20d9b9] | 125 | } |
---|
| 126 | } while (result != img_STOP); |
---|
| 127 | |
---|
[b710403] | 128 | /* + 1 to allow for reading coordinates of legs after the last label */ |
---|
| 129 | stns = osmalloc(ossizeof(station) * (c_labels + 1)); |
---|
[a20d9b9] | 130 | p = osmalloc(c_totlabel); |
---|
| 131 | p_end = p + c_totlabel; |
---|
[421b7d2] | 132 | |
---|
[a20d9b9] | 133 | img_rewind(pimg); |
---|
[421b7d2] | 134 | |
---|
[a20d9b9] | 135 | do { |
---|
| 136 | result = img_read_item(pimg, &(stns[c_stns].pt)); |
---|
| 137 | switch (result) { |
---|
| 138 | case img_MOVE: |
---|
| 139 | case img_LINE: |
---|
[421b7d2] | 140 | break; |
---|
[a20d9b9] | 141 | case img_LABEL: |
---|
| 142 | if (c_stns < c_labels) { |
---|
| 143 | OSSIZE_T len = strlen(pimg->label) + 1; |
---|
[76bbb7c9] | 144 | if (p + len <= p_end) { |
---|
[a20d9b9] | 145 | memcpy(p, pimg->label, len); |
---|
| 146 | stns[c_stns++].name = p; |
---|
[b710403] | 147 | p += len; |
---|
[a20d9b9] | 148 | break; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | /* FALLTHRU */ |
---|
| 152 | case img_BAD: |
---|
| 153 | img_close(pimg); |
---|
[113eba2] | 154 | fatalerror(/*Bad 3d image file `%s'*/106, fnm); |
---|
[57e3ad3] | 155 | } |
---|
| 156 | } while (result != img_STOP); |
---|
| 157 | |
---|
[a20d9b9] | 158 | if (c_stns != c_labels || p != p_end) { |
---|
[113eba2] | 159 | img_close(pimg); |
---|
| 160 | fatalerror(/*Bad 3d image file `%s'*/106, fnm); |
---|
[a20d9b9] | 161 | } |
---|
| 162 | |
---|
[113eba2] | 163 | img_close(pimg); |
---|
| 164 | |
---|
[57e3ad3] | 165 | qsort(stns, c_stns, sizeof(station), cmp_station); |
---|
| 166 | |
---|
| 167 | { |
---|
[a20d9b9] | 168 | OSSIZE_T i; |
---|
[57e3ad3] | 169 | for (i = 0; i < c_stns; i++) { |
---|
| 170 | fprintf(fh_out, "(%8.2f, %8.2f, %8.2f ) %s\n", |
---|
| 171 | stns[i].pt.x, stns[i].pt.y, stns[i].pt.z, stns[i].name); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
[f1067a2] | 175 | safe_fclose(fh_out); |
---|
[57e3ad3] | 176 | |
---|
[a20d9b9] | 177 | return EXIT_SUCCESS; |
---|
[57e3ad3] | 178 | } |
---|