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

Last change on this file since 2831 was 2827, checked in by ldelgass, 12 years ago

Move Vector3/4Array typdefs to the Vector3/4.h headers and remove TypeDefs?.h.
Also misc. cleanups

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