Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1740)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1742)
@@ -119,4 +119,58 @@
 
 	
+	ObjectPvs pvs1, pvs2, mergedPvs;
+
+	pvs1.AddSample((Intersectable *)1, 1);
+	pvs1.AddSample((Intersectable *)3, 1);
+	pvs1.AddSample((Intersectable *)6, 1);
+	pvs1.AddSample((Intersectable *)7, 1);
+	pvs1.AddSample((Intersectable *)9, 1);
+	pvs1.AddSample((Intersectable *)12, 1);
+	pvs1.AddSample((Intersectable *)15, 1);
+	pvs1.AddSample((Intersectable *)7, 1);
+	pvs2.AddSample((Intersectable *)3, 1);
+	pvs2.AddSample((Intersectable *)4, 1);
+	pvs2.AddSample((Intersectable *)6, 1);
+	pvs2.AddSample((Intersectable *)8, 1);
+	pvs2.AddSample((Intersectable *)9, 1);
+	pvs2.AddSample((Intersectable *)14, 1);
+
+	ObjectPvs::Merge(mergedPvs, pvs1, pvs2);
+
+	Debug << "\npvs1\n";
+	cout << "\npvs1\n";
+	ObjectPvsIterator pvs1It = pvs1.GetIterator();
+
+	while (pvs1It.HasMoreEntries())
+	{
+		ObjectPvsEntry entry = pvs1It.Next();
+		
+		Debug << (int)entry.mObject << " ";
+		cout << (int)entry.mObject << " ";
+	}
+
+Debug << "\npvs2\n";
+	cout << "\npvs2\n";
+	ObjectPvsIterator pvs2It = pvs2.GetIterator();
+
+	while (pvs2It.HasMoreEntries())
+	{
+		ObjectPvsEntry entry = pvs2It.Next();
+		
+		Debug << (int)entry.mObject << " ";
+		cout << (int)entry.mObject << " ";
+	}
+
+Debug << "\nmerged\n";
+	cout << "\nmerged\n";
+	ObjectPvsIterator mergedIt = mergedPvs.GetIterator();
+
+	while (mergedIt.HasMoreEntries())
+	{
+		ObjectPvsEntry entry = mergedIt.Next();
+		Debug << (int)entry.mObject << " " << entry.mData.mSumPdf << " x ";
+		cout << (int)entry.mObject << " " << entry.mData.mSumPdf << " x ";
+	}
+
 	// sampling type for view cells construction samples
 	if (strcmp(buf, "object") == 0)
@@ -2477,39 +2531,46 @@
   // extended boxes of visible objects
   Intersectable::NewMail();
+
+  ObjectPvsIterator pit = pvs.GetIterator();
+
+  while (pit.HasMoreEntries())
+  {		
+	  ObjectPvsEntry entry = pit.Next();
   
-  ObjectPvsEntries::const_iterator oi;
-  
-  for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++ oi) 
-	{
-	  Intersectable *object = (*oi).mObject;
+	  Intersectable *object = entry.mObject;
       object->Mail();
-	}
+  }
 
   ObjectPvs nPvs;
   int nPvsSize = 0;
