1 | #include <stack>
|
---|
2 | #include <time.h>
|
---|
3 | #include <iomanip>
|
---|
4 |
|
---|
5 | #include "ViewCell.h"
|
---|
6 | #include "Plane3.h"
|
---|
7 | #include "HierarchyManager.h"
|
---|
8 | #include "Mesh.h"
|
---|
9 | #include "common.h"
|
---|
10 | #include "Environment.h"
|
---|
11 | #include "Polygon3.h"
|
---|
12 | #include "Ray.h"
|
---|
13 | #include "AxisAlignedBox3.h"
|
---|
14 | #include "Exporter.h"
|
---|
15 | #include "Plane3.h"
|
---|
16 | #include "ViewCellsManager.h"
|
---|
17 | #include "Beam.h"
|
---|
18 | #include "KdTree.h"
|
---|
19 | #include "KdIntersectable.h"
|
---|
20 | #include "VspTree.h"
|
---|
21 | #include "OspTree.h"
|
---|
22 | #include "BvHierarchy.h"
|
---|
23 |
|
---|
24 |
|
---|
25 | namespace GtpVisibilityPreprocessor {
|
---|
26 |
|
---|
27 |
|
---|
28 | #define USE_FIXEDPOINT_T 0
|
---|
29 |
|
---|
30 |
|
---|
31 | /*******************************************************************/
|
---|
32 | /* class HierarchyManager implementation */
|
---|
33 | /*******************************************************************/
|
---|
34 |
|
---|
35 |
|
---|
36 | HierarchyManager::HierarchyManager(VspTree &vspTree, OspTree &ospTree)
|
---|
37 | //:mVspTree(vspTree), mOspTree(ospTree)
|
---|
38 | {
|
---|
39 | char subdivisionStatsLog[100];
|
---|
40 | Environment::GetSingleton()->GetStringValue("Hierarchy.subdivisionStats",
|
---|
41 | subdivisionStatsLog);
|
---|
42 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | SubdivisionCandidate *HierarchyManager::NextSubdivisionCandidate()
|
---|
47 | {
|
---|
48 | SubdivisionCandidate *splitCandidate = mTQueue.Top();
|
---|
49 | mTQueue.Pop();
|
---|
50 |
|
---|
51 | return splitCandidate;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | void HierarchyManager::PrepareConstruction(const VssRayContainer &sampleRays,
|
---|
56 | const ObjectContainer &objects,
|
---|
57 | AxisAlignedBox3 *forcedViewSpace,
|
---|
58 | RayInfoContainer &viewSpaceRays,
|
---|
59 | RayInfoContainer &objectSpaceRays)
|
---|
60 | {
|
---|
61 | SubdivisionCandidate *vsc =
|
---|
62 | mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, viewSpaceRays);
|
---|
63 | mTQueue.Push(vsc);
|
---|
64 |
|
---|
65 | SubdivisionCandidate *osc =
|
---|
66 | mOspTree->PrepareConstruction(sampleRays, objects, forcedViewSpace, objectSpaceRays);
|
---|
67 |
|
---|
68 | mTQueue.Push(osc);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | void HierarchyManager::EvalSubdivisionStats(const SubdivisionCandidate &tData)
|
---|
73 | {
|
---|
74 | const float costDecr = tData.GetRenderCostDecrease();
|
---|
75 |
|
---|
76 | //mTotalCost -= costDecr;
|
---|
77 | //mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
|
---|
78 |
|
---|
79 | AddSubdivisionStats(mOspTree->mOspStats.Leaves() + mVspTree->mVspStats.Leaves(),
|
---|
80 | costDecr,
|
---|
81 | mTotalCost
|
---|
82 | );
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | void HierarchyManager::AddSubdivisionStats(const int splits,
|
---|
87 | const float renderCostDecr,
|
---|
88 | const float totalRenderCost)
|
---|
89 | {
|
---|
90 | mSubdivisionStats
|
---|
91 | << "#Splits\n" << splits << endl
|
---|
92 | << "#RenderCostDecrease\n" << renderCostDecr << endl
|
---|
93 | << "#TotalRenderCost\n" << totalRenderCost << endl;
|
---|
94 | //<< "#AvgRenderCost\n" << avgRenderCost << endl;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | bool HierarchyManager::GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const
|
---|
99 | {
|
---|
100 | // TODO matt: make virtual by creating superclasss for traveral data
|
---|
101 | return candidate->GlobalTerminationCriteriaMet();
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | void HierarchyManager::Construct(const VssRayContainer &sampleRays,
|
---|
106 | const ObjectContainer &objects,
|
---|
107 | AxisAlignedBox3 *forcedViewSpace)
|
---|
108 | {
|
---|
109 | RayInfoContainer *objectSpaceRays = new RayInfoContainer();
|
---|
110 | RayInfoContainer *viewSpaceRays = new RayInfoContainer();
|
---|
111 |
|
---|
112 | // prepare vsp and osp trees for traversal
|
---|
113 | PrepareConstruction(sampleRays, objects, forcedViewSpace, *viewSpaceRays, *objectSpaceRays);
|
---|
114 |
|
---|
115 | mVspTree->mVspStats.Reset();
|
---|
116 | mVspTree->mVspStats.Start();
|
---|
117 |
|
---|
118 | cout << "Constructing view space / object space tree ... \n";
|
---|
119 | const long startTime = GetTime();
|
---|
120 |
|
---|
121 | RunConstruction(true);
|
---|
122 |
|
---|
123 | cout << "finished in " << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl;
|
---|
124 |
|
---|
125 | mVspTree->mVspStats.Stop();
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | bool HierarchyManager::SubdivideSubdivisionCandidate(SubdivisionCandidate *sc)
|
---|
130 | {
|
---|
131 | const bool globalTerminationCriteriaMet =
|
---|
132 | GlobalTerminationCriteriaMet(sc);
|
---|
133 |
|
---|
134 | const bool vspSplit = (sc->Type() == SubdivisionCandidate::VIEW_SPACE);
|
---|
135 |
|
---|
136 | if (vspSplit)
|
---|
137 | {
|
---|
138 | VspNode *n = mVspTree->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | KdNode *n = mOspTree->Subdivide(mTQueue, sc, globalTerminationCriteriaMet);
|
---|
143 | }
|
---|
144 |
|
---|
145 | return !globalTerminationCriteriaMet;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | void HierarchyManager::RunConstruction(const bool repair)
|
---|
150 | {
|
---|
151 | int numNodes = 0;
|
---|
152 |
|
---|
153 | while (!FinishedConstruction())
|
---|
154 | {
|
---|
155 | SubdivisionCandidate *splitCandidate = NextSubdivisionCandidate();
|
---|
156 |
|
---|
157 | mTotalCost -= splitCandidate->GetRenderCostDecrease();
|
---|
158 |
|
---|
159 | // cost ratio of cost decrease / totalCost
|
---|
160 | const float costRatio = splitCandidate->GetRenderCostDecrease() / mTotalCost;
|
---|
161 |
|
---|
162 | if (costRatio < mTermMinGlobalCostRatio)
|
---|
163 | ++ mGlobalCostMisses;
|
---|
164 |
|
---|
165 | /*Debug << "\n**********" << endl
|
---|
166 | << "total cost: " << mTotalCost << " render cost decr: "
|
---|
167 | << splitCandidate->GetRenderCostDecrease()
|
---|
168 | << " cost ratio: " << costRatio << endl << endl;*/
|
---|
169 |
|
---|
170 | //-- subdivide leaf node
|
---|
171 |
|
---|
172 | if (SubdivideSubdivisionCandidate(splitCandidate))
|
---|
173 | {
|
---|
174 | // subdivision successful
|
---|
175 | EvalSubdivisionStats(*splitCandidate);
|
---|
176 |
|
---|
177 | // reevaluate candidates affected by the split
|
---|
178 | // for view space splits, this would be object space splits
|
---|
179 | // and other way round
|
---|
180 | if (repair)
|
---|
181 | RepairQueue();
|
---|
182 |
|
---|
183 | cout << "candidate: " << splitCandidate->Type() << ", priority: " << splitCandidate->GetPriority() << endl;
|
---|
184 | }
|
---|
185 |
|
---|
186 | DEL_PTR(splitCandidate);
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | void HierarchyManager::Construct2(const VssRayContainer &sampleRays,
|
---|
192 | const ObjectContainer &objects,
|
---|
193 | AxisAlignedBox3 *forcedViewSpace)
|
---|
194 | {
|
---|
195 | // rays clipped in view space and in object space
|
---|
196 | RayInfoContainer *viewSpaceRays = new RayInfoContainer();
|
---|
197 | RayInfoContainer *objectSpaceRays = new RayInfoContainer();
|
---|
198 |
|
---|
199 |
|
---|
200 | /////////////////////////////////////////////////////////////
|
---|
201 | // view space space partition
|
---|
202 | /////////////////////////////////////////////////////////////
|
---|
203 |
|
---|
204 | #if 0
|
---|
205 | // makes no sense otherwise because only one kd cell available
|
---|
206 | // during view space partition
|
---|
207 | const bool savedCountMethod = mVspTree.mUseKdPvsForHeuristics;
|
---|
208 | const bool savedStoreMethod = mVspTree.mStoreObjectPvs;
|
---|
209 |
|
---|
210 | mVspTree.mUseKdPvsForHeuristics = false;
|
---|
211 | mVspTree.mStoreObjectPvs = false;
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | SubdivisionCandidate *vsc =
|
---|
215 | mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, *viewSpaceRays);
|
---|
216 |
|
---|
217 | // add to queue
|
---|
218 | mTQueue.Push(vsc);
|
---|
219 |
|
---|
220 | long startTime = GetTime();
|
---|
221 | cout << "starting vsp contruction ... " << endl;
|
---|
222 |
|
---|
223 | mVspTree->mVspStats.Reset();
|
---|
224 | mVspTree->mVspStats.Start();
|
---|
225 |
|
---|
226 | int i = 0;
|
---|
227 |
|
---|
228 | // all objects can be seen from everywhere
|
---|
229 | mTotalCost = (float)dynamic_cast<VspTree::VspSubdivisionCandidate *>(vsc)->mParentData.mPvs;
|
---|
230 |
|
---|
231 | const bool repairQueue = false;
|
---|
232 |
|
---|
233 | // process view space candidates
|
---|
234 | RunConstruction(repairQueue);
|
---|
235 |
|
---|
236 | cout << "finished in " << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl;
|
---|
237 | mVspTree->mVspStats.Stop();
|
---|
238 |
|
---|
239 |
|
---|
240 | /////////////////////////////////////////////////////////////
|
---|
241 | // object space partition
|
---|
242 | /////////////////////////////////////////////////////////////
|
---|
243 |
|
---|
244 |
|
---|
245 | Debug << "\n$$$$$$$$$ osp tree construction $$$$$$$$$$\n" << endl;
|
---|
246 | cout << "starting osp contruction ... " << endl;
|
---|
247 |
|
---|
248 | // start with one big kd cell - all objects can be seen from everywhere
|
---|
249 | // note: only true for view space = object space
|
---|
250 |
|
---|
251 | // compute first candidate
|
---|
252 | SubdivisionCandidate *osc =
|
---|
253 | mOspTree->PrepareConstruction(sampleRays, objects, forcedViewSpace, *objectSpaceRays);
|
---|
254 |
|
---|
255 | Debug << "reseting cost, new total cost: " << mTotalCost << endl;
|
---|
256 | mTotalCost = mOspTree->mTotalCost;
|
---|
257 |
|
---|
258 | mTQueue.Push(osc);
|
---|
259 |
|
---|
260 | mOspTree->mOspStats.Reset();
|
---|
261 | mOspTree->mOspStats.Start();
|
---|
262 |
|
---|
263 | startTime = GetTime();
|
---|
264 |
|
---|
265 | // process object space candidates
|
---|
266 | RunConstruction(repairQueue);
|
---|
267 |
|
---|
268 | cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
269 |
|
---|
270 | mOspTree->mOspStats.Stop();
|
---|
271 |
|
---|
272 | float rc = mOspTree->EvalRenderCost(sampleRays);
|
---|
273 |
|
---|
274 | Debug << "here47 My render cost evalulation: " << rc << endl;
|
---|
275 |
|
---|
276 | #if 0
|
---|
277 | // reset parameters
|
---|
278 | mVspTree->mUseKdPvsForHeuristics = savedCountMethod;
|
---|
279 | mVspTree->mStoreObjectPvs = savedStoreMethod;
|
---|
280 | #endif
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | void HierarchyManager::Construct3(const VssRayContainer &sampleRays,
|
---|
285 | const ObjectContainer &objects,
|
---|
286 | AxisAlignedBox3 *forcedViewSpace)
|
---|
287 | {
|
---|
288 | // construct only view space partition
|
---|
289 | // object kd tree is taken for osp
|
---|
290 |
|
---|
291 | mVspTree->mVspStats.Reset();
|
---|
292 | mVspTree->mVspStats.Start();
|
---|
293 |
|
---|
294 | RayInfoContainer *viewSpaceRays = new RayInfoContainer();
|
---|
295 |
|
---|
296 | SubdivisionCandidate *sc =
|
---|
297 | mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, *viewSpaceRays);
|
---|
298 |
|
---|
299 | mTQueue.Push(sc);
|
---|
300 |
|
---|
301 | cout << "starting vsp contruction ... " << endl;
|
---|
302 |
|
---|
303 | long startTime = GetTime();
|
---|
304 |
|
---|
305 | RunConstruction(false);
|
---|
306 |
|
---|
307 | cout << "finished in " << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl;
|
---|
308 | mVspTree->mVspStats.Stop();
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | bool HierarchyManager::FinishedConstruction() const
|
---|
313 | {
|
---|
314 | return mTQueue.Empty();
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | void HierarchyManager::CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList)
|
---|
319 | {
|
---|
320 | switch (mObjectSpaceSubdivisonType)
|
---|
321 | {
|
---|
322 | case KD_BASED_OBJ_SUBDIV:
|
---|
323 | {
|
---|
324 | OspTree::OspSubdivisionCandidate *sc =
|
---|
325 | dynamic_cast<OspTree::OspSubdivisionCandidate *>(mCurrentCandidate);
|
---|
326 |
|
---|
327 | mOspTree->CollectDirtyCandidates(sc, dirtyList);
|
---|
328 | break;
|
---|
329 | }
|
---|
330 | case BV_BASED_OBJ_SUBDIV:
|
---|
331 | {
|
---|
332 | BvHierarchy::BvhSubdivisionCandidate *sc =
|
---|
333 | dynamic_cast<BvHierarchy::BvhSubdivisionCandidate *>(mCurrentCandidate);
|
---|
334 |
|
---|
335 | mBvHierarchy->CollectDirtyCandidates(sc, dirtyList);
|
---|
336 | break;
|
---|
337 | }
|
---|
338 | default:
|
---|
339 | break;
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | void HierarchyManager::CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList)
|
---|
345 | {
|
---|
346 | VspTree::VspSubdivisionCandidate *sc =
|
---|
347 | dynamic_cast<VspTree::VspSubdivisionCandidate *>(mCurrentCandidate);
|
---|
348 |
|
---|
349 | mVspTree->CollectDirtyCandidates(sc, dirtyList);
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | void HierarchyManager::CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList)
|
---|
354 | {
|
---|
355 | // we have either a object space or view space split
|
---|
356 | if (mCurrentCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
|
---|
357 | {
|
---|
358 | CollectViewSpaceDirtyList(dirtyList);
|
---|
359 | }
|
---|
360 | else // object space split
|
---|
361 | {
|
---|
362 | CollectObjectSpaceDirtyList(dirtyList);
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | void HierarchyManager::RepairQueue()
|
---|
368 | {
|
---|
369 | // TODO
|
---|
370 | // for each update of the view space partition:
|
---|
371 | // the candidates from object space partition which
|
---|
372 | // have been afected by the view space split (the kd split candidates
|
---|
373 | // which saw the view cell which was split) must be reevaluated
|
---|
374 | // (maybe not locally, just reinsert them into the queue)
|
---|
375 | //
|
---|
376 | // vice versa for the view cells
|
---|
377 | // for each update of the object space partition
|
---|
378 | // reevaluate split candidate for view cells which saw the split kd cell
|
---|
379 | //
|
---|
380 | // the priority queue update can be solved by implementing a binary heap
|
---|
381 | // (explicit data structure, binary tree)
|
---|
382 | // *) inserting and removal is efficient
|
---|
383 | // *) search is not efficient => store queue position with each
|
---|
384 | // split candidate
|
---|
385 |
|
---|
386 | // collect list of "dirty" candidates
|
---|
387 | vector<SubdivisionCandidate *> dirtyList;
|
---|
388 | CollectDirtyCandidates(dirtyList);
|
---|
389 |
|
---|
390 | //-- reevaluate the dirty list
|
---|
391 | vector<SubdivisionCandidate *>::const_iterator sit, sit_end = dirtyList.end();
|
---|
392 |
|
---|
393 | for (sit = dirtyList.begin(); sit != sit_end; ++ sit)
|
---|
394 | {
|
---|
395 | SubdivisionCandidate* sc = *sit;
|
---|
396 |
|
---|
397 | mTQueue.Erase(sc);
|
---|
398 |
|
---|
399 | // reevaluate
|
---|
400 | sc->EvalPriority();
|
---|
401 |
|
---|
402 | // reinsert
|
---|
403 | mTQueue.Push(sc);
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | } |
---|