Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1761)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 1764)
@@ -516,18 +516,4 @@
 	bool GetStoreKdPVs() const;
 
-	/** Adds a sample to the pvs of the specified view cell.
-		@param obj the hit object
-		@param hitPoint the point where the object is hit.
-		@param vc the sanpled view cell
-		@param pdf probability of the ray
-		@param contribution the relative pvs contribution
-		@returns true if the sample gives a contribution to the pvs.
-	*/
-	virtual bool AddSampleToPvs(Intersectable *obj, 
-								const Vector3 &hitPoint,
-								ViewCell *vc,
-								const float pdf, 
-								float &contribution) const;
-
 	/** Exports single view cells for visualization.
 		@param objects the scene objects
@@ -1121,10 +1107,4 @@
 									BoundingBoxConverter *bconverter);
 
-	bool AddSampleToPvs(Intersectable *obj, 
-						const Vector3 &hitPoint,
-						ViewCell *vc,
-						const float pdf, 
-						float &contribution) const;
-
 	void ExportSingleViewCells(const ObjectContainer &objects,
 							   const int maxViewCells,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 1418)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 1764)
@@ -11,4 +11,5 @@
 #include "Containers.h"
 #include "VssRay.h"
+#include "AxisAlignedBox3.h"
 
 namespace GtpVisibilityPreprocessor {
@@ -18,5 +19,4 @@
 class SceneGraphNode;
 class Ray;
-class AxisAlignedBox3;
 class Intersectable;
 class BspLeaf;
@@ -48,5 +48,6 @@
 				  mWireframe(false),
 				  mUseForcedMaterial(false),
-				  mExportRayDensity(false)
+				  mExportRayDensity(false),
+				  mClampToBox(false)
   {}
 
@@ -154,5 +155,5 @@
 	  const BvHierarchy &bvHierarchy, 
 	  const int maxPvs, 
-	  const AxisAlignedBox3 *box = NULL,
+	  AxisAlignedBox3 *box = NULL,
 	  const bool exportBoundingBoxes = true);
 
@@ -163,4 +164,7 @@
 
   virtual void ExportPolygon(Polygon3 *poly) = 0;
+
+  bool mClampToBox;
+  AxisAlignedBox3 mBoundingBox;
 };
  
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1750)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1764)
@@ -236,13 +236,5 @@
 	*/
 	void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
-	/** Adds a sample to the pvs of the specified view cell.
-	*/
-	bool AddSampleToPvs(
-		Intersectable *obj, 
-		const Vector3 &hitPoint,
-		ViewCell *vc,
-		const float pdf, 
-		float &contribution) const;
-
+	
 	/** Print out statistics.
 	*/
@@ -265,5 +257,5 @@
 	void ExportObjectSpaceHierarchy(Exporter *exporter, 
 									const ObjectContainer &objects,
-									const AxisAlignedBox3 *bbox,
+									AxisAlignedBox3 *bbox,
 									const bool exportBounds = true) const;
 
@@ -391,8 +383,4 @@
 							 const float renderCostPerStorage,
 							 const float vspOspRatio);
