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