glviewvol

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