Index: GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp	(revision 2035)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Mutation.cpp	(revision 2048)
@@ -712,4 +712,6 @@
   sray = SimpleRay(origin, direction, MUTATION_BASED_DISTRIBUTION, pdf);
   sray.mGeneratorId = index;
+
+  return true;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 2023)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 2048)
@@ -209,5 +209,5 @@
 				Name="VCLinkerTool"
 				AdditionalDependencies="xerces-c_2.lib glew32.lib zdll.lib zziplib.lib devil.lib glut32.lib OpenGL32.Lib glu32.lib Preprocessor.lib RTScene.lib RTWorld.lib QtCore4.lib qtmain.lib cg.lib cgGL.lib QtOpenGL4.lib Qt3Support4.lib QtTest4.lib QtGui4.lib QtInterface.lib"
-				OutputFile="../bin/release/Preprocessor.exe"
+				OutputFile="../bin/release/Preprocessor2.exe"
 				LinkIncremental="1"
 				AdditionalLibraryDirectories="..\src\GL;..\lib\release;..\..\Preprocessing\lib\release;..\..\..\..\..\..\NonGTP\Boost\lib;..\..\..\..\..\..\NonGTP\Xerces\xercesc\lib;..\..\..\..\..\..\NonGTP\Zlib\lib;..\..\..\..\..\..\NonGTP\Devil\lib;..\MultiLevelRayTracing\RTScene\Release;..\MultiLevelRayTracing\RTWorld\Release;&quot;$(QTDIR)\lib&quot;;.\QtInterface\Release;&quot;$(CG_LIB_PATH)&quot;"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2003)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2048)
@@ -215,9 +215,15 @@
 	void IntersectWithViewCell();
 
+	void DeterminePvsObjects(VssRayContainer &rays);
+
+	virtual void ComputeRenderError();
+
+	void StorePvs(const ObjectContainer &objectPvs);
+
+
 	//////////////////////
 
-
-	int mSamplesPerPass;
-	int mTotalSamples;
+	//int mSamplesPerPass;
+	//int mTotalSamples;
 	int mInitialSamples;
 
@@ -230,4 +236,6 @@
 	VssRayContainer mVssRays;
 	
+
+
 	///////////
 	// stats
@@ -250,4 +258,11 @@
 
 	ObjectContainer mTrianglePvs;
+
+	// HACK
+	int mGvsSamplesPerPass;
+
+	bool mComputeRenderError;
+
+	bool mEvaluatePixelError;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 2021)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 2048)
@@ -541,11 +541,15 @@
 							   vector<ViewCellPoints *> &viewCells);
 
+	bool ImportRandomViewCells(const string &filename);
 
 	bool ExportRandomViewCells(const string &filename,
 							   const vector<ViewCellPoints *> &viewCells);
 
+	bool ExportRandomViewCells(const string &filename);
+
 	bool GenerateViewPoint(ViewCell *viewCell, SimpleRay &ray);
 
 	bool IsValidViewSpace(ViewCell *vc);
+
 
 	//////////////
@@ -558,9 +562,9 @@
 		@param objects the scene objects
 		@param finalizeViewCells if the view cells should be post processed, i.e.
-			a mesh is created representing the geometry
+			   a mesh is created representing the geometry
 		@param bconverter a conversion routine working with the similarities of bounding
-			boxes: if there is a certain similarity of overlap between 
-			bounding boxes, two tested
-		candidate objects are considered to be the same objects
+			   boxes: if there is a certain similarity of overlap between 
+			   bounding boxes, two tested candidate objects are considered 
+			   to be the same objects
 		@returns the view cells manager if loading was successful, false otherwise
 	*/
@@ -575,15 +579,14 @@
 
 	// TODO: write own visibiltiy filter class
-  void ApplyFilter(KdTree *kdTree,
-				   const float viewSpaceFilterSize,
-				   const float spatialFilterSize);
-
-  // new adaptive version of the filter
-  PvsFilterStatistics 
-  ApplyFilter2(ViewCell *viewCell,
-			   const bool useViewSpaceFilter,
-			   const float filterSize,
-			   ObjectPvs &pvs,
-			   vector<AxisAlignedBox3> *filteredBoxes = NULL);
+	void ApplyFilter(KdTree *kdTree,
+					 const float viewSpaceFilterSize,
+					 const float spatialFilterSize);
+
+	// new adaptive version of the filter
+	PvsFilterStatistics ApplyFilter2(ViewCell *viewCell,
+									 const bool useViewSpaceFilter,
+									 const float filterSize,
+									 ObjectPvs &pvs,
+									 vector<AxisAlignedBox3> *filteredBoxes = NULL);
 
 	void ApplySpatialFilter(KdTree *kdTree,
@@ -606,4 +609,5 @@
 
 	//////////////////////////////////////////////////////////////////
+
 
 	/** If true, the kd nodes are stored instead of the object pvs.
@@ -635,5 +639,5 @@
 
 
-	void ViewCellsManager::ResetPvs();
+	void ResetPvs();
 
 	Preprocessor *GetPreprocessor() const {
@@ -643,4 +647,9 @@
 	void SetPreprocessor(Preprocessor *p) {
 		mPreprocessor = p;
+	}
+
+	vector<ViewCellPoints *> *GetViewCellPoints()
+	{
+		return &mViewCellPoints;
 	}
 
@@ -741,4 +750,5 @@
 	void ExportMergedViewCells(const ObjectContainer &objects);
 
+
 	///////////////////////
 
@@ -746,7 +756,9 @@
 	*/
 	virtual float GetViewSpaceVolume();
+
 	/** Prepares the view cells for sampling after loading them from disc.
 	*/
 	virtual void PrepareLoadedViewCells() {};
+	
 	/** Constructs local view cell merge hierarchy.
 	*/
@@ -755,5 +767,5 @@
 
 	/** Constructs local view cell merge hierarchy based solely on similarity with the
-	current viewcell
+		current viewcell
 	*/
   	ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell, 
@@ -766,6 +778,7 @@
 	virtual void ExportStats(const string &mergeStats);
 
-  
+
 	////////////////////////////////////////////////
+
 
 	Preprocessor *mPreprocessor;
@@ -816,6 +829,9 @@
 	bool mCompressViewCells;
 
+	vector<ViewCellPoints *> mViewCellPoints;
+
 	/// holds the current view cell statistics
 	ViewCellsStatistics mCurrentViewCellsStats;
+
 	/// the scene bounding box
 	AxisAlignedBox3 mViewSpaceBox;
@@ -845,4 +861,5 @@
 
 	MixtureDistribution *mMixtureDistribution;
+
 
 	//////////////////
@@ -1306,5 +1323,4 @@
 	/////////////////////////////////////////
 
-
 	bool mCompressObjects;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h	(revision 2014)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h	(revision 2048)
@@ -69,5 +69,5 @@
   // pool of vss rays to be used in one pass of the sampling
   struct VssRayPool {
-	VssRayPool():mRays(NULL), mIndex(0) {}
+	VssRayPool():mRays(NULL), mIndex(0), mNumber(0) {}
 	~VssRayPool() {
 	  if (mRays)
@@ -79,4 +79,5 @@
 		delete mRays;
 	  mRays = new VssRay[number];
+	  mNumber = number;
 	}
 	
@@ -85,4 +86,7 @@
 	}
 	VssRay *Alloc() {
+		// reset pool
+		if (mIndex == mNumber)
+			mIndex = 0;
 	  return mRays + mIndex++;
 	}
@@ -90,4 +94,5 @@
 	VssRay *mRays;
 	int mIndex;
+	int mNumber;
   };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2046)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 2048)
