1 | #include "VisibilitySolutionLoader.h"
|
---|
2 | #include "ViewCellsTree.h"
|
---|
3 | #include "Bvh.h"
|
---|
4 | #include <stack>
|
---|
5 |
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 |
|
---|
10 | namespace CHCDemoEngine
|
---|
11 | {
|
---|
12 |
|
---|
13 |
|
---|
14 | typedef vector<ViewCell *> ViewCellsContainer;
|
---|
15 |
|
---|
16 |
|
---|
17 | ViewCellsTree *VisibilitySolutionLoader::Load(const std::string &filename,
|
---|
18 | Bvh *bvh,
|
---|
19 | float viewCellsScaleFactor)
|
---|
20 | {
|
---|
21 | FILE *fr = fopen(filename.c_str(), "rb");
|
---|
22 |
|
---|
23 | cout << "Loading visibility solution from file '" + filename + "'" << endl;
|
---|
24 |
|
---|
25 | if (fr == NULL)
|
---|
26 | {
|
---|
27 | cerr << "Error: Cannot open file for reading" << endl;
|
---|
28 | return NULL;
|
---|
29 | }
|
---|
30 |
|
---|
31 | float totalSamples;
|
---|
32 | float totalTime;
|
---|
33 |
|
---|
34 | fread(&totalSamples, sizeof(float), 1, fr);
|
---|
35 | fread(&totalTime, sizeof(float), 1, fr);
|
---|
36 |
|
---|
37 | cout << "loading view cells" << endl;
|
---|
38 |
|
---|
39 | ViewCellsTree *viewCellsTree = new ViewCellsTree();
|
---|
40 |
|
---|
41 | bool ok = viewCellsTree->_LoadFromFile(fr, viewCellsScaleFactor);
|
---|
42 |
|
---|
43 | cout << "finished loading view cells" << endl;
|
---|
44 |
|
---|
45 |
|
---|
46 | if (ok)
|
---|
47 | {
|
---|
48 | // skip loading of bvh nodes
|
---|
49 | int buffer[6];
|
---|
50 | fread(buffer, sizeof(int), 6, fr);
|
---|
51 |
|
---|
52 | const int numNodes = buffer[5];
|
---|
53 | const int numTriangleIds = buffer[2];
|
---|
54 |
|
---|
55 | // skip triangle ids
|
---|
56 | cout << "skipping " << numTriangleIds << " triangle ids" << endl;
|
---|
57 |
|
---|
58 | int tid;
|
---|
59 | for (int i = 0; i < numTriangleIds; ++ i)
|
---|
60 | fread(&tid, sizeof(int), 1, fr);
|
---|
61 |
|
---|
62 | cout << "skipping " << numNodes << " bvh nodes" << endl;
|
---|
63 |
|
---|
64 | for (int i = 0; i < numNodes; ++ i)
|
---|
65 | fread(buffer, sizeof(int), 4, fr);
|
---|
66 |
|
---|
67 |
|
---|
68 | cout << "allocating view cells" << endl;
|
---|
69 |
|
---|
70 | AllocateLeafViewCells(viewCellsTree);
|
---|
71 |
|
---|
72 | cout << "loading pvss" << endl;
|
---|
73 |
|
---|
74 | ok = LoadPvs(fr, bvh);
|
---|
75 |
|
---|
76 | cout << "finished loading pvss" << endl;
|
---|
77 | }
|
---|
78 |
|
---|
79 | fclose(fr);
|
---|
80 |
|
---|
81 | if (ok)
|
---|
82 | cout << "Visibility solution loaded" << endl;
|
---|
83 | else
|
---|
84 | cerr << "Error: loading visibility solution failed." << endl;
|
---|
85 |
|
---|
86 | return viewCellsTree;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | bool VisibilitySolutionLoader::CreateIdSortedList(Bvh *bvh, BvhNodeContainer &nodes)
|
---|
91 | {
|
---|
92 | std::stack<BvhNode *> tStack;
|
---|
93 | tStack.push(bvh->GetStaticRoot());
|
---|
94 |
|
---|
95 | while (!tStack.empty())
|
---|
96 | {
|
---|
97 | BvhNode *node = tStack.top();
|
---|
98 | tStack.pop();
|
---|
99 |
|
---|
100 | nodes.push_back(node);
|
---|
101 |
|
---|
102 | if (!node->IsLeaf())
|
---|
103 | {
|
---|
104 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
105 |
|
---|
106 | BvhNode *front = interior->GetFront();
|
---|
107 | BvhNode *back = interior->GetBack();
|
---|
108 |
|
---|
109 | tStack.push(front);
|
---|
110 | tStack.push(back);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | bool VisibilitySolutionLoader::LoadPvs(FILE *fw, Bvh *bvh)
|
---|
119 | {
|
---|
120 | int number, entries;
|
---|
121 |
|
---|
122 | fread(&number, sizeof(int), 1, fw);
|
---|
123 |
|
---|
124 | if (!number)
|
---|
125 | {
|
---|
126 | cerr << "Warning: empty PVSs in visibility solution" << endl;
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (number != mViewCells.size())
|
---|
131 | {
|
---|
132 | cerr << "Warning: Number of view cells (" << number << ", " << (int)mViewCells.size() << ") does not match when loading PVSs!" << endl;
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | BvhNodeContainer nodes;
|
---|
137 | CreateIdSortedList(bvh, nodes);
|
---|
138 |
|
---|
139 | for (int i = 0; i < number; ++ i)
|
---|
140 | {
|
---|
141 | fread(&entries, sizeof(int), 1, fw);
|
---|
142 |
|
---|
143 | for (int j = 0; j < entries; ++ j)
|
---|
144 | {
|
---|
145 | int objectId;
|
---|
146 | float time;
|
---|
147 |
|
---|
148 | fread(&objectId, sizeof(int), 1, fw);
|
---|
149 | fread(&time, sizeof(float), 1, fw);
|
---|
150 |
|
---|
151 | BvhNode *node = nodes[objectId];
|
---|
152 | mViewCells[i]->mPvs.AddEntry(bvh, node);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | void VisibilitySolutionLoader::AllocateLeafViewCells(ViewCellsTree *viewCellsTree)
|
---|
161 | {
|
---|
162 | stack< pair<ViewCellsTreeNode *, AxisAlignedBox3> > nodeStack;
|
---|
163 |
|
---|
164 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>
|
---|
165 | (viewCellsTree->mRoot, viewCellsTree->mBox));
|
---|
166 |
|
---|
167 | int id = 0;
|
---|
168 |
|
---|
169 | mViewCells.clear();
|
---|
170 | int axes[] = {0, 0, 0};
|
---|
171 |
|
---|
172 | while (!nodeStack.empty())
|
---|
173 | {
|
---|
174 | ViewCellsTreeNode *node = nodeStack.top().first;
|
---|
175 |
|
---|
176 | if (node->IsLeaf())
|
---|
177 | {
|
---|
178 | if (!node->mViewCell)
|
---|
179 | {
|
---|
180 | node->mViewCell = new ViewCell();
|
---|
181 | node->mViewCell->SetId(id ++);
|
---|
182 | node->mViewCell->SetBox(nodeStack.top().second);
|
---|
183 | }
|
---|
184 |
|
---|
185 | mViewCells.push_back(node->mViewCell);
|
---|
186 |
|
---|
187 | nodeStack.pop();
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | AxisAlignedBox3 box = nodeStack.top().second;
|
---|
192 | nodeStack.pop();
|
---|
193 |
|
---|
194 | AxisAlignedBox3 newBox1 = box;
|
---|
195 | AxisAlignedBox3 newBox2 = box;
|
---|
196 | /*if (node->mPosition < newBox.Min(node->mAxis))
|
---|
197 | cout << "e: " << node->mPosition << " " << newBox.Min(node->mAxis) << endl;
|
---|
198 | else if (node->mPosition > newBox.Min(node->mAxis))
|
---|
199 | cout << "o: " << node->mPosition << " " << newBox.Min(node->mAxis) << endl;
|
---|
200 | */
|
---|
201 | newBox1.SetMin(node->mAxis, node->mPosition);
|
---|
202 | newBox2.SetMax(node->mAxis, node->mPosition);
|
---|
203 |
|
---|
204 | ++ axes[node->mAxis];
|
---|
205 |
|
---|
206 | if (node->mAxis == 1)
|
---|
207 | {
|
---|
208 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
|
---|
209 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
|
---|
210 | }
|
---|
211 | else
|
---|
212 | {
|
---|
213 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mFront, newBox1));
|
---|
214 | nodeStack.push(pair<ViewCellsTreeNode *, AxisAlignedBox3>(node->mBack, newBox2));
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | cout << "loaded " << id << " view cells" << endl;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | } |
---|