Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1557)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1570)
@@ -113,7 +113,6 @@
 	/** Computes sample contribution of a simgle ray to the view cells PVS.
 		@param ray finds intersections with view cells and holds the contribution
-		@param castRay true if ray should be cast to gain the information, false if ray
-		is already holding the information and need not be recast.
-	  
+		@param addSample if sample should be added to the pvs
+        	  
 		@returns number of sample contributions
 	*/
@@ -497,4 +496,12 @@
 	bool GetStoreKdPVs() const;
 
+	/** Adds a sample to the pvs of the specified view cell.
+		@param obj the hit object
+		@param hitPoint the point where the object is hit.
+		@param vc the sanpled view cell
+		@param pdf probability of the ray
+		@param contribution the relative pvs contribution
+		@returns true if the sample gives a contribution to the pvs.
+	*/
 	virtual bool AddSampleToPvs(
 		Intersectable *obj, 
@@ -503,4 +510,23 @@
 		const float pdf, 
 		float &contribution) const;
+
+	/** Exports single view cells for visualization.
+		@param objects the scene objects
+		@param limit the maximal number of output view cells
+		@param sortViewCells if the view cells should be sorted by pvs size
+		@param exportPvs if the pvs should also be exported
+		@param exportRays if sample rays should be exported as well
+		@param visRays additional rays
+	*/
+	virtual 
+		void ExportSingleViewCells(
+		const ObjectContainer &objects,
+		const int maxViewCells,
+		const bool sortViewCells,
+		const bool exportPvs,
+		const bool exportRays,
+		const int maxRays,
+		const string prefix,
+		VssRayContainer *visRays = NULL) = NULL;
 
 protected:
@@ -662,7 +688,8 @@
 	/// the width of the box filter
 	float mFilterWidth;
+	/// Maximal size of the filter in terms of contributing view cells
 	int mMaxFilterSize;
-
-	bool mStoreObjectPvs;
+	
+	
 
 	//////////////////
@@ -670,5 +697,5 @@
 	
 	/// color code for view cells visualization
-		bool mShowVisualization;
+	bool mShowVisualization;
 	int mColorCode;
 	bool mExportGeometry;
@@ -751,4 +778,13 @@
 	ViewCell *ConstructSpatialMergeTree(BspNode *root);
 
+	void ExportSingleViewCells(
+		const ObjectContainer &objects,
+		const int maxViewCells,
+		const bool sortViewCells,
+		const bool exportPvs,
+		const bool exportRays,
+		const int maxRays,
+		const string prefix,
+		VssRayContainer *visRays = NULL);
 
 protected:
@@ -769,8 +805,4 @@
 	*/
 	void ExportSplits(const ObjectContainer &objects);
-
-	/** Exports visualization of the BSP PVS.
-	*/
-	void ExportBspPvs(const ObjectContainer &objects);
 
 	/** test if subdivision is valid in terms of volume / area.
@@ -830,4 +862,13 @@
 								vector<MergeCandidate> &candidates);
 
+	void ExportSingleViewCells(
+		const ObjectContainer &objects,
+		const int maxViewCells,
+		const bool sortViewCells,
+		const bool exportPvs,
+		const bool exportRays,
+		const int maxRays,
+		const string prefix,
+		VssRayContainer *visRays = NULL);
 
 protected:
@@ -839,4 +880,5 @@
 	KdNode *GetNodeForPvs(KdLeaf *leaf);
 
+	////////////////////////////////////////
 
 	/// the BSP tree.
@@ -849,6 +891,6 @@
 };
 
-/**
-	Manages different higher order operations on the view cells.
+
+/** Manages different higher order operations on the view cells.
 */
 class VspBspViewCellsManager: public ViewCellsManager
@@ -914,4 +956,15 @@
 
 
+	void ExportSingleViewCells(
+		const ObjectContainer &objects,
+		const int maxViewCells,
+		const bool sortViewCells,
+		const bool exportPvs,
+		const bool exportRays,
+		const int maxRays,
+		const string prefix,
+		VssRayContainer *visRays = NULL);
+
+
 protected:
 
@@ -961,10 +1014,4 @@
 	void ExportSplits(const ObjectContainer &objects, 
 					  const VssRayContainer &rays);
-
-	/** Exports visualization of the BSP PVS.
-	*/
-	void ExportBspPvs(const ObjectContainer &objects,
-					  const VssRayContainer &rays);
-
 
 	/// the view space partition BSP tree.
