- Timestamp:
- Jul 6, 2013, 3:19:05 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/src/RpDxToVtk.c
r3556 r3753 24 24 { 25 25 while (isspace(*string)) { 26 26 string++; 27 27 } 28 28 return string; … … 36 36 line = SkipSpaces(*stringPtr); 37 37 for (p = line; p < endPtr; p++) { 38 39 40 41 38 if (*p == '\n') { 39 *stringPtr = p + 1; 40 return line; 41 } 42 42 } 43 43 *stringPtr = p; … … 46 46 47 47 static int 48 GetPoints(Tcl_Interp *interp, int nPoints, int *counts, char **stringPtr, 49 const char *endPtr, Tcl_Obj *objPtr) 50 { 51 int nValues; 48 GetUniformFieldValues(Tcl_Interp *interp, int nPoints, int *counts, char **stringPtr, 49 const char *endPtr, Tcl_Obj *objPtr) 50 { 52 51 int i; 53 52 const char *p; 54 53 char mesg[2000]; 55 float*array, scale, vmin, vmax;54 double *array, scale, vmin, vmax; 56 55 int iX, iY, iZ; 57 56 58 nValues = 0;59 57 p = *stringPtr; 60 array = malloc(sizeof( float) * nPoints);58 array = malloc(sizeof(double) * nPoints); 61 59 if (array == NULL) { 62 60 return TCL_ERROR; 63 61 } 64 62 vmin = FLT_MAX, vmax = -FLT_MAX; 65 63 iX = iY = iZ = 0; 66 64 for (i = 0; i < nPoints; i++) { 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 65 double value; 66 char *nextPtr; 67 int loc; 68 69 if (p >= endPtr) { 70 Tcl_AppendResult(interp, "unexpected EOF in reading points", 71 (char *)NULL); 72 return TCL_ERROR; 73 } 74 value = strtod(p, &nextPtr); 75 if (nextPtr == p) { 76 Tcl_AppendResult(interp, "bad value found in reading points", 77 (char *)NULL); 78 return TCL_ERROR; 79 } 80 p = nextPtr; 81 loc = iZ*counts[0]*counts[1] + iY*counts[0] + iX; 82 if (++iZ >= counts[2]) { 83 iZ = 0; 84 if (++iY >= counts[1]) { 85 iY = 0; 86 ++iX; 87 } 88 } 89 array[loc] = value; 90 if (value < vmin) { 91 vmin = value; 92 } 93 if (value > vmax) { 94 vmax = value; 95 } 98 96 } 99 97 scale = 1.0 / (vmax - vmin); 100 98 for (i = 0; i < nPoints; i++) { 101 99 #ifdef notdef 102 103 #endif 104 105 100 sprintf(mesg, "%g\n", (array[i] - vmin) * scale); 101 #endif 102 sprintf(mesg, "%g\n", array[i]); 103 Tcl_AppendToObj(objPtr, mesg, -1); 106 104 } 107 105 free(array); … … 110 108 } 111 109 110 static int 111 GetCloudFieldValues(Tcl_Interp *interp, int nXYPoints, int nZPoints, char **stringPtr, 112 const char *endPtr, Tcl_Obj *objPtr) 113 { 114 int i; 115 const char *p; 116 char mesg[2000]; 117 double *array, scale, vmin, vmax; 118 int iXY, iZ; 119 int nPoints; 120 121 nPoints = nXYPoints * nZPoints; 122 123 p = *stringPtr; 124 array = malloc(sizeof(double) * nPoints); 125 if (array == NULL) { 126 return TCL_ERROR; 127 } 128 vmin = FLT_MAX, vmax = -FLT_MAX; 129 iXY = iZ = 0; 130 for (i = 0; i < nPoints; i++) { 131 double value; 132 char *nextPtr; 133 int loc; 134 135 if (p >= endPtr) { 136 Tcl_AppendResult(interp, "unexpected EOF in reading points", 137 (char *)NULL); 138 return TCL_ERROR; 139 } 140 value = strtod(p, &nextPtr); 141 if (nextPtr == p) { 142 Tcl_AppendResult(interp, "bad value found in reading points", 143 (char *)NULL); 144 return TCL_ERROR; 145 } 146 p = nextPtr; 147 loc = nXYPoints * iZ + iXY; 148 if (++iZ >= nZPoints) { 149 iZ = 0; 150 ++iXY; 151 } 152 array[loc] = value; 153 if (value < vmin) { 154 vmin = value; 155 } 156 if (value > vmax) { 157 vmax = value; 158 } 159 } 160 scale = 1.0 / (vmax - vmin); 161 for (i = 0; i < nPoints; i++) { 162 #ifdef notdef 163 sprintf(mesg, "%g\n", (array[i] - vmin) * scale); 164 #endif 165 sprintf(mesg, "%g\n", array[i]); 166 Tcl_AppendToObj(objPtr, mesg, -1); 167 } 168 free(array); 169 *stringPtr = (char *)p; 170 return TCL_OK; 171 } 172 173 static int 174 GetPoints(Tcl_Interp *interp, double *array, int nXYPoints, 175 char **stringPtr, const char *endPtr) 176 { 177 int i; 178 const char *p; 179 180 p = *stringPtr; 181 if (array == NULL) { 182 return TCL_ERROR; 183 } 184 for (i = 0; i < nXYPoints; i++) { 185 double x, y, z; 186 char *nextPtr; 187 188 if (p >= endPtr) { 189 Tcl_AppendResult(interp, "unexpected EOF in reading points", 190 (char *)NULL); 191 return TCL_ERROR; 192 } 193 x = strtod(p, &nextPtr); 194 if (nextPtr == p) { 195 Tcl_AppendResult(interp, "bad value found in reading points", 196 (char *)NULL); 197 return TCL_ERROR; 198 } 199 p = nextPtr; 200 y = strtod(p, &nextPtr); 201 if (nextPtr == p) { 202 Tcl_AppendResult(interp, "bad value found in reading points", 203 (char *)NULL); 204 return TCL_ERROR; 205 } 206 p = nextPtr; 207 z = strtod(p, &nextPtr); 208 if (nextPtr == p) { 209 Tcl_AppendResult(interp, "bad value found in reading points", 210 (char *)NULL); 211 return TCL_ERROR; 212 } 213 p = nextPtr; 214 215 array[i*2 ] = x; 216 array[i*2+1] = y; 217 } 218 219 *stringPtr = (char *)p; 220 return TCL_OK; 221 } 222 112 223 /* 113 224 * DxToVtk string 225 * 226 * In DX format: 227 * rank 0 means scalars, 228 * rank 1 means vectors, 229 * rank 2 means matrices, 230 * rank 3 means tensors 231 * 232 * For rank 1, shape is a single number equal to the number of dimensions. 233 * e.g. rank 1 shape 3 means a 3-component vector field 234 * 114 235 */ 115 236 116 237 static int 117 238 DxToVtkCmd(ClientData clientData, Tcl_Interp *interp, int objc, 118 Tcl_Obj *const *objv) 119 { 120 Tcl_Obj *objPtr, *pointsObjPtr; 239 Tcl_Obj *const *objv) 240 { 241 double *points; 242 Tcl_Obj *objPtr, *pointsObjPtr, *fieldObjPtr; 121 243 char *p, *pend; 122 244 char *string; 123 245 char mesg[2000]; 124 double d elta[3];246 double dx, dy, dz; 125 247 double origin[3]; 126 248 int count[3]; 127 int length, n Components, nPoints;249 int length, nAxes, nPoints, nXYPoints; 128 250 char *name; 251 int isUniform; 252 int i, iz; 129 253 130 254 name = "myScalars"; 131 n Components = nPoints = 0;132 d elta[0] = delta[1] = delta[2]= 0.0; /* Suppress compiler warning. */255 nAxes = nPoints = nXYPoints = 0; 256 dx = dy = dz = 0.0; /* Suppress compiler warning. */ 133 257 origin[0] = origin[1] = origin[2] = 0.0; /* May not have an origin line. */ 134 258 count[0] = count[1] = count[2] = 0; /* Suppress compiler warning. */ 135 259 isUniform = 1; /* Default to expecting uniform grid */ 260 136 261 if (objc != 2) { 137 138 139 262 Tcl_AppendResult(interp, "wrong # arguments: should be \"", 263 Tcl_GetString(objv[0]), " string\"", (char *)NULL); 264 return TCL_ERROR; 140 265 } 141 266 string = Tcl_GetStringFromObj(objv[1], &length); 142 267 if (strncmp("<ODX>", string, 5) == 0) { 143 144 268 string += 5; 269 length -= 5; 145 270 } 146 271 pointsObjPtr = Tcl_NewStringObj("", -1); 272 fieldObjPtr = Tcl_NewStringObj("", -1); 147 273 for (p = string, pend = p + length; p < pend; /*empty*/) { 148 149 150 151 274 char *line; 275 double ddx, ddy, ddz; 276 277 line = GetLine(&p, pend); 152 278 if (line >= pend) { 153 break;/* EOF */154 279 break; /* EOF */ 280 } 155 281 if ((line[0] == '#') || (line[0] == '\n')) { 156 continue; /* Skip blank or comment lines. */ 157 } 158 if (sscanf(line, "object %*d class gridpositions counts %d %d %d", 159 count, count + 1, count + 2) == 3) { 160 if ((count[0] < 0) || (count[1] < 0) || (count[2] < 0)) { 161 sprintf(mesg, "invalid grid size: x=%d, y=%d, z=%d", 162 count[0], count[1], count[2]); 163 Tcl_AppendResult(interp, mesg, (char *)NULL); 164 return TCL_ERROR; 165 } 166 #ifdef notdef 167 fprintf(stderr, "found gridpositions counts %d %d %d\n", 168 count[0], count[1], count[2]); 169 #endif 170 } else if (sscanf(line, "origin %lg %lg %lg", origin, origin + 1, 171 origin + 2) == 3) { 172 /* Found origin. */ 173 #ifdef notdef 174 fprintf(stderr, "found origin %g %g %g\n", 175 origin[0], origin[1], origin[2]); 176 #endif 177 } else if (sscanf(line, "delta %lg %lg %lg", &ddx, &ddy, &ddz) == 3) { 178 if (nComponents == 3) { 179 Tcl_AppendResult(interp, "too many delta statements", 180 (char *)NULL); 181 return TCL_ERROR; 182 } 183 delta[nComponents] = sqrt((ddx * ddx) + (ddy * ddy) + (ddz * ddz)); 184 nComponents++; 185 #ifdef notdef 186 fprintf(stderr, "found delta %g %g %g\n", ddx, ddy, ddx); 187 #endif 188 } else if (sscanf(line, "object %*d class regulararray count %d", 189 &count[2]) == 1) { 190 191 } else if (sscanf(line, "object %*d class array type %*s shape 3" 192 " rank 1 items %d data follows", &nPoints) == 1) { 193 fprintf(stderr, "found class array type shape 3 nPoints=%d\n", 194 nPoints); 195 if (nPoints < 0) { 196 sprintf(mesg, "bad # points %d", nPoints); 197 Tcl_AppendResult(interp, mesg, (char *)NULL); 198 return TCL_ERROR; 199 } 200 if (nPoints != count[0]*count[1]*count[2]) { 201 sprintf(mesg, "inconsistent data: expected %d points" 202 " but found %d points", count[0]*count[1]*count[2], 203 nPoints); 204 Tcl_AppendResult(interp, mesg, (char *)NULL); 205 return TCL_ERROR; 206 } 207 if (GetPoints(interp, nPoints, count, &p, pend, pointsObjPtr) 208 != TCL_OK) { 209 return TCL_ERROR; 210 } 211 } else if (sscanf(line, "object %*d class array type %*s rank 0" 212 " %*s %d data follows", &nPoints) == 1) { 213 #ifdef notdef 214 fprintf(stderr, "found class array type rank 0 nPoints=%d\n", 215 nPoints); 216 #endif 217 if (nPoints != count[0]*count[1]*count[2]) { 218 sprintf(mesg, "inconsistent data: expected %d points" 219 " but found %d points", count[0]*count[1]*count[2], 220 nPoints); 221 Tcl_AppendResult(interp, mesg, (char *)NULL); 222 return TCL_ERROR; 223 } 224 if (GetPoints(interp, nPoints, count, &p, pend, pointsObjPtr) 225 != TCL_OK) { 226 return TCL_ERROR; 227 } 228 #ifdef notdef 229 } else { 230 fprintf(stderr, "unknown line (%.80s)\n", line); 231 #endif 232 } 233 } 234 if (nPoints != count[0]*count[1]*count[2]) { 235 sprintf(mesg, "inconsistent data: expected %d points" 236 " but found %d points", count[0]*count[1]*count[2], 237 nPoints); 238 Tcl_AppendResult(interp, mesg, (char *)NULL); 239 return TCL_ERROR; 240 } 241 242 objPtr = Tcl_NewStringObj("# vtk DataFile Version 2.0\n", -1); 243 Tcl_AppendToObj(objPtr, "Converted from DX file\n", -1); 244 Tcl_AppendToObj(objPtr, "ASCII\n", -1); 245 Tcl_AppendToObj(objPtr, "DATASET STRUCTURED_POINTS\n", -1); 246 sprintf(mesg, "DIMENSIONS %d %d %d\n", count[0], count[1], count[2]); 247 Tcl_AppendToObj(objPtr, mesg, -1); 248 sprintf(mesg, "ORIGIN %g %g %g\n", origin[0], origin[1], origin[2]); 249 Tcl_AppendToObj(objPtr, mesg, -1); 250 sprintf(mesg, "SPACING %g %g %g\n", delta[0], delta[1], delta[2]); 251 Tcl_AppendToObj(objPtr, mesg, -1); 252 sprintf(mesg, "POINT_DATA %d\n", nPoints); 253 Tcl_AppendToObj(objPtr, mesg, -1); 254 sprintf(mesg, "SCALARS %s float 1\n", name); 255 Tcl_AppendToObj(objPtr, mesg, -1); 256 sprintf(mesg, "LOOKUP_TABLE default\n"); 257 Tcl_AppendToObj(objPtr, mesg, -1); 258 Tcl_AppendObjToObj(objPtr, pointsObjPtr); 282 continue; /* Skip blank or comment lines. */ 283 } 284 if (sscanf(line, "object %*d class gridpositions counts %d %d %d", 285 count, count + 1, count + 2) == 3) { 286 isUniform = 1; 287 if ((count[0] < 0) || (count[1] < 0) || (count[2] < 0)) { 288 sprintf(mesg, "invalid grid size: x=%d, y=%d, z=%d", 289 count[0], count[1], count[2]); 290 Tcl_AppendResult(interp, mesg, (char *)NULL); 291 return TCL_ERROR; 292 } 293 #ifdef notdef 294 fprintf(stderr, "found gridpositions counts %d %d %d\n", 295 count[0], count[1], count[2]); 296 #endif 297 } else if (sscanf(line, "origin %lg %lg %lg", origin, origin + 1, 298 origin + 2) == 3) { 299 /* Found origin. */ 300 #ifdef notdef 301 fprintf(stderr, "found origin %g %g %g\n", 302 origin[0], origin[1], origin[2]); 303 #endif 304 } else if (sscanf(line, "delta %lg %lg %lg", &ddx, &ddy, &ddz) == 3) { 305 if (nAxes == 3) { 306 Tcl_AppendResult(interp, "too many delta statements", 307 (char *)NULL); 308 return TCL_ERROR; 309 } 310 if (ddx != 0.0) { 311 if (ddy != 0.0 || ddz != 0.0) { 312 Tcl_AppendResult(interp, "invalid delta statement", 313 (char *)NULL); 314 return TCL_ERROR; 315 } 316 dx = ddx; 317 } else if (ddy != 0.0) { 318 if (ddx != 0.0 || ddz != 0.0) { 319 Tcl_AppendResult(interp, "invalid delta statement", 320 (char *)NULL); 321 return TCL_ERROR; 322 } 323 dy = ddy; 324 } else if (ddz != 0.0) { 325 if (ddx != 0.0 || ddy != 0.0) { 326 Tcl_AppendResult(interp, "invalid delta statement", 327 (char *)NULL); 328 return TCL_ERROR; 329 } 330 dz = ddz; 331 } 332 nAxes++; 333 #ifdef notdef 334 fprintf(stderr, "found delta %g %g %g\n", ddx, ddy, ddx); 335 #endif 336 } else if (sscanf(line, "object %*d class regulararray count %d", 337 &count[2]) == 1) { 338 // Z grid 339 } else if (sscanf(line, "object %*d class array type %*s rank 1 shape 3" 340 " items %d data follows", &nXYPoints) == 1) { 341 // This is a 2D point cloud in xy with a uniform zgrid 342 isUniform = 0; 343 #ifdef notdef 344 fprintf(stderr, "found class array type shape 3 nPoints=%d\n", 345 nPoints); 346 #endif 347 if (nXYPoints < 0) { 348 sprintf(mesg, "bad # points %d", nXYPoints); 349 Tcl_AppendResult(interp, mesg, (char *)NULL); 350 return TCL_ERROR; 351 } 352 points = malloc(sizeof(double) * nXYPoints * 2); 353 if (GetPoints(interp, points, nXYPoints, &p, pend) != TCL_OK) { 354 return TCL_ERROR; 355 } 356 } else if (sscanf(line, "object %*d class array type %*s rank 0" 357 " %*s %d data follows", &nPoints) == 1) { 358 #ifdef notdef 359 fprintf(stderr, "found class array type rank 0 nPoints=%d\n", 360 nPoints); 361 #endif 362 if (isUniform && nPoints != count[0]*count[1]*count[2]) { 363 sprintf(mesg, "inconsistent data: expected %d points" 364 " but found %d points", count[0]*count[1]*count[2], 365 nPoints); 366 Tcl_AppendResult(interp, mesg, (char *)NULL); 367 return TCL_ERROR; 368 } else if (!isUniform && nPoints != nXYPoints * count[2]) { 369 sprintf(mesg, "inconsistent data: expected %d points" 370 " but found %d points", nXYPoints * count[2], 371 nPoints); 372 Tcl_AppendResult(interp, mesg, (char *)NULL); 373 return TCL_ERROR; 374 } 375 if (isUniform) { 376 if (GetUniformFieldValues(interp, nPoints, count, &p, pend, fieldObjPtr) 377 != TCL_OK) { 378 return TCL_ERROR; 379 } 380 } else { 381 if (GetCloudFieldValues(interp, nXYPoints, count[2], &p, pend, fieldObjPtr) 382 != TCL_OK) { 383 return TCL_ERROR; 384 } 385 } 386 #ifdef notdef 387 } else { 388 fprintf(stderr, "unknown line (%.80s)\n", line); 389 #endif 390 } 391 } 392 393 if (isUniform) { 394 objPtr = Tcl_NewStringObj("# vtk DataFile Version 2.0\n", -1); 395 Tcl_AppendToObj(objPtr, "Converted from DX file\n", -1); 396 Tcl_AppendToObj(objPtr, "ASCII\n", -1); 397 Tcl_AppendToObj(objPtr, "DATASET STRUCTURED_POINTS\n", -1); 398 sprintf(mesg, "DIMENSIONS %d %d %d\n", count[0], count[1], count[2]); 399 Tcl_AppendToObj(objPtr, mesg, -1); 400 sprintf(mesg, "ORIGIN %g %g %g\n", origin[0], origin[1], origin[2]); 401 Tcl_AppendToObj(objPtr, mesg, -1); 402 sprintf(mesg, "SPACING %g %g %g\n", dx, dy, dz); 403 Tcl_AppendToObj(objPtr, mesg, -1); 404 sprintf(mesg, "POINT_DATA %d\n", nPoints); 405 Tcl_AppendToObj(objPtr, mesg, -1); 406 sprintf(mesg, "SCALARS %s double 1\n", name); 407 Tcl_AppendToObj(objPtr, mesg, -1); 408 sprintf(mesg, "LOOKUP_TABLE default\n"); 409 Tcl_AppendToObj(objPtr, mesg, -1); 410 Tcl_AppendObjToObj(objPtr, fieldObjPtr); 411 } else { 412 /* Fill points. Have to wait to do this since origin, delta can come after 413 * the point list in the file. 414 */ 415 for (iz = 0; iz < count[2]; iz++) { 416 for (i = 0; i < nXYPoints; i++) { 417 sprintf(mesg, "%g %g %g\n", points[i*2], points[i*2+1], origin[2] + dz * iz); 418 Tcl_AppendToObj(pointsObjPtr, mesg, -1); 419 } 420 } 421 free(points); 422 423 objPtr = Tcl_NewStringObj("# vtk DataFile Version 2.0\n", -1); 424 Tcl_AppendToObj(objPtr, "Converted from DX file\n", -1); 425 Tcl_AppendToObj(objPtr, "ASCII\n", -1); 426 Tcl_AppendToObj(objPtr, "DATASET UNSTRUCTURED_GRID\n", -1); 427 sprintf(mesg, "POINTS %d double\n", nPoints); 428 Tcl_AppendToObj(objPtr, mesg, -1); 429 Tcl_AppendObjToObj(objPtr, pointsObjPtr); 430 sprintf(mesg, "POINT_DATA %d\n", nPoints); 431 Tcl_AppendToObj(objPtr, mesg, -1); 432 sprintf(mesg, "SCALARS %s double 1\n", name); 433 Tcl_AppendToObj(objPtr, mesg, -1); 434 sprintf(mesg, "LOOKUP_TABLE default\n"); 435 Tcl_AppendToObj(objPtr, mesg, -1); 436 Tcl_AppendObjToObj(objPtr, fieldObjPtr); 437 } 438 259 439 Tcl_DecrRefCount(pointsObjPtr); 440 Tcl_DecrRefCount(fieldObjPtr); 260 441 Tcl_SetObjResult(interp, objPtr); 261 442 return TCL_OK;
Note: See TracChangeset
for help on using the changeset viewer.