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

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