@@ -1049,4 +1096,14 @@
         float &contribution) const;
 
+	void ExportSingleViewCells(
+		const ObjectContainer &objects,
+		const int maxViewCells,
+		const bool sortViewCells,
+		const bool exportPvs,
+		const bool exportRays,
+		const int maxRays,
+		const string prefix,
+		VssRayContainer *visRays = NULL);
+
 protected:
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1564)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1570)
@@ -803,6 +803,5 @@
 	}
 
-	// should never come here
-	return NULL;
+	return NULL; // should never come here
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1449)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1570)
@@ -615,4 +615,18 @@
 								ViewCellContainer &viewCells) const;
 
+	/** Returns view cells of this ray, either taking precomputed cells
+		or by recomputation.
+	*/
+	void GetViewCells(const VssRay &ray, ViewCellContainer &viewCells);
+
+	/** Returns view cells tree.
+	*/
+	ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
+
+	/** Sets the view cells tree.
+	*/
+	void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
+
+#if WORK_WITH_VIEWCELLS
 	/** Remove the references of the parent view cell from the kd nodes associated with
 		the objects.
@@ -623,18 +637,5 @@
 	*/
 	void AddViewCellReferences(ViewCell *vc) const;
-
-	/** Returns view cells of this ray, either taking precomputed cells
-		or by recomputation.
-	*/
-	void GetViewCells(const VssRay &ray, ViewCellContainer &viewCells);
-
-	/** Returns view cells tree.
-	*/
-	ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
-
-	/** Sets the view cells tree.
-	*/
-	void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
-
+#endif
 
 protected:
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1566)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1570)
@@ -822,6 +822,5 @@
 	int numSamples;
 	int castSamples = 0;
-
-	char s[64]; 
+	char str[64]; 
 
 	Environment::GetSingleton()->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
@@ -851,12 +850,12 @@
 		// statistics before casting more samples
 		cout << "compute new statistics ... ";
-		sprintf(s, "-%09d-eval.log", castSamples);
-		string fName = string(statsPrefix) + string(s);
+		sprintf(str, "-%09d-eval.log", castSamples);
+		string fName = string(statsPrefix) + string(str);
 
 		mViewCellsTree->ExportStats(fName);
 		cout << "finished" << endl;
 	}
+
 	cout << "finished" << endl;
-
     cout << "Evaluating view cell partition ... " << endl;
 
@@ -897,5 +896,4 @@
 
 		startTime = GetTime();
-
 		cout << "compute new statistics ... " << endl;
 	
@@ -905,6 +903,6 @@
 
 		//-- output stats
-		sprintf(s, "-%09d-eval.log", castSamples);
-		string fileName = string(statsPrefix) + string(s);
+		sprintf(str, "-%09d-eval.log", castSamples);
+		string fileName = string(statsPrefix) + string(str);
 
 		mViewCellsTree->ExportStats(fileName);
@@ -918,5 +916,5 @@
 	
 
-	////////////7
+	////////////
 	//-- histogram
 
@@ -2824,6 +2822,8 @@
 	}
 
-	// export single view cells
-	ExportBspPvs(objects);
+	int leafOut;
+	Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
+	const int raysOut = 100;
+	ExportSingleViewCells(objects, leafOut, false, true, false, raysOut, "");
 }
 
@@ -2858,56 +2858,85 @@
 
 
-void BspViewCellsManager::ExportBspPvs(const ObjectContainer &objects)
-{
-	const int leafOut = 10;
+void BspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
+												const int maxViewCells,
+												const bool sortViewCells,
+												const bool exportPvs,
+												const bool exportRays,
+												const int maxRays,
+												const string prefix,
+												VssRayContainer *visRays)
+{
+	if (sortViewCells)
+	{	// sort view cells to visualize the largest view cells
+		stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::LargerRenderCost);
+	}
+
+	//////////
+	//-- some view cells for output
+
 	ViewCell::NewMail();
