1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | #include "nvconf.h" |
---|
3 | #ifdef HAVE_DX_DX_H |
---|
4 | #include "RpDX.h" |
---|
5 | #undef ERROR |
---|
6 | #include "dxReaderCommon.h" |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | #include <iostream> |
---|
10 | #include <fstream> |
---|
11 | |
---|
12 | // nanovis headers |
---|
13 | #include "nanovis.h" |
---|
14 | |
---|
15 | // rappture headers |
---|
16 | #include "RpEncode.h" |
---|
17 | #include "RpOutcome.h" |
---|
18 | /* Load a 3D volume from a dx-format file the new way |
---|
19 | */ |
---|
20 | Volume * |
---|
21 | load_volume_stream_odx(Rappture::Outcome &context, const char *tag, |
---|
22 | const char *buf, int nBytes) |
---|
23 | { |
---|
24 | char dxfilename[128]; |
---|
25 | |
---|
26 | if (nBytes <= 0) { |
---|
27 | context.error("empty data buffer\n"); |
---|
28 | return NULL; |
---|
29 | } |
---|
30 | |
---|
31 | // write the dx file to disk, because DXImportDX takes a file name |
---|
32 | |
---|
33 | // You can do it like this. Give |
---|
34 | sprintf(dxfilename, "/tmp/dx%d.dx", getpid()); |
---|
35 | |
---|
36 | FILE *f; |
---|
37 | |
---|
38 | f = fopen(dxfilename, "w"); |
---|
39 | |
---|
40 | ssize_t nWritten; |
---|
41 | nWritten = fwrite(buf, sizeof(char), nBytes, f); |
---|
42 | fclose(f); |
---|
43 | if (nWritten != nBytes) { |
---|
44 | context.addError("Can't read %d bytes from file \"%s\"\n", |
---|
45 | nBytes, dxfilename); |
---|
46 | return NULL; |
---|
47 | } |
---|
48 | |
---|
49 | Rappture::DX dxObj(context, dxfilename); |
---|
50 | |
---|
51 | if (unlink(dxfilename) != 0) { |
---|
52 | context.addError("Error deleting dx file: %s\n", dxfilename); |
---|
53 | return NULL; |
---|
54 | } |
---|
55 | |
---|
56 | int nx = dxObj.axisLen()[0]; |
---|
57 | int ny = dxObj.axisLen()[1]; |
---|
58 | int nz = dxObj.axisLen()[2]; |
---|
59 | float dx = nx; |
---|
60 | float dy = ny; |
---|
61 | float dz = nz; |
---|
62 | |
---|
63 | const float* data1 = dxObj.data(); |
---|
64 | float *data = new float[nx*ny*nz*4]; |
---|
65 | memset(data, 0, nx*ny*nz*4); |
---|
66 | int iz=0, ix=0, iy=0; |
---|
67 | float dv = dxObj.dataMax() - dxObj.dataMin(); |
---|
68 | float vmin = dxObj.dataMin(); |
---|
69 | |
---|
70 | for (int i=0; i < nx*ny*nz; i++) { |
---|
71 | int nindex = (iz*nx*ny + iy*nx + ix) * 4; |
---|
72 | float v = data1[i]; |
---|
73 | |
---|
74 | // scale all values [0-1], -1 => out of bounds |
---|
75 | v = (isnan(v)) ? -1.0 : (v - vmin)/dv; |
---|
76 | |
---|
77 | // place the value in the correct index in cdata |
---|
78 | data[nindex] = v; |
---|
79 | |
---|
80 | // calculate next x,y,z coordinate |
---|
81 | if (++iz >= nz) { |
---|
82 | iz = 0; |
---|
83 | if (++iy >= ny) { |
---|
84 | iy = 0; |
---|
85 | ++ix; |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | computeSimpleGradient(data, nx, ny, nz); |
---|
91 | |
---|
92 | TRACE("nx = %i ny = %i nz = %i\n",nx,ny,nz); |
---|
93 | TRACE("dx = %lg dy = %lg dz = %lg\n",dx,dy,dz); |
---|
94 | TRACE("dataMin = %lg\tdataMax = %lg\tnzero_min = %lg\n", dxObj.dataMin(),dxObj.dataMax(),dxObj.nzero_min()); |
---|
95 | |
---|
96 | Volume *volPtr; |
---|
97 | volPtr = NanoVis::load_volume(tag, 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 volPtr; |
---|
120 | } |
---|
121 | #endif |
---|