glviewvol

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