Index: /GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.env
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.env	(revision 1654)
+++ /GTP/trunk/Lib/Vis/Preprocessing/scripts/vsposp_typical.env	(revision 1692)
@@ -173,6 +173,6 @@
 	
 	Evaluation {
-		samplesPerPass 100000
-		samples 100000
+		samplesPerPass 10000
+		samples 50000
 		statsPrefix ../scripts/viewCells
 	}
@@ -233,5 +233,5 @@
 	Construction {
 
-		samples 200000
+		samples 20000
 
 		# type 0 = sequential computation, 1 = interleaved, 2 = gradient
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1686)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1692)
@@ -1102,5 +1102,5 @@
 
 //#if TEST_EVALUATION
-	virtual void EvalViewCellPartition();
+	//virtual void EvalViewCellPartition();
 //#endif
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h	(revision 1149)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.h	(revision 1692)
@@ -94,7 +94,7 @@
 
 	friend void GetRayInfoSets(const RayInfoContainer &sourceRays,
-		const int maxSize,
-		RayInfoContainer &usedRays,
-		RayInfoContainer *savedRays = NULL);
+							   const int maxSize,
+							   RayInfoContainer &usedRays,
+							   RayInfoContainer *savedRays = NULL);
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1687)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1692)
@@ -59,4 +59,5 @@
 mMailbox(0),
 mMemoryIncr(0),
+mRenderCostDecr(0), 
 mPvsEntriesIncr(0),
 mTimeStamp(0)
@@ -70,4 +71,5 @@
 mMailbox(0),
 mMemoryIncr(0),
+mRenderCostDecr(0), 
 mPvsEntriesIncr(0),
 mTimeStamp(0)
@@ -1104,6 +1106,6 @@
 {
 	//-- insert object queries
-	ObjectContainer *objects = 
-		mUseGlobalSorting ? tData.mSortedObjects[axis] : &tData.mNode->mObjects;
+	ObjectContainer *objects = mUseGlobalSorting ? 
+		tData.mSortedObjects[axis] : &tData.mNode->mObjects;
 
 	CreateLocalSubdivisionCandidates(*objects, &mSubdivisionCandidates, !mUseGlobalSorting, axis);
@@ -1284,5 +1286,5 @@
 				}
 				else
-				{	cout << "e rays: " << tData.mNumRays << " ";
+				{	//cout << "e rays: " << tData.mNumRays << " ";
 					//////////////////
 					//-- view cells not constructed yet	=> use surface area heuristic			
@@ -2127,4 +2129,7 @@
 		BvhInterior *oldInterior = dynamic_cast<BvhInterior *>(oldNode);
 		
+		sc->mFrontObjects.clear();
+		sc->mBackObjects.clear();
+
 		oldInterior->GetFront()->CollectObjects(sc->mFrontObjects);
 		oldInterior->GetBack()->CollectObjects(sc->mBackObjects);
@@ -2136,7 +2141,10 @@
 		currentNode = SubdivideNode(*sc, tFrontData, tBackData);
 	
-		oldNode->mRenderCostDecr += sc->GetRenderCostDecrease();
-		oldNode->mPvsEntriesIncr += sc->GetPvsEntriesIncr();
-cout << "here5" << endl;
+		//oldNode->mRenderCostDecr += sc->GetRenderCostDecrease();
+		//oldNode->mPvsEntriesIncr += sc->GetPvsEntriesIncr();
+		
+		oldNode->mRenderCostDecr = sc->GetRenderCostDecrease();
+		oldNode->mPvsEntriesIncr = sc->GetPvsEntriesIncr();
+		
 		///////////////////////////
 		//-- push the new split candidates on the queue
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1686)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1692)
@@ -38,5 +38,5 @@
 // HACK
 const static bool SAMPLE_AFTER_SUBDIVISION = true;
-const static bool CLAMP_TO_BOX = true;
+const static bool CLAMP_TO_BOX = false;
 
 template <typename T> class myless
@@ -5617,5 +5617,6 @@
 }
 
-#if 1
+
+#if 0
 #if TEST_EVALUATION
 void VspOspViewCellsManager::EvalViewCellPartition()
@@ -5744,4 +5745,6 @@
     cout << "Evaluating view cell partition ... " << endl;
 
+	VssRayContainer evaluationSamples;
+
 	while (castSamples < numSamples)
 	{		
@@ -5749,5 +5752,5 @@
 		//-- we have to use uniform sampling strategy for construction rays
 
-		VssRayContainer evaluationSamples;
+		//VssRayContainer evaluationSamples;
 		const int samplingType = mEvaluationSamplingType;
 
@@ -5791,6 +5794,9 @@
 		Debug << "statistis compted in " << timeDiff * 1e-3 << " secs" << endl;
 	
-		disposeRays(evaluationSamples, NULL);
-	}
+		//disposeRays(evaluationSamples, NULL);
+	}
+	
+	disposeRays(evaluationSamples, NULL);
+
 }
 #endif
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp	(revision 1587)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp	(revision 1692)
@@ -65,5 +65,5 @@
 	const float sum = a + b + c;
 
