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