Index: GTP/trunk/Lib/Vis/Preprocessing/src/RandomViewCellsHandler.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RandomViewCellsHandler.h	(revision 2598)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RandomViewCellsHandler.h	(revision 2598)
@@ -0,0 +1,89 @@
+#ifndef _RandomViewCellsHandler_H__
+#define _RandomViewCellsHandler_H__
+
+#include "Containers.h"
+#include "SimpleRay.h"
+
+
+namespace GtpVisibilityPreprocessor {
+
+class ViewCell;
+class ViewCellsManager;
+
+typedef pair<ViewCell *, SimpleRayContainer> ViewCellPoints;
+typedef vector<ViewCellPoints *> ViewCellPointsList;
+
+
+/** This class functions define a system to generate, export and import a list
+	of random view cells and valid view points.
+*/
+
+class RandomViewCellsHandler
+{
+	//friend class ViewCellsManager;
+
+public:
+
+	RandomViewCellsHandler(ViewCellsManager *vcManager);
+	~RandomViewCellsHandler();
+
+	/** Exports list of random view cells.
+	*/
+	bool ExportRandomViewCells(const string &filename);
+	/** Returns the stored list of view cells and view points.
+	*/
+	ViewCellPointsList *GetViewCellPointsList();
+	/** Load list of view cells using the current list of view points.
+	*/
+	bool ImportViewCellsList(const string &filename);
+
+
+protected:
+
+	bool GenerateRandomViewCells(ViewCellContainer &viewCells, 
+								 const int numViewCells);
+
+	bool GenerateRandomViewCells(ViewCellPointsList &viewCells,
+								 const int nViewCells,
+								 const int nViewPoints);
+
+
+	/** Generates a list of random view points in a view cells.
+		@param viewCell the view cells
+		@param numViewPoints the number of view points
+		@param viewPoints returns view points in this container.
+	*/
+	bool GenerateViewPoints(ViewCell *viewCell, 
+							const int numViewPoints,
+							SimpleRayContainer &viewPoints);
+
+	/** Load list of view cells and view points.
+	*/
+	bool ImportViewCellsList(const string &filename, 
+							 vector<ViewCellPoints *> &viewCells);
+
+	/** Exports list of random view cells and valid view points.
+	*/
+	bool ExportRandomViewCells(const string &filename,
+							   const vector<ViewCellPoints *> &viewCells);
+
+	/** Generates a new valid random view point.
+	*/
+	bool GenerateViewPoint(ViewCell *viewCell, SimpleRay &ray);
+
+	/** Returns true if this view cell is in valid view space.
+	*/
+	bool IsValidViewSpace(ViewCell *vc);
+
+	
+protected:
+
+	ViewCellsManager *mViewCellsManager;
+
+	ViewCellPointsList mViewCellPointsList;
+};
+
+}
+
+
+#endif
Index: GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 2594)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/TestPreprocessor.vcproj	(revision 2598)
@@ -145,5 +145,5 @@
 				OptimizeForWindowsApplication="TRUE"
 				AdditionalIncludeDirectories="..\include;..\..\..\..\..\..\NonGTP\Boost;..\src;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces;&quot;$(QTDIR)\include\QtOpenGl&quot;;&quot;$(QTDIR)\include\Qt&quot;;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include&quot;;QtInterface;..\src\sparsehash\src\;..\src\ootl\src\include\ootl;..\src\ootl\src\include\ootl\sandbox;$(NOINHERIT)"
-				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;GTP_INTERNAL;USE_QT;USE_CG;USE_VERBOSE_PVS;QT_OPENGL_LIB;QT_DLL;TRV000"
+				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;GTP_INTERNAL;USE_QT;USE_CG;USE_VERBOSE_PVS;QT_OPENGL_LIB;QT_DLL"
 				StringPooling="TRUE"
 				MinimalRebuild="TRUE"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 2586)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 2598)
@@ -7,4 +7,6 @@
 #include "Containers.h"
 #include "ViewCell.h"
+#include "RandomViewCellsHandler.h"
+
 
 #define TEST_PACKETS 0
