[1237] | 1 | #ifndef _HierarchyManager_H__
|
---|
| 2 | #define _HierarchyManager_H__
|
---|
| 3 |
|
---|
| 4 | #include <stack>
|
---|
| 5 |
|
---|
| 6 | #include "Mesh.h"
|
---|
| 7 | #include "Containers.h"
|
---|
| 8 | #include "Statistics.h"
|
---|
| 9 | #include "VssRay.h"
|
---|
| 10 | #include "RayInfo.h"
|
---|
| 11 | #include "gzstream.h"
|
---|
[1239] | 12 | #include "SubdivisionCandidate.h"
|
---|
[1237] | 13 |
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | namespace GtpVisibilityPreprocessor {
|
---|
| 17 |
|
---|
| 18 | class ViewCellLeaf;
|
---|
| 19 | class OspTree;
|
---|
| 20 | class VspTree;
|
---|
| 21 | class Plane3;
|
---|
| 22 | class AxisAlignedBox3;
|
---|
| 23 | class Ray;
|
---|
| 24 | class ViewCellsStatistics;
|
---|
| 25 | class ViewCellsManager;
|
---|
| 26 | class MergeCandidate;
|
---|
| 27 | class Beam;
|
---|
| 28 | class ViewCellsTree;
|
---|
| 29 | class Environment;
|
---|
| 30 | class VspInterior;
|
---|
| 31 | class VspLeaf;
|
---|
| 32 | class VspNode;
|
---|
| 33 | class KdNode;
|
---|
| 34 | class KdInterior;
|
---|
| 35 | class KdLeaf;
|
---|
| 36 | class OspTree;
|
---|
| 37 | class KdIntersectable;
|
---|
| 38 | class KdTree;
|
---|
| 39 | class VspTree;
|
---|
| 40 | class KdTreeStatistics;
|
---|
[1259] | 41 | class BvHierarchy;
|
---|
[1287] | 42 | class Exporter;
|
---|
[1237] | 43 |
|
---|
| 44 |
|
---|
| 45 |
|
---|
[1551] | 46 | /** View space / object space hierarchy statistics.
|
---|
[1288] | 47 | */
|
---|
| 48 | class HierarchyStatistics: public StatisticsBase
|
---|
| 49 | {
|
---|
| 50 | public:
|
---|
| 51 |
|
---|
| 52 | /// total number of nodes
|
---|
| 53 | int nodes;
|
---|
| 54 | /// maximal reached depth
|
---|
| 55 | int maxDepth;
|
---|
| 56 | /// accumulated depth
|
---|
| 57 | int accumDepth;
|
---|
[1449] | 58 | /// time spent for queue repair
|
---|
[1313] | 59 | float repairTime;
|
---|
[1449] | 60 | // global cost ratio violations
|
---|
| 61 | int mGlobalCostMisses;
|
---|
[1313] | 62 |
|
---|
[1288] | 63 | // Constructor
|
---|
| 64 | HierarchyStatistics()
|
---|
| 65 | {
|
---|
| 66 | Reset();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | int Nodes() const {return nodes;}
|
---|
| 70 | int Interior() const { return nodes / 2; }
|
---|
| 71 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
| 72 |
|
---|
| 73 | // TODO: computation wrong
|
---|
| 74 | double AvgDepth() const { return accumDepth / (double)Leaves();}
|
---|
| 75 |
|
---|
[1449] | 76 | void Reset()
|
---|
[1288] | 77 | {
|
---|
[1449] | 78 | mGlobalCostMisses = 0;
|
---|
[1288] | 79 | nodes = 0;
|
---|
| 80 | maxDepth = 0;
|
---|
| 81 | accumDepth = 0;
|
---|
[1313] | 82 | repairTime = 0;
|
---|
[1288] | 83 | }
|
---|
| 84 |
|
---|
| 85 | void Print(ostream &app) const;
|
---|
| 86 |
|
---|
| 87 | friend ostream &operator<<(ostream &s, const HierarchyStatistics &stat)
|
---|
| 88 | {
|
---|
| 89 | stat.Print(s);
|
---|
| 90 | return s;
|
---|
| 91 | }
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 |
|
---|
[1237] | 95 | typedef FlexibleHeap<SubdivisionCandidate *> SplitQueue;
|
---|
| 96 |
|
---|
| 97 | /** This class implements a structure holding two different hierarchies,
|
---|
| 98 | one for object space partitioning and one for view space partitioning.
|
---|
| 99 |
|
---|
| 100 | The object space and the view space are subdivided using a cost heuristics.
|
---|
| 101 | If an object space split or a view space split is chosen is also evaluated
|
---|
| 102 | based on the heuristics.
|
---|
| 103 |
|
---|
| 104 | The view space heuristics is evaluated by weighting and adding the pvss of the back and
|
---|
| 105 | front node of each specific split. unlike for the standalone method vspbsp tree,
|
---|
| 106 | the pvs of an object would not be the pvs of single object but that of all objects
|
---|
| 107 | which are contained in the same leaf of the object subdivision. This could be done
|
---|
| 108 | by storing the pointer to the object space partition parent, which would allow access to all children.
|
---|
| 109 | Another possibility is to include traced kd-cells in the ray casing process.
|
---|
| 110 |
|
---|
| 111 | Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
|
---|
| 112 | the contribution to an object to the pvs is the number of view cells it can be seen from.
|
---|
| 113 |
|
---|
| 114 | @note
|
---|
| 115 | There is a potential efficiency problem involved in a sense that once a certain type
|
---|
| 116 | of split is chosen for view space / object space, the candidates for the next split of
|
---|
| 117 | object space / view space must be reevaluated.
|
---|
| 118 | */
|
---|
| 119 | class HierarchyManager
|
---|
| 120 | {
|
---|
[1259] | 121 | friend VspTree;
|
---|
| 122 | friend OspTree;
|
---|
| 123 | friend BvHierarchy;
|
---|
[1279] | 124 | friend ViewCellsParseHandlers;
|
---|
[1259] | 125 |
|
---|
[1237] | 126 | public:
|
---|
[1421] | 127 | /** Constructor with the view space partition tree and
|
---|
| 128 | the object space hierarchy type as argument.
|
---|
[1237] | 129 | */
|
---|
[1421] | 130 | HierarchyManager(const int objectSpaceHierarchyType);
|
---|
[1279] | 131 | /** Hack: OspTree will copy the content from this kd tree.
|
---|
| 132 | Only view space hierarchy will be constructed.
|
---|
| 133 | */
|
---|
[1421] | 134 | HierarchyManager(KdTree *kdTree);
|
---|
[1237] | 135 |
|
---|
[1421] | 136 | /** Deletes space partition and view space partition.
|
---|
| 137 | */
|
---|
[1286] | 138 | ~HierarchyManager();
|
---|
| 139 |
|
---|
[1237] | 140 | /** Constructs the view space and object space subdivision from a given set of rays
|
---|
| 141 | and a set of objects.
|
---|
| 142 | @param sampleRays the set of sample rays the construction is based on
|
---|
| 143 | @param objects the set of objects
|
---|
| 144 | */
|
---|
[1308] | 145 | void Construct(
|
---|
[1293] | 146 | const VssRayContainer &sampleRays,
|
---|
| 147 | const ObjectContainer &objects,
|
---|
| 148 | AxisAlignedBox3 *forcedViewSpace);
|
---|
[1237] | 149 |
|
---|
[1259] | 150 | enum
|
---|
| 151 | {
|
---|
| 152 | NO_OBJ_SUBDIV,
|
---|
| 153 | KD_BASED_OBJ_SUBDIV,
|
---|
| 154 | BV_BASED_OBJ_SUBDIV
|
---|
| 155 | };
|
---|
[1237] | 156 |
|
---|
[1370] | 157 | enum
|
---|
| 158 | {
|
---|
| 159 | NO_VIEWSPACE_SUBDIV,
|
---|
| 160 | KD_BASED_VIEWSPACE_SUBDIV
|
---|
| 161 | };
|
---|
| 162 |
|
---|
[1259] | 163 | /** The type of object space subdivison
|
---|
| 164 | */
|
---|
[1370] | 165 | int GetObjectSpaceSubdivisionType() const;
|
---|
| 166 | /** The type of view space space subdivison
|
---|
| 167 | */
|
---|
| 168 | int GetViewSpaceSubdivisionType() const;
|
---|
| 169 | /** Sets a pointer to the view cells manager.
|
---|
| 170 | */
|
---|
[1279] | 171 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
[1370] | 172 | /** Sets a pointer to the view cells tree.
|
---|
| 173 | */
|
---|
[1279] | 174 | void SetViewCellsTree(ViewCellsTree *vcTree);
|
---|
[1370] | 175 | /** Exports the object hierarchy to disc.
|
---|
| 176 | */
|
---|
[1279] | 177 | void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
|
---|
[1370] | 178 | /** Adds a sample to the pvs of the specified view cell.
|
---|
| 179 | */
|
---|
[1279] | 180 | bool AddSampleToPvs(
|
---|
| 181 | Intersectable *obj,
|
---|
| 182 | const Vector3 &hitPoint,
|
---|
| 183 | ViewCell *vc,
|
---|
| 184 | const float pdf,
|
---|
| 185 | float &contribution) const;
|
---|
| 186 |
|
---|
[1421] | 187 | /** Print out statistics.
|
---|
| 188 | */
|
---|
| 189 | void PrintHierarchyStatistics(ostream &stream) const;
|
---|
[1279] | 190 |
|
---|
[1421] | 191 | /** Returns the view space partition tree.
|
---|
| 192 | */
|
---|
[1379] | 193 | VspTree *GetVspTree();
|
---|
[1279] | 194 |
|
---|
[1421] | 195 | /** Returns view space bounding box.
|
---|
| 196 | */
|
---|
[1379] | 197 | AxisAlignedBox3 GetViewSpaceBox() const;
|
---|
[1421] | 198 | /** Returns object space bounding box.
|
---|
| 199 | */
|
---|
[1416] | 200 | AxisAlignedBox3 GetObjectSpaceBox() const;
|
---|
[1379] | 201 |
|
---|
[1421] | 202 | /** Exports object space hierarchy for visualization.
|
---|
| 203 | */
|
---|
[1287] | 204 | void ExportObjectSpaceHierarchy(
|
---|
| 205 | Exporter *exporter,
|
---|
[1416] | 206 | const ObjectContainer &objects,
|
---|
[1418] | 207 | const AxisAlignedBox3 *bbox,
|
---|
| 208 | const bool exportBounds = true) const;
|
---|
[1279] | 209 |
|
---|
[1421] | 210 | /** Returns intersectable pierced by this ray.
|
---|
| 211 | */
|
---|
[1418] | 212 | Intersectable *GetIntersectable(
|
---|
| 213 | const VssRay &ray,
|
---|
| 214 | const bool isTermination) const;
|
---|
| 215 |
|
---|
[1419] | 216 | friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
|
---|
| 217 | {
|
---|
[1421] | 218 | hm.PrintHierarchyStatistics(s);
|
---|
[1419] | 219 | return s;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[1421] | 222 |
|
---|
[1237] | 223 | protected:
|
---|
| 224 |
|
---|
| 225 | bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
|
---|
| 226 |
|
---|
| 227 | /** Prepare construction of the hierarchies, set parameters, compute
|
---|
| 228 | first split candidates.
|
---|
| 229 | */
|
---|
[1308] | 230 | void PrepareObjectSpaceSubdivision(
|
---|
[1237] | 231 | const VssRayContainer &sampleRays,
|
---|
[1308] | 232 | const ObjectContainer &objects);
|
---|
[1237] | 233 |
|
---|
[1311] | 234 | void RunConstruction(
|
---|
[1449] | 235 | const bool repairQueue,
|
---|
[1311] | 236 | const VssRayContainer &sampleRays,
|
---|
| 237 | const ObjectContainer &objects,
|
---|
| 238 | AxisAlignedBox3 *forcedViewSpace);
|
---|
[1449] | 239 |
|
---|
| 240 | void RunConstruction(const bool repairQueue);
|
---|
| 241 |
|
---|
[1287] | 242 | bool ApplySubdivisionCandidate(SubdivisionCandidate *sc);
|
---|
[1237] | 243 |
|
---|
| 244 | bool FinishedConstruction() const;
|
---|
| 245 |
|
---|
| 246 | SubdivisionCandidate *NextSubdivisionCandidate();
|
---|
| 247 |
|
---|
| 248 | void RepairQueue();
|
---|
| 249 |
|
---|
| 250 | void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
|
---|
| 251 |
|
---|
| 252 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
| 253 |
|
---|
[1259] | 254 | void AddSubdivisionStats(
|
---|
| 255 | const int splits,
|
---|
[1237] | 256 | const float renderCostDecr,
|
---|
| 257 | const float totalRenderCost);
|
---|
| 258 |
|
---|
[1259] | 259 | void CollectObjectSpaceDirtyList();
|
---|
| 260 | void CollectViewSpaceDirtyList();
|
---|
[1237] | 261 |
|
---|
[1259] | 262 | bool AddSampleToPvs(Intersectable *obj,
|
---|
| 263 | const float pdf,
|
---|
| 264 | float &contribution) const;
|
---|
| 265 |
|
---|
| 266 | void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
|
---|
| 267 | void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
|
---|
| 268 |
|
---|
[1287] | 269 | void ExportOspTree(Exporter *exporter, const ObjectContainer &objects) const;
|
---|
[1259] | 270 |
|
---|
[1418] | 271 | void ParseEnvironment();
|
---|
[1415] | 272 |
|
---|
[1418] | 273 | bool StartObjectSpaceSubdivision() const;
|
---|
| 274 | bool StartViewSpaceSubdivision() const;
|
---|
| 275 |
|
---|
[1308] | 276 | void PrepareBvHierarchy(
|
---|
[1286] | 277 | const VssRayContainer &sampleRays,
|
---|
[1287] | 278 | const ObjectContainer &objects);
|
---|
[1286] | 279 |
|
---|
[1308] | 280 | void PrepareOspTree(
|
---|
[1286] | 281 | const VssRayContainer &sampleRays,
|
---|
[1287] | 282 | const ObjectContainer &objects);
|
---|
[1286] | 283 |
|
---|
[1311] | 284 | void PrepareViewSpaceSubdivision(
|
---|
| 285 | const VssRayContainer &sampleRays,
|
---|
[1379] | 286 | const ObjectContainer &objects);
|
---|
[1308] | 287 |
|
---|
[1313] | 288 | bool ObjectSpaceSubdivisionConstructed() const;
|
---|
[1329] | 289 | bool ViewSpaceSubdivisionConstructed() const;
|
---|
[1311] | 290 |
|
---|
[1313] | 291 | void ResetQueue();
|
---|
| 292 |
|
---|
[1418] | 293 | void FinishObjectSpaceSubdivision(const ObjectContainer &objects) const;
|
---|
[1313] | 294 |
|
---|
[1370] | 295 | int GetObjectSpaceSubdivisionDepth() const;
|
---|
| 296 |
|
---|
[1449] | 297 | void ConstructInterleaved(
|
---|
| 298 | const VssRayContainer &sampleRays,
|
---|
| 299 | const ObjectContainer &objects,
|
---|
| 300 | AxisAlignedBox3 *forcedViewSpace);
|
---|
| 301 |
|
---|
[1548] | 302 | /** Use iteration to construct the object space hierarchy.
|
---|
| 303 | */
|
---|
[1449] | 304 | void ConstructMultiLevel(
|
---|
| 305 | const VssRayContainer &sampleRays,
|
---|
| 306 | const ObjectContainer &objects,
|
---|
| 307 | AxisAlignedBox3 *forcedViewSpace);
|
---|
| 308 |
|
---|
[1548] | 309 | /** Reset the object space subdivision.
|
---|
| 310 | E.g., deletes hierarchy and resets stats.
|
---|
| 311 | so construction can be restarted.
|
---|
| 312 | */
|
---|
| 313 | void ResetObjectSpaceSubdivision(
|
---|
| 314 | const VssRayContainer &rays,
|
---|
| 315 | const ObjectContainer &objects);
|
---|
[1449] | 316 |
|
---|
[1237] | 317 | protected:
|
---|
| 318 |
|
---|
[1323] | 319 | enum {SEQUENTIAL, INTERLEAVED};
|
---|
[1329] | 320 |
|
---|
[1308] | 321 | int mObjectSpaceSubdivisionType;
|
---|
[1329] | 322 | int mViewSpaceSubdivisionType;
|
---|
| 323 |
|
---|
[1323] | 324 | /// the original osp type
|
---|
| 325 | int mSavedObjectSpaceSubdivisionType;
|
---|
[1329] | 326 | int mSavedViewSpaceSubdivisionType;
|
---|
[1323] | 327 |
|
---|
[1293] | 328 | int mConstructionType;
|
---|
[1259] | 329 |
|
---|
| 330 | VspTree *mVspTree;
|
---|
| 331 | OspTree *mOspTree;
|
---|
| 332 | BvHierarchy *mBvHierarchy;
|
---|
| 333 |
|
---|
[1237] | 334 | AxisAlignedBox3 mBoundingBox;
|
---|
| 335 |
|
---|
| 336 | SplitQueue mTQueue;
|
---|
| 337 |
|
---|
| 338 | SubdivisionCandidate *mCurrentCandidate;
|
---|
| 339 |
|
---|
[1449] | 340 | ////////
|
---|
[1237] | 341 | //-- global criteria
|
---|
| 342 | float mTermMinGlobalCostRatio;
|
---|
| 343 | int mTermGlobalCostMissTolerance;
|
---|
[1449] | 344 |
|
---|
[1237] | 345 | /// keeps track of cost during subdivision
|
---|
| 346 | float mTotalCost;
|
---|
| 347 |
|
---|
[1288] | 348 | HierarchyStatistics mHierarchyStats;
|
---|
| 349 |
|
---|
[1308] | 350 | int mMinDepthForObjectSpaceSubdivion;
|
---|
[1370] | 351 | int mMinDepthForViewSpaceSubdivion;
|
---|
[1308] | 352 |
|
---|
[1294] | 353 | int mTermMaxLeaves;
|
---|
[1237] | 354 | ofstream mSubdivisionStats;
|
---|
[1314] | 355 |
|
---|
| 356 | bool mRepairQueue;
|
---|
[1370] | 357 |
|
---|
| 358 | bool mStartWithObjectSpace;
|
---|
[1449] | 359 |
|
---|
| 360 | bool mUseMultiLevelConstruction;
|
---|
[1237] | 361 | };
|
---|
| 362 |
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | #endif
|
---|