Index: trunk/VUT/GtpVisibilityPreprocessor/include/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/include/Preprocessor.h	(revision 261)
+++ trunk/VUT/GtpVisibilityPreprocessor/include/Preprocessor.h	(revision 439)
@@ -27,12 +27,4 @@
     */
     virtual bool LoadScene(const string filename);
-
-    /** Load the input viewcells. The input viewcells should be given as a collection
-	of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of
-	the viewcell. The method then builds a BSP tree of these view cells.
-	@param filename file to load
-	@return true on success
-    */
-    virtual bool LoadViewCells(const string filename);
 
     /** Generate the viewCells automatically. The particular algorithm to be used depends
Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 437)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 439)
@@ -300,4 +300,10 @@
 			</File>
 			<File
+				RelativePath="..\src\ViewCellsManager.cpp">
+			</File>
+			<File
+				RelativePath="..\src\ViewCellsManager.h">
+			</File>
+			<File
 				RelativePath="..\src\VspBspTree.cpp">
 			</File>
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 430)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 439)
@@ -4,6 +4,4 @@
 #include "MeshKdTree.h"
 #include "Triangle3.h"
-
-ViewCell::HierarchyType ViewCell::sHierarchy = BSP;
 
 ViewCell::ViewCell(): MeshInstance(NULL), mPiercingRays(0)
@@ -13,18 +11,4 @@
 ViewCell::ViewCell(Mesh *mesh): MeshInstance(mesh), mPiercingRays(0)
 {
-}
-
-ViewCell *ViewCell::Generate(Mesh *mesh)
-{
-	switch(sHierarchy)
-	{
-	case KD:
-		return new ViewCell(mesh);
-	case BSP:
-		return new BspViewCell(mesh);
-	default:
-		Debug << "should not come here 3" << endl;
-		return NULL;
-	}
 }
 
@@ -44,79 +28,4 @@
 }
 
-void ViewCell::DeriveViewCells(const ObjectContainer &objects, 
-							   ViewCellContainer &viewCells, 
-							   const int maxViewCells)
-{
-	// maximal max viewcells
-	int limit = maxViewCells > 0 ? 
-		Min((int)objects.size(), maxViewCells) : (int)objects.size();
-
-	for (int i = 0; i < limit; ++ i)
-	{
-		Intersectable *object = objects[i];
-		
-		// extract the mesh instances
-		if (object->Type() == Intersectable::MESH_INSTANCE)
-		{
-			MeshInstance *inst = dynamic_cast<MeshInstance *>(object);
-
-			ViewCell *viewCell = Generate(inst->GetMesh());
-			viewCells.push_back(viewCell);
-		}
-		//TODO: transformed meshes
-	}
-}
-
-ViewCell *ViewCell::ExtrudeViewCell(const Triangle3 &baseTri, const float height)
-{
-	// one mesh per view cell
-	Mesh *mesh = new Mesh();
-	
-	//-- construct prism
-
-	// bottom 
-	mesh->mFaces.push_back(new Face(2,1,0));
-	// top
-    mesh->mFaces.push_back(new Face(3,4,5));
-	// sides
-	mesh->mFaces.push_back(new Face(1, 4, 3, 0));
-	mesh->mFaces.push_back(new Face(2, 5, 4, 1));
-	mesh->mFaces.push_back(new Face(3, 5, 2, 0));
-
-	//--- extrude new vertices for top of prism
-	Vector3 triNorm = baseTri.GetNormal();
-
-	Triangle3 topTri;	
-
-	// add base vertices and calculate top vertices
-	for (int i = 0; i < 3; ++ i)
-		mesh->mVertices.push_back(baseTri.mVertices[i]);
-	
-	// add top vertices	
-	for (int i = 0; i < 3; ++ i)
-		mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
-	
-	mesh->Preprocess();
-
-	return Generate(mesh);
-}
-
-ViewCell *ViewCell::Merge(ViewCell &front, ViewCell &back)
-{
-	ViewCell *vc = Generate();
-	// merge pvs
-	vc->mPvs.Merge(front.mPvs, back.mPvs);
-
-	// merge ray sets
-	stable_sort(front.mPiercingRays.begin(), front.mPiercingRays.end());
-	stable_sort(back.mPiercingRays.begin(), back.mPiercingRays.end());
-
-	std::merge(front.mPiercingRays.begin(), front.mPiercingRays.end(),
-			   back.mPiercingRays.begin(), back.mPiercingRays.end(), 
-			   vc->mPiercingRays.begin());
-
-	return vc;
-}
-
 void ViewCell::AddPassingRay(const Ray &ray, const int contributions)
 {
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp	(revision 376)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp	(revision 439)
@@ -28,5 +28,5 @@
 #include "SceneGraph.h"
 #include "Triangle3.h"
-#include "ViewCell.h"
+#include "ViewCellsManager.h"
 
 // ---------------------------------------------------------------------------
@@ -103,5 +103,5 @@
 void
 X3dParseHandlers::StartIndexedFaceSet(
-																			AttributeList&  attributes)
+									  AttributeList&  attributes)
 {
   int len = attributes.getLength();
@@ -195,5 +195,5 @@
 void
 X3dParseHandlers::startElement(const XMLCh* const name,
-															 AttributeList&  attributes)
+							   AttributeList&  attributes)
 {
   StrX lname(name);
@@ -385,5 +385,5 @@
 //  StdInParseHandlers: Constructors and Destructor
 // ---------------------------------------------------------------------------
-X3dViewCellsParseHandlers::X3dViewCellsParseHandlers(ViewCellContainer *viewCells, 
+X3dViewCellsParseHandlers::X3dViewCellsParseHandlers(ViewCellsManager *viewCellsManager, 
 													 float viewCellHeight) :
   mElementCount(0)
@@ -391,5 +391,5 @@
   , mCharacterCount(0)
   , mSpaceCount(0)
-  , mViewCells(viewCells)
+  , mViewCellsManager(viewCellsManager)
   , mViewCellHeight(viewCellHeight)
 {
@@ -457,6 +457,5 @@
 
 void
-X3dViewCellsParseHandlers::StartCoordinate(
-																					 AttributeList&  attributes)
+X3dViewCellsParseHandlers::StartCoordinate(AttributeList&  attributes)
 {
 	int len = attributes.getLength();
@@ -507,5 +506,5 @@
 
 	for (i = 0; i < mCurrentVertexIndices.size(); i += 3)
-		{
+	{
 		Triangle3 baseTri(vertices[mCurrentVertexIndices[i + 0]], 
 						  vertices[mCurrentVertexIndices[i + 1]],
@@ -513,13 +512,6 @@
 
 		// create view cell from base triangle
-		mViewCells->push_back(ViewCell::ExtrudeViewCell(baseTri, mViewCellHeight));
-
-		Mesh *mesh = mViewCells->back()->GetMesh();
-#ifdef _DEBUG
-		Debug << "Viewcell: " 
-					<< mesh->mVertices[0] << " " << mesh->mVertices[1] << " " << mesh->mVertices[2] << " " 
-					<< mesh->mVertices[3] << " " << mesh->mVertices[4] << " " << mesh->mVertices[5] << "\n";
-#endif
-		}
+		mViewCellsManager->AddViewCell(mViewCellsManager->ExtrudeViewCell(baseTri, mViewCellHeight));
+	}
 }
 
@@ -527,5 +519,5 @@
 void
 X3dViewCellsParseHandlers::startElement(const XMLCh* const name,
-																				AttributeList&  attributes)
+										AttributeList&  attributes)
 {
   StrX lname(name);
@@ -608,5 +600,5 @@
 
 bool
-X3dParser::ParseFile(const string filename, ViewCellContainer &viewCells)
+X3dParser::ParseFile(const string filename, ViewCellsManager &viewCells)
 {
   // Initialize the XML4C system
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.h	(revision 414)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.h	(revision 439)
@@ -47,6 +47,29 @@
 public:
 	RenderSimulator();
+	/** Constructor taking the estimated render cost, 
+		the view cell overhead,
+		and the average move speed of the player into account.
+	*/
 	RenderSimulator(float objRendercost, float vcOverhead, float moveSpeed);
