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 |
|
---|
17 | namespace CHCDemoEngine
|
---|
18 | {
|
---|
19 |
|
---|
20 |
|
---|
21 | ViewCell *ViewCellsTree::GetViewCell(const Vector3 &point) const
|
---|
22 | {
|
---|
23 | ViewCellsTreeNode *node = mRoot;
|
---|
24 |
|
---|
25 | while (!node->IsLeaf())
|
---|
26 | {
|
---|
27 | //cout << "pt: " << point[node->mAxis] << " " << node->mPosition << endl;
|
---|
28 | if (point[node->mAxis] < node->mPosition)
|
---|
29 | node = node->mBack;
|
---|
30 | else
|
---|
31 | node = node->mFront;
|
---|
32 | }
|
---|
33 |
|
---|
34 | //cout << "vc: " << node->mViewCell->GetBox() << " pvs " << node->mViewCell->mPvs.Size() << endl;
|
---|
35 |
|
---|
36 | return node->mViewCell;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | bool ViewCellsTree::LoadFromFile(const std::string &filename, float scaleFactor)
|
---|
41 | {
|
---|
42 | cout << "Loading view cells from file '" + filename + "'";
|
---|
43 |
|
---|
44 | FILE *fr = fopen(filename.c_str(), "rb");
|
---|
45 |
|
---|
46 | if (!fr)
|
---|
47 | {
|
---|
48 | cerr << "Error: Cannot open file for reading" << endl;
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 |
|
---|
52 | bool result = _LoadFromFile(fr, scaleFactor);
|
---|
53 |
|
---|
54 | cout << "finished loading view cells. " << endl;
|
---|
55 | fclose(fr);
|
---|
56 |
|
---|
57 | return result;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | bool ViewCellsTree::_LoadFromFile(FILE *fr, float scaleFactor)
|
---|
62 | {
|
---|
63 | int buffer[256];
|
---|
64 | fread(buffer, sizeof(int), 2, fr);
|
---|
65 |
|
---|
66 | if (buffer[0] != MAGIC)
|
---|
67 | {
|
---|
68 | cerr << "Error: Wrong file type" << endl;
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (buffer[1] != (int)(1000 * VIEWCELLS_VERSION))
|
---|
73 | {
|
---|
74 | cerr << "Error: Wrong viewcells version" << endl;
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 |
|
---|
78 | AxisAlignedBox3 box;
|
---|
79 | // get the bounding box
|
---|
80 | // exchange x and y coordinates and recompute box
|
---|
81 | fread(&box, sizeof(AxisAlignedBox3), 1, fr);
|
---|
82 |
|
---|
83 | box.Scale(scaleFactor);
|
---|
84 |
|
---|
85 | Vector3 v1(box.Min().x, -box.Min().z, box.Min().y);
|
---|
86 | Vector3 v2(box.Max().x, -box.Max().z, box.Max().y);
|
---|
87 |
|
---|
88 | mBox.Initialize();
|
---|
89 |
|
---|
90 | mBox.Include(v1);
|
---|
91 | mBox.Include(v2);
|
---|
92 |
|
---|
93 | stack<ViewCellsTreeNode **> nodeStack;
|
---|
94 | nodeStack.push(&mRoot);
|
---|
95 |
|
---|
96 | cout << "view cells bounds " << mBox << endl;
|
---|
97 |
|
---|
98 | int numViewCells = 0;
|
---|
99 |
|
---|
100 | int axes[] = {0, 0, 0};
|
---|
101 |
|
---|
102 | while (!nodeStack.empty())
|
---|
103 | {
|
---|
104 | ViewCellsTreeNode *&node = *nodeStack.top();
|
---|
105 | nodeStack.pop();
|
---|
106 | node = new ViewCellsTreeNode();
|
---|
107 |
|
---|
108 | ++ numViewCells;
|
---|
109 |
|
---|
110 | int axis;
|
---|
111 | fread(&axis, sizeof(int), 1, fr);
|
---|
112 |
|
---|
113 | // exchange y and z axis like for whole scene
|
---|
114 | if (axis == 2) axis = 1;
|
---|
115 | else if (axis == 1) axis = 2;
|
---|
116 |
|
---|
117 | node->mAxis = axis;
|
---|
118 |
|
---|
119 | if (!node->IsLeaf())
|
---|
120 | {
|
---|
121 | ++ axes[node->mAxis];
|
---|
122 |
|
---|
123 | float mypos;
|
---|
124 | fread(&mypos, sizeof(float), 1, fr);
|
---|
125 |
|
---|
126 | mypos *= scaleFactor;
|
---|
127 |
|
---|
128 | // changed coordinate system must be considered also here
|
---|
129 | if (node->mAxis == 1)
|
---|
130 | {
|
---|
131 | node->mPosition = -mypos;
|
---|
132 |
|
---|
133 | nodeStack.push(&node->mBack);
|
---|
134 | nodeStack.push(&node->mFront);
|
---|
135 | //cout << mypos << " ";
|
---|
136 | }
|
---|
137 | else
|
---|
138 | {
|
---|
139 | node->mPosition = mypos;
|
---|
140 |
|
---|
141 | nodeStack.push(&node->mFront);
|
---|
142 | nodeStack.push(&node->mBack);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 |
|
---|
152 | } |
---|