Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1168)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1178)
@@ -383,5 +383,5 @@
 	/** Evaluates render cost statistics of current view cell hierarchy.
 	*/
-	void EvalViewCellPartition();
+	virtual void EvalViewCellPartition();
 
 	/** Sets maximal size of a view cell filter.
@@ -413,5 +413,4 @@
 	*/
 	void UpdateScalarPvsSize(ViewCell *vc, const int pvsSize, const int entriesInPvs) const;
-
 
 	/** Returns bounding box of a view cell.
@@ -674,4 +673,6 @@
 
 	bool mStoreKdPvs;
+
+	VssRayContainer storedRays;
 };
 
@@ -1011,4 +1012,6 @@
 protected:
 	
+	virtual void EvalViewCellPartition();
+
 	/** Exports view cell geometry.
 	*/
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1168)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1178)
@@ -114,5 +114,5 @@
   stream<<"key='0.0 ";
   int i;
-  int size = rays.size() + 2;
+  int size = (int)rays.size() + 2;
   for (i=1; i < size; i++) {
 	stream<<i/(float)(size-1)<<" ";
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1176)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.cpp	(revision 1178)
@@ -30,4 +30,7 @@
 OspTree *OspTree::OspSplitCandidate::sOspTree = NULL;
 
+// variable for debugging volume contribution for heuristics
+static float debugVol;
+
 // pvs penalty can be different from pvs size
 inline static float EvalPvsPenalty(const int pvs, 
@@ -460,5 +463,5 @@
 		Debug << "pvs heuristics: per kd node" << endl;
 
-	mSplitCandidates = new vector<SortableEntry>;
+	mLocalSplitCandidates = new vector<SortableEntry>;
 
 	Debug << endl;
@@ -494,5 +497,5 @@
 {
 	DEL_PTR(mRoot);
-	DEL_PTR(mSplitCandidates);
+	DEL_PTR(mLocalSplitCandidates);
 }
 
@@ -703,5 +706,6 @@
 	if (newNode->IsLeaf()) // subdivision terminated
 	{
-		//CreateViewCell(tData); // create new view cell
+		// view cell is created during subdivision
+		//CreateViewCell(tData); 
 
 		VspLeaf *leaf = dynamic_cast<VspLeaf *>(newNode);
@@ -980,5 +984,5 @@
   
 	ViewCellLeaf *vc = leaf->GetViewCell();
- 
+
 	// add contributions from samples to the PVS
 	for (it = rays.begin(); it != it_end; ++ it)
@@ -1016,5 +1020,5 @@
 		{
 			// potentially visible kd cells
-			if (!mUseKdPvsForHeuristics) // matt TODo: remove storekdpvs!!
+			if (!mStoreKdPvs) // matt Todo: remove storekdpvs!!
 			{
 				if (vc->AddPvsSample(obj, ray->mPdf, contribution))
@@ -1050,17 +1054,18 @@
 								  float maxBand)
 {
-	mSplitCandidates->clear();
+	mLocalSplitCandidates->clear();
 
 	int requestedSize = 2 * (int)(rays.size());
 
 	// creates a sorted split candidates array
-	if (mSplitCandidates->capacity() > 500000 &&
-		requestedSize < (int)(mSplitCandidates->capacity() / 10) )
-	{
-    	delete mSplitCandidates;
-   		mSplitCandidates = new vector<SortableEntry>;
-	}
-
-	mSplitCandidates->reserve(requestedSize);
+	if (mLocalSplitCandidates->capacity() > 500000 &&
+		requestedSize < (int)(mLocalSplitCandidates->capacity() / 10) )
+	{
+    	delete mLocalSplitCandidates;
+   		mLocalSplitCandidates = new vector<SortableEntry>;
+	}
+
+	mLocalSplitCandidates->reserve(requestedSize);
+
 
 	float pos;
@@ -1075,14 +1080,14 @@
 		pos = (*rit).ExtrapOrigin(axis);
 		
-		mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax, 
+		mLocalSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax, 
 									pos, (*rit).mRay));
 
 		pos = (*rit).ExtrapTermination(axis);
 
-		mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin, 
+		mLocalSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin, 
 									pos, (*rit).mRay));
 	}
 
