1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | #include <stdio.h> |
---|
3 | #include <string> |
---|
4 | #include <memory.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <math.h> |
---|
7 | #include <Trace.h> |
---|
8 | |
---|
9 | inline void endian_swap(unsigned int& x) |
---|
10 | { |
---|
11 | x = (x>>24) | |
---|
12 | ((x<<8) & 0x00FF0000) | |
---|
13 | ((x>>8) & 0x0000FF00) | |
---|
14 | (x<<24); |
---|
15 | } |
---|
16 | |
---|
17 | |
---|
18 | char buff[255]; |
---|
19 | void getLine(FILE*& fp) |
---|
20 | { |
---|
21 | char ch; |
---|
22 | int index = 0; |
---|
23 | do { |
---|
24 | ch = fgetc(fp); |
---|
25 | if (ch == '\n') break; |
---|
26 | buff[index++] = ch; |
---|
27 | } while (!feof(fp)); |
---|
28 | |
---|
29 | buff[index] = '\0'; |
---|
30 | } |
---|
31 | |
---|
32 | void* LoadDx(const char* fname, int& width, int& height, int&depth, float& min, float& max, float& nonzero_min, |
---|
33 | float& axisScaleX, float& axisScaleY, float& axisScaleZ) |
---|
34 | { |
---|
35 | std::string path = fname; |
---|
36 | |
---|
37 | if (path.size() == 0) |
---|
38 | { |
---|
39 | ERROR("file not found[%s]\n", path.c_str()); |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | |
---|
43 | FILE* fp = fopen(path.c_str(), "rb"); |
---|
44 | if (fp == NULL) |
---|
45 | { |
---|
46 | ERROR("file not found %s\n", path.c_str()); |
---|
47 | return NULL; |
---|
48 | } |
---|
49 | |
---|
50 | int index; |
---|
51 | do { |
---|
52 | getLine(fp); |
---|
53 | } while (memcmp(buff, "object", 6)); |
---|
54 | |
---|
55 | float origin_x, origin_y, origin_z; |
---|
56 | double delta_x, delta_y, delta_z; |
---|
57 | double temp; |
---|
58 | char str[5][20]; |
---|
59 | |
---|
60 | //object 1 class gridpositions counts 5 5 5 |
---|
61 | sscanf(buff, "%s%s%s%s%s%d%d%d", str[0], str[1], str[2], str[3], str[4], &width, &depth, &height); |
---|
62 | |
---|
63 | //origin 0 0 0 |
---|
64 | getLine(fp); |
---|
65 | sscanf(buff, "%s%f%f%f", str[0], &(origin_x), &(origin_y), &(origin_z)); |
---|
66 | |
---|
67 | // delta 0.5431 0 0 |
---|
68 | getLine(fp); |
---|
69 | sscanf(buff, "%s%lf%lf%lf", str[0], &(delta_x), &temp, &temp); |
---|
70 | |
---|
71 | // delta 0 0.5431 0 |
---|
72 | getLine(fp); |
---|
73 | sscanf(buff, "%s%lf%lf%lf", str[0], &temp, &(delta_y), &temp); |
---|
74 | |
---|
75 | // delta 0 0 0.5431 |
---|
76 | getLine(fp); |
---|
77 | sscanf(buff, "%s%lf%lf%lf", str[0], &temp, &temp, &(delta_z)); |
---|
78 | |
---|
79 | getLine(fp); |
---|
80 | getLine(fp); |
---|
81 | getLine(fp); |
---|
82 | getLine(fp); |
---|
83 | |
---|
84 | min = nonzero_min = 1e27f; |
---|
85 | max = -1e27f; |
---|
86 | |
---|
87 | float* data = 0; |
---|
88 | int d, h, w; |
---|
89 | float scalar; |
---|
90 | index = 0; |
---|
91 | |
---|
92 | if (width < height) |
---|
93 | { |
---|
94 | int size = width * height * depth; |
---|
95 | data = (float*) malloc(size * sizeof(float)); |
---|
96 | memset(data, 0, sizeof(float) * size); |
---|
97 | |
---|
98 | |
---|
99 | for (w = 0; w < width; ++w) // x |
---|
100 | for (d = 0; d < depth; ++d) //z |
---|
101 | { |
---|
102 | for (h = 0; h < width; ++h) // y |
---|
103 | { |
---|
104 | scalar = atof(fgets(buff, 255, fp)); |
---|
105 | data[h * width + d * width * width + w] = scalar; |
---|
106 | if (scalar < min) min = scalar; |
---|
107 | if (scalar != 0.0f && nonzero_min > scalar) nonzero_min = scalar; |
---|
108 | if (scalar > max) max = scalar; |
---|
109 | } |
---|
110 | for (;h < height; ++h) |
---|
111 | { |
---|
112 | scalar = atof(fgets(buff, 255, fp)); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | height = width; |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | } |
---|
122 | else if (width > height) |
---|
123 | { |
---|
124 | int size = width * width * width; |
---|
125 | data = (float*) malloc(size * sizeof(float)); |
---|
126 | memset(data, 0, sizeof(float) * size); |
---|
127 | for (w = 0; w < width; ++w) // x |
---|
128 | for (d = 0; d < depth; ++d) //z |
---|
129 | for (h = 0; h < height; ++h) // y |
---|
130 | { |
---|
131 | scalar = atof(fgets(buff, 255, fp)); |
---|
132 | data[h * width + d * width * width + w] = scalar; |
---|
133 | if (scalar < min) min = scalar; |
---|
134 | if (scalar != 0.0f && nonzero_min > scalar) nonzero_min = scalar; |
---|
135 | if (scalar > max) max = scalar; |
---|
136 | } |
---|
137 | |
---|
138 | height = width; |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | } |
---|
144 | else |
---|
145 | { |
---|
146 | int size = width * height * depth; |
---|
147 | data = (float*) malloc(size * sizeof(float)); |
---|
148 | |
---|
149 | for (w = 0; w < width; ++w) // x |
---|
150 | for (d = 0; d < depth; ++d) //z |
---|
151 | for (h = 0; h < height; ++h) // y |
---|
152 | { |
---|
153 | scalar = atof(fgets(buff, 255, fp)); |
---|
154 | data[h * width + d * width * height + w] = scalar; |
---|
155 | if (scalar < min) min = scalar; |
---|
156 | if (scalar != 0.0f && nonzero_min > scalar) nonzero_min = scalar; |
---|
157 | if (scalar > max) max = scalar; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | axisScaleX = origin_x + width * delta_x; |
---|
162 | axisScaleY = origin_y + height * delta_y; |
---|
163 | axisScaleZ = origin_z + depth * delta_z; |
---|
164 | |
---|
165 | fclose(fp); |
---|
166 | |
---|
167 | return data; |
---|
168 | } |
---|
169 | |
---|
170 | void* LoadFlowDx(const char* fname, int& width, int& height, int&depth, float& min, float& max, float& nonzero_min, |
---|
171 | float& axisScaleX, float& axisScaleY, float& axisScaleZ) |
---|
172 | { |
---|
173 | std::string path = fname; |
---|
174 | |
---|
175 | if (path.size() == 0) |
---|
176 | { |
---|
177 | ERROR("file not found[%s]\n", path.c_str()); |
---|
178 | return NULL; |
---|
179 | } |
---|
180 | |
---|
181 | FILE* fp = fopen(path.c_str(), "rb"); |
---|
182 | if (fp == NULL) |
---|
183 | { |
---|
184 | ERROR("file not found %s\n", path.c_str()); |
---|
185 | return NULL; |
---|
186 | } |
---|
187 | |
---|
188 | int index; |
---|
189 | do { |
---|
190 | getLine(fp); |
---|
191 | } while (memcmp(buff, "object", 6)); |
---|
192 | |
---|
193 | double origin_x, origin_y, origin_z; |
---|
194 | double delta_x, delta_y, delta_z; |
---|
195 | double temp; |
---|
196 | char str[5][20]; |
---|
197 | |
---|
198 | //object 1 class gridpositions counts 5 5 5 |
---|
199 | sscanf(buff, "%s%s%s%s%s%d%d%d", str[0], str[1], str[2], str[3], str[4], &width, &height, &depth); |
---|
200 | |
---|
201 | getLine(fp); |
---|
202 | sscanf(buff, "%s%lg%lg%lg", str[0], &(origin_x), &(origin_y), &(origin_z)); |
---|
203 | |
---|
204 | getLine(fp); |
---|
205 | sscanf(buff, "%s%lg%lg%lg", str[0], &(delta_x), &temp, &temp); |
---|
206 | |
---|
207 | getLine(fp); |
---|
208 | sscanf(buff, "%s%lg%lg%lg", str[0], &temp, &(delta_y), &temp); |
---|
209 | |
---|
210 | getLine(fp); |
---|
211 | sscanf(buff, "%s%lf%lf%lf", str[0], &temp, &temp, &(delta_z)); |
---|
212 | |
---|
213 | getLine(fp); |
---|
214 | getLine(fp); |
---|
215 | |
---|
216 | min = nonzero_min = 1e27f; |
---|
217 | max = -1e27f; |
---|
218 | |
---|
219 | float* data = 0; |
---|
220 | int d, h, w; |
---|
221 | index = 0; |
---|
222 | |
---|
223 | |
---|
224 | int size = width * height * depth; |
---|
225 | data = (float*) malloc(size * sizeof(float) * 4); |
---|
226 | float* ptr = data; |
---|
227 | double x, y, z, length; |
---|
228 | char* ret = 0; |
---|
229 | |
---|
230 | for (w = 0; w < width; ++w) // x |
---|
231 | for (h = 0; h < height; ++h) // y |
---|
232 | for (d = 0; d < depth; ++d) //z |
---|
233 | { |
---|
234 | ret = fgets(buff, 255, fp); |
---|
235 | sscanf(buff, "%lg%lg%lg", &x, &y, &z); |
---|
236 | ptr = data + (d * width * height + h * width + w) * 4; |
---|
237 | length = sqrt(x*x+y*y+z*z); |
---|
238 | *ptr = length; ptr++; |
---|
239 | *ptr = x; ptr++; |
---|
240 | *ptr = y; ptr++; |
---|
241 | *ptr = z; |
---|
242 | |
---|
243 | if (length < min) min = length; |
---|
244 | if (length != 0.0f && nonzero_min > length) nonzero_min = length; |
---|
245 | if (length > max) |
---|
246 | { |
---|
247 | //TRACE("max %lf %lf %fl\n", x, y, z); |
---|
248 | max = length; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | ptr = data; |
---|
253 | for (int i = 0; i < size; ++i) |
---|
254 | { |
---|
255 | *ptr = *ptr / max; ++ptr; |
---|
256 | *ptr = (*ptr / (2.0*max)) + 0.5; ++ptr; |
---|
257 | *ptr = (*ptr / (2.0 * max)) + 0.5; ++ptr; |
---|
258 | *ptr = (*ptr / (2.0 * max)) + 0.5; ++ptr; |
---|
259 | } |
---|
260 | axisScaleX = width * delta_x; |
---|
261 | axisScaleY = height * delta_y; |
---|
262 | axisScaleZ = depth * delta_z; |
---|
263 | |
---|
264 | fclose(fp); |
---|
265 | |
---|
266 | TRACE("width %d, height %d, depth %d, min %f, max %f, scaleX %f, scaleY %f, scaleZ %f\n", |
---|
267 | width, height, depth, min, max, axisScaleX, axisScaleY, axisScaleZ); |
---|
268 | return data; |
---|
269 | } |
---|
270 | |
---|
271 | void* LoadProcessedFlowRaw(const char* fname, int width, int height, int depth, float min, float max, float& axisScaleX, float& axisScaleY, float& axisScaleZ) |
---|
272 | { |
---|
273 | std::string path = fname; |
---|
274 | |
---|
275 | if (fname == 0) |
---|
276 | { |
---|
277 | ERROR("file name is null\n"); |
---|
278 | return NULL; |
---|
279 | } |
---|
280 | |
---|
281 | FILE* fp = fopen(fname, "rb"); |
---|
282 | if (fp == NULL) |
---|
283 | { |
---|
284 | ERROR("file not found %s\n", fname); |
---|
285 | return NULL; |
---|
286 | } |
---|
287 | |
---|
288 | int size = width * height * depth; |
---|
289 | float* srcdata = (float*) malloc(size * sizeof(float) * 4); |
---|
290 | fread(srcdata, size * 4 * sizeof(float), 1, fp); |
---|
291 | fclose(fp); |
---|
292 | |
---|
293 | axisScaleX = width; |
---|
294 | axisScaleY = height; |
---|
295 | axisScaleZ = depth; |
---|
296 | |
---|
297 | return srcdata; |
---|
298 | } |
---|
299 | |
---|
300 | void LoadProcessedFlowRaw(const char* fname, int width, int height, int depth, float* data) |
---|
301 | { |
---|
302 | std::string path = fname; |
---|
303 | |
---|
304 | if (fname == 0) |
---|
305 | { |
---|
306 | ERROR("file name is null\n"); |
---|
307 | return; |
---|
308 | } |
---|
309 | |
---|
310 | FILE* fp = fopen(fname, "rb"); |
---|
311 | if (fp == NULL) |
---|
312 | { |
---|
313 | ERROR("file not found %s\n", fname); |
---|
314 | return; |
---|
315 | } |
---|
316 | |
---|
317 | int size = width * height * depth; |
---|
318 | fread(data, size * 4 * sizeof(float), 1, fp); |
---|
319 | fclose(fp); |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | void* LoadFlowRaw(const char* fname, int width, int height, int depth, float& min, float& max, float& nonzero_min, |
---|
324 | float& axisScaleX, float& axisScaleY, float& axisScaleZ, int skipByte, bool bigEndian) |
---|
325 | { |
---|
326 | std::string path = fname; |
---|
327 | |
---|
328 | if (path.size() == 0) |
---|
329 | { |
---|
330 | ERROR("file not found[%s]\n", path.c_str()); |
---|
331 | return NULL; |
---|
332 | } |
---|
333 | |
---|
334 | FILE* fp = fopen(path.c_str(), "rb"); |
---|
335 | if (fp == NULL) |
---|
336 | { |
---|
337 | ERROR("file not found %s\n", path.c_str()); |
---|
338 | return NULL; |
---|
339 | } |
---|
340 | |
---|
341 | |
---|
342 | |
---|
343 | min = nonzero_min = 1e27f; |
---|
344 | max = -1e27f; |
---|
345 | |
---|
346 | float* srcdata = 0; |
---|
347 | float* data = 0; |
---|
348 | int d, h, w; |
---|
349 | int index = 0; |
---|
350 | |
---|
351 | int size = width * height * depth; |
---|
352 | srcdata = (float*) malloc(size * sizeof(float) * 3); |
---|
353 | memset(srcdata, 0, size * sizeof(float) * 3); |
---|
354 | if (skipByte != 0) fread(srcdata, skipByte, 1, fp); |
---|
355 | fread(srcdata, size * 3 * sizeof(float), 1, fp); |
---|
356 | fclose(fp); |
---|
357 | |
---|
358 | if (bigEndian) { |
---|
359 | unsigned int* dd = (unsigned int*) srcdata; |
---|
360 | for (int i = 0; i < size * 3; ++i) |
---|
361 | { |
---|
362 | endian_swap(dd[i]); |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | data = (float*) malloc(size * sizeof(float) * 4); |
---|
368 | memset(data, 0, size * sizeof(float) * 4); |
---|
369 | |
---|
370 | float* ptr = data; |
---|
371 | double x, y, z, length; |
---|
372 | |
---|
373 | for (d = 0; d < depth; ++d) //z |
---|
374 | for (h = 0; h < height; ++h) // y |
---|
375 | for (w = 0; w < width; ++w, index +=3) // x |
---|
376 | { |
---|
377 | x = srcdata[index]; |
---|
378 | y = srcdata[index + 1]; |
---|
379 | z = srcdata[index + 2]; |
---|
380 | ptr = data + (d * width * height + h * width + w) * 4; |
---|
381 | length = sqrt(x*x+y*y+z*z); |
---|
382 | *ptr = length; ptr++; |
---|
383 | *ptr = x; ptr++; |
---|
384 | *ptr = y; ptr++; |
---|
385 | *ptr = z; |
---|
386 | |
---|
387 | if (length < min) min = length; |
---|
388 | if (length != 0.0f && nonzero_min > length) nonzero_min = length; |
---|
389 | if (length > max) |
---|
390 | { |
---|
391 | TRACE("max %lf %lf %fl\n", x, y, z); |
---|
392 | max = length; |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | ptr = data; |
---|
397 | for (int i = 0; i < size; ++i) |
---|
398 | { |
---|
399 | *ptr = *ptr / max; ++ptr; |
---|
400 | *ptr = (*ptr / (2.0*max)) + 0.5; ++ptr; |
---|
401 | *ptr = (*ptr / (2.0 * max)) + 0.5; ++ptr; |
---|
402 | *ptr = (*ptr / (2.0 * max)) + 0.5; ++ptr; |
---|
403 | } |
---|
404 | axisScaleX = width; |
---|
405 | axisScaleY = height; |
---|
406 | axisScaleZ = depth; |
---|
407 | |
---|
408 | fclose(fp); |
---|
409 | |
---|
410 | TRACE("width %d, height %d, depth %d, min %f, max %f, scaleX %f, scaleY %f, scaleZ %f\n", |
---|
411 | width, height, depth, min, max, axisScaleX, axisScaleY, axisScaleZ); |
---|
412 | return data; |
---|
413 | } |
---|
414 | |
---|
415 | |
---|