@@ -1296,5 +1296,5 @@
 		 "1000000");
    
-   RegisterOption("GvsPreprocessor.samplesPerPass",
+   RegisterOption("GvsPreprocessor.gvsSamplesPerPass",
 		 optInt,
 		 "gvs_samples_per_pass=",
@@ -1611,5 +1611,4 @@
 		"view_cells_compress_objects=", 
 		"false");
-
 	
 	RegisterOption("ViewCells.useKdPvs",
@@ -1618,5 +1617,4 @@
 		"false");
 
-	
 	RegisterOption("ViewCells.useKdPvsAfterFiltering",
 				   optBool, 
@@ -1624,6 +1622,15 @@
 				   "false");
 
-
-	
+	RegisterOption("ViewCells.importRandomViewCells",
+				   optBool, 
+				   "view_cells_import_random_viewcells=", 
+				   "false");
+
+	RegisterOption("ViewCells.exportRandomViewCells",
+				   optBool, 
+				   "view_cells_export_random_viewcells=", 
+				   "false");
+
+
 
 	/****************************************************************************/
@@ -1784,7 +1791,10 @@
 	RegisterOption("BspTree.Factor.pvs", optFloat, "bsp_factor_pvs=", "1.0");
 
-	/************************************************************************************/
-	/*                         Preprocessor related options                             */
-	/************************************************************************************/
+
+
+
+	/**********************************************************************/
+	/*                     Preprocessor related options                   */
+	/**********************************************************************/
 
 	RegisterOption("Preprocessor.type",
@@ -1863,4 +1873,9 @@
 				   "false");
 	
+	RegisterOption("Preprocessor.evaluatePixelError",
+				   optBool,
+				   "preprocessor_evaluatePixelError",
+				   "false");
+
 	RegisterOption("Preprocessor.applyVisibilitySpatialFilter",
 				   optBool,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2047)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2048)
@@ -155,6 +155,5 @@
 	Environment::GetSingleton()->GetStringValue("Preprocessor.stats",  buffer);
 	mStats.open(buffer);
-	
-	
+		
 	Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter);
 	Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter",
@@ -182,4 +181,6 @@
 	Debug << "export obj: " << mExportObj << endl;
 	Debug << "use view space box: " << mUseViewSpaceBox << endl;
+
+	cout << "samples per pass " << mSamplesPerPass << endl;
 }
 
@@ -262,5 +263,4 @@
 }
 
-#define POWERPLANT_HACK 1
 
 bool Preprocessor::LoadBinaryObj(const string filename,
@@ -1204,5 +1204,7 @@
 	
 	// reserve constant block of rays
-	mRayCaster->ReserveVssRayPool(2*mSamplesPerPass);
+	cout << "======================" << endl;
+	cout << "reserving " << 2 * mSamplesPerPass << " rays " << endl;
+	mRayCaster->ReserveVssRayPool(2 * mSamplesPerPass);
 	
 	return true;
@@ -1311,7 +1313,5 @@
 	}
 
-	mViewCellsManager->DeterminePvsObjects(vssRays
-										   );
-	
+	DeterminePvsObjects(vssRays);
 }
 
@@ -1530,3 +1530,10 @@
 }
 
-}
+
+void Preprocessor::DeterminePvsObjects(VssRayContainer &rays)
+{
+	mViewCellsManager->DeterminePvsObjects(rays, false);
+}
+
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 2023)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 2048)
@@ -221,6 +221,6 @@
 				OptimizeForProcessor="3"
 				OptimizeForWindowsApplication="TRUE"
-				AdditionalIncludeDirectories="..\src;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces;..\..\..\..\..\..\NonGTP\Boost;..\MultiLevelRayTracing;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;&quot;$(CG_INC_PATH)&quot;"
-				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;GTP_INTERNAL;USE_QT;USE_CG"
+				AdditionalIncludeDirectories="..\src;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces;..\..\..\..\..\..\NonGTP\Boost;..\MultiLevelRayTracing;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;&quot;$(CG_INC_PATH)&quot;;Timer"
+				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;GTP_INTERNAL;USE_QT"
 				ExceptionHandling="TRUE"
 				RuntimeLibrary="2"
