Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 555)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 557)
@@ -29,6 +29,6 @@
 VssPreprocessor {
 	samplesPerPass  100000
-	initialSamples 300000
-	vssSamples 1500000
+	initialSamples 1000000
+	vssSamples 0
 	vssSamplesPerPass 500000
 	useImportanceSampling true
@@ -52,5 +52,5 @@
 	maxDepth 	40
 	minPvs		30
-	minRays		400
+	minRays		800
 	minSize		0.001
  	maxCostRatio	1.5
@@ -170,6 +170,6 @@
 
 ViewCells {
-	loadFromFile true
-	exportToFile false
+	loadFromFile false
+	exportToFile true
 	#type kdTree
 	#type vspKdTree
@@ -182,7 +182,7 @@
 	maxViewCells 3000
 	#percentage of total visible objects where pvs is considered invalid
-	maxPvsRatio 1.0
-		
-	delayedConstruction true
+	maxPvsRatio 0.9
+		
+	delayedConstruction false
 		
 		
@@ -259,5 +259,5 @@
 VspBspTree {
 	Construction {
-		samples 900000
+		samples 500000
 		epsilon 0.005
 		randomize false
@@ -292,5 +292,5 @@
 	Termination {
 		# parameters used for autopartition
-		minRays 		100
+		minRays 		300
 		minPolygons 		-1
 		maxDepth		30
@@ -298,10 +298,10 @@
 		#minProbability		0.0001
 		minProbability 		-1
-		maxRayContribution	0.6
-		maxCostRatio		0.99
+		maxRayContribution	0.1
+		maxCostRatio		0.9
 		missTolerance		3
 		#maxAccRayLength	100
 		
-		maxViewCells		3501
+		maxViewCells		1000
 		
 		# used for pvs criterium
@@ -323,6 +323,6 @@
 	PostProcess {
 		maxCostRatio 0.1
-		minViewCells 200
-		useRaysForMerge false
+		minViewCells 100
+		useRaysForMerge true
 	}
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 556)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 557)
@@ -275,5 +275,5 @@
 	/** Merge view cells of leaves l1 and l2.
 	*/
-	bool MergeViewCells(BspLeaf *l1, BspLeaf *l2) const;
+	bool MergeViewCells(BspLeaf *l1, BspLeaf *l2); //const;
 
 	/** Returns true if this view point is in a valid view space,
@@ -721,4 +721,7 @@
 private:
 	
+	ViewCellContainer mOldViewCells;
+	ViewCellContainer mNewViewCells;
+
 	static const float sLeastRaySplitsTable[5];
 	/** Evaluates split plane classification with respect to the plane's 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 556)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 557)
@@ -301,7 +301,6 @@
 
 
-  virtual float GetViewSpaceVolume() {
-	return mViewSpaceBox.GetVolume()*(2.0f*sqr(M_PI));
-  }
+	virtual float GetViewSpaceVolume();
+	
 	/** Creates meshes from the view cells.
 	*/
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 556)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 557)
@@ -38,7 +38,9 @@
 bool BspMergeCandidate::sUseArea = false;
 
+
 /********************************************************************/
 /*                  class VspBspTree implementation                 */
 /********************************************************************/
+
 
 VspBspTree::VspBspTree():
@@ -498,5 +500,5 @@
 
 		mStat.contributingSamples += conSamp;
-		mStat.sampleContributions += sampCon;
+		mStat.sampleContributions +=(int) sampCon;
 
 		//-- store additional info
@@ -1915,4 +1917,5 @@
 typedef pair<BspNode *, BspNodeGeometry *> bspNodePair;
 
+
 int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
 						   const bool onlyUnmailed) const
@@ -1925,4 +1928,5 @@
 	// split planes from the root to this node
 	// needed to verify that we found neighbor leaf
+	// TODO: really needed?
 	vector<Plane3> halfSpaces;
 	ExtractHalfSpaces(n, halfSpaces);
@@ -1934,4 +1938,5 @@
 	nodeStack.push(bspNodePair(mRoot, rgeom));
 
