[80e25c3] | 1 | /* cad3d.c |
---|
| 2 | * Converts a .3d file to CAD-like formats (DXF, Sketch) and also Compass PLT. |
---|
[bd1913f] | 3 | * Also useful as an example of how to use the img code in your own programs |
---|
| 4 | */ |
---|
[d1b1380] | 5 | |
---|
[e1b99fc] | 6 | /* Copyright (C) 1994-2002 Olly Betts |
---|
[846746e] | 7 | * |
---|
[89231c4] | 8 | * This program is free software; you can redistribute it and/or modify |
---|
| 9 | * it under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | * (at your option) any later version. |
---|
[846746e] | 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
[846746e] | 17 | * |
---|
[89231c4] | 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
[846746e] | 21 | */ |
---|
[d1b1380] | 22 | |
---|
[80e25c3] | 23 | /* #define DEBUG_CAD3D */ |
---|
[d1b1380] | 24 | |
---|
[5ed96af8] | 25 | #ifdef HAVE_CONFIG_H |
---|
[c418627] | 26 | #include <config.h> |
---|
| 27 | #endif |
---|
| 28 | |
---|
[e1b99fc] | 29 | #include <float.h> |
---|
[badeec8] | 30 | #include <math.h> |
---|
[d1b1380] | 31 | #include <stdio.h> |
---|
| 32 | #include <stdlib.h> |
---|
[e1b99fc] | 33 | #include <string.h> |
---|
[5ed96af8] | 34 | |
---|
[c418627] | 35 | #include "cmdline.h" |
---|
[e1b99fc] | 36 | #include "debug.h" |
---|
[fa42426] | 37 | #include "filename.h" |
---|
[e1b99fc] | 38 | #include "hash.h" |
---|
| 39 | #include "img.h" |
---|
[c418627] | 40 | #include "message.h" |
---|
[e1b99fc] | 41 | #include "useful.h" |
---|
[c418627] | 42 | |
---|
[3f60fc8] | 43 | /* default values - can be overridden with --htext and --msize respectively */ |
---|
| 44 | #define TEXT_HEIGHT 0.6 |
---|
[c418627] | 45 | #define MARKER_SIZE 0.8 |
---|
[d1b1380] | 46 | |
---|
[3abc88e] | 47 | #define GRID_SPACING 100 |
---|
| 48 | |
---|
[e343e15f] | 49 | #define POINTS_PER_INCH 72.0 |
---|
| 50 | #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH) |
---|
| 51 | |
---|
[b387524] | 52 | /* default to DXF */ |
---|
| 53 | #define FMT_DEFAULT FMT_DXF |
---|
| 54 | |
---|
[c0f7829] | 55 | static FILE *fh; |
---|
| 56 | |
---|
| 57 | /* bounds */ |
---|
| 58 | static double min_x, min_y, min_z, max_x, max_y, max_z; |
---|
| 59 | |
---|
| 60 | static double text_height; /* for station labels */ |
---|
| 61 | static double marker_size; /* for station markers */ |
---|
| 62 | static double grid; /* grid spacing (or 0 for no grid) */ |
---|
[e343e15f] | 63 | static double scale = 500.0; |
---|
| 64 | static double factor; |
---|
[c0f7829] | 65 | |
---|
[5173649] | 66 | static img *pimg; |
---|
| 67 | static const char *survey = NULL; |
---|
| 68 | |
---|
[c0f7829] | 69 | static void |
---|
| 70 | dxf_header(void) |
---|
| 71 | { |
---|
| 72 | fprintf(fh, "0\nSECTION\n"); |
---|
| 73 | fprintf(fh, "2\nHEADER\n"); |
---|
| 74 | fprintf(fh, "9\n$EXTMIN\n"); /* lower left corner of drawing */ |
---|
| 75 | fprintf(fh, "10\n%#-.6f\n", min_x); /* x */ |
---|
| 76 | fprintf(fh, "20\n%#-.6f\n", min_y); /* y */ |
---|
| 77 | fprintf(fh, "30\n%#-.6f\n", min_z); /* min z */ |
---|
| 78 | fprintf(fh, "9\n$EXTMAX\n"); /* upper right corner of drawing */ |
---|
| 79 | fprintf(fh, "10\n%#-.6f\n", max_x); /* x */ |
---|
| 80 | fprintf(fh, "20\n%#-.6f\n", max_y); /* y */ |
---|
| 81 | fprintf(fh, "30\n%#-.6f\n", max_z); /* max z */ |
---|
| 82 | fprintf(fh, "9\n$PDMODE\n70\n3\n"); /* marker style as CROSS */ |
---|
| 83 | fprintf(fh, "9\n$PDSIZE\n40\n%6.2f\n", marker_size); /* marker size */ |
---|
| 84 | fprintf(fh, "0\nENDSEC\n"); |
---|
| 85 | |
---|
| 86 | fprintf(fh, "0\nSECTION\n"); |
---|
| 87 | fprintf(fh, "2\nTABLES\n"); |
---|
| 88 | fprintf(fh, "0\nTABLE\n"); |
---|
| 89 | fprintf(fh, "2\nLAYER\n"); |
---|
| 90 | fprintf(fh, "70\n10\n"); /* max # off layers in this DXF file : 10 */ |
---|
| 91 | /* First Layer: CentreLine */ |
---|
| 92 | fprintf(fh, "0\nLAYER\n2\nCentreLine\n"); |
---|
| 93 | fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */ |
---|
| 94 | fprintf(fh, "62\n5\n"); /* color: kept the same used by SpeleoGen */ |
---|
| 95 | fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */ |
---|
| 96 | /* Next Layer: Stations */ |
---|
| 97 | fprintf(fh, "0\nLAYER\n2\nStations\n"); |
---|
| 98 | fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */ |
---|
| 99 | fprintf(fh, "62\n7\n"); /* color: kept the same used by SpeleoGen */ |
---|
| 100 | fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */ |
---|
| 101 | /* Next Layer: Labels */ |
---|
| 102 | fprintf(fh, "0\nLAYER\n2\nLabels\n"); |
---|
| 103 | fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */ |
---|
| 104 | fprintf(fh, "62\n7\n"); /* color: kept the same used by SpeleoGen */ |
---|
| 105 | fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */ |
---|
| 106 | if (grid > 0) { |
---|
| 107 | /* Next Layer: Grid */ |
---|
| 108 | fprintf(fh, "0\nLAYER\n2\nGrid\n"); |
---|
| 109 | fprintf(fh, "70\n64\n"); /* shows layer is referenced by entities */ |
---|
| 110 | fprintf(fh, "62\n7\n"); /* color: kept the same used by SpeleoGen */ |
---|
| 111 | fprintf(fh, "6\nCONTINUOUS\n"); /* linetype */ |
---|
| 112 | } |
---|
| 113 | fprintf(fh, "0\nENDTAB\n"); |
---|
| 114 | fprintf(fh, "0\nENDSEC\n"); |
---|
| 115 | |
---|
| 116 | fprintf(fh, "0\nSECTION\n"); |
---|
| 117 | fprintf(fh, "2\nENTITIES\n"); |
---|
| 118 | |
---|
| 119 | if (grid > 0) { |
---|
| 120 | double x, y; |
---|
| 121 | x = floor(min_x / grid) * grid + grid; |
---|
| 122 | y = floor(min_y / grid) * grid + grid; |
---|
[80e25c3] | 123 | #ifdef DEBUG_CAD3D |
---|
[c0f7829] | 124 | printf("x_min: %f y_min: %f\n", x, y); |
---|
| 125 | #endif |
---|
| 126 | while (x < max_x) { |
---|
| 127 | /* horizontal line */ |
---|
[421b7d2] | 128 | fprintf(fh, "0\nLINE\n"); |
---|
| 129 | fprintf(fh, "8\nGrid\n"); /* Layer */ |
---|
| 130 | fprintf(fh, "10\n%6.2f\n", x); |
---|
| 131 | fprintf(fh, "20\n%6.2f\n", min_y); |
---|
| 132 | fprintf(fh, "30\n0\n"); |
---|
| 133 | fprintf(fh, "11\n%6.2f\n", x); |
---|
| 134 | fprintf(fh, "21\n%6.2f\n", max_y); |
---|
| 135 | fprintf(fh, "31\n0\n"); |
---|
[c0f7829] | 136 | x += grid; |
---|
| 137 | } |
---|
| 138 | while (y < max_y) { |
---|
[421b7d2] | 139 | /* vertical line */ |
---|
| 140 | fprintf(fh, "0\nLINE\n"); |
---|
| 141 | fprintf(fh, "8\nGrid\n"); /* Layer */ |
---|
| 142 | fprintf(fh, "10\n%6.2f\n", min_x); |
---|
| 143 | fprintf(fh, "20\n%6.2f\n", y); |
---|
| 144 | fprintf(fh, "30\n0\n"); |
---|
| 145 | fprintf(fh, "11\n%6.2f\n", max_x); |
---|
| 146 | fprintf(fh, "21\n%6.2f\n", y); |
---|
| 147 | fprintf(fh, "31\n0\n"); |
---|
[c0f7829] | 148 | y += grid; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | static void |
---|
| 154 | dxf_start_pass(int layer) |
---|
| 155 | { |
---|
[eb18f4d] | 156 | layer = layer; |
---|
[c0f7829] | 157 | } |
---|
| 158 | |
---|
| 159 | static void |
---|
[6317ee2] | 160 | dxf_move(const img_point *p) |
---|
[c0f7829] | 161 | { |
---|
[eb18f4d] | 162 | p = p; |
---|
[c0f7829] | 163 | } |
---|
| 164 | |
---|
| 165 | static void |
---|
[6317ee2] | 166 | dxf_line(const img_point *p1, const img_point *p) |
---|
[c0f7829] | 167 | { |
---|
| 168 | fprintf(fh, "0\nLINE\n"); |
---|
| 169 | fprintf(fh, "8\nCentreLine\n"); /* Layer */ |
---|
[6317ee2] | 170 | fprintf(fh, "10\n%6.2f\n", p1->x); |
---|
| 171 | fprintf(fh, "20\n%6.2f\n", p1->y); |
---|
| 172 | fprintf(fh, "30\n%6.2f\n", p1->z); |
---|
| 173 | fprintf(fh, "11\n%6.2f\n", p->x); |
---|
| 174 | fprintf(fh, "21\n%6.2f\n", p->y); |
---|
| 175 | fprintf(fh, "31\n%6.2f\n", p->z); |
---|
[c0f7829] | 176 | } |
---|
| 177 | |
---|
| 178 | static void |
---|
[6317ee2] | 179 | dxf_label(const img_point *p, const char *s) |
---|
[c0f7829] | 180 | { |
---|
| 181 | /* write station label to dxf file */ |
---|
| 182 | fprintf(fh, "0\nTEXT\n"); |
---|
| 183 | fprintf(fh, "8\nLabels\n"); /* Layer */ |
---|
[6317ee2] | 184 | fprintf(fh, "10\n%6.2f\n", p->x); |
---|
| 185 | fprintf(fh, "20\n%6.2f\n", p->y); |
---|
| 186 | fprintf(fh, "30\n%6.2f\n", p->z); |
---|
[c0f7829] | 187 | fprintf(fh, "40\n%6.2f\n", text_height); |
---|
| 188 | fprintf(fh, "1\n%s\n", s); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | static void |
---|
[6317ee2] | 192 | dxf_cross(const img_point *p) |
---|
[c0f7829] | 193 | { |
---|
| 194 | /* write station marker to dxf file */ |
---|
| 195 | fprintf(fh, "0\nPOINT\n"); |
---|
| 196 | fprintf(fh, "8\nStations\n"); /* Layer */ |
---|
[6317ee2] | 197 | fprintf(fh, "10\n%6.2f\n", p->x); |
---|
| 198 | fprintf(fh, "20\n%6.2f\n", p->y); |
---|
| 199 | fprintf(fh, "30\n%6.2f\n", p->z); |
---|
[c0f7829] | 200 | } |
---|
| 201 | |
---|
| 202 | static void |
---|
| 203 | dxf_footer(void) |
---|
| 204 | { |
---|
| 205 | fprintf(fh, "000\nENDSEC\n"); |
---|
| 206 | fprintf(fh, "000\nEOF\n"); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | static void |
---|
| 210 | sketch_header(void) |
---|
| 211 | { |
---|
| 212 | fprintf(fh, "##Sketch 1 2\n"); /* Sketch file version */ |
---|
| 213 | fprintf(fh, "document()\n"); |
---|
[e343e15f] | 214 | fprintf(fh, "layout((%.3f,%.3f),0)\n", |
---|
| 215 | (max_x - min_x) * factor, (max_y - min_y) * factor); |
---|
[c0f7829] | 216 | } |
---|
| 217 | |
---|
| 218 | static const char *layer_names[] = { |
---|
| 219 | NULL, |
---|
| 220 | "Legs", |
---|
| 221 | "Stations", |
---|
| 222 | NULL, |
---|
| 223 | "Labels" |
---|
| 224 | }; |
---|
| 225 | |
---|
| 226 | static void |
---|
| 227 | sketch_start_pass(int layer) |
---|
| 228 | { |
---|
| 229 | fprintf(fh, "layer('%s',1,1,0,0,(0,0,0))\n", layer_names[layer]); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | static void |
---|
[6317ee2] | 233 | sketch_move(const img_point *p) |
---|
[c0f7829] | 234 | { |
---|
| 235 | fprintf(fh, "b()\n"); |
---|
[6317ee2] | 236 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", p->x * factor, p->y * factor, 0.0); |
---|
[c0f7829] | 237 | } |
---|
| 238 | |
---|
| 239 | static void |
---|
[6317ee2] | 240 | sketch_line(const img_point *p1, const img_point *p) |
---|
[c0f7829] | 241 | { |
---|
[eb18f4d] | 242 | p1 = p1; |
---|
[6317ee2] | 243 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", p->x * factor, p->y * factor, 0.0); |
---|
[c0f7829] | 244 | } |
---|
| 245 | |
---|
| 246 | static void |
---|
[6317ee2] | 247 | sketch_label(const img_point *p, const char *s) |
---|
[c0f7829] | 248 | { |
---|
| 249 | fprintf(fh, "fp((0,0,0))\n"); |
---|
| 250 | fprintf(fh, "le()\n"); |
---|
| 251 | fprintf(fh, "Fn('Times-Roman')\n"); |
---|
| 252 | fprintf(fh, "Fs(5)\n"); |
---|
| 253 | fprintf(fh, "txt('"); |
---|
| 254 | while (*s) { |
---|
| 255 | int ch = *s++; |
---|
| 256 | if (ch == '\'' || ch == '\\') putc('\\', fh); |
---|
| 257 | putc(ch, fh); |
---|
| 258 | } |
---|
[6317ee2] | 259 | fprintf(fh, "',(%.3f,%.3f))\n", p->x * factor, p->y * factor); |
---|
[c0f7829] | 260 | } |
---|
| 261 | |
---|
| 262 | static void |
---|
[6317ee2] | 263 | sketch_cross(const img_point *p) |
---|
[c0f7829] | 264 | { |
---|
| 265 | fprintf(fh, "b()\n"); |
---|
| 266 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", |
---|
[6317ee2] | 267 | p->x * factor - MARKER_SIZE, p->y * factor - MARKER_SIZE, 0.0); |
---|
[c0f7829] | 268 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", |
---|
[6317ee2] | 269 | p->x * factor + MARKER_SIZE, p->y * factor + MARKER_SIZE, 0.0); |
---|
[c0f7829] | 270 | fprintf(fh, "bn()\n"); |
---|
| 271 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", |
---|
[6317ee2] | 272 | p->x * factor + MARKER_SIZE, p->y * factor - MARKER_SIZE, 0.0); |
---|
[c0f7829] | 273 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", |
---|
[6317ee2] | 274 | p->x * factor - MARKER_SIZE, p->y * factor + MARKER_SIZE, 0.0); |
---|
[c0f7829] | 275 | } |
---|
| 276 | |
---|
| 277 | static void |
---|
| 278 | sketch_footer(void) |
---|
| 279 | { |
---|
| 280 | fprintf(fh, "guidelayer('Guide Lines',1,0,0,1,(0,0,1))\n"); |
---|
[e343e15f] | 281 | if (grid) { |
---|
| 282 | fprintf(fh, "grid((0,0,%.3f,%.3f),1,(0,0,1),'Grid')\n", |
---|
| 283 | grid * factor, grid * factor); |
---|
| 284 | } |
---|
[c0f7829] | 285 | } |
---|
| 286 | |
---|
[e1b99fc] | 287 | typedef struct point { |
---|
| 288 | img_point p; |
---|
| 289 | const char *label; |
---|
| 290 | struct point *next; |
---|
| 291 | } point; |
---|
| 292 | |
---|
| 293 | static point **htab; |
---|
| 294 | |
---|
| 295 | static void |
---|
| 296 | plt_header(void) |
---|
| 297 | { |
---|
[febe041] | 298 | size_t i; |
---|
[6317ee2] | 299 | htab = osmalloc(0x2000 * sizeof(point*)); |
---|
[febe041] | 300 | for (i = 0; i < 0x2000; ++i) htab[i] = NULL; |
---|
[5173649] | 301 | /* Survex is E, N, Alt - PLT file is N, E, Alt */ |
---|
| 302 | fprintf(fh, "Z %.3f %.3f %.3f %.3f %.3f %.3f\r\n", |
---|
| 303 | min_y / METRES_PER_FOOT, max_y / METRES_PER_FOOT, |
---|
| 304 | min_x / METRES_PER_FOOT, max_x / METRES_PER_FOOT, |
---|
| 305 | min_z / METRES_PER_FOOT, max_z / METRES_PER_FOOT); |
---|
| 306 | fprintf(fh, "N%s D 1 1 1 C%s\r\n", survey ? survey : "X", pimg->title); |
---|
[e1b99fc] | 307 | } |
---|
| 308 | |
---|
| 309 | static void |
---|
| 310 | plt_start_pass(int layer) |
---|
| 311 | { |
---|
| 312 | layer = layer; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | static void |
---|
[6317ee2] | 316 | set_name(const img_point *p, const char *s) |
---|
[e1b99fc] | 317 | { |
---|
| 318 | int hash; |
---|
| 319 | point *pt; |
---|
| 320 | union { |
---|
| 321 | char data[sizeof(int) * 3]; |
---|
| 322 | int x[3]; |
---|
| 323 | } u; |
---|
[5757725] | 324 | |
---|
[6317ee2] | 325 | u.x[0] = p->x * 100; |
---|
| 326 | u.x[1] = p->y * 100; |
---|
| 327 | u.x[2] = p->z * 100; |
---|
[e1b99fc] | 328 | hash = (hash_data(u.data, sizeof(int) * 3) & 0x1fff); |
---|
| 329 | for (pt = htab[hash]; pt; pt = pt->next) { |
---|
[6317ee2] | 330 | if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z) { |
---|
[e1b99fc] | 331 | /* already got name for these coordinates */ |
---|
[8f7d8f6] | 332 | /* FIXME: what about multiple names for the same station? */ |
---|
[e1b99fc] | 333 | return; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | pt = osnew(point); |
---|
| 338 | pt->label = osstrdup(s); |
---|
[6317ee2] | 339 | pt->p = *p; |
---|
[e1b99fc] | 340 | pt->next = htab[hash]; |
---|
| 341 | htab[hash] = pt; |
---|
| 342 | |
---|
| 343 | return; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | static const char * |
---|
| 347 | find_name(const img_point *p) |
---|
| 348 | { |
---|
| 349 | int hash; |
---|
| 350 | point *pt; |
---|
| 351 | union { |
---|
| 352 | char data[sizeof(int) * 3]; |
---|
| 353 | int x[3]; |
---|
| 354 | } u; |
---|
| 355 | ASSERT(p); |
---|
[5757725] | 356 | |
---|
[e1b99fc] | 357 | u.x[0] = p->x * 100; |
---|
| 358 | u.x[1] = p->y * 100; |
---|
| 359 | u.x[2] = p->z * 100; |
---|
| 360 | hash = (hash_data(u.data, sizeof(int) * 3) & 0x1fff); |
---|
| 361 | for (pt = htab[hash]; pt; pt = pt->next) { |
---|
| 362 | if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z) |
---|
| 363 | return pt->label; |
---|
| 364 | } |
---|
| 365 | return "?"; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | static void |
---|
[6317ee2] | 369 | plt_move(const img_point *p) |
---|
[e1b99fc] | 370 | { |
---|
| 371 | /* Survex is E, N, Alt - PLT file is N, E, Alt */ |
---|
| 372 | fprintf(fh, "M %.3f %.3f %.3f ", |
---|
[6317ee2] | 373 | p->y / METRES_PER_FOOT, p->x / METRES_PER_FOOT, p->z / METRES_PER_FOOT); |
---|
| 374 | fprintf(fh, "S%s\r\n", find_name(p)); |
---|
[e1b99fc] | 375 | } |
---|
| 376 | |
---|
| 377 | static void |
---|
[6317ee2] | 378 | plt_line(const img_point *p1, const img_point *p) |
---|
[e1b99fc] | 379 | { |
---|
| 380 | p1 = p1; |
---|
| 381 | /* Survex is E, N, Alt - PLT file is N, E, Alt */ |
---|
| 382 | fprintf(fh, "D %.3f %.3f %.3f ", |
---|
[6317ee2] | 383 | p->y / METRES_PER_FOOT, p->x / METRES_PER_FOOT, p->z / METRES_PER_FOOT); |
---|
| 384 | fprintf(fh, "S%s\r\n", find_name(p)); |
---|
[e1b99fc] | 385 | } |
---|
| 386 | |
---|
| 387 | static void |
---|
[6317ee2] | 388 | plt_label(const img_point *p, const char *s) |
---|
[e1b99fc] | 389 | { |
---|
| 390 | /* FIXME: also ctrl characters - ought to remap them, not give up */ |
---|
| 391 | if (strchr(s, ' ')) { |
---|
| 392 | fprintf(stderr, "PLT format can't cope with spaces in station names\n"); |
---|
| 393 | exit(1); |
---|
| 394 | } |
---|
[5757725] | 395 | set_name(p, s); |
---|
[e1b99fc] | 396 | } |
---|
| 397 | |
---|
| 398 | static void |
---|
[6317ee2] | 399 | plt_cross(const img_point *p) |
---|
[e1b99fc] | 400 | { |
---|
| 401 | p = p; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | static void |
---|
| 405 | plt_footer(void) |
---|
| 406 | { |
---|
[5173649] | 407 | /* Survex is E, N, Alt - PLT file is N, E, Alt */ |
---|
| 408 | fprintf(fh, "X %.3f %.3f %.3f %.3f %.3f %.3f\r\n", |
---|
| 409 | min_y / METRES_PER_FOOT, max_y / METRES_PER_FOOT, |
---|
| 410 | min_x / METRES_PER_FOOT, max_x / METRES_PER_FOOT, |
---|
| 411 | min_z / METRES_PER_FOOT, max_z / METRES_PER_FOOT); |
---|
[e1b99fc] | 412 | /* Yucky DOS "end of textfile" marker */ |
---|
[6317ee2] | 413 | putc('\x1a', fh); |
---|
[e1b99fc] | 414 | } |
---|
| 415 | |
---|
[c0f7829] | 416 | #define LEGS 1 |
---|
| 417 | #define STNS 2 |
---|
| 418 | #define LABELS 4 |
---|
| 419 | static int dxf_passes[] = { LEGS|STNS|LABELS, 0 }; |
---|
| 420 | static int sketch_passes[] = { LEGS, STNS, LABELS, 0 }; |
---|
[e1b99fc] | 421 | static int plt_passes[] = { LABELS, LEGS, 0 }; |
---|
[c0f7829] | 422 | |
---|
[0a830d7] | 423 | int |
---|
| 424 | main(int argc, char **argv) |
---|
| 425 | { |
---|
[c0f7829] | 426 | char *fnm_3d, *fnm_out; |
---|
[c418627] | 427 | unsigned char labels, crosses, legs; |
---|
[d1b1380] | 428 | int item; |
---|
[493c523] | 429 | int fSeenMove = 0; |
---|
[0ed0e16] | 430 | img_point p, p1; |
---|
[9624a5a] | 431 | int elevation = 0; |
---|
| 432 | double elev_angle = 0; |
---|
| 433 | double s = 0, c = 0; |
---|
[b387524] | 434 | enum { FMT_DXF = 0, FMT_SKETCH, FMT_PLT, FMT_AUTO } format; |
---|
| 435 | static const char *extensions[] = { "dxf", "sk", "plt" }; |
---|
[c0f7829] | 436 | int *pass; |
---|
| 437 | |
---|
| 438 | void (*header)(void); |
---|
| 439 | void (*start_pass)(int); |
---|
[6317ee2] | 440 | void (*move)(const img_point *); |
---|
| 441 | void (*line)(const img_point *, const img_point *); |
---|
| 442 | void (*label)(const img_point *, const char *); |
---|
| 443 | void (*cross)(const img_point *); |
---|
[c0f7829] | 444 | void (*footer)(void); |
---|
[6317ee2] | 445 | const char *mode = "w"; /* default to text output */ |
---|
[d1b1380] | 446 | |
---|
[647407d] | 447 | /* TRANSLATE */ |
---|
[c418627] | 448 | static const struct option long_opts[] = { |
---|
| 449 | /* const char *name; int has_arg (0 no_argument, 1 required, 2 options_*); int *flag; int val */ |
---|
[76bbb7c9] | 450 | {"survey", required_argument, 0, 's'}, |
---|
[c418627] | 451 | {"no-crosses", no_argument, 0, 'c'}, |
---|
| 452 | {"no-station-names", no_argument, 0, 'n'}, |
---|
| 453 | {"no-legs", no_argument, 0, 'l'}, |
---|
[3abc88e] | 454 | {"grid", optional_argument, 0, 'g'}, |
---|
[9c5aca6] | 455 | {"text-height", required_argument, 0, 't'}, |
---|
| 456 | {"marker-size", required_argument, 0, 'm'}, |
---|
[9624a5a] | 457 | {"elevation", required_argument, 0, 'e'}, |
---|
[e343e15f] | 458 | {"reduction", required_argument, 0, 'r'}, |
---|
[421b7d2] | 459 | {"dxf", no_argument, 0, 'D'}, |
---|
| 460 | {"sketch", no_argument, 0, 'S'}, |
---|
[e1b99fc] | 461 | {"plt", no_argument, 0, 'P'}, |
---|
[c418627] | 462 | {"help", no_argument, 0, HLP_HELP}, |
---|
[83643569] | 463 | {"version", no_argument, 0, HLP_VERSION}, |
---|
[c418627] | 464 | {0,0,0,0} |
---|
| 465 | }; |
---|
[d1b1380] | 466 | |
---|
[e1b99fc] | 467 | #define short_opts "s:cnlg::t:m:er::DSPh" |
---|
[421b7d2] | 468 | |
---|
[647407d] | 469 | /* TRANSLATE */ |
---|
[c418627] | 470 | static struct help_msg help[] = { |
---|
[76bbb7c9] | 471 | {HLP_ENCODELONG(0), "only load the sub-survey with this prefix"}, |
---|
| 472 | {HLP_ENCODELONG(1), "do not generate station markers"}, |
---|
| 473 | {HLP_ENCODELONG(2), "do not generate station labels"}, |
---|
| 474 | {HLP_ENCODELONG(3), "do not generate the survey legs"}, |
---|
[e343e15f] | 475 | {HLP_ENCODELONG(4), "generate grid (default "STRING(GRID_SPACING)"m)"}, |
---|
| 476 | {HLP_ENCODELONG(5), "station labels text height (default "STRING(TEXT_HEIGHT)")"}, |
---|
| 477 | {HLP_ENCODELONG(6), "station marker size (default "STRING(MARKER_SIZE)")"}, |
---|
[76bbb7c9] | 478 | {HLP_ENCODELONG(7), "produce an elevation view"}, |
---|
[e343e15f] | 479 | {HLP_ENCODELONG(8), "factor to scale down by (default 500)"}, |
---|
| 480 | {HLP_ENCODELONG(9), "produce DXF output"}, |
---|
[b387524] | 481 | {HLP_ENCODELONG(10), "produce Sketch output"}, |
---|
[e1b99fc] | 482 | {HLP_ENCODELONG(11), "produce Compass PLT output for Carto"}, |
---|
[cb3d1e2] | 483 | {0,0} |
---|
[c418627] | 484 | }; |
---|
| 485 | |
---|
[bdfe97f] | 486 | msg_init(argv); |
---|
[c418627] | 487 | |
---|
| 488 | /* Defaults */ |
---|
| 489 | crosses = 1; |
---|
| 490 | labels = 1; |
---|
| 491 | legs = 1; |
---|
[3abc88e] | 492 | grid = 0; |
---|
[c418627] | 493 | text_height = TEXT_HEIGHT; |
---|
| 494 | marker_size = MARKER_SIZE; |
---|
[b387524] | 495 | format = FMT_AUTO; |
---|
[c418627] | 496 | |
---|
[493c523] | 497 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2); |
---|
[cb3d1e2] | 498 | while (1) { |
---|
[9c5aca6] | 499 | int opt = cmdline_getopt(); |
---|
[c418627] | 500 | if (opt == EOF) break; |
---|
| 501 | switch (opt) { |
---|
[9624a5a] | 502 | case 'e': /* Elevation */ |
---|
| 503 | elevation = 1; |
---|
| 504 | elev_angle = cmdline_double_arg(); |
---|
[421b7d2] | 505 | break; |
---|
[c418627] | 506 | case 'c': /* Crosses */ |
---|
[421b7d2] | 507 | crosses = 0; |
---|
| 508 | break; |
---|
[c418627] | 509 | case 'n': /* Labels */ |
---|
| 510 | labels = 0; |
---|
| 511 | break; |
---|
| 512 | case 'l': /* Legs */ |
---|
| 513 | legs = 0; |
---|
| 514 | break; |
---|
[3abc88e] | 515 | case 'g': /* Grid */ |
---|
[e343e15f] | 516 | if (optarg) { |
---|
[3abc88e] | 517 | grid = cmdline_double_arg(); |
---|
[e343e15f] | 518 | } else { |
---|
| 519 | grid = (double)GRID_SPACING; |
---|
| 520 | } |
---|
| 521 | break; |
---|
| 522 | case 'r': /* Reduction factor */ |
---|
| 523 | scale = cmdline_double_arg(); |
---|
[3abc88e] | 524 | break; |
---|
[c418627] | 525 | case 't': /* Text height */ |
---|
[acc20b1] | 526 | text_height = cmdline_double_arg(); |
---|
[80e25c3] | 527 | #ifdef DEBUG_CAD3D |
---|
[759fb47] | 528 | printf("Text Height: `%s' input, converted to %6.2f\n", optarg, text_height); |
---|
[c418627] | 529 | #endif |
---|
| 530 | break; |
---|
| 531 | case 'm': /* Marker size */ |
---|
[acc20b1] | 532 | marker_size = cmdline_double_arg(); |
---|
[80e25c3] | 533 | #ifdef DEBUG_CAD3D |
---|
[759fb47] | 534 | printf("Marker Size: `%s', converted to %6.2f\n", optarg, marker_size); |
---|
[c418627] | 535 | #endif |
---|
| 536 | break; |
---|
[c0f7829] | 537 | case 'D': |
---|
| 538 | format = FMT_DXF; |
---|
| 539 | break; |
---|
| 540 | case 'S': |
---|
| 541 | format = FMT_SKETCH; |
---|
| 542 | break; |
---|
[e1b99fc] | 543 | case 'P': |
---|
| 544 | format = FMT_PLT; |
---|
| 545 | break; |
---|
[76bbb7c9] | 546 | case 's': |
---|
| 547 | survey = optarg; |
---|
| 548 | break; |
---|
[80e25c3] | 549 | #ifdef DEBUG_CAD3D |
---|
[c418627] | 550 | default: |
---|
[9624a5a] | 551 | printf("Internal Error: 'getopt' returned '%c' %d\n", opt, opt); |
---|
[9c5aca6] | 552 | #endif |
---|
[c418627] | 553 | } |
---|
[cb3d1e2] | 554 | } |
---|
[c418627] | 555 | |
---|
[b387524] | 556 | fnm_3d = argv[optind++]; |
---|
| 557 | if (argv[optind]) { |
---|
| 558 | fnm_out = argv[optind]; |
---|
| 559 | if (format == FMT_AUTO) { |
---|
| 560 | size_t i; |
---|
| 561 | size_t len = strlen(fnm_out); |
---|
| 562 | format = FMT_DEFAULT; |
---|
| 563 | for (i = 0; i < FMT_AUTO; ++i) { |
---|
| 564 | size_t l = strlen(extensions[i]); |
---|
| 565 | if (len > l + 1 && fnm_out[len - l - 1] == FNM_SEP_EXT && |
---|
| 566 | strcasecmp(fnm_out + len - l, extensions[i]) == 0) { |
---|
| 567 | format = i; |
---|
| 568 | break; |
---|
| 569 | } |
---|
| 570 | } |
---|
| 571 | } |
---|
| 572 | } else { |
---|
| 573 | char *baseleaf = baseleaf_from_fnm(fnm_3d); |
---|
| 574 | if (format == FMT_AUTO) format = FMT_DEFAULT; |
---|
[6317ee2] | 575 | /* note : memory allocated by fnm_out gets leaked in this case... */ |
---|
[b387524] | 576 | fnm_out = add_ext(baseleaf, extensions[format]); |
---|
| 577 | osfree(baseleaf); |
---|
| 578 | } |
---|
| 579 | |
---|
[c0f7829] | 580 | switch (format) { |
---|
| 581 | case FMT_DXF: |
---|
| 582 | header = dxf_header; |
---|
| 583 | start_pass = dxf_start_pass; |
---|
| 584 | move = dxf_move; |
---|
| 585 | line = dxf_line; |
---|
| 586 | label = dxf_label; |
---|
| 587 | cross = dxf_cross; |
---|
| 588 | footer = dxf_footer; |
---|
| 589 | pass = dxf_passes; |
---|
| 590 | break; |
---|
| 591 | case FMT_SKETCH: |
---|
| 592 | header = sketch_header; |
---|
| 593 | start_pass = sketch_start_pass; |
---|
| 594 | move = sketch_move; |
---|
| 595 | line = sketch_line; |
---|
| 596 | label = sketch_label; |
---|
| 597 | cross = sketch_cross; |
---|
| 598 | footer = sketch_footer; |
---|
| 599 | pass = sketch_passes; |
---|
[e343e15f] | 600 | factor = POINTS_PER_MM * 1000.0 / scale; |
---|
[6317ee2] | 601 | mode = "wb"; /* Binary file output */ |
---|
[c0f7829] | 602 | break; |
---|
[e1b99fc] | 603 | case FMT_PLT: |
---|
| 604 | header = plt_header; |
---|
| 605 | start_pass = plt_start_pass; |
---|
| 606 | move = plt_move; |
---|
| 607 | line = plt_line; |
---|
| 608 | label = plt_label; |
---|
| 609 | cross = plt_cross; |
---|
| 610 | footer = plt_footer; |
---|
| 611 | pass = plt_passes; |
---|
[6317ee2] | 612 | mode = "wb"; /* Binary file output */ |
---|
[e1b99fc] | 613 | break; |
---|
[c0f7829] | 614 | default: |
---|
| 615 | exit(1); |
---|
| 616 | } |
---|
| 617 | |
---|
[a2ad284] | 618 | pimg = img_open_survey(fnm_3d, survey); |
---|
[c0f7829] | 619 | if (!pimg) fatalerror(img_error(), fnm_3d); |
---|
[d1b1380] | 620 | |
---|
[6317ee2] | 621 | fh = safe_fopen(fnm_out, mode); |
---|
[9c5aca6] | 622 | |
---|
[9624a5a] | 623 | if (elevation) { |
---|
[e343e15f] | 624 | s = sin(rad(elev_angle)); |
---|
| 625 | c = cos(rad(elev_angle)); |
---|
[9624a5a] | 626 | } |
---|
[cb3d1e2] | 627 | |
---|
[c418627] | 628 | /* Get drawing corners */ |
---|
[ee7bafa] | 629 | min_x = min_y = min_z = HUGE_VAL; |
---|
| 630 | max_x = max_y = max_z = -HUGE_VAL; |
---|
[a420b49] | 631 | do { |
---|
[23f7ea7] | 632 | item = img_read_item(pimg, &p); |
---|
[9624a5a] | 633 | |
---|
| 634 | if (elevation) { |
---|
[e343e15f] | 635 | double xnew = p.x * c - p.y * s; |
---|
| 636 | double znew = - p.x * s - p.y * c; |
---|
| 637 | p.y = p.z; |
---|
| 638 | p.z = znew; |
---|
| 639 | p.x = xnew; |
---|
[9624a5a] | 640 | } |
---|
| 641 | |
---|
[a420b49] | 642 | switch (item) { |
---|
[0a830d7] | 643 | case img_MOVE: case img_LINE: case img_LABEL: |
---|
[421b7d2] | 644 | if (p.x < min_x) min_x = p.x; |
---|
| 645 | if (p.x > max_x) max_x = p.x; |
---|
| 646 | if (p.y < min_y) min_y = p.y; |
---|
| 647 | if (p.y > max_y) max_y = p.y; |
---|
| 648 | if (p.z < min_z) min_z = p.z; |
---|
| 649 | if (p.z > max_z) max_z = p.z; |
---|
| 650 | break; |
---|
[a420b49] | 651 | } |
---|
[cb3d1e2] | 652 | } while (item != img_STOP); |
---|
[9c5aca6] | 653 | |
---|
[3abc88e] | 654 | if (grid > 0) { |
---|
| 655 | min_x -= grid / 2; |
---|
| 656 | max_x += grid / 2; |
---|
| 657 | min_y -= grid / 2; |
---|
| 658 | max_y += grid / 2; |
---|
| 659 | } |
---|
| 660 | |
---|
[9c5aca6] | 661 | /* handle empty file gracefully */ |
---|
| 662 | if (min_x > max_x) { |
---|
| 663 | min_x = min_y = min_z = 0; |
---|
| 664 | max_x = max_y = max_z = 0; |
---|
| 665 | } |
---|
[cb3d1e2] | 666 | |
---|
[c418627] | 667 | /* Header */ |
---|
[c0f7829] | 668 | header(); |
---|
| 669 | |
---|
| 670 | p1.x = p1.y = p1.z = 0; /* avoid compiler warning */ |
---|
| 671 | |
---|
| 672 | while (*pass) { |
---|
[421b7d2] | 673 | if (((*pass & LEGS) && legs) || |
---|
[c0f7829] | 674 | ((*pass & STNS) && crosses) || |
---|
| 675 | ((*pass & LABELS) && labels)) { |
---|
| 676 | img_rewind(pimg); |
---|
| 677 | start_pass(*pass); |
---|
| 678 | do { |
---|
[23f7ea7] | 679 | item = img_read_item(pimg, &p); |
---|
[c0f7829] | 680 | |
---|
| 681 | if (format == FMT_SKETCH) { |
---|
| 682 | p.x -= min_x; |
---|
| 683 | p.y -= min_y; |
---|
| 684 | p.z -= min_z; |
---|
| 685 | } |
---|
[d1b1380] | 686 | |
---|
[c0f7829] | 687 | if (elevation) { |
---|
| 688 | double xnew = p.x * c - p.y * s; |
---|
| 689 | double znew = - p.x * s - p.y * c; |
---|
| 690 | p.y = p.z; |
---|
| 691 | p.z = znew; |
---|
| 692 | p.x = xnew; |
---|
| 693 | } |
---|
[9c5aca6] | 694 | |
---|
[c0f7829] | 695 | switch (item) { |
---|
| 696 | case img_BAD: |
---|
| 697 | img_close(pimg); |
---|
| 698 | fatalerror(/*Bad 3d image file `%s'*/106, fnm_3d); |
---|
| 699 | break; |
---|
| 700 | case img_LINE: |
---|
[80e25c3] | 701 | #ifdef DEBUG_CAD3D |
---|
[c0f7829] | 702 | printf("line to %9.2f %9.2f %9.2f\n", p.x, p.y, p.z); |
---|
[f2588ca] | 703 | #endif |
---|
[c0f7829] | 704 | if (!fSeenMove) { |
---|
[80e25c3] | 705 | #ifdef DEBUG_CAD3D |
---|
[1a3eb43] | 706 | printf("Something is wrong -- img_LINE before any img_MOVE!\n"); |
---|
[d1b1380] | 707 | #endif |
---|
[c0f7829] | 708 | img_close(pimg); |
---|
| 709 | fatalerror(/*Bad 3d image file `%s'*/106, fnm_3d); |
---|
| 710 | } |
---|
[6317ee2] | 711 | if ((*pass & LEGS) && legs) line(&p1, &p); |
---|
[c0f7829] | 712 | p1 = p; |
---|
| 713 | break; |
---|
| 714 | case img_MOVE: |
---|
[80e25c3] | 715 | #ifdef DEBUG_CAD3D |
---|
[c0f7829] | 716 | printf("move to %9.2f %9.2f %9.2f\n",x,y,z); |
---|
[0a830d7] | 717 | #endif |
---|
[6317ee2] | 718 | if ((*pass & LEGS) && legs) move(&p); |
---|
[c0f7829] | 719 | fSeenMove = 1; |
---|
| 720 | p1 = p; |
---|
| 721 | break; |
---|
| 722 | case img_LABEL: |
---|
[80e25c3] | 723 | #ifdef DEBUG_CAD3D |
---|
[23f7ea7] | 724 | printf("label `%s' at %9.2f %9.2f %9.2f\n",pimg->label,x,y,z); |
---|
[0a830d7] | 725 | #endif |
---|
[6317ee2] | 726 | if ((*pass & LABELS) && labels) label(&p, pimg->label); |
---|
| 727 | if ((*pass & STNS) && crosses) cross(&p); |
---|
[c0f7829] | 728 | break; |
---|
[80e25c3] | 729 | #ifdef DEBUG_CAD3D |
---|
[c0f7829] | 730 | case img_STOP: |
---|
| 731 | printf("stop\n"); |
---|
| 732 | break; |
---|
| 733 | default: |
---|
| 734 | printf("other info tag (code %d) ignored\n",item); |
---|
[f2588ca] | 735 | #endif |
---|
| 736 | } |
---|
[c0f7829] | 737 | } while (item != img_STOP); |
---|
[f2588ca] | 738 | } |
---|
[f794f56] | 739 | pass++; |
---|
[f2588ca] | 740 | } |
---|
[d1b1380] | 741 | img_close(pimg); |
---|
[c0f7829] | 742 | footer(); |
---|
[f1067a2] | 743 | safe_fclose(fh); |
---|
[d1b1380] | 744 | return 0; |
---|
| 745 | } |
---|