source: nanovis/branches/1.2/ZincBlendeReconstructor.cpp @ 5699

Last change on this file since 5699 was 5406, checked in by ldelgass, 9 years ago

backport from trunk: remove unused ctor params

  • Property svn:eol-style set to native
File size: 15.7 KB
RevLine 
[2798]1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
[3502]2/*
3 * Copyright (c) 2004-2013  HUBzero Foundation, LLC
[3611]4 *
5 * Authors:
6 *   Insoo Woo <iwoo@purdue.edu>
[3502]7 */
[4889]8#include <cstdio>
9#include <cstring>
10#include <cstdlib>
11#include <fstream>
[2844]12
[3492]13#include <vrmath/Vector3f.h>
14
[3611]15#include "ZincBlendeReconstructor.h"
[601]16#include "ZincBlendeVolume.h"
[3492]17#include "Trace.h"
[601]18
[3611]19using namespace nv;
[3492]20using namespace vrmath;
21
[3611]22ZincBlendeReconstructor *ZincBlendeReconstructor::_instance = NULL;
[806]23
[3611]24ZincBlendeReconstructor::ZincBlendeReconstructor()
[601]25{
26}
27
[3611]28ZincBlendeReconstructor::~ZincBlendeReconstructor()
[601]29{
30}
31
[3611]32ZincBlendeReconstructor *ZincBlendeReconstructor::getInstance()
[601]33{
[2822]34    if (_instance == NULL) {
[3611]35        return (_instance = new ZincBlendeReconstructor());
[601]36    }
37
38    return _instance;
39}
40
[3611]41ZincBlendeVolume *ZincBlendeReconstructor::loadFromFile(const char *fileName)
[601]42{
[800]43    std::ifstream stream;
44    stream.open(fileName, std::ios::binary);
[617]45
[2822]46    ZincBlendeVolume *volume = loadFromStream(stream);
[617]47
48    stream.close();
49
50    return volume;
51}
52
[3611]53ZincBlendeVolume *ZincBlendeReconstructor::loadFromStream(std::istream& stream)
[617]54{
[2822]55    ZincBlendeVolume *volume = NULL;
[3492]56    Vector3f origin, delta;
[617]57    int width = 0, height = 0, depth = 0;
[2822]58    void *data = NULL;
[800]59    int version = 1;
[617]60
61    char str[5][20];
62    do {
63        getLine(stream);
[4904]64        if (_buff[0] == '#') {
[800]65            continue;
[4904]66        } else if (strstr((const char*) _buff, "object") != 0) {
[3452]67            TRACE("VERSION 1");
[2822]68            version = 1;
69            break;
[4904]70        } else if (strstr(_buff, "record format") != 0) {
[3452]71            TRACE("VERSION 2");
[2822]72            version = 2;
73            break;
[800]74        }
[2822]75    } while (1);
[617]76
[953]77    if (version == 1) {
78        float dummy;
79
[4904]80        sscanf(_buff, "%s%s%s%s%s%d%d%d", str[0], str[1], str[2], str[3], str[4], &width, &height, &depth);
[800]81        getLine(stream);
[4904]82        sscanf(_buff, "%s%f%f%f", str[0], &(origin.x), &(origin.y), &(origin.z));
[800]83        getLine(stream);
[4904]84        sscanf(_buff, "%s%f%f%f", str[0], &(delta.x), &dummy, &dummy);
[800]85        getLine(stream);
[4904]86        sscanf(_buff, "%s%f%f%f", str[0], &dummy, &(delta.y), &dummy);
[800]87        getLine(stream);
[4904]88        sscanf(_buff, "%s%f%f%f", str[0], &dummy, &dummy, &(delta.z));
[800]89        do {
90            getLine(stream);
[4904]91        } while (strcmp(_buff, "<\\HDR>") != 0);
[617]92
[800]93        width = width / 4;
94        height = height / 4;
95        depth = depth / 4;
96        //data = new double[width * height * depth * 8 * 4];
97        data = malloc(width * height * depth * 8 * 4 * sizeof(double));
[2822]98        // 8 atom per cell, 4 double (x, y, z, and probability) per atom
[800]99        try {
[2822]100            stream.read((char *)data, width * height * depth * 8 * 4 * sizeof(double));
101        } catch (...) {
[3452]102            ERROR("Caught exception trying to read stream");
[800]103        }
[617]104
[800]105        volume = buildUp(origin, delta, width, height, depth, data);
[617]106
[800]107        free(data);
[2822]108    } else if (version == 2) {
109        const char *pt;
[800]110        int datacount;
111        double emptyvalue;
112        do {
113            getLine(stream);
[4904]114            if ((pt = strstr(_buff, "delta")) != 0) {
[800]115                sscanf(pt, "%s%f%f%f", str[0], &(delta.x), &(delta.y), &(delta.z));
[806]116#ifdef _LOADER_DEBUG_
[3452]117                TRACE("delta : %f %f %f", delta.x, delta.y, delta.z);
[806]118#endif
[4904]119            } else if ((pt = strstr(_buff, "datacount")) != 0) {
[800]120                sscanf(pt, "%s%d", str[0], &datacount);
[806]121#ifdef _LOADER_DEBUG_
[3452]122                TRACE("datacount = %d", datacount);
[806]123#endif
[4904]124            } else if ((pt = strstr(_buff, "datatype")) != 0) {
[800]125                sscanf(pt, "%s%s", str[0], str[1]);
[2822]126                if (strcmp(str[1], "double64")) {
[800]127                }
[4904]128            } else if ((pt = strstr(_buff, "count")) != 0) {
[800]129                sscanf(pt, "%s%d%d%d", str[0], &width, &height, &depth);
[806]130#ifdef _LOADER_DEBUG_
[3452]131                TRACE("width height depth %d %d %d", width, height, depth);
[806]132#endif
[4904]133            } else if ((pt = strstr(_buff, "emptymark")) != 0) {
[800]134                sscanf(pt, "%s%lf", str[0], &emptyvalue);
[806]135#ifdef _LOADER_DEBUG_
[3452]136                TRACE("emptyvalue %lf", emptyvalue);
[806]137#endif
[800]138            }
[4904]139        } while (strcmp(_buff, "<\\HDR>") != 0 && strcmp(_buff, "</HDR>") != 0);
[800]140
141        data = malloc(width * height * depth * 8 * 4 * sizeof(double));
[804]142        memset(data, 0, width * height * depth * 8 * 4 * sizeof(double));
[2822]143        stream.read((char *) data, width * height * depth * 8 * 4 * sizeof(double));
[800]144
145        volume =  buildUp(origin, delta, width, height, depth, datacount, emptyvalue, data);
146        free(data);
[617]147    }
[800]148    return volume;
[601]149}
150
[617]151struct _NvAtomInfo {
152    double indexX, indexY, indexZ;
153    double atom;
[601]154
155    int getIndex(int width, int height) const
156    {
157        // NOTE
[2844]158        // Zinc blende data has different axes from OpenGL
[601]159        // + z -> +x (OpenGL)
160        // + x -> +y (OpenGL)
161        // + y -> +z (OpenGL), But in 3D texture coordinate is the opposite direction of z
[2844]162        // The reason why index is multiplied by 4 is that one unit cell has half of eight atoms,
163        // i.e. four atoms are mapped into RGBA component of one texel
[777]164        //return ((int) (indexZ - 1)+ (int) (indexX - 1) * width + (int) (indexY - 1) * width * height) * 4;
[2822]165        return ((int)(indexX - 1) + (int)(indexY - 1) * width + (int)(indexZ - 1) * width * height) * 4;
[601]166    }
167};
168
169template<class T>
[2822]170inline T _NvMax2(T a, T b)
171{ return ((a >= b)? a : b); }
[601]172
173template<class T>
[2822]174inline T _NvMin2(T a, T b)
175{ return ((a >= b)? a : b); }
[601]176
177template<class T>
[2822]178inline T _NvMax3(T a, T b, T c)
179{ return ((a >= b)? ((a >= c) ? a : c) : ((b >= c)? b : c)); }
[601]180
181template<class T>
[2822]182inline T _NvMin3(T a, T b, T c)
183{ return ((a <= b)? ((a <= c) ? a : c) : ((b <= c)? b : c)); }
[601]184
185template<class T>
[2822]186inline T _NvMax9(T* a, T curMax)
187{ return _NvMax3(_NvMax3(a[0], a[1], a[2]), _NvMax3(a[3], a[4], a[5]), _NvMax3(a[6], a[7], curMax)); }
[601]188
189template<class T>
[2822]190inline T _NvMin9(T* a, T curMax)
191{ return _NvMin3(_NvMax3(a[0], a[1], a[2]), _NvMin3(a[3], a[4], a[5]), _NvMin3(a[6], a[7], curMax)); }
[601]192
[617]193template<class T>
[2822]194inline T _NvMax4(T* a)
195{ return _NvMax2(_NvMax2(a[0], a[1]), _NvMax2(a[2], a[3])); }
[617]196
197template<class T>
[2822]198inline T _NvMin4(T* a)
199{ return _NvMin2(_NvMin2(a[0], a[1]), _NvMin2(a[2], a[3])); }
[617]200
[2822]201ZincBlendeVolume *
[3611]202ZincBlendeReconstructor::buildUp(const Vector3f& origin, const Vector3f& delta,
[2844]203                                   int width, int height, int depth, void *data)
[601]204{
[2822]205    ZincBlendeVolume *zincBlendeVolume = NULL;
[601]206
[617]207    float *fourAnionVolume, *fourCationVolume;
[601]208    int cellCount = width * height * depth;
[617]209    fourAnionVolume = new float[cellCount * sizeof(float) * 4];
210    fourCationVolume = new float[cellCount * sizeof(float) * 4];
[601]211
[2822]212    _NvAtomInfo *srcPtr = (_NvAtomInfo *)data;
[601]213
[777]214    float vmin, vmax, nzero_min;
[2822]215    float *component4A, *component4B;
[601]216    int index;
217
[953]218    nzero_min = 0.0f;           /* Suppress compiler warning. */
[617]219    vmin = vmax = srcPtr->atom;
220
[2822]221    for (int i = 0; i < cellCount; ++i) {
[601]222        index = srcPtr->getIndex(width, height);
[804]223
[806]224#ifdef _LOADER_DEBUG_
[3452]225        TRACE("index %d", index);
[806]226#endif
[804]227
[617]228        component4A = fourAnionVolume + index;
229        component4B = fourCationVolume + index;
[601]230
[2822]231        component4A[0] = (float)srcPtr->atom; srcPtr++;
232        component4A[1] = (float)srcPtr->atom; srcPtr++;
233        component4A[2] = (float)srcPtr->atom; srcPtr++;
234        component4A[3] = (float)srcPtr->atom; srcPtr++;
[617]235     
[2822]236        component4B[0] = (float)srcPtr->atom; srcPtr++;
237        component4B[1] = (float)srcPtr->atom; srcPtr++;
238        component4B[2] = (float)srcPtr->atom; srcPtr++;
239        component4B[3] = (float)srcPtr->atom; srcPtr++;
[601]240
[617]241        vmax = _NvMax3(_NvMax4(component4A), _NvMax4(component4B), vmax);
242        vmin = _NvMin3(_NvMin4(component4A), _NvMin4(component4B), vmin);
[777]243
[2822]244        if (vmin != 0.0 && vmin < nzero_min) {
245            nzero_min = vmin;
[777]246        }
[617]247    }
[601]248
[617]249    double dv = vmax - vmin;
[2822]250    if (vmax != 0.0f) {
251        for (int i = 0; i < cellCount; ++i) {
[617]252            fourAnionVolume[i] = (fourAnionVolume[i] - vmin)/ dv;
253            fourCationVolume[i] = (fourCationVolume[i] - vmin) / dv;
254        }
255    }
[601]256
[3492]257    Vector3f cellSize;
[617]258    cellSize.x = 0.25 / width;
259    cellSize.y = 0.25 / height;
260    cellSize.z = 0.25 / depth;
[601]261
[5406]262    zincBlendeVolume = new ZincBlendeVolume(width, height, depth, 4,
[617]263                                            fourAnionVolume, fourCationVolume,
[777]264                                            vmin, vmax, nzero_min, cellSize);
[601]265
[5406]266    zincBlendeVolume->xAxis.setRange(origin.x, origin.x + delta.x * (width-1));
267    zincBlendeVolume->yAxis.setRange(origin.y, origin.y + delta.y * (height-1));
268    zincBlendeVolume->zAxis.setRange(origin.z, origin.z + delta.z * (depth-1));
269
[601]270    return zincBlendeVolume;
271}
272
[2822]273ZincBlendeVolume *
[3611]274ZincBlendeReconstructor::buildUp(const Vector3f& origin, const Vector3f& delta,
[2822]275                                   int width, int height, int depth,
276                                   int datacount, double emptyvalue, void* data)
[800]277{
[2822]278    ZincBlendeVolume *zincBlendeVolume = NULL;
[800]279    float *fourAnionVolume, *fourCationVolume;
280    int size = width * height * depth * 4;
281    fourAnionVolume = new float[size];
282    fourCationVolume = new float[size];
283
284    memset(fourAnionVolume, 0, size * sizeof(float));
285    memset(fourCationVolume, 0, size * sizeof(float));
286
[2822]287    _NvAtomInfo *srcPtr = (_NvAtomInfo *) data;
[800]288
[2822]289    float *component4A, *component4B;
[800]290    float vmin, vmax, nzero_min;
291    int index;
292    nzero_min = 1e23f;
293    vmin = vmax = srcPtr->atom;
[2822]294    for (int i = 0; i < datacount; ++i) {
[800]295        index = srcPtr->getIndex(width, height);
[806]296
297#ifdef _LOADER_DEBUG_
[3452]298        TRACE("[%d] index %d (width:%lf height:%lf depth:%lf)", i, index, srcPtr->indexX, srcPtr->indexY, srcPtr->indexZ);
[806]299#endif
[804]300
301        if (index < 0) {
[806]302#ifdef _LOADER_DEBUG_
[3452]303            TRACE("There is an invalid data");
[806]304#endif
[804]305            srcPtr +=8;
306            continue;
307        }
308
[800]309        component4A = fourAnionVolume + index;
310        component4B = fourCationVolume + index;
311
312        component4A[0] = (srcPtr->atom != emptyvalue)? (float) srcPtr->atom : 0.0f; srcPtr++;
313        component4A[1] = (srcPtr->atom != emptyvalue)? (float) srcPtr->atom : 0.0f; srcPtr++;
314        component4A[2] = (srcPtr->atom != emptyvalue)? (float) srcPtr->atom : 0.0f; srcPtr++;
315        component4A[3] = (srcPtr->atom != emptyvalue)? (float) srcPtr->atom : 0.0f; srcPtr++;
316     
317        component4B[0] = (srcPtr->atom != emptyvalue)? (float) srcPtr->atom : 0.0f; srcPtr++;
318        component4B[1] = (srcPtr->atom != emptyvalue)? (float) srcPtr->atom : 0.0f; srcPtr++;
319        component4B[2] = (srcPtr->atom != emptyvalue)? (float) srcPtr->atom : 0.0f; srcPtr++;
320        component4B[3] = (srcPtr->atom != emptyvalue)? (float) srcPtr->atom : 0.0f; srcPtr++;
321
322        vmax = _NvMax3(_NvMax4(component4A), _NvMax4(component4B), vmax);
323        vmin = _NvMin3(_NvMin4(component4A), _NvMin4(component4B), vmin);
324
[2822]325        if (vmin != 0.0 && vmin < nzero_min) {
[800]326            nzero_min = vmin;   
327        }
328    }
329
330    double dv = vmax - vmin;
[2822]331    if (vmax != 0.0f) {
332        for (int i = 0; i < datacount; ++i) {
[800]333            fourAnionVolume[i] = (fourAnionVolume[i] - vmin)/ dv;
334            fourCationVolume[i] = (fourCationVolume[i] - vmin) / dv;
335        }
336    }
337
[3492]338    Vector3f cellSize;
[800]339    cellSize.x = 0.25 / width;
340    cellSize.y = 0.25 / height;
341    cellSize.z = 0.25 / depth;
342
[5406]343    zincBlendeVolume = new ZincBlendeVolume(width, height, depth, 4,
[800]344                                            fourAnionVolume, fourCationVolume,
345                                            vmin, vmax, nzero_min, cellSize);
346
[5406]347    zincBlendeVolume->xAxis.setRange(origin.x, origin.x + delta.x * (width-1));
348    zincBlendeVolume->yAxis.setRange(origin.y, origin.y + delta.y * (height-1));
349    zincBlendeVolume->zAxis.setRange(origin.z, origin.z + delta.z * (depth-1));
350
[800]351    return zincBlendeVolume;
352}
353
[3611]354void ZincBlendeReconstructor::getLine(std::istream& sin)
[617]355{
356    char ch;
357    int index = 0;
358    do {
359        sin.get(ch);
360        if (ch == '\n') break;
[4904]361        _buff[index++] = ch;
[2822]362        if (ch == '>') {
[4904]363            if (_buff[1] == '\\')
[617]364                break;
365        }
366    } while (!sin.eof());
367
[4904]368    _buff[index] = '\0';
[804]369
[806]370#ifdef _LOADER_DEBUG_
[4904]371    TRACE("%s", _buff);
[806]372#endif
[617]373}
374
[2822]375ZincBlendeVolume *
[4889]376ZincBlendeReconstructor::loadFromMemory(const void *dataBlock)
[804]377{
[2822]378    ZincBlendeVolume *volume = NULL;
[3492]379    Vector3f origin, delta;
[804]380    int width = 0, height = 0, depth = 0;
[2822]381    void *data = NULL;
[804]382    int version = 1;
383
[4889]384    const unsigned char *stream = (const unsigned char *)dataBlock;
[804]385    char str[5][20];
386    do {
387        getLine(stream);
[4904]388        if (_buff[0] == '#') {
[804]389            continue;
[4904]390        } else if (strstr((const char *)_buff, "object") != 0) {
[3452]391            TRACE("VERSION 1");
[2822]392            version = 1;
393            break;
[4904]394        } else if (strstr(_buff, "record format") != 0) {
[3452]395            TRACE("VERSION 2");
[2822]396            version = 2;
397            break;
[804]398        }
[2822]399    } while (1);
[804]400
[2822]401    if (version == 1) {
[953]402        float dummy;
403
[4904]404        sscanf(_buff, "%s%s%s%s%s%d%d%d", str[0], str[1], str[2], str[3], str[4],&width, &height, &depth);
[804]405        getLine(stream);
[4904]406        sscanf(_buff, "%s%f%f%f", str[0], &(origin.x), &(origin.y), &(origin.z));
[804]407        getLine(stream);
[4904]408        sscanf(_buff, "%s%f%f%f", str[0], &(delta.x), &dummy, &dummy);
[804]409        getLine(stream);
[4904]410        sscanf(_buff, "%s%f%f%f", str[0], &dummy, &(delta.y), &dummy);
[804]411        getLine(stream);
[4904]412        sscanf(_buff, "%s%f%f%f", str[0], &dummy, &dummy, &(delta.z));
[804]413        do {
414            getLine(stream);
[4904]415        } while (strcmp(_buff, "<\\HDR>") != 0);
[804]416
417        width = width / 4;
418        height = height / 4;
419        depth = depth / 4;
420        data = malloc(width * height * depth * 8 * 4 * sizeof(double));
[2822]421        // 8 atom per cell, 4 double (x, y, z, and probability) per atom
422        memcpy(data, stream, width * height * depth * 8 * 4 * sizeof(double));
[804]423
424        volume = buildUp(origin, delta, width, height, depth, data);
425
426        free(data);
[2822]427    } else if (version == 2) {
428        const char *pt;
[804]429        int datacount = -1;
430        double emptyvalue;
431        do {
432            getLine(stream);
[4904]433            if ((pt = strstr(_buff, "delta")) != 0) {   
[804]434                sscanf(pt, "%s%f%f%f", str[0], &(delta.x), &(delta.y), &(delta.z));
[806]435#ifdef _LOADER_DEBUG_
[3452]436                TRACE("delta : %f %f %f", delta.x, delta.y, delta.z);
[806]437#endif
[4904]438            } else if ((pt = strstr(_buff, "datacount")) != 0) {
[804]439                sscanf(pt, "%s%d", str[0], &datacount);
[3452]440                TRACE("datacount = %d", datacount);
[4904]441            } else if ((pt = strstr(_buff, "datatype")) != 0) {
[804]442                sscanf(pt, "%s%s", str[0], str[1]);
[2822]443                if (strcmp(str[1], "double64")) {
[804]444                }
[4904]445            } else if ((pt = strstr(_buff, "count")) != 0) {
[804]446                sscanf(pt, "%s%d%d%d", str[0], &width, &height, &depth);
[806]447#ifdef _LOADER_DEBUG_
[3452]448                TRACE("width height depth %d %d %d", width, height, depth);
[806]449#endif
[4904]450            } else if ((pt = strstr(_buff, "emptymark")) != 0) {
[804]451                sscanf(pt, "%s%lf", str[0], &emptyvalue);
[806]452#ifdef _LOADER_DEBUG_
[3452]453                TRACE("emptyvalue %lf", emptyvalue);
[806]454#endif
[804]455            }
[4904]456        } while (strcmp(_buff, "<\\HDR>") != 0 && strcmp(_buff, "</HDR>") != 0);
[804]457
458        if (datacount == -1) datacount = width * height * depth;
459
460        data = malloc(datacount * 8 * 4 * sizeof(double));
461        memset(data, 0, datacount * 8 * 4 * sizeof(double));
462        memcpy(data, stream, datacount * 8 * 4 * sizeof(double));
463
464        volume =  buildUp(origin, delta, width, height, depth, datacount, emptyvalue, data);
465
466        free(data);
467    }
468    return volume;
469}
470
[4889]471void ZincBlendeReconstructor::getLine(const unsigned char*& stream)
[804]472{
473    char ch;
474    int index = 0;
475    do {
476        ch = stream[0];
477        ++stream;
478        if (ch == '\n') break;
[4904]479        _buff[index++] = ch;
[2822]480        if (ch == '>') {
[4904]481            if (_buff[1] == '\\')
[804]482                break;
483        }
484    } while (1);
485
[4904]486    _buff[index] = '\0';
[804]487
[806]488#ifdef _LOADER_DEBUG_
[4904]489    TRACE("%s", _buff);
[806]490#endif
[804]491}
Note: See TracBrowser for help on using the repository browser.