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)
 {
