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

Last change on this file since 1055 was 1055, checked in by gah, 16 years ago
File size: 7.0 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(SimpleFloatBuffer()),
25    _posBuf(SimpleFloatBuffer()),
26    _rank(3),
27    _shape(0),
28    _positions(NULL),
29    _delta(NULL),
30    _origin(NULL)
31{
32    _delta  = (float*) malloc(_rank * _rank* sizeof(float));
33    if (_delta == NULL) {
34        fprintf(stderr,
35            "Error allocating %lu bytes, for _delta, inside DXWriter Constructor\n",
36                (unsigned long)(_rank *_rank * sizeof(float)));
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 = (float*) malloc(_rank*sizeof(float));
52    if (_origin == NULL) {
53        fprintf(stderr,
54            "Error allocating %lu bytes, for _origin, inside DXWriter Constructor\n",
55                (unsigned long)(_rank * sizeof(float)));
56            return;
57    }
58    for (size_t i=0; i < _rank; i++) {
59        _origin[i] = 0.0f;
60    }
61}
62
63DXWriter::DXWriter(float* data, size_t nmemb, size_t rank, size_t shape) :
64    _dataBuf(SimpleFloatBuffer(data,nmemb)),
65    _posBuf(SimpleFloatBuffer()),
66    _rank(rank),
67    _shape(shape),
68    _positions(NULL),
69    _delta(NULL),
70    _origin(NULL)
71{
72    _delta  = (float*) malloc(_rank*_rank*sizeof(float));
73    if (_delta == NULL) {
74        fprintf(stderr,
75            "Error allocating %lu bytes, for _delta, inside DXWriter Constructor\n",
76                (unsigned long)(_rank * _rank * sizeof(float)));
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            }
85            else {
86                _delta[idx] = 1.0f;
87            }
88        }
89    }
90
91    _origin = (float*) malloc(_rank*sizeof(float));
92    if (_origin == NULL) {
93        fprintf(stderr,
94            "Error allocating %lu bytes, for _origin, inside DXWriter Constructor\n",
95                (unsigned long)(_rank*sizeof(float)));
96            return;
97    }
98    for (size_t i=0; i < _rank; i++) {
99        _origin[i] = 0.0f;
100    }
101}
102
103DXWriter::~DXWriter()
104{
105    if (_positions) {
106        free(_positions);
107    }
108
109    if (_delta) {
110        free(_delta);
111    }
112
113    if (_origin) {
114        free(_origin);
115    }
116}
117
118DXWriter&
119DXWriter::origin(float* o)
120{
121    if (o == NULL) {
122        return *this;
123    }
124    size_t nbytes = _rank * sizeof(float);
125    float *tmp = (float*) 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(float* d)
144{
145    if (d == NULL) {
146        return *this;
147    }
148
149    size_t nbytes = _rank * _rank * sizeof(float);
150    float *tmp = (float*)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(float* value)
192{
193    _dataBuf.append(value,1);
194    return *this;
195}
196
197DXWriter&
198DXWriter::data(float *d, size_t nmemb)
199{
200    _dataBuf.append(d,nmemb);
201    return *this;
202}
203
204DXWriter&
205DXWriter::pos(float *p, size_t nmemb)
206{
207    _posBuf.append(p,nmemb);
208    return *this;
209}
210
211/*
212 *  even though dxfile.append() is more expensive,
213 *  we don't only use sprintf inside the for loops because
214 *  we don't know how many loop iterations there will be
215 *  and our static buffer is a constant number of chars.
216 */
217
218DXWriter&
219DXWriter::write(FILE *stream)
220{
221    SimpleCharBuffer dxfile;
222    char b[80];
223    float f = 0.0;
224
225    // expand our original buffer to because we know there are at least
226    // 400 characters in even our smallest dx file.
227    dxfile.set(512);
228
229    dxfile.append("object 1 class gridpositions counts",35);
230    for (size_t i=0; i < _rank; i++) {
231        sprintf(b, " %10lu", (unsigned long)_positions[i]);
232        dxfile.append(b,11);
233    }
234
235    dxfile.append("\norigin");
236    for (size_t i=0; i < _rank; i++) {
237        sprintf(b, " %10g",_origin[i]);
238        dxfile.append(b,11);
239    }
240
241    for (size_t row=0; row < _rank; row++) {
242        dxfile.append("\ndelta");
243        for (size_t col=0; col < _rank; col++) {
244            sprintf(b, " %10g",_delta[(_rank*row)+col]);
245            dxfile.append(b,11);
246        }
247    }
248
249    dxfile.append("\nobject 2 class gridconnections counts", 38);
250    for (size_t i=0; i < _rank; i++) {
251        sprintf(b, " %10lu",(unsigned long)_positions[i]);
252        dxfile.append(b,11);
253    }
254
255    dxfile.append("\nattribute \"element type\" string \"cubes\"\n",41);
256    dxfile.append("attribute \"ref\" string \"positions\"\n",35);
257
258    sprintf(b,"object 3 class array type float rank 0 items %d data follows\n",
259        _dataBuf.nmemb());
260    dxfile.append(b);
261
262    _dataBuf.seek(0,SEEK_SET);
263    while (!_dataBuf.eof()) {
264        _dataBuf.read(&f,1);
265        sprintf(b,"    %10g\n",f);
266        dxfile.append(b,15);
267    }
268
269    dxfile.append("attribute \"dep\" string \"positions\"\n",35);
270    dxfile.append("object \"density\" class field\n",29);
271    dxfile.append("component \"positions\" value 1\n",30);
272    dxfile.append("component \"connections\" value 2\n",32);
273    dxfile.append("component \"data\" value 3\n",25);
274
275    fwrite((const void*)dxfile.bytes(),1,dxfile.size(),stream);
276    return *this;
277}
278
279size_t
280DXWriter::size() const
281{
282    return _dataBuf.size();
283}
284
285size_t
286DXWriter::rank(size_t rank)
287{
288    if (rank > 0) {
289        _rank = rank;
290    }
291
292    return _rank;
293}
294
295size_t
296DXWriter::shape(size_t shape)
297{
298    if (shape > 0) {
299        _shape = shape;
300    }
301
302    return _shape;
303}
Note: See TracBrowser for help on using the repository browser.