Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 445)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 448)
@@ -9,4 +9,5 @@
 #include "VssRay.h"
 #include "RayInfo.h"
+#include "ViewCellBsp.h"
 
 class ViewCell;
@@ -14,318 +15,16 @@
 class Plane3;
 class VspBspTree;  
-class VspBspInterior;
-class VspBspNode;
+class BspInterior;
+class BspNode;
 class AxisAlignedBox3;
 class Ray;
 
-class VspBspNodeGeometry
-{
-public:
-	VspBspNodeGeometry()
-	{};  
-
-	~VspBspNodeGeometry();
-
-	float GetArea() const;
-	
-	/** Computes new cell based on the old cell definition and a new split plane
-		@param side indicates which side of the halfspace 
-	*/
-	void SplitGeometry(VspBspNodeGeometry &front, 
-					   VspBspNodeGeometry &back,
-					   const VspBspTree &tree,
-					   const Plane3 &splitPlane) const;
-
-	Polygon3 *SplitPolygon(Polygon3 *poly, const VspBspTree &tree) const;
-
-	PolygonContainer mPolys;
-};
-
-/** Data structure used for optimized ray casting.
+/*class BspNodeGeometry;
+class BspTreeStatistics;
+class BspViewCellsStatistics;
+class BspNode;
+class BspLeaf;
+class BspInterior;
 */