@@ -377,4 +377,10 @@
 			</File>
 			<File
+				RelativePath=".\Timer\BenchTimer.cpp">
+			</File>
+			<File
+				RelativePath=".\Timer\BenchTimer.h">
+			</File>
+			<File
 				RelativePath="..\src\BoostPreprocessorThread.cpp">
 			</File>
@@ -527,4 +533,10 @@
 			</File>
 			<File
+				RelativePath=".\Timer\merror.cpp">
+			</File>
+			<File
+				RelativePath=".\Timer\merror.h">
+			</File>
+			<File
 				RelativePath="..\src\Mesh.cpp">
 			</File>
@@ -539,4 +551,13 @@
 			</File>
 			<File
+				RelativePath=".\Timer\mstring.h">
+			</File>
+			<File
+				RelativePath=".\Mutation.cpp">
+			</File>
+			<File
+				RelativePath=".\Mutation.h">
+			</File>
+			<File
 				RelativePath="..\src\MutualVisibility.cpp">
 			</File>
@@ -572,4 +593,10 @@
 			</File>
 			<File
+				RelativePath=".\Timer\PerfTimer.h">
+			</File>
+			<File
+				RelativePath=".\Timer\PerfTimerSkeleton.h">
+			</File>
+			<File
 				RelativePath="..\src\Plane3.cpp">
 			</File>
@@ -738,4 +765,10 @@
 			<File
 				RelativePath="..\src\RayInfo.h">
+			</File>
+			<File
+				RelativePath=".\Timer\RDTSCTimer.cpp">
+			</File>
+			<File
+				RelativePath=".\Timer\RDTSCTimer.h">
 			</File>
 			<File
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2008)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2048)
@@ -166,5 +166,5 @@
   
   
-  if (mSnapErrorFrames && pErrorPixels > 0.01f) {
+  if (mSnapErrorFrames && pErrorPixels >= 0.01f) {
 	
 	char filename[256];
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2046)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2048)
@@ -129,22 +129,19 @@
 			   );
 
-  bool
-  ExportRayAnimation(const char *filename,
-					 const vector<VssRayContainer> &vssRays
-					 );
-
-	virtual int
-		GenerateRays(const int number,
-		SamplingStrategy &strategy,
-		SimpleRayContainer &rays);
+	bool ExportRayAnimation(const char *filename,
+							const vector<VssRayContainer> &vssRays);
+
+	virtual int	GenerateRays(const int number,
+							 SamplingStrategy &strategy,
+							 SimpleRayContainer &rays);
 
 	virtual int GenerateRays(const int number,
-		const int raysType,
-		SimpleRayContainer &rays);
+							 const int raysType,
+							 SimpleRayContainer &rays);
 
 	bool GenerateRayBundle(SimpleRayContainer &rayBundle,
-		const SimpleRay &mainRay,
-		const int number,
-		const int shuffleType) const;
+						   const SimpleRay &mainRay,
+						   const int number,
+						   const int shuffleType) const;
 
 	virtual void CastRays(SimpleRayContainer &rays,
@@ -153,5 +150,6 @@
 						  const bool pruneInvalidRays = true);
 
-	/** Compute pixel error of the current PVS solution by sampling given number of viewpoints */
+	/** Compute pixel error of the current PVS solution by sampling given number of viewpoints .
+	*/
 	virtual void ComputeRenderError();
 
@@ -179,8 +177,11 @@
 	PreprocessorThread *GetThread() const;
 
-	
 	Intersectable *GetObjectById(const int id);
 
 	void PrepareHwGlobalLines();
+
+	virtual void DeterminePvsObjects(VssRayContainer &rays);
+
+
 	////////////////////////////////////////////////
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 2017)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 2048)
@@ -2114,5 +2114,5 @@
 		BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
 		const AxisAlignedBox3 box = leaf->GetBoundingBox();
-		stream << "<Leaf"
+		stream << "<Leaf id=\"" << node->GetId() << "\""
 			   << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
 			   << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z << "\""
@@ -2130,5 +2130,5 @@
 		const AxisAlignedBox3 box = interior->GetBoundingBox();
 
