Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj	(revision 633)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj	(revision 660)
@@ -15,10 +15,10 @@
 			OutputDirectory="..\lib\$(ConfigurationName)"
 			IntermediateDirectory="..\obj\$(ConfigurationName)"
-			ConfigurationType="4"
+			ConfigurationType="1"
 			CharacterSet="2">
 			<Tool
 				Name="VCCLCompilerTool"
 				Optimization="0"
-				AdditionalIncludeDirectories="..\support;..\support\devil\include;..\support\zlib\include;..\include;&quot;$(QTDIR)&quot;\includ&quot;;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;..\src;&quot;$(CG_INC_PATH)&quot;;&quot;$(GTPDIR)\NonGTP\Xerces&quot;;&quot;$(GTPDIR)\NonGTP\Devil&quot;;"
+				AdditionalIncludeDirectories="..\support;..\support\devil\include;..\support\zlib\include;..\include..\support;&quot;..\include  ..\support&quot;;..\include;&quot;$(GTPDIR)\NonGTP\Devil\include&quot;;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(GTPDIR)\NonGTP\Xerces&quot;;&quot;$(CG_INC_PATH)&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(GTPDIR)\NonGTP\Zlib\include&quot;"
 				PreprocessorDefinitions="WIN32;_DEBUG;_LIB"
 				MinimalRebuild="TRUE"
@@ -33,6 +33,7 @@
 				Name="VCCustomBuildTool"/>
 			<Tool
-				Name="VCLibrarianTool"
-				OutputFile="$(OutDir)/GtpVisibility.lib"/>
+				Name="VCLinkerTool"
+				AdditionalDependencies="xerces-c_2D.lib zdll.lib zziplibd.lib devil.lib qtmain.lib QtOpenGLd4.lib QtCored4.lib QtGuid4.lib Qt3Supportd4.lib QAxContainer.lib glut32.lib OpenGL32.Lib glu32.lib cg.lib cgGL.lib"
+				AdditionalLibraryDirectories="..\support\xercesc\lib\;..\support\zlib\lib\;..\support\devil\lib;&quot;$(QTDIR)\lib&quot;;..\include;..\src\GL;&quot;$(CG_LIB_PATH)&quot;;&quot;$(GTPDIR)\NonGTP\Xerces&quot;;&quot;$(GTPDIR)\NonGTP\Xerces\xercesc\lib&quot;;&quot;$(GTPDIR)\NonGTP\zlib\lib&quot;;&quot;$(GTPDIR)\NonGTP\Devil\lib&quot;"/>
 			<Tool
 				Name="VCMIDLTool"/>
@@ -49,4 +50,6 @@
 			<Tool
 				Name="VCXMLDataGeneratorTool"/>
+			<Tool
+				Name="VCWebDeploymentTool"/>
 			<Tool
 				Name="VCManagedWrapperGeneratorTool"/>
Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/default.env
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/default.env	(revision 658)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/default.env	(revision 660)
@@ -203,5 +203,9 @@
 	pruneEmptyViewCells false
 	processOnlyValidViewCells false
-	
+
+	stats viewCellStats.log
+
+	samplingType directional
+
 	PostProcess {
 		# how much samples are used for post processing
@@ -216,4 +220,5 @@
 		compress false
 		merge true
+		statsPrefix ../scripts/viewCells
 	}
 
@@ -314,4 +319,6 @@
 	maxStaticMemory	50
 	
+	subdivisionStats ../subDivisionStats.log
+
 	# factors for evaluating split plane costs
 	Factor {
@@ -405,4 +412,6 @@
 	maxTests 10000
 	
+	subdivisionStats ../subDivisionStats.log
+
 	# factors for evaluating split plane costs
 	Factor {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 655)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 660)
@@ -167,8 +167,12 @@
 		/// the current node
 		Plane3 mSplitPlane;
-		
+		/// split axis of this plane (0, 1, 2, or 3 if non-axis-aligned)
+		int mSplitAxis;
+		/// the number of misses of max cost ratio until this split
+		int mMaxCostMisses;
+
 		// parent data
 		VspBspTraversalData mParentData;
-
+		// cost of applying this split
 		float mRenderCost;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 651)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 660)
@@ -30,4 +30,5 @@
 int ViewCell::sMailId = 21843194198;
 int ViewCell::sReservedMailboxes = 1;
+int ViewCell::sLastUpdated = 0;
 
 //int upperPvsLimit = 120;
@@ -117,5 +118,6 @@
 mParent(NULL),
 mMergeCost(0),
-mIsActive(false)
+mIsActive(false),
+mLastUpdated(sLastUpdated)
 {
 }
@@ -216,4 +218,16 @@
 }
 
