Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1006)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1008)
@@ -9,6 +9,5 @@
 #include "VssRay.h"
 #include "RayInfo.h"
-#include "ViewCellBsp.h"
-
+#include "gzstream.h"
 
 
@@ -16,9 +15,6 @@
 
 class ViewCellLeaf;
-//class BspViewCell;
+class VspViewCell;
 class Plane3;
-class VspBspTree;  
-class BspInterior;
-class BspNode;
 class AxisAlignedBox3;
 class Ray;
@@ -29,4 +25,259 @@
 class ViewCellsTree;
 class Environment;
+class VspInterior;
+class VspLeaf;
+class VspNode;
+
+
+/** A definition for an axis aligned plane.
+*/
+struct AxisAlignedPlane
+{
+public:
+	int mAxis;
+	float mPosition;
+};
+
+
+class VspTreeStatistics: public StatisticsBase
+{
+public:
+	// total number of nodes
+	int nodes;
+	// number of splits
+	int splits[3];
+	
+	// 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 minProbabilityNodes;
+	/// nodes termination because of max cost ratio;
+	int maxCostNodes;
+	// max number of rays per node
+	int maxObjectRefs;
+	/// samples contributing to pvs
+	int contributingSamples;
+	/// sample contributions to pvs
+	int sampleContributions;
+	/// largest pvs
+	int maxPvs;
+	/// number of invalid leaves
+	int invalidLeaves;
+	/// accumulated number of rays refs
+	int accumRays;
+	int pvs;
+	// accumulated depth (used to compute average)
+	int accumDepth;
+
+	// Constructor
+	VspTreeStatistics() 
+	{
+		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();}; 
+	double AvgRays() const { return accumRays / (double)Leaves();}; 
+
+	void Reset() 
+	{
+		nodes = 0;
+		for (int i = 0; i < 3; ++ i)
+			splits[i] = 0;
+		
+		maxDepth = 0;
+		minDepth = 99999;
+		accumDepth = 0;
+        pvs = 0;
+		maxDepthNodes = 0;
+		minPvsNodes = 0;
+		minRaysNodes = 0;
+		maxRayContribNodes = 0;
+		minProbabilityNodes = 0;
+		maxCostNodes = 0;
+
+		contributingSamples = 0;
+		sampleContributions = 0;
+
+		maxPvs = 0;
+		invalidLeaves = 0;
+		accumRays = 0;
+	}
+
+	void Print(ostream &app) const;
+
+	friend ostream &operator<<(ostream &s, const VspTreeStatistics &stat) 
+	{
+		stat.Print(s);
+		return s;
+	} 
+};
+
+/**
+    VspNode abstract class serving for interior and leaf node implementation
+*/
+class VspNode 
+{
+
+public:
+	VspNode();
+	virtual ~VspNode(){};
+	VspNode(VspInterior *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.
+	*/
+	VspInterior *GetParent();
+
+	/** Sets parent node.
+	*/
+	void SetParent(VspInterior *parent);
+
+	/** Returns true if this node is a sibling of node n.
+	*/
+	bool IsSibling(VspNode *n) const;
+
+	/** returns depth of the node.
+	*/
+	int GetDepth() const;
+
+	/** returns true if the whole subtree is valid
+	*/
+	bool TreeValid() const;
+
+	void SetTreeValid(const bool v);
+
+	//-- mailing options
+
+	void Mail() { mMailbox = sMailId; }
+	static void NewMail() { ++ sMailId; }
+	bool Mailed() const { return mMailbox == sMailId; }
+
+	static int sMailId;
+	int mMailbox;
+
+	int mTimeStamp;
+
+protected:
+
+	/// if this sub tree is a completely valid view space region
+	bool mTreeValid;
+	/// parent of this node
+	VspInterior *mParent;
+};
+
+
+/** BSP interior node implementation 
+*/
+class VspInterior: public VspNode 
+{
+public:
+	/** Standard contructor taking split plane as argument.
+	*/
+	VspInterior(const AxisAlignedPlane &plane);
+	~VspInterior();
+	/** @return false since it is an interior node 
+	*/
+	bool IsLeaf() const;
+
+	VspNode *GetBack();
+	VspNode *GetFront();
+
+	/** Returns split plane.
+	*/
+	AxisAlignedPlane GetPlane() const;
+
+	/** Replace front or back child with new child.
+	*/
+	void ReplaceChildLink(VspNode *oldChild, VspNode *newChild);
+	/** Replace front and back child.
+	*/
+	void SetupChildLinks(VspNode *b, VspNode *f);
+
+	friend ostream &operator<<(ostream &s, const VspInterior &A)
+	{
+		return s << A.mPlane.mAxis << " " << A.mPlane.mPosition;
+	}
+
+protected:
+
+	/// Splitting plane corresponding to this node
+	AxisAlignedPlane mPlane;
+
+	/// back node
+	VspNode *mBack;
+	/// front node
+	VspNode *mFront;
+};
+
+
+/** BSP leaf node implementation.
+*/
+class VspLeaf: public VspNode 
+{
+
+public:
+	VspLeaf();
+	VspLeaf(ViewCellLeaf *viewCell);
+	VspLeaf(VspInterior *parent);
+	VspLeaf(VspInterior *parent, ViewCellLeaf *viewCell);
+
+	~VspLeaf();
+
+	/** @return true since it is an interior node 
+	*/
+	bool IsLeaf() const;
+	
+	/** Returns pointer of view cell.
+	*/
+	ViewCellLeaf *GetViewCell() const;
+
+	/** Sets pointer to view cell.
+	*/
+	void SetViewCell(ViewCellLeaf *viewCell);
+
+	/// Rays piercing this leaf.
+	VssRayContainer mVssRays;
+	
+	/// leaf pvs
+	ObjectPvs *mPvs;
+
+	/// Probability that the view point lies in this leaf
+	float mProbability;
+
+protected:
+	
+	/// if NULL this does not correspond to feasible viewcell
+	ViewCellLeaf *mViewCell;
+};
+
 
 /**
@@ -57,17 +308,8 @@
 {
 	friend class ViewCellsParseHandlers;
-	friend class VspBspViewCellsManager;
+	friend class VspVspViewCellsManager;
 
 public:
 	
-	/** A definition for an axis aligned plane.
-	*/
-	struct AxisAlignedPlane
-	{
-	public:
-		int mAxis;
-		float mPosition;
-	};
-
 	/** Additional data which is passed down the BSP tree during traversal.
 	*/
@@ -76,5 +318,5 @@
 	public:
 		/// the current node
-		BspNode *mNode;
+		VspNode *mNode;
 		/// current depth
 		int mDepth;
@@ -114,5 +356,5 @@
 		{}
 		
-		VspOspTraversalData(BspNode *node, 
+		VspOspTraversalData(VspNode *node, 
 							const int depth, 
 							RayInfoContainer *rays,
@@ -193,6 +435,5 @@
 #if 1
 			return mRenderCost;
-#endif
-#if 0
+#else
 			return (float) (-mDepth); // for kd tree
 #endif
@@ -217,5 +458,5 @@
 	/** Returns BSP Tree statistics.
 	*/
-	const BspTreeStatistics &GetStatistics() const; 
+	const VspTreeStatistics &GetStatistics() const; 
   
 
@@ -233,5 +474,5 @@
 		@param maxPvs the maximal pvs (-1 means unlimited)
 	*/
-	void CollectLeaves(vector<BspLeaf *> &leaves, 
+	void CollectLeaves(vector<VspLeaf *> &leaves, 
 					   const bool onlyUnmailed = false,
 					   const int maxPvs = -1) const;
@@ -241,7 +482,7 @@
 	AxisAlignedBox3 GetBoundingBox()const;
 
-	/** Returns root of BSP tree.
-	*/
-	BspNode *GetRoot() const;
+	/** Returns root of the view space partitioning tree.
+	*/
+	VspNode *GetRoot() const;
 
 	/** Collects the leaf view cells of the tree
@@ -259,6 +500,6 @@
 	/** finds neighbouring leaves of this tree node.
 	*/
-	int FindNeighbors(BspNode *n, 
-					  vector<BspLeaf *> &neighbors, 
+	int FindNeighbors(VspNode *n, 
+					  vector<VspLeaf *> &neighbors, 
 					  const bool onlyUnmailed) const;
 
@@ -266,10 +507,10 @@
 		@param halfspace defines the halfspace from which the leaf is taken.
 	*/
-	BspLeaf *GetRandomLeaf(const Plane3 &halfspace);
+	VspLeaf *GetRandomLeaf(const Plane3 &halfspace);
 
 	/** Returns random leaf of BSP tree.
 		@param onlyUnmailed if only unmailed leaves should be returned.
 	*/
-	BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
+	VspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
 
 	/** Returns epsilon of this tree.
@@ -291,7 +532,4 @@
 	void SetViewCellsManager(ViewCellsManager *vcm);
 
-	/** Returns distance from node 1 to node 2.
-	*/
-	int TreeDistance(BspNode *n1, BspNode *n2) const;
 
 	/** Collapses the tree with respect to the view cell partition.
@@ -316,5 +554,5 @@
 		the invalid view space.
 	*/
-	BspViewCell *GetOutOfBoundsCell();
+	VspViewCell *GetOutOfBoundsCell();
 
 	/** Writes tree to output stream
@@ -332,10 +570,4 @@
 	int CastBeam(Beam &beam);
 
-	/** Finds approximate neighbours, i.e., finds correct neighbors
-		in most cases but sometimes more.
-	*/
-	int FindApproximateNeighbors(BspNode *n, 
-							     vector<BspLeaf *> &neighbors,
-								 const bool onlyUnmailed) const;
 
 	/** Checks if tree validity-flags are right 
@@ -410,5 +642,5 @@
 	/** Evaluates render cost decrease of next split.
 	*/
-	float EvalRenderCostDecrease(const Plane3 &candidatePlane,
+	float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
 								 const VspOspTraversalData &data) const;
 
@@ -419,5 +651,5 @@
 	/** Collects view cells in the subtree under root.
 	*/
-	void CollectViewCells(BspNode *root, 
+	void CollectViewCells(VspNode *root, 
 						  bool onlyValid, 
 						  ViewCellContainer &viewCells,
@@ -427,5 +659,5 @@
 		the invalid view space. If it does not exist, it is created.
 	*/
-	BspViewCell *GetOrCreateOutOfBoundsCell();
+	VspViewCell *GetOrCreateOutOfBoundsCell();
 
 	/** Collapses the tree with respect to the view cell partition,
@@ -436,5 +668,5 @@
 		this node otherwise
 	*/
-	BspNode *CollapseTree(BspNode *node, int &collapsed);
+	VspNode *CollapseTree(VspNode *node, int &collapsed);
 
 	/** Helper function revalidating the view cell leaf list after merge.
@@ -451,5 +683,5 @@
 		@returns new root of the subtree
 	*/
-	BspNode *Subdivide(VspOspSplitQueue &tQueue,
+	VspNode *Subdivide(VspOspSplitQueue &tQueue,
 					   VspOspSplitCandidate &splitCandidate);
 
@@ -469,5 +701,5 @@
 	*/
 
-	BspInterior *SubdivideNode(const Plane3 &splitPlane,
+	VspInterior *SubdivideNode(const AxisAlignedPlane &splitPlane,
 							   VspOspTraversalData &tData,
 							   VspOspTraversalData &frontData,
@@ -546,5 +778,5 @@
 		
 	*/
-	void AddToPvs(BspLeaf *leaf,
+	void AddToPvs(VspLeaf *leaf,
 				  const RayInfoContainer &rays, 
 				  float &sampleContributions,
@@ -553,5 +785,5 @@
 	/** Propagates valid flag up the tree.
 	*/
-	void PropagateUpValidity(BspNode *node);
+	void PropagateUpValidity(VspNode *node);
 
 	/** Writes the node to disk
@@ -559,7 +791,7 @@
 	*/
 #if ZIPPED_VIEWCELLS
-	void ExportNode(BspNode *node, ogzstream &stream);
+	void ExportNode(VspNode *node, ogzstream &stream);
 #else
-	void ExportNode(BspNode *node, ofstream &stream);
+	void ExportNode(VspNode *node, ofstream &stream);
 #endif
 
@@ -575,10 +807,10 @@
 
 	/// Pointer to the root of the tree
-	BspNode *mRoot;
-		
-	BspTreeStatistics mBspStats;
+	VspNode *mRoot;
+		
+	VspTreeStatistics mVspStats;
 	
 	/// View cell corresponding to the space outside the valid view space
-	BspViewCell *mOutOfBoundsCell;
+	VspViewCell *mOutOfBoundsCell;
 
 	/// box around the whole view domain
