source: branches/blt4/src/core/RpDXWriter.cc

Last change on this file was 3953, checked in by gah, 11 years ago

sync with trunk

File size: 8.2 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) 2004-2012  HUBzero Foundation, LLC
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#include <assert.h>
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 *f)
213{
214    if (f == NULL) {
215        fprintf(stderr,"FILE is NULL, cannot write to NULL file inside DXWriter::write\n");
216        return *this;
217    }
218    SimpleCharBuffer dxfile;
219    _writeDxToBuffer(dxfile);
220    ssize_t nWritten;
221    nWritten = fwrite(dxfile.bytes(), 1, dxfile.size(), f);
222    assert(nWritten == (ssize_t)dxfile.size());
223    return *this;
224}
225
226DXWriter&
227DXWriter::write(const char* fname)
228{
229    FILE *fp = NULL;
230
231    if (fname == NULL) {
232        fprintf(stderr,"filename is NULL, cannot write to NULL file inside DXWriter::write\n");
233        return *this;
234    }
235
236    fp = fopen(fname,"wb");
237    write(fp);
238    fclose(fp);
239
240    return *this;
241}
242
243DXWriter&
244DXWriter::write(char *str)
245{
246    SimpleCharBuffer dxfile;
247    int sz;
248
249    _writeDxToBuffer(dxfile);
250    sz = dxfile.size();
251    str = new char[sz+1];
252    memcpy(str, dxfile.bytes(), sz);
253    str[sz] = '\0';
254    return *this;
255}
256
257/*
258 *  even though dxfile.append() is more expensive,
259 *  we don't only use sprintf inside the for loops because
260 *  we don't know how many loop iterations there will be
261 *  and our static buffer is a constant number of chars.
262 */
263
264DXWriter&
265DXWriter::_writeDxToBuffer(SimpleCharBuffer &dxfile)
266{
267    char b[80];
268    double f = 0.0;
269
270    // expand our original buffer to 512 characters
271    // because we know there are at least
272    // 400 characters in even our smallest dx file.
273    dxfile.set(512);
274
275    dxfile.append("<ODX>object 1 class gridpositions counts",40);
276    for (size_t i=0; i < _rank; i++) {
277        sprintf(b, " %10lu", (unsigned long)_positions[i]);
278        dxfile.append(b,11);
279    }
280
281    dxfile.append("\norigin");
282    for (size_t i=0; i < _rank; i++) {
283        sprintf(b, " %10g",_origin[i]);
284        dxfile.append(b,11);
285    }
286
287    for (size_t row=0; row < _rank; row++) {
288        dxfile.append("\ndelta");
289        for (size_t col=0; col < _rank; col++) {
290            sprintf(b, " %10g",_delta[(_rank*row)+col]);
291            dxfile.append(b,11);
292        }
293    }
294
295    dxfile.append("\nobject 2 class gridconnections counts", 38);
296    for (size_t i=0; i < _rank; i++) {
297        sprintf(b, " %10lu",(unsigned long)_positions[i]);
298        dxfile.append(b,11);
299    }
300
301    dxfile.append("\nattribute \"element type\" string \"cubes\"\n",41);
302    dxfile.append("attribute \"ref\" string \"positions\"\n",35);
303
304    sprintf(b,"object 3 class array type float rank 0 items %lu data follows\n",
305        (unsigned long)_dataBuf.nmemb());
306    dxfile.append(b);
307
308    _dataBuf.seek(0,SEEK_SET);
309    while (!_dataBuf.eof()) {
310        _dataBuf.read(&f,1);
311        // nanovis and many other progs fail when you send it inf data
312        if (!std::isfinite(f)) {
313            f = 0.0;
314        }
315        sprintf(b,"    %10g\n",f);
316        // we do not know the length of the string
317        // only that it has 10 sigdigs, so we cannot tell
318        // append the size of the data
319        // dxfile.append(b,15);
320        dxfile.append(b);
321    }
322
323    dxfile.append("attribute \"dep\" string \"positions\"\n",35);
324    dxfile.append("object \"density\" class field\n",29);
325    dxfile.append("component \"positions\" value 1\n",30);
326    dxfile.append("component \"connections\" value 2\n",32);
327    dxfile.append("component \"data\" value 3\n",25);
328
329    return *this;
330}
331
332size_t
333DXWriter::size() const
334{
335    return _dataBuf.size();
336}
337
338size_t
339DXWriter::rank(size_t rank)
340{
341    if (rank > 0) {
342        _rank = rank;
343    }
344
345    return _rank;
346}
347
348size_t
349DXWriter::shape(size_t shape)
350{
351    if (shape > 0) {
352        _shape = shape;
353    }
354
355    return _shape;
356}
Note: See TracBrowser for help on using the repository browser.