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