source: trunk/packages/vizservers/nanovis/RpDX.cpp @ 1984

Last change on this file since 1984 was 1984, checked in by gah, 14 years ago

Clean up debugging/printing traces

File size: 10.2 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture::DX
4 *
5 *  Rappture DX object for file reading and interacting
6 *  with libDX and friends.
7 *
8 * ======================================================================
9 *  AUTHOR:  Derrick S. Kearney, Purdue University
10 *  Copyright (c) 2005-2009  Purdue Research Foundation
11 *
12 *  See the file "license.terms" for information on usage and
13 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * ======================================================================
15 */
16#include "RpDX.h"
17#undef ERROR
18#include "Trace.h"
19#include <math.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <float.h>
23
24using namespace Rappture;
25
26DX::DX(Outcome &result, const char* filename) :
27    _dataMin(FLT_MAX),
28    _dataMax(-FLT_MAX),
29    _nzero_min(FLT_MAX),
30    _numAxis(0),
31    _axisLen(NULL),
32    _data(NULL),
33    _n(0),
34    _rank(0),
35    _shape(0),
36    _positions(NULL),
37    _delta(NULL),
38    _max(NULL),
39    _origin(NULL)
40{
41    Array dxpos;
42    Array dxdata;
43    Array dxgrid;
44    // category and type are probably not needed
45    // we keep them around in case they hold useful info for the future
46    // we could replace them with NULL inthe DXGetArrayInfo fxn call
47    Category category;
48    Type type;
49
50    if (filename == NULL) {
51        result.addError("filename is NULL");
52        return;
53    }
54    // open the file with libdx
55    fprintf(stdout, "Calling DXImportDX(%s)\n", filename);
56    fflush(stdout);
57    DXenable_locks(0);
58    _dxobj = DXImportDX((char*)filename,NULL,NULL,NULL,NULL);
59    if (_dxobj == NULL) {
60        result.addError("can't import DX file \"%s\"", filename);
61        return;
62    }
63
64    // parse out the positions array
65
66    // FIXME: nanowire will need a different way to parse out the positions
67    //        array since it uses a productarray to store its positions.
68    //        Possibly use DXGetProductArray().
69    dxpos = (Array) DXGetComponentValue((Field) _dxobj, (char *)"positions");
70    if (dxpos == NULL) {
71        result.addError("can't get component value of \"positions\"");
72        return;
73    }
74    DXGetArrayInfo(dxpos, &_n, &type, &category, &_rank, &_shape);
75
76    fprintf(stdout, "_n = %d\n",_n);
77    if (type != TYPE_FLOAT) {
78        result.addError("\"positions\" is not type float (type=%d)\n", type);
79        return;
80    }
81    fprintf(stdout, "_rank = %d\n",_rank);
82    fprintf(stdout, "_shape = %d\n",_shape);
83
84    float* pos = NULL;
85    pos = (float*) DXGetArrayData(dxpos);
86    if (pos == NULL) {
87        result.addError("DXGetArrayData failed to return positions array");
88        return;
89    }
90
91    // first call to get the number of axis needed
92    dxgrid = (Array) DXGetComponentValue((Field) _dxobj, (char *)"connections");
93    if (DXQueryGridConnections(dxgrid, &_numAxis, NULL) == NULL) {
94        // raise error, data is not a regular grid and we cannot handle it
95        result.addError("DX says our grid is not regular, we cannot handle this data");
96        return;
97    }
98
99    _positions = new float[_n*_numAxis];
100    if (_positions == NULL) {
101        // malloc failed, raise error
102        result.addError("malloc of _positions array failed");
103        return;
104    }
105    memcpy(_positions,pos,sizeof(float)*_n*_numAxis);
106
107    _axisLen = new int[_numAxis];
108    if (_axisLen == NULL) {
109        // malloc failed, raise error
110        result.addError("malloc of _axisLen array failed");
111        return;
112    }
113    memset(_axisLen, 0, _numAxis);
114
115    _delta = new float[_numAxis*_numAxis];
116    if (_delta == NULL) {
117        result.addError("malloc of _delta array failed");
118        return;
119    }
120    memset(_delta, 0, _numAxis*_numAxis);
121
122    _origin = new float[_numAxis];
123    if (_origin == NULL) {
124        result.addError("malloc of _origin array failed");
125        return;
126    }
127    memset(_origin, 0, _numAxis);
128
129    _max = new float[_numAxis];
130    if (_max == NULL) {
131        result.addError("malloc of _max array failed");
132        return;
133    }
134    memset(_max, 0, _numAxis);
135
136    __findPosMax();
137
138    // parse out the gridconnections (length of each axis) array
139    // dxgrid = (Array) DXQueryGridConnections(dxpos, &_numAxis, _axisLen);
140    DXQueryGridPositions(dxpos, NULL, _axisLen, _origin, _delta);
141
142    fprintf(stdout, "_max = [%g,%g,%g]\n",_max[0],_max[1],_max[2]);
143    fprintf(stdout, "_delta = [%g,%g,%g]\n",_delta[0],_delta[1],_delta[2]);
144    fprintf(stdout, "         [%g,%g,%g]\n",_delta[3],_delta[4],_delta[5]);
145    fprintf(stdout, "         [%g,%g,%g]\n",_delta[6],_delta[7],_delta[8]);
146    fprintf(stdout, "_origin = [%g,%g,%g]\n",_origin[0],_origin[1],_origin[2]);
147    fprintf(stdout, "_axisLen = [%i,%i,%i]\n",_axisLen[0],_axisLen[1],_axisLen[2]);
148    fflush(stdout);
149
150    // grab the data array from the dx object and store it in _data
151    dxdata = (Array) DXGetComponentValue((Field) _dxobj, (char *)"data");
152    DXGetArrayInfo(dxdata, NULL, &type, NULL, NULL, NULL);
153    _data = new float[_n];
154    if (_data == NULL) {
155        result.addError("malloc of _data array failed");
156        return;
157    }
158
159    switch (type) {
160    case TYPE_FLOAT:
161        float *float_data;
162
163        float_data = (float *)DXGetArrayData(dxdata);
164        memcpy(_data, float_data, sizeof(float)*_n);
165        break;
166
167    case TYPE_DOUBLE:
168        double *double_data;
169        double_data = (double *)DXGetArrayData(dxdata);
170        for (int i = 0; i < _n; i++) {
171            _data[i] = double_data[i];
172        }
173        break;
174
175    default:
176        result.addError("don't know how to handle data of type %d\n", type);
177        return;
178    }
179
180    // print debug info
181    for (int lcv = 0, pt = 0; lcv < _n; lcv +=3, pt+=9) {
182        fprintf(stdout,
183            "(%f,%f,%f)|->% 8e\n(%f,%f,%f)|->% 8e\n(%f,%f,%f)|->% 8e\n",
184            _positions[pt],_positions[pt+1],_positions[pt+2], _data[lcv],
185            _positions[pt+3],_positions[pt+4],_positions[pt+5],_data[lcv+1],
186            _positions[pt+6],_positions[pt+7],_positions[pt+8],_data[lcv+2]);
187        fflush(stdout);
188    }
189    __collectDataStats();
190}
191
192DX::~DX()
193{
194    delete[] _axisLen;
195    delete[] _delta;
196    delete[] _origin;
197    delete[] _max;
198    delete[] _data;
199    delete[] _positions;
200}
201
202void
203DX::__findPosMax()
204{
205    int lcv = 0;
206
207    // initialize the max array and delta array
208    // max holds the maximum value found for each index
209    for (lcv = 0; lcv < _numAxis; lcv++) {
210        _max[lcv] = _positions[lcv];
211    }
212
213    for (lcv=lcv; lcv < _n*_numAxis; lcv++) {
214        int maxIdx = lcv%_numAxis;
215        if (_positions[lcv] > _max[maxIdx]) {
216            _max[maxIdx] = _positions[lcv];
217        }
218    }
219}
220
221void
222DX::__collectDataStats()
223{
224    _dataMin = FLT_MAX;
225    _dataMax = -FLT_MAX;
226    _nzero_min = FLT_MAX;
227
228    // populate _dataMin, _dataMax, _nzero_min
229    for (int lcv = 0; lcv < _n; lcv++) {
230        if (_data[lcv] < _dataMin) {
231            _dataMin = _data[lcv];
232        }
233        if (_data[lcv] > _dataMax) {
234            _dataMax = _data[lcv];
235        }
236        if ((_data[lcv] > 0.0f) && (_data[lcv] < _nzero_min)) {
237            _nzero_min = _data[lcv];
238        }
239    }
240    if (_nzero_min == FLT_MAX) {
241        ERROR("could not find a positive minimum value\n");
242    }
243}
244
245/*
246 * getInterpPos()
247 *
248 * creates a new grid of positions which can be used for interpolation.
249 * we create the positions array using the function DXMakeGridPositionsV
250 * which creates an n-dimensional grid of regularly spaced positions.
251 * This function overwrites the original positions array
252 */
253void
254DX::__getInterpPos()
255{
256    Array dxpos;
257    float* pos = NULL;
258
259    // gather the positions we want to interpolate over
260    dxpos = DXMakeGridPositionsV(_numAxis, _axisLen, _origin, _delta);
261    DXGetArrayInfo(dxpos, &_n, NULL, NULL, &_rank, &_shape);
262    pos = (float*) DXGetArrayData(dxpos);
263    if (pos == NULL) {
264        fprintf(stdout, "DXGetArrayData failed to return positions array\n");
265        fflush(stdout);
266    }
267
268    if (_positions != NULL) {
269        delete[] _positions;
270    }
271    _positions = new float[_n*_numAxis];
272    if (_positions == NULL) {
273        // malloc failed, raise error
274        fprintf(stdout, "malloc of _axisLen array failed");
275        fflush(stdout);
276    }
277    memcpy(_positions,pos,sizeof(float)*_n*_numAxis);
278
279    pos = NULL;
280    DXDelete((object*)dxpos);
281}
282
283/*
284 * getInterpData()
285 *
286 * this function interpolates over a positions array to produce data for each
287 * point in the positions array. we use the position data stored in _positions
288 * array.
289 */
290void
291DX::__getInterpData()
292{
293    int pts = _n;
294    int interppts = pts;
295    Interpolator interpolator;
296
297    _data = new float[_n];
298    if (_data == NULL) {
299        // malloc failed, raise error
300        fprintf(stdout, "malloc of _data array failed");
301        fflush(stdout);
302    }
303    memset(_data,0,_n);
304
305    // build the interpolator and interpolate
306    fprintf(stdout, "creating DXNewInterpolator...\n");
307    fflush(stdout);
308    interpolator = DXNewInterpolator(_dxobj,INTERP_INIT_IMMEDIATE,-1.0);
309    fprintf(stdout,"_rank = %i\n",_rank);
310    fprintf(stdout,"_shape = %i\n",_shape);
311    fprintf(stdout,"_n = %i\n",_n);
312    fprintf(stdout,"start interppts = %i\n",interppts);
313    fflush(stdout);
314    DXInterpolate(interpolator,&interppts,_positions,_data);
315    fprintf(stdout,"interppts = %i\n",interppts);
316    fflush(stdout);
317
318    // print debug info
319    for (int lcv = 0, pt = 0; lcv < pts; lcv+=3,pt+=9) {
320        fprintf(stdout,
321            "(%f,%f,%f)|->% 8e\n(%f,%f,%f)|->% 8e\n(%f,%f,%f)|->% 8e\n",
322            _positions[pt],_positions[pt+1],_positions[pt+2], _data[lcv],
323            _positions[pt+3],_positions[pt+4],_positions[pt+5],_data[lcv+1],
324            _positions[pt+6],_positions[pt+7],_positions[pt+8],_data[lcv+2]);
325        fflush(stdout);
326    }
327
328    __collectDataStats();
329}
330
331/*
332 * interpolate()
333 *
334 * generate a positions array with optional new axis length and
335 * interpolate to get the new data values at each point in
336 * the positions array. this function currently only works if you
337 * do not change the axis length (i.e. newAxisLen == NULL).
338 */
339DX&
340DX::interpolate(int* newAxisLen)
341{
342    fprintf(stdout, "----begin interpolation----\n");
343    fflush(stdout);
344    if (newAxisLen != NULL) {
345        for (int i = 0; i < _numAxis; i++) {
346            _axisLen[i] = newAxisLen[i];
347        }
348    }
349    __getInterpPos();
350    __getInterpData();
351    fprintf(stdout, "----end interpolation----\n");
352    fflush(stdout);
353    return *this;
354}
355
Note: See TracBrowser for help on using the repository browser.