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