Index: trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 310)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 311)
@@ -12,5 +12,5 @@
 template<typename T>
 struct LtSample {
-    bool operator()(const T *a, const T *b) const
+    bool operator()(const T a, const T b) const
     {
 		return a < b;
@@ -24,15 +24,15 @@
   int mVisibleSamples;
   PvsData<T>() {}
-  PvsData<T>(const int samples):mVisibleSamples(samples) {}
+  PvsData<T>(const int samples): mVisibleSamples(samples) {}
 };
 
 template<typename T>
-class mypvs 
+class Pvs
 {
 public:
-    mypvs(): mSamples(0), mEntries() {}
+    Pvs(): mSamples(0), mEntries() {}
 	int mSamples;
 
-	//int Compress() {return 0;}
+	int Compress() {return 0;}
 	int GetSize() {return (int)mEntries.size();}
 
@@ -48,145 +48,58 @@
 
 template <typename T>
-PvsData<T> *mypvs<T>::Find(T sample)
+PvsData<T> *Pvs<T>::Find(T sample)
 {
-  /*std::map<T *, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
+  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
   if (i != mEntries.end()) {
     return &(*i).second;
-  } else*/
+  } else
     return NULL;
 }
 
 template <typename T>
-void mypvs<T>::GetData(const int index,
+void Pvs<T>::GetData(const int index,
 	       T &entry,
 	       PvsData<T> &data)
 {
-  /*PvsMap<typename T>::iterator i = mEntries.begin();
+  std::map<T, PvsData<T>, LtSample<T> >::iterator i = mEntries.begin();
   for (int k = 0; k != index && i != mEntries.end(); i++, k++);
 
   entry = (*i).first;
-  data = (*i).second;*/
+  data = (*i).second;
 }
 
-struct LtKdNode
+template <typename T>
+int Pvs<T>::AddSample(T sample)
 {
-  bool operator()(const KdNode *a,
-		  const KdNode *b) const
-  {
-    return a < b;
-  }
-  
+	int result;
+	PvsData<T> *data = Find(sample);
+
+	if (data) 
+	{
+		data->mVisibleSamples ++;
+		result = 0;
+	
+	} 
+	else 
+	{
+		mEntries[sample] = PvsData<T>(1);
+		result = 1;
+	}
+
+	return  result;
+}
+
+/** Class instantiating the Pvs template for kd tree nodes.
+*/
+class KdPvs: public Pvs<KdNode *>
+{
+	int Compress();
 };
 
-/** Superclass for all types of PVS data.
-*/
-class Pvs {
-public:
-	Pvs(): mSamples(0) {}
-	int mSamples;
+typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
+typedef PvsData<KdNode *> KdPvsData;
 
-	virtual int Compress() = 0;
-	virtual int GetSize() = 0;
-};
-
-struct KdPvsData {
-  int mVisibleSamples;
-  KdPvsData() {}
-  KdPvsData(const int samples):mVisibleSamples(samples) {}
-};
-
-typedef std::map<KdNode *, KdPvsData, LtKdNode> KdPvsMap;
-
-
-class KdPvs: public Pvs {
-public:
- 
-  KdPvsMap mEntries;
-  
-  KdPvs():mEntries() {}
-  
-  KdPvsData *Find(KdNode *node);
-  int AddNodeSample(KdNode *node);
-  int Compress() {return 0;}
-  int GetSize() { return (int)mEntries.size(); }
-  void GetData(const int index,
-	       KdNode *&node,
-	       KdPvsData &data
-	       );
-  
-};
-
-struct LtBspNode
-{
-  bool operator()(const BspNode *a,
-		  const BspNode *b) const
-  {
-	  return a < b;
-  }
-};
-
-struct BspPvsData {
-  int mVisibleSamples;
-  BspPvsData() {}
-  BspPvsData(const int samples): mVisibleSamples(samples) {}
-};
-
-typedef std::map<BspNode *, BspPvsData, LtBspNode> BspPvsMap;
-
-class BspPvs: public Pvs 
-{
-public:
-	BspPvs(): mEntries() {}
-
-	BspPvsMap mEntries;
- 
-	BspPvsData *Find(BspNode *node);
-	int AddNodeSample(BspNode *node);
-	
-	int Compress() {return 0;}
-	
-	int GetSize() {return (int)mEntries.size();}
-	
-	void GetData(const int index,
-				 BspNode *&node,
-				 BspPvsData &data);
-};
-
-
-struct LtIntersectable
-{
-  bool operator()(const Intersectable *a,
-		  const Intersectable *b) const
-  {
-	  return a < b;
-  }
-};
-
-struct ViewCellPvsData {
-  int mVisibleSamples;
-  ViewCellPvsData() {}
-  ViewCellPvsData(const int samples): mVisibleSamples(samples) {}
-};
-
-typedef std::map<Intersectable *, ViewCellPvsData, LtIntersectable> ViewCellPvsMap;
-
-class ViewCellPvs 
-{
-public:
-	ViewCellPvs(): mEntries() {}
-
-	ViewCellPvsMap mEntries;
- 
-	int Compress() {return 0;}
-	int GetSize() {return (int)mEntries.size();}
-
-	ViewCellPvsData *Find(Intersectable *obj);
-	int AddObject(Intersectable *obj);
-
-	void GetData(const int index, 
-				 Intersectable *&obj,
-				 ViewCellPvsData &data);
-
-};
+//typedef Pvs<KdNode *> KdPvs;
+typedef Pvs<Intersectable *> ViewCellPvs;
 
 #endif
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 310)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 311)
@@ -1150,12 +1150,12 @@
 		 "10");
 