+	Debug << "here77" << endl;
 
 	while (!nodeStack.empty())
@@ -1942,51 +1947,51 @@
 		nodeStack.pop();
 
-		bool isAdjacent = true;
-
-		
+		if (node->IsLeaf())
+		{	
+			Debug << "here22" << endl;
+			// test if this leaf is in valid view space
+			if (node->TreeValid() &&
+				(node != n) && 
+				(!onlyUnmailed || !node->Mailed()))
+			{
+				Debug << "here" << endl;
+				bool isAdjacent = true;
 #if 1
-		// test all planes of current node if still adjacent
-		for (int i = 0; (i < halfSpaces.size()) && isAdjacent && (node != n); ++ i)
-		{
-			const int cf =
-				Polygon3::ClassifyPlane(geom->mPolys,
-										halfSpaces[i],
-										mEpsilon);
-
-			if (cf == Polygon3::BACK_SIDE)
-			{
-				isAdjacent = false;
-			}
-		}
-#else
-		// test all planes of current node if still adjacent
-		for (int i = 0; i < nodeGeom.size()) && isAdjacent && (node != n); ++ i)
-		{
-			Polygon3 *poly = nodeGeom->mPolys[i];
-
-			const int cf =
-				Polygon3::ClassifyPlane(geom->mPolys,
-										poly->GetSupportingPlane(),
-										mEpsilon);
-
-			if (cf == Polygon3::BACK_SIDE)
-			{
-				isAdjacent = false;
-			}
-		}
+
+				// test all planes of current node if still adjacent
+				for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i)
+				{
+					const int cf =
+						Polygon3::ClassifyPlane(geom->mPolys,
+												halfSpaces[i],
+												mEpsilon);
+
+					if (cf == Polygon3::BACK_SIDE)
+					{
+						isAdjacent = false;
+					}
+				}
+#else // TODO: why is this wrong??
+				// test all planes of current node if still adjacent
+				for (int i = 0; (i < (int)nodeGeom.mPolys.size()) && isAdjacent; ++ i)
+				{
+					Debug << "here33" << endl;
+					Polygon3 *poly = nodeGeom.mPolys[i];
+
+					const int cf =
+						Polygon3::ClassifyPlane(geom->mPolys,
+												poly->GetSupportingPlane(),
+												mEpsilon);
+
+					if (cf == Polygon3::BACK_SIDE)
+					{
+						isAdjacent = false;
+					}
+				}
 #endif