-
-	bool AddSampleToPvs(Intersectable *obj, 
-						const float pdf,
-						float &contribution) const;
 
 	/** Collect affected view space candidates.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1732)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1764)
@@ -38,4 +38,6 @@
 class KdTreeStatistics;
 
+
+#define WORK_WITH_VIEWCELLS 0
 
 /** View space partition statistics.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1763)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1764)
@@ -28,9 +28,10 @@
 #define ABS_CONTRIBUTION_WEIGHT 1.0f
 
-// warning: Should not count origin object for sampling because it disturbs heuristics
+// warning: Should not count origin object for sampling 
+// because it disturbs heuristics
 #define SAMPLE_ORIGIN_OBJECTS 0  // matt temp
 
 // $$JB HACK
-#define USE_KD_PVS 1
+#define USE_KD_PVS 0
 #define KD_PVS_AREA (1e-4f)
 
@@ -42,6 +43,6 @@
 // HACK
 const static bool SAMPLE_AFTER_SUBDIVISION = true;
-const static bool CLAMP_TO_BOX = false;
-//const static bool CLAMP_TO_BOX = true;
+//const static bool CLAMP_TO_BOX = false;
+const static bool CLAMP_TO_BOX = true;
 
 
@@ -118,59 +119,4 @@
 	Environment::GetSingleton()->GetStringValue("ViewCells.samplingType", buf);
 
-	
-	/*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)
@@ -956,18 +902,4 @@
 {
 	return mExportPvs;
-}
-
-
-bool ViewCellsManager::AddSampleToPvs(Intersectable *obj, 
-									  const Vector3 &hitPoint,
-									  ViewCell *vc,
-									  const float pdf, 
-									  float &contribution) const
-{
-	if (!obj)
-	  return false;
-	
-	// potentially visible objects
-	return vc->AddPvsSample(obj, pdf, contribution);
 }
 
@@ -2008,20 +1940,24 @@
 ViewCellsManager::AddSampleContributions(VssRay &ray)
 {
-  // assumes viewcells have been stored...
-  ViewCellContainer *viewcells = &ray.mViewCells;
-  ViewCellContainer::const_iterator it;
-
-  if (!ray.mTerminationObject)
-	return;
-  
-  Intersectable *obj = GetIntersectable(ray, true);
-  
-  for (it = viewcells->begin(); it != viewcells->end(); ++it) {
-	ViewCell *viewcell = *it;
-	if (viewcell->GetValid()) {
-	  // if ray not outside of view space
-	  viewcell->GetPvs().AddSample(obj, ray.mPdf);
-	}
-  }
+	// assumes viewcells have been stored...
+	ViewCellContainer *viewcells = &ray.mViewCells;
+	ViewCellContainer::const_iterator it;
+
+	if (!ray.mTerminationObject)
+		return;
+
+	Intersectable *obj = GetIntersectable(ray, true);
+
+	for (it = viewcells->begin(); it != viewcells->end(); ++it) 
+	{
+		ViewCell *viewcell = *it;
+		if (viewcell->GetValid()) 
+		{
+			Intersectable *entry = GetIntersectable(ray, true);
+			if (entry)    
+				// if ray not outside of view space
+				viewcell->GetPvs().AddSample(entry, ray.mPdf);
+		}
+	}
 }
 
@@ -2061,6 +1997,5 @@
 	
 	Intersectable *obj = GetIntersectable(ray, true);
-	
-	
+		
 	ViewCellContainer::const_iterator it = viewcells.begin();
 	
@@ -2068,6 +2003,6 @@
 	{
 		ViewCell *viewcell = *it;
-		
-		if (viewcell->GetValid()) // tests if view cell is in valid view space
+		// tests if view cell is in valid view space
+		if (viewcell->GetValid())
 		{
 			float contribution;
@@ -2113,10 +2048,10 @@
 				// if view point is valid, add new object to the pvs
 				if (ray.mTerminationObject)
-				{
+				{ 
 					viewcell->GetPvs().AddSample(obj, ray.mPdf);
 				}				
 #if SAMPLE_ORIGIN_OBJECTS
 				 if (ray.mOriginObject)
-				 {
+				 { 
 					 viewcell->GetPvs().AddSample(ray.mOriginObject, ray.mPdf);
 				 }
@@ -2593,4 +2528,5 @@
 	  {
 		  Intersectable *o = *noi;
+		 
 		  // $$ JB warning: pdfs are not correct at this point!	  
 		  nPvs.AddSample(o, Limits::Small);
@@ -2598,6 +2534,6 @@
 	  }
   }
-  //  cout<<"nPvs size = "<<nPvsSize<<endl;
-
+
+  // cout<<"nPvs size = "<<nPvsSize<<endl;
   pvs.MergeInPlace(nPvs);
 }
@@ -2850,6 +2786,4 @@
 	}
 
-	//cout << "recursivly updating pvs" << endl;
-
 	////////////////
 	//-- interior node => propagate pvs up the tree
@@ -2899,5 +2833,5 @@
 
 			if (!intersect->Mailed())
-			{
+			{ 
 				pvs.AddSample(intersect, entry.mData.mSumPdf);
 				intersect->Mail();
@@ -5504,51 +5438,49 @@
 	if (exporter)
 	{
+		if (CLAMP_TO_BOX)
+		{	
+			exporter->mClampToBox = true; 	
+		}
+
 		EvaluateViewCellsStats();
 
+		const long starttime = GetTime();
+		cout << "exporting final view cells (after initial construction + post process) ... ";
+
+		// matt: hack for clamping scene
+		AxisAlignedBox3 bbox = mViewSpaceBox;
+		bbox.Scale(scale);
+
+		if (1 && mExportRays)
+		{	
+			exporter->ExportRays(visRays, RgbColor(0, 1, 0));
+		}
+
+		mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, 
+													  CLAMP_TO_BOX ? &bbox : NULL, false);
+		
 		// hack color code (show pvs size)
 		const int savedColorCode = mColorCode;
 		mColorCode = 1; // export pvs
 
-		const long starttime = GetTime();
-		cout << "exporting final view cells (after initial construction + post process) ... ";
-
-		// matt: hack for clamping scene
-		AxisAlignedBox3 bbox = mViewSpaceBox;
-		bbox.Scale(scale);
-
+		//ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, mColorCode, GetClipPlane());
+		ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
+
+		delete exporter;
+
+		cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
+		mColorCode = savedColorCode;
+	}
+
+	// export final object partition
+	exporter = Exporter::GetExporter("final_object_partition.wrl");
+
+	if (exporter)
+	{
 		if (CLAMP_TO_BOX)
 		{	
-			exporter->SetWireframe(); 
-			exporter->ExportBox(bbox);
-			exporter->SetFilled(); 
-		}
-
-		if (0 && mExportGeometry)
-		{
-			exporter->ExportGeometry(objects, true, CLAMP_TO_BOX ? &bbox : NULL);
-		}
-
-		if (1 && mExportRays)
-		{	
-			exporter->ExportRays(visRays, RgbColor(0, 1, 0));
-		}
-
-		mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, 
-													  CLAMP_TO_BOX ? &bbox : NULL, false);
-		
-		//ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, mColorCode, GetClipPlane());
-		ExportViewCellsForViz(exporter, NULL, mColorCode, GetClipPlane());
-
-		delete exporter;
-
-		cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
-		mColorCode = savedColorCode;
-	}
-
-	// export final object partition
-	exporter = Exporter::GetExporter("final_object_partition.wrl");
-
-	if (exporter)
-	{
+			exporter->mClampToBox = true; 	
+		}
+
 		const long starttime = GetTime();
 
@@ -5904,20 +5836,20 @@
 			{
 				// todo: maybe not correct for kd node pvs
-			  
-			  if (viewcell->GetPvs().GetSampleContribution(terminationObj,
-														   ray.mPdf, 
-														   contribution))
+				if (viewcell->GetPvs().GetSampleContribution(
+						terminationObj, ray.mPdf, contribution))
 				{
-				  ++ ray.mPvsContribution;
+					++ ray.mPvsContribution;
 				}
-			  
-			  ray.mRelativePvsContribution += contribution;
+
+				ray.mRelativePvsContribution += contribution;
 			}
 			
 			////////////////
-			//-- for directional sampling it is important to count only contributions 
-			//-- made in one direction!
+			//-- for directional sampling it is important to count 
+			//-- only contributions made in one direction!
 			//-- the other contributions of this sample will be counted for the opposite ray!
+
 #if SAMPLE_ORIGIN_OBJECTS
+
 			if (originObj && 
 				viewcell->GetPvs().GetSampleContribution(originObj,
@@ -5946,40 +5878,13 @@
 
 		//$$JB hack
-#if !USE_KD_PVS
-		AddSampleToPvs(
-					   terminationObj, 
-					   ray.mTermination, 
-					   viewCell, 
-					   ray.mPdf, 
-					   ray.mRelativePvsContribution);
-#else
 		viewCell->GetPvs().AddSample(terminationObj, ray.mPdf);
-#endif
 		
 #if SAMPLE_ORIGIN_OBJECTS
-		AddSampleToPvs(
-					   originObj, 
-					   ray.mOrigin, 
-					   viewCell, 
-					   ray.mPdf, 
-					   ray.mRelativePvsContribution);
+		viewCell->GetPvs().AddSample(originObj, ray.mPdf);
 #endif			
 	}
 	
-	
-
 	return ABS_CONTRIBUTION_WEIGHT*ray.mPvsContribution +
 	  (1.0f - ABS_CONTRIBUTION_WEIGHT)*ray.mRelativePvsContribution;
-}
-
-
-bool VspOspViewCellsManager::AddSampleToPvs(Intersectable *obj, 
-											const Vector3 &hitPoint,
-											ViewCell *vc,
-											const float pdf, 
-											float &contribution) const
-{
-  // The hierarchy manager decides about the type of sample cast
-  return mHierarchyManager->AddSampleToPvs(obj, hitPoint, vc, pdf, contribution);
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1758)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1764)
@@ -106,5 +106,5 @@
 bool Exporter::ExportBvHierarchy(const BvHierarchy &bvHierarchy, 
 								 const int maxPvs,
-								 const AxisAlignedBox3 *box,
+								 AxisAlignedBox3 *box,
 								 const bool exportBoundingBoxes)
 {
@@ -149,5 +149,5 @@
 
 		SetFilled();
-		ExportGeometry(leaf->mObjects, true);
+		ExportGeometry(leaf->mObjects, true, box);
 	}
 
@@ -177,14 +177,20 @@
 	case Intersectable::TRIANGLE_INTERSECTABLE:
 		{
-		  const Triangle3 triangle = dynamic_cast<TriangleIntersectable *>(object)->GetItem();
-		  Polygon3 poly(triangle);
-		  ExportPolygon(&poly);
-		  break;
+			// if (mClampToBox && !Overlap(mBoundingBox, object->GetBox()))	 return;
+			const Triangle3 triangle = dynamic_cast<TriangleIntersectable *>(object)->GetItem();
+
+            Polygon3 poly(triangle);
+			ExportPolygon(&poly);
+			break;
 		}
 	case Intersectable::BVH_INTERSECTABLE:
 		{
 			BvhNode *node = dynamic_cast<BvhNode *>(object);
+			
 			if (node->IsLeaf())
+			{
 				ExportGeometry(dynamic_cast<BvhLeaf *>(node)->mObjects, true);
+			}
+
 			break;
 		}
@@ -247,5 +253,4 @@
 	}
 	
-
 	///////////////////////
 	//-- all objects exported as one mesh
@@ -260,8 +265,6 @@
 		if (bbox && !Overlap(*bbox, obj->GetBox()))
 		{
-			//cout << *bbox << " " << obj->GetBox() << endl;
 			continue;
 		}
-
 		switch (obj->Type())
 		{
@@ -293,7 +296,4 @@
 	}
 	
-	//cout << "describe:\n" << dummyMesh << endl;
-	//dummyMesh.CheckMesh();
-
 	ExportMesh(&dummyMesh);
 	CLEAR_CONTAINER(polys);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1763)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1764)
@@ -262,5 +262,5 @@
 							const float pdf, 
 							float &contribution)
-{
+{ cout << "here15" << endl;
 	const bool result = mPvs.AddSample(sample, pdf, contribution);
 	// have to recompute pvs size
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1757)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1764)
@@ -407,5 +407,4 @@
 void Pvs<T, S>::MergeInPlace(const Pvs<T, S> &a)
 {
-    //const int samples = mSamples + a.mSamples;
 	ObjectPvs interPvs;
 	
@@ -415,5 +414,4 @@
 	mEntries = interPvs.mEntries;
 	mSamples = interPvs.mSamples;
-	//mSamples = samples;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1763)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1764)
@@ -1033,32 +1033,23 @@
 
 		bool madeContrib = false;
-		float contribution;
-
-		Intersectable *obj = ray->mTerminationObject;
-
-		if (obj) 
+		float contribution = 1;
+
+		Intersectable *entry = 
+			mHierarchyManager->GetIntersectable(ray->mTerminationObject, true);
+
+		if (entry) 
 		{
 			madeContrib = 
-				mViewCellsManager->AddSampleToPvs(
-					obj, 
-					ray->mTermination, 
-					vc, 
-					ray->mPdf, 
-					contribution);
+				vc->GetPvs().AddSample(entry, ray->mPdf);
 
 			sc += contribution;
 		}
 #if COUNT_ORIGIN_OBJECTS
-		obj = ray->mOriginObject;
-
-		if (obj) 
+		entry = mHierarchyManager->GetIntersectable(ray->mOriginObject, true);
+
+		if (entry) 
 		{
 			madeContrib = 
-				mViewCellsManager->AddSampleToPvs(
-					obj,
-					ray->mOrigin, 
-					vc, 
-					ray->mPdf, 
-					contribution);
+				vc->GetPvs().AddSample(entry, ray->mPdf);
 
 			sc += contribution;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1763)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1764)
@@ -1428,37 +1428,4 @@
 
 
-bool HierarchyManager::AddSampleToPvs(Intersectable *obj, 
-									  const Vector3 &hitPoint,
-									  ViewCell *vc,
-									  const float pdf, 
-									  float &contribution) const
-{
-	if (!obj) return false;
-
-	switch (mObjectSpaceSubdivisionType)
-	{
-	case NO_OBJ_SUBDIV:
-		{
-			// potentially visible objects
-			return vc->AddPvsSample(obj, pdf, contribution);
-		}
-	case KD_BASED_OBJ_SUBDIV:
-		{
-			// potentially visible kd cells
-			KdLeaf *leaf = mOspTree->GetLeaf(hitPoint/*ray->mOriginNode*/);
-			return mOspTree->AddLeafToPvs(leaf, vc, pdf, contribution);
-		}
-	case BV_BASED_OBJ_SUBDIV:
-		{
-			BvhLeaf *leaf = mBvHierarchy->GetLeaf(obj);
-			
-			return vc->AddPvsSample(leaf, pdf, contribution);
-		}
-	default:
-		return false;
-	}
-}
-
-
 void HierarchyManager::PrintHierarchyStatistics(ostream &stream) const
 {
@@ -1488,5 +1455,5 @@
 void HierarchyManager::ExportObjectSpaceHierarchy(Exporter *exporter,
 												  const ObjectContainer &objects,
-												  const AxisAlignedBox3 *bbox,
+												  AxisAlignedBox3 *bbox,
 												  const bool exportBounds) const
 {
@@ -1570,4 +1537,5 @@
 		break;
 	}
+
 	return obj;
 }
@@ -1972,5 +1940,5 @@
 	mOldViewCells = viewCells;
 
-	return viewCells.size() + bvhNodes.size();
+	return (int)(viewCells.size() + bvhNodes.size());
 }
 