-	virtual Real SimulateRendering() = 0;
+
+	/** Simulates rendering of the view cells.
+		@returns the statistics of the simulation.
+	*/
+	virtual SimulationStatistics SimulateRendering() = 0;
+
+	/** Sets estimated render time for a single object in the PVS.
+	*/
+	void SetObjectRenderCost(const float objRenderCost);
+
+	/** Contant overhead for crossing a view cell border.
+	*/
+	void SetVcOverhead(const float vcOverhead);
+
+	/** Average move speed of the player during rendering.
+	*/
+	void SetMoveSpeed(const float moveSpeed);
+
+
+protected:
 
 	/// render time for single object of the PVS
@@ -56,17 +79,15 @@
 	/// move speed of player
 	float mMoveSpeed;
-
-	SimulationStatistics mStat;
 };
 
-class BspViewCellRenderSimulator: public RenderSimulator
+class BspRenderSimulator: public RenderSimulator
 {
 public:
-	BspViewCellRenderSimulator(float objRenderCost, 
+	BspRenderSimulator(float objRenderCost, 
 							   float vcOverhead, 
 							   float moveSpeed, 
 							   BspTree *bspTree);
 
-	Real SimulateRendering();
+	SimulationStatistics SimulateRendering();
 
 protected:
@@ -80,12 +101,12 @@
 };
 
-class KdViewCellRenderSimulator: public RenderSimulator
+class KdRenderSimulator: public RenderSimulator
 {
 public:
-	KdViewCellRenderSimulator(float objRenderCost, 
+	KdRenderSimulator(float objRenderCost, 
 							  float vcOverhead, 
 							  float moveSpeed, 
 							  KdTree *kdTree);
-	Real SimulateRendering();
+	SimulationStatistics SimulateRendering();
 
 protected:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 437)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 439)
@@ -527,6 +527,5 @@
 		VspBspLeaf *leaf = dynamic_cast<VspBspLeaf *>(tData.mNode);
 	
-		BspViewCell *viewCell =
-			dynamic_cast<BspViewCell *>(ViewCell::Generate());
+		BspViewCell *viewCell = new BspViewCell();
 		
 		leaf->SetViewCell(viewCell);
@@ -1437,5 +1436,5 @@
 {
 	BspViewCell *viewCell = 
-		dynamic_cast<BspViewCell *>(ViewCell::Merge(*front->mViewCell, *back->mViewCell));
+	NULL;//TODO	dynamic_cast<BspViewCell *>(ViewCell::Merge(*front->mViewCell, *back->mViewCell));
 	
 	if (!viewCell)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp	(revision 420)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RenderSimulator.cpp	(revision 439)
@@ -26,14 +26,33 @@
 {}
 
-RenderSimulator::RenderSimulator(float objRenderCost, float vcOverhead, float moveSpeed):
-mObjRenderCost(objRenderCost), mVcOverhead(vcOverhead), mMoveSpeed(moveSpeed)
-{
-}
-
-/*****************************************************
- *     class ViewCellRenderSimulator implementation  *
- *****************************************************/
-
-BspViewCellRenderSimulator::BspViewCellRenderSimulator(float objRenderCost, 
+RenderSimulator::RenderSimulator(float objRenderCost, 
+								 float vcOverhead, 
+								 float moveSpeed):
+mObjRenderCost(objRenderCost), 
+mVcOverhead(vcOverhead), 
+mMoveSpeed(moveSpeed)
+{
+}
+
+void RenderSimulator::SetObjectRenderCost(const float objRenderCost)
+{
+	mObjRenderCost = objRenderCost;
+}
+
+void RenderSimulator::SetVcOverhead(const float vcOverhead)
+{
+	mVcOverhead = vcOverhead;
+}
+
+void RenderSimulator::SetMoveSpeed(const float moveSpeed)
+{
+	mMoveSpeed = moveSpeed;
+}
+
+/********************************************************/
+/*     class BspRenderSimulator implementation          */
+/********************************************************/
+
+BspRenderSimulator::BspRenderSimulator(float objRenderCost, 
 											   float vcOverhead, 
 											   float moveSpeed,
@@ -43,8 +62,10 @@
 }
 
-Real BspViewCellRenderSimulator::SimulateRendering()
-{
-	mStat.Reset();
-	mStat.Start();
+SimulationStatistics BspRenderSimulator::SimulateRendering()
+{
+	SimulationStatistics simStat;
+
+	simStat.Reset();
+	simStat.Start();
 
 	Real renderTime = 0;
@@ -84,8 +105,8 @@
 		renderTime += vcCost;
 
-		if (vcCost > mStat.maxCost)
-			mStat.maxCost = vcCost;
-		else if (vcCost < mStat.minCost)
-			mStat.minCost = vcCost;
+		if (vcCost > simStat.maxCost)
+			simStat.maxCost = vcCost;
+		else if (vcCost < simStat.minCost)
+			simStat.minCost = vcCost;
 
 		// probability that a view cell border is crossed
@@ -94,5 +115,5 @@
 		// crossing the border of a view cell is also depending on the move speed
 		loadPvsOverhead += pCrossVc * mVcOverhead;
-overallarea+=area;
+overallarea += area;
 		pInVcTotal += pInVc;
 	}
@@ -102,16 +123,16 @@
 	loadPvsOverhead /= pCrossVcTotal;
 
-	mStat.avgRtWithoutOverhead = renderTime;
-	mStat.avgRenderTime = renderTime + loadPvsOverhead;
-	
-	mStat.maxCost /= pInVcTotal;
-	mStat.minCost /= pInVcTotal;
-
-	mStat.Stop();
-
-	return renderTime + loadPvsOverhead;
-}
-
-Real BspViewCellRenderSimulator::RenderPvs(ViewCell &viewCell, 
+	simStat.avgRtWithoutOverhead = renderTime;
+	simStat.avgRenderTime = renderTime + loadPvsOverhead;
+	
+	simStat.maxCost /= pInVcTotal;
+	simStat.minCost /= pInVcTotal;
+
+	simStat.Stop();
+
+	return simStat;
+}
+
+Real BspRenderSimulator::RenderPvs(ViewCell &viewCell, 
 									   float objRenderTime) const
 {
@@ -123,5 +144,5 @@
  *******************************************************/
 
-KdViewCellRenderSimulator::KdViewCellRenderSimulator(float objRenderCost, 
+KdRenderSimulator::KdRenderSimulator(float objRenderCost, 
 											 float vcOverhead,
 											 float moveSpeed,
@@ -131,9 +152,11 @@
 }
 
-Real KdViewCellRenderSimulator::SimulateRendering()
-{
+SimulationStatistics KdRenderSimulator::SimulateRendering()
+{
+	SimulationStatistics simStat;
+
 	//mKdTree->CollectLeavesPvs();
-	mStat.Reset();
-	mStat.Start();
+	simStat.Reset();
+	simStat.Start();
 
 	// total render time
@@ -170,8 +193,8 @@
 		renderTime += vcCost;
 
-		if (vcCost > mStat.maxCost)
-			mStat.maxCost = vcCost;
-		else if (vcCost < mStat.minCost)
-			mStat.minCost = vcCost;
+		if (vcCost > simStat.maxCost)
+			simStat.maxCost = vcCost;
+		else if (vcCost < simStat.minCost)
+			simStat.minCost = vcCost;
 
 		// probability that a view cell border is crossed
@@ -186,17 +209,17 @@
 	loadPvsOverhead /= pCrossVcTotal;
 
-    mStat.avgRtWithoutOverhead = renderTime;
-	mStat.avgRenderTime = renderTime + loadPvsOverhead;
-
-	mStat.maxCost /= pInVcTotal;
-	mStat.minCost /= pInVcTotal;
-
-	mStat.Stop();
-
-	return renderTime + loadPvsOverhead;
-}
-
-Real KdViewCellRenderSimulator::RenderPvs(KdLeaf *leaf, 
-										   float objRenderTime) const
+    simStat.avgRtWithoutOverhead = renderTime;
+	simStat.avgRenderTime = renderTime + loadPvsOverhead;
+
+	simStat.maxCost /= pInVcTotal;
+	simStat.minCost /= pInVcTotal;
+
+	simStat.Stop();
+
+	return simStat;
+}
+
+Real KdRenderSimulator::RenderPvs(KdLeaf *leaf, 
+								  float objRenderTime) const
 {
 	return leaf->mKdPvs.GetSize() * objRenderTime;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 437)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 439)
@@ -353,28 +353,4 @@
 }
 
-void BspTree::ParseEnvironment()
-{
-	//-- parse bsp cell tree construction method
-	char constructionMethodStr[60];
-	
-	environment->GetStringValue("BspTree.Construction.input", 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, "fromSamples") == 0)
-		sConstructionMethod = FROM_SAMPLES;
-	else 
-	{
-		cerr << "Wrong construction method " << constructionMethodStr << endl;
-		exit(1);
-    }
-
-	Debug << "Construction method: " << constructionMethodStr << endl;
-}
-
 const BspTreeStatistics &BspTree::GetStatistics() const 
 {
@@ -809,5 +785,5 @@
 		if (mGenerateViewCells)
 		{
-			viewCell = dynamic_cast<BspViewCell *>(ViewCell::Generate());
+			viewCell = new BspViewCell();
 		}
 		else
@@ -1980,5 +1956,5 @@
 {
 	BspViewCell *viewCell = 
-		dynamic_cast<BspViewCell *>(ViewCell::Merge(*front->mViewCell, *back->mViewCell));
+NULL;//todo		dynamic_cast<BspViewCell *>(ViewCell::Merge(*front->mViewCell, *back->mViewCell));
 	
 	if (!viewCell)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 419)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 439)
@@ -29,14 +29,4 @@
 
 	int Type() const;
-
-	/** Derives viewcells from object container.
-		@param objects the intersectables the viewcells are derived from
-		@param viewCells the viewcells are returned in this container
-		@param maxViewCells the maximum number of viewcells created. if 0 => unbounded
-	*/
-	static void DeriveViewCells(const ObjectContainer &objects, 
-								ViewCellContainer &viewCells, 
-								const int maxViewCells);
-
 	
 	/** Adds a passing ray to the passing ray container.
@@ -44,15 +34,4 @@
 	void AddPassingRay(const Ray &ray, const int contributions);	
 
-	/** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector.
-		@param the base triangle
-		@param the height of the newly created view cell
-	*/
-	static ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height);
-
-	/** Merges two view cells.
-		@note the piercing rays of the front and back will be ordered	
-		@returns new view cell based on the merging.
-	*/
-	static ViewCell *Merge(ViewCell &front, ViewCell &back);
 
 	/// Ray set description of the rays passing through this node.	
@@ -61,14 +40,4 @@
 	/// Rays piercing this view cell.
 	RayContainer mPiercingRays;
-
-	/// view cells types
-	enum HierarchyType {BSP, KD, VSP};
-
-	/** Generates view cells of type specified by view cells type.
-	*/
-	static ViewCell *Generate(Mesh *mesh = NULL);
-
-	/// type of view cells hierarchy (KD, BSP)
-	static HierarchyType sHierarchy;
 
 protected:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ExactPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ExactPreprocessor.h	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ExactPreprocessor.h	(revision 439)
@@ -10,6 +10,4 @@
     virtual bool ComputeVisibility();
 
-		virtual bool BuildBspTree() { return false; }
-
   };
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 429)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 439)
@@ -6,5 +6,5 @@
 #include "ViewCell.h"
 #include "Environment.h"
