1 | #ifndef _ViewCell_H__
|
---|
2 | #define _ViewCell_H__
|
---|
3 |
|
---|
4 | #include "Mesh.h"
|
---|
5 | #include "Containers.h"
|
---|
6 | #include "Ray.h"
|
---|
7 | #include "Statistics.h"
|
---|
8 | #include "Material.h"
|
---|
9 | #include "gzstream.h"
|
---|
10 | #include "ObjectPvs.h"
|
---|
11 |
|
---|
12 | namespace GtpVisibilityPreprocessor {
|
---|
13 |
|
---|
14 | struct Triangle3;
|
---|
15 |
|
---|
16 | class BspInterior;
|
---|
17 | class BspPvs;
|
---|
18 | class BspLeaf;
|
---|
19 | class VspLeaf;
|
---|
20 | class KdLeaf;
|
---|
21 | class ViewCellInterior;
|
---|
22 | class MergeCandidate;
|
---|
23 | class ViewCellsManager;
|
---|
24 | class ViewCellLeaf;
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | /** Statistics for a view cell partition.
|
---|
29 | */
|
---|
30 | class ViewCellsStatistics: public StatisticsBase
|
---|
31 | {
|
---|
32 | public:
|
---|
33 |
|
---|
34 | /// number of view cells
|
---|
35 | int viewCells;
|
---|
36 | /// cost of the PVS
|
---|
37 | float pvsCost;
|
---|
38 | /// largest PVS of all view cells
|
---|
39 | float maxPvs;
|
---|
40 | /// smallest PVS of all view cells
|
---|
41 | float minPvs;
|
---|
42 | /// view cells with empty PVS
|
---|
43 | int emptyPvs;
|
---|
44 | /// number of leaves covering the view space
|
---|
45 | int leaves;
|
---|
46 | /// largest number of leaves covered by one view cell
|
---|
47 | int maxLeaves;
|
---|
48 | /// number of invalid view cells
|
---|
49 | int invalid;
|
---|
50 |
|
---|
51 | // Constructor
|
---|
52 | ViewCellsStatistics()
|
---|
53 | {
|
---|
54 | Reset();
|
---|
55 | }
|
---|
56 |
|
---|
57 | double AvgLeaves() const {return (double)leaves / (double)viewCells;};
|
---|
58 | double AvgPvs() const {return (double)pvsCost / (double)viewCells;};
|
---|
59 |
|
---|
60 | void Reset()
|
---|
61 | {
|
---|
62 | viewCells = 0;
|
---|
63 | pvsCost = 0;
|
---|
64 | maxPvs = 0;
|
---|
65 |
|
---|
66 | minPvs = 999999;
|
---|
67 | emptyPvs = 0;
|
---|
68 | leaves = 0;
|
---|
69 | maxLeaves = 0;
|
---|
70 | invalid = 0;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void Print(std::ostream &app) const;
|
---|
74 |
|
---|
75 | friend std::ostream &operator<<(std::ostream &s, const ViewCellsStatistics &stat)
|
---|
76 | {
|
---|
77 | stat.Print(s);
|
---|
78 | return s;
|
---|
79 | }
|
---|
80 | };
|
---|
81 |
|
---|
82 |
|
---|
83 | /** Statistics for a view cells tree.
|
---|
84 | */
|
---|
85 | class ViewCellsTreeStats
|
---|
86 | {
|
---|
87 | public:
|
---|
88 | int mPass;
|
---|
89 |
|
---|
90 | int mNumViewCells;
|
---|
91 |
|
---|
92 | float mRenderCostDecrease;
|
---|
93 |
|
---|
94 | float mTotalRenderCost;
|
---|
95 |
|
---|
96 | float mCurrentPvsCost;
|
---|
97 |
|
---|
98 | float mExpectedCost;
|
---|
99 |
|
---|
100 | float mAvgRenderCost;
|
---|
101 |
|
---|
102 | float mDeviation;
|
---|
103 |
|
---|
104 | float mTotalPvsCost;
|
---|
105 |
|
---|
106 | int mEntriesInPvs;
|
---|
107 |
|
---|
108 | float mMemoryCost;
|
---|
109 |
|
---|
110 | int mPvsSizeDecr;
|
---|
111 |
|
---|
112 | float mVolume;
|
---|
113 |
|
---|
114 |
|
---|
115 | void Reset()
|
---|
116 | {
|
---|
117 | mPass = 0;
|
---|
118 | mNumViewCells = 0;
|
---|
119 | mRenderCostDecrease = 0;
|
---|
120 | mTotalRenderCost = 0;
|
---|
121 | mCurrentPvsCost = 0;
|
---|
122 | mExpectedCost = 0;
|
---|
123 | mAvgRenderCost = 0;
|
---|
124 | mDeviation = 0;
|
---|
125 | mTotalPvsCost = 0;
|
---|
126 | mEntriesInPvs = 0;
|
---|
127 | mMemoryCost = 0;
|
---|
128 | mPvsSizeDecr = 0;
|
---|
129 | mVolume = 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | void Print(std::ostream &app) const;
|
---|
134 |
|
---|
135 | friend std::ostream &operator<<(std::ostream &s, const ViewCellsTreeStats &stat)
|
---|
136 | {
|
---|
137 | stat.Print(s);
|
---|
138 | return s;
|
---|
139 | }
|
---|
140 | };
|
---|
141 |
|
---|
142 |
|
---|
143 | /** This class represents a view cell, which is a region in view space.
|
---|
144 | The objects seen by the view cell are stored in a pvs.
|
---|
145 | A view cell can be represented in many different ways, e.g., as a mesh.
|
---|
146 | */
|
---|
147 | class ViewCell: public MeshInstance
|
---|
148 | {
|
---|
149 | friend class ViewCellsTree;
|
---|
150 | friend class ViewCellsManager;
|
---|
151 |
|
---|
152 | public:
|
---|
153 |
|
---|
154 | ViewCell();
|
---|
155 | /** Constructor taking a mesh representing the shape of the viewcell.
|
---|
156 | */
|
---|
157 | ViewCell(Mesh *mesh);
|
---|
158 | /** Default destructor.
|
---|
159 | */
|
---|
160 | virtual ~ViewCell();
|
---|
161 | /** Returns const reference to pvs.
|
---|
162 | */
|
---|
163 | const ObjectPvs &GetPvs() const;
|
---|
164 | /** Returns reference to pvs.
|
---|
165 | */
|
---|
166 | ObjectPvs &GetPvs();
|
---|
167 | /** Completely substitutes the pvs.
|
---|
168 | */
|
---|
169 | void SetPvs(const ObjectPvs &pvs);
|
---|
170 | /** Type of view cell.
|
---|
171 | */
|
---|
172 | int Type() const;
|
---|
173 | /** Adds a passing ray to the passing ray container.
|
---|
174 | */
|
---|
175 | void AddPassingRay(const Ray &ray, const int contributions);
|
---|
176 | /** Returns volume of the view cell.
|
---|
177 | */
|
---|
178 | inline float GetVolume() const
|
---|
179 | {
|
---|
180 | return mVolume;
|
---|
181 | }
|
---|
182 | /** Sets the volume of the view cell.
|
---|
183 | */
|
---|
184 | inline void SetVolume(float volume)
|
---|
185 | {
|
---|
186 | mVolume = volume;
|
---|
187 | }
|
---|
188 | /** Returns area of the view cell.
|
---|
189 | */
|
---|
190 | inline float GetArea() const
|
---|
191 | {
|
---|
192 | return mArea;
|
---|
193 | }
|
---|
194 | /** Sets the area of the view cell.
|
---|
195 | */
|
---|
196 | void SetArea(float area)
|
---|
197 | {
|
---|
198 | mArea = area;
|
---|
199 | }
|
---|
200 | /** if this view cell is the root of a view cell hierarchy
|
---|
201 | */
|
---|
202 | bool IsRoot() const;
|
---|
203 | /** Returns parent view cell.
|
---|
204 | */
|
---|
205 | ViewCellInterior *GetParent() const;
|
---|
206 | /** Sets parent of this view cell.
|
---|
207 | */
|
---|
208 | void SetParent(ViewCellInterior *parent);
|
---|
209 | /** Sets the mesh for this view cell.
|
---|
210 | */
|
---|
211 | void SetMesh(Mesh *mesh);
|
---|
212 | /** Sets this view cell to be a valid view cell according to some criteria.
|
---|
213 | */
|
---|
214 | void SetValid(const bool valid);
|
---|
215 | /** Returns true if this view cell is considered to be valid according to
|
---|
216 | some criteria.
|
---|
217 | */
|
---|
218 | bool GetValid() const;
|
---|
219 | /** Returns estimated render cost of this view cell.
|
---|
220 | */
|
---|
221 | float GetRenderCost() const;
|
---|
222 | /** set color for visiualizations.
|
---|
223 | */
|
---|
224 | void SetColor(const RgbColor &color);
|
---|
225 | /** get color for visualuzations.
|
---|
226 | */
|
---|
227 | RgbColor GetColor() const;
|
---|
228 | /** Adds a sample to the pvs.
|
---|
229 | @param sample the sample to be added
|
---|
230 | @param pdf a continuos measure of visibility
|
---|
231 | @param contribution returns the contribution of this sample to the pvs
|
---|
232 | */
|
---|
233 | bool AddPvsSample(Intersectable *sample, const float pdf, float &contribution);
|
---|
234 | /** if this is a view cell correspending to a leaf in a hierarchy.
|
---|
235 | */
|
---|
236 | virtual bool IsLeaf() const = 0;
|
---|
237 |
|
---|
238 | friend inline bool SmallerPvs(const ViewCell *a, const ViewCell *b);
|
---|
239 | friend inline bool GreaterOrEqualPvs(const ViewCell *a, const ViewCell *b);
|
---|
240 | friend inline bool SmallerRenderCost(const ViewCell *a, const ViewCell *b);
|
---|
241 | friend inline bool LargerRenderCost(const ViewCell *a, const ViewCell *b);
|
---|
242 |
|
---|
243 |
|
---|
244 | /** Sets merge cost used for merging this view cell from other cells.
|
---|
245 | @hack The function is available for leaves also to have a common interface,
|
---|
246 | but it should be less than zero for leaves.
|
---|
247 | */
|
---|
248 | void SetMergeCost(const float mergeCost);
|
---|
249 | /** Returns merge cost needed to merge this leaf from other cells.
|
---|
250 | @hack The function is available for leaves also to have a common interface,
|
---|
251 | but it should be less than zero for leaves.
|
---|
252 | */
|
---|
253 | float GetMergeCost() const;
|
---|
254 |
|
---|
255 | void UpdatePvsCost() {
|
---|
256 | mPvsCost = GetPvs().EvalPvsCost();
|
---|
257 | }
|
---|
258 |
|
---|
259 | void SetTrianglesInPvs(const float c) {
|
---|
260 | mPvsCost = c;
|
---|
261 | }
|
---|
262 |
|
---|
263 | void SetEntriesInPvs(const int e) {
|
---|
264 | mEntriesInPvs = e;
|
---|
265 | }
|
---|
266 |
|
---|
267 | float GetTrianglesInPvs() const {
|
---|
268 | return mPvsCost;
|
---|
269 | }
|
---|
270 |
|
---|
271 | int GetEntriesInPvs() const {
|
---|
272 | return mEntriesInPvs;
|
---|
273 | }
|
---|
274 |
|
---|
275 | int GetFilteredPvsSize() const
|
---|
276 | {
|
---|
277 | return mFilteredPvsSize;
|
---|
278 | }
|
---|
279 |
|
---|
280 | void SetFilteredPvsSize(const int s) {
|
---|
281 | mFilteredPvsSize = s;
|
---|
282 | }
|
---|
283 |
|
---|
284 | //virtual int ViewCellType;
|
---|
285 |
|
---|
286 | protected:
|
---|
287 |
|
---|
288 | /// parent view cell in the view cell hierarchy
|
---|
289 | ViewCellInterior *mParent;
|
---|
290 | /// the potentially visible objects
|
---|
291 | ObjectPvs mPvs;
|
---|
292 | /// the volume of this view cell
|
---|
293 | float mVolume;
|
---|
294 | /// the area of this view cell
|
---|
295 | float mArea;
|
---|
296 | /// the cost that were paid for merging this view cells from two others.
|
---|
297 | float mMergeCost;
|
---|
298 | /// if the view cell is valid view space
|
---|
299 | bool mValid;
|
---|
300 | /// color used for consistent visualization
|
---|
301 | RgbColor mColor;
|
---|
302 | /// store pvs size, used for evaluation purpose when pvss are stored only in the leaves
|
---|
303 | float mPvsCost;
|
---|
304 | /// stores number of entries in pvs
|
---|
305 | int mEntriesInPvs;
|
---|
306 | /** if the pvs size scalar (+ entries into pvs)
|
---|
307 | is up to date and corresponding to the real pvs size
|
---|
308 | */
|
---|
309 | bool mPvsSizeValid;
|
---|
310 |
|
---|
311 | /// Filter cost of the pvs
|
---|
312 | int mFilteredPvsSize;
|
---|
313 |
|
---|
314 | };
|
---|
315 |
|
---|
316 | inline bool SmallerPvs(const ViewCell *a, const ViewCell *b)
|
---|
317 | {
|
---|
318 | // HACK: take scalar value because pvs may not have been stored properly
|
---|
319 | #if 1
|
---|
320 | return a->mPvsCost < b->mPvsCost;
|
---|
321 | #else
|
---|
322 | return a->GetPvs().EvalPvsCost() < b->GetPvs().EvalPvsCost();
|
---|
323 | #endif
|
---|
324 | }
|
---|
325 |
|
---|
326 | inline bool GreaterOrEqualPvs(const ViewCell *a, const ViewCell *b)
|
---|
327 | {
|
---|
328 | return !SmallerPvs(a, b);
|
---|
329 | }
|
---|
330 |
|
---|
331 | inline bool SmallerRenderCost(const ViewCell *a, const ViewCell *b)
|
---|
332 | {
|
---|
333 | return a->GetRenderCost() < b->GetRenderCost();
|
---|
334 | }
|
---|
335 |
|
---|
336 | inline bool LargerRenderCost(const ViewCell *a, const ViewCell *b)
|
---|
337 | {
|
---|
338 | return a->GetRenderCost() > b->GetRenderCost();
|
---|
339 | }
|
---|
340 |
|
---|
341 | class ViewCellInterior: public ViewCell
|
---|
342 | {
|
---|
343 | friend class ViewCellsManager;
|
---|
344 |
|
---|
345 | public:
|
---|
346 | ViewCellInterior();
|
---|
347 | ~ViewCellInterior();
|
---|
348 |
|
---|
349 | ViewCellInterior(Mesh *mesh);
|
---|
350 |
|
---|
351 | /** Sets pointer from parent to child and vice versa.
|
---|
352 | */
|
---|
353 | void SetupChildLink(ViewCell *l);
|
---|
354 | void ReplaceChildLink(ViewCell *prev, ViewCell *cur);
|
---|
355 |
|
---|
356 | void RemoveChildLink(ViewCell *l);
|
---|
357 | bool IsLeaf() const;
|
---|
358 |
|
---|
359 | void SetCost(const float c) {
|
---|
360 | mCost = c;
|
---|
361 | }
|
---|
362 |
|
---|
363 | float GetCost() const {
|
---|
364 | return mCost;
|
---|
365 | }
|
---|
366 |
|
---|
367 | ViewCellContainer mChildren;
|
---|
368 |
|
---|
369 | protected:
|
---|
370 |
|
---|
371 | /// Pverall cost resulting from the merge.
|
---|
372 | float mCost;
|
---|
373 | };
|
---|
374 |
|
---|
375 |
|
---|
376 | /**
|
---|
377 | Leaf of the view cell.
|
---|
378 | */
|
---|
379 | class ViewCellLeaf: public ViewCell
|
---|
380 | {
|
---|
381 | public:
|
---|
382 |
|
---|
383 | ViewCellLeaf()
|
---|
384 | {
|
---|
385 | mActiveViewCell = this;
|
---|
386 | }
|
---|
387 |
|
---|
388 | ViewCellLeaf(Mesh *mesh): ViewCell(mesh)
|
---|
389 | {
|
---|
390 | mActiveViewCell = this;
|
---|
391 | }
|
---|
392 |
|
---|
393 | bool IsLeaf() const
|
---|
394 | {
|
---|
395 | return true;
|
---|
396 | }
|
---|
397 |
|
---|
398 | /** Returns active view cell, i.e. this view cell or
|
---|
399 | a parent view cell which is set as active view cell.
|
---|
400 | */
|
---|
401 | ViewCell *GetActiveViewCell() const
|
---|
402 | { return mActiveViewCell; }
|
---|
403 |
|
---|
404 | /** Sets this view cell to be an active view cell.
|
---|
405 | */
|
---|
406 | void SetActiveViewCell(ViewCell *vc)
|
---|
407 | { mActiveViewCell = vc;}
|
---|
408 |
|
---|
409 | /** points to the currently active view cell. This is the
|
---|
410 | view cell representing the current brach.
|
---|
411 | */
|
---|
412 | ViewCell *mActiveViewCell;
|
---|
413 | };
|
---|
414 |
|
---|
415 |
|
---|
416 | /** Leaf of the view cell hierarchy corresponding
|
---|
417 | to a leaf in a spatial hierarchy.
|
---|
418 | */
|
---|
419 | template<typename T>
|
---|
420 | class HierarchyLeafViewCell: public ViewCellLeaf
|
---|
421 | {
|
---|
422 | public:
|
---|
423 |
|
---|
424 | HierarchyLeafViewCell<T>(): ViewCellLeaf() { }
|
---|
425 | HierarchyLeafViewCell<T>(Mesh *mesh):
|
---|
426 | ViewCellLeaf(mesh) { }
|
---|
427 |
|
---|
428 | bool IsLeaf() const
|
---|
429 | {
|
---|
430 | return true;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /// Leaves of some hierarchy which contains this view cell.
|
---|
434 | vector<T> mLeaves;
|
---|
435 | };
|
---|
436 |
|
---|
437 |
|
---|
438 | typedef HierarchyLeafViewCell<VspLeaf *> VspViewCell;
|
---|
439 | typedef HierarchyLeafViewCell<BspLeaf *> BspViewCell;
|
---|
440 | typedef HierarchyLeafViewCell<KdLeaf *> KdViewCell;
|
---|
441 |
|
---|
442 |
|
---|
443 |
|
---|
444 |
|
---|
445 | class ViewCellsTree
|
---|
446 | {
|
---|
447 | friend class ViewCellsManager;
|
---|
448 | friend class ViewCellsParseHandlers;
|
---|
449 |
|
---|
450 | public:
|
---|
451 |
|
---|
452 | /** Default constructor.
|
---|
453 | */
|
---|
454 | ViewCellsTree();
|
---|
455 | /** View cells tree constructor taking a view cell mnanager as parameter
|
---|
456 | */
|
---|
457 | ViewCellsTree(ViewCellsManager *vcm);
|
---|
458 | /** Destructor.
|
---|
459 | */
|
---|
460 | ~ViewCellsTree();
|
---|
461 | /** Returns number of leaves this view cell consists of.
|
---|
462 | */
|
---|
463 | int GetNumInitialViewCells(ViewCell *vc) const;
|
---|
464 | /** Collects leaves corresponding to a view cell.
|
---|
465 | */
|
---|
466 | void CollectLeaves(ViewCell *vc, ViewCellContainer &leaves) const;
|
---|
467 | /** Merges view cells according to some cost heuristics.
|
---|
468 | */
|
---|
469 | int ConstructMergeTree(const VssRayContainer &rays, const ObjectContainer &objects);
|
---|
470 | /** Refines view cells using shuffling, i.e., border leaves
|
---|
471 | of two view cells are exchanged if the resulting view cells
|
---|
472 | are tested to be "better" than the old ones.
|
---|
473 | @returns number of refined view cells
|
---|
474 | */
|
---|
475 | int RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
|
---|
476 | /** Assign colors to the viewcells so that they can be renderered interactively without
|
---|
477 | color flickering.
|
---|
478 | */
|
---|
479 | void AssignRandomColors();
|
---|
480 | /** Updates view cell stats for this particular view cell.
|
---|
481 | */
|
---|
482 | void UpdateViewCellsStats(ViewCell *vc, ViewCellsStatistics &vcStat);
|
---|
483 | /** Get costs resulting from each merge step.
|
---|
484 | */
|
---|
485 | void GetCostFunction(vector<float> &costFunction);
|
---|
486 | /** Returns storage cost resulting from each merge step.
|
---|
487 | */
|
---|
488 | void GetStorageFunction(vector<int> &storageCost);
|
---|
489 | /** Returns optimal set of view cells for a given number of view cells.
|
---|
490 | */
|
---|
491 | void CollectBestViewCellSet(ViewCellContainer &viewCells, const int numViewCells);
|
---|
492 | /** Root of view cells tree.
|
---|
493 | */
|
---|
494 | ViewCell *GetRoot() const;
|
---|
495 | /** Returns pvs of view cell.
|
---|
496 | @note pvs is returned per reference if tree is not compressed,
|
---|
497 | per copy else.
|
---|
498 | */
|
---|
499 | void GetPvs(ViewCell *vc, ObjectPvs &pvs) const;
|
---|
500 | /** Returns pvs size (i.e. the render cost of the stored objects)
|
---|
501 | */
|
---|
502 | float GetTrianglesInPvs(ViewCell *vc) const;
|
---|
503 | /** Returns number of entries associated with this view cell.
|
---|
504 |
|
---|
505 | This returns the same value as the "GetPvsSize" function for object pvs
|
---|
506 | but most likely different values if we use object space grouping.
|
---|
507 | E.g., using bounding volumes.
|
---|
508 | */
|
---|
509 | int GetPvsEntries(ViewCell *vc) const;
|
---|
510 | /** Returns the number of physically stored entries in the view cells sub tree.
|
---|
511 | This can vary based on the current storage method
|
---|
512 | */
|
---|
513 | int CountStoredPvsEntries(ViewCell *root) const;
|
---|
514 | /** Returns memory cost of this view cell.
|
---|
515 | */
|
---|
516 | float GetMemoryCost(ViewCell *vc) const;
|
---|
517 | /** Sets method of storage for view cells.
|
---|
518 | */
|
---|
519 | void SetViewCellsStorage(int type);
|
---|
520 | /** pvs storage methods
|
---|
521 | */
|
---|
522 | enum {PVS_IN_INTERIORS, COMPRESSED, PVS_IN_LEAVES};
|
---|
523 | /** If view cells in this tree have compressed pvs.
|
---|
524 | */
|
---|
525 | int ViewCellsStorage() const;
|
---|
526 | /** Returns active view cell that is in the path of this view cell.
|
---|
527 | */
|
---|
528 | ViewCell *GetActiveViewCell(ViewCellLeaf *vc) const;
|
---|
529 | /** Sets the leaves to be the currently active view cells.
|
---|
530 | */
|
---|
531 | void SetActiveSetToLeaves();
|
---|
532 | /** Propagates pvs up the tree to the root and downwards the tree.
|
---|
533 | */
|
---|
534 | void PropagatePvs(ViewCell *vc);
|
---|
535 | /** Exports view cells to file.
|
---|
536 | */
|
---|
537 | bool Export(OUT_STREAM &stream, const bool exportPvs = false);
|
---|
538 | /** Export statistics of this view cell tree.
|
---|
539 | */
|
---|
540 | void ExportStats(const std::string &mergeStats);
|
---|
541 | /** Sets root of hierarchy.
|
---|
542 | */
|
---|
543 | void SetRoot(ViewCell *root);
|
---|
544 | /** Assignes unique ids to view cells.
|
---|
545 | */
|
---|
546 | void CreateUniqueViewCellsIds();
|
---|
547 | /** Resets pvs of whole tree.
|
---|
548 | */
|
---|
549 | void ResetPvs();
|
---|
550 | /** Counts pvs of the view cell taking the kd cells into account.
|
---|
551 | */
|
---|
552 | int CountKdPvs(const ViewCellLeaf *vc) const;
|
---|
553 | /** Sets pointer to view cells manager.
|
---|
554 | */
|
---|
555 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
556 |
|
---|
557 | ViewCellInterior *ImportBinInterior(IN_STREAM &stream, ViewCellInterior *parent);
|
---|
558 |
|
---|
559 | ViewCellLeaf *ImportBinLeaf(IN_STREAM &stream,
|
---|
560 | ViewCellInterior *parent,
|
---|
561 | const ObjectContainer &pvsObjects);
|
---|
562 |
|
---|
563 | void ExportBinInterior(OUT_STREAM &stream, ViewCellInterior *interior);
|
---|
564 |
|
---|
565 | void ExportBinLeaf(OUT_STREAM &stream, ViewCell *leaf);
|
---|
566 |
|
---|
567 | bool ExportBinary(OUT_STREAM &stream);
|
---|
568 |
|
---|
569 | bool ImportBinary(IN_STREAM &stream, const ObjectContainer &pvsObjects);
|
---|
570 |
|
---|
571 | ViewCell *ImportNextNode(IN_STREAM &stream,
|
---|
572 | ViewCellInterior *parent,
|
---|
573 | const ObjectContainer &objects);
|
---|
574 |
|
---|
575 | protected:
|
---|
576 |
|
---|
577 | /** Reads the environment and sets member variables.
|
---|
578 | */
|
---|
579 | void ReadEnvironment();
|
---|
580 |
|
---|
581 |
|
---|
582 | //////////////////////////////////////////////////////////////
|
---|
583 | // merge related stuff //
|
---|
584 | //////////////////////////////////////////////////////////////
|
---|
585 |
|
---|
586 |
|
---|
587 | /** Computes render cost of the merged pvs.
|
---|
588 | */
|
---|
589 | float ComputeMergedPvsCost(const ObjectPvs &pvs1, const ObjectPvs &pvs2) const;
|
---|
590 | /** Returns cost of this leaf according to current heuristics.
|
---|
591 | */
|
---|
592 | float GetCostHeuristics(ViewCell *vc) const;
|
---|
593 | /** Returns cost of leaf.
|
---|
594 | */
|
---|
595 | float GetRenderCost(ViewCell *vc) const;
|
---|
596 | /** Evaluates the merge cost of this merge candidate pair.
|
---|
597 | */
|
---|
598 | void EvalMergeCost(MergeCandidate &mc) const;
|
---|
599 | /** Variance of leaf.
|
---|
600 | */
|
---|
601 | float GetVariance(ViewCell *vc) const;
|
---|
602 | /** Standard deviation of leaf.
|
---|
603 | */
|
---|
604 | float GetDeviation(ViewCell *vc) const;
|
---|
605 | /** Tries to set this merge candidate to valid.
|
---|
606 | @returns false if both view cells are the same
|
---|
607 | */
|
---|
608 | bool ValidateMergeCandidate(MergeCandidate &mc) const;
|
---|
609 | /** Merge view cells of leaves l1 and l2.
|
---|
610 | @returns difference in pvs size
|
---|
611 | */
|
---|
612 | ViewCellInterior *MergeViewCells(ViewCell *l, ViewCell *r, float &pvsDiff);
|
---|
613 | /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
|
---|
614 | to view cell 2.
|
---|
615 | */
|
---|
616 | void ShuffleLeaf(ViewCell *leaf, ViewCellInterior *vc1, ViewCellInterior *vc2) const;
|
---|
617 | /** Shuffles the leaves, i.e., tests if exchanging
|
---|
618 | the leaves helps in improving the view cells.
|
---|
619 | */
|
---|
620 | bool ShuffleLeaves(MergeCandidate &mc) const;
|
---|
621 | /** Calculates cost for merge of view cell 1 and 2.
|
---|
622 | */
|
---|
623 | float EvalShuffleCost(ViewCell *leaf,
|
---|
624 | ViewCellInterior *vc1,
|
---|
625 | ViewCellInterior *vc2) const;
|
---|
626 | /** Exports a snapshot of the merged view cells to disc.
|
---|
627 | */
|
---|
628 | void ExportMergedViewCells(ViewCellContainer &viewCells,
|
---|
629 | const ObjectContainer &objects,
|
---|
630 | const int numNewViewCells);
|
---|
631 | /** Merge queue must be reset after some time because expected value
|
---|
632 | may not be valid.
|
---|
633 | */
|
---|
634 | void ResetMergeQueue();
|
---|
635 | /** Updates the current cut of view cells.
|
---|
636 | @returns number of newly merged view cells
|
---|
637 | */
|
---|
638 | int UpdateActiveViewCells(ViewCellContainer &viewCells);
|
---|
639 | /** Helper function pullling pvs as high up in the tree as possible.
|
---|
640 | */
|
---|
641 | void PullUpVisibility(ViewCellInterior *interior);
|
---|
642 | /** Compress pvs of view cell and children.
|
---|
643 | */
|
---|
644 | void CompressViewCellsPvs(ViewCell *root);
|
---|
645 | /** Returns memory usage of view cells.
|
---|
646 | */
|
---|
647 | float GetMemUsage() const;
|
---|
648 | /** Exports single view cell.
|
---|
649 | NOTE: should be in exporter!!
|
---|
650 | */
|
---|
651 | void ExportViewCell(ViewCell *viewCell, OUT_STREAM &stream, const bool exportPvs);
|
---|
652 | /** Exports pvs of a view cell.
|
---|
653 | */
|
---|
654 | void ExportPvs(ViewCell *viewCell, OUT_STREAM &stream);
|
---|
655 | /** Counts the logical number of entries in the pvs this view cell.
|
---|
656 | The pvs is assumed to be stored using lossless compression.
|
---|
657 | */
|
---|
658 | int GetEntriesInPvsForCompressedStorage(ViewCell *vc) const;
|
---|
659 | /** Computes pvs size of this view cell.
|
---|
660 | The pvs is assumed to be stored using lossless compression.
|
---|
661 | */
|
---|
662 | float GetPvsCostForCompressedStorage(ViewCell *vc) const;
|
---|
663 | /** Computes pvs size of this view cell.
|
---|
664 | The pvs is assumed to be stored in the leaves.
|
---|
665 | */
|
---|
666 | float GetPvsCostForLeafStorage(ViewCell *vc) const;
|
---|
667 | /** Counts the logical number of entries in the pvs this view cell.
|
---|
668 | The pvs is assumed to be stored using the leaves.
|
---|
669 | */
|
---|
670 | int GetEntriesInPvsForLeafStorage(ViewCell *vc) const;
|
---|
671 | /** Update stats for the log.
|
---|
672 | */
|
---|
673 | void UpdateStats(std::ofstream &stats,
|
---|
674 | const ViewCellsTreeStats &vcStats);
|
---|
675 |
|
---|
676 |
|
---|
677 | //////////////////////////////////////
|
---|
678 |
|
---|
679 | /// if the view cell tree hold compressed pvs
|
---|
680 | int mViewCellsStorage;
|
---|
681 | /// pointer to the view cells manager
|
---|
682 | ViewCellsManager *mViewCellsManager;
|
---|
683 | /// the root of the view cells hierarchy
|
---|
684 | ViewCell *mRoot;
|
---|
685 |
|
---|
686 | /// if merge visualization should be shown
|
---|
687 | bool mExportMergedViewCells;
|
---|
688 | /// intermediate container of merged view cells.
|
---|
689 | ViewCellContainer mMergedViewCells;
|
---|
690 | /// if merged view cells are refined.
|
---|
691 | bool mRefineViewCells;
|
---|
692 | /// weights between variance and render cost increase in the range [0 .. 1].
|
---|
693 | float mRenderCostWeight;
|
---|
694 |
|
---|
695 | /// overall cost used to normalize cost ratio
|
---|
696 | float mOverallCost;
|
---|
697 | float mExpectedCost;
|
---|
698 | float mDeviation;
|
---|
699 | float mAvgRenderCost;
|
---|
700 |
|
---|
701 | /// the area is used for pvs heuristics
|
---|
702 | int mUseAreaForPvs;
|
---|
703 | /// number of currently active view cells (=current cut)
|
---|
704 | int mNumActiveViewCells;
|
---|
705 | /// minimal number of view cells
|
---|
706 | int mMergeMinViewCells;
|
---|
707 | /// maximal cost ratio for the merge
|
---|
708 | float mMergeMaxCostRatio;
|
---|
709 |
|
---|
710 | typedef std::priority_queue<MergeCandidate> MergeQueue;
|
---|
711 |
|
---|
712 | MergeQueue mMergeQueue;
|
---|
713 |
|
---|
714 | float mMaxMemory;
|
---|
715 |
|
---|
716 | int mMaxMergesPerPass;
|
---|
717 | float mAvgCostMaxDeviation;
|
---|
718 |
|
---|
719 | int *mPvsIds;
|
---|
720 | ObjectContainer mTempObjects;
|
---|
721 | };
|
---|
722 |
|
---|
723 |
|
---|
724 | /**
|
---|
725 | Candidate for leaf merging based on priority.
|
---|
726 | */
|
---|
727 | class MergeCandidate
|
---|
728 | {
|
---|
729 | friend class ViewCellsTree;
|
---|
730 |
|
---|
731 | public:
|
---|
732 |
|
---|
733 | MergeCandidate(ViewCell *l, ViewCell *r);
|
---|
734 |
|
---|
735 | /** If this merge pair is still valid.
|
---|
736 | */
|
---|
737 | bool IsValid() const;
|
---|
738 |
|
---|
739 |
|
---|
740 | friend bool operator<(const MergeCandidate &leafa, const MergeCandidate &leafb)
|
---|
741 | {
|
---|
742 | return leafb.GetMergeCost() < leafa.GetMergeCost();
|
---|
743 | }
|
---|
744 |
|
---|
745 | void SetLeftViewCell(ViewCell *l);
|
---|
746 | void SetRightViewCell(ViewCell *l);
|
---|
747 |
|
---|
748 | ViewCell *GetLeftViewCell() const;
|
---|
749 | ViewCell *GetRightViewCell() const;
|
---|
750 |
|
---|
751 | /** Returns leaf view cell initially associated with this merge candidate.
|
---|
752 | */
|
---|
753 | ViewCell *GetInitialLeftViewCell() const;
|
---|
754 | /** Returns leaf view cell initially associated with this merge candidate.
|
---|
755 | */
|
---|
756 | ViewCell *GetInitialRightViewCell() const;
|
---|
757 |
|
---|
758 | /** Returns the increase of the standard deviation of this merge candidate.
|
---|
759 | */
|
---|
760 | float GetDeviationIncr() const;
|
---|
761 |
|
---|
762 | /** Merge cost of this candidate pair.
|
---|
763 | */
|
---|
764 | float GetMergeCost() const;
|
---|
765 |
|
---|
766 | /** Render cost of this candidate.
|
---|
767 | */
|
---|
768 | float GetRenderCost() const;
|
---|
769 |
|
---|
770 | static float sRenderCostWeight;
|
---|
771 |
|
---|
772 | protected:
|
---|
773 |
|
---|
774 | /// render cost increase by this merge
|
---|
775 | float mRenderCost;
|
---|
776 | /// increase / decrease of standard deviation
|
---|
777 | float mDeviationIncr;
|
---|
778 |
|
---|
779 | ViewCell *mLeftViewCell;
|
---|
780 | ViewCell *mRightViewCell;
|
---|
781 |
|
---|
782 | ViewCell *mInitialLeftViewCell;
|
---|
783 | ViewCell *mInitialRightViewCell;
|
---|
784 | };
|
---|
785 |
|
---|
786 |
|
---|
787 | class MergeStatistics: public StatisticsBase
|
---|
788 | {
|
---|
789 | public:
|
---|
790 |
|
---|
791 | int merged;
|
---|
792 | int siblings;
|
---|
793 | int candidates;
|
---|
794 | int nodes;
|
---|
795 |
|
---|
796 | int accTreeDist;
|
---|
797 | int maxTreeDist;
|
---|
798 |
|
---|
799 | Real collectTime;
|
---|
800 | Real mergeTime;
|
---|
801 |
|
---|
802 | Real overallCost;
|
---|
803 |
|
---|
804 | Real expectedRenderCost;
|
---|
805 | Real deviation;
|
---|
806 | Real heuristics;
|
---|
807 |
|
---|
808 | // Constructor
|
---|
809 | MergeStatistics()
|
---|
810 | {
|
---|
811 | Reset();
|
---|
812 | }
|
---|
813 |
|
---|
814 | double AvgTreeDist() const {return (double)accTreeDist / (double)merged;};
|
---|
815 |
|
---|
816 | void Reset()
|
---|
817 | {
|
---|
818 | nodes = 0;
|
---|
819 | merged = 0;
|
---|
820 | siblings = 0;
|
---|
821 | candidates = 0;
|
---|
822 |
|
---|
823 | accTreeDist = 0;
|
---|
824 | maxTreeDist = 0;
|
---|
825 |
|
---|
826 | collectTime = 0;
|
---|
827 | mergeTime = 0;
|
---|
828 | overallCost = 0;
|
---|
829 |
|
---|
830 | expectedRenderCost = 0;
|
---|
831 | deviation = 0;
|
---|
832 | heuristics = 0;
|
---|
833 |
|
---|
834 | }
|
---|
835 |
|
---|
836 | void Print(std::ostream &app) const;
|
---|
837 |
|
---|
838 | friend std::ostream &operator<<(std::ostream &s, const MergeStatistics &stat)
|
---|
839 | {
|
---|
840 | stat.Print(s);
|
---|
841 | return s;
|
---|
842 | }
|
---|
843 | };
|
---|
844 |
|
---|
845 | }
|
---|
846 |
|
---|
847 | #endif
|
---|