+
+void ViewCell::SetActive()
+{
+	mIsActive = true;
+	mLastUpdated = sLastUpdated;
+}
+
+
+bool ViewCell::IsActive() const
+{
+	return mIsActive  && (mLastUpdated == sLastUpdated);
+}
 
 
@@ -1251,5 +1265,5 @@
 
 
-void ViewCellsTree::ExportStats()
+void ViewCellsTree::ExportStats(const string &mergeStats)
 {
 	TraversalQueue tqueue;
@@ -1269,5 +1283,5 @@
 
 	ofstream stats;
-	stats.open("mergeStats.log");
+	stats.open(mergeStats.c_str());
 
 	stats 
@@ -1376,4 +1390,6 @@
 }
 
+
+
 void ViewCellsTree::CollectBestViewCellSet(ViewCellContainer &viewCells, 
 										   const int numViewCells)
@@ -1391,8 +1407,4 @@
 		if (vc->IsLeaf() || ((viewCells.size() + tqueue.size() + 1) >= numViewCells))
 		{
-			// todo: should be done with a function taking the active flag and some 
-			// time stamp so I don't have to reset view cells, this also means that 
-			// the leaf view cells can be set active fist
-			vc->mIsActive = true; 
 			viewCells.push_back(vc);
 		}
@@ -1611,5 +1623,5 @@
 ViewCell *ViewCellsTree::GetActiveViewCell(ViewCell *vc) const
 {
-	while (vc->GetParent() && !vc->mIsActive)
+	while (vc->GetParent() && !vc->IsActive())
 	{
 		vc = vc->GetParent();
@@ -1622,4 +1634,5 @@
 void ViewCellsTree::PropagatePvs(ViewCell *vc)
 {
+	// propagate pvs up
 	while (vc->GetParent())
 	{
@@ -1631,6 +1644,6 @@
 		return;
 
+	// propagate pvs to the leaves
 	stack<ViewCell *> tstack;
-
 	tstack.push(vc);
 
@@ -1791,5 +1804,5 @@
 		stream << "<Leaf ";
 		stream << "id=\"" << viewCell->GetId() << "\" ";
-		stream << "active=\"" << viewCell->mIsActive << "\" ";
+		stream << "active=\"" << viewCell->IsActive() << "\" ";
 		stream << "mergecost=\"" << viewCell->GetMergeCost() << "\" ";
 		stream << "pvs=\"";
@@ -1810,5 +1823,5 @@
 		stream << "<Interior ";
 		stream << "id=\"" << viewCell->GetId() << "\" ";
-		stream << "active=\"" << viewCell->mIsActive << "\" ";
+		stream << "active=\"" << viewCell->IsActive() << "\" ";
 		stream << "mergecost=\"" << viewCell->GetMergeCost() << "\" ";
 		stream << "pvs=\"";
@@ -1834,4 +1847,33 @@
 
 
+void ViewCellsTree::ResetPvs()
+{
+	stack<ViewCell *> tstack;
+
+	tstack.push(mRoot);
+
+	while (!tstack.empty())
+	{
+		ViewCell *vc = tstack.top();
+		tstack.pop();
+
+		vc->GetPvs().mEntries.clear();
+
+		if (!vc->IsLeaf())
+		{
+			ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(vc);
+			ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
+			for (it = interior->mChildren.begin(); it != it_end; ++ it)
+			{
+				tstack.push(*it);
+			}
+		}
+	}
+}
+
+
+void ViewCellsTree::SetActiveSetToLeaves()
+{
+}
 
 /**************************************************************************/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 658)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 660)
@@ -99,12 +99,14 @@
 			Mesh *mesh = new Mesh();
 
+			VertexIndexContainer vc;
+
 			// add vertices
 			for (int i = 0; i < (int)(*it).size(); ++ i)
 			{
 				mesh->mVertices.push_back(mCurrentVertices[(*it)[i]]);
-				
+				vc.push_back(i);
 			}
 			
-			mesh->mFaces.push_back(new Face(0,1,2));
+			mesh->mFaces.push_back(new Face(vc));
 			mesh->Preprocess();
 			// make an instance of this mesh
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 651)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 660)
@@ -320,5 +320,5 @@
 	virtual void ExportViewCellGeometry(Exporter *exporter, 
 										ViewCell *vc,
-										const Plane3 *cuttingPlane = NULL) const = 0;
+										const Plane3 *clipPlane = NULL) const = 0;
 
 	virtual void FinalizeViewCells(const bool createMesh);
@@ -352,6 +352,15 @@
 	void CollectViewCells(const int n);
 
+	/** Returns true if this (logical) view cell is equal to a spatial node.
+	*/
 	virtual bool EqualToSpatialNode(ViewCell *viewCell) const { return false;}
 
+	/** Sets current view cells set to active, i.e., the sampling is done in this view cell set.
+	*/
+	void SetViewCellsActive();
+
+	/** Evaluates worth of current view cell hierarchy.
+	*/
+	void EvalViewCellPartition();
 
 protected:
@@ -407,14 +416,14 @@
 	void CreateViewCellMeshes();
 
-
 	/** Takes different measures to prepares the view cells after loading them from disc.
 	*/
 	virtual void PrepareLoadedViewCells() {};
 