-  RegisterOption("ViewCells.type",
+  RegisterOption("ViewCells.hierarchyType",
 		 optString,
-		 "-view_cells",
-		 "BSP");
+		 "-view_cells_hierarchy_type",
+		 "bspTree");
 
   RegisterOption("ViewCells.filename",
 		 optString,
-		 "-viewcells_filename=",
+		 "-view_cells_filename=",
 		 "atlanta_viewcells_large.x3d");
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 245)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 311)
@@ -797,5 +797,5 @@
 	    // $$ JB TEMPORARY solution -> should add object PVS or explictly computed
 	    // kd tree PVS
-	    if (leaf->mKdPvs.AddNodeSample(node))
+	    if (leaf->mKdPvs.AddSample(node))
 	      totalPvsSize++;
 	  }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 310)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 311)
@@ -263,5 +263,4 @@
 mStoreSplitPolys(false)
 {
-	mypvs<BspNode *> testpvs; testpvs.Find(mRoot);
 	Randomize(); // initialise random generator for heuristics
 }
@@ -1192,10 +1191,12 @@
 	float maxt = 1e6;
 	float mint = 0;
-
+	
 	Intersectable::NewMail();
 
 	// test with tree bounding box
 	if (!mBox.GetMinMaxT(ray, &mint, &maxt))
+	{
 		return 0;
+	}
 
 	if (mint < 0)
@@ -1255,5 +1256,4 @@
 			// find intersection with plane between ray origin and exit point
 			extp = splitPlane->FindIntersection(ray.GetLoc(), extp, &maxt);
-
 		} else // reached leaf => intersection with view cell
 		{
@@ -1261,5 +1261,5 @@
 			
 			//ray.mBspLeaves.push_back(leaf);
-			
+			//Debug << "adding view cell" << leaf->mViewCell;
 			if (!leaf->mViewCell->Mailed())
 			{
@@ -1268,5 +1268,5 @@
 				++ hits;
 			}
-
+			//Debug << "view cell added" << endl;
    			// get the next node from the stack
 			if (tStack.empty())
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 310)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 311)
@@ -47,4 +47,6 @@
 	static bool ParseEnvironment();
 
+	/** Ray set description of the rays passing through this node */
+	PassingRaySet mPassingRays;
 
 protected:
@@ -52,7 +54,4 @@
 	/// the potentially visible objects
 	ViewCellPvs mPvs;