-struct VspBspRayTraversalData 
-{
-    VspBspNode *mNode;
-    Vector3 mExitPoint;
-    float mMaxT;
-    
-    VspBspRayTraversalData() {}
-
-    VspBspRayTraversalData(VspBspNode *n, const Vector3 &extp, const float maxt):
-    mNode(n), mExitPoint(extp), mMaxT(maxt) 
-	{}
-};
-
-class VspBspTreeStatistics: public StatisticsBase
-{
-public:
-	// total number of nodes
-	int nodes;
-	// number of splits
-	int splits;
-	// totals number of rays
-	int rays;
-	// maximal reached depth
-	int maxDepth;
-	// minimal depth
-	int minDepth;
-	
-	// max depth nodes
-	int maxDepthNodes;
-	// minimum depth nodes
-	int minDepthNodes;
-	// max depth nodes
-	int minPvsNodes;
-	// nodes with minimum PVS
-	int minRaysNodes;
-	// max ray contribution nodes
-	int maxRayContribNodes;
-	// minimum area nodes
-	int minAreaNodes;
-
-	// max number of rays per node
-	int maxObjectRefs;
-	// accumulated depth (used to compute average)
-	int accumDepth;
-	// number of initial polygons
-	int polys;
-	/// samples contributing to pvs
-	int contributingSamples;
-	/// sample contributions to pvs
-	int sampleContributions;
-	/// largest pvs
-	int largestPvs;
-
-	// Constructor
-	VspBspTreeStatistics() 
-	{
-		Reset();
-	}
-
-	int Nodes() const {return nodes;}
-	int Interior() const { return nodes / 2; }
-	int Leaves() const { return (nodes / 2) + 1; }
-	
-	// TODO: computation wrong
-	double AvgDepth() const { return accumDepth / (double)Leaves();}; 
-  
-	void Reset() 
-	{
-		nodes = 0;
-		splits = 0;
-		
-		maxDepth = 0;
-		minDepth = 99999;
-		polys = 0;
-		accumDepth = 0;
-        
-		maxDepthNodes = 0;
-		minPvsNodes = 0;
-		minRaysNodes = 0;
-		maxRayContribNodes = 0;
-		minAreaNodes = 0;
-
-		contributingSamples = 0;
-		sampleContributions = 0;
-	}
-
-	void Print(ostream &app) const;
-
-	friend ostream &operator<<(ostream &s, const VspBspTreeStatistics &stat) 
-	{
-		stat.Print(s);
-		return s;
-	} 
-};
-
-class VspBspViewCellsStatistics: public StatisticsBase
-{
-public:
-
-	/// number of view cells
-	int viewCells;
-
-	/// size of the PVS
-	int pvs;
-  
-	/// largest PVS of all view cells
-	int maxPvs;
-
-	/// smallest PVS of all view cells
-	int minPvs;
-
-	/// view cells with empty PVS
-	int emptyPvs;
-
-	/// number of bsp leaves covering the view space
-	int bspLeaves;
-
-	/// largest number of leaves covered by one view cell  
-	int maxVspBspLeaves;
-
-    // Constructor
-	VspBspViewCellsStatistics() 
-	{
-		Reset();
-	}
-
-	double AvgVspBspLeaves() const {return (double)bspLeaves / (double)viewCells;};
-	double AvgPvs() const {return (double)pvs / (double)viewCells;};
-  
-	void Reset() 
-	{
-		viewCells = 0;	
-		pvs = 0;  
-		maxPvs = 0;
-
-		minPvs = 999999;
-		emptyPvs = 0;
-		bspLeaves = 0;
-		maxVspBspLeaves = 0;
-	}
-
-	void Print(ostream &app) const;
-
-	friend ostream &operator<<(ostream &s, const VspBspViewCellsStatistics &stat) 
-	{
-		stat.Print(s);
-		return s;
-	}  
-};
-
-/**
-    VspBspNode abstract class serving for interior and leaf node implementation
-*/
-class VspBspNode 
-{
-	friend class VspBspTree;
-
-public:
-	VspBspNode();
-	virtual ~VspBspNode(){};
-	VspBspNode(VspBspInterior *parent);
-
-	/** Determines whether this node is a leaf or not
-	@return true if leaf
-	*/
-	virtual bool IsLeaf() const = 0;
-
-	/** Determines whether this node is a root
-	@return true if root
-	*/
-	virtual bool IsRoot() const;
-
-	/** Returns parent node.
-	*/
-	VspBspInterior *GetParent();
-
-	/** Sets parent node.
-	*/
-	void SetParent(VspBspInterior *parent);
-
-	
-	static int sMailId;
-	int mMailbox;
-  
-	void Mail() { mMailbox = sMailId; }
-	static void NewMail() { ++ sMailId; }
-	bool Mailed() const { return mMailbox == sMailId; }
-
-protected:
-
-	/// parent of this node
-	VspBspInterior *mParent;
-};
-
-/** BSP interior node implementation 
-*/
-class VspBspInterior : public VspBspNode 
-{
-	friend class VspBspTree;
-public:
-	/** Standard contructor taking split plane as argument.
-	*/
-	VspBspInterior(const Plane3 &plane);
-	~VspBspInterior();
-	/** @return false since it is an interior node 
-	*/
-	bool IsLeaf() const;
-
-	VspBspNode *GetBack();
-	VspBspNode *GetFront();
-
-	Plane3 *GetPlane();
-
-	void ReplaceChildLink(VspBspNode *oldChild, VspBspNode *newChild);
-	void SetupChildLinks(VspBspNode *b, VspBspNode *f);
-
-	/** Splits polygons with respect to the split plane.
-		@param polys the polygons to be split. the polygons are consumed and
-			   distributed to the containers frontPolys, backPolys, coincident.
-		@param frontPolys returns the polygons in the front of the split plane
-		@param backPolys returns the polygons in the back of the split plane
-		@param coincident returns the polygons coincident to the split plane
-
-		@returns the number of splits	
-	*/
-	int SplitPolygons(PolygonContainer &polys, 
-					  PolygonContainer &frontPolys, 
-					  PolygonContainer &backPolys, 
-					  PolygonContainer &coincident);
-
-	friend ostream &operator<<(ostream &s, const VspBspInterior &A)
-	{
-		return s << A.mPlane;
-	}
-
-protected:
-
-	/// Splitting plane corresponding to this node
-	Plane3 mPlane;
-	/// back node
-	VspBspNode *mBack;
-	/// front node
-	VspBspNode *mFront;
-};
-
-/** BSP leaf node implementation.
-*/
-class VspBspLeaf : public VspBspNode 
-{
-	friend class VspBspTree;
-
-public:
-	VspBspLeaf();
-	VspBspLeaf(BspViewCell *viewCell);
-	VspBspLeaf(VspBspInterior *parent);
-	VspBspLeaf(VspBspInterior *parent, BspViewCell *viewCell);
-
-	/** @return true since it is an interior node 
-	*/
-	bool IsLeaf() const;
-	
-	/** Returns pointer of view cell.
-	*/
-	BspViewCell *GetViewCell() const;
-
-	/** Sets pointer to view cell.
-	*/
-	void SetViewCell(BspViewCell *viewCell);
-
-	/** Adds ray sample contributions to the PVS.
-		@param sampleContributions the number contributions of the samples
-		@param contributingSampels the number of contributing rays
-		
-	*/
-	void AddToPvs(const RayInfoContainer &rays, 
-				  int &sampleContributions,
-				  int &contributingSamples, 
-				  bool storeLeavesWithRays = false);
-
-protected:
-
-	/// if NULL this does not correspond to feasible viewcell
-	BspViewCell *mViewCell;
-};
 
 /**
@@ -345,18 +44,17 @@
 	{  
 		/// the current node
-		VspBspNode *mNode;
+		BspNode *mNode;
 		/// polygonal data for splitting
 		PolygonContainer *mPolygons;
 		/// current depth
 		int mDepth;
-		/// the view cell associated with this subdivsion
-		ViewCell *mViewCell;
+		
 		/// rays piercing this node
 		RayInfoContainer *mRays;
 		/// area of current node
 		float mArea;
-		/// geometry of current node induced by split planes
-		VspBspNodeGeometry *mGeometry;
-
+		/// geometry of node as induced by planes
+		BspNodeGeometry *mGeometry;
+		
 		/// pvs size
 		int mPvs;
@@ -374,5 +72,4 @@
 		mPolygons(NULL),
 		mDepth(0),
-		mViewCell(NULL),
 		mRays(NULL),
 		mPvs(0),
@@ -381,20 +78,31 @@
 		{}
 		
-		VspBspTraversalData(VspBspNode *node, 
+		VspBspTraversalData(BspNode *node, 
 						 PolygonContainer *polys, 
 						 const int depth, 
-						 ViewCell *viewCell,
 						 RayInfoContainer *rays,
 						 int pvs,
 						 float area,
-						 VspBspNodeGeometry *cell): 
+						 BspNodeGeometry *geom): 
 		mNode(node), 
 		mPolygons(polys), 
 		mDepth(depth), 
-		mViewCell(viewCell),
 		mRays(rays),
 		mPvs(pvs),
 		mArea(area),
-		mGeometry(cell)
+		mGeometry(geom)
+		{}
+
+		VspBspTraversalData(PolygonContainer *polys, 
+							const int depth, 
+							RayInfoContainer *rays,
+							BspNodeGeometry *geom): 
+		mNode(NULL), 
+		mPolygons(polys), 
+		mDepth(depth), 
+		mRays(rays),
+		mPvs(0),
+		mArea(0),
+		mGeometry(geom)
 		{}
     };
@@ -406,7 +114,11 @@
 	VspBspTree();
 
+	/** Default destructor.
+	*/
 	~VspBspTree();
 