-#include "RenderSimulator.h"
+#include "ViewCellsManager.h"
 
 Preprocessor::Preprocessor():
@@ -12,5 +12,5 @@
 mBspTree(NULL),
 mVspKdTree(NULL),
-mRenderSimulator(NULL)
+mViewCellsManager(NULL)
 {
 }
@@ -21,91 +21,6 @@
 	DEL_PTR(mBspTree);
 	DEL_PTR(mKdTree);
-
-	DEL_PTR(mRenderSimulator);
-
-	DeleteViewCells();
-}
-
-bool
-Preprocessor::LoadViewCells(const string filename)
-{
-	X3dParser parser;
-	
-	environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
-	
-	return parser.ParseFile(filename, mViewCells);
-}
-
-bool
-Preprocessor::ParseViewCellsOptions()
-{
-	// parse type of view cells
-	char viewCellsStr[64];
-	environment->GetStringValue("ViewCells.hierarchy", viewCellsStr);
-
-	if (strcmp(viewCellsStr, "bspTree") == 0)
-	{
-		ViewCell::sHierarchy = ViewCell::BSP;
-	}
-	else if (strcmp(viewCellsStr, "kdTree") == 0)
-	{
-		ViewCell::sHierarchy = ViewCell::KD;
-	}
-	else if (strcmp(viewCellsStr, "vspTree") == 0)
-	{
-		ViewCell::sHierarchy = ViewCell::VSP;
-	}
-	else if (strcmp(viewCellsStr, "sceneDependent") == 0)
-	{
-		//TODO
-	}
-	else
-	{
-		cerr<<"Wrong view cells type" << viewCellsStr << endl;
-		exit(1);
-	}
-
-	return true;
-}
-
-RenderSimulator *Preprocessor::GetRenderSimulator()
-{
-	if (mRenderSimulator)
-		return mRenderSimulator;
-
-    float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
-
-	environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
-	environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
-	environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
-
-	Debug << "render simulator using render cost=" << objRenderCost << ", vc overhead=" << vcOverhead << ", move speed=" << moveSpeed << endl;
-	if (ViewCell::sHierarchy == ViewCell::BSP)
-	{
-		mRenderSimulator = new BspViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mBspTree);
-		Debug << "creating bsp render simulator" << endl;
-	}
-	else if (ViewCell::sHierarchy == ViewCell::KD)// KD view cells
-	{
-		mRenderSimulator = new KdViewCellRenderSimulator(objRenderCost, vcOverhead, moveSpeed, mKdTree);
-		Debug << "creating kd render simulator" << endl;
-	}
-	else
-	{
-		Debug << "not implemented yet" << endl;
-		return NULL;
-	}
-
-	return mRenderSimulator;
-}
-
-void Preprocessor::DeleteViewCells()
-{
-	for (int i = 0; i < (int)mViewCells.size(); ++ i)
-	{
-		Mesh *mesh = mViewCells[i]->GetMesh();
-		DEL_PTR(mesh);
-	}
-	CLEAR_CONTAINER(mViewCells);
+	DEL_PTR(mVspKdTree);
+	DEL_PTR(mViewCellsManager);
 }
 