-
-	//////////
-	//-- some rays for output
-	const int raysOut = min((int)mBspRays.size(), mVisualizationSamples);
-
-	cout << "visualization using " << mVisualizationSamples << " samples" << endl;
-	Debug << "\nOutput view cells: " << endl;
-
-	// sort view cells in order to find the largest view cells
-	if (0) stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
-
-	int limit = min(leafOut, (int)mViewCells.size());
-
+	const int limit = min(maxViewCells, (int)mViewCells.size());
+	
 	for (int i = 0; i < limit; ++ i)
 	{
-		cout << "creating output for view cell " << i << " ... ";
-		VssRayContainer vcRays;
-		Intersectable::NewMail();
-		ViewCell *vc;
-
-		if (0 || ((int)mViewCells.size() <= limit))
-			vc = mViewCells[i];
-		else
-			vc = mViewCells[Random((int)mViewCells.size())];
-
-		cout << "creating output for view cell " << i << " ... ";
-
-		if(0)
-		{
-			// check whether we can add the current ray to the output rays
-			for (int k = 0; k < raysOut; ++ k)
-			{
-				BspRay *ray = mBspRays[k];
-				for	(int j = 0; j < (int)ray->intersections.size(); ++ j)
+		const int idx = sortViewCells ? (int)RandomValue(0, (float)mViewCells.size() - 0.5f) : i;
+		ViewCell *vc = mViewCells[idx];
+
+		if (vc->Mailed() || vc->GetId() == OUT_OF_BOUNDS_ID)
+			continue;
+
+		vc->Mail();
+
+		ObjectPvs pvs;
+		mViewCellsTree->GetPvs(vc, pvs);
+
+		char s[64]; sprintf(s, "%sviewcell%04d.wrl", prefix.c_str(), i);
+		Exporter *exporter = Exporter::GetExporter(s);
+		
+		cout << "view cell " << idx << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc) << endl;
+
+		if (exportRays)
+		{
+			////////////
+			//-- export rays piercing this view cell
+
+			// take rays stored with the view cells during subdivision
+			VssRayContainer vcRays;
+            VssRayContainer collectRays;
+
+			// collect initial view cells
+			ViewCellContainer leaves;
+			mViewCellsTree->CollectLeaves(vc, leaves);
+
+			ViewCellContainer::const_iterator vit, vit_end = leaves.end();
+       		for (vit = leaves.begin(); vit != vit_end; ++ vit)
+			{	
+				BspLeaf *vcLeaf = dynamic_cast<BspViewCell *>(*vit)->mLeaves[0];
+				VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
+
+				for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
 				{
-					BspLeaf *leaf = ray->intersections[j].mLeaf;
-					if (vc == leaf->GetViewCell())
-					{
-						vcRays.push_back(ray->vssRay);
-					}
+					collectRays.push_back(*rit);
 				}
 			}
-		}
-
-		//bspLeaves[j]->Mail();
-		char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
-
-		Exporter *exporter = Exporter::GetExporter(s);
+
+			const int raysOut = min((int)collectRays.size(), maxRays);
+cout << "here500 " << raysOut << endl;
+			// prepare some rays for output
+			VssRayContainer::const_iterator rit, rit_end = collectRays.end();
+			for (rit = collectRays.begin(); rit != rit_end; ++ rit)
+			{
+				const float p = RandomValue(0.0f, (float)collectRays.size());
+			
+				if (p < raysOut)
+				{
+					vcRays.push_back(*rit);
+				}
+			}
+			exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
+		}
+		
+		////////////////
+		//-- export view cell geometry
 
 		exporter->SetWireframe();
@@ -2918,36 +2947,32 @@
 
 		ExportViewCellGeometry(exporter, vc, NULL, NULL);
+		exporter->SetFilled();
+
+		if (exportPvs)
+		{
+			Intersectable::NewMail();
+
+			ObjectPvsMap::const_iterator oit, oit_end = pvs.mEntries.end();
+			cout << endl;
+			// output PVS of view cell
+			for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
+			{		
+				Intersectable *intersect = (*oit).first;
+				
+				if (!intersect->Mailed())
+				{
+					intersect->Mail();
+					m = RandomMaterial();
+					exporter->SetForcedMaterial(m);
+					exporter->ExportIntersectable(intersect);
+					cout << " i: " << intersect;
+				}
+			}
+			cout << endl;
+		}
 		
-		// export rays piercing this view cell
-		exporter->ExportRays(vcRays, RgbColor(0, 1, 0));
-
-		m.mDiffuseColor = RgbColor(1, 0, 0);
-		exporter->SetForcedMaterial(m);
-
-		ObjectPvsMap::const_iterator it,
-			it_end = vc->GetPvs().mEntries.end();
-
-		exporter->SetFilled();
-
-		// output PVS of view cell
-		for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
-		{
-			Intersectable *intersect = (*it).first;
-
-			if (!intersect->Mailed())
-			{
-				Material m = RandomMaterial();
-				exporter->SetForcedMaterial(m);
-
-				exporter->ExportIntersectable(intersect);
-				intersect->Mail();
-			}
-		}
-
 		DEL_PTR(exporter);
 		cout << "finished" << endl;
 	}
