source: trunk/packages/vizservers/nanovis/dxReader2.cpp @ 2720

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

make opendx library optional

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