1 | /* cad3d.c |
---|
2 | * Converts a .3d file to CAD-like formats (DXF, Sketch) and also Compass PLT. |
---|
3 | * Also useful as an example of how to use the img code in your own programs |
---|
4 | */ |
---|
5 | |
---|
6 | /* Copyright (C) 1994-2002 Olly Betts |
---|
7 | * |
---|
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. |
---|
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 |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
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 |
---|
21 | */ |
---|
22 | |
---|
23 | /* #define DEBUG_CAD3D */ |
---|
24 | |
---|
25 | #ifdef HAVE_CONFIG_H |
---|
26 | #include <config.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <float.h> |
---|
30 | #include <math.h> |
---|
31 | #include <stdio.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <string.h> |
---|
34 | |
---|
35 | #include "cmdline.h" |
---|
36 | #include "debug.h" |
---|
37 | #include "filename.h" |
---|
38 | #include "hash.h" |
---|
39 | #include "img.h" |
---|
40 | #include "message.h" |
---|
41 | #include "useful.h" |
---|
42 | |
---|
43 | /* default values - can be overridden with --htext and --msize respectively */ |
---|
44 | #define TEXT_HEIGHT 0.6 |
---|
45 | #define MARKER_SIZE 0.8 |
---|
46 | |
---|
47 | #define GRID_SPACING 100 |
---|
48 | |
---|
49 | #define POINTS_PER_INCH 72.0 |
---|
50 | #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH) |
---|
51 | |
---|
52 | /* default to DXF */ |
---|
53 | #define FMT_DEFAULT FMT_DXF |
---|
54 | |
---|
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) */ |
---|
63 | static double scale = 500.0; |
---|
64 | static double factor; |
---|
65 | |
---|
66 | static img *pimg; |
---|
67 | static const char *survey = NULL; |
---|
68 | |
---|
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; |
---|
123 | #ifdef DEBUG_CAD3D |
---|
124 | printf("x_min: %f y_min: %f\n", x, y); |
---|
125 | #endif |
---|
126 | while (x < max_x) { |
---|
127 | /* horizontal line */ |
---|
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"); |
---|
136 | x += grid; |
---|
137 | } |
---|
138 | while (y < max_y) { |
---|
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"); |
---|
148 | y += grid; |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | static void |
---|
154 | dxf_start_pass(int layer) |
---|
155 | { |
---|
156 | layer = layer; |
---|
157 | } |
---|
158 | |
---|
159 | static void |
---|
160 | dxf_move(const img_point *p) |
---|
161 | { |
---|
162 | p = p; |
---|
163 | } |
---|
164 | |
---|
165 | static void |
---|
166 | dxf_line(const img_point *p1, const img_point *p) |
---|
167 | { |
---|
168 | fprintf(fh, "0\nLINE\n"); |
---|
169 | fprintf(fh, "8\nCentreLine\n"); /* Layer */ |
---|
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); |
---|
176 | } |
---|
177 | |
---|
178 | static void |
---|
179 | dxf_label(const img_point *p, const char *s) |
---|
180 | { |
---|
181 | /* write station label to dxf file */ |
---|
182 | fprintf(fh, "0\nTEXT\n"); |
---|
183 | fprintf(fh, "8\nLabels\n"); /* Layer */ |
---|
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); |
---|
187 | fprintf(fh, "40\n%6.2f\n", text_height); |
---|
188 | fprintf(fh, "1\n%s\n", s); |
---|
189 | } |
---|
190 | |
---|
191 | static void |
---|
192 | dxf_cross(const img_point *p) |
---|
193 | { |
---|
194 | /* write station marker to dxf file */ |
---|
195 | fprintf(fh, "0\nPOINT\n"); |
---|
196 | fprintf(fh, "8\nStations\n"); /* Layer */ |
---|
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); |
---|
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"); |
---|
214 | fprintf(fh, "layout((%.3f,%.3f),0)\n", |
---|
215 | (max_x - min_x) * factor, (max_y - min_y) * factor); |
---|
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 |
---|
233 | sketch_move(const img_point *p) |
---|
234 | { |
---|
235 | fprintf(fh, "b()\n"); |
---|
236 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", p->x * factor, p->y * factor, 0.0); |
---|
237 | } |
---|
238 | |
---|
239 | static void |
---|
240 | sketch_line(const img_point *p1, const img_point *p) |
---|
241 | { |
---|
242 | p1 = p1; |
---|
243 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", p->x * factor, p->y * factor, 0.0); |
---|
244 | } |
---|
245 | |
---|
246 | static void |
---|
247 | sketch_label(const img_point *p, const char *s) |
---|
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 | } |
---|
259 | fprintf(fh, "',(%.3f,%.3f))\n", p->x * factor, p->y * factor); |
---|
260 | } |
---|
261 | |
---|
262 | static void |
---|
263 | sketch_cross(const img_point *p) |
---|
264 | { |
---|
265 | fprintf(fh, "b()\n"); |
---|
266 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", |
---|
267 | p->x * factor - MARKER_SIZE, p->y * factor - MARKER_SIZE, 0.0); |
---|
268 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", |
---|
269 | p->x * factor + MARKER_SIZE, p->y * factor + MARKER_SIZE, 0.0); |
---|
270 | fprintf(fh, "bn()\n"); |
---|
271 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", |
---|
272 | p->x * factor + MARKER_SIZE, p->y * factor - MARKER_SIZE, 0.0); |
---|
273 | fprintf(fh, "bs(%.3f,%.3f,%.3f)\n", |
---|
274 | p->x * factor - MARKER_SIZE, p->y * factor + MARKER_SIZE, 0.0); |
---|
275 | } |
---|
276 | |
---|
277 | static void |
---|
278 | sketch_footer(void) |
---|
279 | { |
---|
280 | fprintf(fh, "guidelayer('Guide Lines',1,0,0,1,(0,0,1))\n"); |
---|
281 | if (grid) { |
---|
282 | fprintf(fh, "grid((0,0,%.3f,%.3f),1,(0,0,1),'Grid')\n", |
---|
283 | grid * factor, grid * factor); |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
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 | { |
---|
298 | size_t i; |
---|
299 | htab = osmalloc(0x2000 * sizeof(point*)); |
---|
300 | for (i = 0; i < 0x2000; ++i) htab[i] = NULL; |
---|
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); |
---|
307 | } |
---|
308 | |
---|
309 | static void |
---|
310 | plt_start_pass(int layer) |
---|
311 | { |
---|
312 | layer = layer; |
---|
313 | } |
---|
314 | |
---|
315 | static void |
---|
316 | set_name(const img_point *p, const char *s) |
---|
317 | { |
---|
318 | int hash; |
---|
319 | point *pt; |
---|
320 | union { |
---|
321 | char data[sizeof(int) * 3]; |
---|
322 | int x[3]; |
---|
323 | } u; |
---|
324 | |
---|
325 | u.x[0] = p->x * 100; |
---|
326 | u.x[1] = p->y * 100; |
---|
327 | u.x[2] = p->z * 100; |
---|
328 | hash = (hash_data(u.data, sizeof(int) * 3) & 0x1fff); |
---|
329 | for (pt = htab[hash]; pt; pt = pt->next) { |
---|
330 | if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z) { |
---|
331 | /* already got name for these coordinates */ |
---|
332 | /* FIXME: what about multiple names for the same station? */ |
---|
333 | return; |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | pt = osnew(point); |
---|
338 | pt->label = osstrdup(s); |
---|
339 | pt->p = *p; |
---|
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); |
---|
356 | |
---|
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 |
---|
369 | plt_move(const img_point *p) |
---|
370 | { |
---|
371 | /* Survex is E, N, Alt - PLT file is N, E, Alt */ |
---|
372 | fprintf(fh, "M %.3f %.3f %.3f ", |
---|
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)); |
---|
375 | } |
---|
376 | |
---|
377 | static void |
---|
378 | plt_line(const img_point *p1, const img_point *p) |
---|
379 | { |
---|
380 | p1 = p1; |
---|
381 | /* Survex is E, N, Alt - PLT file is N, E, Alt */ |
---|
382 | fprintf(fh, "D %.3f %.3f %.3f ", |
---|
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)); |
---|
385 | } |
---|
386 | |
---|
387 | static void |
---|
388 | plt_label(const img_point *p, const char *s) |
---|
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 | } |
---|
395 | set_name(p, s); |
---|
396 | } |
---|
397 | |
---|
398 | static void |
---|
399 | plt_cross(const img_point *p) |
---|
400 | { |
---|
401 | p = p; |
---|
402 | } |
---|
403 | |
---|
404 | static void |
---|
405 | plt_footer(void) |
---|
406 | { |
---|
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); |
---|
412 | /* Yucky DOS "end of textfile" marker */ |
---|
413 | putc('\x1a', fh); |
---|
414 | } |
---|
415 | |
---|
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 }; |
---|
421 | static int plt_passes[] = { LABELS, LEGS, 0 }; |
---|
422 | |
---|
423 | int |
---|
424 | main(int argc, char **argv) |
---|
425 | { |
---|
426 | char *fnm_3d, *fnm_out; |
---|
427 | unsigned char labels, crosses, legs; |
---|
428 | int item; |
---|
429 | int fSeenMove = 0; |
---|
430 | img_point p, p1; |
---|
431 | int elevation = 0; |
---|
432 | double elev_angle = 0; |
---|
433 | double s = 0, c = 0; |
---|
434 | enum { FMT_DXF = 0, FMT_SKETCH, FMT_PLT, FMT_AUTO } format; |
---|
435 | static const char *extensions[] = { "dxf", "sk", "plt" }; |
---|
436 | int *pass; |
---|
437 | |
---|
438 | void (*header)(void); |
---|
439 | void (*start_pass)(int); |
---|
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 *); |
---|
444 | void (*footer)(void); |
---|
445 | const char *mode = "w"; /* default to text output */ |
---|
446 | |
---|
447 | /* TRANSLATE */ |
---|
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 */ |
---|
450 | {"survey", required_argument, 0, 's'}, |
---|
451 | {"no-crosses", no_argument, 0, 'c'}, |
---|
452 | {"no-station-names", no_argument, 0, 'n'}, |
---|
453 | {"no-legs", no_argument, 0, 'l'}, |
---|
454 | {"grid", optional_argument, 0, 'g'}, |
---|
455 | {"text-height", required_argument, 0, 't'}, |
---|
456 | {"marker-size", required_argument, 0, 'm'}, |
---|
457 | {"elevation", required_argument, 0, 'e'}, |
---|
458 | {"reduction", required_argument, 0, 'r'}, |
---|
459 | {"dxf", no_argument, 0, 'D'}, |
---|
460 | {"sketch", no_argument, 0, 'S'}, |
---|
461 | {"plt", no_argument, 0, 'P'}, |
---|
462 | {"help", no_argument, 0, HLP_HELP}, |
---|
463 | {"version", no_argument, 0, HLP_VERSION}, |
---|
464 | {0,0,0,0} |
---|
465 | }; |
---|
466 | |
---|
467 | #define short_opts "s:cnlg::t:m:er::DSPh" |
---|
468 | |
---|
469 | /* TRANSLATE */ |
---|
470 | static struct help_msg help[] = { |
---|
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"}, |
---|
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)")"}, |
---|
478 | {HLP_ENCODELONG(7), "produce an elevation view"}, |
---|
479 | {HLP_ENCODELONG(8), "factor to scale down by (default 500)"}, |
---|
480 | {HLP_ENCODELONG(9), "produce DXF output"}, |
---|
481 | {HLP_ENCODELONG(10), "produce Sketch output"}, |
---|
482 | {HLP_ENCODELONG(11), "produce Compass PLT output for Carto"}, |
---|
483 | {0,0} |
---|
484 | }; |
---|
485 | |
---|
486 | msg_init(argv); |
---|
487 | |
---|
488 | /* Defaults */ |
---|
489 | crosses = 1; |
---|
490 | labels = 1; |
---|
491 | legs = 1; |
---|
492 | grid = 0; |
---|
493 | text_height = TEXT_HEIGHT; |
---|
494 | marker_size = MARKER_SIZE; |
---|
495 | format = FMT_AUTO; |
---|
496 | |
---|
497 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2); |
---|
498 | while (1) { |
---|
499 | int opt = cmdline_getopt(); |
---|
500 | if (opt == EOF) break; |
---|
501 | switch (opt) { |
---|
502 | case 'e': /* Elevation */ |
---|
503 | elevation = 1; |
---|
504 | elev_angle = cmdline_double_arg(); |
---|
505 | break; |
---|
506 | case 'c': /* Crosses */ |
---|
507 | crosses = 0; |
---|
508 | break; |
---|
509 | case 'n': /* Labels */ |
---|
510 | labels = 0; |
---|
511 | break; |
---|
512 | case 'l': /* Legs */ |
---|
513 | legs = 0; |
---|
514 | break; |
---|
515 | case 'g': /* Grid */ |
---|
516 | if (optarg) { |
---|
517 | grid = cmdline_double_arg(); |
---|
518 | } else { |
---|
519 | grid = (double)GRID_SPACING; |
---|
520 | } |
---|
521 | break; |
---|
522 | case 'r': /* Reduction factor */ |
---|
523 | scale = cmdline_double_arg(); |
---|
524 | break; |
---|
525 | case 't': /* Text height */ |
---|
526 | text_height = cmdline_double_arg(); |
---|
527 | #ifdef DEBUG_CAD3D |
---|
528 | printf("Text Height: `%s' input, converted to %6.2f\n", optarg, text_height); |
---|
529 | #endif |
---|
530 | break; |
---|
531 | case 'm': /* Marker size */ |
---|
532 | marker_size = cmdline_double_arg(); |
---|
533 | #ifdef DEBUG_CAD3D |
---|
534 | printf("Marker Size: `%s', converted to %6.2f\n", optarg, marker_size); |
---|
535 | #endif |
---|
536 | break; |
---|
537 | case 'D': |
---|
538 | format = FMT_DXF; |
---|
539 | break; |
---|
540 | case 'S': |
---|
541 | format = FMT_SKETCH; |
---|
542 | break; |
---|
543 | case 'P': |
---|
544 | format = FMT_PLT; |
---|
545 | break; |
---|
546 | case 's': |
---|
547 | survey = optarg; |
---|
548 | break; |
---|
549 | #ifdef DEBUG_CAD3D |
---|
550 | default: |
---|
551 | printf("Internal Error: 'getopt' returned '%c' %d\n", opt, opt); |
---|
552 | #endif |
---|
553 | } |
---|
554 | } |
---|
555 | |
---|
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; |
---|
575 | /* note : memory allocated by fnm_out gets leaked in this case... */ |
---|
576 | fnm_out = add_ext(baseleaf, extensions[format]); |
---|
577 | osfree(baseleaf); |
---|
578 | } |
---|
579 | |
---|
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; |
---|
600 | factor = POINTS_PER_MM * 1000.0 / scale; |
---|
601 | mode = "wb"; /* Binary file output */ |
---|
602 | break; |
---|
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; |
---|
612 | mode = "wb"; /* Binary file output */ |
---|
613 | break; |
---|
614 | default: |
---|
615 | exit(1); |
---|
616 | } |
---|
617 | |
---|
618 | pimg = img_open_survey(fnm_3d, survey); |
---|
619 | if (!pimg) fatalerror(img_error(), fnm_3d); |
---|
620 | |
---|
621 | fh = safe_fopen(fnm_out, mode); |
---|
622 | |
---|
623 | if (elevation) { |
---|
624 | s = sin(rad(elev_angle)); |
---|
625 | c = cos(rad(elev_angle)); |
---|
626 | } |
---|
627 | |
---|
628 | /* Get drawing corners */ |
---|
629 | min_x = min_y = min_z = HUGE_VAL; |
---|
630 | max_x = max_y = max_z = -HUGE_VAL; |
---|
631 | do { |
---|
632 | item = img_read_item(pimg, &p); |
---|
633 | |
---|
634 | if (elevation) { |
---|
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; |
---|
640 | } |
---|
641 | |
---|
642 | switch (item) { |
---|
643 | case img_MOVE: case img_LINE: case img_LABEL: |
---|
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; |
---|
651 | } |
---|
652 | } while (item != img_STOP); |
---|
653 | |
---|
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 | |
---|
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 | } |
---|
666 | |
---|
667 | /* Header */ |
---|
668 | header(); |
---|
669 | |
---|
670 | p1.x = p1.y = p1.z = 0; /* avoid compiler warning */ |
---|
671 | |
---|
672 | while (*pass) { |
---|
673 | if (((*pass & LEGS) && legs) || |
---|
674 | ((*pass & STNS) && crosses) || |
---|
675 | ((*pass & LABELS) && labels)) { |
---|
676 | img_rewind(pimg); |
---|
677 | start_pass(*pass); |
---|
678 | do { |
---|
679 | item = img_read_item(pimg, &p); |
---|
680 | |
---|
681 | if (format == FMT_SKETCH) { |
---|
682 | p.x -= min_x; |
---|
683 | p.y -= min_y; |
---|
684 | p.z -= min_z; |
---|
685 | } |
---|
686 | |
---|
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 | } |
---|
694 | |
---|
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: |
---|
701 | #ifdef DEBUG_CAD3D |
---|
702 | printf("line to %9.2f %9.2f %9.2f\n", p.x, p.y, p.z); |
---|
703 | #endif |
---|
704 | if (!fSeenMove) { |
---|
705 | #ifdef DEBUG_CAD3D |
---|
706 | printf("Something is wrong -- img_LINE before any img_MOVE!\n"); |
---|
707 | #endif |
---|
708 | img_close(pimg); |
---|
709 | fatalerror(/*Bad 3d image file `%s'*/106, fnm_3d); |
---|
710 | } |
---|
711 | if ((*pass & LEGS) && legs) line(&p1, &p); |
---|
712 | p1 = p; |
---|
713 | break; |
---|
714 | case img_MOVE: |
---|
715 | #ifdef DEBUG_CAD3D |
---|
716 | printf("move to %9.2f %9.2f %9.2f\n",x,y,z); |
---|
717 | #endif |
---|
718 | if ((*pass & LEGS) && legs) move(&p); |
---|
719 | fSeenMove = 1; |
---|
720 | p1 = p; |
---|
721 | break; |
---|
722 | case img_LABEL: |
---|
723 | #ifdef DEBUG_CAD3D |
---|
724 | printf("label `%s' at %9.2f %9.2f %9.2f\n",pimg->label,x,y,z); |
---|
725 | #endif |
---|
726 | if ((*pass & LABELS) && labels) label(&p, pimg->label); |
---|
727 | if ((*pass & STNS) && crosses) cross(&p); |
---|
728 | break; |
---|
729 | #ifdef DEBUG_CAD3D |
---|
730 | case img_STOP: |
---|
731 | printf("stop\n"); |
---|
732 | break; |
---|
733 | default: |
---|
734 | printf("other info tag (code %d) ignored\n",item); |
---|
735 | #endif |
---|
736 | } |
---|
737 | } while (item != img_STOP); |
---|
738 | } |
---|
739 | pass++; |
---|
740 | } |
---|
741 | img_close(pimg); |
---|
742 | footer(); |
---|
743 | safe_fclose(fh); |
---|
744 | return 0; |
---|
745 | } |
---|