source: branches/nanovis2/packages/vizservers/nanovis/DataLoader.cpp @ 3351

Last change on this file since 3351 was 2862, checked in by ldelgass, 13 years ago

Move extern declarations for DataLoader? functions to a header, remove DataLoader?
from default objects built, as only ParticleSystem? uses it and ParticleSystem?
is not built by default.

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