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