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

Last change on this file since 1380 was 1380, checked in by gah, 13 years ago
File size: 3.0 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#include "RpOutcome.h"
16/* Load a 3D volume from a dx-format file the new way
17 */
18bool
19load_volume_stream_odx(Rappture::Outcome &outcome, int index, const char *buf,
20                       int nBytes)
21{
22    char dxfilename[128];
23
24    if (nBytes == 0) {
25        outcome.error("data not found in stream");
26        return false;
27    }
28
29    // write the dx file to disk, because DXImportDX takes a file name
30
31    // You can do it like this.  Give
32    sprintf(dxfilename, "/tmp/dx%d.dx", getpid());
33
34    FILE *f;
35
36    f = fopen(dxfilename, "w");
37
38    ssize_t nWritten;
39    nWritten = fwrite(buf, sizeof(char), nBytes, f);
40    fclose(f);
41    if (nWritten != nBytes) {
42        outcome.addError("Can't read %d bytes from file \"%s\"\n",
43                         nBytes, dxfilename);
44        return false;
45    }
46
47    Rappture::DX dxObj(outcome, dxfilename);
48
49    if (unlink(dxfilename) != 0) {
50        outcome.addError("Error deleting dx file: %s\n", dxfilename);
51    }
52
53    int nx = dxObj.axisLen()[0];
54    int ny = dxObj.axisLen()[1];
55    int nz = dxObj.axisLen()[2];
56    float dx = nx;
57    float dy = ny;
58    float dz = nz;
59
60    const float* data1 = dxObj.data();
61    float *data = new float[nx*ny*nz*4];
62    memset(data, 0, nx*ny*nz*4);
63    int iz=0, ix=0, iy=0;
64    float dv = dxObj.dataMax() - dxObj.dataMin();
65    float vmin = dxObj.dataMin();
66
67    for (int i=0; i < nx*ny*nz; i++) {
68        int nindex = (iz*nx*ny + iy*nx + ix) * 4;
69        float v = data1[i];
70
71        // scale all values [0-1], -1 => out of bounds
72        v = (isnan(v)) ? -1.0 : (v - vmin)/dv;
73
74        // place the value in the correct index in cdata
75        data[nindex] = v;
76
77        // calculate next x,y,z coordinate
78        if (++iz >= nz) {
79            iz = 0;
80            if (++iy >= ny) {
81                iy = 0;
82                ++ix;
83            }
84        }
85    }
86
87    computeSimpleGradient(data, nx, ny, nz);
88
89    fprintf(stdout,"End Data Stats index = %i\n",index);
90    fprintf(stdout,"nx = %i ny = %i nz = %i\n",nx,ny,nz);
91    fprintf(stdout,"dx = %lg dy = %lg dz = %lg\n",dx,dy,dz);
92    fprintf(stdout,"dataMin = %lg\tdataMax = %lg\tnzero_min = %lg\n", dxObj.dataMin(),dxObj.dataMax(),dxObj.nzero_min());
93    fflush(stdout);
94
95    Volume *volPtr;
96    volPtr = NanoVis::load_volume(index, nx, ny, nz, 4, data,
97                                  dxObj.dataMin(),
98                                  dxObj.dataMax(),
99                                  dxObj.nzero_min());
100    const float *origin = dxObj.origin();
101    const float *max = dxObj.max();
102
103    volPtr->xAxis.SetRange(origin[0], max[0]);
104    volPtr->yAxis.SetRange(origin[1], max[1]);
105    volPtr->zAxis.SetRange(origin[2], max[2]);
106    volPtr->wAxis.SetRange(dxObj.dataMin(), dxObj.dataMax());
107    volPtr->update_pending = true;
108
109    delete [] data;
110
111    //
112    // Center this new volume on the origin.
113    //
114    float dx0 = -0.5;
115    float dy0 = -0.5*dy/dx;
116    float dz0 = -0.5*dz/dx;
117    NanoVis::volume[index]->move(Vector3(dx0, dy0, dz0));
118    return true;
119}
Note: See TracBrowser for help on using the repository browser.