Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 491)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 495)
@@ -28,9 +28,9 @@
 VssPreprocessor {
 	samplesPerPass  100000
-	initialSamples 1000000
+	initialSamples 500000
 	vssSamples 200000
 	vssSamplesPerPass 100000
 	useImportanceSampling true
-	loadInitialSamples  true
+	loadInitialSamples  false
 	storeInitialSamples false
 }
@@ -236,5 +236,5 @@
 VspBspTree {
 	Construction {
-		samples 300000
+		samples 500000
 		epsilon 0.005
 		randomize false
@@ -266,10 +266,10 @@
 	Termination {
 		# parameters used for autopartition
-		minRays 		20
+		minRays 		100
 		minPolygons 		-1
 		maxDepth		30
 		minPvs 			20
 		#minArea 		0.0001
-		#minArea 		0.000
+		minArea 		0.000
 		maxRayContribution	0.005
 		maxCostRatio		0.9
@@ -277,5 +277,5 @@
 		#maxAccRayLength	100
 		
-		maxViewCells		5000
+		maxViewCells		20000
 		
 		# used for pvs criterium
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 495)
@@ -257,10 +257,7 @@
 	int RefineViewCells(const VssRayContainer &rays);
 
-	/** Collapses the tree with respect to the view cell partition,
-		i.e. leaves having the same view cell are collapsed.
-		@returns node of type leaf if the node could be collapsed, 
-		this node otherwise
-	*/
-	BspNode *CollapseTree(BspNode *node);
+	/** Collapses the tree with respect to the view cell partition.
+	*/
+	void CollapseTree();
 
   ViewCell *
@@ -315,4 +312,10 @@
 	};
 
+	/** Collapses the tree with respect to the view cell partition,
+		i.e. leaves having the same view cell are collapsed.
+		@returns node of type leaf if the node could be collapsed, 
+		this node otherwise
+	*/
+	BspNode *CollapseTree(BspNode *node);
 	/** Shuffles the leaves, i.e., tests if exchanging
 		the leaves helps in improving the view cells.
@@ -421,9 +424,15 @@
 	int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent);
 
-	/** Selects a plane axis aligned.
+	/** Selects an axis aligned for the next split.
+		@returns cost for this split
 	*/
 	float SelectAxisAlignedPlane(Plane3 &plane, 
 								 const VspBspTraversalData &tData,
-								 int &bestAxis);
+								 int &axis,
+								 float &position,
+								 int &raysBack,
+								 int &raysFront,
+								 int &pvsBack,
+								 int &pvsFront);
 
 	/** Sorts split candidates for surface area heuristics for axis aligned splits.
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 491)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 495)
@@ -188,5 +188,5 @@
 					 const int maxCostMisses):
 VspKdNode(p), mRays(), mPvsSize(0), mValidPvs(false), mViewCell(NULL),
-mMaxCostMisses(maxCostMisses)
+mMaxCostMisses(maxCostMisses), mPvs(NULL), mVolume(0)
 {
 	mRays.reserve(nRays);
@@ -195,4 +195,5 @@
 VspKdLeaf::~VspKdLeaf()
 {
+	DEL_PTR(mPvs);
 }
 
@@ -785,10 +786,9 @@
 	if (1) //-- area substitute for probability
 	{
-		
+		pOverall = box.SurfaceArea();
+
 		const bool useMidSplit = true;
-		//const bool useMidSplit = false;
-			
-		pOverall = box.SurfaceArea();
-			
+		//const bool useMidSplit = false;	
+					
 		if (!useMidSplit)
 		{
@@ -892,4 +892,6 @@
 	pvsFront = nPvsFront[bestAxis];
 
+	if (bestAxis == 1)
+		Debug << "y axis!" << endl;
 	return nCostRatio[bestAxis];
 }
@@ -1887,10 +1889,21 @@
 	RayInfoContainer::const_iterator it, it_end = globalRays.end();
 
-	// TODO
+	Intersectable::NewMail();
 
 	// warning: implicit conversion from VssRay to Ray
 	for (it = globalRays.begin(); it != globalRays.end(); ++ it)
-		pvsSize += box.GetMinMaxT(*(*it).mRay, NULL, NULL);
-
+	{
+		VssRay *ray = (*it).mRay;
+
+		// ray intersects node bounding box
+		if (box.GetMinMaxT(*ray, NULL, NULL))
+		{
+			if (ray->mTerminationObject && !ray->mTerminationObject->Mailed())
+			{
+				ray->mTerminationObject->Mail();
+				++ pvsSize;
+			}
+		}
+	}
 	return pvsSize;
 }
@@ -2345,10 +2358,14 @@
 	}
 
+	return node;
+}
+
+
+void VspKdTree::CollapseTree()
+{
+	CollapseTree(mRoot);
 	// revalidate leaves
 	RepairVcLeafLists();
-
-	return node;
-}
-
+}
 
 int VspKdTree::RefineViewCells(const VssRayContainer &rays)
@@ -2379,5 +2396,5 @@
 		const bool wasShuffled = false;
 			//ShuffleLeaves(mc.GetLeaf1(), mc.GetLeaf2());
-		
+			// matt: restore
 		//-- stats
 		if (wasShuffled)
@@ -2403,18 +2420,14 @@
 float GetShuffledVcCost(VspKdLeaf *leaf, VspKdViewCell *vc1, VspKdViewCell *vc2)
 {
-#if 0 // TODO
 	const int pvs1 = SubtractedPvsSize(vc1->GetPvs(), *leaf->mPvs);
 	const int pvs2 = AddedPvsSize(vc2->GetPvs(), *leaf->mPvs);
 
-	const float area1 = vc1->GetArea() - leaf->mArea;
-	const float area2 = vc2->GetArea() + leaf->mArea;
-
-	const float cost1 = pvs1 * area1;
-	const float cost2 = pvs2 * area2;
+	const float vol1 = vc1->GetVolume() - leaf->mVolume;
+	const float vol2 = vc2->GetVolume() + leaf->mVolume;
+
+	const float cost1 = pvs1 * vol1;
+	const float cost2 = pvs2 * vol2;
 
 	return cost1 + cost2;
-#else
-	return -1;
-#endif
 }
 
@@ -2424,11 +2437,10 @@
 							VspKdViewCell *vc2) const
 {
-#if 0 // TODO
 	// compute new pvs and area
 	vc1->GetPvs().SubtractPvs(*leaf->mPvs);
 	vc2->GetPvs().AddPvs(*leaf->mPvs);
 	
-	vc1->SetArea(vc1->GetArea() - leaf->mArea);
-	vc2->SetArea(vc2->GetArea() + leaf->mArea);
+	vc1->SetArea(vc1->GetVolume() - leaf->mVolume);
+	vc2->SetArea(vc2->GetVolume() + leaf->mVolume);
 
 	/// add to second view cell
@@ -2442,5 +2454,4 @@
 
 	leaf->SetViewCell(vc2);  // finally change view cell
-#endif
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 495)
@@ -35,42 +35,4 @@
 float BspMergeCandidate::sOverallCost = 0;
 
-
-/// Adds object to the pvs of the front and back node
-inline void AddObject2Pvs(Intersectable *object,
-						  const int side,
-						  int &pvsBack,
-						  int &pvsFront)
-{
-	if (!object)
-		return;
-
-	if (side <= 0)
-	{
-		if (!object->Mailed() && !object->Mailed(2))
-		{
-			++ pvsBack;
-
-			if (object->Mailed(1))
-				object->Mail(2);
-			else
-				object->Mail();
-		}
-	}
-
-	if (side >= 0)
-	{
-		if (!object->Mailed(1) && !object->Mailed(2))
-		{
-			++ pvsFront;
-
-			if (object->Mailed())
-				object->Mail(2);
-			else
-				object->Mail(1);
-		}
-	}
-}
-
-
 /****************************************************************/
 /*                  class VspBspTree implementation             */