-	// scale so we get value between zero and one
+	// scale so we get vaccumated value of 1
 	if (sum)
 	{
@@ -73,5 +73,5 @@
 	}
 
-	//cout << "a " << a << "b: " << b << "c: " << c << endl;
+	//cout << "a: " << a << " b: " << b << " c: " << c << " sum: " << sum << endl;
 	point = mItem.mVertices[0] * a + mItem.mVertices[1] * b + mItem.mVertices[2] * c;
 	normal = mItem.GetNormal();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1687)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1692)
@@ -192,4 +192,5 @@
 mTreeValid(true), 
 mMemoryIncr(0),
+mRenderCostDecr(0),
 mPvsEntriesIncr(0),
 mTimeStamp(0)
@@ -750,14 +751,15 @@
 	if (newNode->IsLeaf()) // subdivision terminated
 	{
+		VspLeaf *leaf = dynamic_cast<VspLeaf *>(newNode);
+		
+#if 0
+        /////////////
+		//-- store pvs optained from rays
+
 		// view cell is created during subdivision
-		VspLeaf *leaf = dynamic_cast<VspLeaf *>(newNode);
 		ViewCell *viewCell = leaf->GetViewCell();
 
 		int conSamp = 0;
 		float sampCon = 0.0f;
-
-#if 0
-		/////////////
-		//-- store pvs optained from rays
 
 		AddSamplesToPvs(leaf, *tData.mRays, sampCon, conSamp);
@@ -1517,19 +1519,20 @@
 
 		float t;
+		
+		// classify ray
+		const int cf = rayInf.ComputeRayIntersection(candidatePlane.mAxis, 
+													 candidatePlane.mPosition, 
+													 t);
+
 		VssRay *ray = rayInf.mRay;
-
-		// classify ray
-		const int cf = 
-			rayInf.ComputeRayIntersection(candidatePlane.mAxis, 
-										  candidatePlane.mPosition, t);
 
 		// evaluate contribution of ray endpoint to front 
 		// and back pvs with respect to the classification
-		UpdateContributionsToPvs(*ray, true, cf, pvsFront, pvsBack, totalPvs);	
+		UpdateContributionsToPvs(*ray, true, cf, pvsFront, pvsBack, totalPvs);
 #if COUNT_ORIGIN_OBJECTS
 		UpdateContributionsToPvs(*ray, false, cf, pvsFront, pvsBack, totalPvs);
 #endif
 	}
-
+    
 	AxisAlignedBox3 frontBox;
 	AxisAlignedBox3 backBox;
@@ -3303,5 +3306,7 @@
 		// create new interior node and two leaf node
 		const AxisAlignedPlane splitPlane = oldInterior->GetPlane();
-		
+	
+		sc->mSplitPlane = splitPlane;
+	
 		// evaluate the changes in render cost and pvs entries
 		EvalSubdivisionCandidate(*sc, false);
@@ -3309,8 +3314,10 @@
 		newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData);
 	
-		oldNode->mRenderCostDecr += sc->GetRenderCostDecrease();
-		oldNode->mPvsEntriesIncr += sc->GetPvsEntriesIncr();
-cout << "here4" << endl;
-		
+		//oldNode->mRenderCostDecr += sc->GetRenderCostDecrease();
+		//oldNode->mPvsEntriesIncr += sc->GetPvsEntriesIncr();
+
+		oldNode->mRenderCostDecr = sc->GetRenderCostDecrease();
+		oldNode->mPvsEntriesIncr = sc->GetPvsEntriesIncr();
+
 		/////////////
 		//-- evaluate new split candidates for global greedy cost heuristics
