Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 309)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 310)
@@ -14,9 +14,4 @@
 #	filename ../data/soda/soda.dat
 #	filename ../data/soda/soda5.dat
-#	viewcells ../data/atlanta/atlanta_viewcells_large.x3d
-#	viewcells ../data/atlanta/atlanta_viewcells_large2.x3d
-	viewcells ../data/vienna/viewcells-25-sel.x3d
-#	viewcells ../data/vienna/viewcells-25.x3d
-#	viewcells ../data/vienna/viewcells-large-sel.x3d
 }
 
@@ -63,10 +58,22 @@
 	totalSamples	1000000
 	samplesPerPass  5
-	viewCells    BSP
-	#viewCells    KD
 }
 
+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
@@ -93,7 +100,4 @@
 	#splitPlaneStrategy 72
 	
-#       constructionMethod rays
-	constructionMethod viewCells
-#	constructionMethod sceneGeometry
 	maxCandidates 50
 	maxViewCells 999999
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 308)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 310)
@@ -8,4 +8,64 @@
 class Ray;
 class Intersectable;
+
+
+template<typename T>
+struct LtSample {
+    bool operator()(const T *a, const T *b) const
+    {
+		return a < b;
+	} 
+};
+
+//typedef std::map<T *, PvsData<T>, LtSample<T>> PvsMap<T>;
+
+template<typename T> 
+struct PvsData {
+  int mVisibleSamples;
+  PvsData<T>() {}
+  PvsData<T>(const int samples):mVisibleSamples(samples) {}
+};
+
+template<typename T>
+class mypvs 
+{
+public:
+    mypvs(): mSamples(0), mEntries() {}
+	int mSamples;
+
+	//int Compress() {return 0;}
+	int GetSize() {return (int)mEntries.size();}
+
+	PvsData<T> *Find(T sample);
+	int AddSample(T sample);
+
+	void GetData(const int index,
+	       T &entry,
+	       PvsData<T> &data);
+	std::map<T, PvsData<T>, LtSample<T> > mEntries;
+};
+
+
+template <typename T>
+PvsData<T> *mypvs<T>::Find(T sample)
+{
+  /*std::map<T *, PvsData<T>, LtSample<T> >::iterator i = mEntries.find(sample);
+  if (i != mEntries.end()) {
+    return &(*i).second;
+  } else*/
+    return NULL;
+}
+
+template <typename T>
+void mypvs<T>::GetData(const int index,
+	       T &entry,
+	       PvsData<T> &data)
+{
+  /*PvsMap<typename T>::iterator i = mEntries.begin();
+  for (int k = 0; k != index && i != mEntries.end(); i++, k++);
+
+  entry = (*i).first;
+  data = (*i).second;*/
+}
 
 struct LtKdNode
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 308)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 310)
@@ -244,5 +244,6 @@
 };
 