-
-	Debug << endl;
 }
 
@@ -2989,5 +3014,5 @@
 												 const AxisAlignedBox3 *sceneBox,
 												 const AxisAlignedPlane *clipPlane
-													) const
+												 ) const
 {
 	if (clipPlane)
@@ -3336,4 +3361,17 @@
 
 
+void KdViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
+											   const int maxViewCells,
+											   const bool sortViewCells,
+											   const bool exportPvs,
+											   const bool exportRays,
+											   const int maxRays,
+											   const string prefix,
+											   VssRayContainer *visRays)
+{
+	// TODO
+}
+
+
 void KdViewCellsManager::Visualize(const ObjectContainer &objects,
 								   const VssRayContainer &sampleRays)
@@ -3392,9 +3430,7 @@
 
 			ViewCell *vc = leaf->mViewCell;
-
-			//bspLeaves[j]->Mail();
-			char s[64]; sprintf(s, "kd-pvs%04d.x3d", i);
-
-			Exporter *exporter = Exporter::GetExporter(s);
+			char str[64]; sprintf(str, "viewcell%04d.wrl", i);
+
+			Exporter *exporter = Exporter::GetExporter(str);
 			exporter->SetFilled();
 
@@ -3463,5 +3499,4 @@
 
 			exporter->SetFilled();
-
 			delete exporter;
 		}
@@ -3470,8 +3505,7 @@
 		{
 			Intersectable *object = objects[k];
-			char s[64];
-			sprintf(s, "sample-pvs%04d.x3d", k);
-
-			Exporter *exporter = Exporter::GetExporter(s);
+			char str[64]; sprintf(str, "viewcell%04d.wrl", i);
+
+			Exporter *exporter = Exporter::GetExporter(str);
 			exporter->SetWireframe();
 
@@ -3707,4 +3741,5 @@
 	//////////////////////
 	//-- recast the rest of the rays
+
 	startTime = GetTime();
 
@@ -4165,5 +4200,10 @@
 	////////
 	//-- export single view cells
-	ExportBspPvs(objects, visRays);
+	
+	int leafOut;
+	Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
+	const int raysOut = 100;
+	
+	ExportSingleViewCells(objects, leafOut, false, true, false, raysOut, "");
 }
 
@@ -4200,5 +4240,4 @@
 			exporter->ExportGeometry(objects);
 		}
-
 		delete exporter;
 	}
@@ -4206,51 +4245,46 @@
 
 
-void VspBspViewCellsManager::ExportBspPvs(const ObjectContainer &objects,
-										  const VssRayContainer &rays)
-{
-	int leafOut;
-	Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
+void VspBspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
+												   const int maxViewCells,
+												   const bool sortViewCells,
+												   const bool exportPvs,
+												   const bool exportRays,
+												   const int maxRays,
+												   const string prefix,
+												   VssRayContainer *visRays)
+{	
+	if (sortViewCells)
+	{
+		// sort view cells to visualize the largest view cells
+		stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::LargerRenderCost);
+	}
+
+	//////////
+	//-- some view cells for output
 
 	ViewCell::NewMail();
-
-	cout << "visualization using " << (int)rays.size() << " samples" << endl;
-	Debug << "visualization using " << (int)rays.size() << " samples" << endl;
-	Debug << "\nOutput view cells: " << endl;
-
-	const bool sortViewCells = true;
-
-	// sort view cells to visualize the largest view cells
-	if (sortViewCells)
-	{
-		//stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
-		stable_sort(mViewCells.begin(), mViewCells.end(), ViewCell::LargerRenderCost);
-	}
-
-	//////////
-	//-- some rays for output
-
-	const int limit = min(leafOut, (int)mViewCells.size());
-	const int raysOut = min((int)rays.size(), 100);
-
+	const int limit = min(maxViewCells, (int)mViewCells.size());
+	
 	for (int i = 0; i < limit; ++ i)
 	{
 		cout << "creating output for view cell " << i << " ... ";
 
-		ViewCell *vc;
-	
-		if (sortViewCells) // largest view cell pvs first
-			vc = mViewCells[i];
-		else
-			vc = mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 1)];
+		ViewCell *vc = sortViewCells ? // largest view cell pvs first?
+			mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 0.5f)] : mViewCells[i];
+
+		if (vc->Mailed() || vc->GetId() == OUT_OF_BOUNDS_ID)
+			continue;
+
+		vc->Mail();
 
 		ObjectPvs pvs;
 		mViewCellsTree->GetPvs(vc, pvs);
 
