source: nanovis/trunk/Unirect.cpp @ 6716

Last change on this file since 6716 was 5552, checked in by ldelgass, 9 years ago

whitespace

  • Property svn:eol-style set to native
File size: 22.8 KB
RevLine 
[2798]1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
[3502]2/*
3 * Copyright (C) 2004-2013  HUBzero Foundation, LLC
4 *
5 * Author: George A. Howlett <gah@purdue.edu>
6 */
[4895]7
8#include <assert.h>
9#include <string.h>
10#include <math.h>
[1429]11#include <float.h>
[4895]12#include <ctype.h> // For isspace()
[3597]13
[1365]14#include <tcl.h>
[2827]15
[3597]16#include "Command.h"
[2827]17#include "Unirect.h"
[1462]18#include "Trace.h"
[1365]19
[3611]20using namespace nv;
21
[2881]22static inline char *
[5552]23skipspaces(char *string)
[1429]24{
25    while (isspace(*string)) {
[2843]26        string++;
[1429]27    }
28    return string;
29}
30
[2881]31static inline char *
[5552]32getline(char **stringPtr, char *endPtr)
[1429]33{
34    char *line, *p;
35
36    line = skipspaces(*stringPtr);
37    for (p = line; p < endPtr; p++) {
[2843]38        if (*p == '\n') {
39            *p++ = '\0';
40            *stringPtr = p;
41            return line;
42        }
[1429]43    }
44    *stringPtr = p;
45    return line;
46}
47
[1469]48int
[4063]49nv::Unirect2d::parseBuffer(Tcl_Interp *interp, const char *bytes, size_t len)
[1469]50{
[4063]51    Tcl_Obj *objPtr = Tcl_NewStringObj(bytes, len);
[1469]52    Tcl_Obj **objv;
53    int objc;
54    if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK) {
[2843]55        return TCL_ERROR;
[1469]56    }
[3605]57    int result = loadData(interp, objc, objv);
[1469]58    Tcl_DecrRefCount(objPtr);
59    if ((result != TCL_OK) || (!isInitialized())) {
[2843]60        return TCL_ERROR;
[1469]61    }
62    return TCL_OK;
63}
64
65int
[4063]66nv::Unirect3d::parseBuffer(Tcl_Interp *interp, const char *bytes, size_t len)
[1469]67{
[4878]68    Tcl_Obj *objPtr = Tcl_NewStringObj(bytes, len);
[1469]69    Tcl_Obj **objv;
70    int objc;
71    if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK) {
[2843]72        return TCL_ERROR;
[1469]73    }
[4878]74    int result = loadData(interp, objc, objv);
[1469]75    Tcl_DecrRefCount(objPtr);
76    if ((result != TCL_OK) || (!isInitialized())) {
[2843]77        return TCL_ERROR;
[1469]78    }
79    return TCL_OK;
80}
81
[5552]82int
83nv::Unirect3d::loadData(Tcl_Interp *interp, int objc,
[4056]84                        Tcl_Obj *const *objv)
[1365]85{
86    int num[3], nValues;
87    float min[3], max[3];
[3559]88    const char *units[4];
[1365]89
[1510]90    if ((objc-1) & 0x01) {
[1365]91        Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ",
[1462]92                "wrong # of arguments: should be key-value pairs",
[1365]93                (char *)NULL);
94        return TCL_ERROR;
95    }
96
97    /* Default order is  z, y, x. */
98    int axis1, axis2, axis3;
[5552]99    axis1 = 0;                        /* X-axis */
100    axis2 = 1;                        /* Y-axis */
101    axis3 = 2;                        /* Z-axis */
[1365]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;
[3559]105    units[0] = units[1] = units[2] = units[3] = NULL;
[1365]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)
[2843]175                != TCL_OK) {
[1365]176                return TCL_ERROR;
177            }
[1984]178            _values = (float *)realloc(_values, sizeof(float) * nValues);
[1365]179            int j;
180            for (j = 0; j < nValues; j++) {
[1984]181                if (GetFloatFromObj(interp, vobj[j], _values + j)!=TCL_OK) {
[1365]182                    return TCL_ERROR;
[2843]183                }
184            }
[1365]185        } else if ((c == 'u') && (strcmp(string, "units") == 0)) {
[1429]186            _vUnits = strdup(Tcl_GetString(objv[i+1]));
[1447]187        } else if ((c == 'c') && (strcmp(string, "components") == 0)) {
[2843]188            int n;
[1447]189
[2843]190            if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) {
[1447]191                return TCL_ERROR;
192            }
[2843]193            if (n <= 0) {
[1447]194                Tcl_AppendResult(interp, "bad extents value: must be > 0",
195                                 (char *)NULL);
196                return TCL_ERROR;
197            }
[2843]198            _nComponents = n;
[1447]199        } else if ((c == 'a') && (strcmp(string, "axisorder") == 0)) {
[2843]200            Tcl_Obj **axes;
201            int n;
[1365]202
[5552]203            if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &axes)
[2843]204                != TCL_OK) {
[1365]205                return TCL_ERROR;
206            }
[2843]207            if (n != 3) {
[1365]208                return TCL_ERROR;
[2843]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            }
[1365]215        } else {
216            Tcl_AppendResult(interp, "unknown key \"", string,
217                (char *)NULL);
218            return TCL_ERROR;
219        }
220    }
[1984]221    if (_values == NULL) {
[1365]222        Tcl_AppendResult(interp, "missing \"values\" key", (char *)NULL);
223        return TCL_ERROR;
224    }
[1429]225    if ((size_t)nValues != (num[0] * num[1] * num[2] * _nComponents)) {
[3452]226        TRACE("num[0]=%d num[1]=%d num[2]=%d ncomponents=%d nValues=%d",
[2843]227               num[0], num[1], num[2], _nComponents, nValues);
[5552]228        Tcl_AppendResult(interp,
229                "wrong # of values: must be xnum*ynum*znum*extents",
[2843]230                         (char *)NULL);
[1453]231       return TCL_ERROR;
[1365]232    }
[5552]233
[1453]234#ifdef notdef
235    if ((axis1 != 0) || (axis2 != 1) || (axis3 != 2)) {
[2843]236        // Reorder the data into x, y, z where x varies fastest and so on.
237        int z;
238        float *data, *dp;
[1365]239
[2843]240        dp = data = new float[nValues];
241        for (z = 0; z < num[0]; z++) {
242            int y;
[1365]243
[2843]244            for (y = 0; y < num[1]; y++) {
245                int x;
[1365]246
[2843]247                for (x = 0; x < num[2]; x++) {
248                    int i;
[5552]249
[2843]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;
[1365]261    }
[1453]262#endif
[1429]263    _nValues = nValues;
[1365]264    if (units[3] != NULL) {
[2843]265        if (_vUnits != NULL) {
266            free(_vUnits);
267        }
268        _vUnits = strdup(units[3]);
[1365]269    }
[1429]270    _xMin = min[axis3];
271    _xMax = max[axis3];
272    _xNum = num[axis3];
[1365]273    if (units[axis3] != NULL) {
[2843]274        if (_xUnits != NULL) {
275            free(_xUnits);
276        }
277        _xUnits = strdup(units[axis3]);
[1365]278    }
[1429]279    _yMin = min[axis2];
280    _yMax = max[axis2];
281    _yNum = num[axis2];
[1365]282    if (units[axis2] != NULL) {
[2843]283        if (_yUnits != NULL) {
284            free(_yUnits);
285        }
286        _yUnits = strdup(units[axis2]);
[1365]287    }
[1429]288    _zMin = min[axis1];
289    _zMax = max[axis1];
290    _zNum = num[axis1];
[1365]291    if (units[axis1] != NULL) {
[2843]292        if (_zUnits != NULL) {
293            free(_zUnits);
294        }
295        _zUnits = strdup(units[axis1]);
[1365]296    }
[1429]297    _initialized = true;
[1458]298#ifdef notdef
[5552]299    {
[2843]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);
[1453]311    }
[1458]312#endif
[1365]313    return TCL_OK;
314}
315
[5552]316int
317nv::Unirect2d::loadData(Tcl_Interp *interp, int objc,
[4056]318                        Tcl_Obj *const *objv)
[1365]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];
[5552]328    axis[0] = 1;                        /* X-axis */
329    axis[1] = 0;                        /* Y-axis */
[1365]330
[1429]331    _xNum = _yNum = _nValues = 0;
332    _xMin = _yMin = _xMax = _yMax = 0.0f;
333    if (_xUnits != NULL) {
[2843]334        free(_xUnits);
[1365]335    }
[1429]336    if (_yUnits != NULL) {
[2843]337        free(_yUnits);
[1365]338    }
[1429]339    if (_vUnits != NULL) {
[2843]340        free(_vUnits);
[1365]341    }
[1429]342    _xUnits = _yUnits = _vUnits = NULL;
[1984]343    _nValues = 0;
[1365]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)) {
[1429]353            if (GetFloatFromObj(interp, objv[i+1], &_xMin) != TCL_OK) {
[1365]354                return TCL_ERROR;
355            }
356        } else if ((c == 'x') && (strcmp(string, "xmax") == 0)) {
[1429]357            if (GetFloatFromObj(interp, objv[i+1], &_xMax) != TCL_OK) {
[1365]358                return TCL_ERROR;
359            }
360        } else if ((c == 'x') && (strcmp(string, "xnum") == 0)) {
[2843]361            int n;
[1365]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            }
[2843]370            _xNum = n;
[1365]371        } else if ((c == 'x') && (strcmp(string, "xunits") == 0)) {
[1429]372            _xUnits = strdup(Tcl_GetString(objv[i+1]));
[1365]373        } else if ((c == 'y') && (strcmp(string, "ymin") == 0)) {
[1429]374            if (GetFloatFromObj(interp, objv[i+1], &_yMin) != TCL_OK) {
[1365]375                return TCL_ERROR;
376            }
377        } else if ((c == 'y') && (strcmp(string, "ymax") == 0)) {
[1429]378            if (GetFloatFromObj(interp, objv[i+1], &_yMax) != TCL_OK) {
[1365]379                return TCL_ERROR;
380            }
381        } else if ((c == 'y') && (strcmp(string, "ynum") == 0)) {
[2843]382            int n;
[1365]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            }
[2843]391            _yNum = n;
[1365]392        } else if ((c == 'y') && (strcmp(string, "yunits") == 0)) {
[1429]393            _yUnits = strdup(Tcl_GetString(objv[i+1]));
[1365]394        } else if ((c == 'v') && (strcmp(string, "values") == 0)) {
395            Tcl_Obj **vobj;
[2843]396            int n;
[1365]397
[2843]398            Tcl_IncrRefCount(objv[i+1]);
[1365]399            if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &vobj) != TCL_OK){
400                return TCL_ERROR;
401            }
[2843]402            if (n <= 0) {
[1365]403                Tcl_AppendResult(interp, "empty values list : must be > 0",
404                                 (char *)NULL);
405                return TCL_ERROR;
[2843]406            }
407            _nValues = n;
[1984]408            _values = (float *)realloc(_values, sizeof(float) * _nValues);
[1365]409            size_t j;
[1429]410            for (j = 0; j < _nValues; j++) {
411                if (GetFloatFromObj(interp, vobj[j], _values + j)!=TCL_OK) {
[1365]412                    return TCL_ERROR;
[2843]413                }
414            }
415            Tcl_DecrRefCount(objv[i+1]);
[1365]416        } else if ((c == 'u') && (strcmp(string, "units") == 0)) {
[1429]417            _vUnits = strdup(Tcl_GetString(objv[i+1]));
[1447]418        } else if ((c == 'c') && (strcmp(string, "components") == 0)) {
[2843]419            int n;
[1365]420
[2843]421            if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) {
[1365]422                return TCL_ERROR;
423            }
[2843]424            if (n <= 0) {
[1365]425                Tcl_AppendResult(interp, "bad extents value: must be > 0",
426                                 (char *)NULL);
427                return TCL_ERROR;
428            }
[2843]429            _nComponents = n;
[1365]430        } else if ((c == 'a') && (strcmp(string, "axisorder") == 0)) {
[2843]431            Tcl_Obj **order;
432            int n;
[1365]433
[5552]434            if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &order)
[2843]435                != TCL_OK) {
[1365]436                return TCL_ERROR;
437            }
[2843]438            if (n != 2) {
[5552]439                Tcl_AppendResult(interp,
[2843]440                        "wrong # of axes defined for ordering the data",
441                        (char *)NULL);
[1365]442                return TCL_ERROR;
[2843]443            }
444            if ((GetAxisFromObj(interp, order[0], axis) != TCL_OK) ||
445                (GetAxisFromObj(interp, order[1], axis+1) != TCL_OK)) {
446                return TCL_ERROR;
447            }
[1365]448        } else {
449            Tcl_AppendResult(interp, "unknown key \"", string,
450                (char *)NULL);
451            return TCL_ERROR;
452        }
453    }
[1984]454    if (_nValues == 0) {
[1365]455        Tcl_AppendResult(interp, "missing \"values\" key", (char *)NULL);
456        return TCL_ERROR;
457    }
[1429]458    if (_nValues != (_xNum * _yNum * _nComponents)) {
[5552]459        Tcl_AppendResult(interp,
460                "wrong number of values: must be xnum*ynum*components",
[2843]461                         (char *)NULL);
[1365]462        return TCL_ERROR;
463    }
[5552]464
[1365]465    if ((axis[0] != 1) || (axis[1] != 0)) {
[3452]466        TRACE("reordering data");
[2843]467        /* Reorder the data into x, y where x varies fastest and so on. */
468        size_t y;
469        float *dp;
[1365]470
[2843]471        dp = _values = (float *)realloc(_values, sizeof(float) * _nValues);
472        for (y = 0; y < _yNum; y++) {
473            size_t x;
[1365]474
[2843]475            for (x = 0; x < _xNum; x++) {
476                size_t i, v;
[5552]477
[2843]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        }
[1365]486    }
[1429]487    _initialized = true;
[1365]488    return TCL_OK;
489}
[1429]490
[4068]491#ifdef USE_DX_READER
[1429]492bool
[4063]493nv::Unirect3d::importDx(size_t nComponents,
[5552]494                        size_t length, char *string)
[1429]495{
[1458]496    int nx, ny, nz, npts;
[1429]497    double x0, y0, z0, dx, dy, dz, ddx, ddy, ddz;
[1529]498    char *p, *pend;
[1429]499
[5430]500    dx = dy = dz = 0.0;
[5552]501    x0 = y0 = z0 = 0.0;                      /* May not have an origin line. */
[5430]502    nx = ny = nz = npts = 0;
[1529]503    for (p = string, pend = p + length; p < pend; /*empty*/) {
[2843]504        char *line;
[1429]505
[2843]506        line = getline(&p, pend);
[1529]507        if (line == pend) {
[2843]508            break;                        /* EOF */
509        }
[1429]510        if ((line[0] == '#') || (line == '\0')) {
[2843]511            continue;                        /* Skip blank or comment lines. */
512        }
[5552]513        if (sscanf(line, "object %*d class gridpositions counts %d %d %d",
[2843]514                   &nx, &ny, &nz) == 3) {
515            if ((nx < 0) || (ny < 0) || (nz < 0)) {
[5552]516                ERROR("invalid grid size: x=%d, y=%d, z=%d",
[4063]517                      nx, ny, nz);
[2843]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. */
[5328]524            int count = 0;
[2843]525            if (ddx != 0.0) {
526                dx = ddx;
[5328]527                count++;
[5430]528            }
529            if (ddy != 0.0) {
[2843]530                dy = ddy;
[5328]531                count++;
[5430]532            }
533            if (ddz != 0.0) {
[2843]534                dz = ddz;
[5328]535                count++;
[2843]536            }
[5328]537            if (count > 1) {
538                ERROR("don't know how to handle multiple non-zero delta values");
539                return false;
540            }
[5460]541        } else if (sscanf(line, "object %*d class array type %*s rank 1"
542                " shape 3 items %d data follows", &npts) == 1) {
[2843]543            if (npts < 0) {
[4063]544                ERROR("bad # points %d", npts);
[2843]545                return false;
546            }
[3452]547            TRACE("#points=%d", npts);
[2843]548            if (npts != nx*ny*nz) {
[4063]549                ERROR("inconsistent data: expected %d points"
550                      " but found %d points", nx*ny*nz, npts);
[2843]551                return false;
552            }
553            break;
554        }
[1429]555    }
556    if (npts != nx*ny*nz) {
[4063]557        ERROR("inconsistent data: expected %d points"
558              " but found %d points", nx*ny*nz, npts);
[2843]559        return false;
[1429]560    }
[5328]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    }
[1429]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;
[1434]578    _nComponents = nComponents;
579
[1984]580    _values = (float *)realloc(_values, sizeof(float) * npts * _nComponents);
[1429]581    _nValues = 0;
582    for (size_t ix = 0; ix < _xNum; ix++) {
[2843]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        }
[1429]618    }
[1529]619    /* Make sure that we read all of the expected points. */
[1458]620    if (_nValues != (size_t)npts) {
[4063]621        ERROR("inconsistent data: expected %d points"
622              " but found %d points", npts, _nValues);
[2843]623        free(_values);
624        _values = NULL;
625        return false;
[1429]626    }
627    _nValues *= _nComponents;
628    _initialized = true;
[1453]629#ifdef notdef
[5552]630    {
[2843]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);
[1452]642    }
[1453]643#endif
[1429]644    return true;
645}
[4068]646#endif
[1429]647
[1434]648void
[4056]649nv::Unirect3d::getVectorRange()
[1434]650{
651    assert(_nComponents == 3);
652    _magMax = -DBL_MAX, _magMin = DBL_MAX;
653    size_t i;
654    for (i = 0; i < _nValues; i += _nComponents) {
[2843]655        double vx, vy, vz, vm;
[1434]656
[2843]657        vx = _values[i];
658        vy = _values[i+1];
659        vz = _values[i+2];
[5552]660
[2843]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        }
[1434]668    }
[3452]669    TRACE("getVectorRange: %g %g", _magMin, _magMax);
[1434]670}
[1447]671
[5552]672bool
[4056]673nv::Unirect3d::convert(nv::Unirect2d *dataPtr)
[1447]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:
[2843]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;
[1447]700    case 2:
[2843]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;
[1447]716    }
717    return true;
718}
Note: See TracBrowser for help on using the repository browser.