1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: George A. Howlett <gah@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <assert.h> |
---|
9 | #include <string.h> |
---|
10 | #include <math.h> |
---|
11 | #include <float.h> |
---|
12 | #include <ctype.h> // For isspace() |
---|
13 | |
---|
14 | #include <tcl.h> |
---|
15 | |
---|
16 | #include "Command.h" |
---|
17 | #include "Unirect.h" |
---|
18 | #include "Trace.h" |
---|
19 | |
---|
20 | using namespace nv; |
---|
21 | |
---|
22 | static inline char * |
---|
23 | skipspaces(char *string) |
---|
24 | { |
---|
25 | while (isspace(*string)) { |
---|
26 | string++; |
---|
27 | } |
---|
28 | return string; |
---|
29 | } |
---|
30 | |
---|
31 | static inline char * |
---|
32 | getline(char **stringPtr, char *endPtr) |
---|
33 | { |
---|
34 | char *line, *p; |
---|
35 | |
---|
36 | line = skipspaces(*stringPtr); |
---|
37 | for (p = line; p < endPtr; p++) { |
---|
38 | if (*p == '\n') { |
---|
39 | *p++ = '\0'; |
---|
40 | *stringPtr = p; |
---|
41 | return line; |
---|
42 | } |
---|
43 | } |
---|
44 | *stringPtr = p; |
---|
45 | return line; |
---|
46 | } |
---|
47 | |
---|
48 | int |
---|
49 | nv::Unirect2d::parseBuffer(Tcl_Interp *interp, const char *bytes, size_t len) |
---|
50 | { |
---|
51 | Tcl_Obj *objPtr = Tcl_NewStringObj(bytes, len); |
---|
52 | Tcl_Obj **objv; |
---|
53 | int objc; |
---|
54 | if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK) { |
---|
55 | return TCL_ERROR; |
---|
56 | } |
---|
57 | int result = loadData(interp, objc, objv); |
---|
58 | Tcl_DecrRefCount(objPtr); |
---|
59 | if ((result != TCL_OK) || (!isInitialized())) { |
---|
60 | return TCL_ERROR; |
---|
61 | } |
---|
62 | return TCL_OK; |
---|
63 | } |
---|
64 | |
---|
65 | int |
---|
66 | nv::Unirect3d::parseBuffer(Tcl_Interp *interp, const char *bytes, size_t len) |
---|
67 | { |
---|
68 | Tcl_Obj *objPtr = Tcl_NewStringObj(bytes, len); |
---|
69 | Tcl_Obj **objv; |
---|
70 | int objc; |
---|
71 | if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK) { |
---|
72 | return TCL_ERROR; |
---|
73 | } |
---|
74 | int result = loadData(interp, objc, objv); |
---|
75 | Tcl_DecrRefCount(objPtr); |
---|
76 | if ((result != TCL_OK) || (!isInitialized())) { |
---|
77 | return TCL_ERROR; |
---|
78 | } |
---|
79 | return TCL_OK; |
---|
80 | } |
---|
81 | |
---|
82 | int |
---|
83 | nv::Unirect3d::loadData(Tcl_Interp *interp, int objc, |
---|
84 | Tcl_Obj *const *objv) |
---|
85 | { |
---|
86 | int num[3], nValues; |
---|
87 | float min[3], max[3]; |
---|
88 | const char *units[4]; |
---|
89 | |
---|
90 | if ((objc-1) & 0x01) { |
---|
91 | Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ", |
---|
92 | "wrong # of arguments: should be key-value pairs", |
---|
93 | (char *)NULL); |
---|
94 | return TCL_ERROR; |
---|
95 | } |
---|
96 | |
---|
97 | /* Default order is z, y, x. */ |
---|
98 | int axis1, axis2, axis3; |
---|
99 | axis1 = 0; /* X-axis */ |
---|
100 | axis2 = 1; /* Y-axis */ |
---|
101 | axis3 = 2; /* Z-axis */ |
---|
102 | |
---|
103 | num[0] = num[1] = num[2] = nValues = 0; |
---|
104 | min[0] = min[1] = min[2] = max[0] = max[1] = max[2] = 0.0f; |
---|
105 | units[0] = units[1] = units[2] = units[3] = NULL; |
---|
106 | |
---|
107 | int i; |
---|
108 | for (i = 1; i < objc; i += 2) { |
---|
109 | const char *string; |
---|
110 | char c; |
---|
111 | |
---|
112 | string = Tcl_GetString(objv[i]); |
---|
113 | c = string[0]; |
---|
114 | if ((c == 'x') && (strcmp(string, "xmin") == 0)) { |
---|
115 | if (GetFloatFromObj(interp, objv[i+1], min+2) != TCL_OK) { |
---|
116 | return TCL_ERROR; |
---|
117 | } |
---|
118 | } else if ((c == 'x') && (strcmp(string, "xmax") == 0)) { |
---|
119 | if (GetFloatFromObj(interp, objv[i+1], max+2) != TCL_OK) { |
---|
120 | return TCL_ERROR; |
---|
121 | } |
---|
122 | } else if ((c == 'x') && (strcmp(string, "xnum") == 0)) { |
---|
123 | if (Tcl_GetIntFromObj(interp, objv[i+1], num+2) != TCL_OK) { |
---|
124 | return TCL_ERROR; |
---|
125 | } |
---|
126 | if (num[2] <= 0) { |
---|
127 | Tcl_AppendResult(interp, "bad xnum value: must be > 0", |
---|
128 | (char *)NULL); |
---|
129 | return TCL_ERROR; |
---|
130 | } |
---|
131 | } else if ((c == 'x') && (strcmp(string, "xunits") == 0)) { |
---|
132 | units[2] = Tcl_GetString(objv[i+1]); |
---|
133 | } else if ((c == 'y') && (strcmp(string, "ymin") == 0)) { |
---|
134 | if (GetFloatFromObj(interp, objv[i+1], min+1) != TCL_OK) { |
---|
135 | return TCL_ERROR; |
---|
136 | } |
---|
137 | } else if ((c == 'y') && (strcmp(string, "ymax") == 0)) { |
---|
138 | if (GetFloatFromObj(interp, objv[i+1], max+1) != TCL_OK) { |
---|
139 | return TCL_ERROR; |
---|
140 | } |
---|
141 | } else if ((c == 'y') && (strcmp(string, "ynum") == 0)) { |
---|
142 | if (Tcl_GetIntFromObj(interp, objv[i+1], num+1) != TCL_OK) { |
---|
143 | return TCL_ERROR; |
---|
144 | } |
---|
145 | if (num[1] <= 0) { |
---|
146 | Tcl_AppendResult(interp, "bad ynum value: must be > 0", |
---|
147 | (char *)NULL); |
---|
148 | return TCL_ERROR; |
---|
149 | } |
---|
150 | } else if ((c == 'y') && (strcmp(string, "yunits") == 0)) { |
---|
151 | units[1] = Tcl_GetString(objv[i+1]); |
---|
152 | } else if ((c == 'z') && (strcmp(string, "zmin") == 0)) { |
---|
153 | if (GetFloatFromObj(interp, objv[i+1], min) != TCL_OK) { |
---|
154 | return TCL_ERROR; |
---|
155 | } |
---|
156 | } else if ((c == 'z') && (strcmp(string, "zmax") == 0)) { |
---|
157 | if (GetFloatFromObj(interp, objv[i+1], max) != TCL_OK) { |
---|
158 | return TCL_ERROR; |
---|
159 | } |
---|
160 | } else if ((c == 'z') && (strcmp(string, "znum") == 0)) { |
---|
161 | if (Tcl_GetIntFromObj(interp, objv[i+1], num) != TCL_OK) { |
---|
162 | return TCL_ERROR; |
---|
163 | } |
---|
164 | if (num[0] <= 0) { |
---|
165 | Tcl_AppendResult(interp, "bad znum value: must be > 0", |
---|
166 | (char *)NULL); |
---|
167 | return TCL_ERROR; |
---|
168 | } |
---|
169 | } else if ((c == 'z') && (strcmp(string, "zunits") == 0)) { |
---|
170 | units[0] = Tcl_GetString(objv[i+1]); |
---|
171 | } else if ((c == 'v') && (strcmp(string, "values") == 0)) { |
---|
172 | Tcl_Obj **vobj; |
---|
173 | |
---|
174 | if (Tcl_ListObjGetElements(interp, objv[i+1], &nValues, &vobj) |
---|
175 | != TCL_OK) { |
---|
176 | return TCL_ERROR; |
---|
177 | } |
---|
178 | _values = (float *)realloc(_values, sizeof(float) * nValues); |
---|
179 | int j; |
---|
180 | for (j = 0; j < nValues; j++) { |
---|
181 | if (GetFloatFromObj(interp, vobj[j], _values + j)!=TCL_OK) { |
---|
182 | return TCL_ERROR; |
---|
183 | } |
---|
184 | } |
---|
185 | } else if ((c == 'u') && (strcmp(string, "units") == 0)) { |
---|
186 | _vUnits = strdup(Tcl_GetString(objv[i+1])); |
---|
187 | } else if ((c == 'c') && (strcmp(string, "components") == 0)) { |
---|
188 | int n; |
---|
189 | |
---|
190 | if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) { |
---|
191 | return TCL_ERROR; |
---|
192 | } |
---|
193 | if (n <= 0) { |
---|
194 | Tcl_AppendResult(interp, "bad extents value: must be > 0", |
---|
195 | (char *)NULL); |
---|
196 | return TCL_ERROR; |
---|
197 | } |
---|
198 | _nComponents = n; |
---|
199 | } else if ((c == 'a') && (strcmp(string, "axisorder") == 0)) { |
---|
200 | Tcl_Obj **axes; |
---|
201 | int n; |
---|
202 | |
---|
203 | if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &axes) |
---|
204 | != TCL_OK) { |
---|
205 | return TCL_ERROR; |
---|
206 | } |
---|
207 | if (n != 3) { |
---|
208 | return TCL_ERROR; |
---|
209 | } |
---|
210 | if ((GetAxisFromObj(interp, axes[0], &axis1) != TCL_OK) || |
---|
211 | (GetAxisFromObj(interp, axes[1], &axis2) != TCL_OK) || |
---|
212 | (GetAxisFromObj(interp, axes[2], &axis3) != TCL_OK)) { |
---|
213 | return TCL_ERROR; |
---|
214 | } |
---|
215 | } else { |
---|
216 | Tcl_AppendResult(interp, "unknown key \"", string, |
---|
217 | (char *)NULL); |
---|
218 | return TCL_ERROR; |
---|
219 | } |
---|
220 | } |
---|
221 | if (_values == NULL) { |
---|
222 | Tcl_AppendResult(interp, "missing \"values\" key", (char *)NULL); |
---|
223 | return TCL_ERROR; |
---|
224 | } |
---|
225 | if ((size_t)nValues != (num[0] * num[1] * num[2] * _nComponents)) { |
---|
226 | TRACE("num[0]=%d num[1]=%d num[2]=%d ncomponents=%d nValues=%d", |
---|
227 | num[0], num[1], num[2], _nComponents, nValues); |
---|
228 | Tcl_AppendResult(interp, |
---|
229 | "wrong # of values: must be xnum*ynum*znum*extents", |
---|
230 | (char *)NULL); |
---|
231 | return TCL_ERROR; |
---|
232 | } |
---|
233 | |
---|
234 | #ifdef notdef |
---|
235 | if ((axis1 != 0) || (axis2 != 1) || (axis3 != 2)) { |
---|
236 | // Reorder the data into x, y, z where x varies fastest and so on. |
---|
237 | int z; |
---|
238 | float *data, *dp; |
---|
239 | |
---|
240 | dp = data = new float[nValues]; |
---|
241 | for (z = 0; z < num[0]; z++) { |
---|
242 | int y; |
---|
243 | |
---|
244 | for (y = 0; y < num[1]; y++) { |
---|
245 | int x; |
---|
246 | |
---|
247 | for (x = 0; x < num[2]; x++) { |
---|
248 | int i; |
---|
249 | |
---|
250 | /* Compute the index from the data's described ordering. */ |
---|
251 | i = ((z*num[axis2]*num[axis3]) + (y*num[axis3]) + x) * 3; |
---|
252 | for(size_t v = 0; v < _nComponents; v++) { |
---|
253 | dp[v] = values[i+v]; |
---|
254 | } |
---|
255 | dp += _nComponents; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | delete [] values; |
---|
260 | values = data; |
---|
261 | } |
---|
262 | #endif |
---|
263 | _nValues = nValues; |
---|
264 | if (units[3] != NULL) { |
---|
265 | if (_vUnits != NULL) { |
---|
266 | free(_vUnits); |
---|
267 | } |
---|
268 | _vUnits = strdup(units[3]); |
---|
269 | } |
---|
270 | _xMin = min[axis3]; |
---|
271 | _xMax = max[axis3]; |
---|
272 | _xNum = num[axis3]; |
---|
273 | if (units[axis3] != NULL) { |
---|
274 | if (_xUnits != NULL) { |
---|
275 | free(_xUnits); |
---|
276 | } |
---|
277 | _xUnits = strdup(units[axis3]); |
---|
278 | } |
---|
279 | _yMin = min[axis2]; |
---|
280 | _yMax = max[axis2]; |
---|
281 | _yNum = num[axis2]; |
---|
282 | if (units[axis2] != NULL) { |
---|
283 | if (_yUnits != NULL) { |
---|
284 | free(_yUnits); |
---|
285 | } |
---|
286 | _yUnits = strdup(units[axis2]); |
---|
287 | } |
---|
288 | _zMin = min[axis1]; |
---|
289 | _zMax = max[axis1]; |
---|
290 | _zNum = num[axis1]; |
---|
291 | if (units[axis1] != NULL) { |
---|
292 | if (_zUnits != NULL) { |
---|
293 | free(_zUnits); |
---|
294 | } |
---|
295 | _zUnits = strdup(units[axis1]); |
---|
296 | } |
---|
297 | _initialized = true; |
---|
298 | #ifdef notdef |
---|
299 | { |
---|
300 | FILE *f; |
---|
301 | f = fopen("/tmp/unirect3d.txt", "w"); |
---|
302 | fprintf(f, "unirect3d xmin %g xmax %g xnum %d ", _xMin, _xMax, _xNum); |
---|
303 | fprintf(f, "ymin %g ymax %g ynum %d ", _yMin, _yMax, _yNum); |
---|
304 | fprintf(f, "zmin %g zmax %g znum %d ", _zMin, _zMax, _zNum); |
---|
305 | fprintf(f, "components %d values {\n", _nComponents); |
---|
306 | for (size_t i = 0; i < _nValues; i+= 3) { |
---|
307 | fprintf(f, "%g %g %g\n", _values[i], _values[i+1], _values[i+2]); |
---|
308 | } |
---|
309 | fprintf(f, "}\n"); |
---|
310 | fclose(f); |
---|
311 | } |
---|
312 | #endif |
---|
313 | return TCL_OK; |
---|
314 | } |
---|
315 | |
---|
316 | int |
---|
317 | nv::Unirect2d::loadData(Tcl_Interp *interp, int objc, |
---|
318 | Tcl_Obj *const *objv) |
---|
319 | { |
---|
320 | if ((objc & 0x01) == 0) { |
---|
321 | Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ", |
---|
322 | "wrong number of arguments: should be key-value pairs", |
---|
323 | (char *)NULL); |
---|
324 | return TCL_ERROR; |
---|
325 | } |
---|
326 | |
---|
327 | int axis[2]; |
---|
328 | axis[0] = 1; /* X-axis */ |
---|
329 | axis[1] = 0; /* Y-axis */ |
---|
330 | |
---|
331 | _xNum = _yNum = _nValues = 0; |
---|
332 | _xMin = _yMin = _xMax = _yMax = 0.0f; |
---|
333 | if (_xUnits != NULL) { |
---|
334 | free(_xUnits); |
---|
335 | } |
---|
336 | if (_yUnits != NULL) { |
---|
337 | free(_yUnits); |
---|
338 | } |
---|
339 | if (_vUnits != NULL) { |
---|
340 | free(_vUnits); |
---|
341 | } |
---|
342 | _xUnits = _yUnits = _vUnits = NULL; |
---|
343 | _nValues = 0; |
---|
344 | |
---|
345 | int i; |
---|
346 | for (i = 1; i < objc; i += 2) { |
---|
347 | const char *string; |
---|
348 | char c; |
---|
349 | |
---|
350 | string = Tcl_GetString(objv[i]); |
---|
351 | c = string[0]; |
---|
352 | if ((c == 'x') && (strcmp(string, "xmin") == 0)) { |
---|
353 | if (GetFloatFromObj(interp, objv[i+1], &_xMin) != TCL_OK) { |
---|
354 | return TCL_ERROR; |
---|
355 | } |
---|
356 | } else if ((c == 'x') && (strcmp(string, "xmax") == 0)) { |
---|
357 | if (GetFloatFromObj(interp, objv[i+1], &_xMax) != TCL_OK) { |
---|
358 | return TCL_ERROR; |
---|
359 | } |
---|
360 | } else if ((c == 'x') && (strcmp(string, "xnum") == 0)) { |
---|
361 | int n; |
---|
362 | if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) { |
---|
363 | return TCL_ERROR; |
---|
364 | } |
---|
365 | if (n <= 0) { |
---|
366 | Tcl_AppendResult(interp, "bad xnum value: must be > 0", |
---|
367 | (char *)NULL); |
---|
368 | return TCL_ERROR; |
---|
369 | } |
---|
370 | _xNum = n; |
---|
371 | } else if ((c == 'x') && (strcmp(string, "xunits") == 0)) { |
---|
372 | _xUnits = strdup(Tcl_GetString(objv[i+1])); |
---|
373 | } else if ((c == 'y') && (strcmp(string, "ymin") == 0)) { |
---|
374 | if (GetFloatFromObj(interp, objv[i+1], &_yMin) != TCL_OK) { |
---|
375 | return TCL_ERROR; |
---|
376 | } |
---|
377 | } else if ((c == 'y') && (strcmp(string, "ymax") == 0)) { |
---|
378 | if (GetFloatFromObj(interp, objv[i+1], &_yMax) != TCL_OK) { |
---|
379 | return TCL_ERROR; |
---|
380 | } |
---|
381 | } else if ((c == 'y') && (strcmp(string, "ynum") == 0)) { |
---|
382 | int n; |
---|
383 | if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) { |
---|
384 | return TCL_ERROR; |
---|
385 | } |
---|
386 | if (n <= 0) { |
---|
387 | Tcl_AppendResult(interp, "bad ynum value: must be > 0", |
---|
388 | (char *)NULL); |
---|
389 | return TCL_ERROR; |
---|
390 | } |
---|
391 | _yNum = n; |
---|
392 | } else if ((c == 'y') && (strcmp(string, "yunits") == 0)) { |
---|
393 | _yUnits = strdup(Tcl_GetString(objv[i+1])); |
---|
394 | } else if ((c == 'v') && (strcmp(string, "values") == 0)) { |
---|
395 | Tcl_Obj **vobj; |
---|
396 | int n; |
---|
397 | |
---|
398 | Tcl_IncrRefCount(objv[i+1]); |
---|
399 | if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &vobj) != TCL_OK){ |
---|
400 | return TCL_ERROR; |
---|
401 | } |
---|
402 | if (n <= 0) { |
---|
403 | Tcl_AppendResult(interp, "empty values list : must be > 0", |
---|
404 | (char *)NULL); |
---|
405 | return TCL_ERROR; |
---|
406 | } |
---|
407 | _nValues = n; |
---|
408 | _values = (float *)realloc(_values, sizeof(float) * _nValues); |
---|
409 | size_t j; |
---|
410 | for (j = 0; j < _nValues; j++) { |
---|
411 | if (GetFloatFromObj(interp, vobj[j], _values + j)!=TCL_OK) { |
---|
412 | return TCL_ERROR; |
---|
413 | } |
---|
414 | } |
---|
415 | Tcl_DecrRefCount(objv[i+1]); |
---|
416 | } else if ((c == 'u') && (strcmp(string, "units") == 0)) { |
---|
417 | _vUnits = strdup(Tcl_GetString(objv[i+1])); |
---|
418 | } else if ((c == 'c') && (strcmp(string, "components") == 0)) { |
---|
419 | int n; |
---|
420 | |
---|
421 | if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) { |
---|
422 | return TCL_ERROR; |
---|
423 | } |
---|
424 | if (n <= 0) { |
---|
425 | Tcl_AppendResult(interp, "bad extents value: must be > 0", |
---|
426 | (char *)NULL); |
---|
427 | return TCL_ERROR; |
---|
428 | } |
---|
429 | _nComponents = n; |
---|
430 | } else if ((c == 'a') && (strcmp(string, "axisorder") == 0)) { |
---|
431 | Tcl_Obj **order; |
---|
432 | int n; |
---|
433 | |
---|
434 | if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &order) |
---|
435 | != TCL_OK) { |
---|
436 | return TCL_ERROR; |
---|
437 | } |
---|
438 | if (n != 2) { |
---|
439 | Tcl_AppendResult(interp, |
---|
440 | "wrong # of axes defined for ordering the data", |
---|
441 | (char *)NULL); |
---|
442 | return TCL_ERROR; |
---|
443 | } |
---|
444 | if ((GetAxisFromObj(interp, order[0], axis) != TCL_OK) || |
---|
445 | (GetAxisFromObj(interp, order[1], axis+1) != TCL_OK)) { |
---|
446 | return TCL_ERROR; |
---|
447 | } |
---|
448 | } else { |
---|
449 | Tcl_AppendResult(interp, "unknown key \"", string, |
---|
450 | (char *)NULL); |
---|
451 | return TCL_ERROR; |
---|
452 | } |
---|
453 | } |
---|
454 | if (_nValues == 0) { |
---|
455 | Tcl_AppendResult(interp, "missing \"values\" key", (char *)NULL); |
---|
456 | return TCL_ERROR; |
---|
457 | } |
---|
458 | if (_nValues != (_xNum * _yNum * _nComponents)) { |
---|
459 | Tcl_AppendResult(interp, |
---|
460 | "wrong number of values: must be xnum*ynum*components", |
---|
461 | (char *)NULL); |
---|
462 | return TCL_ERROR; |
---|
463 | } |
---|
464 | |
---|
465 | if ((axis[0] != 1) || (axis[1] != 0)) { |
---|
466 | TRACE("reordering data"); |
---|
467 | /* Reorder the data into x, y where x varies fastest and so on. */ |
---|
468 | size_t y; |
---|
469 | float *dp; |
---|
470 | |
---|
471 | dp = _values = (float *)realloc(_values, sizeof(float) * _nValues); |
---|
472 | for (y = 0; y < _yNum; y++) { |
---|
473 | size_t x; |
---|
474 | |
---|
475 | for (x = 0; x < _xNum; x++) { |
---|
476 | size_t i, v; |
---|
477 | |
---|
478 | /* Compute the index from the data's described ordering. */ |
---|
479 | i = (y + (_yNum * x)) * _nComponents; |
---|
480 | for(v = 0; v < _nComponents; v++) { |
---|
481 | dp[v] = _values[i+v]; |
---|
482 | } |
---|
483 | dp += _nComponents; |
---|
484 | } |
---|
485 | } |
---|
486 | } |
---|
487 | _initialized = true; |
---|
488 | return TCL_OK; |
---|
489 | } |
---|
490 | |
---|
491 | #ifdef USE_DX_READER |
---|
492 | bool |
---|
493 | nv::Unirect3d::importDx(size_t nComponents, |
---|
494 | size_t length, char *string) |
---|
495 | { |
---|
496 | int nx, ny, nz, npts; |
---|
497 | double x0, y0, z0, dx, dy, dz, ddx, ddy, ddz; |
---|
498 | char *p, *pend; |
---|
499 | |
---|
500 | dx = dy = dz = 0.0; |
---|
501 | x0 = y0 = z0 = 0.0; /* May not have an origin line. */ |
---|
502 | nx = ny = nz = npts = 0; |
---|
503 | for (p = string, pend = p + length; p < pend; /*empty*/) { |
---|
504 | char *line; |
---|
505 | |
---|
506 | line = getline(&p, pend); |
---|
507 | if (line == pend) { |
---|
508 | break; /* EOF */ |
---|
509 | } |
---|
510 | if ((line[0] == '#') || (line == '\0')) { |
---|
511 | continue; /* Skip blank or comment lines. */ |
---|
512 | } |
---|
513 | if (sscanf(line, "object %*d class gridpositions counts %d %d %d", |
---|
514 | &nx, &ny, &nz) == 3) { |
---|
515 | if ((nx < 0) || (ny < 0) || (nz < 0)) { |
---|
516 | ERROR("invalid grid size: x=%d, y=%d, z=%d", |
---|
517 | nx, ny, nz); |
---|
518 | return false; |
---|
519 | } |
---|
520 | } else if (sscanf(line, "origin %lg %lg %lg", &x0, &y0, &z0) == 3) { |
---|
521 | /* Found origin. */ |
---|
522 | } else if (sscanf(line, "delta %lg %lg %lg", &ddx, &ddy, &ddz) == 3) { |
---|
523 | /* Found one of the delta lines. */ |
---|
524 | int count = 0; |
---|
525 | if (ddx != 0.0) { |
---|
526 | dx = ddx; |
---|
527 | count++; |
---|
528 | } |
---|
529 | if (ddy != 0.0) { |
---|
530 | dy = ddy; |
---|
531 | count++; |
---|
532 | } |
---|
533 | if (ddz != 0.0) { |
---|
534 | dz = ddz; |
---|
535 | count++; |
---|
536 | } |
---|
537 | if (count > 1) { |
---|
538 | ERROR("don't know how to handle multiple non-zero delta values"); |
---|
539 | return false; |
---|
540 | } |
---|
541 | } else if (sscanf(line, "object %*d class array type %*s rank 1" |
---|
542 | " shape 3 items %d data follows", &npts) == 1) { |
---|
543 | if (npts < 0) { |
---|
544 | ERROR("bad # points %d", npts); |
---|
545 | return false; |
---|
546 | } |
---|
547 | TRACE("#points=%d", npts); |
---|
548 | if (npts != nx*ny*nz) { |
---|
549 | ERROR("inconsistent data: expected %d points" |
---|
550 | " but found %d points", nx*ny*nz, npts); |
---|
551 | return false; |
---|
552 | } |
---|
553 | break; |
---|
554 | } |
---|
555 | } |
---|
556 | if (npts != nx*ny*nz) { |
---|
557 | ERROR("inconsistent data: expected %d points" |
---|
558 | " but found %d points", nx*ny*nz, npts); |
---|
559 | return false; |
---|
560 | } |
---|
561 | if (npts > 1 && ((dx == dy) && (dx == dz) && (dx == 0.0))) { |
---|
562 | ERROR("Missing or invalid deltas in DX file: %g %g %g", dx, dy, dz); |
---|
563 | return false; |
---|
564 | } |
---|
565 | if (dx < 0.0 || dy < 0.0 || dz < 0.0) { |
---|
566 | ERROR("Negative deltas not supported in DX file: %g %g %g", dx, dy, dz); |
---|
567 | return false; |
---|
568 | } |
---|
569 | |
---|
570 | _initialized = false; |
---|
571 | _xValueMin = _yValueMin = _zValueMin = FLT_MAX; |
---|
572 | _xValueMax = _yValueMax = _zValueMax = -FLT_MAX; |
---|
573 | _xMin = x0, _yMin = y0, _zMin = z0; |
---|
574 | _xNum = nx, _yNum = ny, _zNum = nz; |
---|
575 | _xMax = _xMin + dx * _xNum; |
---|
576 | _yMax = _yMin + dy * _yNum; |
---|
577 | _zMax = _zMin + dz * _zNum; |
---|
578 | _nComponents = nComponents; |
---|
579 | |
---|
580 | _values = (float *)realloc(_values, sizeof(float) * npts * _nComponents); |
---|
581 | _nValues = 0; |
---|
582 | for (size_t ix = 0; ix < _xNum; ix++) { |
---|
583 | for (size_t iy = 0; iy < _yNum; iy++) { |
---|
584 | for (size_t iz = 0; iz < _zNum; iz++) { |
---|
585 | char *line; |
---|
586 | if ((p == pend) || (_nValues > (size_t)npts)) { |
---|
587 | break; |
---|
588 | } |
---|
589 | line = getline(&p, pend); |
---|
590 | if ((line[0] == '#') || (line[0] == '\0')) { |
---|
591 | continue; /* Skip blank or comment lines. */ |
---|
592 | } |
---|
593 | double vx, vy, vz; |
---|
594 | if (sscanf(line, "%lg %lg %lg", &vx, &vy, &vz) == 3) { |
---|
595 | int nindex = (iz*nx*ny + iy*nx + ix) * 3; |
---|
596 | if (vx < _xValueMin) { |
---|
597 | _xValueMin = vx; |
---|
598 | } else if (vx > _xValueMax) { |
---|
599 | _xValueMax = vx; |
---|
600 | } |
---|
601 | if (vy < _yValueMin) { |
---|
602 | _yValueMin = vy; |
---|
603 | } else if (vy > _yValueMax) { |
---|
604 | _yValueMax = vy; |
---|
605 | } |
---|
606 | if (vz < _zValueMin) { |
---|
607 | _zValueMin = vz; |
---|
608 | } else if (vz > _zValueMax) { |
---|
609 | _zValueMax = vz; |
---|
610 | } |
---|
611 | _values[nindex] = vx; |
---|
612 | _values[nindex+1] = vy; |
---|
613 | _values[nindex+2] = vz; |
---|
614 | _nValues++; |
---|
615 | } |
---|
616 | } |
---|
617 | } |
---|
618 | } |
---|
619 | /* Make sure that we read all of the expected points. */ |
---|
620 | if (_nValues != (size_t)npts) { |
---|
621 | ERROR("inconsistent data: expected %d points" |
---|
622 | " but found %d points", npts, _nValues); |
---|
623 | free(_values); |
---|
624 | _values = NULL; |
---|
625 | return false; |
---|
626 | } |
---|
627 | _nValues *= _nComponents; |
---|
628 | _initialized = true; |
---|
629 | #ifdef notdef |
---|
630 | { |
---|
631 | FILE *f; |
---|
632 | f = fopen("/tmp/dx.txt", "w"); |
---|
633 | fprintf(f, "unirect3d xmin %g xmax %g xnum %d ", _xMin, _xMax, _xNum); |
---|
634 | fprintf(f, "ymin %g ymax %g ynum %d ", _yMin, _yMax, _yNum); |
---|
635 | fprintf(f, "zmin %g zmax %g znum %d ", _zMin, _zMax, _zNum); |
---|
636 | fprintf(f, "components %d values {\n", _nComponents); |
---|
637 | for (size_t i = 0; i < _nValues; i+= 3) { |
---|
638 | fprintf(f, "%g %g %g\n", _values[i], _values[i+1], _values[i+2]); |
---|
639 | } |
---|
640 | fprintf(f, "}\n"); |
---|
641 | fclose(f); |
---|
642 | } |
---|
643 | #endif |
---|
644 | return true; |
---|
645 | } |
---|
646 | #endif |
---|
647 | |
---|
648 | void |
---|
649 | nv::Unirect3d::getVectorRange() |
---|
650 | { |
---|
651 | assert(_nComponents == 3); |
---|
652 | _magMax = -DBL_MAX, _magMin = DBL_MAX; |
---|
653 | size_t i; |
---|
654 | for (i = 0; i < _nValues; i += _nComponents) { |
---|
655 | double vx, vy, vz, vm; |
---|
656 | |
---|
657 | vx = _values[i]; |
---|
658 | vy = _values[i+1]; |
---|
659 | vz = _values[i+2]; |
---|
660 | |
---|
661 | vm = sqrt(vx*vx + vy*vy + vz*vz); |
---|
662 | if (vm > _magMax) { |
---|
663 | _magMax = vm; |
---|
664 | } |
---|
665 | if (vm < _magMin) { |
---|
666 | _magMin = vm; |
---|
667 | } |
---|
668 | } |
---|
669 | TRACE("getVectorRange: %g %g", _magMin, _magMax); |
---|
670 | } |
---|
671 | |
---|
672 | bool |
---|
673 | nv::Unirect3d::convert(nv::Unirect2d *dataPtr) |
---|
674 | { |
---|
675 | _initialized = false; |
---|
676 | |
---|
677 | _xValueMin = dataPtr->xValueMin(); |
---|
678 | _yValueMin = dataPtr->yValueMin(); |
---|
679 | _zValueMin = 0.0; |
---|
680 | _xMin = dataPtr->xMin(); |
---|
681 | _yMin = dataPtr->yMin(); |
---|
682 | _zMin = 0.0; |
---|
683 | _xMax = dataPtr->xMax(); |
---|
684 | _yMax = dataPtr->yMax(); |
---|
685 | _zMax = 0.0; |
---|
686 | _xNum = dataPtr->xNum(); |
---|
687 | _yNum = dataPtr->yNum(); |
---|
688 | _zNum = 1; |
---|
689 | switch (dataPtr->nComponents()) { |
---|
690 | case 1: |
---|
691 | case 3: |
---|
692 | _values = (float *)realloc(_values, sizeof(float) * dataPtr->nValues()); |
---|
693 | if (_values == NULL) { |
---|
694 | return false; |
---|
695 | } |
---|
696 | memcpy(_values, dataPtr->values(), dataPtr->nValues()); |
---|
697 | _nValues = dataPtr->nValues(); |
---|
698 | _nComponents = dataPtr->nComponents(); |
---|
699 | break; |
---|
700 | case 2: |
---|
701 | float *values; |
---|
702 | _nValues = 3 * _xNum * _yNum * _zNum; |
---|
703 | _values = (float *)realloc(_values, sizeof(float) * _nValues); |
---|
704 | if (_values == NULL) { |
---|
705 | return false; |
---|
706 | } |
---|
707 | values = dataPtr->values(); |
---|
708 | size_t i, j; |
---|
709 | for (j = i = 0; i < dataPtr->nValues(); i += 2, j+= 3) { |
---|
710 | _values[j] = values[i]; |
---|
711 | _values[j+1] = values[i+1]; |
---|
712 | _values[j+2] = 0.0f; |
---|
713 | } |
---|
714 | _nComponents = 3; |
---|
715 | break; |
---|
716 | } |
---|
717 | return true; |
---|
718 | } |
---|