@@ -512,2 +427,47 @@
 		exporter->ExportIntersectable(objects[j]);
 }
+
+bool Preprocessor::PrepareViewCells()
+{
+	//-- parse type of view cell container
+	char viewCellsStr[64];
+	environment->GetStringValue("ViewCells.type", viewCellsStr);
+
+	if (strcmp(viewCellsStr, "kdTree") == 0)
+	{
+		//mViewCellsManager = new KdViewCellsManager(mKdTree);
+	}
+	if (strcmp(viewCellsStr, "bspTree") == 0)
+	{
+		//mViewCellsManager = new BspViewCellsManager(mBspTree, mBspConstructionSamples);
+	}
+	else if (strcmp(viewCellsStr, "vspTree") == 0)
+	{
+		//mViewCellsManager = new VspKdViewCellContaienr(mVspKdTree, mVspKdConstructionSamples);
+	}
+	else if (strcmp(viewCellsStr, "sceneDependent") == 0)
+	{
+		//TODO
+		//mViewCellsManager = new BspViewCellContainer(mBspTree);
+	}
+	else
+	{
+		cerr<<"Wrong view cells type" << viewCellsStr << endl;
+		exit(1);
+	}
+
+	//-- parse view cells construction method
+	bool loadViewCells = false;
+	environment->GetBoolValue("ViewCell.Construction.loadFromFile", loadViewCells);
+
+	//-- load view cells from file if requested
+	if (loadViewCells)
+	{
+		char buff[100];
+		environment->GetStringValue("ViewCells.filename", buff);
+		string vcFilename(buff);
+		mViewCellsManager->LoadViewCells(vcFilename);
+	}
+
+	return true;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 438)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 439)
@@ -9,5 +9,5 @@
 #include "VssRay.h"
 #include "VssTree.h"