@@ -83,5 +45,4 @@
 mViewCellsManager(NULL),
 mStoreRays(false),
-mOnlyDrivingAxis(false),
 mOutOfBoundsCell(NULL)
 {
@@ -794,5 +755,10 @@
 float VspBspTree::SelectAxisAlignedPlane(Plane3 &plane,
 										 const VspBspTraversalData &tData,
-										 int &bestAxis)
+										 int &axis,
+										 float &position,
+										 int &raysBack,
+										 int &raysFront,
+										 int &pvsBack,
+										 int &pvsFront)
 {
 	AxisAlignedBox3 box;
@@ -810,9 +776,9 @@
 	float nPosition[3];
 	float nCostRatio[3];
-	bestAxis = -1;
+	int bestAxis = -1;
 
 	const int sAxis = box.Size().DrivingAxis();
 
-	for (int axis = 0; axis < 3; ++ axis)
+	for (axis = 0; axis < 3; ++ axis)
 	{
 		if (!mOnlyDrivingAxis || axis == sAxis)
@@ -851,5 +817,14 @@
 	}
 
-	//-- assign best axis
+	//-- assign values
+	axis = bestAxis;
+	position = nPosition[bestAxis];
+
+	raysBack = 0;//nRaysBack[bestAxis];
+	raysFront = 0;//nRaysFront[bestAxis];
+
+	pvsBack = 0;//nPvsBack[bestAxis];
+	pvsFront = 0;//nPvsFront[bestAxis];
+
 	Vector3 normal(0,0,0); normal[bestAxis] = 1;
 	plane = Plane3(normal, nPosition[bestAxis]);
@@ -1070,4 +1045,5 @@
 }
 
