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
RevLine 
[882]1
[932]2#include "RpDX.h"
[931]3#include "dxReaderCommon.h"
[882]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"
[1325]15#include "RpOutcome.h"
[882]16/* Load a 3D volume from a dx-format file the new way
17 */
[1493]18Volume *
19load_volume_stream_odx(Rappture::Outcome &context, const char *tag,
20                        const char *buf, int nBytes)
[956]21{
[882]22    char dxfilename[128];
23
[1382]24    if (nBytes <= 0) {
25        context.error("empty data buffer\n");
[1493]26        return NULL;
[882]27    }
28
29    // write the dx file to disk, because DXImportDX takes a file name
[1028]30
31    // You can do it like this.  Give
[882]32    sprintf(dxfilename, "/tmp/dx%d.dx", getpid());
33
[886]34    FILE *f;
[931]35
[886]36    f = fopen(dxfilename, "w");
[1380]37
38    ssize_t nWritten;
39    nWritten = fwrite(buf, sizeof(char), nBytes, f);
[886]40    fclose(f);
[1380]41    if (nWritten != nBytes) {
[1381]42        context.addError("Can't read %d bytes from file \"%s\"\n",
[1380]43                         nBytes, dxfilename);
[1493]44        return NULL;
[1380]45    }
[882]46
[1381]47    Rappture::DX dxObj(context, dxfilename);
[882]48
[1325]49    if (unlink(dxfilename) != 0) {
[1381]50        context.addError("Error deleting dx file: %s\n", dxfilename);
[1493]51        return NULL;
[931]52    }
[882]53
[956]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;
[882]60
[956]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();
[882]67
[956]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];
[931]71
[956]72        // scale all values [0-1], -1 => out of bounds
73        v = (isnan(v)) ? -1.0 : (v - vmin)/dv;
[931]74
[956]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        }
[882]86    }
[931]87
[956]88    computeSimpleGradient(data, nx, ny, nz);
89
[931]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);
[956]92    fprintf(stdout,"dataMin = %lg\tdataMax = %lg\tnzero_min = %lg\n", dxObj.dataMin(),dxObj.dataMax(),dxObj.nzero_min());
[886]93    fflush(stdout);
[882]94
[956]95    Volume *volPtr;
[1493]96    volPtr = NanoVis::load_volume(tag, nx, ny, nz, 4, data,
[1325]97                                  dxObj.dataMin(),
98                                  dxObj.dataMax(),
99                                  dxObj.nzero_min());
[959]100    const float *origin = dxObj.origin();
101    const float *max = dxObj.max();
[931]102
[959]103    volPtr->xAxis.SetRange(origin[0], max[0]);
104    volPtr->yAxis.SetRange(origin[1], max[1]);
105    volPtr->zAxis.SetRange(origin[2], max[2]);
[961]106    volPtr->wAxis.SetRange(dxObj.dataMin(), dxObj.dataMax());
[956]107    volPtr->update_pending = true;
[959]108
[961]109    delete [] data;
[956]110
[931]111    //
112    // Center this new volume on the origin.
113    //
[886]114    float dx0 = -0.5;
115    float dy0 = -0.5*dy/dx;
116    float dz0 = -0.5*dz/dx;
[1478]117    volPtr->location(Vector3(dx0, dy0, dz0));
[1493]118    return volPtr;
[882]119}
Note: See TracBrowser for help on using the repository browser.