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