glviewvol
view src/volume.cc @ 2:701507c8238f
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 28 Dec 2014 15:26:36 +0200 |
parents | cc9e0d8590e2 |
children | 32c4a7160350 |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <alloca.h>
5 #include <errno.h>
6 #include <math.h>
7 #include "volume.h"
9 static char *strip_space(char *s);
12 Volume::~Volume()
13 {
14 }
16 int Volume::num_samples(int dim) const
17 {
18 return 0;
19 }
21 void Volume::normalf(float *norm, float x, float y, float z, float delta)
22 {
23 float dx, dy, dz;
24 dx = dy = dz = delta;
26 if(num_samples(0) > 0) {
27 // discrete volume
28 dx /= (float)num_samples(0);
29 dy /= (float)num_samples(1);
30 dz /= (float)num_samples(2);
31 }
33 norm[0] = valuef(x + dx, y, z) - valuef(x - dx, y, z);
34 norm[1] = valuef(x, y + dy, z) - valuef(x, y - dy, z);
35 norm[2] = valuef(x, y, z + dz) - valuef(x, y, z - dz);
37 float len = sqrt(norm[0] * norm[0] + norm[1] * norm[1] + norm[2] * norm[2]);
38 if(len != 0.0) {
39 norm[0] /= len;
40 norm[1] /= len;
41 norm[2] /= len;
42 }
43 }
45 void Volume::normali(float *norm, int x, int y, int z)
46 {
47 int sz[3];
49 if((sz[0] = num_samples(0)) <= 0) {
50 // if it's a continuous volume, just call normalf
51 normalf(norm, x, y, z);
52 return;
53 }
54 sz[1] = num_samples(1);
55 sz[2] = num_samples(2);
57 int prevx = x <= 0 ? 0 : x;
58 int nextx = x >= sz[0] - 1 ? sz[0] - 1 : x;
59 int prevy = y <= 0 ? 0 : y;
60 int nexty = y >= sz[1] - 1 ? sz[1] - 1 : y;
61 int prevz = z <= 0 ? 0 : z;
62 int nextz = z >= sz[2] - 1 ? sz[2] - 1 : z;
64 norm[0] = valuei(nextx, y, z) - valuei(prevx, y, z);
65 norm[1] = valuei(x, nexty, z) - valuei(x, prevy, z);
66 norm[2] = valuei(x, y, nextz) - valuei(x, y, prevz);
68 float len = sqrt(norm[0] * norm[0] + norm[1] * norm[1] + norm[2] * norm[2]);
69 if(len != 0.0) {
70 norm[0] /= len;
71 norm[1] /= len;
72 norm[2] /= len;
73 }
74 }
76 // ---- VoxelVolume (discrete) implementation ----
77 VoxelVolume::VoxelVolume()
78 {
79 size[0] = size[1] = size[2] = 0;
80 }
82 bool VoxelVolume::load(const char *fname)
83 {
84 char *prefix = (char*)alloca(strlen(fname) + 1);
85 strcpy(prefix, fname);
86 char *slash = strrchr(prefix, '/');
87 if(slash) {
88 *slash = 0;
89 } else {
90 prefix = 0;
91 }
93 FILE *fp = fopen(fname, "r");
94 if(!fp) {
95 fprintf(stderr, "failed to open file: %s: %s\n", fname, strerror(errno));
96 return false;
97 }
99 char buf[256], path[300];
100 while(fgets(buf, sizeof buf, fp)) {
101 char *line = strip_space(buf);
102 sprintf(path, "%s/%s", prefix, line);
104 Image img;
105 if(!img.load(path)) {
106 slices.clear();
107 return false;
108 }
110 if(slices.empty()) {
111 size[0] = img.width;
112 size[1] = img.height;
113 } else {
114 if(img.width != size[0] || img.height != size[1]) {
115 fprintf(stderr, "slice %d \"%s\" size mismatch (%dx%d, %dx%d expected)\n",
116 (int)slices.size(), line, img.width, img.height, size[0], size[1]);
117 slices.clear();
118 return false;
119 }
120 }
122 slices.push_back(img);
123 img.pixels = 0; // otherwise the destructor will free it
124 }
126 size[2] = slices.size();
127 fclose(fp);
128 return true;
129 }
131 int VoxelVolume::num_samples(int dim) const
132 {
133 return size[dim];
134 }
136 static inline int clamp(int x, int low, int high)
137 {
138 return x < low ? low : (x > high ? high : x);
139 }
141 static inline float lookup(int x0, int y0, int x1, int y1, float tx, float ty,
142 float *pixels, int width, int height)
143 {
144 float v00, v01, v10, v11;
146 v00 = pixels[y0 * width + x0];
147 v01 = pixels[y1 * width + x0];
148 v10 = pixels[y0 * width + x1];
149 v11 = pixels[y1 * width + x1];
151 float v0 = v00 + (v01 - v00) * ty;
152 float v1 = v10 + (v11 - v10) * ty;
154 return v0 + (v1 - v0) * tx;
155 }
157 float VoxelVolume::valuef(float x, float y, float z) const
158 {
159 if(slices.empty()) {
160 return 0.0f;
161 }
163 float floor_x = floor(x);
164 float ceil_x = ceil(x);
165 float tx = x - floor_x;
167 float floor_y = floor(y);
168 float ceil_y = ceil(y);
169 float ty = y - floor_y;
171 float floor_z = floor(z);
172 float ceil_z = ceil(z);
173 float tz = z - floor_z;
175 int x0 = clamp(floor_x, 0, size[0] - 1);
176 int x1 = clamp(ceil_x, 0, size[0] - 1);
178 int y0 = clamp(floor_y, 0, size[1] - 1);
179 int y1 = clamp(ceil_y, 0, size[1] - 1);
181 int s0 = clamp(floor_z, 0, size[2] - 1);
182 int s1 = clamp(ceil_z, 0, size[2] - 1);
184 float val_s0 = lookup(x0, y0, x1, y1, tx, ty, slices[s0].pixels, size[0], size[1]);
185 float val_s1 = lookup(x0, y0, x1, y1, tx, ty, slices[s1].pixels, size[0], size[1]);
187 return val_s0 + (val_s1 - val_s0) * tz;
188 }
190 float VoxelVolume::valuei(int x, int y, int z) const
191 {
192 x = clamp(x, 0, size[0] - 1);
193 y = clamp(y, 0, size[1] - 1);
194 z = clamp(z, 0, size[2] - 1);
196 return slices[z].pixels[y * size[0] + x];
197 }
199 static char *strip_space(char *s)
200 {
201 while(*s && isspace(*s)) s++;
202 if(!*s) return 0;
204 char *end = s + strlen(s) - 1;
205 while(end > s && isspace(*end)) end--;
206 end[1] = 0;
208 return s;
209 }