1 | #include "ViewCellsTree.h"
|
---|
2 | #include "Timer/PerfTimer.h"
|
---|
3 | #include <stack>
|
---|
4 | #include <string>
|
---|
5 |
|
---|
6 | using 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 |
|
---|
16 | namespace CHCDemoEngine
|
---|
17 | {
|
---|
18 |
|
---|
19 |
|
---|
20 | ViewCell *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 |
|
---|
39 | bool 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 |
|
---|
60 | bool 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 | // exchange x and y coordinates and recompute box
|
---|
79 | fread(&mBox, sizeof(AxisAlignedBox3), 1, fr);
|
---|
80 |
|
---|
81 | Vector3 v1(mBox.Min().x, -mBox.Min().z, mBox.Min().y);
|
---|
82 | Vector3 v2(mBox.Max().x, -mBox.Max().z, mBox.Max().y);
|
---|
83 |
|
---|
84 | mBox.Initialize();
|
---|
85 |
|
---|
86 | mBox.Include(v1);
|
---|
87 | mBox.Include(v2);
|
---|
88 |
|
---|
89 | stack<ViewCellsTreeNode **> nodeStack;
|
---|
90 | nodeStack.push(&mRoot);
|
---|
91 |
|
---|
92 | cout << "view cells bounds " << mBox << endl;
|
---|
93 |
|
---|
94 | int numViewCells = 0;
|
---|
95 |
|
---|
96 | int axes[] = {0, 0, 0};
|
---|
97 |
|
---|
98 | while (!nodeStack.empty())
|
---|
99 | {
|
---|
100 | ViewCellsTreeNode *&node = *nodeStack.top();
|
---|
101 | nodeStack.pop();
|
---|
102 | node = new ViewCellsTreeNode();
|
---|
103 |
|
---|
104 | ++ numViewCells;
|
---|
105 |
|
---|
106 | int axis;
|
---|
107 | fread(&axis, sizeof(int), 1, fr);
|
---|
108 |
|
---|
109 | // exchange y and z axis like for whole scene
|
---|
110 | if (axis == 2) axis = 1;
|
---|
111 | else if (axis == 1) axis = 2;
|
---|
112 |
|
---|
113 | node->mAxis = axis;
|
---|
114 |
|
---|
115 | if (!node->IsLeaf())
|
---|
116 | {
|
---|
117 | ++ axes[node->mAxis];
|
---|
118 |
|
---|
119 | float mypos;
|
---|
120 | fread(&mypos, sizeof(float), 1, fr);
|
---|
121 | // changed coordinate system must be considered also here
|
---|
122 | if (node->mAxis == 1)
|
---|
123 | {
|
---|
124 | node->mPosition = -mypos;
|
---|
125 |
|
---|
126 | nodeStack.push(&node->mBack);
|
---|
127 | nodeStack.push(&node->mFront);
|
---|
128 | //cout << mypos << " ";
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | node->mPosition = mypos;
|
---|
133 |
|
---|
134 | nodeStack.push(&node->mFront);
|
---|
135 | nodeStack.push(&node->mBack);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | cout << "\nloaded " << numViewCells << " view cells" << " " << axes[0] << " " << axes[1] << " " << axes[2] << endl;
|
---|
141 | return true;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | } |
---|