-
-	void CreateCuttingPlane();
-
-	Plane3 mCuttingPlane;
-	bool mUseCuttingPlaneForViz;
+	/** Creates clip plane for visualization.
+	*/
+	void CreateClipPlane();
+
+	Plane3 mClipPlane;
+	bool mUseClipPlaneForViz;
 
 
@@ -441,8 +450,9 @@
 	float mMaxPvsRatio;
 
+	int mSamplingType;
 	int mNumActiveViewCells;
 	bool mCompressViewCells;
 
-	ViewCellsStatistics mViewCellsStats;
+	ViewCellsStatistics mCurrentViewCellsStats;
 	/// the scene bounding box
 	AxisAlignedBox3 mViewSpaceBox;
@@ -517,5 +527,5 @@
 	void ExportViewCellGeometry(Exporter *exporter, 
 								ViewCell *vc, 
-								const Plane3 *cuttingPlane = NULL) const;
+								const Plane3 *clipPlane = NULL) const;
 	
 	void CollectMergeCandidates(const VssRayContainer &rays, 
@@ -600,5 +610,5 @@
 	void ExportViewCellGeometry(Exporter *exporter, 
 								ViewCell *vc,
-								const Plane3 *cuttingPlane = NULL) const;
+								const Plane3 *clipPlane = NULL) const;
 
 	void CollectMergeCandidates(const VssRayContainer &rays, 
@@ -666,5 +676,5 @@
 	void ExportViewCellGeometry(Exporter *exporter, 
 								ViewCell *vc,
-								const Plane3 *cuttingPlane = NULL) const;
+								const Plane3 *clipPlane = NULL) const;
 
 	void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
@@ -735,5 +745,5 @@
 	void ExportViewCellGeometry(Exporter *exporter, 
 								ViewCell *vc,
-								const Plane3 *cuttingPlane = NULL) const;
+								const Plane3 *clipPlane = NULL) const;
 
 	//float GetVolume(ViewCell *viewCell) const;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 656)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 660)
@@ -129,4 +129,8 @@
 	environment->GetBoolValue("VspBspTree.useCostHeuristics", mUseCostHeuristics);
 	environment->GetBoolValue("VspBspTree.useSplitCostQueue", mUseSplitCostQueue);
+
+	char subdivisionStatsLog[100];
+	environment->GetStringValue("BspTree.subdivisionStats", subdivisionStatsLog);
+	mSubdivisionStats.open(subdivisionStatsLog);
 
 
@@ -556,7 +560,6 @@
 							  geom);
 
+	// compute first split candidate
 	VspBspSplitCandidate splitCandidate;
-
-
 	EvalSplitCandidate(tData, splitCandidate);
 
@@ -820,9 +823,4 @@
 		VspBspTraversalData tBackData;
 
-#if OCTREE_HACK
-		//Debug << "new axis:" << (tData.mAxis + 1) % 3 << endl;
-		tFrontData.mAxis = (tData.mAxis + 1) % 3;
-		tBackData.mAxis = (tData.mAxis + 1) % 3;
-#endif
 		//-- continue subdivision
 		// create new interior node and two leaf node
@@ -831,5 +829,7 @@
 		newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData, coincident);
 		
-#if 0 // TODO
+		const int splitAxis = splitCandidate.mSplitAxis;
+		const int maxCostMisses = splitCandidate.mMaxCostMisses;
+
 		if (splitAxis < 3)
 			++ mBspStats.splits[splitAxis];
@@ -838,9 +838,10 @@
 
 		tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && splitAxis < 3);
+		
 		// how often was max cost ratio missed in this branch?
 		tFrontData.mMaxCostMisses = maxCostMisses;
 		tBackData.mMaxCostMisses = maxCostMisses;
-
-#endif
+		
+
 		if (1)
 		{
@@ -936,5 +937,5 @@
 
 	// compute locally best split plane
-	SelectPlane(splitData.mSplitPlane, leaf, tData, frontData, backData, splitAxis);
+	bool success = SelectPlane(splitData.mSplitPlane, leaf, tData, frontData, backData, splitAxis);
 
 	// TODO: reuse
@@ -945,4 +946,5 @@
 	splitData.mRenderCost = EvalRenderCostDecrease(splitData.mSplitPlane, tData);
 	splitData.mParentData = tData;
+	splitData.mMaxCostMisses = success ? tData.mMaxCostMisses : tData.mMaxCostMisses + 1;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 658)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 660)
@@ -1206,4 +1206,19 @@
 			"bspTree");
 
+	RegisterOption("ViewCells.stats",
+					optString,
+					"view_cells_stats=",
+					"viewCellsStats.log");
+
+	RegisterOption("ViewCells.PostProcess.statsPrefix",
+					optString,
+					"view_cells_stats_prefix=",
+					"viewCells");
+
+
+	RegisterOption("ViewCells.active",
+					optInt,
+					"1000");
+	
 	RegisterOption("ViewCells.active",
 					optInt,
@@ -1214,4 +1229,14 @@
 					"view_cells_construction_samples=",
 					"5000000");