-		stream << "<Interior"
+		stream << "<Interior id=\"" << node->GetId() << "\""
 			   << " min=\"" << box.Min().x << " " << box.Min().y << " " << box.Min().z << "\""
 			   << " max=\"" << box.Max().x << " " << box.Max().y << " " << box.Max().z
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2043)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2048)
@@ -352,5 +352,5 @@
 
 
-bool ViewCellsManager::GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,
+/*bool ViewCellsManager::GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,
 											   const int nViewCells,
 											   const int nViewPoints)
@@ -369,5 +369,6 @@
 	ViewCellContainer::const_iterator vit, vit_end = rViewCells.end();
     
-	for (vit = rViewCells.begin(); vit != vit_end; ++ vit)
+	int i = 0;
+    for (vit = rViewCells.begin(); vit != vit_end; ++ vit, ++ i)
 	{
 		ViewCell *vc = *vit;
@@ -379,4 +380,5 @@
 
 		SimpleRayContainer viewPoints;
+		cout << "processing view cell " << i << endl;
 		// generate random view points
 		GenerateViewPoints(vc, nViewPoints, viewPoints);
@@ -388,4 +390,80 @@
 			vcPts->second.push_back(*pit);	
 		}
+	}
+
+	return true;
+}*/
+
+
+bool ViewCellsManager::GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,
+											   const int nViewCells,
+											   const int nViewPoints)
+{
+	ViewCellContainer rViewCells;
+
+	cout << "generating " << nViewCells << " random view cells" << endl;
+	///GenerateRandomViewCells(rViewCells, nViewCells);
+
+	//cout << "finished" << endl;
+
+	//for (int i = 0; i < viewCells.size(); ++ i)
+	//	cout << "vc " << i << ": " << viewCells[i]->GetId() << endl;
+
+	cout << "generating " << nViewPoints << " view points per view cell" << endl;
+	    
+	int generatedViewCells = 0;
+	int i = 0;
+    while (generatedViewCells < nViewCells)
+	{
+		++ i;
+		
+		const int idx = (int)RandomValue(0.0f, (float)mViewCells.size() - 0.5f);
+			
+		ViewCell *viewCell = GetViewCell(idx);
+		
+		cout << "testing view cell: " << viewCell->GetId() << endl;
+
+		if (!viewCell->Mailed())
+		{
+			viewCell->Mail();
+
+			SimpleRayContainer viewPoints;
+			cout<<"here18"<<endl;
+			// generate random view points
+			if (IsValidViewSpace(viewCell) && 
+				GenerateViewPoints(viewCell, nViewPoints, viewPoints))
+			{cout << "here99" << endl;
+				Debug << "vc: " << viewCell->GetId() << endl;
+
+				ViewCellPoints *vcPts = new ViewCellPoints();
+				viewCells.push_back(vcPts);
+
+				// valid view cell found
+				vcPts->first = viewCell;
+
+				// generate view points
+				++ generatedViewCells;
+
+				SimpleRayContainer::const_iterator pit, pit_end = viewPoints.end();
+
+				for (pit = viewPoints.begin(); pit != pit_end; ++ pit)
+				{
+					Debug << "vp: " << (*pit) << endl;
+					vcPts->second.push_back(*pit);	
+				}
+				cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;	
+			}
+			else
+			{
+				cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;	
+			}
+		}
+
+		if (i > nViewCells * 1000000) // safety
+		{
+			cout << "big error" << endl;
+			break;
+		}
+		cout << "processd view cells " << generatedViewCells << " of " << nViewCells << endl << endl;
 	}
 
@@ -437,4 +515,10 @@
 
 
+bool ViewCellsManager::ImportRandomViewCells(const string &filename)
+{
+	return ImportRandomViewCells(filename, mViewCellPoints);
+}
+
+
 bool ViewCellsManager::GenerateViewPoints(ViewCell *viewCell, 
 										  const int numViewPoints,
@@ -445,4 +529,6 @@
 	int i = 0;
 
+	cout << "generating view points for view cell " << viewCell->GetId() << endl;
+
 	while (generatedPts < numViewPoints)
 	{
@@ -450,11 +536,12 @@
 
 		if (GenerateViewPoint(viewCell, pt))
-		{
+		{cout<<"here4"<<endl;
 			++ generatedPts;
+			cout << "generated view point " << generatedPts << endl;
 			viewPoints.push_back(pt);
 		}
 
 		// savety criterium
-		if (++ i > numViewPoints + 100000)
+		if (++ i > numViewPoints * 3)
 		{
 			return false;
@@ -462,5 +549,20 @@
 	}
 	
+	cout << "view point generation finished" << endl;
+
 	return true;
+}
+
+
+bool ViewCellsManager::ExportRandomViewCells(const string &filename)
+{
+	const int numViewCells = 100;
+	const int numViewPoints = 10;
+
+	preprocessor->mViewCellsManager->
+		GenerateRandomViewCells(mViewCellPoints, numViewCells, numViewPoints);
+
+	//cout << "exporting random view cells" << endl;
+	return preprocessor->mViewCellsManager->ExportRandomViewCells(filename,  mViewCellPoints);
 }
 
@@ -473,5 +575,5 @@
 	int tries = 0;
 	Vector3 viewPoint, direction;
-	const int maxTries = 100;
+	const int maxTries = 10;
 
 	while (1) 
@@ -499,23 +601,18 @@
 		//halton.GenerateNext();
 
-		//cout << "vp: " << viewPoint << endl;
-		//cout << "dir: " << direction << endl;
-
 		ViewCell *v = GetViewCell(viewPoint);
 
 		if (v && v->GetValid())
 		{
-			//cout << "validating view point" << endl;
-
 			mPreprocessor->GetRenderer()->mViewPoint = viewPoint;
 			mPreprocessor->GetRenderer()->mViewDirection = direction;
-			//preprocessor->renderer->EvalPvsStat();
-cout<<"*********hgere55"<<endl;
-			if (preprocessor->renderer->ValidViewPoint())
+			//mPreprocessor->GetRenderer()->EvalPvsStat();
+
+			if (mPreprocessor->GetRenderer()->ValidViewPoint())
 			{
-				cout << "view point valid" << endl;
+				cout << "view point valid " << viewPoint << " " << direction << endl;
 				break;
 			}
-			cout<<"here33"<<endl;
+			
 		}
 
@@ -526,5 +623,8 @@
 		//	cout << "error!" << endl;
 		if (++ tries > maxTries)
+		{
+			cerr << "error: no view point computed" << endl;
 			return false;
+		}
 	}
 
@@ -584,5 +684,5 @@
 				else
 				{
-					cout << "error: invalid view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;	
+					cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;	
 				}
 			}
@@ -615,4 +715,6 @@
 
 	DEL_PTR(mMixtureDistribution);
+	
+	CLEAR_CONTAINER(mViewCellPoints);
 }
 
@@ -1005,4 +1107,9 @@
 			vm->FinalizeViewCells(true);
 			// vm->mViewCellsTree->AssignRandomColors();
+		}
+
+		if (0)
+		{
+			cout << "=============== " << vm->GetViewCellById(5318)->GetPvs().GetSize() << "========" << endl;
 		}
 
@@ -2609,6 +2716,4 @@
 
 
-
-
 int ViewCellsManager::GetNumViewCells() const
 {
@@ -3540,4 +3645,5 @@
 }
 
