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