-		char s[64]; sprintf(s, "bsp-pvs%04d.wrl", i);
+		char s[64]; sprintf(s, "%sviewcell%04d.wrl", prefix.c_str(), i);
 		Exporter *exporter = Exporter::GetExporter(s);
 		
-		Debug << i << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc) << endl;
-
-		if (mExportRays)
+		cout << "view cell " << vc->GetId() << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc) << endl;
+
+		if (exportRays)
 		{
 			////////////
@@ -4267,5 +4301,5 @@
 			ViewCellContainer::const_iterator vit, vit_end = leaves.end();
        		for (vit = leaves.begin(); vit != vit_end; ++ vit)
-			{
+			{	
 				BspLeaf *vcLeaf = dynamic_cast<BspViewCell *>(*vit)->mLeaves[0];
 				VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
@@ -4277,4 +4311,6 @@
 			}
 
+			const int raysOut = min((int)collectRays.size(), maxRays);
+			cout << "here500 " << raysOut << endl;
 			// prepare some rays for output
 			VssRayContainer::const_iterator rit, rit_end = collectRays.end();
@@ -4304,39 +4340,30 @@
 		exporter->SetFilled();
 
-		if (1)
-		{
-			////////
-			//-- export pvs
+		if (exportPvs)
+		{
 			Intersectable::NewMail();
+
 			ObjectPvsMap::const_iterator oit, oit_end = pvs.mEntries.end();
-			
+			cout << endl;
 			// output PVS of view cell
 			for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
 			{		
 				Intersectable *intersect = (*oit).first;
+				
 				if (!intersect->Mailed())
 				{
+					intersect->Mail();
 					m = RandomMaterial();
 					exporter->SetForcedMaterial(m);
-
 					exporter->ExportIntersectable(intersect);
-					intersect->Mail();
+					cout << " i: " << intersect;
 				}
 			}
+			cout << endl;
 		}
 		
-		if (0)
-		{   // export scene geometry
-			m.mDiffuseColor = RgbColor(1, 0, 0);
-			exporter->SetForcedMaterial(m);
-
-			exporter->ExportGeometry(objects);
-		}
-
 		DEL_PTR(exporter);
 		cout << "finished" << endl;
 	}
-
-	Debug << endl;
 }
 
@@ -5105,23 +5132,23 @@
 	}
 	
