Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2615)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2625)
@@ -35,5 +35,6 @@
 mSamplingType(SamplingStrategy::VIEWCELL_BASED_DISTRIBUTION),
 mProcessedViewCells(0),
-mCurrentViewCell(NULL)
+mCurrentViewCell(NULL),
+mCurrentViewPoint(Vector3(0.0f, 0.0f, 0.0f))
 {
 	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
@@ -96,5 +97,5 @@
 
 
-bool GvsPreprocessor::NextViewCell()
+ViewCell *GvsPreprocessor::NextViewCell()
 {
 	//if (mViewCellsManager->GetNumViewCells() == mProcessedViewCells)
@@ -102,18 +103,18 @@
 
 	if (mProcessedViewCells == (int)mViewCells.size())
-		return false; // no more view cells
-
-	mCurrentViewCell = mViewCells[mProcessedViewCells];
-
-   if (!mCurrentViewCell->GetMesh())
-		mViewCellsManager->CreateMesh(mCurrentViewCell);
-
-	mGvsStats.mViewCellId = mCurrentViewCell->GetId();
-
-	Debug << "vc: " << mCurrentViewCell->GetId() << endl;
+		return NULL; // no more view cells
+
+	ViewCell *vc = mViewCells[mProcessedViewCells];
+
+   if (!vc->GetMesh())
+		mViewCellsManager->CreateMesh(vc);
+
+	mGvsStats.mViewCellId = vc->GetId();
+
+	Debug << "vc: " << vc->GetId() << endl;
 
 	++ mProcessedViewCells;
     
-	return true;
+	return vc;
 }
 
@@ -178,5 +179,5 @@
 		result = ray.mPvsContribution > 0;
 	}
-	else
+	else // original per view cell gvs
 	{
 		Intersectable *obj = ray.mTerminationObject;
@@ -187,4 +188,5 @@
 			mTrianglePvs.push_back(obj);
 		
+			// if using kd pvs, exchange the triangle with the node in the pvs
 			if (mUseKdPvs)
 			{
@@ -194,5 +196,7 @@
 				{
 					node->Mail();
-					mKdPvs.push_back(mKdTree->GetOrCreateKdIntersectable(node));
+					KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(node);
+					mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
+					mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
 				}			
 			}
@@ -478,11 +482,7 @@
 	// the point is chosen to be on the side closer to the original ray
 	if (Distance(pt1, pt3) < Distance(pt2, pt3))
-	{
 		newPoint = pt1 + eps * (pt1 - pt2);
-	}	
 	else
-	{
 		newPoint = pt2 + eps * (pt2 - pt1);
-	}
 
 	//cout << "passing point: " << newPoint << endl << endl;
@@ -874,6 +874,13 @@
         KdLeaf *leaf = *lit;
 		
+		// add to kdnode pvs
 		if (mUseKdPvs)
-			mKdPvs.push_back(mKdTree->GetOrCreateKdIntersectable(leaf));
+		{
+			leaf->Mail();
+			KdIntersectable *kdInt = mKdTree->GetOrCreateKdIntersectable(leaf);
+			mCurrentViewCell->GetPvs().AddSampleDirty(kdInt, 1.0f);
+	
+			mViewCellsManager->UpdateStatsForViewCell(mCurrentViewCell, kdInt);
+		}
 
 		ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
@@ -898,106 +905,51 @@
 void GvsPreprocessor::PerViewCellComputation()
 {
-	// hack: reset counter
-	ObjectContainer::const_iterator oit, oit_end = mObjects.end();
-
-	for (oit = mObjects.begin(); oit != oit_end; ++ oit)
-	{
-		(*oit)->mCounter = 0;
-	}
-	
-	
-	while (NextViewCell())// && (dummy < 3))
-	{
-		//++ dummy;
-		//if (mProcessedViewCells < 53) continue;
-	
-		if (mUseKdPvs)
-		{
-			KdNode::NewMail();
-			mKdPvs.clear();
-		}
-
-		long startTime = GetTime();
-		cout << "\n***********************\n" 
-			 << "processing view cell " << mProcessedViewCells 
-			 << " (id: " << mCurrentViewCell->GetId() << ")" << endl;
-
-		// compute the pvs of the current view cell
-		ProcessViewCell();
-
-		//mGvsStats.mTrianglePvs = mCurrentViewCell->GetPvs().GetSize();
-		mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
-		mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
-
-		if (!mUseKdPvs)
-		{
-			ObjectContainer objectPvs;
-
-			// optain object pvs
-			GetObjectPvs(objectPvs);
-
-			// add pvs
-			ObjectContainer::const_iterator it, it_end = objectPvs.end();
-
-			for (it = objectPvs.begin(); it != it_end; ++ it)
-			{
-				mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
-			}
-
-			cout << "triangle pvs of " << (int)mTrianglePvs.size() 
-				<< " was converted to object pvs of " << (int)objectPvs.size() << endl;
-
-			mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
-		}
-		else
-		{
-			// add pvs
-			ObjectContainer::const_iterator it, it_end = mKdPvs.end();
-
-			for (it = mKdPvs.begin(); it != it_end; ++ it)
-			{
-				mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
-			}
-
-			mGvsStats.mPerViewCellPvs = mKdPvs.size();
-		}
-
-		////////
-		//-- stats
-		
-		mGvsStats.mViewCells = mProcessedViewCells;//mPass;
-		
-
-		mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
-		mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples;
-
-		// timing
-		const long currentTime = GetTime();
-        
-		mGvsStats.mTimePerViewCell = TimeDiff(startTime, currentTime) * 1e-3f;
-		mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
-
-		//lastTime = currentTime;
-
-		mGvsStats.Stop();
-		mGvsStats.Print(mGvsStatsStream);
-
-        mTrianglePvs.clear();
-#if 0
-		if (GVS_DEBUG)
-		{
-			//VisualizeViewCell(mCurrentViewCell);
-			VisualizeViewCell(objectPvs);
-			CLEAR_CONTAINER(mVssRays);
-		}
-		cout << "finished" << endl;
-
-		if (mEvaluatePixelError || mExportVisibility)
-		{
-			StorePvs(objectPvs);
-		}
-#endif
-	}
-}
+	ViewCell *vc;
+
+	while (vc = NextViewCell())
+	{
+		// hack: reset counters
+		ObjectContainer::const_iterator oit, oit_end = mObjects.end();
+
+		for (oit = mObjects.begin(); oit != oit_end; ++ oit)
+			(*oit)->mCounter = 0;
+
+		ComputeViewCell(vc);
+	}
+}
+
+
+void GvsPreprocessor::PerViewCellComputation2()
+{
+	ViewCell *vc;
+
+	while (1)
+	{
+		if (!mRendererWidget)
+			continue;
+
+        //ViewCell *vc = mViewCellsManager->GetViewCell(mCurrentViewPoint);
+        ViewCell *vc = mViewCellsManager->GetViewCell(mRendererWidget->GetViewPoint());
+
+		//cout << "v " << mRendererWidget->GetViewPoint() << " ";
+
+		// no valid view cell or view cell already computed
+		if (!vc || !vc->GetPvs().Empty())
+			continue;
+
+		//cout << "computing new view cell: " << vc->GetId() << endl;
+
+		// hack: reset counters
+		ObjectContainer::const_iterator oit, oit_end = mObjects.end();
+
+		for (oit = mObjects.begin(); oit != oit_end; ++ oit)
+			(*oit)->mCounter = 0;
+
+		ComputeViewCell(vc);
+		++ mProcessedViewCells;
+	}
+}
+
+
 
 
@@ -1155,5 +1107,6 @@
 		cout << "finished view cell construction" << endl;
 	}