-
+#include "ViewCellsManager.h"
 
 bool useViewSpaceBox = true;//true;
@@ -451,20 +451,8 @@
   }
 	
-  //-- construct BSP view cells
-  if (ViewCell::sHierarchy == ViewCell::BSP) 
-  {
-	  const int bspSamples = min((int)mVssRays.size(), mBspConstructionSamples);
-		
-	  Debug << "bpssamples: " << bspSamples << endl;
-	  for (int i = 0; i < bspSamples; ++ i)
-	 	  bspRays.push_back(new Ray(*mVssRays[i]));
-	 
-	  //-- construct BSP tree using the samples
-	  mBspTree = new BspTree(&mUnbounded);	
-
-	  ObjectContainer objects;
-	  mSceneGraph->CollectObjects(&objects);
-	  mBspTree->Construct(objects, bspRays);
-	}
+  ObjectContainer objects;
+  mSceneGraph->CollectObjects(&objects);
+
+  //mViewCellsManager->Construct(mVssRays, objects);
 
   vssTree = new VssTree;
@@ -541,5 +529,5 @@
 
 	// cast rays into BSP tree
-	if (ViewCell::sHierarchy == ViewCell::BSP) 
+	/*if (ViewCell::sHierarchy == ViewCell::BSP) 
 	  {
 		for (int i = 0; i < (int)vssRays.size(); ++ i)
@@ -548,5 +536,5 @@
 		  }
 	  }
-
+*/
 	samples+=num;
 	float pvs = vssTree->GetAvgPvsSize();