-	// export pvss of some view cell
-	ExportPvs(objects, visRays);
-}
-
-
-void VspOspViewCellsManager::ExportPvs(const ObjectContainer &objects,
-									   const VssRayContainer &rays)
-{
+	// export some view cell
 	int leafOut;
 	Environment::GetSingleton()->GetIntValue("ViewCells.Visualization.maxOutput", leafOut);
-
-	ViewCell::NewMail();
-
-	cout << "visualization using " << (int)rays.size() << " samples" << endl;
-	Debug << "visualization using " << (int)rays.size() << " samples" << endl;
-	
-	const bool sortedViewCells = false;
-
-	if (sortedViewCells)
+	const int raysOut = 100;
+
+	ExportSingleViewCells(objects, leafOut, false, true, false, raysOut, "");
+}
+
+
+void VspOspViewCellsManager::ExportSingleViewCells(const ObjectContainer &objects,
+												   const int maxViewCells,
+												   const bool sortViewCells,
+												   const bool exportPvs,
+												   const bool exportRays,
+												   const int maxRays,
+												   const string prefix,
+												   VssRayContainer *visRays)
+{
+	if (sortViewCells)
 	{
 		// sort view cells to visualize the view cells with highest render cost
@@ -5129,11 +5156,8 @@
 	}
 
-	int limit = min(leafOut, (int)mViewCells.size());
-	int raysOut = 0;
-
-	cout << "\nOutput of " << limit << " view cells: " << endl;
-
-	///////////////
-	//-- some rays for output
+	ViewCell::NewMail();
+	const int limit = min(maxViewCells, (int)mViewCells.size());
+	
+	cout << "\nExporting " << limit << " single view cells: " << endl;
 	
 	for (int i = 0; i < limit; ++ i)
@@ -5142,77 +5166,84 @@
 		
 		// largest view cell pvs first of random view cell
-		ViewCell *vc = sortedViewCells ? 
+		ViewCell *vc = sortViewCells ? 
 			mViewCells[i] : mViewCells[(int)RandomValue(0, (float)mViewCells.size() - 1)];
 		
+		if (vc->Mailed()) // already used
+			continue;
+
+		vc->Mail();
+
 		ObjectPvs pvs;
 		mViewCellsTree->GetPvs(vc, pvs);
 
-		char s[64]; sprintf(s, "bsp-pvs%04d.wrl", i);
+		char s[64]; sprintf(s, "%sviewcell%04d.wrl", prefix.c_str(), i);
 		Exporter *exporter = Exporter::GetExporter(s);
 		
-		Debug << i << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc) << endl;
-
-		////////////
-		//-- export the sample rays
-
-		if (1 || mExportRays)
-		{
-			// output rays stored with the view cells during subdivision
-			if (1)
-			{
-				VssRayContainer vcRays;
-                VssRayContainer collectRays;
-
-				raysOut = min((int)rays.size(), 100);
-
-				// collect intial view cells
-				ViewCellContainer leaves;
-				mViewCellsTree->CollectLeaves(vc, leaves);
-
-				ViewCellContainer::const_iterator vit, vit_end = leaves.end();
-       
-				for (vit = leaves.begin(); vit != vit_end; ++ vit)
+		cout << "view cell " << vc->GetId() << ": pvs size=" << (int)mViewCellsTree->GetPvsSize(vc) << endl;
+
+		if (exportPvs)
+		{
+			Material m;
+
+			Intersectable::NewMail();
+			ObjectPvsMap::const_iterator oit, oit_end = pvs.mEntries.end();
+			
+			// output PVS of view cell
+			for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
+			{		
+				Intersectable *intersect = (*oit).first;
+				if (!intersect->Mailed())
 				{
-					VspLeaf *vcLeaf = dynamic_cast<VspViewCell *>(*vit)->mLeaves[0];
-					VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
-
-					for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
-					{
-						collectRays.push_back(*rit);
-					}
-				}
-
-				VssRayContainer::const_iterator rit, rit_end = collectRays.end();
-
-				for (rit = collectRays.begin(); rit != rit_end; ++ rit)
-				{
-					const float p = RandomValue(0.0f, (float)collectRays.size());
-			
-					if (p < raysOut)
-						vcRays.push_back(*rit);
-				}
-				
-				//-- export rays piercing this view cell
-				exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
-
-				//-- export objects seen from the rays
-				Intersectable::NewMail();
-				
-				VssRayContainer::const_iterator vcit, vcit_end = vcRays.end();
-				for (vcit = vcRays.begin(); vcit != vcit_end; ++ vcit)
-				{
-					VssRay *ray = *vcit;
-					//Intersectable *obj = ray->mTerminationObject;
-					Intersectable *obj = mHierarchyManager->GetIntersectable(*ray, true);
-					
-					if (obj && !obj->Mailed())
-					{
-						obj->Mail();
-						exporter->ExportIntersectable(obj);
-					}
+					m = RandomMaterial();
+					exporter->SetForcedMaterial(m);
+
+					exporter->ExportIntersectable(intersect);
+					intersect->Mail();
 				}
 			}
 		}
 
+		if (exportRays)
+		{
+			////////////
+			//-- export the sample rays
+
+			// output rays stored with the view cells during subdivision
+			VssRayContainer vcRays;
+			VssRayContainer collectRays;
+
+			// collect intial view cells
+			ViewCellContainer leaves;
+			mViewCellsTree->CollectLeaves(vc, leaves);
+
+			ViewCellContainer::const_iterator vit, vit_end = leaves.end();
+
+			for (vit = leaves.begin(); vit != vit_end; ++ vit)
+			{
+				VspLeaf *vcLeaf = dynamic_cast<VspViewCell *>(*vit)->mLeaves[0];
+				VssRayContainer::const_iterator rit, rit_end = vcLeaf->mVssRays.end();
+
+				for (rit = vcLeaf->mVssRays.begin(); rit != rit_end; ++ rit)
+				{
+					collectRays.push_back(*rit);
+				}
+			}
+
+			const int raysOut = min((int)collectRays.size(), maxRays);
+
+			VssRayContainer::const_iterator rit, rit_end = collectRays.end();
+
+			for (rit = collectRays.begin(); rit != rit_end; ++ rit)
+			{
+				const float p = RandomValue(0.0f, (float)collectRays.size());
+
+				if (p < raysOut)
+					vcRays.push_back(*rit);
+			}
+
+			exporter->ExportRays(vcRays, RgbColor(1, 1, 1));
+		}
+		
+	
 		/////////////////
 		//-- export view cell geometry
@@ -5231,5 +5262,5 @@
 	}
 
-	Debug << endl;
+	cout << endl;
 }
 