-	else if (0)
+#if 0
+	else
 	{	
 		//-- test successful view cells loading by exporting them again
@@ -1162,4 +1115,5 @@
 		mViewCellsManager->ExportViewCells("test.xml.gz", mViewCellsManager->GetExportPvs(), mObjects);
 	}
+#endif
 
 	mGvsStats.Stop();
@@ -1168,4 +1122,5 @@
 	if (mPerViewCell)
 	{
+#if 0
 		// provide list of view cells to compute
 		CompileViewCellsList();
@@ -1178,4 +1133,8 @@
 			ComputeRenderError();
 		}
+#else
+		PerViewCellComputation2();
+
+#endif
 	}
 	else
@@ -1272,3 +1231,94 @@
 
 
-}
+void GvsPreprocessor::ComputeViewCell(ViewCell *vc)
+{
+	mCurrentViewCell = vc;
+
+	if (mUseKdPvs)
+	{
+		KdNode::NewMail();
+		//mKdPvs.clear();
+	}
+
+	long startTime = GetTime();
+	cout << "\n***********************\n" 
+		<< "processing view cell " << mProcessedViewCells 
+		<< " (id: " << mCurrentViewCell->GetId() << ")" << endl;
+
+	// compute the pvs of the current view cell
+	ProcessViewCell();
+
+	//mGvsStats.mTrianglePvs = mCurrentViewCell->GetPvs().GetSize();
+	mGvsStats.mTrianglePvs = (int)mTrianglePvs.size();
+	mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
+
+	if (!mUseKdPvs)
+	{
+		ObjectContainer objectPvs;
+
+		// optain object pvs
+		GetObjectPvs(objectPvs);
+
+		// add pvs
+		ObjectContainer::const_iterator it, it_end = objectPvs.end();
+
+		for (it = objectPvs.begin(); it != it_end; ++ it)
+		{
+			mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
+		}
+
+		cout << "triangle pvs of " << (int)mTrianglePvs.size() 
+			<< " was converted to object pvs of " << (int)objectPvs.size() << endl;
+
+		mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
+	}
+	else
+	{
+		/*// add pvs
+		ObjectContainer::const_iterator it, it_end = mKdPvs.end();
+
+		for (it = mKdPvs.begin(); it != it_end; ++ it)
+			mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
+		*/
+		mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();;
+	}
+
+
+	////////
+	//-- stats
+
+	mGvsStats.mViewCells = mProcessedViewCells;//mPass;
+
+
+	mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
+	mGvsStats.mTotalSamples += mGvsStats.mPerViewCellSamples;
+
+	// timing
+	const long currentTime = GetTime();
+
+	mGvsStats.mTimePerViewCell = TimeDiff(startTime, currentTime) * 1e-3f;
+	mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
+
+	//lastTime = currentTime;
+
+	mGvsStats.Stop();
+	mGvsStats.Print(mGvsStatsStream);
+
+	mTrianglePvs.clear();
+#if 0
+	if (GVS_DEBUG)
+	{
+		//VisualizeViewCell(mCurrentViewCell);
+		VisualizeViewCell(objectPvs);
+		CLEAR_CONTAINER(mVssRays);
+	}
+	cout << "finished" << endl;
+
+	if (mEvaluatePixelError || mExportVisibility)
+	{
+		StorePvs(objectPvs);
+	}
+#endif
+}
+
+}