-	stable_sort(mSplitCandidates->begin(), mSplitCandidates->end());
+	stable_sort(mLocalSplitCandidates->begin(), mLocalSplitCandidates->end());
 }
 
@@ -1411,9 +1416,9 @@
 
 
-	vector<SortableEntry>::const_iterator ci, ci_end = mSplitCandidates->end();
-
 	//-- traverse through visibility events
 
-	for (ci = mSplitCandidates->begin(); ci != ci_end; ++ ci)
+	vector<SortableEntry>::const_iterator ci, ci_end = mLocalSplitCandidates->end();
+
+	for (ci = mLocalSplitCandidates->begin(); ci != ci_end; ++ ci)
 	{
 		EvalPvsIncr(*ci, pvsl, pvsr);
@@ -1469,16 +1474,17 @@
 	}
 	
-	//if (axis != 1)
+	const float volRatio = tData.mBoundingBox.GetVolume() / (sizeBox * mBoundingBox.GetVolume());
+
+/*	
+//if (axis != 1)
 	//Debug << "axis=" << axis << " costRatio=" << ratio << " pos=" << position << " t=" << (position - minBox) / (maxBox - minBox)
 	//      <<"\t pb=(" << pvsBack << ")\t pf=(" << pvsFront << ")" << endl;
 
-	const float volRatio = tData.mBoundingBox.GetVolume() / (sizeBox * mBoundingBox.GetVolume());
-
-	Debug << "\n§§§§ eval local cost §§§§" << endl
+Debug << "\n§§§§ eval local cost §§§§" << endl
 		  << "back pvs: " << penaltyBack << " front pvs: " << penaltyFront << " total pvs: " << penaltyOld << endl 
 		  << "back p: " << pBack * volRatio << " front p " << pFront * volRatio << " p: " << pOverall * volRatio << endl
 		  << "old rc: " << oldRenderCost * volRatio << " new rc: " << newRenderCost * volRatio << endl
 		  << "render cost decrease: " << oldRenderCost * volRatio - newRenderCost * volRatio << endl;
-
+*/
 	return ratio;
 }
@@ -1648,7 +1654,6 @@
 	normalizedOldRenderCost = oldRenderCost / viewSpaceVol;
 
-	//Debug << "decrease: " << oldRenderCost - newRenderCost << endl;
 	const float renderCostDecrease = (oldRenderCost - newRenderCost) / viewSpaceVol;
-	
+	/*
 	Debug << "\n==== eval render cost decrease ===" << endl
 		  << "back pvs: " << pvsBack << " front pvs " << pvsFront << " total pvs: " << totalPvs << endl 
@@ -1656,5 +1661,5 @@
 		  << "old rc: " << normalizedOldRenderCost << " new rc: " << newRenderCost / viewSpaceVol << endl
 		  << "render cost decrease: " << renderCostDecrease << endl;
-
+*/
 	return renderCostDecrease;
 }
@@ -1690,5 +1695,6 @@
 	}
 
-	//-- cost heuristics
+	//-- evaluate cost heuristics
+
 	float pOverall;
 	
@@ -1986,4 +1992,5 @@
 {
 	vector<VspLeaf *> leaves;
+	CollectLeaves(leaves);
 
 	vector<VspLeaf *>::const_iterator lit, lit_end = leaves.end();
@@ -2933,4 +2940,5 @@
 	mBoundingBox = kdTree.GetBox();
 	mRoot = kdTree.GetRoot();
+	mSplitCandidates = new vector<SortableEntry>;
 }
 
@@ -2949,4 +2957,5 @@
 		DEL_PTR(mRoot);
 
+	DEL_PTR(mSplitCandidates);
 	mSubdivisionStats.close();
 }
@@ -3230,5 +3239,5 @@
 	float frontProb;
 	float backProb;
-	
+
 	// compute locally best split plane
 	const bool success = 
@@ -3337,12 +3346,12 @@
 	RayInfoContainer usedRays;
 int mMaxTests = 5000; // HACK
-	if (mMaxTests < (int)tData.mRays->size())
-	{
-		GetRayInfoSets(*tData.mRays, mMaxTests, usedRays);
-	}
-	else
-	{
+//	if (mMaxTests < (int)tData.mRays->size())
+//	{
+//		GetRayInfoSets(*tData.mRays, mMaxTests, usedRays);
+//	}
+//	else
+//	{
 		usedRays = *tData.mRays;
-	}
+//	}
 
 	// go through the lists, count the number of objects left and right