@@ -562,66 +550,4 @@
   delete vssTree;
 
-  if (ViewCell::sHierarchy == ViewCell::BSP) 
-	{
-	  Debug << mBspTree->GetStatistics();
-
-	  ObjectContainer objects;
-	  ExportSplits(objects, bspRays, 10000);
-	  ExportBspPvs(objects, bspRays, 10000);
-
-	  BspViewCellsStatistics stat;
-	  mBspTree->EvaluateViewCellsStats(stat);
-	  Debug << "original view cell partition:\n" << stat << endl;
-
-	  // clear BSP samples
-	  CLEAR_CONTAINER(bspRays);
-	}
-
   return true;
 }
-
-void VssPreprocessor::CastRay(const BspTree &tree, const VssRay & vssRay)
-{
-  //-- cast ray to BSP tree to get intersection with view cells
-  Ray ray(vssRay);
-  mBspTree->CastRay(ray);
-				
-  //if (0 && ray.sourceObject.mObject)
-  //sampleContributions += 
-  //AddObjectSamples(ray.sourceObject.mObject, ray);
-			
-  if (!ray.intersections.empty()) // second intersection found
-  {
-	  //sampleContributions += 
-	  AddObjectSamples(ray.intersections[0].mObject, ray);
-  }
-}
-
-int VssPreprocessor::AddObjectSamples(Intersectable *obj, const Ray &ray)
-{
-  int contributingSamples = 0;
-  int j;
- 
-  // object can be seen from the view cell => add to view cell pvs
-  for (j=0; j < ray.bspIntersections.size(); ++ j)
-	{ 	
-	  BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
-	  // if ray not in unbounded space
-	  if (leaf->GetViewCell() != &mUnbounded)
-		contributingSamples += 
-		  leaf->GetViewCell()->GetPvs().AddSample(obj);
-	}
- 
-  // rays passing through this viewcell
-  if (mPass > 1)
-	for (j=1; j < ((int)ray.bspIntersections.size() - 1); ++ j) 
-	  {
-		BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
-		
-		if (leaf->GetViewCell() != &mUnbounded)
-		  leaf->GetViewCell()->
-			AddPassingRay(ray, contributingSamples ? 1 : 0);
-	  }
-  
-  return contributingSamples;
-}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 422)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 439)
@@ -54,31 +54,7 @@
   p->KdTreeStatistics(cout);
   
-  // parse view cell hierarchy options
-  p->ParseViewCellsOptions();
+  // parse view cells related options
+  p->PrepareViewCells();
 
-  if (ViewCell::sHierarchy == ViewCell::BSP)
-  {
-	  BspTree::ParseEnvironment();
-
-	  // We construct BSP tree immediately for "from view cells" or "from scene geometry"
-	  // construction method. 
-	  // For the "from samples" option, construction is delayed until enough samples were collected
-	  if (BspTree::sConstructionMethod != BspTree::FROM_SAMPLES)
-	  {		
-		  if (BspTree::sConstructionMethod == BspTree::FROM_INPUT_VIEW_CELLS)
-		  {
-			  //-- load view cells from file
-			  environment->GetStringValue("ViewCells.filename", buff);
-			  string vcFilename(buff);
-			  p->LoadViewCells(vcFilename);
-			  Debug << (int)p->mViewCells.size() << " view cells loaded" << endl;
-		  }
-		  
-		  p->BuildBspTree();
-	
-		  p->Export("vc_bsptree.x3d", false, false, true);
-		  p->BspTreeStatistics(Debug);
-	  }
-  }
 
   //  p->mSceneGraph->Export("soda.x3d");
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 429)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 439)
@@ -65,5 +65,5 @@
 		@param sampleContributions contribution of the samples
 	*/