@@ -45,5 +47,5 @@
 struct BspRay;
 class BspTree;
-
+//class RandomViewCellsHandler;
 
 
@@ -77,5 +79,4 @@
 };
 
-typedef pair<ViewCell *, SimpleRayContainer> ViewCellPoints;
 
 struct VizBuffer
@@ -507,30 +508,13 @@
 	*/
 	int GetNumViewCells() const;
-
-	bool GenerateRandomViewCells(ViewCellContainer &viewCells, 
-								 const int numViewCells);
-
-	bool GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,
-								 const int nViewCells,
-								 const int nViewPoints);
-
-
-	bool GenerateViewPoints(ViewCell *viewCell, 
-							const int numViewPoints,
-							SimpleRayContainer &viewPoints);
-
-	bool ImportRandomViewCells(const string &filename, 
-							   vector<ViewCellPoints *> &viewCells);
-
-	bool ImportRandomViewCells(const string &filename);
-
-	bool ExportRandomViewCells(const string &filename,
-							   const vector<ViewCellPoints *> &viewCells);
-
+	/** Returns the stored list of view cells and view points.
+	*/
+	ViewCellPointsList *GetViewCellPointsList();
+	/** Exports list of random view cells.
+	*/
 	bool ExportRandomViewCells(const string &filename);
-
-	bool GenerateViewPoint(ViewCell *viewCell, SimpleRay &ray);
-
-	bool IsValidViewSpace(ViewCell *vc);
+	/** Imports list of random view cells and view points.
+	*/
+	bool ImportViewCellsList(const string &filename);
 
 
@@ -642,9 +626,4 @@
 	{
 		mPreprocessor = p;
-	}
-
-	vector<ViewCellPoints *> *GetViewCellPoints()
-	{
-		return &mViewCellPoints;
 	}
 
@@ -822,5 +801,6 @@
 	bool mCompressViewCells;
 
-	vector<ViewCellPoints *> mViewCellPoints;
+	/// Handler for generating and loading random view cells.
+	RandomViewCellsHandler *mRandomViewCellsHandler;
 
 	/// holds the current view cell statistics
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2593)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2598)
@@ -1483,9 +1483,9 @@
 	//	mutex.unlock();
 
-	if (mViewCellsManager->GetViewCellPoints()->size()) {
+	if (mViewCellsManager->GetViewCellPointsList()->size()) {
 	  
-	  vector<ViewCellPoints *> *vcPoints = mViewCellsManager->GetViewCellPoints();
+	  ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
 	  
-	  vector<ViewCellPoints *>::const_iterator
+	  ViewCellPointsList::const_iterator
 		vit = vcPoints->begin(),
 		vit_end = vcPoints->end();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 2594)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 2598)
@@ -126,5 +126,5 @@
 				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;;Timer;..\src\sparsehash\src\google\sparsehash;..\src\sparsehash\src\windows;..\src\sparsehash\src\google;..\src\sparsehash\src;..\src\ootl\;..\src\ootl\src\;..\src\ootl\src\cpp;..\src\ootl\src\cpp\include\;..\src\ootl\src\include\ootl;..\src\ootl\src\include\ootl\sandbox;..\src\ootl\src\cpp\include\ootl\mswin;..\src\havran"
-				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;GTP_INTERNAL;USE_QT;USE_VERBOSE_PVS;USE_CG;TRV000"
+				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;GTP_INTERNAL;USE_QT;USE_VERBOSE_PVS;USE_CG"
 				StringPooling="TRUE"
 				MinimalRebuild="TRUE"
@@ -684,4 +684,10 @@
 			</File>
 			<File
+				RelativePath=".\RandomViewCellsHandler.cpp">
+			</File>
+			<File
+				RelativePath=".\RandomViewCellsHandler.h">
+			</File>
+			<File
 				RelativePath="..\src\Ray.cpp">
 			</File>
@@ -1129,4 +1135,7 @@
 			</File>
 			<File
+				RelativePath=".\havran\ktbf2trv.cpp">
+			</File>
+			<File
 				RelativePath=".\havran\ktbftrav.cpp">
 			</File>