@@ -3338,41 +3345,5 @@
 	if (newNode->IsLeaf()) // subdivision terminated
 	{
-		// view cell is created during subdivision
-		VspLeaf *leaf = dynamic_cast<VspLeaf *>(newNode);
-		ViewCell *viewCell = leaf->GetViewCell();
-
-		int conSamp = 0;
-		float sampCon = 0.0f;
-
-#if 0
-		/////////////
-		//-- store pvs optained from rays
-
-		AddSamplesToPvs(leaf, *tData.mRays, sampCon, conSamp);
-
-		// update scalar pvs size value
-		ObjectPvs &pvs = viewCell->GetPvs();
-		mViewCellsManager->UpdateScalarPvsSize(viewCell, pvs.CountObjectsInPvs(), pvs.GetSize());
-
-		mVspStats.contributingSamples += conSamp;
-		mVspStats.sampleContributions += (int)sampCon;
-#endif
-		if (mStoreRays)
-		{
-			//////////
-			//-- store rays piercing this view cell
-			RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
-			for (it = tData.mRays->begin(); it != it_end; ++ it)
-			{
-				(*it).mRay->Ref();			
-				leaf->mVssRays.push_back((*it).mRay);
-				//leaf->mVssRays.push_back(new VssRay(*(*it).mRay));
-			}
-		}
-
-		// finally evaluate statistics for this leaf
-		EvaluateLeafStats(tData);
-		// detach subdivision candidate: this leaf is no candidate for
-		// splitting anymore
+		// detach subdivision candidate: this leaf is no candidate for splitting anymore
 		tData.mNode->SetSubdivisionCandidate(NULL); 
 		// detach node so it won't get deleted
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1687)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1692)
@@ -1630,5 +1630,6 @@
 
 
-void HierarchyManager::EvaluateSubdivision(const VssRayContainer &sampleRays,											 							   const ObjectContainer &objects,
+void HierarchyManager::EvaluateSubdivision(const VssRayContainer &sampleRays,											 							   
+										   const ObjectContainer &objects,
 										   const string &filename)
 {
@@ -1681,9 +1682,10 @@
 	ViewCellContainer viewCells;
 	mVspTree->CollectViewCells(viewCells, false);
-	CLEAR_CONTAINER(viewCells);
-
+	
 	// helper trees can be destroyed
 	DEL_PTR(mVspTree);
 	DEL_PTR(mBvHierarchy);
+
+	CLEAR_CONTAINER(viewCells);
 
 	// reset hierarchies
@@ -1726,33 +1728,51 @@
 	{
 		SubdivisionCandidate *nextCandidate = NextSubdivisionCandidate(tQueue);
-		
-		cout << "next candidate: " << nextCandidate->GetPriority() << endl;
+		bool isLeaf;
+		int timeStamp;
+		float rcDecr; 
+		int entriesIncr; 
 
         if (nextCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
 		{
-			cout << "here8" << endl;
-			mVspTree->SubdivideAndCopy(tQueue, nextCandidate);
-
+			timeStamp = (int)-nextCandidate->GetPriority();
+
+			VspNode *newNode = mVspTree->SubdivideAndCopy(tQueue, nextCandidate);
 			VspNode *oldNode = (VspNode *)nextCandidate->mEvaluationHack;
 			
-			totalRenderCost -= oldNode->mRenderCostDecr;
-			entriesInPvs += oldNode->mPvsEntriesIncr;
+			isLeaf = newNode->IsLeaf();
+			rcDecr = oldNode->mRenderCostDecr;
+			entriesIncr = oldNode->mPvsEntriesIncr;
 		}
 		else
-		{cout << "here9" << endl;
-			mBvHierarchy->SubdivideAndCopy(tQueue, nextCandidate);
+		{
+			timeStamp = (int)-nextCandidate->GetPriority();
 			
+			BvhNode *newNode = mBvHierarchy->SubdivideAndCopy(tQueue, nextCandidate);
 			BvhNode *oldNode = (BvhNode *)nextCandidate->mEvaluationHack;
 			
-			totalRenderCost -= oldNode->mRenderCostDecr;
-			entriesInPvs += oldNode->mPvsEntriesIncr;
+			isLeaf = newNode->IsLeaf();
+			rcDecr = oldNode->mRenderCostDecr;
+			entriesIncr = oldNode->mPvsEntriesIncr;
 		}		
-
-		++ steps;
-		cout << "rc: " << nextCandidate->GetRenderCostDecrease() << endl;
-		cout << "pvs: " << nextCandidate->GetPvsEntriesIncr() << endl;
-
-		const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();
-		UpdateStats(stats, steps, totalRenderCost, entriesInPvs, memoryCost, false);
+				
+		if (!isLeaf)
+		{
+			totalRenderCost -= rcDecr;
+			entriesInPvs += entriesIncr;
+			// if (rcDecr <= 0)
+			if (nextCandidate->Type() == SubdivisionCandidate::VIEW_SPACE) 
+				cout << "v";//cout << "vsp t: " << timeStamp << " rc: " << rcDecr << " pvs: " << entriesIncr << endl;
+			else
+				cout << "o";//"osp t: " << timeStamp << " rc: " << rcDecr << " pvs: " << entriesIncr << endl;
+
+			++ steps;
+
+			if ((steps % 500) == 499)
+				cout << steps << " steps taken" << endl;
+			const float memoryCost = (float)entriesInPvs * (float)ObjectPvs::GetEntrySize();
+			UpdateStats(stats, steps, totalRenderCost, entriesInPvs, memoryCost, false);
+		}
+
+		DEL_PTR(nextCandidate);
 	}
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1588)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1692)
@@ -35,5 +35,5 @@
 	Vector3 point;
 	Vector3 normal;
-	  
+	//cout << "y";
 	const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f);
 
@@ -66,5 +66,5 @@
 	
 	Vector3 normal;
-	
+	//cout << "x";
 	object->GetRandomSurfacePoint(origin, normal);
 	direction = UniformRandomVector(normal);
@@ -137,5 +137,5 @@
 	mPreprocessor.mViewCellsManager->GetViewPoint(origin);
 	direction = mPreprocessor.mKdTree->GetBox().GetRandomPoint() - origin;
-	
+	//cout << "z";
 	const float c = Magnitude(direction);
 
