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