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