-/** Implementation of the ViewCell BSP tree. */
+/** Implementation of the view cell BSP tree. 
+*/
 class BspTree 
 {
@@ -271,7 +272,4 @@
 	typedef std::stack<BspTraversalData> BspTraversalStack;
 
-	/// BSP tree construction type
-	enum {VIEW_CELLS, SCENE_GEOMETRY, RAYS};
-
 	/** Default constructor creating an empty tree.
 		@param viewCell view cell corresponding to unbounded space
@@ -335,5 +333,7 @@
 	int CastRay(Ray &ray);
 
-	//static bool displayDebug;
+	/// bsp tree construction types
+	enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_RAYS};
+
 protected:
 	
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 309)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 310)
@@ -1068,9 +1068,4 @@
 		 "atlanta2.x3d");
 
-  RegisterOption("Scene.viewcells",
-		 optString,
-		 "-viewcells_filename=",
-		 "atlanta_viewcells_large.x3d");
-
   RegisterOption("Unigraphics.meshGrouping",
 		 optInt,
@@ -1109,5 +1104,4 @@
 		 "0.1");
 
-
   RegisterOption("KdTree.sahUseFaces",
 		 optBool,
@@ -1151,9 +1145,4 @@
 		 "1000000");
 
-  RegisterOption("Sampling.viewCells",
-		 optString,
-		 "-view_cells",
-		 "BSP");
-
   RegisterOption("Sampling.samplesPerPass",
 		 optInt,
@@ -1161,4 +1150,20 @@
 		 "10");
 
+  RegisterOption("ViewCells.type",
+		 optString,
+		 "-view_cells",
+		 "BSP");
+
+  RegisterOption("ViewCells.filename",
+		 optString,
+		 "-viewcells_filename=",
+		 "atlanta_viewcells_large.x3d");
+
+
+  RegisterOption("BspTree.constructionMethod",
+	  optString,
+	  "-bsp_construction_method=",
+	  "fromViewCells");
+
   RegisterOption("BspTree.Termination.maxPolygons",
 		 optInt,
@@ -1180,9 +1185,4 @@
 		"-bsp_split_method=",
 		"leastSplits");
-
-  RegisterOption("BspTree.constructionMethod",
-	  optString,
-	  "-bsp_construction_method=",
-	  "viewCells");
 
   RegisterOption("BspTree.maxCandidates",
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 309)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 310)
@@ -17,5 +17,5 @@
 int BspTree::sMaxCandidates = 10;
 int BspTree::sSplitPlaneStrategy = NEXT_POLYGON; 
-int BspTree::sConstructionMethod = VIEW_CELLS;
+int BspTree::sConstructionMethod = FROM_INPUT_VIEW_CELLS;
 int BspTree::sTermMaxPolysForAxisAligned = 50;
 
@@ -263,4 +263,5 @@
 mStoreSplitPolys(false)
 {
+	mypvs<BspNode *> testpvs; testpvs.Find(mRoot);
 	Randomize(); // initialise random generator for heuristics
 }
@@ -1059,4 +1060,25 @@
 void BspTree::ParseEnvironment()
 {
+	//-- parse bsp cell tree construction method
+	char constructionMethodStr[60];
+	
+	environment->GetStringValue("BspTree.constructionMethod", constructionMethodStr);
+
+	sConstructionMethod = FROM_INPUT_VIEW_CELLS;
+	
+	if (strcmp(constructionMethodStr, "fromViewCells") == 0)
+		sConstructionMethod = FROM_INPUT_VIEW_CELLS;
+	else if (strcmp(constructionMethodStr, "fromSceneGeometry") == 0)
+		sConstructionMethod = FROM_SCENE_GEOMETRY;
+	else if (strcmp(constructionMethodStr, "fromRays") == 0)
+		sConstructionMethod = FROM_RAYS;
+	else 
+	{
+		cerr << "Wrong construction method " << constructionMethodStr << endl;
+		exit(1);
+    }
+
+	Debug << "Construction method: " << constructionMethodStr << endl;
+
 	environment->GetIntValue("BspTree.Termination.maxDepth", sTermMaxDepth);
 	environment->GetIntValue("BspTree.Termination.maxPolygons", sTermMaxPolygons);
@@ -1092,25 +1114,4 @@
 
 	Debug << endl;
-
-	//-- parse BSP tree construction method
-	char constructionMethodStr[60];
-	
-	environment->GetStringValue("BspTree.constructionMethod", constructionMethodStr);
-
-	sConstructionMethod = BspTree::VIEW_CELLS;
-	
-	if (strcmp(constructionMethodStr, "viewCells") == 0)
-		sConstructionMethod = BspTree::VIEW_CELLS;
-	else if (strcmp(constructionMethodStr, "sceneGeometry") == 0)
-		sConstructionMethod = BspTree::SCENE_GEOMETRY;
-	else if (strcmp(constructionMethodStr, "rays") == 0)
-		sConstructionMethod = BspTree::RAYS;
-	else 
-	{
-		cerr << "Wrong bsp construction method " << constructionMethodStr << endl;
-		exit(1);
-    }
-
-	Debug << "Construction method: " << constructionMethodStr << endl;
 }
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 308)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 310)
@@ -45,4 +45,7 @@
 	void AddPassingRay(const Ray &ray, const int contributions);	
 
+	static bool ParseEnvironment();
+
+
 protected:
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 309)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 310)
@@ -30,4 +30,39 @@
 }
 
+bool Preprocessor::ParseViewCellsOptions()
+{
+	// parse type of view cells
+	char viewCellsStr[64];
+	environment->GetStringValue("ViewCells.hierarchyType", viewCellsStr);
+
+	int vcType = BSP_VIEW_CELLS;
+  
+	if (strcmp(viewCellsStr, "bspTree") == 0)
+		vcType = BSP_VIEW_CELLS;
+	else if (strcmp(viewCellsStr, "kdTree") == 0)
+		vcType = KD_VIEW_CELLS;
+	else if (strcmp(viewCellsStr, "sceneDependent") == 0)
+		vcType = SCENE_DEPENDENT;
+	else
+	{
+		cerr<<"Wrong view cells type" << viewCellsStr << endl;
+		exit(1);
+	}
+
+	// decide about view cell subdivision type used for preprocessing
+	switch (vcType)
+	{
+	case BSP_VIEW_CELLS:
+	case KD_VIEW_CELLS:
+		mViewCellsType = vcType;
+		break;
+	case SCENE_DEPENDENT:
+		mViewCellsType = BSP_VIEW_CELLS; // TODO
+		break;
+	}
+
+	return true;
+}
+
 void Preprocessor::DeleteViewCells()
 {
@@ -108,13 +143,13 @@
 	switch (BspTree::sConstructionMethod)
 	{
-	case BspTree::VIEW_CELLS:
+	case BspTree::FROM_INPUT_VIEW_CELLS:
 		mBspTree->Construct(mViewCells);
 		break;
-	case BspTree::SCENE_GEOMETRY:
+	case BspTree::FROM_SCENE_GEOMETRY:
 		DeleteViewCells(); // we generate new view cells
 		mSceneGraph->CollectObjects(&objects);
 		mBspTree->Construct(objects, &mViewCells);
 		break;
-	case BspTree::RAYS:
+	case BspTree::FROM_RAYS:
 		DeleteViewCells(); // we generate new view cells	
 		mBspTree->Construct(rays, &mViewCells);
@@ -126,4 +161,5 @@
 	return true;
 }
+
 
 
@@ -169,5 +205,2 @@
   return false;
 }
-
-
-
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 309)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 310)
@@ -9,4 +9,5 @@
 #include "Exporter.h"
 #include "X3dExporter.h" // delete later
+#include "ViewCell.h"
 
 #define USE_EXE_PATH false
@@ -33,24 +34,26 @@
   p->BuildKdTree();
   p->KdTreeStatistics(cout);
-
+  
+  // parse view cell hierarchy options
+  p->ParseViewCellsOptions();
 
   if (p->mViewCellsType == Preprocessor::BSP_VIEW_CELLS)
   {
-	   // if BSP tree construction method needs predefined view cells
-	  if (BspTree::sConstructionMethod == BspTree::VIEW_CELLS)
+	  if (BspTree::sConstructionMethod == BspTree::FROM_INPUT_VIEW_CELLS)
 	  {
-		  environment->GetStringValue("Scene.viewcells", buff);
-		  
+		   // view cells input file name
+		  environment->GetStringValue("ViewCells.filename", buff);
 		  string vcFilename(buff);
-	  	 
+
 		  if (vcFilename != "")
-	          p->LoadViewCells(vcFilename);
+			  p->LoadViewCells(vcFilename);
 		  else
 			  p->GenerateViewCells();
 
-		   Debug << "Viewcells loaded / generated. Number of view cells: " << (int)p->mViewCells.size() << endl;
+		  Debug << "Viewcells loaded / generated. Number of view cells: " << (int)p->mViewCells.size() << endl;
 	  }
+	
+	  p->BuildBspTree();
 	  
-	  p->BuildBspTree();
 	  p->BspTreeStatistics(Debug);
 	  p->Export("vc_bsptree2.x3d", false, false, true);
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 309)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 310)
@@ -92,4 +92,8 @@
   void DeleteViewCells();
 
+  /** Parses the view cell options
+  */
+  bool ParseViewCellsOptions();
+
   /// scene graph loaded from file
   SceneGraph *mSceneGraph;
@@ -111,6 +115,8 @@
   ViewCell *mRootViewCell;
 
-  enum {BSP_VIEW_CELLS, KD_VIEW_CELLS};
+  /// view cell hierarchy types
+  enum {BSP_VIEW_CELLS, KD_VIEW_CELLS, SCENE_DEPENDENT};
 
+  /// type of the view cells
   int mViewCellsType;
 };
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp	(revision 308)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp	(revision 310)
@@ -4,4 +4,24 @@
 #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)
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 309)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 310)
@@ -14,19 +14,4 @@
   environment->GetIntValue("Sampling.totalSamples", mTotalSamples);
   
