rev |
line source |
elene@6
|
1 #include <GL/glew.h>
|
elene@6
|
2
|
elene@6
|
3 #include <assert.h>
|
elene@6
|
4
|
elene@5
|
5 #include <ctype.h>
|
elene@5
|
6 #include <errno.h>
|
elene@4
|
7 #include <stdio.h>
|
elene@5
|
8 #include <string.h>
|
elene@5
|
9
|
elene@5
|
10 #include <string>
|
elene@5
|
11 #include <vector>
|
elene@4
|
12
|
elene@9
|
13 #include <metasurf.h>
|
elene@9
|
14
|
elene@9
|
15 #include "mesh.h"
|
elene@3
|
16 #include "volume.h"
|
elene@4
|
17
|
elene@5
|
18 static char *strip_whitespaces(char *buf);
|
elene@5
|
19
|
elene@4
|
20 Volume::Volume()
|
elene@4
|
21 {
|
elene@4
|
22 width = height = 0;
|
elene@5
|
23 zaspect = 1;
|
elene@6
|
24 vol_tex_valid = false;
|
elene@6
|
25
|
elene@6
|
26 glGenTextures(1, &vol_tex);
|
elene@4
|
27 }
|
elene@4
|
28
|
elene@6
|
29 Volume::~Volume()
|
elene@6
|
30 {
|
elene@6
|
31 glDeleteTextures(1, &vol_tex);
|
elene@6
|
32 }
|
elene@6
|
33
|
elene@6
|
34 void Volume::create_vol_tex() const
|
elene@6
|
35 {
|
elene@6
|
36 if(slices.empty())
|
elene@6
|
37 return;
|
elene@6
|
38
|
elene@6
|
39 glBindTexture(GL_TEXTURE_3D, vol_tex);
|
elene@6
|
40 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
elene@6
|
41 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
elene@6
|
42 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
elene@6
|
43 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
elene@6
|
44 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
elene@6
|
45
|
elene@6
|
46 assert(glGetError() == GL_NO_ERROR);
|
elene@6
|
47 glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height,
|
elene@6
|
48 slices.size(), 0, GL_LUMINANCE, GL_FLOAT, 0);
|
elene@6
|
49 assert(glGetError() == GL_NO_ERROR);
|
elene@6
|
50
|
elene@6
|
51 for(size_t i=0; i<slices.size(); i++) {
|
elene@6
|
52 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, i, width, height,
|
elene@6
|
53 1, GL_LUMINANCE, GL_FLOAT, slices[i].get_pixels());
|
elene@6
|
54 }
|
elene@6
|
55 }
|
elene@6
|
56
|
elene@6
|
57 bool Volume::load(const char *fname)
|
elene@4
|
58 {
|
elene@5
|
59 FILE *fp = fopen(fname, "r");
|
elene@5
|
60 if(!fp) {
|
elene@5
|
61 fprintf(stderr, "Failed to open file: %s: %s\n", fname, strerror(errno));
|
elene@5
|
62 return false;
|
elene@5
|
63 }
|
elene@5
|
64
|
elene@5
|
65 char buf[512];
|
elene@5
|
66 if(!fgets(buf, sizeof buf, fp)) {
|
elene@5
|
67 fprintf(stderr, "Empty file: %s.\n", fname);
|
elene@5
|
68 return false;
|
elene@5
|
69 }
|
elene@5
|
70 if(strstr(buf, "VOLUME") != buf) {
|
elene@5
|
71 fprintf(stderr, "Invalid volume file format: %s\n", fname);
|
elene@5
|
72 return false;
|
elene@5
|
73 }
|
elene@5
|
74
|
elene@5
|
75 bool reading_slices = false;
|
elene@5
|
76 std::vector<std::string> img_fnames;
|
elene@5
|
77
|
elene@5
|
78 while(fgets(buf, sizeof buf, fp)) {
|
elene@5
|
79 char *line = strip_whitespaces(buf);
|
elene@5
|
80 if(!line || *line == 0 || *line == '#')
|
elene@5
|
81 continue;
|
elene@5
|
82
|
elene@5
|
83 if(reading_slices == false) {
|
elene@5
|
84 if(strcmp(line, "SLICES") == 0)
|
elene@5
|
85 reading_slices = true;
|
elene@5
|
86
|
elene@5
|
87 sscanf(line, "zaspect = %f", &zaspect);
|
elene@5
|
88 }
|
elene@5
|
89 else {
|
elene@5
|
90 img_fnames.push_back(line);
|
elene@5
|
91 }
|
elene@5
|
92 }
|
elene@5
|
93
|
elene@5
|
94 fclose(fp);
|
elene@5
|
95
|
elene@5
|
96 for(size_t i=0; i<img_fnames.size(); i++) {
|
elene@5
|
97 Image img;
|
elene@5
|
98 if(img.load(img_fnames[i].c_str())) {
|
elene@5
|
99 push_slice(std::move(img));
|
elene@5
|
100 }
|
elene@5
|
101 else {
|
elene@5
|
102 fprintf(stderr, "Failed to load slice: %s\n", img_fnames[i].c_str());
|
elene@5
|
103 }
|
elene@5
|
104 }
|
elene@5
|
105
|
elene@4
|
106 return true;
|
elene@4
|
107 }
|
elene@4
|
108
|
elene@4
|
109 bool Volume::push_slice(Image &&slice)
|
elene@4
|
110 {
|
elene@4
|
111 if(slices.empty()) {
|
elene@4
|
112 width = slice.get_width();
|
elene@4
|
113 height = slice.get_height();
|
elene@4
|
114 }
|
elene@4
|
115 else {
|
elene@4
|
116 if(width != slice.get_width() || height != slice.get_height()) {
|
elene@4
|
117 fprintf(stderr, "failed to load slice no: %d\n", (int)slices.size() + 1);
|
elene@4
|
118 return false;
|
elene@4
|
119 }
|
elene@4
|
120 }
|
elene@4
|
121
|
elene@4
|
122 slices.push_back(slice);
|
elene@4
|
123 return true;
|
elene@4
|
124 }
|
elene@5
|
125
|
elene@6
|
126 const Image *Volume::get_slice(int num_slice) const
|
elene@6
|
127 {
|
elene@6
|
128 if(num_slice >= (int)slices.size() || num_slice < 0)
|
elene@6
|
129 return 0;
|
elene@6
|
130
|
elene@6
|
131 return &slices[num_slice];
|
elene@6
|
132 }
|
elene@6
|
133
|
elene@6
|
134 const Image *Volume::get_slice_by_z(float z) const
|
elene@6
|
135 {
|
elene@6
|
136 int idx = get_slice_idx_by_z(z);
|
elene@6
|
137 return get_slice(idx);
|
elene@6
|
138 }
|
elene@6
|
139
|
elene@6
|
140 int Volume::get_slice_count() const
|
elene@6
|
141 {
|
elene@6
|
142 return (int)slices.size();
|
elene@6
|
143 }
|
elene@6
|
144
|
elene@6
|
145 int Volume::get_slice_idx_by_z(float z) const
|
elene@6
|
146 {
|
elene@6
|
147 int last_slice_idx = (int)slices.size() - 1;
|
elene@6
|
148 int idx = z * last_slice_idx;
|
elene@6
|
149 if(idx < 0) {
|
elene@6
|
150 idx = 0;
|
elene@6
|
151 }
|
elene@6
|
152 else if(idx > last_slice_idx) {
|
elene@6
|
153 idx = last_slice_idx;
|
elene@6
|
154 }
|
elene@6
|
155
|
elene@6
|
156 return idx;
|
elene@6
|
157 }
|
elene@6
|
158
|
elene@6
|
159 unsigned int Volume::get_texture() const
|
elene@6
|
160 {
|
elene@6
|
161 if(!vol_tex_valid) {
|
elene@6
|
162 create_vol_tex();
|
elene@6
|
163 vol_tex_valid = true;
|
elene@6
|
164 }
|
elene@6
|
165
|
elene@6
|
166 return vol_tex;
|
elene@6
|
167 }
|
elene@6
|
168
|
elene@9
|
169 static Mesh *cur_mesh;
|
elene@9
|
170 static Volume *cur_vol;
|
elene@9
|
171
|
elene@9
|
172 static float cb_eval(float x, float y, float z)
|
elene@9
|
173 {
|
elene@9
|
174 const Image *img = cur_vol->get_slice_by_z((z + 1) / 2.0);
|
elene@9
|
175 const float *pixels = img->get_pixels();
|
elene@9
|
176
|
elene@9
|
177 int px = (x + 1) / 2.0 * img->get_width();
|
elene@9
|
178 int py = (y + 1) / 2.0 * img->get_height();
|
elene@9
|
179
|
elene@9
|
180 return pixels[px + img->get_width() * py];
|
elene@9
|
181 }
|
elene@9
|
182
|
elene@11
|
183 static void cb_vertex(float x, float y, float z)
|
elene@11
|
184 {
|
elene@11
|
185 float dx = 1.0 / cur_vol->get_slice(0)->get_width();
|
elene@11
|
186 float dy = 1.0 / cur_vol->get_slice(0)->get_height();
|
elene@11
|
187 float dz = 1.0 / cur_vol->get_slice_count();
|
elene@11
|
188 float dfdx = cb_eval(x - dx, y, z) - cb_eval(x + dx, y, z);
|
elene@11
|
189 float dfdy = cb_eval(x, y - dy, z) - cb_eval(x, y + dy, z);
|
elene@11
|
190 float dfdz = cb_eval(x, y, z - dz) - cb_eval(x, y, z + dz);
|
elene@11
|
191
|
elene@11
|
192 cur_mesh->add_normal(Vector3(dfdx, dfdy, dfdz));
|
elene@11
|
193 cur_mesh->add_vertex(Vector3(x, y, z));
|
elene@11
|
194 }
|
elene@11
|
195
|
elene@9
|
196 void Volume::create_mesh(Mesh *mesh, float threshold)
|
elene@9
|
197 {
|
elene@9
|
198 cur_mesh = mesh;
|
elene@9
|
199 cur_vol = this;
|
elene@9
|
200
|
elene@9
|
201 metasurface *ms = msurf_create();
|
elene@9
|
202 msurf_threshold(ms, threshold);
|
elene@9
|
203 msurf_resolution(ms, width, height, slices.size());
|
elene@11
|
204 msurf_vertex_func(ms, cb_vertex);
|
elene@9
|
205 msurf_eval_func(ms, cb_eval);
|
elene@9
|
206 msurf_polygonize(ms);
|
elene@9
|
207 msurf_free(ms);
|
elene@9
|
208 }
|
elene@9
|
209
|
elene@8
|
210 void Volume::draw() const
|
elene@5
|
211 {
|
elene@5
|
212 }
|
elene@5
|
213
|
elene@5
|
214 static char *strip_whitespaces(char *buf)
|
elene@5
|
215 {
|
elene@5
|
216 while(*buf && isspace(*buf))
|
elene@5
|
217 buf++;
|
elene@5
|
218
|
elene@5
|
219 if(!*buf)
|
elene@5
|
220 return 0;
|
elene@5
|
221
|
elene@5
|
222 char *end = buf + strlen(buf) - 1;
|
elene@5
|
223 while(end > buf && isspace(*end))
|
elene@5
|
224 end--;
|
elene@5
|
225 end[1] = 0;
|
elene@5
|
226 return buf;
|
elene@5
|
227 }
|