source: branches/blt4/packages/vizservers/nanovis/Unirect.cpp @ 1982

Last change on this file since 1982 was 1982, checked in by gah, 13 years ago

Clean up debugging/printing traces

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