@@ -5535,6 +5566,5 @@
 void VspOspViewCellsManager::EvalViewCellPartition()
 {
-	int castSamples = (int)storedRays.size();
-
+	const int castSamples = (int)storedRays.size();
 	char s[64]; 
 	char statsPrefix[100];
@@ -5545,5 +5575,5 @@
 
 	// should directional sampling be used?
-	bool dirSamples = (mEvaluationSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
+	const bool dirSamples = (mEvaluationSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
 
 	cout << "reseting pvs ... ";
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1570)
@@ -114,4 +114,5 @@
 //mMailbox(0)
 {
+	mId = -100;
 }
 
@@ -127,4 +128,5 @@
 //mMailbox(0)
 {
+	mId = -100;
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1570)
@@ -56,5 +56,5 @@
 }
 
-
+#if WORK_WITH_VIEWCELLS
 static bool ViewCellHasMultipleReferences(Intersectable *obj, 
 										  ViewCell *vc, 
@@ -75,8 +75,45 @@
 		}
 	}
-
+	
 	return false;
 }
 
+
+void VspTree::RemoveParentViewCellReferences(ViewCell *parent) const
+{
+	KdLeaf::NewMail();
+
+	// remove the parents from the object pvss
+	ObjectPvsMap::const_iterator oit, oit_end = parent->GetPvs().mEntries.end();
+
+	for (oit = parent->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
+	{
+		Intersectable *object = (*oit).first;
+		// HACK: make sure that the view cell is removed from the pvs
+		const float high_contri = 9999999;
+
+		// remove reference count of view cells
+		object->mViewCellPvs.RemoveSample(parent, high_contri);
+	}
+}
+
+
+void VspTree::AddViewCellReferences(ViewCell *vc) const
+{
+	KdLeaf::NewMail();
+
+	// Add front view cell to the object pvsss
+	ObjectPvsMap::const_iterator oit, oit_end = vc->GetPvs().mEntries.end();
+
+	for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
+	{
+		Intersectable *object = (*oit).first;
+
+		// increase reference count of view cells
+		object->mViewCellPvs.AddSample(vc, 1);
+	}
+}
+
+#endif
 
 void VspTreeStatistics::Print(ostream &app) const
@@ -826,5 +863,6 @@
 	++ mVspStats.splits[splitPlane.mAxis];
 
-    ///////////////////////////////////////////
+
+    ///////////////////////
 	//-- create front and back and subdivide further
 
@@ -837,7 +875,8 @@
 		parent->ReplaceChildLink(leaf, interior);
 		interior->SetParent(parent);
-
+#if WORK_WITH_VIEWCELLS
 		// remove "parent" view cell from pvs of all objects (traverse trough rays)
 		RemoveParentViewCellReferences(tData.mNode->GetViewCell());
+#endif
 	}
 	else // new root
@@ -866,5 +905,6 @@
 #if WORK_WITH_VIEWCELL_PVS
 	// create front and back view cell
-	// add front and back view cell to "Potentially Visbilie View Cells" 
+	// add front and back view cell to 
+	// "potentially visible view cells" 
 	// of the objects in front and back pvs
 
