[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" |
---|
| 15 | |
---|
| 16 | /* Load a 3D volume from a dx-format file the new way |
---|
| 17 | */ |
---|
| 18 | Rappture::Outcome |
---|
[956] | 19 | load_volume_stream_odx(int index, const char *buf, int nBytes) |
---|
| 20 | { |
---|
[886] | 21 | Rappture::Outcome outcome; |
---|
[882] | 22 | |
---|
| 23 | char dxfilename[128]; |
---|
| 24 | |
---|
[886] | 25 | if (nBytes == 0) { |
---|
| 26 | return outcome.error("data not found in stream"); |
---|
[882] | 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 | |
---|
[886] | 33 | FILE *f; |
---|
[931] | 34 | |
---|
[886] | 35 | f = fopen(dxfilename, "w"); |
---|
| 36 | fwrite(buf, sizeof(char), nBytes, f); |
---|
| 37 | fclose(f); |
---|
[882] | 38 | |
---|
[931] | 39 | Rappture::DX dxObj(dxfilename); |
---|
[882] | 40 | |
---|
[931] | 41 | if (remove(dxfilename) != 0) { |
---|
| 42 | fprintf(stderr, "Error deleting dx file: %s\n", dxfilename); |
---|
| 43 | fflush(stderr); |
---|
| 44 | } |
---|
[882] | 45 | |
---|
[956] | 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; |
---|
[882] | 52 | |
---|
[956] | 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(); |
---|
[882] | 59 | |
---|
[956] | 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]; |
---|
[931] | 63 | |
---|
[956] | 64 | // scale all values [0-1], -1 => out of bounds |
---|
| 65 | v = (isnan(v)) ? -1.0 : (v - vmin)/dv; |
---|
[931] | 66 | |
---|
[956] | 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 | } |
---|
[882] | 78 | } |
---|
[931] | 79 | |
---|
[956] | 80 | computeSimpleGradient(data, nx, ny, nz); |
---|
| 81 | |
---|
[931] | 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); |
---|
[956] | 85 | fprintf(stdout,"dataMin = %lg\tdataMax = %lg\tnzero_min = %lg\n", dxObj.dataMin(),dxObj.dataMax(),dxObj.nzero_min()); |
---|
[886] | 86 | fflush(stdout); |
---|
[882] | 87 | |
---|
[956] | 88 | Volume *volPtr; |
---|
| 89 | volPtr = NanoVis::load_volume(index, nx, ny, nz, 4, data, |
---|
| 90 | dxObj.dataMin(), dxObj.dataMax(), dxObj.nzero_min()); |
---|
[959] | 91 | const float *origin = dxObj.origin(); |
---|
| 92 | const float *max = dxObj.max(); |
---|
[931] | 93 | |
---|
[959] | 94 | volPtr->xAxis.SetRange(origin[0], max[0]); |
---|
| 95 | volPtr->yAxis.SetRange(origin[1], max[1]); |
---|
| 96 | volPtr->zAxis.SetRange(origin[2], max[2]); |
---|
[961] | 97 | volPtr->wAxis.SetRange(dxObj.dataMin(), dxObj.dataMax()); |
---|
[956] | 98 | volPtr->update_pending = true; |
---|
[959] | 99 | |
---|
[961] | 100 | delete [] data; |
---|
[956] | 101 | |
---|
[931] | 102 | // |
---|
| 103 | // Center this new volume on the origin. |
---|
| 104 | // |
---|
[886] | 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)); |
---|
[931] | 109 | |
---|
[886] | 110 | return outcome; |
---|
[882] | 111 | } |
---|