-  char viewCellsStr[64];
-  environment->GetStringValue("Sampling.viewCells", viewCellsStr);
-
-  mViewCellsType = BSP_VIEW_CELLS;
-
-  if (strcmp(viewCellsStr, "BSP") == 0)
-	  mViewCellsType = BSP_VIEW_CELLS;
-  else if (strcmp(viewCellsStr, "KD") == 0)
-      mViewCellsType= KD_VIEW_CELLS;
-  else 
-  {
-	  cerr<<"Wrong view cells type" << viewCellsStr << endl;
-	  exit(1);
-  }
-
   mKdPvsDepth = 100;
   mStats.open("stats.log");
@@ -117,5 +102,21 @@
 	int sampleContributions = 0;
 
-	if (mViewCellsType == KD_VIEW_CELLS)
+	if (mViewCellsType == BSP_VIEW_CELLS)
+	{
+		// cast ray to KD tree to find intersection with other objects
+		mKdTree->CastRay(ray);
+		
+		// cast ray to BSP tree to get intersection with view cells
+		mBspTree->CastRay(ray);
+
+		sampleContributions += sampleContributions += AddObjectSamples(object, ray);
+
+		if (ray.intersections.size() > 0) // view cell found
+		{
+			if (ray.intersections.size()) 
+				sampleContributions += AddObjectSamples(ray.intersections[0].mObject, ray);
+		}
+	}
+	else
 	{
 		mKdTree->CastRay(ray);
@@ -128,20 +129,6 @@
 			}
 		} 
-	} else	
-	{
-		mBspTree->CastRay(ray);
-
-		if (ray.viewCells.size() > 0) // view cell found
-		{
-			sampleContributions += sampleContributions += AddObjectSamples(object, ray);
+	} 	
 	
-			// cast ray to KD tree to find intersections with other objects
-			mKdTree->CastRay(ray);
-
-			if (ray.intersections.size()) 
-				sampleContributions += AddObjectSamples(ray.intersections[0].mObject, ray);
-		}
-	}
-
 	return sampleContributions;
 }
@@ -251,6 +238,5 @@
 		int passSamples = 0;
 		int index = 0;
-		Debug << "************************************" << endl;
-		
+				
 		for (i = 0; i < objects.size(); i++) {
 			KdNode *nodeToSample = NULL;