@@ -880,38 +920,4 @@
 
 
-void VspTree::RemoveParentViewCellReferences(ViewCell *parent) const
-{
-	KdLeaf::NewMail();
-
-	// remove the parents from the object pvss
-	ObjectPvsMap::const_iterator oit, oit_end = parent->GetPvs().mEntries.end();
-
-	for (oit = parent->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-	{
-		Intersectable *object = (*oit).first;
-		// HACK: make sure that the view cell is removed from the pvs
-		const float high_contri = 9999999;
-
-		// remove reference count of view cells
-		object->mViewCellPvs.RemoveSample(parent, high_contri);
-	}
-}
-
-
-void VspTree::AddViewCellReferences(ViewCell *vc) const
-{
-	KdLeaf::NewMail();
-
-	// Add front view cell to the object pvsss
-	ObjectPvsMap::const_iterator oit, oit_end = vc->GetPvs().mEntries.end();
-
-	for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-	{
-		Intersectable *object = (*oit).first;
-
-		// increase reference count of view cells
-		object->mViewCellPvs.AddSample(vc, 1);
-	}
-}
 
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1344)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1570)
@@ -39,21 +39,24 @@
   /// unique object Id
   int mId;
-  
   /// universal counter
   int mCounter;
-	
-  /// object based pvs
-  KdPvs mKdPvs;
-  
-  /// view cell based pvs per object
-  ViewCellPvs mViewCellPvs;
 
   /// pointer to the container bvh leaf
   BvhLeaf *mBvhLeaf;
-
+  /// some rays piercing this intersectable
   VssRayContainer mVssRays;
 
   /// # of references to this instance
   int mReferences;
+
+  //////////////////
+  // note matt: delete these, they are only taking memory+
+
+  /// object based pvs
+  KdPvs mKdPvs;
+  /// view cell based pvs per object
+  ViewCellPvs mViewCellPvs;
+	
+  ///////////////////////
 
   enum { MESH_INSTANCE, 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1566)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1570)
@@ -65,8 +65,8 @@
 	{
 		VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
+		// ray is not pushed into the queue => can delete ray
 		if (!HandleRay(newRay))
-		{
 			delete newRay;
-		}
+		
 		return true;
 	}
@@ -78,5 +78,6 @@
 bool GvsPreprocessor::HandleRay(VssRay *vssRay)
 {
-	mViewCellsManager->ComputeSampleContribution(*vssRay, true, false);
+	const bool storeRaysForViz = true;
+	mViewCellsManager->ComputeSampleContribution(*vssRay, true, storeRaysForViz);
 		
 	// some pvs contribution for this ray?
@@ -85,5 +86,14 @@
 		//cout << " h " << mSampleContriPerPass << " " << mSamplesPerPass << " " << mTotalSamples << endl;
 		mRayQueue.push(vssRay);
-		mVssRays.push_back(new VssRay(*vssRay));
+
+		if (storeRaysForViz)
+		{
+			ViewCellContainer::const_iterator vit, vit_end = vssRay->mViewCells.end();
+			for (vit = vssRay->mViewCells.begin(); vit != vit_end; ++ vit)
+			{
+				(*vit)->mVssRays.push_back(new VssRay(*vssRay));
+			}
+		}
+		//mVssRays.push_back(new VssRay(*vssRay));
         ++ mSampleContriPerPass;
 
@@ -443,6 +453,18 @@
 		mViewCellsManager->PrintPvsStatistics(mStats);
 
+		char str[64]; sprintf(str, "tmp/pass%04d-", mPass);
+		
 		// visualization
-//		mViewCellsManager->Visualize(mVssRays);
+		const bool exportRays = true;
+		const bool exportPvs = true;
+		mViewCellsManager->ExportSingleViewCells(mObjects, 10, false, exportPvs, exportRays, 100, str);
+		// remove pass samples
+		ViewCellContainer::const_iterator vit, vit_end = mViewCellsManager->GetViewCells().end();
+
+		for (vit = mViewCellsManager->GetViewCells().begin(); vit != vit_end; ++ vit)
+		{
+			CLEAR_CONTAINER((*vit)->mVssRays);
+		}
+
 		CLEAR_CONTAINER(mVssRays);
 		// ComputeRenderError();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 1551)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 1570)
@@ -27,4 +27,5 @@
 
 
+	////////////
 //-- static members
 
@@ -39,5 +40,5 @@
 {
 	MailablePvsData *vdata = obj->mViewCellPvs.Find(vc);
-//return false;
+
 	if (vdata)
 	{
@@ -1968,5 +1969,4 @@
 {	
 	contribution = 0; // todo
-
 	MailablePvsData *vdata = obj->mViewCellPvs.Find(vc);
 
@@ -1979,5 +1979,4 @@
 		obj->mViewCellPvs.AddSample(vc, 1);
 	}
-	
 	vdata->Mail();
 