+
 void ViewCellsManager::UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs)
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2023)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2048)
@@ -860,12 +860,20 @@
 GlRendererBuffer::EvalPvsStat()
 {
-cout<<"here15"<<endl;
-  MakeCurrent();
-
-  GlRenderer::EvalPvsStat();
-  
-  DoneCurrent();
-  cout<<"here185"<<endl;
+	MakeCurrent();
+
+	GlRenderer::EvalPvsStat();
+
+	DoneCurrent();
   //  mRenderingFinished.wakeAll();
+}
+
+
+void GlRendererBuffer::EvalPvsStat(const SimpleRayContainer &viewPoints)
+{
+	MakeCurrent();
+
+	GlRenderer::EvalPvsStat(viewPoints);
+  
+	DoneCurrent();
 }
 
@@ -1291,10 +1299,13 @@
 
 bool GlRendererBuffer::ValidViewPoint()
-{	cout << "here49" << endl;
-	/*MakeCurrent();
+{	
+	MakeCurrent();
+
+	SetupProjection(GetWidth(), GetHeight());
 
 	bool result = GlRenderer::ValidViewPoint();
+
 	DoneCurrent();
-*/bool result = true;
+	
 	return result;
 }
@@ -1304,5 +1315,4 @@
 GlRenderer::EvalPvsStat()
 {
-cout <<"here3399"<<endl;
   mPvsStat.Reset();
   halton.Reset();
@@ -1372,9 +1382,88 @@
 }
 
+
+void GlRenderer::EvalPvsStat(const SimpleRayContainer &viewPoints)
+{
+	ClearErrorBuffer();
+
+	mPvsStat.Reset();
+	halton.Reset();
+
+	SetupProjection(GetWidth(), GetHeight());
+
+	cout << "mPvsStatFrames=" << mPvsStatFrames << endl;
+
+	SimpleRayContainer::const_iterator sit, sit_end = viewPoints.end();
+	int i = 0;
+
+	for (sit = viewPoints.begin(); sit != sit_end; ++ sit, ++ i) 
+	{
+		//cout << "view point: " << (*sit) << endl;;
+		SimpleRay sray = *sit;
+
+		float err;
+
+		// set frame id for saving the error buffer
+		mFrame = i;
+		mViewPoint = sray.mOrigin;
+		mViewDirection = sray.mDirection;
+
+	 	if (0 && (mPvsErrorBuffer[i].mError == 1.0f))
+		{
+			// check if the view point is valid
+			if (!ValidViewPoint())
+			{
+				cout << "error: " << " view point invalid" << endl;
+				mPvsErrorBuffer[i].mError = -1.0f;
+			}
+			else
+			{
+				cout << "view point valid" << endl;
+			}
+		}
+
+		if (mPvsErrorBuffer[i].mError > 0.0f) 
+		{
+			int pvsSize;
+
+			// compute the pixel error
+			float error = GetPixelError(pvsSize);
+
+			mPvsErrorBuffer[i].mError = error;
+			mPvsErrorBuffer[i].mPvsSize = pvsSize;
+
+			cout << "(" << i << "," << mPvsErrorBuffer[i].mError <<")";
+		}
+
+		err = mPvsErrorBuffer[i].mError;
+
+		if (err >= 0.0f) 
+		{
+			if (err > mPvsStat.maxError)
+				mPvsStat.maxError = err;
+
+			mPvsStat.sumError += err;
+			mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
+
+			if (err == 0.0f)
+				++ mPvsStat.errorFreeFrames;
+
+			++ mPvsStat.frames;
+		}
+	}
+
+	glFinish();
+	cout << endl << flush;
+}
+
+
+
 bool
 GlRenderer::ValidViewPoint()
 {
-  if (!mDetectEmptyViewSpace)
-	return true;
+   //cout<<"VV4 ";
+	if (!mDetectEmptyViewSpace)
+		return true;
+	//cout << "vp: " << mViewPoint << " dir: " << mViewDirection << endl;
 
   OcclusionQuery *query = mOcclusionQueries[0];
@@ -1415,6 +1504,8 @@
   //  cout<<"VV4 ";
   
+	  //cout<<"count: " << pixelCount<<endl;
+  
   if (pixelCount > 0)
-  {cout<<"count: " << pixelCount<<endl;
+  {
 	return false; // backfacing polygon found -> not a valid viewspace sample
   }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2032)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2048)
@@ -285,9 +285,23 @@
 	}
 
-	string viewCellsFile = ReplaceSuffix(filename, ".obj", ".vc");
-	//viewCellsFile = ReplaceSuffix(filename, ".dat", ".vc");
-
-	const int numViewCells = 5;
-	const int numViewPoints = 20;
+	string viewCellPointsFile;
+
+	if (strstr(filename.c_str(), ".obj"))
+		viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
+	else if (strstr(filename.c_str(), ".dat"))
+		viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
+	else if (strstr(filename.c_str(), ".x3d"))
+		viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
+
+	bool importRandomViewCells;
+	Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
+											   importRandomViewCells);
+
+	if (importRandomViewCells)
+	{
+		cout << "importing random view cells" << endl;
+		preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
+		cout << "finished" << endl;
+	}
 
 	bool useHwGlobalLines;
@@ -377,14 +391,23 @@
 		}
 
-		vector<ViewCellPoints *> myViewCells;
-
-		preprocessor->mViewCellsManager->GenerateRandomViewCells(myViewCells, numViewCells, numViewPoints);
-		cout << "exporting random view cells" << endl;
-		preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellsFile, myViewCells);
-
-		//preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellsFile, myViewCells);
-		CLEAR_CONTAINER(myViewCells);
-
-		//preprocessor->renderer->EvalPvsStat();
+		bool exportRandomViewCells;
+		Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
+												  exportRandomViewCells);
+
+		if (exportRandomViewCells)
+		{
+			cout << "exporting random view cells" << endl;
+			preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
+			cout << "finished" << endl;
+		}
+
+		/*bool evaluatePixelError;
+		Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
+
+		if (evaluatePixelError)
+		{
+			cout << "evaluating pixel error" << endl;
+			preprocessor->ComputeRenderError();
+		}*/
 
 		qApp->exec();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp	(revision 2046)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/CombinedPreprocessor.cpp	(revision 2048)