+
+	RegisterOption("ViewCells.Evaluation.samples",
+					optInt,
+					"view_cells_evaluation_samples=",
+					"8000000");
+
+	RegisterOption("ViewCells.Evaluation.samplesPerPass",
+					optInt,
+					"view_cells_evaluation_samples_per_oass=",
+					"300000");
 
 	RegisterOption("ViewCells.Construction.samplesPerPass",
@@ -1280,5 +1305,4 @@
 		"-view_cells_delayed_construction",
 		"false");
-
 	
 
@@ -1297,5 +1321,5 @@
 		"-view_cells_process_only_valid_view_cells",
 		"false");
-	
+
 	
 	RegisterOption("ViewCells.PostProcess.maxCostRatio",
@@ -1303,5 +1327,4 @@
 			"-view_cells_post_process_max_cost_ratio=",
 			"0.9");
-
 	
 	RegisterOption("ViewCells.PostProcess.renderCostWeight",
@@ -1309,5 +1332,4 @@
 			"-view_cells_post_process_render_cost_weight",
 			"0.5");
-
 	
 	RegisterOption("ViewCells.PostProcess.avgCostMaxDeviation",
@@ -1346,13 +1368,12 @@
 		"8.0");
 
-	RegisterOption("ViewCells.Visualization.useCuttingPlane",
+	RegisterOption("ViewCells.Visualization.useClipPlane",
 		optBool, 
-		"view_cells_viz_use_cutting_plane=", 
+		"view_cells_viz_use_clip_plane=", 
 		"false");
-
 	
-	RegisterOption("ViewCells.Visualization.cuttingPlaneAxis",
+	RegisterOption("ViewCells.Visualization.clipPlaneAxis",
 		optInt, 
-		"view_cells_viz_cutting_plane_axis=", 
+		"view_cells_viz_clip_plane_axis=", 
 		"0");
 
@@ -1390,4 +1411,9 @@
 		"bsp_construction_input=",
 		"fromViewCells");
+
+	RegisterOption("BspTree.subdivisionStats",
+					optString,
+					"vsp_bsp_subdivision_stats=",
+					"bspSubdivisionStats.log");
 
 	RegisterOption("BspTree.Construction.samples",
@@ -1840,8 +1866,14 @@
 					"vsp_bsp_term_min_pvs=",
 					"20");
+
 	RegisterOption("VspBspTree.Termination.minProbability",
 					optFloat,
 					"vsp_bsp_term_min_probability=",
 					"0.001");
+
+	RegisterOption("VspBspTree.subdivisionStats",
+					optString,
+					"vsp_bsp_subdivision_stats=",
+					"vspBspSubdivisionStats.log");
 
 	RegisterOption("VspBspTree.Termination.maxRayContribution",
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 650)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 660)
@@ -264,5 +264,7 @@
 	environment->GetFloatValue("BspTree.Construction.epsilon", mEpsilon);
 
-	mSubdivisionStats.open("subdivisionStats.log");
+	char subdivisionStatsLog[100];
+	environment->GetStringValue("BspTree.subdivisionStats", subdivisionStatsLog);
+	mSubdivisionStats.open(subdivisionStatsLog);
 
     Debug << "BSP max depth: " << mTermMaxDepth << endl;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 651)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 660)
@@ -191,12 +191,17 @@
 
 
-						 
+	/** Sets this view cell to be an active view cell.
+	*/
+	void SetActive();
+	/** Returns if this view cell is active.
+	*/
+	bool IsActive() const;
+
 	// last mail id -> warning not thread safe!
 	// both mailId and mailbox should be unique for each thread!!!
 	static int sMailId;
 	static int sReservedMailboxes;
-	
-	bool mIsActive;
-
+
+	static int sLastUpdated;
 	
 protected:
@@ -212,6 +217,8 @@
 	bool mValid;
 
-  /** color used for consistent visualization */
-  RgbColor mColor;
+	int mLastUpdated;
+	bool mIsActive;
+	/** color used for consistent visualization */
+	RgbColor mColor;
 };
 
@@ -350,6 +357,12 @@
 	bool IsCompressed() const;
 
+	/** Returns active view cell that is in the path of this view cell.
+	*/
 	ViewCell *GetActiveViewCell(ViewCell *vc) const;
 
+	/** Sets the leaves to be the currently active view cells.
+	*/
+    void SetActiveSetToLeaves();
+
 	/** Propagates pvs up the tree to the root and downwards the tree.
 	*/
@@ -360,5 +373,5 @@
 	/** Export statistics of this view cell tree.
 	*/
-	void ExportStats();
+	void ExportStats(const string &mergeStats);
 
 	/** Sets root of hierarchy.
@@ -372,4 +385,7 @@
 	void CreateUniqueViewCellsIds();
 
+	/** Resets pvs of whole tree.
+	*/
+	void ResetPvs();
 
 protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 654)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 660)
@@ -329,5 +329,6 @@
 			const bool isActive = (bool)strtol(ptr, &endptr, 10);
 
