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