-	void ProcessBspViewCells(const Ray &ray,
+	/*void ProcessBspViewCells(const Ray &ray,
 							 Intersectable *object,
 							 const int faceIndex,
@@ -76,10 +76,14 @@
 							 int &contributingSamples,
 							 int &sampleContributions);
+*/
+	void ProcessViewCells(const Ray &ray,
+						  Intersectable *object,
+						  const int faceIndex,
+						  int &contributingSamples,
+						  int &sampleContributions);
 
-	/** Adds objects samples to bsp view cells.
+	/** Adds objects samples to kd and bsp view cells.
 	*/
 	int AddObjectSamples(Intersectable *obj, const Ray &ray);
-
-	bool BuildBspTree();
 
 	/** Post processes view cells (i.e., merges or subdivides view cells based
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 429)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 439)
@@ -14,4 +14,6 @@
 class SceneGraph;
 class Exporter;
+class ViewCellsManager;
+
 
 /** Namespace for the external visibility preprocessor
@@ -40,12 +42,4 @@
   virtual bool LoadScene(const string filename);
   
-  /** Load the input viewcells. The input viewcells should be given as a collection
-      of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of
-      the viewcell. The method then builds a BSP tree of these view cells.
-	@param filename file to load
-	@return true on success
-  */
-  virtual bool LoadViewCells(const string filename);
-  
   /** Export all preprocessed data in a XML format understandable by the
       PreprocessingInterface of the GtpVisibilityPreprocessor Module. The file can be compressed depending
@@ -60,10 +54,4 @@
   */
   virtual bool BuildKdTree();
-  
-   /** Build the BSP tree of currently loaded occluders/occludees/viewcells. The construction
-      is driven by the environment settings, which also says which of the three types of
-      entities should be used to drive the heuristical construction (only occluders by default)
-  */
-  virtual bool BuildBspTree() = 0;
 
   /** Compute visibility method. This method has to be reimplemented by the actual
@@ -73,4 +61,9 @@
   virtual bool ComputeVisibility() = 0;
   
+  /** View cells are either loaded or prepared for generation, according to the chosen environment
+      object. Important evironment options are, e.g, the view cell type.
+      Should be done after scene loading (i.e., some options are based on scene type).
+  */
+  bool PrepareViewCells();
   
   bool
@@ -85,13 +78,4 @@
   virtual void BspTreeStatistics(ostream &s);
 
-  void DeleteViewCells();
-
-  RenderSimulator *GetRenderSimulator();
-
-  /** Parses the view cell options
-  */
-  bool ParseViewCellsOptions();
-
-
   /// scene graph loaded from file
   SceneGraph *mSceneGraph;
@@ -104,14 +88,9 @@
   /// list of all loaded occludees
   ObjectContainer mOccludees;
-  /// list of all loaded/generated viewcells
-  ViewCellContainer mViewCells;
-  
+   
   /// BSP tree representing the viewcells
   BspTree *mBspTree;
 
-  /// the view cell corresponding to unbounded space
-  BspViewCell mUnbounded;
-
-  RenderSimulator *mRenderSimulator;
+  ViewCellsManager *mViewCellsManager;
 
   /** Kd tree inducing a coarse partition of view space that are the building
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.h	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.h	(revision 439)
@@ -5,5 +5,5 @@
 #include "Containers.h"
 
-class ViewCell;
+class ViewCellsManager;
 
 class X3dParser : public Parser
@@ -13,5 +13,5 @@
   
   bool ParseFile(const string filename, SceneGraphNode **root);
-  bool ParseFile(const string filename, ViewCellContainer &viewCells);
+  bool ParseFile(const string filename, ViewCellsManager &viewCells);
 
   float mViewCellHeight;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 437)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 439)
@@ -7,4 +7,5 @@
 #include "Polygon3.h"
 #include "ViewCell.h"
+#include "ViewCellsManager.h"
 #include "RenderSimulator.h"
 
@@ -53,40 +54,9 @@
 }
 
-bool
-SamplingPreprocessor::BuildBspTree()
-{
-	// delete old tree
-	DEL_PTR(mBspTree);
-	mBspTree = new BspTree(&mUnbounded);
-	ObjectContainer objects;
-	
-	switch (BspTree::sConstructionMethod)
-	{
-	case BspTree::FROM_INPUT_VIEW_CELLS:
-		mBspTree->Construct(mViewCells);
-		break;
-	case BspTree::FROM_SCENE_GEOMETRY:
-		DeleteViewCells(); // we generate new view cells
-		mSceneGraph->CollectObjects(&objects);
-		mBspTree->Construct(objects);
-		break;
-	case BspTree::FROM_SAMPLES:
-		DeleteViewCells(); // we generate new view cells
-		mBspTree->Construct(mSampleRays);
-		break;
-	default:
-		Debug << "Error: Method not available\n";
-		break;
-	}
-	
-	
-	return true;
-}
-
 int
 SamplingPreprocessor::AddNodeSamples(const Ray &ray,
-																		 Intersectable *sObject,
-																		 Intersectable *tObject
-																		 )
+									 Intersectable *sObject,
+									 Intersectable *tObject
+									 )
 {
   int contributingSamples = 0;
@@ -110,7 +80,7 @@
 	for (j=1; j < ((int)ray.kdLeaves.size() - 1); j++) {
 		ray.kdLeaves[j]->AddPassingRay2(ray,
-																		objects,
-																		ray.kdLeaves.size()
-																		);
+										objects,
+										ray.kdLeaves.size()
+										);
   }
   
@@ -138,5 +108,5 @@
  
 	// object can be seen from the view cell => add to view cell pvs
-	for (j=0; j < ray.bspIntersections.size(); ++ j)
+	/*for (j=0; j < ray.bspIntersections.size(); ++ j)
 	{ 	
 		BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
@@ -145,8 +115,8 @@
 			contributingSamples += 
 				leaf->GetViewCell()->GetPvs().AddSample(obj);
-	}
+	}*/
  
 	// rays passing through this viewcell
-	if (mPass > 1)
+	/*if (mPass > 1)
 		for (j=1; j < ((int)ray.bspIntersections.size() - 1); ++ j) 
 		{
@@ -157,5 +127,5 @@
 					AddPassingRay(ray, contributingSamples ? 1 : 0);
 		}
- 
+ */
 	return contributingSamples;
 }
@@ -167,5 +137,5 @@
 	int sampleContributions = 0;
 
-	long t1 = GetRealTime();
+/*	long t1 = GetRealTime();
 	// cast ray to KD tree to find intersection with other objects
 	mKdTree->CastRay(ray);
@@ -214,5 +184,5 @@
 		break;
 	}
-
+*/
 	return sampleContributions;
 }