@@ -3396,10 +3405,17 @@
 	float minSum = 1e20f;
 
+	debugVol = 0;
+	const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
+
 	/////////////////////////////
+	// the sweep heuristics
+
 
 	Intersectable::NewMail();
 	ViewCell::NewMail();
-
+	
+	
 	//-- traverse through events and find best split plane
+	
 	vector<SortableEntry>::const_iterator ci, ci_end = mSplitCandidates->end();
 
@@ -3431,12 +3447,12 @@
 				currentPos = (*ci).mPos;			
 
-			// note matt: can happen that volr is less than zero: bug or numerical error?
-			//if (volr < 0) Debug << "warning!";
-
-			/*Debug << "pos: " << (*ci).mPos 
+			if ((totalVol - voll - volr - debugVol) / viewSpaceVol > 0.0001)
+				Debug << "front and back volume: " << (totalVol - voll - volr) / viewSpaceVol << " error: " << (totalVol - voll - volr - debugVol) / viewSpaceVol << endl;
+			/*	Debug << "pos: " << currentPos 
 				 << "\t (pvsl: " << pvsl << ", pvsr: " << pvsr << ")"
 				 << "\t (voll: " << voll << ", volr: " << volr << ")"
 				 << "\t (vcl: " << vcl << ", vcr: " << vcr << ", nvc: " << numViewCells << ")"
-				 << "\t sum: " << sum << endl;*/
+				 << "\t sum: " << sum << endl;
+				 */
 
 			if (sum < minSum)
@@ -3471,10 +3487,8 @@
 		ratio = newRenderCost / oldRenderCost;
 	}
-	
+
 	//Debug << "axis=" << axis << " costRatio=" << ratio << " pos=" 
 	//	  << position << " t=" << (position - minBox) / (maxBox - minBox)
 	//      << "\t pb=(" << volBack << ")\t pf=(" << volFront << ")" << endl;
-
-	const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
 
 	Debug << "\n§§§§ eval local cost §§§§" << endl
@@ -3543,7 +3557,4 @@
 							  );
 		}
-
-
-
 
 		if (ray->mTerminationObject && (GetLeaf(ray->mTermination, ray->mTerminationNode) == leaf))
@@ -3741,4 +3752,6 @@
 			// => remove ref to right child node
 			volRight -= vol;
+
+			debugVol += vol;
 		}
 
@@ -3756,4 +3769,5 @@
 			// contributes only to left node now
 			volLeft += vol;
+			debugVol -= vol;
 		}
 	}
@@ -3793,4 +3807,5 @@
 		sAxis = box.Size().DrivingAxis();
 	}
+	
 
 	// -- evaluate split cost for all three axis
@@ -4011,15 +4026,14 @@
 	}
 
+	ViewCellContainer::const_iterator vit, vit_end = collectedViewCells.end();
+
+	// evaluate view cells volume contribution with respect to mail box
+	for (vit = collectedViewCells.begin(); vit != vit_end; ++ vit)
+	{
+		AddViewCellVolumeContri(*vit, pFront, pBack, pFrontAndBack);
+	}
+
 	// these are non-overlapping sets
 	pOverall = pFront + pBack + pFrontAndBack;
-
-	ViewCellContainer::const_iterator vit, vit_end = collectedViewCells.end();
-
-	// evaluate view cells volume contribution with respect to mail box
-	for (vit = collectedViewCells.begin(); vit != vit_end; ++ vit)
-	{
-		AddViewCellVolumeContri(*vit, pFront, pBack, pFrontAndBack);
-	}
-
 
 	//-- pvs rendering heuristics
