source: trunk/packages/vizservers/nanovis/DataLoader.cpp @ 2096

Last change on this file since 2096 was 2096, checked in by ldelgass, 14 years ago

Normalize line endings, set eol-style to native on *.cpp, *.h files

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