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