-			viewCell->mIsActive = isActive;
+			if (isActive)
+				viewCell->SetActive();
 		}
 		else if (attrName == "mergecost")
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 658)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 660)
@@ -61,7 +61,22 @@
 	environment->GetIntValue("ViewCells.active", mNumActiveViewCells);
 	environment->GetBoolValue("ViewCells.PostProcess.compress", mCompressViewCells);
-	environment->GetBoolValue("ViewCells.Visualization.useCuttingPlane", mUseCuttingPlaneForViz);
+	environment->GetBoolValue("ViewCells.Visualization.useClipPlane", mUseClipPlaneForViz);
 
 	environment->GetBoolValue("ViewCells.PostProcess.merge", mMergeViewCells);
+
+	char samplingBuf[50];
+	environment->GetStringValue("ViewCells.samplingType", samplingBuf);
+	
+	if (strcmp(samplingBuf, "box") == 0)
+		mSamplingType = Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
+	else if (strcmp(samplingBuf, "directional") == 0)
+		mSamplingType = Preprocessor::DIRECTION_BASED_DISTRIBUTION;
+
+#if 0
+
+		char viewCellsStatsLog[100];
+		environment->GetStringValue("ViewCells.stats", viewCellsStats);
+		mViewCellsStats.open(viewCellsStatsLog);
+#endif
 
 	mMinPvsSize = emptyViewCells ? 1 : 0;
@@ -157,10 +172,10 @@
 	//-- construction rays => we use uniform samples for this
 	CastPassSamples(mInitialSamples, 
-					//Preprocessor::DIRECTION_BASED_DISTRIBUTION,
-					Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION, 
+					mSamplingType,
 					initialSamples);
 	
 	cout << "finished" << endl;
 
+	
 	// construct view cells
 	const int numInitialSamples = 
@@ -172,4 +187,26 @@
 	disposeRays(initialSamples, outRays);
 	
+
+	// -- stats after contruction
+	ResetViewCells();
+  	Debug << "\nView cells after initial sampling: " << mCurrentViewCellsStats << endl;
+
+	if (0) // export initial view cells
+	{
+		cout << "exporting initial view cells (=leaves) ... ";
+		Exporter *exporter = Exporter::GetExporter("view_cells.x3d");
+
+		if (exporter)
+		{
+			if (mExportGeometry)
+				exporter->ExportGeometry(preprocessor->mObjects);
+
+			ExportViewCellsForViz(exporter);
+
+			delete exporter;
+		}
+		cout << "finished" << endl;
+	}
+
 
 	//-- guided rays are used for further sampling
@@ -191,5 +228,5 @@
 		dirSamples = !dirSamples; // toggle sampling method
 		numSamples += CastPassSamples(mSamplesPerPass, 
-									  samplingType,
+									  mSamplingType,
 									  constructionSamples);
 
@@ -217,9 +254,24 @@
 				    Preprocessor::DIRECTION_BASED_DISTRIBUTION, 
 					postProcessSamples);
-	
+
+
+	// stats before post processing (i.e., merge)
+	EvaluateViewCellsStats();
+	Debug << "\noriginal view cell partition before post process:\n" << mCurrentViewCellsStats << endl;
+
+	mRenderer->RenderScene();
+	SimulationStatistics ss;
+	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
+
+    Debug << ss << endl;
+
+
+
 	cout << "starting post processing and visualization" << endl;
+
 
 	// store viewCells for postprocessing
 	const bool storeViewCells = true;
+
 
 	if (SAMPLE_AFTER_SUBDIVISION)
@@ -240,5 +292,4 @@
 		ComputeSampleContributions(visualizationSamples, true, storeViewCells);
 
-	//Debug << "visualizationsamples: " << mVisualizationSamples << " " << visualizationSamples.size() << endl;
 	// different visualizations
 	Visualize(preprocessor->mObjects, visualizationSamples);
@@ -247,4 +298,20 @@
 
 	return numSamples;
+}
+
+
+void ViewCellsManager::EvalViewCellPartition()
+{
+	int samplesPerPass;
+	int numSamples;
+	int castSamples = 0;
+
+	environment->GetIntValue("ViewCells.Evaluation.samplesPerPass", samplesPerPass);
+	environment->GetIntValue("ViewCells.Evaluation.samples", numSamples);
+
+	while (castSamples < numSamples)
+	{
+	}
+	//char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
 }
 
@@ -385,5 +452,5 @@
 void ViewCellsManager::EvaluateViewCellsStats()
 {
-	mViewCellsStats.Reset();
+	mCurrentViewCellsStats.Reset();
 
 	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
@@ -391,5 +458,5 @@
 	for (it = mViewCells.begin(); it != it_end; ++ it)
 	{
-		mViewCellsTree->UpdateViewCellsStats(*it, mViewCellsStats);
+		mViewCellsTree->UpdateViewCellsStats(*it, mCurrentViewCellsStats);
 	}
 }
@@ -698,5 +765,5 @@
 	mViewSpaceBox = box;
 
-	CreateCuttingPlane();
+	CreateClipPlane();
 	
 	mTotalAreaValid = false;