@@ -1148,4 +1157,7 @@
 			<File
 				RelativePath=".\havran\subdivm.h">
+			</File>
+			<File
+				RelativePath=".\havran\testrt.h">
 			</File>
 			<File
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RandomViewCellsHandler.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RandomViewCellsHandler.cpp	(revision 2598)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RandomViewCellsHandler.cpp	(revision 2598)
@@ -0,0 +1,354 @@
+#include "RandomViewCellsHandler.h"
+#include "ViewCellsManager.h"
+#include "ViewCellsParser.h"
+#include "Preprocessor.h"
+#include "GlRenderer.h"
+
+
+namespace GtpVisibilityPreprocessor {
+
+
+RandomViewCellsHandler::RandomViewCellsHandler(ViewCellsManager *vc): mViewCellsManager(vc)
+{
+}
+
+
+RandomViewCellsHandler::~RandomViewCellsHandler()
+{
+	CLEAR_CONTAINER(mViewCellPointsList);
+}
+
+
+bool RandomViewCellsHandler::ExportRandomViewCells(const string &filename,
+											 const vector<ViewCellPoints *> &viewCells)
+{
+	std::ofstream outStream;
+	outStream.open(filename.c_str());
+
+	vector<ViewCellPoints *>::const_iterator vit, vit_end = viewCells.end();
+    
+	for (vit = viewCells.begin(); vit != vit_end; ++ vit)
+	{
+		ViewCell *vc = (*vit)->first;
+
+		outStream << "v " << vc->GetId() << endl;
+
+		SimpleRayContainer viewPoints;
+	
+		SimpleRayContainer::const_iterator pit, pit_end = (*vit)->second.end();
+
+		for (pit = (*vit)->second.begin(); pit != pit_end; ++ pit)
+		{
+			const Vector3 pt = (*pit).mOrigin;
+			const Vector3 dir = (*pit).mDirection;
+
+			outStream << "p " << pt.x  << " " << pt.y  << " " << pt.z 
+				      << " "  << dir.x << " " << dir.y << " " << dir.z << endl;
+		}
+	}
+
+	return true;
+}
+
+
+bool RandomViewCellsHandler::GenerateRandomViewCells(ViewCellPointsList &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)mViewCellsManager->GetViewCells().size() - 0.5f);
+			
+		ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
+		
+		cout << "testing view cell: " << viewCell->GetId() << endl;
+
+		if (!viewCell->Mailed())
+		{
+			viewCell->Mail();
+
+			SimpleRayContainer viewPoints;
+			
+			// generate random view points
+			if (IsValidViewSpace(viewCell) && 
+				GenerateViewPoints(viewCell, nViewPoints, viewPoints))
+			{
+				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;
+	}
+
+	return true;
+}
+
+
+bool RandomViewCellsHandler::ImportViewCellsList(const string &filename, 
+										   vector<ViewCellPoints *> &viewCells)
+{
+	ifstream inStream(filename.c_str());
+	if (!inStream.is_open())
+		return false;
+
+	ViewCellPoints *currentViewCell = NULL;
+
+ 	string buf;
+	while (!(getline(inStream, buf)).eof())
+	{
+		switch (buf[0])
+		{
+		case 'v':
+			{
+				int id;
+				sscanf(buf.c_str(), "v %d", &id);
+			
+				currentViewCell = new ViewCellPoints();
+				currentViewCell->first = mViewCellsManager->GetViewCellById(id);
+						
+				viewCells.push_back(currentViewCell);
+				break;
+			}
+		case 'p':
+			{
+				Vector3 pt, dir;
+				sscanf(buf.c_str(), "p %f %f %f %f %f %f", &pt.x, &pt.y, &pt.z, &dir.x, &dir.y, &dir.z);
+				
+				SimpleRay ray(pt, dir, 0, 1);
+				currentViewCell->second.push_back(ray);
+				break;
+			}
+		default:
+			break;
+		}
+	}
+
+	return true;
+}
+
+
+bool RandomViewCellsHandler::ImportViewCellsList(const string &filename)
+{
+	return ImportViewCellsList(filename, mViewCellPointsList);
+}
+
+
+bool RandomViewCellsHandler::GenerateViewPoints(ViewCell *viewCell, 
+										  const int numViewPoints,
+										  SimpleRayContainer &viewPoints)
+{
+	bool success = true;
+	int generatedPts = 0;
+	int i = 0;
+
+	cout << "generating view points for view cell " << viewCell->GetId() << endl;
+
+	while (generatedPts < numViewPoints)
+	{
+		SimpleRay pt;
+
+		if (GenerateViewPoint(viewCell, pt))
+		{
+			++ generatedPts;
+			cout << "generated view point " << generatedPts << endl;
+			viewPoints.push_back(pt);
+		}
+
+		// savety criterium
+		if (++ i > numViewPoints * 3)
+		{
+			return false;
+		}
+	}
+	
+	cout << "view point generation finished" << endl;
+
+	return true;
+}
+
+
+bool RandomViewCellsHandler::ExportRandomViewCells(const string &filename)
+{
+	// export ten view cells with 100 random view points inside each
+	const int numViewCells = 100;
+	const int numViewPoints = 10;
+
+	GenerateRandomViewCells(mViewCellPointsList, numViewCells, numViewPoints);
+
+	//cout << "exporting random view cells" << endl;
+	return ExportRandomViewCells(filename,  mViewCellPointsList);
+}
+
+
+bool RandomViewCellsHandler::GenerateViewPoint(ViewCell *viewCell, 
+											   SimpleRay &ray)
+{
+	// do not use this function since it could return different 
+	// viewpoints for different executions of the algorithm
+
+	int tries = 0;
+	Vector3 viewPoint, direction;
+	const int maxTries = 10;
+
+	while (1) 
+	{
+		// hack
+		if (!viewCell->GetMesh())
+			mViewCellsManager->CreateMesh(viewCell);
+
+        Mesh *mesh = viewCell->GetMesh();
+		AxisAlignedBox3 box = mesh->mBox;
+		
+		Vector3 pVector = Vector3(Random(1.0f),
+								  Random(1.0f),
+								  Random(1.0f));
+
+		viewPoint =  box.GetPoint(pVector);
+	
+		const Vector3 dVector = Vector3(2 * M_PI * Random(1.0f), M_PI * Random(1.0f), 0.0f);
+		direction = Normalize(Vector3(sin(dVector.x), 0.0f, cos(dVector.x)));
+
+		ViewCell *v = mViewCellsManager->GetViewCell(viewPoint);
+
+		GlRendererBuffer *renderer = mViewCellsManager->GetPreprocessor()->GetRenderer();
+
+		if (v && v->GetValid())
+		{
+			renderer->SetViewPoint(viewPoint);
+			renderer->SetViewDirection(direction);
+		
+			if (renderer->ValidViewPoint())
+			{
+				cout << "view point valid " << viewPoint << " " << direction << endl;
+				break;
+			}
+			
+		}
+
+		if (++ tries > maxTries)
+		{
+			cerr << "error: no view point computed" << endl;
+			return false;
+		}
+	}
+
+	ray = SimpleRay(viewPoint, direction, 0, 1);
+	//cout << "view point generated: " << viewPoint << " " << direction << endl;
+
+	return true;
+}
+
+
+bool RandomViewCellsHandler::IsValidViewSpace(ViewCell *vc)
+{
+	SimpleRay simpleRay;
+	//check if view point can be generated
+	return GenerateViewPoint(vc, simpleRay);
+}
+
+
+bool RandomViewCellsHandler::GenerateRandomViewCells(ViewCellContainer &viewCells, 
+													 const int numViewCells)
+{
+	int generatedViewCells = 0;
+	//HaltonSequence halton;
+	//float r[1];
+
+	ViewCell::NewMail();
+
+	while (generatedViewCells < numViewCells)
+	{
+		// savety criterium
+		const int tries = 100000 + generatedViewCells;
+		int i = 0;
+
+		// generate next view cell
+		while (1)
+		{
+			//halton.GetNext(1, r);
+			//const int idx = (int)(r[0] * mViewCells.size() - 1.0f);
+			const int idx = (int)RandomValue(0.0f, (float)mViewCellsManager->GetViewCells().size() - 0.5f);
+			
+			ViewCell *viewCell = mViewCellsManager->GetViewCell(idx);
+
+			if (!viewCell->Mailed())
+			{
+				viewCell->Mail();
+
+				// check for valid view space
+				if (IsValidViewSpace(viewCell))
+				{
+					// valid view cell found
+					viewCells.push_back(viewCell);
+
+	                ++ generatedViewCells;
+					//cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;	
+					break;
+				}
+				else
+				{
+					cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;	
+				}
+			}
+
+			if (++ i == tries) // no new view cell fond
+			{
+				cerr << "big error! no view cell found" << endl;
+				return false;
+			}		
+		}			
+	}
+
+	return true;
+}
+
+
+ViewCellPointsList *RandomViewCellsHandler::GetViewCellPointsList()
+{
+	return &mViewCellPointsList;
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2593)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2598)
@@ -116,4 +116,6 @@
 	mSamplesStat.Reset();
 	//mStats.open("mystats.log");
