1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | #include "dxReaderCommon.h" |
---|
3 | #include "GradientFilter.h" |
---|
4 | |
---|
5 | #include "Vector3.h" |
---|
6 | #include "stdlib.h" |
---|
7 | |
---|
8 | float * |
---|
9 | merge(float* scalar, float* gradient, int size) |
---|
10 | { |
---|
11 | float* data = (float*) malloc(sizeof(float) * 4 * size); |
---|
12 | |
---|
13 | Vector3* g = (Vector3*) gradient; |
---|
14 | |
---|
15 | int ngen = 0, sindex = 0; |
---|
16 | for (sindex = 0; sindex <size; ++sindex) { |
---|
17 | data[ngen++] = scalar[sindex]; |
---|
18 | data[ngen++] = g[sindex].x; |
---|
19 | data[ngen++] = g[sindex].y; |
---|
20 | data[ngen++] = g[sindex].z; |
---|
21 | } |
---|
22 | return data; |
---|
23 | } |
---|
24 | |
---|
25 | void |
---|
26 | normalizeScalar(float* fdata, int count, float min, float max) |
---|
27 | { |
---|
28 | float v = max - min; |
---|
29 | if (v != 0.0f) { |
---|
30 | for (int i = 0; i < count; ++i) { |
---|
31 | fdata[i] = fdata[i] / v; |
---|
32 | } |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | float* |
---|
37 | computeGradient(float* fdata, int width, int height, int depth, |
---|
38 | float min, float max) |
---|
39 | { |
---|
40 | float* gradients = (float *)malloc(width * height * depth * 3 * |
---|
41 | sizeof(float)); |
---|
42 | float* tempGradients = (float *)malloc(width * height * depth * 3 * |
---|
43 | sizeof(float)); |
---|
44 | int sizes[3] = { width, height, depth }; |
---|
45 | computeGradients(tempGradients, fdata, sizes, DATRAW_FLOAT); |
---|
46 | filterGradients(tempGradients, sizes); |
---|
47 | quantizeGradients(tempGradients, gradients, sizes, DATRAW_FLOAT); |
---|
48 | normalizeScalar(fdata, width * height * depth, min, max); |
---|
49 | float* data = merge(fdata, gradients, width * height * depth); |
---|
50 | return data; |
---|
51 | } |
---|
52 | |
---|
53 | void |
---|
54 | computeSimpleGradient(float* data, int nx, int ny, int nz) |
---|
55 | { |
---|
56 | // Compute the gradient of this data. BE CAREFUL: center |
---|
57 | // calculation on each node to avoid skew in either direction. |
---|
58 | int ngen = 0; |
---|
59 | for (int iz=0; iz < nz; iz++) { |
---|
60 | for (int iy=0; iy < ny; iy++) { |
---|
61 | for (int ix=0; ix < nx; ix++) { |
---|
62 | // gradient in x-direction |
---|
63 | |
---|
64 | // INSOO -teST |
---|
65 | double valm1 = (ix == 0) ? 0.0 : data[ngen - 4]; |
---|
66 | double valp1 = (ix == nx-1) ? 0.0 : data[ngen + 4]; |
---|
67 | if (valm1 < 0 || valp1 < 0) { |
---|
68 | data[ngen+1] = 0.0; |
---|
69 | } else { |
---|
70 | data[ngen+1] = valp1-valm1; // assume dx=1 // ISO |
---|
71 | //data[ngen+1] = ((valp1-valm1) + 1) * 0.5; // assume dx=1 |
---|
72 | } |
---|
73 | |
---|
74 | // gradient in y-direction |
---|
75 | valm1 = (iy == 0) ? 0.0 : data[ngen-4*nx]; |
---|
76 | valp1 = (iy == ny-1) ? 0.0 : data[ngen+4*nx]; |
---|
77 | if (valm1 < 0 || valp1 < 0) { |
---|
78 | data[ngen+2] = 0.0; |
---|
79 | } else { |
---|
80 | data[ngen+2] = valp1-valm1; // assume dx=1 |
---|
81 | //data[ngen+2] = ((valp1-valm1) + 1) * 0.5; // assume dy=1 // ISO |
---|
82 | } |
---|
83 | |
---|
84 | // gradient in z-direction |
---|
85 | valm1 = (iz == 0) ? 0.0 : data[ngen-4*nx*ny]; |
---|
86 | valp1 = (iz == nz-1) ? 0.0 : data[ngen+4*nx*ny]; |
---|
87 | if (valm1 < 0 || valp1 < 0) { |
---|
88 | data[ngen+3] = 0.0; |
---|
89 | } else { |
---|
90 | data[ngen+3] = valp1-valm1; // assume dx=1 |
---|
91 | //data[ngen+3] = ((valp1-valm1) + 1.0) * 0.5; // assume dz=1 //ISO |
---|
92 | } |
---|
93 | |
---|
94 | ngen += 4; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|