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

Last change on this file since 1380 was 1365, checked in by gah, 15 years ago
File size: 12.1 KB
Line 
1
2#include <tcl.h>
3#include <Unirect.h>
4
5extern int GetFloatFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr,
6        float *valuePtr);
7extern int GetAxisFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, int *indexPtr);
8
9int
10Rappture::Unirect3d::LoadData(Tcl_Interp *interp, int objc,
11                              Tcl_Obj *const *objv)
12{
13    int num[3], nValues;
14    float min[3], max[3];
15    float *values;
16    const char *units[4], *order;
17
18
19    if ((objc & 0x01) == 0) {
20        Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ",
21                "wrong number of arguments: should be key-value pairs",
22                (char *)NULL);
23        return TCL_ERROR;
24    }
25
26    /* Default order is  z, y, x. */
27    int axis1, axis2, axis3;
28    axis1 = 2;                  /* Z-axis */
29    axis2 = 1;                  /* Y-axis */
30    axis3 = 0;                  /* X-axis */
31
32    int extents = 1;
33    values = NULL;
34    num[0] = num[1] = num[2] = nValues = 0;
35    min[0] = min[1] = min[2] = max[0] = max[1] = max[2] = 0.0f;
36    order = units[0] = units[1] = units[2] = units[3] = NULL;
37
38    int i;
39    for (i = 1; i < objc; i += 2) {
40        const char *string;
41        char c;
42
43        string = Tcl_GetString(objv[i]);
44        c = string[0];
45        if ((c == 'x') && (strcmp(string, "xmin") == 0)) {
46            if (GetFloatFromObj(interp, objv[i+1], min+2) != TCL_OK) {
47                return TCL_ERROR;
48            }
49        } else if ((c == 'x') && (strcmp(string, "xmax") == 0)) {
50            if (GetFloatFromObj(interp, objv[i+1], max+2) != TCL_OK) {
51                return TCL_ERROR;
52            }
53        } else if ((c == 'x') && (strcmp(string, "xnum") == 0)) {
54            if (Tcl_GetIntFromObj(interp, objv[i+1], num+2) != TCL_OK) {
55                return TCL_ERROR;
56            }
57            if (num[2] <= 0) {
58                Tcl_AppendResult(interp, "bad xnum value: must be > 0",
59                     (char *)NULL);
60                return TCL_ERROR;
61            }
62        } else if ((c == 'x') && (strcmp(string, "xunits") == 0)) {
63            units[2] = Tcl_GetString(objv[i+1]);
64        } else if ((c == 'y') && (strcmp(string, "ymin") == 0)) {
65            if (GetFloatFromObj(interp, objv[i+1], min+1) != TCL_OK) {
66                return TCL_ERROR;
67            }
68        } else if ((c == 'y') && (strcmp(string, "ymax") == 0)) {
69            if (GetFloatFromObj(interp, objv[i+1], max+1) != TCL_OK) {
70                return TCL_ERROR;
71            }
72        } else if ((c == 'y') && (strcmp(string, "ynum") == 0)) {
73            if (Tcl_GetIntFromObj(interp, objv[i+1], num+1) != TCL_OK) {
74                return TCL_ERROR;
75            }
76            if (num[1] <= 0) {
77                Tcl_AppendResult(interp, "bad ynum value: must be > 0",
78                                 (char *)NULL);
79                return TCL_ERROR;
80            }
81        } else if ((c == 'y') && (strcmp(string, "yunits") == 0)) {
82            units[1] = Tcl_GetString(objv[i+1]);
83        } else if ((c == 'z') && (strcmp(string, "zmin") == 0)) {
84            if (GetFloatFromObj(interp, objv[i+1], min) != TCL_OK) {
85                return TCL_ERROR;
86            }
87        } else if ((c == 'z') && (strcmp(string, "zmax") == 0)) {
88            if (GetFloatFromObj(interp, objv[i+1], max) != TCL_OK) {
89                return TCL_ERROR;
90            }
91        } else if ((c == 'z') && (strcmp(string, "znum") == 0)) {
92            if (Tcl_GetIntFromObj(interp, objv[i+1], num) != TCL_OK) {
93                return TCL_ERROR;
94            }
95            if (num[0] <= 0) {
96                Tcl_AppendResult(interp, "bad znum value: must be > 0",
97                                 (char *)NULL);
98                return TCL_ERROR;
99            }
100        } else if ((c == 'z') && (strcmp(string, "zunits") == 0)) {
101            units[0] = Tcl_GetString(objv[i+1]);
102        } else if ((c == 'v') && (strcmp(string, "values") == 0)) {
103            Tcl_Obj **vobj;
104
105            if (Tcl_ListObjGetElements(interp, objv[i+1], &nValues, &vobj)
106                != TCL_OK) {
107                return TCL_ERROR;
108            }
109            values = new float[nValues];
110            int j;
111            for (j = 0; j < nValues; j++) {
112                if (GetFloatFromObj(interp, vobj[j], values + j)!=TCL_OK) {
113                    return TCL_ERROR;
114                }
115            }
116        } else if ((c == 'u') && (strcmp(string, "units") == 0)) {
117            _vUnits = strdup(Tcl_GetString(objv[i+1]));
118        } else if ((c == 'o') && (strcmp(string, "order") == 0)) {
119            Tcl_Obj **axes;
120            int n;
121
122            if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &axes)
123                != TCL_OK) {
124                return TCL_ERROR;
125            }
126            if (n != 3) {
127                return TCL_ERROR;
128            }
129            if ((GetAxisFromObj(interp, axes[0], &axis1) != TCL_OK) ||
130                (GetAxisFromObj(interp, axes[1], &axis2) != TCL_OK) ||
131                (GetAxisFromObj(interp, axes[2], &axis3) != TCL_OK)) {
132                return TCL_ERROR;
133            }
134        } else {
135            Tcl_AppendResult(interp, "unknown key \"", string,
136                (char *)NULL);
137            return TCL_ERROR;
138        }
139    }
140    if (values == NULL) {
141        Tcl_AppendResult(interp, "missing \"values\" key", (char *)NULL);
142        return TCL_ERROR;
143    }
144    if (nValues != (num[0] * num[1] * num[2] * extents)) {
145        Tcl_AppendResult(interp,
146                "wrong number of values: must be xnum*ynum*znum*extents",
147                         (char *)NULL);
148        return TCL_ERROR;
149    }
150   
151    fprintf(stderr, "generating %dx%dx%dx%d = %d points\n",
152            num[0], num[1], num[2], extents, num[0]*num[1]*num[2]*extents);
153
154    if ((axis1 != 2) || (axis2 != 1) || (axis3 != 0)) {
155        // Reorder the data into x, y, z where x varies fastest and so on.
156        int z;
157        float *data, *dp;
158
159        dp = data = new float[nValues];
160        for (z = 0; z < num[0]; z++) {
161            int y;
162
163            for (y = 0; y < num[1]; y++) {
164                int x;
165
166                for (x = 0; x < num[2]; x++) {
167                    int i, v;
168                   
169                    /* Compute the index from the data's described ordering. */
170                    i = ((z*num[axis2]*num[axis3]) + (y*num[axis3]) + x) * 3;
171                    for(v = 0; v < extents; v++) {
172                        dp[v] = values[i+v];
173                    }
174                    dp += extents;
175                }
176            }
177        }
178        delete [] values;
179        values = data;
180    }
181
182    _values = values;
183    _nValues = nValues;
184    _extents = extents;
185    if (units[3] != NULL) {
186        _vUnits = strdup(units[3]);
187    }
188    _xMin = min[axis3];
189    _xMax = max[axis3];
190    _xNum = num[axis3];
191    if (units[axis3] != NULL) {
192        _xUnits = strdup(units[axis3]);
193    }
194    _yMin = min[axis2];
195    _yMax = max[axis2];
196    _yNum = num[axis2];
197    if (units[axis2] != NULL) {
198        _yUnits = strdup(units[axis2]);
199    }
200    _zMin = min[axis1];
201    _zMax = max[axis1];
202    _zNum = num[axis1];
203    if (units[axis1] != NULL) {
204        _zUnits = strdup(units[axis1]);
205    }
206    _initialized = true;
207    return TCL_OK;
208}
209
210
211int
212Rappture::Unirect2d::LoadData(Tcl_Interp *interp, int objc,
213                              Tcl_Obj *const *objv)
214{
215
216    if ((objc & 0x01) == 0) {
217        Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ",
218                "wrong number of arguments: should be key-value pairs",
219                (char *)NULL);
220        return TCL_ERROR;
221    }
222
223    int axis[2];
224    axis[0] = 1;                        /* X-axis */
225    axis[1] = 0;                        /* Y-axis */
226
227    _extents = 1;
228    _xNum = _yNum = _nValues = 0;
229    _xMin = _yMin = _xMax = _yMax = 0.0f;
230    if (_xUnits != NULL) {
231        free(_xUnits);
232    }
233    if (_yUnits != NULL) {
234        free(_yUnits);
235    }
236    if (_vUnits != NULL) {
237        free(_vUnits);
238    }
239    _xUnits = _yUnits = _vUnits = NULL;
240    if (_values != NULL) {
241        delete [] _values;
242    }
243    _values = NULL;
244
245    int i;
246    for (i = 1; i < objc; i += 2) {
247        const char *string;
248        char c;
249
250        string = Tcl_GetString(objv[i]);
251        c = string[0];
252        if ((c == 'x') && (strcmp(string, "xmin") == 0)) {
253            if (GetFloatFromObj(interp, objv[i+1], &_xMin) != TCL_OK) {
254                return TCL_ERROR;
255            }
256        } else if ((c == 'x') && (strcmp(string, "xmax") == 0)) {
257            if (GetFloatFromObj(interp, objv[i+1], &_xMax) != TCL_OK) {
258                return TCL_ERROR;
259            }
260        } else if ((c == 'x') && (strcmp(string, "xnum") == 0)) {
261            int n;
262            if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) {
263                return TCL_ERROR;
264            }
265            if (n <= 0) {
266                Tcl_AppendResult(interp, "bad xnum value: must be > 0",
267                     (char *)NULL);
268                return TCL_ERROR;
269            }
270            _xNum = n;
271        } else if ((c == 'x') && (strcmp(string, "xunits") == 0)) {
272            _xUnits = strdup(Tcl_GetString(objv[i+1]));
273        } else if ((c == 'y') && (strcmp(string, "ymin") == 0)) {
274            if (GetFloatFromObj(interp, objv[i+1], &_yMin) != TCL_OK) {
275                return TCL_ERROR;
276            }
277        } else if ((c == 'y') && (strcmp(string, "ymax") == 0)) {
278            if (GetFloatFromObj(interp, objv[i+1], &_yMax) != TCL_OK) {
279                return TCL_ERROR;
280            }
281        } else if ((c == 'y') && (strcmp(string, "ynum") == 0)) {
282            int n;
283            if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) {
284                return TCL_ERROR;
285            }
286            if (n <= 0) {
287                Tcl_AppendResult(interp, "bad ynum value: must be > 0",
288                                 (char *)NULL);
289                return TCL_ERROR;
290            }
291            _yNum = n;
292        } else if ((c == 'y') && (strcmp(string, "yunits") == 0)) {
293            _yUnits = strdup(Tcl_GetString(objv[i+1]));
294        } else if ((c == 'v') && (strcmp(string, "values") == 0)) {
295            Tcl_Obj **vobj;
296            int n;
297
298            if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &vobj) != TCL_OK){
299                return TCL_ERROR;
300            }
301            if (n <= 0) {
302                Tcl_AppendResult(interp, "empty values list : must be > 0",
303                                 (char *)NULL);
304                return TCL_ERROR;
305            }
306            _nValues = n;
307            _values = new float[_nValues];
308            size_t j;
309            for (j = 0; j < _nValues; j++) {
310                if (GetFloatFromObj(interp, vobj[j], _values + j)!=TCL_OK) {
311                    return TCL_ERROR;
312                }
313            }
314        } else if ((c == 'u') && (strcmp(string, "units") == 0)) {
315            _vUnits = strdup(Tcl_GetString(objv[i+1]));
316        } else if ((c == 'e') && (strcmp(string, "extents") == 0)) {
317            int n;
318
319            if (Tcl_GetIntFromObj(interp, objv[i+1], &n) != TCL_OK) {
320                return TCL_ERROR;
321            }
322            if (n <= 0) {
323                Tcl_AppendResult(interp, "bad extents value: must be > 0",
324                                 (char *)NULL);
325                return TCL_ERROR;
326            }
327            _extents = n;
328        } else if ((c == 'a') && (strcmp(string, "axisorder") == 0)) {
329            Tcl_Obj **order;
330            int n;
331
332            if (Tcl_ListObjGetElements(interp, objv[i+1], &n, &order)
333                != TCL_OK) {
334                return TCL_ERROR;
335            }
336            if (n != 2) {
337                Tcl_AppendResult(interp,
338                        "wrong # of axes defined for ordering the data",
339                        (char *)NULL);
340                return TCL_ERROR;
341            }
342            if ((GetAxisFromObj(interp, order[0], axis) != TCL_OK) ||
343                (GetAxisFromObj(interp, order[1], axis+1) != TCL_OK)) {
344                return TCL_ERROR;
345            }
346        } else {
347            Tcl_AppendResult(interp, "unknown key \"", string,
348                (char *)NULL);
349            return TCL_ERROR;
350        }
351    }
352    if (_values == NULL) {
353        Tcl_AppendResult(interp, "missing \"values\" key", (char *)NULL);
354        return TCL_ERROR;
355    }
356    if (_nValues != (_xNum * _yNum * _extents)) {
357        Tcl_AppendResult(interp,
358                "wrong number of values: must be xnum*ynum*extents",
359                         (char *)NULL);
360        return TCL_ERROR;
361    }
362   
363    fprintf(stderr, "generating %dx%dx%d = %d points\n",
364            _xNum, _yNum, _extents, _xNum * _yNum * _extents);
365
366#ifndef notdef
367    if ((axis[0] != 1) || (axis[1] != 0)) {
368        fprintf(stderr, "reordering data\n");
369        // Reorder the data into x, y where x varies fastest and so on.
370        size_t y;
371        float *data, *dp;
372
373        dp = data = new float[_nValues];
374        for (y = 0; y < _yNum; y++) {
375            size_t x;
376
377            for (x = 0; x < _xNum; x++) {
378                size_t i, v;
379                   
380                /* Compute the index from the data's described ordering. */
381                i = (y + (_yNum * x)) * _extents;
382                for(v = 0; v < _extents; v++) {
383                    dp[v] = _values[i+v];
384                }
385                dp += _extents;
386            }
387        }
388        delete [] _values;
389        _values = data;
390    }
391#endif
392    _initialized = true;
393    return TCL_OK;
394}
Note: See TracBrowser for help on using the repository browser.