+
+	mRandomViewCellsHandler = new RandomViewCellsHandler(this);
 }
 
@@ -157,4 +159,23 @@
 
 	return (int)passSamples.size();
+}
+
+
+ViewCellsManager::~ViewCellsManager()
+{
+	// HACK: if view cells tree does not 
+	// handle view cells, we have to do it here
+	// question: rather create view cells resource manager?
+	if (!ViewCellsTreeConstructed())
+	{
+		CLEAR_CONTAINER(mViewCells);
+	}
+	else
+	{
+		DEL_PTR(mViewCellsTree);
+	}
+
+	DEL_PTR(mMixtureDistribution);
+	DEL_PTR(mRandomViewCellsHandler);
 }
 
@@ -341,348 +362,4 @@
 	}
 	return NULL;
-}
-
-
-bool ViewCellsManager::ExportRandomViewCells(const string &filename,
-											 const vector<ViewCellPoints *> &viewCells)
-{
-	std::ofstream outStream;
-	outStream.open(filename.c_str());
-
-	vector<ViewCellPoints *>::const_iterator vit, vit_end = viewCells.end();
-    
-	for (vit = viewCells.begin(); vit != vit_end; ++ vit)
-	{
-		ViewCell *vc = (*vit)->first;
-
-		outStream << "v " << vc->GetId() << endl;
-
-		SimpleRayContainer viewPoints;
-	
-		SimpleRayContainer::const_iterator pit, pit_end = (*vit)->second.end();
-
-		for (pit = (*vit)->second.begin(); pit != pit_end; ++ pit)
-		{
-			const Vector3 pt = (*pit).mOrigin;
-			const Vector3 dir = (*pit).mDirection;
-
-			outStream << "p " << pt.x  << " " << pt.y  << " " << pt.z 
-				      << " "  << dir.x << " " << dir.y << " " << dir.z << endl;
-		}
-	}
-
-	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;
-			
-			// generate random view points
-			if (IsValidViewSpace(viewCell) && 
-				GenerateViewPoints(viewCell, nViewPoints, viewPoints))
-			{
-				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;
-	}
-
-	return true;
-}
-
-
-bool ViewCellsManager::ImportRandomViewCells(const string &filename, 
-											 vector<ViewCellPoints *> &viewCells)
-{
-	ifstream inStream(filename.c_str());
-	if (!inStream.is_open())
-		return false;
-
-	ViewCellPoints *currentViewCell = NULL;
-
- 	string buf;
-	while (!(getline(inStream, buf)).eof())
-	{
-		switch (buf[0])
-		{
-		case 'v':
-			{
-				int id;
-				sscanf(buf.c_str(), "v %d", &id);
-			
-				currentViewCell = new ViewCellPoints();
-				currentViewCell->first = GetViewCellById(id);
-						
-				viewCells.push_back(currentViewCell);
-				break;
-			}
-		case 'p':
-			{
-				Vector3 pt, dir;
-				sscanf(buf.c_str(), "p %f %f %f %f %f %f", &pt.x, &pt.y, &pt.z, &dir.x, &dir.y, &dir.z);
-				
-				SimpleRay ray(pt, dir, 0, 1);
-				currentViewCell->second.push_back(ray);
-				break;
-			}
-		default:
-			break;
-		}
-	}
-
-	return true;
-}
-
-
-bool ViewCellsManager::ImportRandomViewCells(const string &filename)
-{
-	return ImportRandomViewCells(filename, mViewCellPoints);
-}
-
-
-bool ViewCellsManager::GenerateViewPoints(ViewCell *viewCell, 
-										  const int numViewPoints,
-										  SimpleRayContainer &viewPoints)
-{
-	bool success = true;
-	int generatedPts = 0;
-	int i = 0;
-
-	cout << "generating view points for view cell " << viewCell->GetId() << endl;
-
-	while (generatedPts < numViewPoints)
-	{
-		SimpleRay pt;
-
-		if (GenerateViewPoint(viewCell, pt))
-		{
-			++ generatedPts;
-			cout << "generated view point " << generatedPts << endl;
-			viewPoints.push_back(pt);
-		}
-
-		// savety criterium
-		if (++ i > numViewPoints * 3)
-		{
-			return false;
-		}
-	}
-	
-	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);
-}
-
-
-bool ViewCellsManager::GenerateViewPoint(ViewCell *viewCell, 
-										 SimpleRay &ray)
-{
-	// do not use this function since it could return different 
-	// viewpoints for different executions of the algorithm
-
-	int tries = 0;
-	Vector3 viewPoint, direction;
-	const int maxTries = 10;
-
-	while (1) 
-	{
-		// hack
-		if (!viewCell->GetMesh())
-			CreateMesh(viewCell);
-
-        Mesh *mesh = viewCell->GetMesh();
-		AxisAlignedBox3 box = mesh->mBox;
-		
-		Vector3 pVector = Vector3(Random(1.0f),
-								  Random(1.0f),
-								  Random(1.0f));
-
-		viewPoint =  box.GetPoint(pVector);
-	
-		const Vector3 dVector = Vector3(2 * M_PI * Random(1.0f), M_PI * Random(1.0f), 0.0f);
-		direction = Normalize(Vector3(sin(dVector.x), 0.0f, cos(dVector.x)));
-
-		ViewCell *v = GetViewCell(viewPoint);
-
-		if (v && v->GetValid())
-		{
-			mPreprocessor->GetRenderer()->mViewPoint = viewPoint;
-			mPreprocessor->GetRenderer()->mViewDirection = direction;
-		
-			if (mPreprocessor->GetRenderer()->ValidViewPoint())
-			{
-				cout << "view point valid " << viewPoint << " " << direction << endl;
-				break;
-			}
-			
-		}
-
-		if (++ tries > maxTries)
-		{
-			cerr << "error: no view point computed" << endl;
-			return false;
-		}
-	}
-
-	ray = SimpleRay(viewPoint, direction, 0, 1);
-	//cout << "view point generated: " << viewPoint << " " << direction << endl;
-
-	return true;
-}
-
-
-bool ViewCellsManager::IsValidViewSpace(ViewCell *vc)
-{
-	SimpleRay simpleRay;
-	//check if view point can be generated
-	return GenerateViewPoint(vc, simpleRay);
-}
-
-
-bool ViewCellsManager::GenerateRandomViewCells(ViewCellContainer &viewCells, 
-											   const int numViewCells)
-{
-	int generatedViewCells = 0;
-	//HaltonSequence halton;
-	//float r[1];
-
-	ViewCell::NewMail();
-
-	while (generatedViewCells < numViewCells)
-	{
-		// savety criterium
-		const int tries = 100000 + generatedViewCells;
-		int i = 0;
-
-		// generate next view cell
-		while (1)
-		{
-			//halton.GetNext(1, r);
-			//const int idx = (int)(r[0] * mViewCells.size() - 1.0f);
-			const int idx = (int)RandomValue(0.0f, (float)mViewCells.size() - 0.5f);
-			
-			ViewCell *viewCell = GetViewCell(idx);
-
-			if (!viewCell->Mailed())
-			{
-				viewCell->Mail();
-
-				// check for valid view space
-				if (IsValidViewSpace(viewCell))
-				{
-					// valid view cell found
-					viewCells.push_back(viewCell);
-
-	                ++ generatedViewCells;
-					//cout << "view cell " << generatedViewCells << " generated: " << viewCell->GetId() << endl;	
-					break;
-				}
-				else
-				{
-					cout << "error: invalid view cell " << generatedViewCells << " with id " << viewCell->GetId() << endl;	
-				}
-			}
-
-			if (++ i == tries) // no new view cell fond
-			{
-				cerr << "big error! no view cell found" << endl;
-				return false;
-			}		
-		}			
-	}
-
-	return true;
-}
-
-
-ViewCellsManager::~ViewCellsManager()
-{
-	// HACK: if view cells tree does not 
-	// handle view cells, we have to do it here
-	// question: rather create view cells resource manager?
-	if (!ViewCellsTreeConstructed())
-	{
-		CLEAR_CONTAINER(mViewCells);
-	}
-	else
-	{
-		DEL_PTR(mViewCellsTree);
-	}
-
-	DEL_PTR(mMixtureDistribution);
-	CLEAR_CONTAINER(mViewCellPoints);
 }
 