-	const VspBspTreeStatistics &GetStatistics() const; 
+	/** Returns BSP Tree statistics.
+	*/
+	const BspTreeStatistics &GetStatistics() const; 
   
 
@@ -420,5 +132,5 @@
 	/** Returns list of BSP leaves.
 	*/
-	void CollectLeaves(vector<VspBspLeaf *> &leaves) const;
+	void CollectLeaves(vector<BspLeaf *> &leaves) const;
 
 	/** Returns box which bounds the whole tree.
@@ -428,5 +140,5 @@
 	/** Returns root of BSP tree.
 	*/
-	VspBspNode *GetRoot() const;
+	BspNode *GetRoot() const;
 
 	/** Exports VspBsp tree to file.
@@ -450,9 +162,10 @@
 	/** Returns statistics.
 	*/
-	VspBspTreeStatistics &GetStat();
+	BspTreeStatistics &GetStat();
 
 	/** finds neighbouring leaves of this tree node.
 	*/
-	int FindNeighbors(VspBspNode *n, vector<VspBspLeaf *> &neighbors, 
+	int FindNeighbors(BspNode *n, 
+					  vector<BspLeaf *> &neighbors, 
 					  const bool onlyUnmailed) const;
 
@@ -460,5 +173,5 @@
 		leading to this node.
 	*/
-	void ConstructGeometry(VspBspNode *n, PolygonContainer &cell) const;
+	void ConstructGeometry(BspNode *n, PolygonContainer &cell) const;
 
 	/** Constructs geometry associated with the half space intersections 
@@ -469,19 +182,19 @@
 	/** Construct geometry and stores it in a geometry node container.
 	*/
-	void ConstructGeometry(VspBspNode *n, VspBspNodeGeometry &cell) const;
+	void ConstructGeometry(BspNode *n, BspNodeGeometry &cell) const;
 
 	/** Returns random leaf of BSP tree.
 		@param halfspace defines the halfspace from which the leaf is taken.
 	*/
-	VspBspLeaf *GetRandomLeaf(const Plane3 &halfspace);
+	BspLeaf *GetRandomLeaf(const Plane3 &halfspace);
 
 	/** Returns random leaf of BSP tree.
 		@param onlyUnmailed if only unmailed leaves should be returned.
 	*/
-	VspBspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
+	BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
 
 	/** Traverses tree and counts all view cells as well as their PVS size.
 	*/
-	void EvaluateViewCellsStats(VspBspViewCellsStatistics &stat) const;
+	void EvaluateViewCellsStats(BspViewCellsStatistics &stat) const;
 
 
@@ -489,4 +202,8 @@
 	*/
 	BspViewCell *GetRootCell() const;
+
+	/** Returns epsilon of this tree.
+	*/
+	float GetEpsilon() const;
 
 protected:
@@ -521,11 +238,12 @@
 		@returns new root of the subtree
 	*/
-	VspBspNode *Subdivide(VspBspTraversalStack &tStack, VspBspTraversalData &tData);
-
-	/** Constructs the tree from the given list of polygons and rays.
+	BspNode *Subdivide(VspBspTraversalStack &tStack, 
+					   VspBspTraversalData &tData);
+
+	/** Constructs the tree from the given traversal data.
 		@param polys stores set of polygons on which subdivision may be based
 		@param rays storesset of rays on which subdivision may be based
 	*/
-	void Construct(PolygonContainer *polys, RayInfoContainer *rays);
+	void Construct(const PolygonContainer &polys, RayInfoContainer *rays);
 
 	/** Selects the best possible splitting plane. 
@@ -536,5 +254,5 @@
 		@returns the split plane
 	*/
-	Plane3 SelectPlane(VspBspLeaf *leaf, 
+	Plane3 SelectPlane(BspLeaf *leaf, 
 					   VspBspTraversalData &data);
 
@@ -564,5 +282,5 @@
 	*/
 
-	VspBspInterior *SubdivideNode(VspBspTraversalData &tData,
+	BspInterior *SubdivideNode(VspBspTraversalData &tData,
 							   VspBspTraversalData &frontData,
 							   VspBspTraversalData &backData,
@@ -575,5 +293,5 @@
 		@param rays bundle of rays on which the split can be based
 	*/
-	Plane3 SelectPlaneHeuristics(VspBspLeaf *leaf,
+	Plane3 SelectPlaneHeuristics(BspLeaf *leaf,
 								 VspBspTraversalData &data);
 
@@ -639,9 +357,4 @@
 	bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const;
 
-	/** Bounds ray and returns minT and maxT.
-		@returns true if ray hits BSP tree bounding box
-	*/
-	bool BoundRay(const Ray &ray, float &minT, float &maxT) const;
-
 	/** Subdivides the rays into front and back rays according to the split plane.
 		
@@ -662,5 +375,5 @@
 	/** Extracts the split planes representing the space bounded by node n.
 	*/
-	void ExtractHalfSpaces(VspBspNode *n, vector<Plane3> &halfSpaces) const;
+	void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const;
 
 	/** Adds the object to the pvs of the front and back leaf with a given classification.
@@ -686,10 +399,35 @@
 	float AccumulatedRayLength(const RayInfoContainer &rays) const;
 
-
+	/** Splits polygons with respect to the split plane.
+
+		@param plane the split plane
+		@param polys the polygons to be split. the polygons are consumed and
+			   distributed to the containers frontPolys, backPolys, coincident.
+		@param frontPolys returns the polygons in the front of the split plane
+		@param backPolys returns the polygons in the back of the split plane
+		@param coincident returns the polygons coincident to the split plane
+
+		@returns the number of splits	
+	*/
+	int SplitPolygons(const Plane3 &plane,
+					  PolygonContainer &polys, 
+					  PolygonContainer &frontPolys, 
+					  PolygonContainer &backPolys, 
+					  PolygonContainer &coincident) const;
+
+	/** Adds ray sample contributions to the PVS.
+		@param sampleContributions the number contributions of the samples
+		@param contributingSampels the number of contributing rays
+		
+	*/
+	void AddToPvs(BspLeaf *leaf,
+				  const RayInfoContainer &rays, 
+				  int &sampleContributions,
+				  int &contributingSamples);
 
 	/// Pointer to the root of the tree
-	VspBspNode *mRoot;
-		
-	VspBspTreeStatistics mStat;
+	BspNode *mRoot;
+		
+	BspTreeStatistics mStat;
 
 	/// Strategies for choosing next split plane.
@@ -749,4 +487,8 @@
 	bool mPvsUseArea;
 
+	float mEpsilon;
+
+	int mMaxTests;
+
 private:
 	
