source: trunk/vizservers/nanovis/NvZincBlendeReconstructor.cpp @ 749

Last change on this file since 749 was 617, checked in by vrinside, 17 years ago

Added new zinc blende renderer - It is still needed to compare with unicell based simulation data.
Removed tentatively used class, NvVolQDVolumeShader,NvVolQDVolume
Moved Font.bmp into resources directory

File size: 5.9 KB
Line 
1#include "NvZincBlendeReconstructor.h"
2#include "ZincBlendeVolume.h"
3
4NvZincBlendeReconstructor* NvZincBlendeReconstructor::_instance = NULL;
5
6NvZincBlendeReconstructor::NvZincBlendeReconstructor()
7{
8}
9
10NvZincBlendeReconstructor::~NvZincBlendeReconstructor()
11{
12}
13
14NvZincBlendeReconstructor* NvZincBlendeReconstructor::getInstance()
15{
16    if (_instance == NULL)
17    {
18        return (_instance = new NvZincBlendeReconstructor());
19    }
20
21    return _instance;
22}
23
24ZincBlendeVolume* NvZincBlendeReconstructor::loadFromFile(const char* fileName)
25{
26    Vector3 origin, delta;
27    double temp;
28    int width = 0, height = 0, depth = 0;
29    void* data = NULL;
30
31    ifstream stream;
32    stream.open(fileName, ios::binary);
33
34    ZincBlendeVolume* volume = loadFromStream(stream);
35
36    stream.close();
37
38    return volume;
39}
40
41ZincBlendeVolume* NvZincBlendeReconstructor::loadFromStream(std::istream& stream)
42{
43    Vector3 origin, delta;
44    double temp;
45    int width = 0, height = 0, depth = 0;
46    void* data = NULL;
47
48    char str[5][20];
49    do {
50        getLine(stream);
51    } while(strstr(buff, "object") == 0);
52
53
54     sscanf(buff, "%s%s%s%s%s%d%d%d", str[0], str[1], str[2], str[3], str[4],&width, &height, &depth);
55     getLine(stream);
56     sscanf(buff, "%s%f%f%f", str[0], &(origin.x), &(origin.y), &(origin.z));
57     getLine(stream);
58     sscanf(buff, "%s%f%f%f", str[0], &(delta.x), &temp, &temp);
59     getLine(stream);
60     sscanf(buff, "%s%f%f%f", str[0], &temp, &(delta.y), &temp);
61     getLine(stream);
62     sscanf(buff, "%s%f%f%f", str[0], &temp, &temp, &(delta.z));
63
64    do {
65        getLine(stream);
66    } while(strcmp(buff, "<\\HDR>") != 0);
67
68   
69    width = width / 4;
70    height = height / 4;
71    depth = depth / 4;
72    data = new double[width * height * depth * 8 * 4]; // 8 atom per cell, 4 double (x, y, z, and probability) per atom
73
74    try {
75        stream.read((char*) data, width * height * depth * 8 * 4 * sizeof(double));
76    }
77    catch (...)
78    {
79        printf("ERROR\n");
80    }
81
82    return buildUp(origin, delta, width, height, depth, data);
83}
84
85struct _NvAtomInfo {
86    double indexX, indexY, indexZ;
87    double atom;
88
89    int getIndex(int width, int height) const
90    {
91        // NOTE
92        // Zinc blende data has different axises from OpenGL
93        // + z -> +x (OpenGL)
94        // + x -> +y (OpenGL)
95        // + y -> +z (OpenGL), But in 3D texture coordinate is the opposite direction of z
96        // The reasone why index is multiplied by 4 is that one unit cell has half of eight atoms
97        // ,i.e. four atoms are mapped into RGBA component of one texel
98        return ((int) (indexZ - 1)+ (int) (indexX - 1) * width + (int) (indexY - 1) * width * height) * 4;
99    }
100};
101
102
103template<class T>
104inline T _NvMax2(T a, T b) { return ((a >= b)? a : b); }
105
106template<class T>
107inline T _NvMin2(T a, T b) { return ((a >= b)? a : b); }
108
109template<class T>
110inline T _NvMax3(T a, T b, T c) { return ((a >= b)? ((a >= c) ? a : c) : ((b >= c)? b : c)); }
111
112template<class T>
113inline T _NvMin3(T a, T b, T c) { return ((a <= b)? ((a <= c) ? a : c) : ((b <= c)? b : c)); }
114
115template<class T>
116inline T _NvMax9(T* a, T curMax) { return _NvMax3(_NvMax3(a[0], a[1], a[2]), _NvMax3(a[3], a[4], a[5]), _NvMax3(a[6], a[7], curMax)); }
117
118template<class T>
119inline T _NvMin9(T* a, T curMax) { return _NvMin3(_NvMax3(a[0], a[1], a[2]), _NvMin3(a[3], a[4], a[5]), _NvMin3(a[6], a[7], curMax)); }
120
121template<class T>
122inline T _NvMax4(T* a) { return _NvMax2(_NvMax2(a[0], a[1]), _NvMax2(a[2], a[3])); }
123
124template<class T>
125inline T _NvMin4(T* a) { return _NvMin2(_NvMin2(a[0], a[1]), _NvMin2(a[2], a[3])); }
126
127
128ZincBlendeVolume* NvZincBlendeReconstructor::buildUp(const Vector3& origin, const Vector3& delta, int width, int height, int depth, void* data)
129{
130    ZincBlendeVolume* zincBlendeVolume = NULL;
131
132    float *fourAnionVolume, *fourCationVolume;
133    int cellCount = width * height * depth;
134    fourAnionVolume = new float[cellCount * sizeof(float) * 4];
135    fourCationVolume = new float[cellCount * sizeof(float) * 4];
136
137    _NvAtomInfo* srcPtr = (_NvAtomInfo*) data;
138
139    float vmin, vmax;
140    float* component4A, *component4B;
141    int index;
142
143    vmin = vmax = srcPtr->atom;
144
145    int i;
146    for (i = 0; i < cellCount; ++i)
147    {
148        index = srcPtr->getIndex(width, height);
149        component4A = fourAnionVolume + index;
150        component4B = fourCationVolume + index;
151
152        component4A[0] = (float) srcPtr->atom; srcPtr++;
153        component4A[1] = (float) srcPtr->atom; srcPtr++;
154        component4A[2] = (float) srcPtr->atom; srcPtr++;
155        component4A[3] = (float) srcPtr->atom; srcPtr++;
156     
157        component4B[0] = (float) srcPtr->atom; srcPtr++;
158        component4B[1] = (float) srcPtr->atom; srcPtr++;
159        component4B[2] = (float) srcPtr->atom; srcPtr++;
160        component4B[3] = (float) srcPtr->atom; srcPtr++;
161
162        vmax = _NvMax3(_NvMax4(component4A), _NvMax4(component4B), vmax);
163        vmin = _NvMin3(_NvMin4(component4A), _NvMin4(component4B), vmin);
164    }
165
166    double dv = vmax - vmin;
167    if (vmax != 0.0f)
168    {
169        for (i=0; i < cellCount; ++i)
170        {
171            fourAnionVolume[i] = (fourAnionVolume[i] - vmin)/ dv;
172            fourCationVolume[i] = (fourCationVolume[i] - vmin) / dv;
173        }
174    }
175
176    Vector3 cellSize;
177    cellSize.x = 0.25 / width;
178    cellSize.y = 0.25 / height;
179    cellSize.z = 0.25 / depth;
180
181    zincBlendeVolume = new ZincBlendeVolume(origin.x, origin.y, origin.z,
182                                            width, height, depth, 1, 4,
183                                            fourAnionVolume, fourCationVolume,
184                                            vmin, vmax, cellSize);
185
186    return zincBlendeVolume;
187}
188
189void NvZincBlendeReconstructor::getLine(std::istream& sin)
190{
191    char ch;
192    int index = 0;
193    do {
194        sin.get(ch);
195        if (ch == '\n') break;
196        buff[index++] = ch;
197        if (ch == '>')
198        {
199            if (buff[1] == '\\')
200                break;
201        }
202    } while (!sin.eof());
203
204    buff[index] = '\0';
205}
206
Note: See TracBrowser for help on using the repository browser.