source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ViewCellsTree.cpp @ 3244

Revision 3244, 2.6 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "ViewCellsTree.h"
2#include "Timer/PerfTimer.h"
3#include <stack>
4#include <string>
5
6using namespace std;
7
8
9// MAGIC of all bin exports
10#ifndef MAGIC
11#define MAGIC 0x827923
12#endif
13
14#define VIEWCELLS_VERSION 1.0f
15
16namespace CHCDemoEngine
17{
18
19
20ViewCell *ViewCellsTree::GetViewCell(const Vector3 &point) const
21{
22        ViewCellsTreeNode *node = mRoot;
23
24        while (!node->IsLeaf())
25        {
26                //cout << "pt: " << point[node->mAxis] << " " << node->mPosition << endl;
27                if (point[node->mAxis] < node->mPosition)
28                        node = node->mBack;
29                else
30                        node = node->mFront;
31        }
32
33        //cout << "vc: " << node->mViewCell->GetBox() << " pvs " << node->mViewCell->mPvs.Size() << endl;
34
35        return node->mViewCell;
36}
37
38
39bool ViewCellsTree::LoadFromFile(const std::string &filename)
40{
41        cout << "Loading view cells from file '" + filename + "'";
42
43        FILE *fr = fopen(filename.c_str(), "rb");
44
45        if (!fr)
46        {
47                cerr << "Error: Cannot open file for reading" << endl;
48                return false;
49        }
50
51        bool result = _LoadFromFile(fr);
52
53        cout << "finished loading view cells. " << endl;
54        fclose(fr);
55
56        return result;
57}
58
59
60bool ViewCellsTree::_LoadFromFile(FILE *fr)
61{
62        int buffer[256];
63        fread(buffer, sizeof(int), 2, fr);
64
65        if (buffer[0] != MAGIC)
66        {
67                cerr << "Error: Wrong file type" << endl;
68                return false;
69        }
70
71        if (buffer[1] != (int)(1000 * VIEWCELLS_VERSION))
72        {
73                cerr << "Error: Wrong viewcells version" << endl;
74                return false;
75        }
76
77        // get the bounding box
78        fread(&mBox, sizeof(AxisAlignedBox3), 1, fr);
79
80        Vector3 v1(mBox.Min().x, -mBox.Min().z, mBox.Min().y);
81        Vector3 v2(mBox.Max().x, -mBox.Max().z, mBox.Max().y);
82       
83        Vector3 newMin = v1;
84        Vector3 newMax = v2;
85
86        Minimize(newMin, v2);
87        Maximize(newMax, v1);
88
89        mBox.SetMin(newMin);
90        mBox.SetMax(newMax);
91
92        stack<ViewCellsTreeNode **> nodeStack;
93        nodeStack.push(&mRoot);
94
95        cout << "view cells bounds " << mBox << endl;
96
97        int numViewCells = 0;
98
99        while (!nodeStack.empty())
100        {
101                ViewCellsTreeNode *&node = *nodeStack.top();
102                nodeStack.pop();
103                node = new ViewCellsTreeNode();
104
105                ++ numViewCells;
106
107                int axis;
108                fread(&axis, sizeof(int), 1, fr);
109
110                // exchange y and z axis like we have to do for the scene
111                if (axis == 2) axis = 1;
112                else if (axis == 1) axis = 2;
113
114                node->mAxis = axis;
115
116                if (node->mAxis > 2 || node->mAxis < -1) cout << "a " << node->mAxis << " ";
117
118                if (!node->IsLeaf())
119                {
120                        float pos;
121                        fread(&pos, sizeof(float), 1, fr);
122
123                        if (axis == 1) pos = -pos;
124                        node->mPosition = pos;
125                       
126                        nodeStack.push(&node->mFront);
127                        nodeStack.push(&node->mBack);
128                }
129        }
130
131        cout << "loaded " << numViewCells << " view cells" << endl;
132        return true;
133}
134
135
136}
Note: See TracBrowser for help on using the repository browser.