@@ -704,18 +771,18 @@
 
 
-void ViewCellsManager::CreateCuttingPlane()
+void ViewCellsManager::CreateClipPlane()
 {
 	int axis = 0;
 
-	const float factor = 0.65;
+	const float factor = 0.65f;
 	Vector3 point = mViewSpaceBox.Min() +  mViewSpaceBox.Size() * factor;
 
-	if (mUseCuttingPlaneForViz)
-        environment->GetIntValue("ViewCells.Visualization.cuttingPlaneAxis", axis);
+	if (mUseClipPlaneForViz)
+        environment->GetIntValue("ViewCells.Visualization.clipPlaneAxis", axis);
 
 	Vector3 normal(0,0,0);
 	normal[axis] = 1;
 
-	mCuttingPlane = Plane3(normal, point);
+	mClipPlane = Plane3(normal, point);
 }
 
@@ -733,5 +800,5 @@
 	CollectViewCells();
 	
-	mViewCellsStats.Reset();
+	mCurrentViewCellsStats.Reset();
 	EvaluateViewCellsStats();
 
@@ -813,6 +880,5 @@
 
 	CastLineSegment(origin, termination, viewcells);
-	//Debug << "constribution: " << (int)viewcells.size() << endl;
-
+	
 	// copy viewcells memory efficiently
 	//const bool storeViewcells = !addRays;
@@ -902,5 +968,5 @@
 void ViewCellsManager::PrintStatistics(ostream &s) const
 {
-	s << mViewCellsStats << endl;
+	s << mCurrentViewCellsStats << endl;
 }
 
@@ -926,5 +992,5 @@
 			ExportColor(exporter, *it);		
 			ExportViewCellGeometry(exporter, *it, 
-				mUseCuttingPlaneForViz ? &mCuttingPlane : NULL);
+				mUseClipPlaneForViz ? &mClipPlane : NULL);
 		}
 	}
@@ -960,4 +1026,13 @@
 
 
+void ViewCellsManager::SetViewCellsActive()
+{
+	++ ViewCell::sLastUpdated;
+	ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+	for (it = mViewCells.begin(); it != it_end; ++ it)
+	{
+		(*it)->SetActive();
+	}
+}
 
 /**********************************************************************/
@@ -1032,39 +1107,9 @@
 
 	//EvaluateViewCellsStats();
-	Debug << "\nView cells after construction:\n" << mViewCellsStats << endl;
+	Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
 
 	// recast rest of the rays
 	if (SAMPLE_AFTER_SUBDIVISION)
 		ComputeSampleContributions(savedRays, true, false);
-
-
-	ResetViewCells();
-    
-	Debug << "\nView cells after " << (int)savedRays.size() 
-		  << " samples:\n" << mViewCellsStats << endl;
-
-	if (1) // export initial view cells
-	{
-		cout << "exporting initial view cells (=leaves) ... ";
-		Exporter *exporter = Exporter::GetExporter("view_cells.x3d");
-
-		if (exporter)
-		{
-			if (0 && mExportRays)
-				exporter->ExportRays(rays, RgbColor(1, 1, 1));
-			
-			if (mExportGeometry)
-				exporter->ExportGeometry(objects);
-
-			//exporter->SetWireframe();
-			exporter->SetFilled();
-			ExportViewCellsForViz(exporter);
-
-
-			delete exporter;
-		}
-		cout << "finished" << endl;
-	}
-
 
 	return sampleContributions;
@@ -1115,5 +1160,4 @@
 		return 0;
 	}
-
 	
 	// view cells already finished before post processing step (i.e. because they were loaded)
@@ -1130,15 +1174,4 @@
     int vcSize = 0;
 	int pvsSize = 0;
-
-	EvaluateViewCellsStats();
-	Debug << "\noriginal view cell partition:\n" << mViewCellsStats << endl;
-
-	mRenderer->RenderScene();
-	SimulationStatistics ss;
-	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
-
-    Debug << ss << endl;
-
-	
 
 	//-- merge view cells
@@ -1151,15 +1184,17 @@
 	if (mMergeViewCells)
 	{
-		Debug << "constructing visibility based merge tree" << endl;
+		cout << "constructing visibility based merge tree" << endl;
 		mViewCellsTree->ConstructMergeTree(rays, objects);
 	}
 	else
 	{
-		Debug << "constructing spatial merge tree" << endl;
+		cout << "constructing spatial merge tree" << endl;
 		// create spatial merge hierarchy
 		mViewCellsTree->SetRoot(ConstructSpatialMergeTree(mBspTree->GetRoot()));
 	}
 
-	mViewCellsTree->ExportStats();
+	// export statistics after merge
+	if (0)
+		mViewCellsTree->ExportStats("mergeStats.log");
 
 	//-- stats and visualizations
@@ -1172,7 +1207,9 @@
 	
 
+	//-- visualization and statistics
     // reset view cells and stats
 	ResetViewCells();