-		if (isAdjacent)
-		{
-		
-			if (node->IsLeaf())
-			{	
-				// test if this leaf is in valid view space
-				if (node->TreeValid() &&
-					node != n && 
-					(!onlyUnmailed || !node->Mailed()))
-				{
-					// neighbor was found
+				Debug << "here2" << endl;
+				// neighbor was found
+				if (isAdjacent)
 					neighbors.push_back(dynamic_cast<BspLeaf *>(node));
-				}
 			}
 			else
@@ -2505,5 +2510,5 @@
 
 
-bool VspBspTree::MergeViewCells(BspLeaf *l1, BspLeaf *l2) const
+bool VspBspTree::MergeViewCells(BspLeaf *l1, BspLeaf *l2) //const
 {
 	//-- change pointer to view cells of all leaves associated
@@ -2545,6 +2550,19 @@
 
 	// clean up old view cells
-	DEL_PTR(fVc);
-	DEL_PTR(bVc);
+	if (0)
+	{
+		DEL_PTR(fVc);
+		DEL_PTR(bVc);
+	}
+	else // throw them into old view cells container
+	{
+		fVc->mMailbox = -1;
+		bVc->mMailbox = -1;
+
+		mOldViewCells.push_back(fVc);
+		mOldViewCells.push_back(bVc);
+
+		mNewViewCells.push_back(vc);
+	}
 
 	return true;
@@ -2764,8 +2782,14 @@
 	BspMergeCandidate::sUseArea = mUseAreaForPvs;
 
+	// the current view cells are kept in this container
+	ViewCellContainer viewCells;
+	ViewCell::NewMail();
+	CollectViewCells(mRoot, true, viewCells, true);
+	ViewCell::NewMail();
+
 	MergeStatistics mergeStats;
 	mergeStats.Start();
 	// TODO: REMOVE LATER for performance!
-	const bool showMergeStats = false;
+	const bool showMergeStats = true;
 	//BspMergeCandidate::sOverallCost = mBox.SurfaceArea() * mStat.maxPvs;
 	long startTime = GetTime();
@@ -2846,8 +2870,37 @@
 
 					cout << "exporting view cells ... ";
-					ViewCellContainer viewCells;	
-					CollectViewCells(mRoot, false, viewCells);
-			
-					
+								
+				
+					ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
+
+					Debug << "here2111 " << viewCells.size() << endl;
+					for (int i = 0; i < viewCells.size(); ++ i)
+					{
+						while (!viewCells.empty() && (viewCells.back()->mMailbox == -1))
+						{
+							Debug << "here244" << endl;
+							viewCells.pop_back();
+						}
+
+						// already merged view cell, put it to end of vector
+						if ((i < viewCells.size()) && (viewCells[i]->mMailbox == -1))
+						{
+							Debug << "here2" << endl;
+							swap(viewCells[i], viewCells.back());
+						}
+					}
+				
+					cout << "finished" << endl;
+					// not part of any leaf
+					CLEAR_CONTAINER(mOldViewCells);
+
+					int newVcSize = (int)mNewViewCells.size();
+					// add new view cells to container
+					while (!mNewViewCells.empty())
+					{
+						viewCells.push_back(mNewViewCells.back());
+						mNewViewCells.pop_back();
+					}
+
 					char s[64];
 					sprintf(s, "merged_viewcells%07d.x3d", nViewCells);
@@ -2860,6 +2913,15 @@
 						ViewCellContainer::const_iterator it, it_end = viewCells.end();
 		
+						int i = (int)viewCells.size();
 						for (it = viewCells.begin(); it != it_end; ++ it)
 						{
+							Material m = RandomMaterial();
+							if (i-- == newVcSize)
+							{
+								m.mDiffuseColor.r = 1;
+								m.mDiffuseColor.g = 0;
+								m.mDiffuseColor.b = 0;
+							}
+							exporter->SetForcedMaterial(m);
 							mViewCellsManager->ExportVcGeometry(exporter, *it);
 						}
@@ -2882,4 +2944,6 @@
 		}
 	}
+
+
 	cout << "finished merge" << endl;
 	mergeStats.mergeTime = TimeDiff(startTime, GetTime());
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 556)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 557)
@@ -678,5 +678,7 @@
   
   // cast view cell samples
-  while (1) {
+  while (samples < mVssSamples) 
+  {
+	
 	int num = mVssSamplesPerPass;
 	SimpleRayContainer rays;
@@ -720,5 +722,5 @@
 
 		// test if we really have enough rays
-		//if (constructionRays.size() >= nSamples)
+		//if (constructionRays.size() >= nSamples) 
 		mViewCellsManager->Construct(mObjects, constructionRays);
 	}
@@ -743,7 +745,6 @@
 	cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
 	cout<<"*****************************\n";
-	if (samples >= mVssSamples)
-	  break;
-	pass++;
+//	if (samples >= mVssSamples) break;
+	pass ++;
   }
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 547)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 557)
@@ -1759,16 +1759,17 @@
 }
 
