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);
+}
+
+
+}