-	Debug << "\nView cells after merge:\n" << mViewCellsStats << endl;
+	Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
+
 
 	int savedColorCode  = mColorCode;
@@ -1468,5 +1505,5 @@
 		{
 			importance = (float)vc->GetPvs().GetSize() /
-				(float)mViewCellsStats.maxPvs;
+				(float)mCurrentViewCellsStats.maxPvs;
 
 		}
@@ -1475,5 +1512,5 @@
 		{
             int lSize = mViewCellsTree->GetSize(vc);
-			importance = (float)lSize / (float)mViewCellsStats.maxLeaves;
+			importance = (float)lSize / (float)mCurrentViewCellsStats.maxLeaves;
 		}
 		//break;
@@ -1502,5 +1539,5 @@
 void BspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
 												 ViewCell *vc,
-												 const Plane3 *cuttingPlane) const
+												 const Plane3 *clipPlane) const
 {
 	if (vc->GetMesh())
@@ -1513,6 +1550,5 @@
 	mBspTree->ConstructGeometry(vc, geom);
 
-
-	if (cuttingPlane)
+	if (clipPlane)
 	{
 		BspNodeGeometry front;
@@ -1521,5 +1557,5 @@
 		geom.SplitGeometry(front,
 						   back,
-						   *cuttingPlane,
+						   *clipPlane,
 						   mViewSpaceBox, 
 						   0.0001f);
@@ -1775,5 +1811,5 @@
 
 	EvaluateViewCellsStats();
-	Debug << "\nView cells after construction:\n" << mViewCellsStats << endl;
+	Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
 
 	return 0;
@@ -1978,5 +2014,5 @@
 void KdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
 												ViewCell *vc,
-												const Plane3 *cuttingPlane) const
+												const Plane3 *clipPlane) const
 {
 	ViewCellContainer leaves;
@@ -2098,7 +2134,8 @@
 	ResetViewCells();
 
-	Debug << "\nView cells after construction:\n" << mViewCellsStats << endl;
+	Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
 
 	long startTime = GetTime();
+
 	// recast rest of rays
 	ComputeSampleContributions(savedRays, true, false);
@@ -2328,5 +2365,5 @@
 		{
 			importance = (float)vc->GetPvs().GetSize() /
-				(float)mViewCellsStats.maxPvs;
+				(float)mCurrentViewCellsStats.maxPvs;
 		}
 		break;
@@ -2335,5 +2372,5 @@
         	int lSize = mViewCellsTree->GetSize(vc);
 			importance = (float)lSize /
-				(float)mViewCellsStats.maxLeaves;
+				(float)mCurrentViewCellsStats.maxLeaves;
 		}
 		break;
@@ -2359,5 +2396,5 @@
 void VspKdViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
 												   ViewCell *vc,
