source: trunk/src/core/RpDXWriter.cc @ 1092

Last change on this file since 1092 was 1092, checked in by gah, 16 years ago

yet another fix for perl language binding

File size: 8.3 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture::DXWriter
4 *
5 *  Rappture DXWriter object for forming dx files nanovis can read
6 *
7 * ======================================================================
8 *  AUTHOR:  Derrick S. Kearney, Purdue University
9 *  Copyright (c) 2005-2008  Purdue Research Foundation
10 *
11 *  See the file "license.terms" for information on usage and
12 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * ======================================================================
14 */
15#include <cmath>
16#include <cstdio>
17#include <cstdlib>
18#include <cfloat>
19#include <RpDXWriter.h>
20
21using namespace Rappture;
22
23DXWriter::DXWriter() :
24    _dataBuf(SimpleDoubleBuffer()),
25    _posBuf(SimpleDoubleBuffer()),
26    _rank(3),
27    _shape(0),
28    _positions(NULL),
29    _delta(NULL),
30    _origin(NULL)
31{
32    _delta  = (double*) malloc(_rank*_rank*sizeof(double));
33    if (_delta == NULL) {
34        fprintf(stderr,
35            "Error allocating %lu bytes, for _delta, inside DXWriter Constructor\n",
36            (unsigned long)(_rank*_rank*sizeof(double)));
37            return;
38    }
39    for (size_t j=0; j < _rank; j++) {
40        for (size_t i=0; i < _rank; i++) {
41            size_t idx = (_rank*j)+i;
42            if (j != i) {
43                _delta[idx]= 0.0f;
44            }
45            else {
46                _delta[idx] = 1.0f;
47            }
48        }
49    }
50
51    _origin = (double*) malloc(_rank*sizeof(double));
52    if (_origin == NULL) {
53        fprintf(stderr,
54            "Error allocating %lu bytes, for _origin, inside DXWriter Constructor\n",
55            (unsigned long)(_rank*sizeof(double)));
56            return;
57    }
58    for (size_t i=0; i < _rank; i++) {
59        _origin[i] = 0.0f;
60    }
61}
62
63DXWriter::DXWriter(double* data, size_t nmemb, size_t rank, size_t shape) :
64    _dataBuf(SimpleDoubleBuffer(data,nmemb)),
65    _posBuf(SimpleDoubleBuffer()),
66    _rank(rank),
67    _shape(shape),
68    _positions(NULL),
69    _delta(NULL),
70    _origin(NULL)
71{
72    _delta  = (double*) malloc(_rank*_rank*sizeof(double));
73    if (_delta == NULL) {
74        fprintf(stderr,
75            "Error allocating %lu bytes, for _delta, inside DXWriter Constructor\n",
76            (unsigned long)(_rank*_rank*sizeof(double)));
77            return;
78    }
79    for (size_t j=0; j < _rank; j++) {
80        for (size_t i=0; i < _rank; i++) {
81            size_t idx = (_rank*j)+i;
82            if (j != i) {
83                _delta[idx] = 0.0f;
84            } else {
85                _delta[idx] = 1.0f;
86            }
87        }
88    }
89
90    _origin = (double*) malloc(_rank*sizeof(double));
91    if (_origin == NULL) {
92        fprintf(stderr,
93            "Error allocating %lu bytes, for _origin, inside DXWriter Constructor\n",
94            (unsigned long)(_rank*sizeof(double)));
95            return;
96    }
97    for (size_t i=0; i < _rank; i++) {
98        _origin[i] = 0.0f;
99    }
100}
101
102DXWriter::~DXWriter()
103{
104    if (_positions) {
105        free(_positions);
106    }
107
108    if (_delta) {
109        free(_delta);
110    }
111
112    if (_origin) {
113        free(_origin);
114    }
115}
116
117DXWriter&
118DXWriter::origin(double* o)
119{
120    if (o == NULL) {
121        return *this;
122    }
123
124    size_t nbytes = _rank * sizeof(double);
125    double *tmp = (double*) malloc(nbytes);
126    if (tmp == NULL) {
127        fprintf(stderr,"Unable to malloc %lu bytes inside DXWriter::origin\n",
128            (unsigned long)nbytes);
129        return *this;
130    }
131    memcpy((void*)tmp,(void const*)o,nbytes);
132
133    if (_origin) {
134        free(_origin);
135    }
136
137    _origin = tmp;
138
139    return *this;
140}
141
142DXWriter&
143DXWriter::delta(double* d)
144{
145    if (d == NULL) {
146        return *this;
147    }
148
149    size_t nbytes = _rank * _rank * sizeof(double);
150    double *tmp = (double*)malloc(nbytes);
151    if (tmp == NULL) {
152        fprintf(stderr,"Unable to malloc %lu bytes inside DXWriter::delta\n",
153            (unsigned long)nbytes);
154        return *this;
155    }
156    memcpy((void*)tmp,(void const*)d,nbytes);
157
158    if (_delta) {
159        free(_delta);
160    }
161
162    _delta = tmp;
163
164    return *this;
165}
166
167DXWriter&
168DXWriter::counts(size_t *p)
169{
170    if (p == NULL) {
171        return *this;
172    }
173    size_t nbytes = _rank * sizeof(size_t);
174    size_t *tmp = (size_t*) malloc(nbytes);
175    if (tmp == NULL) {
176        fprintf(stderr,"Unable to malloc %lu bytes inside DXWriter::pos\n",
177            (unsigned long)nbytes);
178        return *this;
179    }
180    memcpy((void*)tmp,(void const*)p,nbytes);
181
182    if (_positions) {
183        free(_positions);
184    }
185
186    _positions = tmp;
187    return *this;
188}
189
190DXWriter&
191DXWriter::append(double* value)
192{
193    _dataBuf.append(value,1);
194    return *this;
195}
196
197DXWriter&
198DXWriter::data(double *d, size_t nmemb)
199{
200    _dataBuf.append(d,nmemb);
201    return *this;
202}
203
204DXWriter&
205DXWriter::pos(double *p, size_t nmemb)
206{
207    _posBuf.append(p,nmemb);
208    return *this;
209}
210
211DXWriter&
212DXWriter::write(FILE *stream)
213{
214    SimpleCharBuffer *dxfile = NULL;
215
216    if (stream == NULL) {
217        fprintf(stderr,"stream is NULL, cannot write to NULL file inside DXWriter::write\n");
218        return *this;
219    }
220
221    dxfile = new SimpleCharBuffer();
222
223    _writeDxToBuffer(dxfile);
224    fwrite((const void*)(dxfile->bytes()),1,dxfile->size(),stream);
225
226    delete dxfile;
227
228    return *this;
229}
230
231DXWriter&
232DXWriter::write(const char* fname)
233{
234    FILE *fp = NULL;
235
236    if (fname == NULL) {
237        fprintf(stderr,"filename is NULL, cannot write to NULL file inside DXWriter::write\n");
238        return *this;
239    }
240
241    fp = fopen(fname,"wb");
242    write(fp);
243    fclose(fp);
244
245    return *this;
246}
247
248DXWriter&
249DXWriter::write(char *str)
250{
251    SimpleCharBuffer *dxfile = NULL;
252    int sz = 0;
253
254    dxfile = new SimpleCharBuffer();
255
256    _writeDxToBuffer(dxfile);
257    sz = dxfile->size();
258    str = new char[sz+1];
259    memcpy(str,dxfile->bytes(),sz);
260    str[sz] = '\0';
261
262    delete dxfile;
263
264    return *this;
265}
266
267/*
268 *  even though dxfile.append() is more expensive,
269 *  we don't only use sprintf inside the for loops because
270 *  we don't know how many loop iterations there will be
271 *  and our static buffer is a constant number of chars.
272 */
273
274DXWriter&
275DXWriter::_writeDxToBuffer(SimpleCharBuffer *dxfile)
276{
277    char b[80];
278    double f = 0.0;
279
280    // expand our original buffer to 512 characters
281    // because we know there are at least
282    // 400 characters in even our smallest dx file.
283    dxfile->set(512);
284
285    dxfile->append("<ODX>object 1 class gridpositions counts",40);
286    for (size_t i=0; i < _rank; i++) {
287        sprintf(b, " %10lu", (unsigned long)_positions[i]);
288        dxfile->append(b,11);
289    }
290
291    dxfile->append("\norigin");
292    for (size_t i=0; i < _rank; i++) {
293        sprintf(b, " %10g",_origin[i]);
294        dxfile->append(b,11);
295    }
296
297    for (size_t row=0; row < _rank; row++) {
298        dxfile->append("\ndelta");
299        for (size_t col=0; col < _rank; col++) {
300            sprintf(b, " %10g",_delta[(_rank*row)+col]);
301            dxfile->append(b,11);
302        }
303    }
304
305    dxfile->append("\nobject 2 class gridconnections counts", 38);
306    for (size_t i=0; i < _rank; i++) {
307        sprintf(b, " %10lu",(unsigned long)_positions[i]);
308        dxfile->append(b,11);
309    }
310
311    dxfile->append("\nattribute \"element type\" string \"cubes\"\n",41);
312    dxfile->append("attribute \"ref\" string \"positions\"\n",35);
313
314    sprintf(b,"object 3 class array type float rank 0 items %lu data follows\n",
315        (unsigned long)_dataBuf.nmemb());
316    dxfile->append(b);
317
318    _dataBuf.seek(0,SEEK_SET);
319    while (!_dataBuf.eof()) {
320        _dataBuf.read(&f,1);
321        // nanovis and many other progs fail when you send it inf data
322        if (!std::isfinite(f)) {
323            f = 0.0;
324        }
325        sprintf(b,"    %10g\n",f);
326        // we do not know the length of the string
327        // only that it has 10 sigdigs, so we cannot tell
328        // append the size of the data
329        // dxfile->append(b,15);
330        dxfile->append(b);
331    }
332
333    dxfile->append("attribute \"dep\" string \"positions\"\n",35);
334    dxfile->append("object \"density\" class field\n",29);
335    dxfile->append("component \"positions\" value 1\n",30);
336    dxfile->append("component \"connections\" value 2\n",32);
337    dxfile->append("component \"data\" value 3\n",25);
338
339    return *this;
340}
341
342size_t
343DXWriter::size() const
344{
345    return _dataBuf.size();
346}
347
348size_t
349DXWriter::rank(size_t rank)
350{
351    if (rank > 0) {
352        _rank = rank;
353    }
354
355    return _rank;
356}
357
358size_t
359DXWriter::shape(size_t shape)
360{
361    if (shape > 0) {
362        _shape = shape;
363    }
364
365    return _shape;
366}
Note: See TracBrowser for help on using the repository browser.