+
 bool VspBspTree::SelectPlaneHeuristics(Plane3 &bestPlane,
 									   BspLeaf *leaf,
@@ -1086,8 +1062,6 @@
 	for (int i = 0; i < limit; ++ i)
 	{
-		// assure that no index is taken twice
+		//-- assure that no index is taken twice
 		const int candidateIdx = (int)RandomValue(0, (Real)(-- maxIdx));
-		//Debug << "current Idx: " << maxIdx << " cand idx " << candidateIdx << endl;
-
 		Polygon3 *poly = (*data.mPolygons)[candidateIdx];
 
@@ -1109,5 +1083,18 @@
 	//-- axis aligned splits
 	int axis;
-	candidateCost = SelectAxisAlignedPlane(plane, data, axis);
+	float position;
+	int raysBack;
+	int raysFront;
+	int pvsFront;
+	int pvsBack;
+
+	candidateCost = SelectAxisAlignedPlane(plane, 
+										   data, 
+										   axis,
+										   position,
+										   raysBack,
+										   raysFront,
+										   pvsFront,
+										   pvsBack);
 
 	if (candidateCost < lowestCost)
@@ -1116,4 +1103,6 @@
 		{
 			useAxisAlignedPlane = true;
+			//if (data.mPolygons->size() > 0)
+			//	Debug << "haha" << endl;
 			//! error also computed if cost ratio is missed
 			++ mStat.splits[axis];
@@ -1142,4 +1131,5 @@
 	Intersectable::NewMail(); sFrontAndBackId = ViewCell::sMailId;
 }
+
 
 float VspBspTree::SplitPlaneCost(const Plane3 &candidatePlane,
@@ -1159,12 +1149,8 @@
 	float pBack = 0;
 
-	const bool pvsUseLen = false;
-
 	if (mSplitPlaneStrategy & PVS)
 	{
-// matt: change back!!
-Intersectable::NewMail(3);
 		// create unique ids for pvs heuristics
-		//GenerateUniqueIdsForPvs();
+		GenerateUniqueIdsForPvs();
 
 		if (mPvsUseArea) // use front and back cell areas to approximate volume
@@ -1227,15 +1213,11 @@
 			// assure that we only count the object
 			// once for the front and once for the back side of the plane
-
-			// add the termination object
-			//AddObjToPvs(ray->mTerminationObject, cf, frontPvs, backPvs);
-			AddObject2Pvs(ray->mTerminationObject, cf, frontPvs, backPvs);
-			// add the source object
-			//AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs);
+			AddObjToPvs(ray->mTerminationObject, cf, frontPvs, backPvs);
+			AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs);
 
 			// use number of rays to approximate volume
 			if (!mPvsUseArea)
 			{
-				++ pOverall;
+				pOverall = (float)data.mRays->size();
 
 				if (cf >= 0)
@@ -1253,5 +1235,5 @@
 
 	if (mSplitPlaneStrategy & BALANCED_RAYS)
-		cost += mBalancedRaysFactor * fabs(sumBalancedRays) /  raysSize;
+		cost += mBalancedRaysFactor * fabs(sumBalancedRays) / raysSize;
 
 	// pvs criterium
@@ -2125,5 +2107,5 @@
 BspNode *VspBspTree::CollapseTree(BspNode *node)
 {
-    if (node->IsLeaf())
+	if (node->IsLeaf())
 		return node;
 
@@ -2156,8 +2138,13 @@
 	}
 
+	return node;
+}
+
+
+void VspBspTree::CollapseTree()
+{
+	CollapseTree(mRoot);
 	// revalidate leaves
 	RepairVcLeafLists();
-
-	return node;
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 495)
@@ -1340,5 +1340,5 @@
 	}
 	
-	// choose candidate planes extracted from rays
+	//-- choose candidate planes extracted from rays
 	for (int i = 0; i < mMaxRayCandidates; ++ i)
 	{
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 486)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 495)
@@ -390,8 +390,9 @@
 	
 	static void NewMail();
-
 	static int sMailId;
 
-	
+	ObjectPvs *mPvs;
+	float mVolume;
+
 protected:
 
@@ -616,10 +617,14 @@
 
 	/** Collapses the tree with respect to the view cell partition.
-		@returns node of type leaf if the node could be collapsed, this node otherwise
+	*/
+	void CollapseTree();
+
+protected:
+
+	/** Collapses the tree with respect to the view cell partition.
+		@returns node of type leaf if the node could be collapsed, 
+		this node otherwise
 	*/
 	VspKdNode *CollapseTree(VspKdNode *node);
-
-protected:
-
 	// incremental construction
 	virtual void UpdateRays(VssRayContainer &remove, VssRayContainer &add);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 495)