-												   const Plane3 *cuttingPlane) const
+												   const Plane3 *clipPlane) const
 {
 	VspKdViewCell *kdVc = dynamic_cast<VspKdViewCell *>(vc);
@@ -2450,32 +2487,4 @@
 
 
-/*void VspBspViewCellsManager::CollectNodes(ViewCell *vc, vector<BspNode *>nodes)
-{
-	void ViewCellsTree::CollectLeaves(ViewCell *vc, vector<BspNode *> &nodes) const
-	
-	if (vc->IsLeaf())
-	{
-		BspViewCell *bvc = dynamic_cast<BspViewCell *>(vc);
-		nodes->push_back(bvc->mLeaf);
-	}
-	else
-	{
-		ViewCellContainer::const_iterator it, it_end = interior->mChildren.end();
-
-		for (it = interior->mChildren.begin; it != it_end; ++ it)
-		{
-			vector<BspNode *> mynodes;
-			CollectNodes(*it, mynodes);
-
-			if (mynodes[0]->GetParent() && (mynodes[0]->GetParent() == mynodes[1]->GetParent()))
-			{
-				nodes.push_back(nodes[0]->GetParent());
-			}
-
-		}
-	}
-}*/
-
-
 int VspBspViewCellsManager::ConstructSubdivision(const ObjectContainer &objects,
 												 const VssRayContainer &rays)
@@ -2485,6 +2494,4 @@
 		return 0;
 
-	Debug << "Constructing bsp view cells" << endl;
-
 	int sampleContributions = 0;
 
@@ -2496,5 +2503,5 @@
 	VssRayContainer savedRays;
 
-	Debug << "samples used for subdivision: " << mInitialSamples 
+	Debug << "samples used for vsp bsp subdivision: " << mInitialSamples 
 		  << " rays: " << (int)rays.size() << endl;
 
@@ -2507,6 +2514,4 @@
 
 	mVspBspTree->Construct(constructionRays, &mViewSpaceBox);
-
-	Debug << mVspBspTree->GetStatistics() << endl;
 
 	// collapse invalid regions
@@ -2519,36 +2524,12 @@
     cout << "finished" << endl;
 
-	cout << "reseting view cell stats ... ";
+	// -- stats
+	Debug << mVspBspTree->GetStatistics() << endl;
+
 	ResetViewCells();
-	cout << "finished" << endl;
-
-	Debug << "\nView cells after construction:\n" << mViewCellsStats << endl;
-
-	if (0) // export initial view cells
-	{
-		cout << "exporting initial view cells (=leaves) ... ";
-		Exporter *exporter = Exporter::GetExporter("view_cells.x3d");
-
-		if (exporter)
-		{
-			if (0 && mExportRays)
-				exporter->ExportRays(rays, RgbColor(1, 1, 1));
-
-			if (mExportGeometry)
-				exporter->ExportGeometry(objects);
-
-			//exporter->SetWireframe();
-			exporter->SetFilled();
-			ExportViewCellsForViz(exporter);
-
-			delete exporter;
-		}
-		cout << "finished" << endl;
-	}
+	Debug << "\nView cells after construction:\n" << mCurrentViewCellsStats << endl;
+
 
 	startTime = GetTime();
-
-	// reset view cells and stats
-	ResetViewCells();
 
 	cout << "Computing remaining ray contributions ... ";
@@ -2575,9 +2556,4 @@
 	int pvsSize = 0;
 
-	mRenderer->RenderScene();
-	SimulationStatistics ss;
-	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
-    Debug << ss << endl;
-
 	//-- merge view cells
 	
@@ -2588,5 +2564,4 @@
 	if (mMergeViewCells)
 	{
-		Debug << "merging view cells" << endl;
 		// TODO: should be done BEFORE the ray casting
 		mViewCellsTree->ConstructMergeTree(rays, objects);
@@ -2594,9 +2569,9 @@
 	else
 	{
-		Debug << "spatial merge" << endl;
 		mViewCellsTree->SetRoot(ConstructSpatialMergeTree(mVspBspTree->GetRoot()));
 	}
 
-	mViewCellsTree->ExportStats();
+	if (0)
+		mViewCellsTree->ExportStats("mergeStats.log");
 
 	//-- stats and visualizations
@@ -2611,16 +2586,14 @@
 	int savedColorCode = mColorCode;
 	
+	// get currently active view cell set
+	ResetViewCells();
+	Debug << "\nView cells after merge:\n" << mCurrentViewCellsStats << endl;
+
 	//BspLeaf::NewMail();
 	if (0) // export merged view cells
 	{
 		mColorCode = 0;
-		cout << "reseting view cells ... ";
-		ResetViewCells();
-
-		cout << "finished" << endl;
-
 		Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
-		Debug << "\nView cells after merge:\n" << mViewCellsStats << endl;
-
+		
 		cout << "exporting view cells after merge ... ";
 
@@ -2775,19 +2748,11 @@
 	mVspBspTree->ValidateTree();
 
-	// recompute view cell statistics
-	mViewCellsStats.Reset();
-	EvaluateViewCellsStats();
-
 	// has to be recomputed
 	mTotalAreaValid = false;
-
-
 	VssRayContainer postProcessRays;
-
 	GetRaySets(rays, mPostProcessSamples, postProcessRays);
 
 	Debug << "post processing using " << (int)postProcessRays.size() << " samples" << endl;
 
-	Debug << "\nview cell partition after sampling:\n" << mViewCellsStats << endl << endl;
 
 	// should maybe be done here to allow merge working with area or volume
@@ -2806,5 +2771,7 @@
 		RefineViewCells(postProcessRays, objects);
 
-	//-- render simulation after merge
+
+	//-- render simulation after merge + refine
+	
 	cout << "\nevaluating bsp view cells render time before compress ... ";
 	dynamic_cast<RenderSimulator *>(mRenderer)->RenderScene();
@@ -2831,4 +2798,5 @@
 		Debug << "number of entries after compress: " << pvsEntries << endl;
 	}
+
 
 	// collapse sibling leaves that share the same view cell
@@ -3288,5 +3256,5 @@
 		{
 			importance = (float)vc->GetPvs().GetSize() /
-				(float)mViewCellsStats.maxPvs;
+				(float)mCurrentViewCellsStats.maxPvs;
 
 		}
@@ -3295,5 +3263,5 @@
 		{
             int lSize = mViewCellsTree->GetSize(vc);
-			importance = (float)lSize / (float)mViewCellsStats.maxLeaves;
+			importance = (float)lSize / (float)mCurrentViewCellsStats.maxLeaves;
 		}
 		break;
@@ -3321,5 +3289,5 @@
 void VspBspViewCellsManager::ExportViewCellGeometry(Exporter *exporter,
                                                     ViewCell *vc,
-													const Plane3 *cuttingPlane) const
+													const Plane3 *clipPlane) const
 {
 	if (vc->GetMesh())
@@ -3331,5 +3299,5 @@
 
 	
-	if (cuttingPlane)
+	if (clipPlane)
 	{
 		ViewCellContainer leaves;
@@ -3351,5 +3319,5 @@
 			geom.SplitGeometry(front,
 							   back,
-							   *cuttingPlane,
+							   *clipPlane,
 							   mViewSpaceBox, 
 							   0.0001f);
