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
RevLine 
[1051]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
[3953]9 *  Copyright (c) 2004-2012  HUBzero Foundation, LLC
[1051]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>
[1328]20#include <assert.h>
[1051]21using namespace Rappture;
22
23DXWriter::DXWriter() :
[1086]24    _dataBuf(SimpleDoubleBuffer()),
25    _posBuf(SimpleDoubleBuffer()),
[1051]26    _rank(3),
27    _shape(0),
28    _positions(NULL),
29    _delta(NULL),
30    _origin(NULL)
31{
[1086]32    _delta  = (double*) malloc(_rank*_rank*sizeof(double));
[1051]33    if (_delta == NULL) {
34        fprintf(stderr,
[1055]35            "Error allocating %lu bytes, for _delta, inside DXWriter Constructor\n",
[1086]36            (unsigned long)(_rank*_rank*sizeof(double)));
[1051]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
[1086]51    _origin = (double*) malloc(_rank*sizeof(double));
[1051]52    if (_origin == NULL) {
53        fprintf(stderr,
[1055]54            "Error allocating %lu bytes, for _origin, inside DXWriter Constructor\n",
[1086]55            (unsigned long)(_rank*sizeof(double)));
[1051]56            return;
57    }
58    for (size_t i=0; i < _rank; i++) {
59        _origin[i] = 0.0f;
60    }
61}
62
[1086]63DXWriter::DXWriter(double* data, size_t nmemb, size_t rank, size_t shape) :
64    _dataBuf(SimpleDoubleBuffer(data,nmemb)),
65    _posBuf(SimpleDoubleBuffer()),
[1051]66    _rank(rank),
67    _shape(shape),
68    _positions(NULL),
69    _delta(NULL),
70    _origin(NULL)
71{
[1086]72    _delta  = (double*) malloc(_rank*_rank*sizeof(double));
[1051]73    if (_delta == NULL) {
74        fprintf(stderr,
[1055]75            "Error allocating %lu bytes, for _delta, inside DXWriter Constructor\n",
[1086]76            (unsigned long)(_rank*_rank*sizeof(double)));
[1051]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;
[1090]84            } else {
[1051]85                _delta[idx] = 1.0f;
86            }
87        }
88    }
89
[1086]90    _origin = (double*) malloc(_rank*sizeof(double));
[1051]91    if (_origin == NULL) {
92        fprintf(stderr,
[1055]93            "Error allocating %lu bytes, for _origin, inside DXWriter Constructor\n",
[1086]94            (unsigned long)(_rank*sizeof(double)));
[1051]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&
[1086]118DXWriter::origin(double* o)
[1051]119{
120    if (o == NULL) {
121        return *this;
122    }
[1086]123
124    size_t nbytes = _rank * sizeof(double);
125    double *tmp = (double*) malloc(nbytes);
[1051]126    if (tmp == NULL) {
[1055]127        fprintf(stderr,"Unable to malloc %lu bytes inside DXWriter::origin\n",
[1086]128            (unsigned long)nbytes);
[1051]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&
[1086]143DXWriter::delta(double* d)
[1051]144{
145    if (d == NULL) {
146        return *this;
147    }
148
[1086]149    size_t nbytes = _rank * _rank * sizeof(double);
150    double *tmp = (double*)malloc(nbytes);
[1051]151    if (tmp == NULL) {
[1055]152        fprintf(stderr,"Unable to malloc %lu bytes inside DXWriter::delta\n",
[1086]153            (unsigned long)nbytes);
[1051]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    }
[1055]173    size_t nbytes = _rank * sizeof(size_t);
174    size_t *tmp = (size_t*) malloc(nbytes);
[1051]175    if (tmp == NULL) {
[1055]176        fprintf(stderr,"Unable to malloc %lu bytes inside DXWriter::pos\n",
[1086]177            (unsigned long)nbytes);
[1051]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&
[1086]191DXWriter::append(double* value)
[1051]192{
193    _dataBuf.append(value,1);
194    return *this;
195}
196
197DXWriter&
[1086]198DXWriter::data(double *d, size_t nmemb)
[1051]199{
200    _dataBuf.append(d,nmemb);
201    return *this;
202}
203
204DXWriter&
[1086]205DXWriter::pos(double *p, size_t nmemb)
[1051]206{
207    _posBuf.append(p,nmemb);
208    return *this;
209}
210
[1086]211DXWriter&
[1325]212DXWriter::write(FILE *f)
[1086]213{
[1325]214    if (f == NULL) {
215        fprintf(stderr,"FILE is NULL, cannot write to NULL file inside DXWriter::write\n");
[1086]216        return *this;
217    }
[1325]218    SimpleCharBuffer dxfile;
[1086]219    _writeDxToBuffer(dxfile);
[1325]220    ssize_t nWritten;
221    nWritten = fwrite(dxfile.bytes(), 1, dxfile.size(), f);
222    assert(nWritten == (ssize_t)dxfile.size());
[1086]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{
[1325]246    SimpleCharBuffer dxfile;
247    int sz;
[1086]248
249    _writeDxToBuffer(dxfile);
[1325]250    sz = dxfile.size();
[1086]251    str = new char[sz+1];
[1325]252    memcpy(str, dxfile.bytes(), sz);
[1086]253    str[sz] = '\0';
254    return *this;
255}
256
[1051]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&
[1325]265DXWriter::_writeDxToBuffer(SimpleCharBuffer &dxfile)
[1051]266{
267    char b[80];
[1086]268    double f = 0.0;
[1051]269
[1086]270    // expand our original buffer to 512 characters
271    // because we know there are at least
[1051]272    // 400 characters in even our smallest dx file.
[1325]273    dxfile.set(512);
[1051]274
[1325]275    dxfile.append("<ODX>object 1 class gridpositions counts",40);
[1051]276    for (size_t i=0; i < _rank; i++) {
[1055]277        sprintf(b, " %10lu", (unsigned long)_positions[i]);
[1325]278        dxfile.append(b,11);
[1051]279    }
280
[1325]281    dxfile.append("\norigin");
[1051]282    for (size_t i=0; i < _rank; i++) {
283        sprintf(b, " %10g",_origin[i]);
[1325]284        dxfile.append(b,11);
[1051]285    }
286
287    for (size_t row=0; row < _rank; row++) {
[1325]288        dxfile.append("\ndelta");
[1051]289        for (size_t col=0; col < _rank; col++) {
290            sprintf(b, " %10g",_delta[(_rank*row)+col]);
[1325]291            dxfile.append(b,11);
[1051]292        }
293    }
294
[1325]295    dxfile.append("\nobject 2 class gridconnections counts", 38);
[1051]296    for (size_t i=0; i < _rank; i++) {
[1055]297        sprintf(b, " %10lu",(unsigned long)_positions[i]);
[1325]298        dxfile.append(b,11);
[1051]299    }
300
[1325]301    dxfile.append("\nattribute \"element type\" string \"cubes\"\n",41);
302    dxfile.append("attribute \"ref\" string \"positions\"\n",35);
[1051]303
[1060]304    sprintf(b,"object 3 class array type float rank 0 items %lu data follows\n",
305        (unsigned long)_dataBuf.nmemb());
[1325]306    dxfile.append(b);
[1051]307
308    _dataBuf.seek(0,SEEK_SET);
309    while (!_dataBuf.eof()) {
310        _dataBuf.read(&f,1);
[1086]311        // nanovis and many other progs fail when you send it inf data
[1092]312        if (!std::isfinite(f)) {
[1086]313            f = 0.0;
314        }
[1051]315        sprintf(b,"    %10g\n",f);
[1086]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
[1325]319        // dxfile.append(b,15);
320        dxfile.append(b);
[1051]321    }
322
[1325]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);
[1051]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.