-
-	/** Ray set description of the rays passing through this node */
-	PassingRaySet mPassingRays;
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h	(revision 245)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h	(revision 311)
@@ -162,5 +162,7 @@
   
   void AddPassingRay(const Ray &ray, const int contributions) {
+	  Debug << "here2" << endl;
     mPassingRays.AddRay(ray, contributions);
+	Debug << "here4" << endl;
   }
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 308)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 311)
@@ -510,5 +510,44 @@
 X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
 {
-	// TODO
+	stack<BspNode *> tStack;
+
+	tStack.push(tree.GetRoot());
+
+	bool fm = mUseForcedMaterial;
+	
+	mUseForcedMaterial = true;
+	
+	mForcedMaterial.mDiffuseColor.g = 1.0f;
+  	mForcedMaterial.mDiffuseColor.b = 1.0f;
+  
+	while (!tStack.empty()) 
+	{
+		BspNode *node = tStack.top();
+		tStack.pop();
+
+		if (node->IsLeaf()) 
+		{
+			ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
+     
+			// set the mesh material according to the ray density
+			if (vc->mPassingRays.mRays) 
+			{
+				float importance = vc->mPassingRays.mContributions/(float)vc->mPassingRays.mRays;
+
+				mForcedMaterial.mDiffuseColor.r = importance;
+				mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
+				ExportViewCell(vc);
+			}  
+		} else 
+		{
+			BspInterior *interior = (BspInterior *)node;
+			tStack.push(interior->GetFront());
+			tStack.push(interior->GetBack());
+		}
+	}
+  
+	// restore the state of forced material
+	mUseForcedMaterial = fm;
+
 	return true;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 258)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 311)
@@ -6,10 +6,12 @@
 Scene {
 
-#	filename ../data/glasgow/glasgow1.x3d
-filename ../data/vienna/vienna-buildings.x3d
+#	filename glasgow1.x3d
+#	filename vienna.x3d
+#	filename ../data/vienna/vienna-simple.x3d
+	filename ../data/vienna/vienna-buildings.x3d
+#	filename ../data/vienna/viewcells-25-sel.x3d
 #	filename ../data/atlanta/atlanta2.x3d
 #	filename ../data/soda/soda.dat
 #	filename ../data/soda/soda5.dat
-
 }
 
@@ -54,5 +56,54 @@
 
 Sampling {
-	totalSamples	50000000
-	samplesPerPass  20
+	totalSamples	1000000
+	samplesPerPass  5
 }
