source: trunk/vizservers/nanovis/dxReader2.cpp @ 961

Last change on this file since 961 was 961, checked in by gah, 13 years ago

added type checking to dx objects

File size: 2.8 KB
Line 
1
2#include "RpDX.h"
3#include "dxReaderCommon.h"
4
5#include <stdio.h>
6#include <iostream>
7#include <fstream>
8
9// nanovis headers
10#include "Nv.h"
11#include "nanovis.h"
12
13// rappture headers
14#include "RpEncode.h"
15
16/* Load a 3D volume from a dx-format file the new way
17 */
18Rappture::Outcome
19load_volume_stream_odx(int index, const char *buf, int nBytes)
20{
21    Rappture::Outcome outcome;
22
23    char dxfilename[128];
24
25    if (nBytes == 0) {
26        return outcome.error("data not found in stream");
27    }
28
29    // write the dx file to disk, because DXImportDX takes a file name
30    // george suggested using a pipe here
31    sprintf(dxfilename, "/tmp/dx%d.dx", getpid());
32
33    FILE *f;
34
35    f = fopen(dxfilename, "w");
36    fwrite(buf, sizeof(char), nBytes, f);
37    fclose(f);
38
39    Rappture::DX dxObj(dxfilename);
40
41    if (remove(dxfilename) != 0) {
42        fprintf(stderr, "Error deleting dx file: %s\n", dxfilename);
43        fflush(stderr);
44    }
45
46    int nx = dxObj.axisLen()[0];
47    int ny = dxObj.axisLen()[1];
48    int nz = dxObj.axisLen()[2];
49    float dx = nx;
50    float dy = ny;
51    float dz = nz;
52
53    const float* data1 = dxObj.data();
54    float *data = new float[nx*ny*nz*4];
55    memset(data, 0, nx*ny*nz*4);
56    int iz=0, ix=0, iy=0;
57    float dv = dxObj.dataMax() - dxObj.dataMin();
58    float vmin = dxObj.dataMin();
59
60    for (int i=0; i < nx*ny*nz; i++) {
61        int nindex = (iz*nx*ny + iy*nx + ix) * 4;
62        float v = data1[i];
63
64        // scale all values [0-1], -1 => out of bounds
65        v = (isnan(v)) ? -1.0 : (v - vmin)/dv;
66
67        // place the value in the correct index in cdata
68        data[nindex] = v;
69
70        // calculate next x,y,z coordinate
71        if (++iz >= nz) {
72            iz = 0;
73            if (++iy >= ny) {
74                iy = 0;
75                ++ix;
76            }
77        }
78    }
79
80    computeSimpleGradient(data, nx, ny, nz);
81
82    fprintf(stdout,"End Data Stats index = %i\n",index);
83    fprintf(stdout,"nx = %i ny = %i nz = %i\n",nx,ny,nz);
84    fprintf(stdout,"dx = %lg dy = %lg dz = %lg\n",dx,dy,dz);
85    fprintf(stdout,"dataMin = %lg\tdataMax = %lg\tnzero_min = %lg\n", dxObj.dataMin(),dxObj.dataMax(),dxObj.nzero_min());
86    fflush(stdout);
87
88    Volume *volPtr;
89    volPtr = NanoVis::load_volume(index, nx, ny, nz, 4, data,
90                                  dxObj.dataMin(), dxObj.dataMax(), dxObj.nzero_min());
91    const float *origin = dxObj.origin();
92    const float *max = dxObj.max();
93
94    volPtr->xAxis.SetRange(origin[0], max[0]);
95    volPtr->yAxis.SetRange(origin[1], max[1]);
96    volPtr->zAxis.SetRange(origin[2], max[2]);
97    volPtr->wAxis.SetRange(dxObj.dataMin(), dxObj.dataMax());
98    volPtr->update_pending = true;
99
100    delete [] data;
101
102    //
103    // Center this new volume on the origin.
104    //
105    float dx0 = -0.5;
106    float dy0 = -0.5*dy/dx;
107    float dz0 = -0.5*dz/dx;
108    NanoVis::volume[index]->move(Vector3(dx0, dy0, dz0));
109
110    return outcome;
111}
Note: See TracBrowser for help on using the repository browser.