@@ -1387,6 +1387,6 @@
 
 	// collapse siblings belonging to the same view cell
-	mVspKdTree->CollapseTree(mVspKdTree->GetRoot());
-	
+	mVspKdTree->CollapseTree();
+
 	// collapse siblings belonging to the same view cell
 	mVspKdTree->RefineViewCells(rays);
@@ -1763,6 +1763,9 @@
 	// collapse invalid regions
 	cout << "collapsing invalid tree regions ... ";
-	mVspBspTree->CollapseTree(mVspBspTree->GetRoot()); 
-	cout << "finished" << endl;
+	long startTime = GetTime();
+	mVspBspTree->CollapseTree(); 
+	Debug << "tree collapsed in " << TimeDiff(startTime, GetTime()) * 1e-3 << " seconds" << endl;
+    cout << "finished" << endl;
+	
 	cout << "reseting view cell stats ... ";
 	ResetViewCells();
@@ -1782,6 +1785,6 @@
 			ExportViewCells(exporter);
 
-			//if (mExportRays)
-			//	exporter->ExportRays(postProcessRays, RgbColor(1, 1, 0));
+			if (0 && mExportRays)
+				exporter->ExportRays(rays, RgbColor(1, 1, 1));
 			
 			if (mExportGeometry)
@@ -1793,5 +1796,5 @@
 	}
 
-	long startTime = GetTime();
+	startTime = GetTime();
 	cout << "Computing remaining ray contributions ... ";
 	// recast rest of rays
@@ -1942,5 +1945,6 @@
 	
 	// collapse sibling leaves that share the same view cell
-	mVspBspTree->CollapseTree(mVspBspTree->GetRoot());
+	mVspBspTree->CollapseTree();
+
 	// reset view cells and stats
 	ResetViewCells();
@@ -2132,5 +2136,5 @@
 		//-- export rays piercing this view cell
 #if 1
-		exporter->ExportRays(vcRays, RgbColor(0, 1, 0));
+		exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
 #endif
 #if 0