+
+ViewCells {
+	hierarchyType bspTree
+	#hierarchyType kdTree
+	#hierarchyType sceneDependent
+#	filename ../data/atlanta/atlanta_viewcells_large.x3d
+#	filename ../data/atlanta/atlanta_viewcells_large2.x3d
+	filename ../data/vienna/viewcells-25-sel.x3d
+#	filename ../data/vienna/viewcells-25.x3d
+#	filename ../data/vienna/viewcells-large-sel.x3d
+}
+
+BspTree {
+#       constructionMethod fromRays
+	constructionMethod fromViewCells
+#	constructionMethod fromSceneGeometry
+
+	# next polygon         = 1
+	# axis aligned         = 2
+	# least splits         = 4
+	# balanced polygons    = 8
+	# balanced view cells  = 16
+	# largest polygon area = 32
+	# vertical axis        = 64
+	
+	splitPlaneStrategy 66
+	# least splits + balanced polygons
+	#splitPlaneStrategy 12
+	
+	#axis aligned + vertical axis
+	#splitPlaneStrategy 66
+	
+	# axis aligned + balanced view cells
+	# splitPlaneStrategy 18
+	
+	# largest polygon area
+	#splitPlaneStrategy 32
+	
+	# axus aligned + balanced polygons
+	#splitPlaneStrategy 72
+	
+	maxCandidates 50
+	maxViewCells 999999
+	Termination {
+		maxPolysForAxisAligned 100
+		maxPolygons 0
+		maxDepth 200
+	}
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp	(revision 310)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp	(revision 311)
@@ -1,152 +1,12 @@
 #include <iostream>
+#include "Pvs.h"
+
 using namespace std;
-#include "Pvs.h"
-#include "Intersectable.h"
-
-
-/*
-int mypvs::AddSample(T *sample)
-{
-  int result;
-  
-  PvsData<typename T> *data = Find(sample);
-  if (data) {
-    data->mVisibleSamples++;
-    result = 0;
-  } else {
-    mEntries[node] = PvsData<typename T>(1);
-    result = 1;
-  }
-  return  result;
-}
 
 
 
-*/
-KdPvsData *
-KdPvs::Find(KdNode *node)
+int
+KdPvs::Compress()
 {
-  KdPvsMap::iterator i = mEntries.find(node);
-  if (i != mEntries.end()) {
-    //    cout<<node<<" "<<(*i).first<<" "<<(*i).second.mVisibleSamples<<endl;
-    return &(*i).second;
-  } else
-    return NULL;
+	return 0; // TODO
 }
-
-int
-KdPvs::AddNodeSample(KdNode *node)
-{
-  int result;
-  
-  KdPvsData *data = Find(node);
-  if (data) {
-    data->mVisibleSamples++;
-    result = 0;
-  } else {
-    mEntries[node] = KdPvsData(1);
-    result = 1;
-  }
-  return  result;
-}
-
-
-void
-KdPvs::GetData(const int index,
-	       KdNode *&node,
-	       KdPvsData &data
-	       )
-{
-  KdPvsMap::iterator i = mEntries.begin();
-  for (int k = 0; k!=index && i != mEntries.end(); i++, k++);
-  node = (*i).first;
-  data = (*i).second;
-}
-
-BspPvsData *BspPvs::Find(BspNode *node)
-{
-	BspPvsMap::iterator i = mEntries.find(node);
-	
-	if (i != mEntries.end()) 
-	{
-		//    cout<<node<<" "<<(*i).first<<" "<<(*i).second.mVisibleSamples<<endl;
-		return &(*i).second;
-	} 
-	else
-	  return NULL;
-}
-
-// TODO: Get common ancestor for bsp and kd tree (HierarchyNode)
-int BspPvs::AddNodeSample(BspNode *node)
-{
-  int result;
-  
-  BspPvsData *data = Find(node);
-
-  if (data) 
-  {
-	  data->mVisibleSamples ++;
-	  result = 0;
-  } 
-  else 
-  {
-	  mEntries[node] = BspPvsData(1);
-	  result = 1;
-  }
-
-  return  result;
-}
-
-
-void BspPvs::GetData(const int index, BspNode *&node, BspPvsData &data)
-{
-	BspPvsMap::iterator i = mEntries.begin();
-  
-	for (int k = 0; k!=index && i != mEntries.end(); i++, k++);
-
-	node = (*i).first;
-	data = (*i).second;
-}
-
-ViewCellPvsData *ViewCellPvs::Find(Intersectable *obj)
-{
-	ViewCellPvsMap::iterator i = mEntries.find(obj);
-	
-	if (i != mEntries.end()) 
-	{
-		//    cout<<node<<" "<<(*i).first<<" "<<(*i).second.mVisibleSamples<<endl;
-		return &(*i).second;
-	} 
-	else
-	  return NULL;
-}
-
-int ViewCellPvs::AddObject(Intersectable *obj)
-{
-	int result;
-  
-	ViewCellPvsData *data = Find(obj);
-
-	if (data) 
-	{
-		data->mVisibleSamples ++;
-		result = 0;
-	} 
-	else 
-	{
-		mEntries[obj] = ViewCellPvsData(1);
-		result = 1;
-	}
-
-	return  result;
-}
-
-void ViewCellPvs::GetData(const int index, Intersectable *&obj, ViewCellPvsData &data)
-{
-	ViewCellPvsMap::iterator i = mEntries.begin();
-  
-	for (int k = 0; k!=index && i != mEntries.end(); i++, k++);
-
-	obj = (*i).first;
-	data = (*i).second;
-}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 310)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 311)
@@ -49,9 +49,9 @@
   for (j=0; j < ray.leaves.size(); j++) {
     KdNode *node = GetNodeForPvs( ray.leaves[j] );
-    contributingSamples += object->mKdPvs.AddNodeSample(node);
-  }
-  
+    contributingSamples += object->mKdPvs.AddSample(node);
+  }
+   
   if (mPass > 10)