@@ -4288,4 +4302,5 @@
 	{
 		VssRay *ray = *rit;
+
 		float minT, maxT;
 		static Ray hray;
@@ -4498,8 +4513,4 @@
 {
 	SplitCandidate *splitCandidate = mTQueue.Top();
-
-	cout << "next candidate: " << splitCandidate->Type()
-		<< ", priority: " << splitCandidate->GetPriority() << endl;
-
 	mTQueue.Pop();
 
@@ -4547,9 +4558,9 @@
 									mVspTree.mBoundingBox);
 
-#if WORK_WITH_VIEWCELL_PVS
-	
 	// create first view cell
 	mVspTree.CreateViewCell(vData, false);
 		
+#if WORK_WITH_VIEWCELL_PVS
+	
 	// add first view cell to all the objects view cell pvs
 	ObjectPvsMap::const_iterator oit, 
@@ -4736,5 +4747,6 @@
 			++ mGlobalCostMisses;
 
-		/*		cout << "nodes: " << ++ numNodes << endl;
+		/*		
+		cout << "nodes: " << ++ numNodes << endl;
 		Debug << "\n**********" << endl
 			  << "total cost: " << mTotalCost << " render cost decr: " 
@@ -4743,6 +4755,8 @@
 */
 		//-- subdivide leaf node
+
 		if (SubdivideSplitCandidate(splitCandidate))
 		{
+			// subdivision successful
 			EvalSubdivisionStats(*splitCandidate);
 		
@@ -4752,4 +4766,6 @@
 			if (repair)
 				RepairQueue();
+
+			cout << "candidate: " << splitCandidate->Type() << ", priority: " << splitCandidate->GetPriority() << endl;
 		}
 
@@ -4763,4 +4779,5 @@
 								  AxisAlignedBox3 *forcedViewSpace)
 {
+	// rays clipped in view space and in object space
 	RayInfoContainer *viewSpaceRays = new RayInfoContainer();
 	RayInfoContainer *objectSpaceRays = new RayInfoContainer();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1176)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1178)
@@ -31,5 +31,4 @@
 
 
-
 template <typename T> class myless
 {
@@ -921,8 +920,7 @@
 		cout << "finished" << endl;
 	}
-	
 	cout << "finished" << endl;
 
-	cout << "Evaluating view cell partition" << endl;
+    cout << "Evaluating view cell partition ... " << endl;
 
 	while (castSamples < numSamples)
@@ -978,5 +976,4 @@
 		cout << "finished in " << timeDiff * 1e-3 << " secs" << endl;
 		Debug << "finished in " << timeDiff * 1e-3 << " secs" << endl;
-
 	
 		disposeRays(evaluationSamples, NULL);
@@ -2534,12 +2531,9 @@
 	
 
-#else
-
-	// really merge cells: slow put sumpdf is correct
+#else // really merge cells: slow put sumPdf is correct
 	viewCellInterior->GetPvs().Merge(backVc->GetPvs());
 	viewCellInterior->GetPvs().Merge(frontVc->GetPvs());
 #endif
 }
-
 
 
@@ -4885,8 +4879,13 @@
 
 	long startTime;
-
+Debug << "here601" << endl;
 	//mHierarchyManager->Construct(constructionRays, objects, &mViewSpaceBox);
 	mHierarchyManager->Construct2(constructionRays, objects, &mViewSpaceBox);
 
+	VssRayContainer::const_iterator vit, vit_end = constructionRays.end();
+	for (vit = constructionRays.begin(); vit != vit_end; ++ vit)
+	{
+		storedRays.push_back(new VssRay(*(*vit)));
+	}
 
 	// print subdivision statistics
@@ -4902,4 +4901,5 @@
 
 	cout << "Computing remaining ray contributions ... ";
+
 
 	// recast rest of rays
@@ -5278,5 +5278,7 @@
 	int raysOut = 0;
 
+
 	//-- some rays for output
