source: git/src/cad3d.c @ bef66af

RELEASE/1.0RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since bef66af was bef66af, checked in by Olly Betts <olly@…>, 20 years ago

Added SVG support from John Pybus

git-svn-id: file:///home/survex-svn/survex/trunk@2711 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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