Ignore:
Timestamp:
Mar 7, 2012, 2:01:55 PM (13 years ago)
Author:
ldelgass
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/DataLoader.cpp

    r2798 r2827  
    55#include <stdlib.h>
    66#include <math.h>
    7 #include <Trace.h>
     7
     8#include "Trace.h"
    89
    910inline void endian_swap(unsigned int& x)
     
    1516}
    1617
    17 
    1818char buff[255];
    19 void getLine(FILE*& fp)
     19void getLine(FILE *fp)
    2020{
    2121    char ch;
     
    3030}
    3131
    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;
     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;
    5757    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);
     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);
    7474   
    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;
     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;
    195182    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);
     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);
    209196   
    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 
     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 TracChangeset for help on using the changeset viewer.