[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:
|
---|
[1279] | 141 | /** Constructor with the object space hierarchy type as argument.
|
---|
[1237] | 142 | */
|
---|
[1279] | 143 | HierarchyManager(VspTree *vspTree, const int objectSpaceHierarchyType);
|
---|
| 144 | /** Hack: OspTree will copy the content from this kd tree.
|
---|
| 145 | Only view space hierarchy will be constructed.
|
---|
| 146 | */
|
---|
| 147 | HierarchyManager(VspTree *vspTree, KdTree *kdTree);
|
---|
[1237] | 148 |
|
---|
[1286] | 149 | ~HierarchyManager();
|
---|
| 150 |
|
---|
[1237] | 151 | /** Constructs the view space and object space subdivision from a given set of rays
|
---|
| 152 | and a set of objects.
|
---|
| 153 | @param sampleRays the set of sample rays the construction is based on
|
---|
| 154 | @param objects the set of objects
|
---|
| 155 | */
|
---|
[1308] | 156 | void Construct(
|
---|
[1293] | 157 | const VssRayContainer &sampleRays,
|
---|
| 158 | const ObjectContainer &objects,
|
---|
| 159 | AxisAlignedBox3 *forcedViewSpace);
|
---|
[1237] | 160 |
|
---|
[1259] | 161 | enum
|
---|
| 162 | {
|
---|
| 163 | NO_OBJ_SUBDIV,
|
---|
| 164 | KD_BASED_OBJ_SUBDIV,
|
---|
| 165 | BV_BASED_OBJ_SUBDIV
|
---|
| 166 | };
|
---|
[1237] | 167 |
|
---|
[1259] | 168 | /** The type of object space subdivison
|
---|
| 169 | */
|
---|
[1313] | 170 | int GetObjectSpaceSubdivisionType() const
|
---|
[1259] | 171 | {
|
---|
[1308] | 172 | return mObjectSpaceSubdivisionType;
|
---|
[1259] | 173 | }
|
---|
[1311] | 174 |
|
---|
[1279] | 175 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 176 |
|
---|
| 177 | void SetViewCellsTree(ViewCellsTree *vcTree);
|
---|
| 178 |
|
---|
| 179 | void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
|
---|
| 180 |
|
---|
| 181 | bool AddSampleToPvs(
|
---|
| 182 | Intersectable *obj,
|
---|
| 183 | const Vector3 &hitPoint,
|
---|
| 184 | ViewCell *vc,
|
---|
| 185 | const float pdf,
|
---|
| 186 | float &contribution) const;
|
---|
| 187 |
|
---|
[1313] | 188 | void PrintHierarchyStatistics(ofstream &stream) const;
|
---|
[1279] | 189 |
|
---|
| 190 | VspTree *GetVspTree() { return mVspTree; }
|
---|
| 191 |
|
---|
[1287] | 192 | void ExportObjectSpaceHierarchy(
|
---|
| 193 | Exporter *exporter,
|
---|
| 194 | const ObjectContainer &objects) const;
|
---|
[1279] | 195 |
|
---|
[1286] | 196 |
|
---|
[1237] | 197 | protected:
|
---|
| 198 |
|
---|
| 199 | bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
|
---|
| 200 |
|
---|
| 201 | /** Prepare construction of the hierarchies, set parameters, compute
|
---|
| 202 | first split candidates.
|
---|
| 203 | */
|
---|
[1308] | 204 | void PrepareObjectSpaceSubdivision(
|
---|
[1237] | 205 | const VssRayContainer &sampleRays,
|
---|
[1308] | 206 | const ObjectContainer &objects);
|
---|
[1237] | 207 |
|
---|
[1311] | 208 | void RunConstruction(
|
---|
| 209 | const VssRayContainer &sampleRays,
|
---|
| 210 | const ObjectContainer &objects,
|
---|
| 211 | AxisAlignedBox3 *forcedViewSpace);
|
---|
| 212 |
|
---|
[1287] | 213 | bool ApplySubdivisionCandidate(SubdivisionCandidate *sc);
|
---|
[1237] | 214 |
|
---|
| 215 | bool FinishedConstruction() const;
|
---|
| 216 |
|
---|
| 217 | SubdivisionCandidate *NextSubdivisionCandidate();
|
---|
| 218 |
|
---|
| 219 | void RepairQueue();
|
---|
| 220 |
|
---|
| 221 | void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
|
---|
| 222 |
|
---|
| 223 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
| 224 |
|
---|
[1259] | 225 | void AddSubdivisionStats(
|
---|
| 226 | const int splits,
|
---|
[1237] | 227 | const float renderCostDecr,
|
---|
| 228 | const float totalRenderCost);
|
---|
| 229 |
|
---|
[1259] | 230 | void CollectObjectSpaceDirtyList();
|
---|
| 231 | void CollectViewSpaceDirtyList();
|
---|
[1237] | 232 |
|
---|
[1259] | 233 | bool AddSampleToPvs(Intersectable *obj,
|
---|
| 234 | const float pdf,
|
---|
| 235 | float &contribution) const;
|
---|
| 236 |
|
---|
| 237 | void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
|
---|
| 238 | void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
|
---|
| 239 |
|
---|
[1287] | 240 | void ExportOspTree(Exporter *exporter, const ObjectContainer &objects) const;
|
---|
| 241 | void ExportBvHierarchy(Exporter *exporter, const ObjectContainer &objects) const;
|
---|
[1259] | 242 |
|
---|
[1308] | 243 | void PrepareBvHierarchy(
|
---|
[1286] | 244 | const VssRayContainer &sampleRays,
|
---|
[1287] | 245 | const ObjectContainer &objects);
|
---|
[1286] | 246 |
|
---|
[1308] | 247 | void PrepareOspTree(
|
---|
[1286] | 248 | const VssRayContainer &sampleRays,
|
---|
[1287] | 249 | const ObjectContainer &objects);
|
---|
[1286] | 250 |
|
---|
[1288] | 251 | void ParseEnvironment();
|
---|
| 252 |
|
---|
[1308] | 253 | bool StartObjectSpaceSubdivision() const;
|
---|
[1329] | 254 | bool StartViewSpaceSubdivision() const;
|
---|
[1288] | 255 |
|
---|
[1311] | 256 | void PrepareViewSpaceSubdivision(
|
---|
| 257 | const VssRayContainer &sampleRays,
|
---|
| 258 | const ObjectContainer &objects,
|
---|
[1329] | 259 | AxisAlignedBox3 *forcedViewSpace = NULL);
|
---|
[1308] | 260 |
|
---|
[1313] | 261 | bool ObjectSpaceSubdivisionConstructed() const;
|
---|
[1329] | 262 | bool ViewSpaceSubdivisionConstructed() const;
|
---|
[1311] | 263 |
|
---|
[1313] | 264 | void ResetQueue();
|
---|
| 265 |
|
---|
| 266 |
|
---|
[1237] | 267 | protected:
|
---|
| 268 |
|
---|
[1323] | 269 | enum {SEQUENTIAL, INTERLEAVED};
|
---|
[1329] | 270 |
|
---|
[1308] | 271 | int mObjectSpaceSubdivisionType;
|
---|
[1329] | 272 | int mViewSpaceSubdivisionType;
|
---|
| 273 |
|
---|
[1323] | 274 | /// the original osp type
|
---|
| 275 | int mSavedObjectSpaceSubdivisionType;
|
---|
[1329] | 276 | int mSavedViewSpaceSubdivisionType;
|
---|
[1323] | 277 |
|
---|
[1293] | 278 | int mConstructionType;
|
---|
[1259] | 279 |
|
---|
| 280 | VspTree *mVspTree;
|
---|
| 281 | OspTree *mOspTree;
|
---|
| 282 | BvHierarchy *mBvHierarchy;
|
---|
| 283 |
|
---|
[1237] | 284 | AxisAlignedBox3 mBoundingBox;
|
---|
| 285 |
|
---|
| 286 | SplitQueue mTQueue;
|
---|
| 287 |
|
---|
| 288 | SubdivisionCandidate *mCurrentCandidate;
|
---|
| 289 |
|
---|
| 290 | //-- global criteria
|
---|
| 291 | float mTermMinGlobalCostRatio;
|
---|
| 292 | int mTermGlobalCostMissTolerance;
|
---|
| 293 | int mGlobalCostMisses;
|
---|
| 294 |
|
---|
| 295 | /// keeps track of cost during subdivision
|
---|
| 296 | float mTotalCost;
|
---|
| 297 |
|
---|
[1288] | 298 | HierarchyStatistics mHierarchyStats;
|
---|
| 299 |
|
---|
[1308] | 300 | int mMinDepthForObjectSpaceSubdivion;
|
---|
| 301 |
|
---|
[1294] | 302 | int mTermMaxLeaves;
|
---|
[1237] | 303 | ofstream mSubdivisionStats;
|
---|
[1314] | 304 |
|
---|
| 305 | bool mRepairQueue;
|
---|
[1237] | 306 | };
|
---|
| 307 |
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | #endif
|
---|