@@ -7387,3 +7064,27 @@
 }
 
-}
+
+ViewCellPointsList *ViewCellsManager::GetViewCellPointsList()
+{
+	return mRandomViewCellsHandler->GetViewCellPointsList();
+}
+
+
+bool ViewCellsManager::ExportRandomViewCells(const string &filename)
+{
+	// export ten view cells with 100 random view points inside each
+	const int numViewCells = 100;
+	const int numViewPoints = 10;
+
+	//cout << "exporting random view cells" << endl;
+	return mRandomViewCellsHandler->ExportRandomViewCells(filename);
+}
+
+
+bool ViewCellsManager::ImportViewCellsList(const string &filename)
+{
+	return mRandomViewCellsHandler->ImportViewCellsList(filename);
+}
+
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2593)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2598)
@@ -103,6 +103,6 @@
 	mUseGlLists = false;
 
-	if (mViewCellsManager->GetViewCellPoints()->size())
-		mPvsStatFrames = (int)mViewCellsManager->GetViewCellPoints()->size();
+	if (mViewCellsManager->GetViewCellPointsList()->size())
+		mPvsStatFrames = (int)mViewCellsManager->GetViewCellPointsList()->size();
 	else
 		Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/FilterBasedDistribution.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/FilterBasedDistribution.cpp	(revision 2582)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/FilterBasedDistribution.cpp	(revision 2598)
@@ -68,5 +68,5 @@
   
   // pickup random object from the filtered pvs
-  int id = RandomValue(0, mFilteredPvs.GetSize()-1);
+  int id = (int)RandomValue(0, (Real)((float)mFilteredPvs.GetSize() - 0.5f));
   
   ObjectPvsIterator pit = mFilteredPvs.GetIterator();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2593)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2598)
@@ -298,5 +298,5 @@
 	{
 		cout << "importing random view cells" << endl;
-		preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
+		preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile);
 		cout << "finished" << endl;
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2593)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2598)
@@ -198,4 +198,7 @@
   }
 
+  void SetViewPoint(const Vector3 &vp) { mViewPoint = vp; }
+  void SetViewDirection(const Vector3 &vd) { mViewDirection = vd; }
+
 public:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2597)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2598)
@@ -794,5 +794,5 @@
 void GvsPreprocessor::CompileViewCellsList()
 {
-	vector<ViewCellPoints *> *vcPoints = mViewCellsManager->GetViewCellPoints();
+	ViewCellPointsList *vcPoints = mViewCellsManager->GetViewCellPointsList();
 
 	
