source: trunk/packages/vizservers/nanovis/Unirect.cpp @ 1453

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