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