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