@@ -74,4 +74,5 @@
   
   long startTime = GetTime();
+
   for (int i=0; i < mTotalSamples; i += mSamplesPerPass, mPass++) {
 	mRayCaster->InitPass();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 2048)
@@ -64,5 +64,6 @@
 		 BVH_INTERSECTABLE,
 		 TRIANGLE_INTERSECTABLE,
-		 OBJECTS_INTERSECTABLE
+		 OBJECTS_INTERSECTABLE,
+		 DUMMY_INTERSECTABLE
 		};
   
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 2005)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParserXerces.h	(revision 2048)
@@ -74,4 +74,7 @@
   void CreateViewCellsManager(/*const char *name*/);
 
+  void ExchangeElements();
+  void ExchangePvs(ViewCell *vc);
+
 
   /////////////////////////
@@ -94,6 +97,7 @@
   VspNode *mVspRoot;
   ViewCell *mViewCellRoot;
-
- // typedef map<int, ViewCell *> ViewCellsMap;
+int mUniqueObjectId;
+  vector<BvhLeaf *> mBvhLeaves;
+  //typedef map<int, ViewCell *> ViewCellsMap;
   //ViewCellsMap mViewCells;
   ViewCellContainer mViewCells;
@@ -119,6 +123,8 @@
   int mViewSpaceHierarchyType;
   int mObjectSpaceHierarchyType;
-int nViewCells;
-int nObjects;
+  int nViewCells;
+  int nObjects;
+  
+  
   ////////////////////////////////
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2023)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2048)
@@ -26,5 +26,6 @@
 class TriangleIntersectable;
  class BvhNode;
-  
+  class SimpleRayContainer;
+
 struct VssRayContainer;
 
@@ -152,4 +153,6 @@
   virtual void
   EvalPvsStat();
+
+  virtual void EvalPvsStat(const SimpleRayContainer &viewPoints);
 
   virtual void InitGL();
@@ -272,4 +275,5 @@
 	virtual void EvalPvsStat();
 
+	virtual void EvalPvsStat(const SimpleRayContainer &viewPoints);
 
 	virtual int GetWidth() const = 0;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2017)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2048)
@@ -25,4 +25,5 @@
 };
 
+
 static const float MIN_DIST = 0.001f;
 
@@ -37,5 +38,5 @@
 	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
 	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
-	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
+	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.gvsSamplesPerPass", mGvsSamplesPerPass);
 	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.minContribution", mMinContribution);
 	Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
@@ -44,4 +45,6 @@
 	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.maxViewCells", mMaxViewCells);
 
+	Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", mEvaluatePixelError);
+
 	char gvsStatsLog[100];
 	Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
@@ -51,5 +54,6 @@
 	Debug << "number of total samples: " << mTotalSamples << endl;
 	Debug << "number of initial samples: " << mInitialSamples << endl;
-	Debug << "number of samples per pass: " << mSamplesPerPass << endl;
+	cout << "number of gvs samples per pass: " << mGvsSamplesPerPass << endl;
+	cout << "number of samples per pass: " << mSamplesPerPass << endl;
 	Debug << "threshold: " << mThreshold << endl;
 	Debug << "epsilon: " << mEps << endl;
@@ -83,5 +87,5 @@
 		mRayQueue.pop();
 
-		delete ray;
+		//delete ray;
 	}
 }
@@ -141,8 +145,7 @@
 		if (!HandleRay(newRay))
 		{
-			delete newRay;
-		}
-		
-
+			//delete newRay;
+		}
+		
 		return 1;
 	}
@@ -286,5 +289,6 @@
 								   const VssRay &ray2,
 								   const VssRay &oldRay)
