glviewvol

view src/volume.cc @ 3:32c4a7160350

den kanei kryo stin ellada
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Dec 2014 21:48:15 +0200
parents 701507c8238f
children 04330eb80b36
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 VoxelVolume::~VoxelVolume()
83 {
84 for(size_t i=0; i<slices.size(); i++) {
85 slices[i].destroy();
86 }
87 }
89 bool VoxelVolume::load(const char *fname)
90 {
91 if(!fname) return false;
93 char *prefix = (char*)alloca(strlen(fname) + 1);
94 strcpy(prefix, fname);
95 char *slash = strrchr(prefix, '/');
96 if(slash) {
97 *slash = 0;
98 } else {
99 prefix = 0;
100 }
102 FILE *fp = fopen(fname, "r");
103 if(!fp) {
104 fprintf(stderr, "failed to open file: %s: %s\n", fname, strerror(errno));
105 return false;
106 }
108 char buf[256], path[300];
109 while(fgets(buf, sizeof buf, fp)) {
110 char *line = strip_space(buf);
111 sprintf(path, "%s/%s", prefix, line);
113 Image img;
114 if(!img.load(path)) {
115 slices.clear();
116 return false;
117 }
119 if(slices.empty()) {
120 size[0] = img.width;
121 size[1] = img.height;
122 } else {
123 if(img.width != size[0] || img.height != size[1]) {
124 fprintf(stderr, "slice %d \"%s\" size mismatch (%dx%d, %dx%d expected)\n",
125 (int)slices.size(), line, img.width, img.height, size[0], size[1]);
126 slices.clear();
127 return false;
128 }
129 }
131 slices.push_back(img);
132 }
134 size[2] = slices.size();
135 fclose(fp);
136 return true;
137 }
139 int VoxelVolume::num_samples(int dim) const
140 {
141 return size[dim];
142 }
144 static inline int clamp(int x, int low, int high)
145 {
146 return x < low ? low : (x > high ? high : x);
147 }
149 static inline float lookup(int x0, int y0, int x1, int y1, float tx, float ty,
150 float *pixels, int width, int height)
151 {
152 float v00, v01, v10, v11;
154 v00 = pixels[y0 * width + x0];
155 v01 = pixels[y1 * width + x0];
156 v10 = pixels[y0 * width + x1];
157 v11 = pixels[y1 * width + x1];
159 float v0 = v00 + (v01 - v00) * ty;
160 float v1 = v10 + (v11 - v10) * ty;
162 return v0 + (v1 - v0) * tx;
163 }
165 float VoxelVolume::valuef(float x, float y, float z) const
166 {
167 if(slices.empty()) {
168 return 0.0f;
169 }
171 float floor_x = floor(x);
172 float ceil_x = ceil(x);
173 float tx = x - floor_x;
175 float floor_y = floor(y);
176 float ceil_y = ceil(y);
177 float ty = y - floor_y;
179 float floor_z = floor(z);
180 float ceil_z = ceil(z);
181 float tz = z - floor_z;
183 int x0 = clamp(floor_x, 0, size[0] - 1);
184 int x1 = clamp(ceil_x, 0, size[0] - 1);
186 int y0 = clamp(floor_y, 0, size[1] - 1);
187 int y1 = clamp(ceil_y, 0, size[1] - 1);
189 int s0 = clamp(floor_z, 0, size[2] - 1);
190 int s1 = clamp(ceil_z, 0, size[2] - 1);
192 float val_s0 = lookup(x0, y0, x1, y1, tx, ty, slices[s0].pixels, size[0], size[1]);
193 float val_s1 = lookup(x0, y0, x1, y1, tx, ty, slices[s1].pixels, size[0], size[1]);
195 return val_s0 + (val_s1 - val_s0) * tz;
196 }
198 float VoxelVolume::valuei(int x, int y, int z) const
199 {
200 x = clamp(x, 0, size[0] - 1);
201 y = clamp(y, 0, size[1] - 1);
202 z = clamp(z, 0, size[2] - 1);
204 return slices[z].pixels[y * size[0] + x];
205 }
207 static char *strip_space(char *s)
208 {
209 while(*s && isspace(*s)) s++;
210 if(!*s) return 0;
212 char *end = s + strlen(s) - 1;
213 while(end > s && isspace(*end)) end--;
214 end[1] = 0;
216 return s;
217 }