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