-{//cout <<"x"<<Magnitude(p1 - p2) << " ";
+{
+	//cout <<"y"<<Magnitude(p1 - p2) << " ";
 	int castRays = 0;
 
@@ -320,6 +324,5 @@
 
 	// this ray will not be further processed
-	if (!enqueued)
-		delete newRay;
+	//if (!enqueued) delete newRay;
 
 	return castRays;
@@ -339,5 +342,5 @@
 	else
 	{
-		cout << "not yet implemented" << endl;
+		cout << "border sampling: " << tObj->Type() << " not yet implemented" << endl;
 	}
 
@@ -427,5 +430,6 @@
 
 	CLEAR_CONTAINER(invalidSamples);
-//cout << "***********"<< endl;
+	
+	//cout << "cast rays: " << castRays << endl;
 	return castRays;
 }
@@ -495,8 +499,11 @@
 	// other types not implemented yet
 	if (tObj->Type() == Intersectable::TRIANGLE_INTERSECTABLE)
+	{
 		occluder = static_cast<TriangleIntersectable *>(tObj)->GetItem();
+	}
 	else
-		cout << "not yet implemented" << endl;
-	
+	{
+		cout << "reverse sampling: " << tObj->Type() << " not yet implemented" << endl;
+	}
 	// get a point which is passing just outside of the occluder
     Vector3 newPoint;
@@ -603,6 +610,5 @@
 	++ mGvsStats.mGvsPass;
 
-	while (!mRayQueue.empty() 
-		)//&& (mGvsStats.mTotalSamples + castSamples < mTotalSamples) )
+	while (!mRayQueue.empty())//&& (mGvsStats.mTotalSamples + castSamples < mTotalSamples) )
 	{
 		// handle next ray
@@ -614,6 +620,12 @@
 		castSamples += newSamples;
 
-		//cout << "new samples: " << newSamples << " " << "queue: "  << (int)mRayQueue.size() << endl;
-		delete ray;
+		//delete ray;
+	}
+
+	// HACK
+	if (mRayCaster->mVssRayPool.mIndex > mSamplesPerPass)
+	{
+		cout << "warning: new samples: " << castSamples << " " << "queue: "  << (int)mRayQueue.size() << endl;
+		Debug << "warning: new samples: " << castSamples << " " << "queue: "  << (int)mRayQueue.size() << endl;
 	}
 
@@ -753,4 +765,5 @@
 {
 	//Intersectable::NewMail();
+
 	// compute direct intersections with view cell
 	IntersectWithViewCell();
@@ -763,4 +776,6 @@
 	while (1) 
 	{
+		mRayCaster->InitPass();
+
 		// Ray queue empty => 
 		// cast a number of uniform samples to fill ray queue
@@ -773,9 +788,7 @@
 		mGvsStats.mPerViewCellSamples += newSamples;
 		
-
-		if (passSamples >= mSamplesPerPass)
+		if (passSamples >= mGvsSamplesPerPass)
 		{
 			++ mPass;
-
 			mGvsStats.mPassContribution = mGvsStats.mTotalContribution - oldContribution;
 
@@ -805,4 +818,18 @@
 void GvsPreprocessor::CompileViewCellsList()
 {
+	vector<ViewCellPoints *> *vcPoints = mViewCellsManager->GetViewCellPoints();
+
+	if (!vcPoints->empty())
+	{
+		vector<ViewCellPoints *>::const_iterator vit, vit_end = vcPoints->end();
+
+		for (vit = vcPoints->begin(); vit != vit_end; ++ vit)
+		{
+			mViewCells.push_back((*vit)->first);
+		}
+
+		return;
+	}
+
 	while ((int)mViewCells.size() < mMaxViewCells)
     {
@@ -886,8 +913,8 @@
 		(*oit)->mCounter = 0;
 	}
-	long lastTime = GetTime();
-
-	while (NextViewCell())
-	{
+//int dummy = 0;
+	while (NextViewCell())// && (dummy < 3))
+	{//++ dummy;
+		long startTime = GetTime();
 		cout << "\n***********************\n" 
 			 << "processing view cell " << mProcessedViewCells 
@@ -907,5 +934,6 @@
 		GetObjectPvs(objectPvs);
 
-		cout << "triangle pvs of " << (int)mTrianglePvs.size() << " was converted to object pvs of " << (int)objectPvs.size() << endl;
+		cout << "triangle pvs of " << (int)mTrianglePvs.size() 
+			 << " was converted to object pvs of " << (int)objectPvs.size() << endl;
 
 		////////
@@ -922,8 +950,8 @@
 		const long currentTime = GetTime();
         
-		mGvsStats.mTimePerViewCell = TimeDiff(lastTime, currentTime) * 1e-3f;
+		mGvsStats.mTimePerViewCell = TimeDiff(startTime, currentTime) * 1e-3f;
 		mGvsStats.mTotalTime += mGvsStats.mTimePerViewCell;
 
-		lastTime = currentTime;
+		//lastTime = currentTime;
 
 		mGvsStats.Stop();
@@ -940,5 +968,46 @@
 
 		// is this really necessary?
-		ClearRayQueue();
+		//ClearRayQueue();
+
+		if (mEvaluatePixelError || mExportVisibility)
+		{
+			StorePvs(objectPvs);
+		}
+	}
+}
+
+
+void GvsPreprocessor::StorePvs(const ObjectContainer &objectPvs)
+{
+	ObjectContainer::const_iterator oit, oit_end = objectPvs.end();
+
+	for (oit = objectPvs.begin(); oit != oit_end; ++ oit)
+	{
+		mCurrentViewCell->GetPvs().AddSample(*oit, 1);
+	}
+}
+
+
+void GvsPreprocessor::ComputeRenderError()
+{
+	cout << "computing gvs render error" << endl;
+	vector<ViewCellPoints *> *vcPoints = mViewCellsManager->GetViewCellPoints();
+
+	vector<ViewCellPoints *>::const_iterator vit, vit_end = vcPoints->end();
+
+	for (vit = vcPoints->begin(); vit != vit_end; ++ vit)
+	{
+		ViewCellPoints *vcPoints = *vit;
+		
+		renderer->EvalPvsStat(vcPoints->second);
+
+		mStats <<
+			"#ViewCell\n" << vcPoints->first->GetId() << endl <<
+			"#AvgPvsRenderError\n" <<renderer->mPvsStat.GetAvgError() << endl <<
+			"#AvgPixelError\n" << renderer->GetAvgPixelError() << endl <<
+			"#MaxPixelError\n" << renderer->GetMaxPixelError() << endl <<
+			"#MaxPvsRenderError\n" << renderer->mPvsStat.GetMaxError() << endl <<
+			"#ErrorFreeFrames\n" << renderer->mPvsStat.GetErrorFreeFrames() << endl <<
+			"#AvgRenderPvs\n" << renderer->mPvsStat.GetAvgPvs() << endl;
 	}
 }
@@ -1007,4 +1076,5 @@
 	while (mGvsStats.mTotalSamples < mTotalSamples) 
 	{
+		mRayCaster->InitPass();
 		// Ray queue empty => 
 		// cast a number of uniform samples to fill ray queue
@@ -1017,5 +1087,5 @@
 		mGvsStats.mTotalSamples += newSamples;
 
-		if (passSamples % (mSamplesPerPass + 1) == mSamplesPerPass)
+		if (passSamples % (mGvsSamplesPerPass + 1) == mGvsSamplesPerPass)
 		{
 			++ mPass;
@@ -1075,6 +1145,10 @@
 		// list of view cells to compute
 		CompileViewCellsList();
-
 		PerViewCellComputation();
+
+		if (mEvaluatePixelError)
+		{
+			ComputeRenderError();
+		}
 	}
 	else
@@ -1090,6 +1164,18 @@
 		CLEAR_CONTAINER(mVssRays);
 	}
+	
+	// export the preprocessed information to a file
+	if (0 && mExportVisibility)
+	{
+		ExportPreprocessedData(mVisibilityFileName);
+	}
 
 	return true;
+}
+
+
+void GvsPreprocessor::DeterminePvsObjects(VssRayContainer &rays)
+{
+	mViewCellsManager->DeterminePvsObjects(rays, true);
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 2017)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 2048)
@@ -64,5 +64,5 @@
 static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
 
-
+#define PVS_HACK 1
 
 inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
@@ -393,4 +393,68 @@
 
 
+void ViewCellsParseHandlers::ExchangePvs(ViewCell *vc)
+{
+	//cout << "exchanging pvs" << endl;
+
+	sort(mBvhLeaves.begin(), mBvhLeaves.end(), ilt);
+	ObjectPvs newPvs;
+
+	ObjectPvsIterator pit = vc->GetPvs().GetIterator();
+
+	BvhLeaf *dummyInst = new BvhLeaf(AxisAlignedBox3());
+
+	ObjectContainer oldIntersectables;
+	// output PVS of view cell
+	while (pit.HasMoreEntries())
+	{		
+		ObjectPvsEntry entry = pit.Next();
+
+		Intersectable *intersect = entry.mObject;
+		oldIntersectables.push_back(intersect);
+
+		DummyIntersectable *dummyIntersect = static_cast<DummyIntersectable *>(intersect);
+
+		const int objId = dummyIntersect->GetItem();	
+		dummyInst->SetId(objId);
+
+		vector<BvhLeaf *>::iterator oit =
+			lower_bound(mBvhLeaves.begin(), 
+						mBvhLeaves.end(), 
+						dummyInst, ilt);	
+
+		if ((oit != mBvhLeaves.end()) && ((*oit)->GetId() == objId))
+		{
+			// $$JB we should store a float a per object which corresponds
+			// to sumof pdfs, i.e. its relative visibility
+			// temporarily set to 1.0f
+			//cout << (*oit)->GetId() << " ";
+			
+			newPvs.AddSample(*oit, 1.0f);
+		}
+		else
+		{
+			Debug << "error: object with id " << objId << " does not exist" << endl;
+		}
+	}
+
+	//newPvs.SimpleSort();
+	delete dummyInst;
+	vc->SetPvs(newPvs);
+
+	CLEAR_CONTAINER(oldIntersectables);
+}
+
+
+void ViewCellsParseHandlers::ExchangeElements()
+{
+	ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
+
+	for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
+	{//cout << "j";
+		ExchangePvs(*vit);
+	}
+}
+
+
 void ViewCellsParseHandlers::StartViewCellHierarchyElement(const std::string &element,
 														   AttributeList& attributes)
@@ -500,4 +564,8 @@
 	for (it = objIndices.begin(); it != it_end; ++ it)
 	{
+#if PVS_HACK
+		//cout << "u";
+		pvs.AddSample(new DummyIntersectable(*it), 1);
+#else
 		const int objId = *it;	
 		dummyInst.SetId(objId);
@@ -513,5 +581,7 @@
 			// to sumof pdfs, i.e. its relative visibility
 			// temporarily set to 1.0f
-			pvs.AddSample(*oit, 1.0f);				
+			//cout << "r";
+
+			pvs.AddSample(*oit, 1.0f);
 		}
 		else
@@ -519,4 +589,5 @@
 			Debug << "error: object with id " << objId << " does not exist" << endl;
 		}
+#endif
 	}
 }
