[727] | 1 | #ifndef _VspBspTree_H__
|
---|
| 2 | #define _VspBspTree_H__
|
---|
| 3 |
|
---|
| 4 | #include "Mesh.h"
|
---|
| 5 | #include "Containers.h"
|
---|
| 6 | #include "Polygon3.h"
|
---|
| 7 | #include <stack>
|
---|
| 8 | #include "Statistics.h"
|
---|
| 9 | #include "VssRay.h"
|
---|
| 10 | #include "RayInfo.h"
|
---|
| 11 | #include "ViewCellBsp.h"
|
---|
| 12 |
|
---|
| 13 | class ViewCell;
|
---|
| 14 | //class BspViewCell;
|
---|
| 15 | class Plane3;
|
---|
| 16 | class VspBspTree;
|
---|
| 17 | class BspInterior;
|
---|
| 18 | class BspNode;
|
---|
| 19 | class AxisAlignedBox3;
|
---|
| 20 | class Ray;
|
---|
| 21 | class ViewCellsStatistics;
|
---|
| 22 | class ViewCellsManager;
|
---|
| 23 | class MergeCandidate;
|
---|
| 24 | class Beam;
|
---|
| 25 | class ViewCellsTree;
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | struct BspRay;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | BspNode abstract class serving for interior and leaf node implementation
|
---|
| 33 | */
|
---|
| 34 | class BspNode
|
---|
| 35 | {
|
---|
| 36 | friend class BspTree;
|
---|
| 37 |
|
---|
| 38 | public:
|
---|
| 39 | BspNode();
|
---|
| 40 | virtual ~BspNode(){};
|
---|
| 41 | BspNode(BspInterior *parent);
|
---|
| 42 |
|
---|
| 43 | /** Determines whether this node is a leaf or not
|
---|
| 44 | @return true if leaf
|
---|
| 45 | */
|
---|
| 46 | virtual bool IsLeaf() const = 0;
|
---|
| 47 |
|
---|
| 48 | /** Determines whether this node is a root
|
---|
| 49 | @return true if root
|
---|
| 50 | */
|
---|
| 51 | virtual bool IsRoot() const;
|
---|
| 52 |
|
---|
| 53 | /** Returns parent node.
|
---|
| 54 | */
|
---|
| 55 | BspInterior *GetParent();
|
---|
| 56 |
|
---|
| 57 | /** Sets parent node.
|
---|
| 58 | */
|
---|
| 59 | void SetParent(BspInterior *parent);
|
---|
| 60 |
|
---|
| 61 | /** Returns true if this node is a sibling of node n.
|
---|
| 62 | */
|
---|
| 63 | bool IsSibling(BspNode *n) const;
|
---|
| 64 |
|
---|
| 65 | /** returns depth of the node.
|
---|
| 66 | */
|
---|
| 67 | int GetDepth() const;
|
---|
| 68 |
|
---|
| 69 | /** returns true if the whole subtree is valid
|
---|
| 70 | */
|
---|
| 71 | bool TreeValid() const;
|
---|
| 72 |
|
---|
| 73 | void SetTreeValid(const bool v);
|
---|
| 74 |
|
---|
| 75 | //-- mailing options
|
---|
| 76 |
|
---|
| 77 | void Mail() { mMailbox = sMailId; }
|
---|
| 78 | static void NewMail() { ++ sMailId; }
|
---|
| 79 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
| 80 |
|
---|
| 81 | static int sMailId;
|
---|
| 82 | int mMailbox;
|
---|
| 83 |
|
---|
| 84 | int mTimeStamp;
|
---|
| 85 |
|
---|
| 86 | protected:
|
---|
| 87 |
|
---|
| 88 | /// if this sub tree is a completely valid view space region
|
---|
| 89 | bool mTreeValid;
|
---|
| 90 | /// parent of this node
|
---|
| 91 | BspInterior *mParent;
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | /** BSP interior node implementation
|
---|
| 95 | */
|
---|
| 96 | class BspInterior: public BspNode
|
---|
| 97 | {
|
---|
| 98 | friend class BspTree;
|
---|
| 99 | public:
|
---|
| 100 | /** Standard contructor taking split plane as argument.
|
---|
| 101 | */
|
---|
| 102 | BspInterior(const Plane3 &plane);
|
---|
| 103 | ~BspInterior();
|
---|
| 104 | /** @return false since it is an interior node
|
---|
| 105 | */
|
---|
| 106 | bool IsLeaf() const;
|
---|
| 107 |
|
---|
| 108 | BspNode *GetBack();
|
---|
| 109 | BspNode *GetFront();
|
---|
| 110 |
|
---|
| 111 | /** Returns split plane.
|
---|
| 112 | */
|
---|
| 113 | Plane3 GetPlane() const;
|
---|
| 114 |
|
---|
| 115 | /** Replace front or back child with new child.
|
---|
| 116 | */
|
---|
| 117 | void ReplaceChildLink(BspNode *oldChild, BspNode *newChild);
|
---|
| 118 | /** Replace front and back child.
|
---|
| 119 | */
|
---|
| 120 | void SetupChildLinks(BspNode *b, BspNode *f);
|
---|
| 121 |
|
---|
| 122 | friend ostream &operator<<(ostream &s, const BspInterior &A)
|
---|
| 123 | {
|
---|
| 124 | return s << A.mPlane;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | protected:
|
---|
| 128 |
|
---|
| 129 | /// Splitting plane corresponding to this node
|
---|
| 130 | Plane3 mPlane;
|
---|
| 131 |
|
---|
| 132 | /// back node
|
---|
| 133 | BspNode *mBack;
|
---|
| 134 | /// front node
|
---|
| 135 | BspNode *mFront;
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 | /** BSP leaf node implementation.
|
---|
| 139 | */
|
---|
| 140 | class BspLeaf: public BspNode
|
---|
| 141 | {
|
---|
| 142 | friend class BspTree;
|
---|
| 143 |
|
---|
| 144 | public:
|
---|
| 145 | BspLeaf();
|
---|
| 146 | BspLeaf(ViewCell *viewCell);
|
---|
| 147 | BspLeaf(BspInterior *parent);
|
---|
| 148 | BspLeaf(BspInterior *parent, ViewCell *viewCell);
|
---|
| 149 |
|
---|
| 150 | ~BspLeaf();
|
---|
| 151 |
|
---|
| 152 | /** @return true since it is an interior node
|
---|
| 153 | */
|
---|
| 154 | bool IsLeaf() const;
|
---|
| 155 |
|
---|
| 156 | /** Returns pointer of view cell.
|
---|
| 157 | */
|
---|
| 158 | ViewCell *GetViewCell() const;
|
---|
| 159 |
|
---|
| 160 | /** Sets pointer to view cell.
|
---|
| 161 | */
|
---|
| 162 | void SetViewCell(ViewCell *viewCell);
|
---|
| 163 |
|
---|
| 164 | /// Rays piercing this leaf.
|
---|
| 165 | VssRayContainer mVssRays;
|
---|
| 166 |
|
---|
| 167 | /// leaf pvs
|
---|
| 168 | ObjectPvs *mPvs;
|
---|
| 169 |
|
---|
| 170 | /// Probability that the view point lies in this leaf
|
---|
| 171 | float mProbability;
|
---|
| 172 |
|
---|
| 173 | protected:
|
---|
| 174 |
|
---|
| 175 | /// if NULL this does not correspond to feasible viewcell
|
---|
| 176 | ViewCell *mViewCell;
|
---|
| 177 | };
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | This bsp tree spatially organizes the view cells.
|
---|
| 181 | */
|
---|
| 182 | class ViewCellBspTree
|
---|
| 183 | {
|
---|
| 184 | public:
|
---|
| 185 | /** Default constructor creating an empty tree.
|
---|
| 186 | */
|
---|
| 187 | VspBspTree();
|
---|
| 188 |
|
---|
| 189 | /** Default destructor.
|
---|
| 190 | */
|
---|
| 191 | ~VspBspTree();
|
---|
| 192 |
|
---|
| 193 | /** Returns list of BSP leaves with pvs smaller than
|
---|
| 194 | a certain threshold.
|
---|
| 195 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
| 196 | @param maxPvs the maximal pvs (-1 means unlimited)
|
---|
| 197 | */
|
---|
| 198 | void CollectLeaves(vector<BspLeaf *> &leaves,
|
---|
| 199 | const bool onlyUnmailed = false,
|
---|
| 200 | const int maxPvs = -1) const;
|
---|
| 201 |
|
---|
| 202 | /** Returns box which bounds the whole tree.
|
---|
| 203 | */
|
---|
| 204 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
| 205 |
|
---|
| 206 | /** Returns root of BSP tree.
|
---|
| 207 | */
|
---|
| 208 | BspNode *GetRoot() const;
|
---|
| 209 |
|
---|
| 210 | /** Collects the leaf view cells of the tree
|
---|
| 211 | @param viewCells returns the view cells
|
---|
| 212 | */
|
---|
| 213 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
| 214 |
|
---|
| 215 | /** A ray is cast possible intersecting the tree.
|
---|
| 216 | @param the ray that is cast.
|
---|
| 217 | @returns the number of intersections with objects stored in the tree.
|
---|
| 218 | */
|
---|
| 219 | int CastRay(Ray &ray);
|
---|
| 220 |
|
---|
| 221 | /** finds neighbouring leaves of this tree node.
|
---|
| 222 | */
|
---|
| 223 | int FindNeighbors(BspNode *n,
|
---|
| 224 | vector<BspLeaf *> &neighbors,
|
---|
| 225 | const bool onlyUnmailed) const;
|
---|
| 226 |
|
---|
| 227 | /** Constructs geometry associated with the half space intersections
|
---|
| 228 | leading to this node.
|
---|
| 229 | */
|
---|
| 230 | void ConstructGeometry(BspNode *n, BspNodeGeometry &geom) const;
|
---|
| 231 |
|
---|
| 232 | /** Construct geometry of view cell.
|
---|
| 233 | */
|
---|
| 234 | void ConstructGeometry(ViewCell *vc, BspNodeGeometry &geom) const;
|
---|
| 235 |
|
---|
| 236 | /** Returns random leaf of BSP tree.
|
---|
| 237 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
| 238 | */
|
---|
| 239 | BspLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
| 240 |
|
---|
| 241 | /** Returns random leaf of BSP tree.
|
---|
| 242 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
| 243 | */
|
---|
| 244 | BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
| 245 |
|
---|
| 246 | /** Casts line segment into the tree.
|
---|
| 247 | @param origin the origin of the line segment
|
---|
| 248 | @param termination the end point of the line segment
|
---|
| 249 | @returns view cells intersecting the line segment.
|
---|
| 250 | */
|
---|
| 251 | int CastLineSegment(const Vector3 &origin,
|
---|
| 252 | const Vector3 &termination,
|
---|
| 253 | ViewCellContainer &viewcells);
|
---|
| 254 |
|
---|
| 255 | /** Returns view cell the current point is located in.
|
---|
| 256 | */
|
---|
| 257 | ViewCell *GetViewCell(const Vector3 &point);
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 | /** Returns true if this view point is in a valid view space,
|
---|
| 261 | false otherwise.
|
---|
| 262 | */
|
---|
| 263 | bool ViewPointValid(const Vector3 &viewPoint) const;
|
---|
| 264 |
|
---|
| 265 | /** Returns view cell corresponding to
|
---|
| 266 | the invalid view space.
|
---|
| 267 | */
|
---|
| 268 | BspViewCell *GetOutOfBoundsCell();
|
---|
| 269 |
|
---|
| 270 |
|
---|
| 271 |
|
---|
| 272 | protected:
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | /// Pointer to the root of the tree
|
---|
| 277 | BspNode *mRoot;
|
---|
| 278 |
|
---|
| 279 | };
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 |
|
---|
| 283 |
|
---|
| 284 | #endif
|
---|