-  // now go through the pvs again
-  for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++oi) {
-	Intersectable *object = (*oi).mObject;
-
-	//	Vector3 center = object->GetBox().Center();
-	//	AxisAlignedBox3 box(center - Vector3(spatialFilterSize/2),
-	//						center + Vector3(spatialFilterSize/2));
-
-	AxisAlignedBox3 box = object->GetBox();
-	box.Enlarge(Vector3(spatialFilterSize/2));
-	
-	ObjectContainer objects;
-	
-	// $$ warning collect objects takes only unmailed ones!
-	kdTree->CollectObjects(box, objects);
-	//	cout<<"collected objects="<<objects.size()<<endl;
-	ObjectContainer::const_iterator noi = objects.begin();
-	for (; noi != objects.end(); ++ noi) 
-	{
-		Intersectable *o = *noi;
-		// $$ JB warning: pdfs are not correct at this point!	  
-		nPvs.AddSample(o, Limits::Small);
-		nPvsSize ++;
-	}
+  
+  ObjectPvsIterator pit2 = pvs.GetIterator();
+
+  while (pit2.HasMoreEntries())
+  {		
+	  // now go through the pvs again
+	  ObjectPvsEntry entry = pit2.Next();
+	  Intersectable *object = entry.mObject;
+
+	  //	Vector3 center = object->GetBox().Center();
+	  //	AxisAlignedBox3 box(center - Vector3(spatialFilterSize/2),
+	  //						center + Vector3(spatialFilterSize/2));
+
+	  AxisAlignedBox3 box = object->GetBox();
+	  box.Enlarge(Vector3(spatialFilterSize/2));
+
+	  ObjectContainer objects;
+
+	  // $$ warning collect objects takes only unmailed ones!
+	  kdTree->CollectObjects(box, objects);
+	  //	cout<<"collected objects="<<objects.size()<<endl;
+	  ObjectContainer::const_iterator noi = objects.begin();
+	  for (; noi != objects.end(); ++ noi) 
+	  {
+		  Intersectable *o = *noi;
+		  // $$ JB warning: pdfs are not correct at this point!	  
+		  nPvs.AddSample(o, Limits::Small);
+		  nPvsSize ++;
+	  }
   }
   //  cout<<"nPvs size = "<<nPvsSize<<endl;
@@ -2531,9 +2592,14 @@
 
   Intersectable::NewMail();
-  ObjectPvsEntries::const_iterator oi;
+
+  ObjectPvsIterator pit = pvs.GetIterator();
+
   // first mark all object from this pvs
-  for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++ oi) {
-	Intersectable *object = (*oi).mObject;
-	object->Mail();
+  while (pit.HasMoreEntries())
+  {		
+	  ObjectPvsEntry entry = pit.Next();
+
+	  Intersectable *object = entry.mObject;
+	  object->Mail();
   }
 
@@ -2541,5 +2607,5 @@
   int pvsSize = 0;
   int nPvsSize = 0;
-  float samples = (float)pvs.mSamples;
+  float samples = (float)pvs.GetSamples();
   cout<<"Samples = "<<samples<<endl;
   //  cout<<"Filter size = "<<filterSize<<endl;
@@ -2554,6 +2620,11 @@
 
   float sumRadius = 0.0f;
-  for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++oi, pvsSize++) {
-	Intersectable *object = (*oi).mObject;
+
+  ObjectPvsIterator pit2 = pvs.GetIterator();
+
+  // first mark all object from this pvs
+   for (; pit2.HasMoreEntries(); pvsSize++) {
+  ObjectPvsEntry entry = pit2.Next();
+	Intersectable *object = entry.mObject;
 	// compute filter size based on the distance and the numebr of samples
 	AxisAlignedBox3 box = object->GetBox();
@@ -2562,5 +2633,5 @@
 	float globalRadius = distance*globalC;
 
-	int objectSamples = (int)(*oi).mData.mSumPdf;
+	int objectSamples = (int)entry.mData.mSumPdf;
 	float localRadius = MAX_FLOAT;
 	
@@ -2599,5 +2670,5 @@
 
 	// update samples and globalC
-	samples = (float)pvs.mSamples;
+	samples = (float)pvs.GetSamples();
 	globalC = 2.0f*filterSize/sqrt(samples);
 	cout<<"neighboring viewcells = "<<i-1<<endl;
@@ -2605,40 +2676,45 @@
   }
   
-  for (oi = pvs.mEntries.begin(); oi != pvs.mEntries.end(); ++oi) {
-	Intersectable *object = (*oi).mObject;
-	// compute filter size based on the distance and the numebr of samples
-	AxisAlignedBox3 box = object->GetBox();
-
-	float distance = Distance(center, box.Center());
-	float globalRadius = distance*globalC;
-
-	
-	int objectSamples = (int)(*oi).mData.mSumPdf;
-	float localRadius = MAX_FLOAT;
-	if (objectSamples > MIN_LOCAL_SAMPLES)
-	  localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/sqrt((float)objectSamples);
-	
-	//	cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl;
-	
-	// now compute the filter size
-	float radius = Min(localRadius, globalRadius);
-
-	// cout<<"box = "<<box<<endl;
-	//	cout<<"distance = "<<distance<<endl;
-	//	cout<<"radiues = "<<radius<<endl;
-	
-	box.Enlarge(Vector3(radius));
-
-	ObjectContainer objects;
-	// $$ warning collect objects takes only unmailed ones!
-	CollectObjects(box, objects);
-	//	cout<<"collected objects="<<objects.size()<<endl;
-	ObjectContainer::const_iterator noi = objects.begin();
-	for (; noi != objects.end(); ++ noi) {
-	  Intersectable *o = *noi;
-	  // $$ JB warning: pdfs are not correct at this point!	  
-	  nPvs.AddSample(o, Limits::Small);
-	  nPvsSize ++;
-	}
+  pit = pvs.GetIterator();
+
+  // first mark all object from this pvs
+  while (pit.HasMoreEntries())
+  {		
+	  ObjectPvsEntry entry = pit.Next();
+
+	  Intersectable *object = entry.mObject;
+	  // compute filter size based on the distance and the numebr of samples
+	  AxisAlignedBox3 box = object->GetBox();
+
+	  float distance = Distance(center, box.Center());
+	  float globalRadius = distance*globalC;
+
+	  int objectSamples = (int)entry.mData.mSumPdf;
+	  float localRadius = MAX_FLOAT;
+	  if (objectSamples > MIN_LOCAL_SAMPLES)
+		  localRadius = filterSize*0.5f*Magnitude(box.Diagonal())/sqrt((float)objectSamples);
+
+	  //	cout<<"lr="<<localRadius<<" gr="<<globalRadius<<endl;
+
+	  // now compute the filter size
+	  float radius = Min(localRadius, globalRadius);
+
+	  // cout<<"box = "<<box<<endl;
+	  //	cout<<"distance = "<<distance<<endl;
+	  //	cout<<"radiues = "<<radius<<endl;
+
+	  box.Enlarge(Vector3(radius));
+
+	  ObjectContainer objects;
+	  // $$ warning collect objects takes only unmailed ones!
+	  CollectObjects(box, objects);
+	  //	cout<<"collected objects="<<objects.size()<<endl;
+	  ObjectContainer::const_iterator noi = objects.begin();
+	  for (; noi != objects.end(); ++ noi) {
+		  Intersectable *o = *noi;
+		  // $$ JB warning: pdfs are not correct at this point!	  
+		  nPvs.AddSample(o, Limits::Small);
+		  nPvsSize ++;
+	  }
   }
   cout<<"nPvs size = "<<nPvsSize<<endl;
@@ -2786,13 +2862,16 @@
 	for (vit = interior->mChildren.begin(); vit != vit_end; ++ vit, ++ oit)
 	{
-	    ObjectPvsEntries::iterator pit, pit_end = (*oit).mEntries.end();
-	
-		for (pit = (*oit).mEntries.begin(); pit != pit_end; ++ pit)
-		{
-			Intersectable *intersect = (*pit).mObject;
+		ObjectPvsIterator pit = (*oit).GetIterator();
+		
+		// first mark all object from this pvs
+		while (pit.HasMoreEntries())
+		{		
+			ObjectPvsEntry entry = pit.Next();
+
+			Intersectable *intersect = entry.mObject;
 
 			if (!intersect->Mailed())
 			{
-				pvs.AddSample(intersect, (*pit).mData.mSumPdf);
+				pvs.AddSample(intersect, entry.mData.mSumPdf);
 				intersect->Mail();
 			}
@@ -3274,10 +3353,12 @@
 		{
 			Intersectable::NewMail();
-			ObjectPvsEntries::const_iterator oit, oit_end = pvs.mEntries.end();
-			
-			// output PVS of view cell
-			for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
+			ObjectPvsIterator pit = pvs.GetIterator();
+
+			while (pit.HasMoreEntries())
 			{		
-				Intersectable *intersect = (*oit).mObject;
+				ObjectPvsEntry entry = pit.Next();
+
+                // output PVS of view cell
+				Intersectable *intersect = entry.mObject;
 				
 				if (!intersect->Mailed())
@@ -3775,9 +3856,12 @@
 			exporter->SetFilled();
 
-			ObjectPvsEntries::iterator it, it_end = vc->GetPvs().mEntries.end();
-			// -- output PVS of view cell
-			for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
-			{
-				Intersectable *intersect = (*it).mObject;
+			ObjectPvsIterator pit = vc->GetPvs().GetIterator();
+			
+			while (pit.HasMoreEntries())
+			{		
+				ObjectPvsEntry entry = pit.Next();
+				
+				//-- output PVS of view cell
+				Intersectable *intersect = entry.mObject;
 				if (!intersect->Mailed())
 				{
@@ -4623,10 +4707,13 @@
 			Intersectable::NewMail();
 
-			ObjectPvsEntries::const_iterator oit, oit_end = pvs.mEntries.end();
+			ObjectPvsIterator pit = pvs.GetIterator();
+
 			cout << endl;
+
 			// output PVS of view cell
-			for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
-			{		
-				Intersectable *intersect = (*oit).mObject;
+			while (pit.HasMoreEntries())
+			{
+				ObjectPvsEntry entry = pit.Next();		
+				Intersectable *intersect = entry.mObject;
 				
 				if (!intersect->Mailed())
@@ -5490,10 +5577,14 @@
 
 			Intersectable::NewMail();
-			ObjectPvsEntries::const_iterator oit, oit_end = pvs.mEntries.end();
 			
+			ObjectPvsIterator pit = pvs.GetIterator();
+
 			// output PVS of view cell
-			for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
+			while (pit.HasMoreEntries())
 			{		
-				Intersectable *intersect = (*oit).mObject;
+				ObjectPvsEntry entry = pit.Next();
+				
+				Intersectable *intersect = entry.mObject;
+
 				if (!intersect->Mailed())
 				{
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1741)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1742)
@@ -61,10 +61,13 @@
 	int count = 0;
 
-	ObjectPvsEntries::const_iterator it, it_end = vc->GetPvs().mEntries.end();
-	for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it)
-	{
-		if (!(*it).mObject->Mailed())
-		{
-			(*it).mObject->Mail();
+	ObjectPvsIterator pit = vc->GetPvs().GetIterator();
+
+	while (pit.HasMoreEntries())
+	{
+		ObjectPvsEntry entry = pit.Next();
+
+		if (!entry.mObject->Mailed())
+		{
+			entry.mObject->Mail();
 			++ count;
 		}
@@ -76,25 +79,28 @@
 
 /// Fast computation of merged pvs size
-static float ComputeMergedPvsCost(const ObjectPvs &pvs1, const ObjectPvs &pvs2)
+static float ComputeMergedPvsCost(const ObjectPvs &pvs1, 
+								  const ObjectPvs &pvs2)
 {
 	// add first pvs
 	float pvs = (float)pvs1.GetSize();
 
-	ObjectPvsEntries::const_iterator it, it_end = pvs1.mEntries.end();
-
 	Intersectable::NewMail();
 
 	// mail all objects in first pvs
-	for (it = pvs1.mEntries.begin(); it != it_end; ++ it)
-	{
-		(*it).mObject->Mail();
-	}
-
-	it_end = pvs2.mEntries.end();
-
-	// look if the entries are also in second pvs
-	for (it = pvs2.mEntries.begin(); it != it_end; ++ it)
-	{
-		Intersectable *obj = (*it).mObject;
+	ObjectPvsIterator pit = pvs1.GetIterator();
+
+	while (pit.HasMoreEntries())
+	{
+		ObjectPvsEntry entry = pit.Next();
+		entry.mObject->Mail();
+	}
+
+	pit = pvs2.GetIterator();
+
+	while (pit.HasMoreEntries())
+	{
+		ObjectPvsEntry entry = pit.Next();
+
+		Intersectable *obj = entry.mObject;
 		if (!obj->Mailed())
 			++ pvs;
@@ -830,12 +836,14 @@
 
 	// compute new pvs size
-	ObjectPvsEntries::const_iterator it, it_end =  pvs1.mEntries.end();
-
 	Intersectable::NewMail();
 
+	ObjectPvsIterator pit = pvs1.GetIterator();
+
 	// first mail all objects in first pvs
-	for (it = pvs1.mEntries.begin(); it != it_end; ++ it)
-	{
-		Intersectable *obj = (*it).mObject;
+	while (pit.HasMoreEntries())
+	{
+		ObjectPvsEntry entry = pit.Next();
+
+		Intersectable *obj = entry.mObject;
 
 		obj->Mail();
@@ -843,10 +851,10 @@
 	}
 
-	it_end = pvs2.mEntries.end();
-
-	
-	for (it = pvs2.mEntries.begin(); it != it_end; ++ it)
-	{
-		Intersectable *obj = (*it).mObject;
+	ObjectPvsIterator pit2 = pvs2.GetIterator();
+
+	while (pit2.HasMoreEntries())
+	{
+		ObjectPvsEntry entry = pit2.Next();
+		Intersectable *obj = entry.mObject;
 
 		// test if object already considered	
@@ -1636,5 +1644,5 @@
 	}
 }
-	
+
 
 void ViewCellsTree::PullUpVisibility(ViewCellInterior *interior)
@@ -1643,6 +1651,4 @@
 
 	ViewCellContainer::const_iterator cit, cit_end = interior->mChildren.end();
-
-	ObjectPvsEntries::const_iterator oit;
 
 	// mail all objects in the leaf sets
@@ -1653,12 +1659,16 @@
 		ViewCell *vc = *cit;
 
-		ObjectPvsEntries::const_iterator oit_end = vc->GetPvs().mEntries.end();
-
-		for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			Intersectable *obj = (*oit).mObject;
+		ObjectPvsIterator pit = vc->GetPvs().GetIterator();
+
+		while (pit.HasMoreEntries())
+		{		
+			ObjectPvsEntry entry = pit.Next();
+			Intersectable *obj = entry.mObject;
+
 			if ((cit == interior->mChildren.begin()) && !obj->Mailed())
+			{
 				obj->Mail();
-			
+			}
+
 			int incm = obj->IncMail();
 		}
@@ -1666,6 +1676,5 @@
 
 	interior->GetPvs().Clear();
-	
-	
+		
 	// only the objects which are present in all leaf pvs 
 	// should remain in the parent pvs
@@ -1674,25 +1683,28 @@
 	{
 		ViewCell *vc = *cit;
-
-		ObjectPvsEntries::const_iterator oit_end = vc->GetPvs().mEntries.end();
-
-		for (oit = vc->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
+		
+		ObjectPvsIterator pit = vc->GetPvs().GetIterator();
+
+		while (pit.HasMoreEntries())
 		{		
-			if ((*oit).mObject->Mailed((int)interior->mChildren.size()))
+			ObjectPvsEntry entry = pit.Next();
+
+			if (entry.mObject->Mailed((int)interior->mChildren.size()))
 			{	
-				interior->GetPvs().AddSample((*oit).mObject, (*oit).mData.mSumPdf);
+				interior->GetPvs().AddSample(entry.mObject, entry.mData.mSumPdf);
 			}
 		}
 	}
 
-
 	// delete all the objects from the leaf sets which were moved to parent pvs
-	ObjectPvsEntries::const_iterator oit_end = interior->GetPvs().mEntries.end();
-
-	for (oit = interior->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-	{
+	ObjectPvsIterator pit = interior->GetPvs().GetIterator();
+
+	while (pit.HasMoreEntries())
+	{		
+		ObjectPvsEntry entry = pit.Next();
+
 		for (cit = interior->mChildren.begin(); cit != cit_end; ++ cit)
 		{
-			if (!(*cit)->GetPvs().RemoveSample((*oit).mObject, Limits::Infinity))
+			if (!(*cit)->GetPvs().RemoveSample(entry.mObject, Limits::Infinity))
 			{
 				Debug << "should not come here!" << endl;
@@ -1723,5 +1735,4 @@
 	{
 		root = root->GetParent();
-		cout << "h";
 		pvs.MergeInPlace(root->GetPvs());
 	}
@@ -1735,5 +1746,5 @@
 		vc = tstack.top();
 		tstack.pop();
-
+	
 		// add newly found pvs to merged pvs
 		ObjectPvs interPvs;
@@ -1783,14 +1794,15 @@
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
-		ObjectPvsEntries::iterator oit, oit_end = (*it)->GetPvs().mEntries.end();
-
-		for (oit = (*it)->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			Intersectable *intersect = (*oit).mObject;
+		ObjectPvsIterator oit = (*it)->GetPvs().GetIterator();
+
+		while (oit.HasMoreEntries())
+		{
+			ObjectPvsEntry entry = oit.Next();
+			Intersectable *intersect = entry.mObject;
 
 			if (!intersect->Mailed())
 			{
 				intersect->Mail();
-				newPvs.AddSample(intersect, (*oit).mData.mSumPdf);
+				newPvs.AddSample(intersect, entry.mData.mSumPdf);
 			}
 		}
@@ -1833,9 +1845,10 @@
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
-		ObjectPvsEntries::iterator oit, oit_end = (*it)->GetPvs().mEntries.end();
-
-		for (oit = (*it)->GetPvs().mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			Intersectable *intersect = (*oit).mObject;
+		ObjectPvsIterator oit = (*it)->GetPvs().GetIterator();
+
+		while (oit.HasMoreEntries())
+		{
+			ObjectPvsEntry entry = oit.Next();
+			Intersectable *intersect = entry.mObject;
 
 			if (!intersect->Mailed())
@@ -1975,6 +1988,6 @@
 	case PVS_IN_INTERIORS:
 	default:
-		// pvs is stored consistently in the tree up to the root
-		// just return pvs size 
+		// pvs is stored consistently in the tree up
+		//to the root just return pvs size 
 		pvsCost = vc->GetPvs().EvalPvsCost();	
 		break;
@@ -2350,9 +2363,11 @@
 void ViewCellsTree::ExportPvs(ViewCell *viewCell, OUT_STREAM &stream)
 {
-	ObjectPvsEntries::iterator it, it_end = viewCell->GetPvs().mEntries.end();
-
-	for (it = viewCell->GetPvs().mEntries.begin(); it != it_end; ++ it)
-	{
-		Intersectable *obj = (*it).mObject;
+	ObjectPvsIterator it = viewCell->GetPvs().GetIterator();
+
+	while (it.HasMoreEntries())
+	{
+		ObjectPvsEntry entry;
+		Intersectable *obj = entry.mObject;
+
 		// hack: just output full pvs
 		if (obj->Type() == Intersectable::BVH_INTERSECTABLE)
@@ -2370,5 +2385,5 @@
 		else
 		{
-			stream << (*it).mObject->GetId() << " ";
+			stream << entry.mObject->GetId() << " ";
 		}
 	}
@@ -2464,5 +2479,4 @@
 
 
-
 /**************************************************************************/
 /*                     MergeCandidate implementation                      */
@@ -2586,3 +2600,4 @@
 }
 
-}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1741)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1742)
@@ -121,10 +121,38 @@
 
 
-template<typename T, typename S>
+/*template<typename T, typename S>
 class PvsIterator
 {
+	Next(PvsEntry<T, S>)
 private:
 	typename vector<PvsEntry<T, S> >::iterator mItCurrent;
 	typename vector<PvsEntry<T, S> >::iterator mItEnd;
+};*/
+
+
+template<typename T, typename S>
+class PvsIterator
+{
+public:
+PvsIterator<T, S>(){}
+	PvsIterator<T, S>(const typename vector<PvsEntry<T, S> >::const_iterator &itCurrent,
+					  const typename vector<PvsEntry<T, S> >::const_iterator &itEnd):
+	mItCurrent(itCurrent), mItEnd(itEnd)
+	{
+	}
+
+	bool HasMoreEntries() const 
+	{
+		return (mItCurrent != mItEnd);
+	}
+
+	PvsEntry<T, S> Next()
+	{
+		return *(mItCurrent ++);
+	}
+	
+private:
+	typename vector<PvsEntry<T, S> >::const_iterator mItCurrent;
+	typename vector<PvsEntry<T, S> >::const_iterator mItEnd;
 };
 
@@ -143,6 +171,9 @@
 	Pvs(): mSamples(0), mEntries() {}
 
+	/** creates pvs and initializes it with the given entries. 
+		Assumes that entries are sorted-
+	*/
 	Pvs(const vector<PvsEntry<T, S> > &samples);
-	//virtual ~Pvs();
+	virtual ~Pvs() {};
 
 	/** Compresses PVS lossless or lossy.
@@ -185,5 +216,5 @@
 	float AddSample(T sample, const float pdf);
 	
-	/** Adds sample to PVS.
+	/** Adds sample to PVS. Assumes that the pvs is sorted
 		@returns contribution of sample (0 or 1)
 	*/
@@ -234,5 +265,12 @@
 	static void Merge(Pvs<T, S> &mergedPvs, const Pvs<T, S> &a, const Pvs<T, S> &b);
 
-//protected:
+	int GetSamples() const
+	{
+		return mSamples;
+	}
+
+	typename PvsIterator<T, S> GetIterator() const;
+
+protected:
 
 	/// vector of PVS entries
@@ -373,6 +411,4 @@
 	std::vector<PvsEntry<T, S> >::const_iterator ait = a.mEntries.begin(), ait_end = a.mEntries.end();
 	std::vector<PvsEntry<T, S> >::const_iterator bit = b.mEntries.begin(), bit_end = b.mEntries.end();
-	Intersectable::NewMail();
-	cout << "a: " << a.mEntries.size() << " b " << b.mEntries.size() << endl;
 	
 	for (; (ait != ait_end); ++ ait)
@@ -387,21 +423,13 @@
 
 			// object found => add up probabilities
-			if (bObj != aEntry.mObject)
+			if (bObj == aEntry.mObject)
 			{
 				PvsData newData(aEntry.mData.mSumPdf + (*bit).mData.mSumPdf);
 				PvsEntry<T, S> entry(bObj, newData);
 				mergedPvs.mEntries.push_back(entry);
-				if (bObj->Mailed())
-					cout << "Error2 " << bObj << endl;
-				bObj->Mail();
-				cout << "e: " << (int)bObj;
 			}
 			else
 			{
-				if (bObj->Mailed())
-					cout << "Error3 " << bObj << endl;
-				bObj->Mail();
 				mergedPvs.mEntries.push_back(*bit);
-				cout << "e: " << (int)bObj;
 			}
 		}
@@ -412,8 +440,4 @@
 		{
 			mergedPvs.mEntries.push_back(*ait);
-			if (aObj->Mailed())
-				cout << "Error1 " << aObj << endl;
-			aObj->Mail();
-			cout << "e: " << (int)aObj;
 		}
 	}
@@ -422,64 +446,6 @@
 	for (; (bit != bit_end); ++ bit)
 	{
-		Intersectable *bObj = (*bit).mObject;
-		
 		mergedPvs.mEntries.push_back(*bit);
-		if (bObj->Mailed())
-			cout << "Error4 " << bObj << endl;
-		bObj->Mail();
-		cout << "e: " << (int)bObj;
-	}
-
-/*
-	while ((ait != ait_end) || (bit != bit_end))
-	{cout << "x";
-
-		// first iterate through first vector until we find the objects
-		for (; (ait != ait_end) && ((bit == bit_end) || ((*ait).mObject <= (*bit).mObject)); ++ ait)
-		{
-			Intersectable *obj = (*ait).mObject;
-			aEntry = (*ait);
-			//bObject = (bit != bit_end) ? (*bit).mObject : NULL;
-
-			// only push back if objects different 
-			// (equal case is handled by second loop)
-			if (obj != bEntry.mObject)
-			{
-				mergedPvs.mEntries.push_back(*ait);
-				if (obj->Mailed())
-					cout << "Error1 " << obj << endl;
-				obj->Mail();
-				cout << "e: " << obj;
-			}
-		}
-
-		// iterate through second pvs
-		for (; (bit != bit_end) && ((ait == ait_end) || ((*bit).mObject <= (*ait).mObject)); ++ bit)
-		{
-			Intersectable *obj = (*bit).mObject;
-			bEntry = (*bit);
-			//aObject = (ait != ait_end) ? (*ait).mObject : NULL;
-
-			// object found => add up probabilities
-			if (obj != aEntry.mObject)
-			{
-				PvsData newData(aEntry.mData.mSumPdf + bEntry.mData.mSumPdf);
-				PvsEntry<T, S> entry(obj, newData);
-				mergedPvs.mEntries.push_back(entry);
-				if (obj->Mailed())
-					cout << "Error2 " << obj << endl;
-				obj->Mail();
-				cout << "e: " << obj;
-			}
-			else
-			{
-				if (obj->Mailed())
-					cout << "Error3 " << obj << endl;
-				obj->Mail();
-				mergedPvs.mEntries.push_back(*bit);
-				cout << "e: " << obj;
-			}
-		}
-	}*/
+	}
 }
 
@@ -545,5 +511,4 @@
 		S &data = (*it).mData;
 		data.mSumPdf += pdf;
-cout << "w";
 		return data.mSumPdf;
 	}
@@ -551,5 +516,4 @@
 	{
 		PvsEntry<T, S> entry(sample, pdf);
-		cout << "i";
 		mEntries.insert(it, entry);
 		return pdf;
@@ -564,5 +528,5 @@
 	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
 
-	if ((it != mEntries.end()) && ((*it).first == T))
+	if ((it != mEntries.end()) && ((*it).mObject == sample))
 	{
 		S &data = (*it).second;
@@ -732,4 +696,13 @@
 
 
+template <typename T, typename S>
+typename PvsIterator<T, S> Pvs<T, S>::GetIterator() const
+{
+	PvsIterator<T, S> pit(mEntries.begin(), mEntries.end());
+
+	return pit;
+}
+
+
 ///////////////////////////////////////
 
@@ -757,7 +730,8 @@
 //-- typedefs
 
-typedef std::vector<PvsEntry<Intersectable *, PvsData> > ObjectPvsEntries;
+typedef PvsEntry<Intersectable *, PvsData> ObjectPvsEntry;
+typedef std::vector<ObjectPvsEntry> ObjectPvsEntries;
 typedef Pvs<ViewCell *, MailablePvsData> ViewCellPvs;
-
+typedef PvsIterator<Intersectable *, PvsData> ObjectPvsIterator;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1741)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1742)
@@ -1841,9 +1841,11 @@
 
 		// hack: should not be done here
-		ObjectPvsEntries::const_iterator oit, oit_end = pvs.mEntries.end();
-
-		for (oit = pvs.mEntries.begin(); oit != oit_end; ++ oit)
-		{
-			BvhIntersectable *intersect = dynamic_cast<BvhIntersectable *>((*oit).mObject);
+		ObjectPvsIterator pit = pvs.GetIterator();
+
+		while (pit.HasMoreEntries())
+		{
+			ObjectPvsEntry entry = pit.Next();
+
+			BvhIntersectable *intersect = dynamic_cast<BvhIntersectable *>(entry.mObject);
 
 			BvhLeaf *leaf = intersect->GetItem();