@@ -839,5 +910,5 @@
 		}
 		if (attrName == "pvs") 
-		{
+		{//cout <<"e";
 			StrX attrValue(attributes.getValue(i));
 			const char *ptr = attrValue.LocalForm();
@@ -846,5 +917,6 @@
 			// otherwise view cell is undefined
 
-			StartViewCellPvs(viewCell->GetPvs(), ptr); // assume that view cell comes before pvs
+			// hack: assume that view cell comes before pvs
+			StartViewCellPvs(viewCell->GetPvs(), ptr); 
 			//StartViewCellPvs(pvs, ptr);
 		}
@@ -1244,5 +1316,16 @@
 
 	leaf->mObjects = objects;
-	BvHierarchy::AssociateObjectsWithLeaf(leaf);	
+	BvHierarchy::AssociateObjectsWithLeaf(leaf);
+	
+	if (PVS_HACK)
+	{
+		if (1) // Temp matt: should HAVE right id
+		{
+			leaf->SetId((int)mBvhLeaves.size());
+		}
+		mBvhLeaves.push_back(leaf);	
+				
+		//cout << "i";
+	}
 }
 
@@ -1263,6 +1346,4 @@
 	}
 
-	//TODO: find objects and add them to pvs
-	// TODO: get view cell with specified id
 	MeshInstance dummyInst(NULL);
 
@@ -1424,5 +1505,9 @@
 #else
 	parser->parse(filename.c_str());
+
 #endif
+
+	if (PVS_HACK)
+		handler.ExchangeElements();
 
 	const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1999)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 2048)
@@ -188,5 +188,5 @@
 {
 public:
-	TriangleIntersectable(Triangle3 item):
+	TriangleIntersectable(const Triangle3 &item):
 	IntersectableWrapper<Triangle3>(item) {}
 
@@ -220,4 +220,16 @@
 
 
+class DummyIntersectable: public IntersectableWrapper<int>
+{
+public:
+  DummyIntersectable(const int item):
+	IntersectableWrapper<int>(item) {}
+
+	int Type() const
+	{
+		return Intersectable::DUMMY_INTERSECTABLE;
+	}
+};
+
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 2044)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 2048)
@@ -669,6 +669,5 @@
 				  } else
 					if (strcmp(curr, "mutation")==0) {
-					  // temp matt: still no mutationstrategy!
-					  mDistributions.push_back(new MutationBasedDistribution(mPreprocessor));
+					 mDistributions.push_back(new MutationBasedDistribution(mPreprocessor));
 					}
 	