+	
 	for (int i = 0; i < limit; ++ i)
 	{
@@ -5725,4 +5727,77 @@
 
 
+void VspOspViewCellsManager::EvalViewCellPartition()
+{
+	Debug << "here23" << endl;
+	int samplesPerPass;
+	int numSamples;
+	int castSamples = 0;
+
+	char s[64]; 
+
+	Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
+	Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samples", numSamples);
+
+	char statsPrefix[100];
+	Environment::GetSingleton()->GetStringValue("ViewCells.Evaluation.statsPrefix", statsPrefix);
+
+	Debug << "view cell evaluation samples per pass: " << samplesPerPass << endl;
+	Debug << "view cell evaluation samples: " << numSamples << endl;
+	Debug << "view cell stats prefix: " << statsPrefix << endl;
+
+	// should directional sampling be used?
+	bool dirSamples = (mEvaluationSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
+
+	cout << "reseting pvs ... ";
+		
+	const bool startFromZero = true;
+
+	// reset pvs and start over from zero
+	if (startFromZero)
+	{
+		mViewCellsTree->ResetPvs();
+	}
+	else // start from current sampless
+	{
+		// statistics before casting more samples
+		cout << "compute new statistics ... ";
+		sprintf(s, "-%09d-eval.log", castSamples);
+		string fName = string(statsPrefix) + string(s);
+
+		mViewCellsTree->ExportStats(fName);
+		cout << "finished" << endl;
+	}
+	cout << "finished" << endl;
+
+    cout << "Evaluating view cell partition ... " << endl;
+
+	VssRayContainer evaluationSamples = storedRays;
+	const int samplingType = mEvaluationSamplingType;
+	
+	cout << "computing sample contributions of " << (int)evaluationSamples.size()  << " samples ... ";
+
+	ComputeSampleContributions(evaluationSamples, true, false);
+
+	cout << "finished" << endl;
+	
+	cout << "compute new statistics ... ";
+	Debug << "compute new statistics ... ";
+
+	//-- propagate pvs or pvs size information
+	ObjectPvs pvs;
+	UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), pvs);
+
+	//-- output stats
+
+	sprintf(s, "-%09d-eval.log", castSamples);
+	string fileName = string(statsPrefix) + string(s);
+
+	mViewCellsTree->ExportStats(fileName);
+	
+	cout << "finished" << endl;
+
+	disposeRays(evaluationSamples, NULL);
+}
+
 //////////////////////////////////
 /*ViewCellsManager *ViewCellsManagerFactory::Create(const string mName)
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1168)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1178)
@@ -251,6 +251,8 @@
 							float &contribution)
 {
+	Debug << "here9 "<< endl;
+	Debug << "here2 " << mVolume << endl;
 	const bool result = mPvs.AddSample(sample, pdf, contribution);
-
+	Debug << "here4" << endl;
 	// update pvs size scalar
 	//mPvsSize = mPvs.GetSize();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1176)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspOspTree.h	(revision 1178)
@@ -451,5 +451,7 @@
 
 	SplitCandidate *GetSplitCandidate() const 
-	{ return mSplitCandidate; }
+	{ 
+		return mSplitCandidate; 
+	}
 
 public:
@@ -1050,6 +1052,5 @@
 		and must be reevaluated.
 	*/
-	void CollectDirtyCandidates(VspSplitCandidate *sc, 
-		vector<SplitCandidate *> &dirtyList);
+	void CollectDirtyCandidates(VspSplitCandidate *sc, vector<SplitCandidate *> &dirtyList);
 
 	/** Rays will be clipped to the bounding box.
@@ -1071,5 +1072,5 @@
 	ViewCellsManager *mViewCellsManager;
 	
-	vector<SortableEntry> *mSplitCandidates;
+	vector<SortableEntry> *mLocalSplitCandidates;
 
 	/// Pointer to the root of the tree
Index: /GTP/trunk/Lib/Vis/QtRenderer/QtRenderer.vcproj
===================================================================
--- /GTP/trunk/Lib/Vis/QtRenderer/QtRenderer.vcproj	(revision 1166)
+++ /GTP/trunk/Lib/Vis/QtRenderer/QtRenderer.vcproj	(revision 1178)
@@ -24,5 +24,5 @@
 				MinimalRebuild="TRUE"
 				BasicRuntimeChecks="3"
-				RuntimeLibrary="1"
+				RuntimeLibrary="3"
 				UsePrecompiledHeader="0"
 				WarningLevel="3"
@@ -33,5 +33,5 @@
 			<Tool
 				Name="VCLinkerTool"
-				AdditionalDependencies="Preprocessor.lib qtmain.lib QtGui4d.lib Qt3Support4d.lib QAxContainerd.lib QtCore4d.lib QtOpenGL4.lib xerces-c_2.lib glew32.lib OpenGL32.Lib glu32.lib cg.lib cgGL.lib"
+				AdditionalDependencies="Preprocessor.lib qtmain.lib QtGui4.lib Qt3Support4.lib QAxContainerd.lib QtCore4.lib QtOpenGL4.lib xerces-c_2.lib glew32.lib OpenGL32.Lib glu32.lib cg.lib cgGL.lib"
 				OutputFile="$(OutDir)/QtRenderer.dll"
 				LinkIncremental="2"
