| 1 | /* 3dtopos.c */ |
|---|
| 2 | /* Produce a .pos file from a .3d file */ |
|---|
| 3 | /* Copyright (C) 2001,2002 Olly Betts |
|---|
| 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 |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 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" |
|---|
| 32 | #include "filename.h" |
|---|
| 33 | #include "img.h" |
|---|
| 34 | #include "message.h" |
|---|
| 35 | #include "namecmp.h" |
|---|
| 36 | #include "osalloc.h" |
|---|
| 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; */ |
|---|
| 40 | {"survey", required_argument, 0, 's'}, |
|---|
| 41 | {"help", no_argument, 0, HLP_HELP}, |
|---|
| 42 | {"version", no_argument, 0, HLP_VERSION}, |
|---|
| 43 | {0, 0, 0, 0} |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | #define short_opts "s:" |
|---|
| 47 | |
|---|
| 48 | static struct help_msg help[] = { |
|---|
| 49 | /* <-- */ |
|---|
| 50 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"}, |
|---|
| 51 | {0, 0} |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | typedef struct { |
|---|
| 55 | const char *name; |
|---|
| 56 | img_point pt; |
|---|
| 57 | } station; |
|---|
| 58 | |
|---|
| 59 | static int separator; |
|---|
| 60 | |
|---|
| 61 | static int |
|---|
| 62 | cmp_station(const void *a, const void *b) |
|---|
| 63 | { |
|---|
| 64 | return name_cmp(((const station *)a)->name, ((const station *)b)->name, |
|---|
| 65 | separator); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | static station *stns; |
|---|
| 69 | static OSSIZE_T c_stns = 0; |
|---|
| 70 | |
|---|
| 71 | int |
|---|
| 72 | main(int argc, char **argv) |
|---|
| 73 | { |
|---|
| 74 | char *fnm, *fnm_out; |
|---|
| 75 | FILE *fh_out; |
|---|
| 76 | img *pimg; |
|---|
| 77 | int result; |
|---|
| 78 | OSSIZE_T c_labels = 0; |
|---|
| 79 | OSSIZE_T c_totlabel = 0; |
|---|
| 80 | char *p, *p_end; |
|---|
| 81 | const char *survey = NULL; |
|---|
| 82 | |
|---|
| 83 | msg_init(argv); |
|---|
| 84 | |
|---|
| 85 | cmdline_set_syntax_message("3D_FILE [POS_FILE]", NULL); /* TRANSLATE */ |
|---|
| 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; |
|---|
| 90 | if (opt == 's') survey = optarg; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | fnm = argv[optind++]; |
|---|
| 94 | if (argv[optind]) { |
|---|
| 95 | fnm_out = argv[optind]; |
|---|
| 96 | } else { |
|---|
| 97 | char *base = baseleaf_from_fnm(fnm); |
|---|
| 98 | fnm_out = add_ext(base, EXT_SVX_POS); |
|---|
| 99 | osfree(base); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | pimg = img_open_survey(fnm, survey); |
|---|
| 103 | if (!pimg) fatalerror(img_error(), fnm); |
|---|
| 104 | separator = pimg->separator; |
|---|
| 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 { |
|---|
| 112 | img_point pt; |
|---|
| 113 | result = img_read_item(pimg, &pt); |
|---|
| 114 | switch (result) { |
|---|
| 115 | case img_MOVE: |
|---|
| 116 | case img_LINE: |
|---|
| 117 | break; |
|---|
| 118 | case img_LABEL: |
|---|
| 119 | c_labels++; |
|---|
| 120 | c_totlabel += strlen(pimg->label) + 1; |
|---|
| 121 | break; |
|---|
| 122 | case img_BAD: |
|---|
| 123 | img_close(pimg); |
|---|
| 124 | fatalerror(img_error(), fnm); |
|---|
| 125 | } |
|---|
| 126 | } while (result != img_STOP); |
|---|
| 127 | |
|---|
| 128 | /* + 1 to allow for reading coordinates of legs after the last label */ |
|---|
| 129 | stns = osmalloc(ossizeof(station) * (c_labels + 1)); |
|---|
| 130 | p = osmalloc(c_totlabel); |
|---|
| 131 | p_end = p + c_totlabel; |
|---|
| 132 | |
|---|
| 133 | img_rewind(pimg); |
|---|
| 134 | |
|---|
| 135 | do { |
|---|
| 136 | result = img_read_item(pimg, &(stns[c_stns].pt)); |
|---|
| 137 | switch (result) { |
|---|
| 138 | case img_MOVE: |
|---|
| 139 | case img_LINE: |
|---|
| 140 | break; |
|---|
| 141 | case img_LABEL: |
|---|
| 142 | if (c_stns < c_labels) { |
|---|
| 143 | OSSIZE_T len = strlen(pimg->label) + 1; |
|---|
| 144 | if (p + len <= p_end) { |
|---|
| 145 | memcpy(p, pimg->label, len); |
|---|
| 146 | stns[c_stns++].name = p; |
|---|
| 147 | p += len; |
|---|
| 148 | break; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | /* FALLTHRU */ |
|---|
| 152 | case img_BAD: |
|---|
| 153 | img_close(pimg); |
|---|
| 154 | fatalerror(/*Bad 3d image file `%s'*/106, fnm); |
|---|
| 155 | } |
|---|
| 156 | } while (result != img_STOP); |
|---|
| 157 | |
|---|
| 158 | if (c_stns != c_labels || p != p_end) { |
|---|
| 159 | img_close(pimg); |
|---|
| 160 | fatalerror(/*Bad 3d image file `%s'*/106, fnm); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | img_close(pimg); |
|---|
| 164 | |
|---|
| 165 | qsort(stns, c_stns, sizeof(station), cmp_station); |
|---|
| 166 | |
|---|
| 167 | { |
|---|
| 168 | OSSIZE_T i; |
|---|
| 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 | |
|---|
| 175 | safe_fclose(fh_out); |
|---|
| 176 | |
|---|
| 177 | return EXIT_SUCCESS; |
|---|
| 178 | } |
|---|