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

Last change on this file since 1493 was 1493, checked in by gah, 15 years ago

Changed vector id to name

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 */
18Volume *
19load_volume_stream_odx(Rappture::Outcome &context, const char *tag,
20                        const char *buf, int nBytes)
21{
22    char dxfilename[128];
23
24    if (nBytes <= 0) {
25        context.error("empty data buffer\n");
26        return NULL;
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        context.addError("Can't read %d bytes from file \"%s\"\n",
43                         nBytes, dxfilename);
44        return NULL;
45    }
46
47    Rappture::DX dxObj(context, dxfilename);
48
49    if (unlink(dxfilename) != 0) {
50        context.addError("Error deleting dx file: %s\n", dxfilename);
51        return NULL;
52    }
53
54    int nx = dxObj.axisLen()[0];
55    int ny = dxObj.axisLen()[1];
56    int nz = dxObj.axisLen()[2];
57    float dx = nx;
58    float dy = ny;
59    float dz = nz;
60
61    const float* data1 = dxObj.data();
62    float *data = new float[nx*ny*nz*4];
63    memset(data, 0, nx*ny*nz*4);
64    int iz=0, ix=0, iy=0;
65    float dv = dxObj.dataMax() - dxObj.dataMin();
66    float vmin = dxObj.dataMin();
67
68    for (int i=0; i < nx*ny*nz; i++) {
69        int nindex = (iz*nx*ny + iy*nx + ix) * 4;
70        float v = data1[i];
71
72        // scale all values [0-1], -1 => out of bounds
73        v = (isnan(v)) ? -1.0 : (v - vmin)/dv;
74
75        // place the value in the correct index in cdata
76        data[nindex] = v;
77
78        // calculate next x,y,z coordinate
79        if (++iz >= nz) {
80            iz = 0;
81            if (++iy >= ny) {
82                iy = 0;
83                ++ix;
84            }
85        }
86    }
87
88    computeSimpleGradient(data, nx, ny, nz);
89
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(tag, 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    volPtr->location(Vector3(dx0, dy0, dz0));
118    return volPtr;
119}
Note: See TracBrowser for help on using the repository browser.