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