-    for (j=1; j < ray.leaves.size() - 1; j++) {
+    for (j=1; j < ((int)ray.leaves.size() - 1); j++) {
       ray.leaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
   }
@@ -65,18 +65,18 @@
 	int contributingSamples = 0;
 	int j;
-
-	if (mPass > 10)
-		for (j=1; j < ray.viewCells.size() - 1; j++) 
-		{
-			ray.viewCells[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
-		}
-
+ 
 	// add object to view cell
 	for (j=0; j < ray.viewCells.size(); j++) 
-	{
+	{ 
 		ViewCell *vc = ray.viewCells[j];
-		contributingSamples += vc->GetPvs().AddObject(obj);
+		contributingSamples += vc->GetPvs().AddSample(obj);
 	}
-
+ 
+	if (mPass > 1)
+		for (j=1; j < ((int)ray.viewCells.size() - 1); j++) 
+		{
+            ray.viewCells[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
+		}
+ 
 	return contributingSamples;
 }
@@ -109,11 +109,11 @@
 		// cast ray to BSP tree to get intersection with view cells
 		mBspTree->CastRay(ray);
-
-		sampleContributions += sampleContributions += AddObjectSamples(object, ray);
-
+		
+		sampleContributions += AddObjectSamples(object, ray);
+				
 		if (ray.intersections.size() > 0) // view cell found
 		{
-			if (ray.intersections.size()) 
-				sampleContributions += AddObjectSamples(ray.intersections[0].mObject, ray);
+			sampleContributions += 
+				AddObjectSamples(ray.intersections[0].mObject, ray);
 		}
 	}
@@ -325,5 +325,7 @@
 
 					SetupRay(ray, point, direction);
+					
 					sampleContributions = CastRay(object, ray);
+					
 				}
 			} else {
@@ -346,5 +348,5 @@
 				}
 			}
-			
+		
 			passSamples++;
 			
@@ -405,5 +407,6 @@
     exporter->SetExportRayDensity(true);
     exporter->ExportKdTree(*mKdTree);
-	exporter->ExportBspTree(*mBspTree);
+	if (mViewCellsType == BSP_VIEW_CELLS)	
+		exporter->ExportBspTree(*mBspTree);
 
     delete exporter;
@@ -432,28 +435,38 @@
       Exporter *exporter = Exporter::GetExporter(s);
       exporter->SetWireframe();
-      KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
+
+	  if (mViewCellsType == BSP_VIEW_CELLS)
+	  {
+
+	  }
+	  else
+	  {
+		  KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
+	  	  Intersectable::NewMail();
 		  
-      Intersectable::NewMail();
-		  
-      // avoid adding the object to the list
-      object->Mail();
-      ObjectContainer visibleObjects;
-      for (; i != object->mKdPvs.mEntries.end(); i++) {
-	KdNode *node = (*i).first;
-	exporter->ExportBox(mKdTree->GetBox(node));
-	mKdTree->CollectObjects(node, visibleObjects);
-      }
-      
-      exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0));
-      exporter->SetFilled();
-
-      for (int j = 0; j < visibleObjects.size(); j++)
-	exporter->ExportIntersectable(visibleObjects[j]);
+		  // avoid adding the object to the list
+		  object->Mail();
+		  ObjectContainer visibleObjects;
+
+		  for (; i != object->mKdPvs.mEntries.end(); i++) 
+		  {
+			  KdNode *node = (*i).first;
+			  exporter->ExportBox(mKdTree->GetBox(node));
+			  mKdTree->CollectObjects(node, visibleObjects);
+		  }
+
+		  exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0));
+		  exporter->SetFilled();
+
+		  for (int j = 0; j < visibleObjects.size(); j++)
+			  exporter->ExportIntersectable(visibleObjects[j]);
 	
-      Material m;
-      m.mDiffuseColor = RgbColor(1, 0, 0);
-      exporter->SetForcedMaterial(m);
-      exporter->ExportIntersectable(object);
-      
+
+		  Material m;
+	      m.mDiffuseColor = RgbColor(1, 0, 0);
+		  exporter->SetForcedMaterial(m);
+		  exporter->ExportIntersectable(object);
+	  }
+
       delete exporter;
     }