@@ -413,6 +383,6 @@
 		
 			int pvsSize = 0;
-			if (ViewCell::sHierarchy == ViewCell::KD)
-				pvsSize = object->mKdPvs.GetSize();
+			//if (ViewCell::sHierarchy == ViewCell::KD)
+			//	pvsSize = object->mKdPvs.GetSize();
 						
 			
@@ -479,21 +449,9 @@
 					}
 					//-------------------
-					if (ViewCell::sHierarchy == ViewCell::BSP) 
-					{
-						ProcessBspViewCells(ray,
-											reverseSample ? NULL : object,
-											faceIndex,
-											passContributingSamples,
-											passSampleContributions);
-
-					}
-					else if (ViewCell::sHierarchy == ViewCell::VSP)
-					{
-						ProcessVspViewCells(ray,
-											reverseSample ? NULL : object,
-											faceIndex,
-											passContributingSamples,
-											passSampleContributions);
-					}
+					ProcessViewCells(ray,
+									 reverseSample ? NULL : object,
+									 faceIndex,
+									 passContributingSamples,
+									 passSampleContributions);
 				}
 			} else {
@@ -514,5 +472,6 @@
 		mPass++;
         
-		if (ViewCell::sHierarchy == ViewCell::KD)
+		// TODO: move into view cell container
+		/*if (ViewCell::sHierarchy == ViewCell::KD)
 		{
 			pvsSize = 0;
@@ -525,5 +484,5 @@
 		else
 			pvsSize += passSampleContributions;
-		
+		*/
 		float avgRayContrib = (passContributingSamples > 0) ? 
 			passSampleContributions/(float)passContributingSamples : 0;
@@ -547,21 +506,15 @@
 	}
 	
-	if (ViewCell::sHierarchy == ViewCell::KD)	
+//	if (ViewCell::sHierarchy == ViewCell::KD)	
 		cout << "#totalKdPvsSize=" << mKdTree->CollectLeafPvs() << endl;
   
 	//-- render simulation
-	RenderSimulator *rs = GetRenderSimulator();
-
-	if (rs)
-	{
-		cout << "\nevaluating bsp view cells render time before merge ... ";
-
-		rs->SimulateRendering();
-	
-		cout << " finished" << endl;
-
-		cout << GetRenderSimulator()->mStat << endl;
-		Debug << GetRenderSimulator()->mStat << endl;
-	}
+	cout << "\nevaluating bsp view cells render time before merge ... ";
+
+	SimulationStatistics ss = mViewCellsManager->SimulateRendering();
+
+	cout << " finished" << endl;
+	cout << ss << endl;
+	Debug << ss << endl;
 
 	if (mBspTree)
@@ -610,18 +563,12 @@
 
 		//-- render simulation
-		
-		RenderSimulator *rs = GetRenderSimulator();
-
-		if (rs)
-		{
-			cout << "\nevaluating render time after merge ... ";
+		cout << "\nevaluating render time after merge ... ";
 			
-			rs->SimulateRendering();
-
-			cout << " finished" << endl;
-
-			cout << GetRenderSimulator()->mStat << endl;
-			Debug << GetRenderSimulator()->mStat << endl;
-		}
+		SimulationStatistics ss = mViewCellsManager->SimulateRendering();
+
+		cout << " finished" << endl;
+
+		cout << ss << endl;
+		Debug << ss << endl;
 
 		if (1) // export view cells
@@ -806,5 +753,12 @@
 }
 
-
+void SamplingPreprocessor::ProcessViewCells(const Ray &ray,
+                                            Intersectable *object,
+											const int faceIndex,
+											int &contributingSamples,
+											int &sampleContributions)
+{
+}
+/*
 void SamplingPreprocessor::ProcessVspViewCells(Ray &ray,
                                                Intersectable *object,
@@ -843,10 +797,4 @@
 		}
 	}
-	// save rays for post processing
-	/*else if (((int)mVspSampleRays.size() < mPostProcessSamples) ||
-			 ((int)mVspSampleRays.size() < mVisualizationSamples))
-	{
-		mVspSampleRays.push_back(new Ray(ray));
-	}*/
 }
 
@@ -897,5 +845,5 @@
 	}
 }
-
+*/
 // merge or subdivide view cells
 int SamplingPreprocessor::PostprocessViewCells(const RayContainer &rays)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 438)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 439)
@@ -58,8 +58,4 @@
 
 		  );
-		
-
-	
-  virtual bool BuildBspTree() { return false; }
 
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dParserXerces.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dParserXerces.h	(revision 313)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dParserXerces.h	(revision 439)
@@ -16,4 +16,5 @@
 class Mesh;
 class Material;
+class ViewCellsManager;
 
 class X3dParseHandlers : public HandlerBase
@@ -114,5 +115,5 @@
   //  Constructors and Destructor
   // -----------------------------------------------------------------------
-  X3dViewCellsParseHandlers(ViewCellContainer *mViewCells, 
+  X3dViewCellsParseHandlers(ViewCellsManager *viewCellsManager, 
 							float viewCellHeight);
   ~X3dViewCellsParseHandlers();
@@ -152,5 +153,5 @@
   void resetDocument();
 
-  ViewCellContainer *mViewCells;
+  ViewCellsManager *mViewCellsManager;
   float mViewCellHeight;
 