+
 bool AxisAlignedBox3::GetRaySegment(const Ray &ray, 
 									float &minT, 
 									float &maxT) const
 {
-	maxT = 1e6;
+	maxT = 1e15;
 	minT = 0;
 	
-	// test with  bounding box
+	// test with bounding box
 	if (!GetMinMaxT(ray, &minT, &maxT))
 		return false;
 
-	if (minT < 0) // start ray from origin
+	if (minT < 0)
 		minT = 0;
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 556)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 557)
@@ -139,4 +139,10 @@
 
 
+float ViewCellsManager::GetViewSpaceVolume()
+{
+	return mViewSpaceBox.GetVolume()*(2.0f*sqr(M_PI));
+}
+
+
 bool ViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
 {
@@ -428,5 +434,5 @@
 	//hray.Init(ray.GetOrigin(), ray.GetDir(), Ray::LINE_SEGMENT);
 	if (!GetViewSpaceBox().GetRaySegment(hray, tmin, tmax) || (tmin > tmax))
-	return;
+		return;
 
 	Vector3 origin = hray.Extrap(tmin);
@@ -679,6 +685,6 @@
 {
 	// compute view cell area as subsititute for probability
-#if 0
-	return GetArea(viewCell) / GetViewSpaceBox().SurfaceArea();
+#if 1
+	return GetVolume(viewCell) / GetViewSpaceBox().GetVolume();
 #else
 	return GetArea(viewCell) / GetAccVcArea();
@@ -835,4 +841,5 @@
 		cout << "exporting view cells after merge ... ";
 		Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
+
 
 		if (exporter)
@@ -1888,5 +1895,5 @@
 	VssRayContainer savedRays;
 
-	Debug << "construction samples: " << mConstructionSamples << " rays: " << rays.size() << endl;
+	Debug << "construction samples: " << mConstructionSamples << " rays: " << (int)rays.size() << endl;
 	GetRaySets(rays, mConstructionSamples, constructionRays, &savedRays);
 
@@ -2176,8 +2183,66 @@
 				exporter->ExportRays(visRays, RgbColor(0, 1, 0));
 			}
-
+			
 			ExportViewCellsForViz(exporter);
 			delete exporter;
 		}
+	}
+
+	// export shuffled view cells
+	if (1)
+	{
+		cout << "exporting shuffled view cells ...";
+
+		Exporter *exporter = Exporter::GetExporter("shuffled_view_cells.x3d");
+		if (exporter)
+		{
+			if (1)
+			{
+				exporter->SetWireframe();
+				exporter->ExportBox(mViewSpaceBox);
+				exporter->SetFilled();
+			}
+
+			if (mExportGeometry)
+			{
+				exporter->ExportGeometry(objects);
+			}
+
+			ViewCellContainer::const_iterator vit, vit_end = mViewCells.end();
+
+			Material vm, lm;
+
+			for (vit = mViewCells.begin(); vit != mViewCells.end(); ++ vit)
+			{
+				BspViewCell *vc = dynamic_cast<BspViewCell *>(*vit);
+
+				vm = RandomMaterial();
+
+				lm = vm;
+					
+				vm.mDiffuseColor.r -= 0.3f;
+				vm.mDiffuseColor.g -= 0.3f;
+				vm.mDiffuseColor.b -= 0.3f;
+
+				vector<BspLeaf *>::const_iterator lit, lit_end = vc->mLeaves.end();
+
+				for (lit = vc->mLeaves.begin(); lit != lit_end; ++ lit)
+				{
+					BspLeaf *leaf = *lit;
+
+					if (leaf->Mailed())
+						exporter->SetForcedMaterial(lm);
+					else
+						exporter->SetForcedMaterial(vm);
+					
+					BspNodeGeometry geom;
+					mVspBspTree->ConstructGeometry(leaf, geom);
+					exporter->ExportPolygons(geom.mPolys);
+				}
+			}
+			
+			delete exporter;
+		}
+
 
 		cout << "finished" << endl;
@@ -2375,9 +2440,4 @@
 			importance = (float)bspVc->mLeaves.size() /
 				(float)mViewCellsStats.maxLeaves;
-
-			if (CheckValid(vc))
-				m.mDiffuseColor.b = 1.0f;
-			m.mDiffuseColor.r = importance;
-			m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r;
 		}
 		break;
@@ -2387,8 +2447,4 @@
 				(float)(mVspBspTree->GetStatistics().maxDepth * 2);
 
-			if (CheckValid(vc))
-				m.mDiffuseColor.b = 1.0f;
-			m.mDiffuseColor.r = importance;
-			m.mDiffuseColor.g = 1.0f - m.mDiffuseColor.r;
 		}
 		break;
