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 | */ |
---|
18 | bool |
---|
19 | load_volume_stream_odx(Rappture::Outcome &context, int userID, const char *buf, |
---|
20 | int nBytes) |
---|
21 | { |
---|
22 | char dxfilename[128]; |
---|
23 | |
---|
24 | if (nBytes <= 0) { |
---|
25 | context.error("empty data buffer\n"); |
---|
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 | context.addError("Can't read %d bytes from file \"%s\"\n", |
---|
43 | nBytes, dxfilename); |
---|
44 | return false; |
---|
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 false; |
---|
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,"End Data Stats userID = %i\n",userID); |
---|
91 | fprintf(stdout,"nx = %i ny = %i nz = %i\n",nx,ny,nz); |
---|
92 | fprintf(stdout,"dx = %lg dy = %lg dz = %lg\n",dx,dy,dz); |
---|
93 | fprintf(stdout,"dataMin = %lg\tdataMax = %lg\tnzero_min = %lg\n", dxObj.dataMin(),dxObj.dataMax(),dxObj.nzero_min()); |
---|
94 | fflush(stdout); |
---|
95 | |
---|
96 | Volume *volPtr; |
---|
97 | volPtr = NanoVis::load_volume(userID, nx, ny, nz, 4, data, |
---|
98 | dxObj.dataMin(), |
---|
99 | dxObj.dataMax(), |
---|
100 | dxObj.nzero_min()); |
---|
101 | const float *origin = dxObj.origin(); |
---|
102 | const float *max = dxObj.max(); |
---|
103 | |
---|
104 | volPtr->xAxis.SetRange(origin[0], max[0]); |
---|
105 | volPtr->yAxis.SetRange(origin[1], max[1]); |
---|
106 | volPtr->zAxis.SetRange(origin[2], max[2]); |
---|
107 | volPtr->wAxis.SetRange(dxObj.dataMin(), dxObj.dataMax()); |
---|
108 | volPtr->update_pending = true; |
---|
109 | |
---|
110 | delete [] data; |
---|
111 | |
---|
112 | // |
---|
113 | // Center this new volume on the origin. |
---|
114 | // |
---|
115 | float dx0 = -0.5; |
---|
116 | float dy0 = -0.5*dy/dx; |
---|
117 | float dz0 = -0.5*dz/dx; |
---|
118 | volPtr->location(Vector3(dx0, dy0, dz0)); |
---|
119 | return true; |
---|
120 | } |
---|