Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 569)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 570)
@@ -26,4 +26,5 @@
 class Exporter;
 class Beam;
+class Preprocessor;
 
 struct BspRay;
@@ -55,7 +56,7 @@
 
 	/** Constructs view cells container taking a preprocessor
-		@returns construction rays.
-	*/
-	int Construct(VssRayContainer &rays);
+		@returns the output rays (if not null)
+	*/ 
+	int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
 
 	/** Constructs view cell container with a given number of samples.
@@ -411,5 +412,4 @@
 				  const VssRayContainer &rays);
 
-
 	int PostProcess(const ObjectContainer &objects, 
 					const VssRayContainer &rays);
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 568)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 570)
@@ -1980,5 +1980,5 @@
 				bool isAdjacent = true;
 
-			/*	if (1)
+				if (1)
 				{
 					// test all planes of current node if still adjacent
@@ -2014,5 +2014,5 @@
 						}
 					}
-				}*/
+				}
 				// neighbor was found
 				if (isAdjacent)
@@ -3115,21 +3115,4 @@
 	queue<BspMergeCandidate> *backQueue = &queue2;
 
-#if 1
-    Exporter *exporter = Exporter::GetExporter("neighors.x3d");
-
-	if (exporter)
-	{
-		cout << "exporting neighbors ... ";
-		exporter->ExportGeometry(objects);
-	}
-
-	// HACK for visualization
-	//ViewCellContainer viewCells;
-	//ViewCell::NewMail();
-	//CollectViewCells(mRoot, true, viewCells, true);
-	//for (int i = 0; i < viewCells.size(); ++i)
-	//	viewCells[i]->SetId((int)RandomValue(0, Real(256*256*256)));
-	
-	
 	while (!mMergeQueue.empty())
 	{
@@ -3137,48 +3120,6 @@
 		shuffleQueue->push(mc);
 		mMergeQueue.pop();
-
-		// visualize neighbors
-		Material m = RandomMaterial();
-		exporter->SetForcedMaterial(m);
-	
-
-		if (!mc.GetLeaf1()->Mailed())
-		{
-			BspNodeGeometry geom1;
-			ConstructGeometry(mc.GetLeaf1(), geom1);
-			//m.mDiffuseColor.r = (mc.GetLeaf1()->GetViewCell()->GetId() & 256)/ 255.0f;
-			//m.mDiffuseColor.g = ((mc.GetLeaf1()->GetViewCell()->GetId()>>8) & 256)/ 255.0f;
-			//m.mDiffuseColor.b = ((mc.GetLeaf1()->GetViewCell()->GetId()>>16) & 256)/ 255.0f;
-			//exporter->SetForcedMaterial(m);
-			exporter->ExportPolygons(geom1.mPolys);
-			mc.GetLeaf1()->Mail();
-		}
-
-		if (!mc.GetLeaf2()->Mailed())
-		{
-			BspNodeGeometry geom2;
-			ConstructGeometry(mc.GetLeaf2(), geom2);
-			//m.mDiffuseColor.r = (mc.GetLeaf2()->GetViewCell()->GetId() & 256)/ 255.0f;
-			//m.mDiffuseColor.g = ((mc.GetLeaf2()->GetViewCell()->GetId()>>8) & 256)/ 255.0f;
-			//m.mDiffuseColor.b = ((mc.GetLeaf2()->GetViewCell()->GetId()>>16) & 256)/ 255.0f;
-			//exporter->SetForcedMaterial(m);
-			exporter->ExportPolygons(geom2.mPolys);
-			mc.GetLeaf2()->Mail();
-		}		
-	}
-
-	if (exporter)
-	{
-		cout << "finished" << endl;
-		delete exporter;
-	}
-#else
-	while (!mMergeQueue.empty())
-	{
-		BspMergeCandidate mc = mMergeQueue.top();
-		shuffleQueue->push(mc);
-		mMergeQueue.pop();
-	}
-#endif
+	}
+
 	const int numPasses = 5;
 	int pass = 0;
@@ -3257,5 +3198,4 @@
 	//const int pvs1 = SubtractedPvsSize(vc1, leaf, *leaf->mPvs);
 	const int pvs1 = SubtractedPvsSize(vc1->GetPvs(), *leaf->mPvs);
-	
 	const int pvs2 = AddedPvsSize(vc2->GetPvs(), *leaf->mPvs);
 
@@ -3277,6 +3217,6 @@
 	}
 
-	const float cost1 = (float)pvs1 * p1;
-	const float cost2 = (float)pvs2 * p2;
+	const float cost1 = (float)pvs1;// * p1;
+	const float cost2 = (float)pvs2;// * p2;
 
 	return cost1 + cost2;
@@ -3330,8 +3270,8 @@
 	BspViewCell *vc2 = leaf2->GetViewCell();
 
-	float cost1; 
-	float cost2; 
-
-	if (mUseAreaForPvs)
+	float cost1 = vc1->GetPvs().GetSize(); 
+	float cost2 = vc2->GetPvs().GetSize(); 
+
+	/*if (mUseAreaForPvs)
 	{
 		cost1 = vc1->GetPvs().GetSize() * vc1->GetArea();
@@ -3342,5 +3282,5 @@
 		cost1 = vc1->GetPvs().GetSize() * vc1->GetVolume();
 		cost2 = vc2->GetPvs().GetSize() * vc2->GetVolume();
-	}
+	}*/
 
 	const float oldCost = cost1 + cost2;
@@ -3450,4 +3390,5 @@
 }
 
+
 /************************************************************************/
 /*                BspMergeCandidate implementation                      */
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 556)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 570)
@@ -21,5 +21,5 @@
 #define USE_EXE_PATH false
 
-
+#include <crtdbg.h>
 
 
@@ -28,4 +28,14 @@
 main(int argc, char **argv)
 {
+	
+//Now just call this function at the start of your program and if you're
+//compiling in debug mode (F5), any leaks will be displayed in the Output
+//window when the program shuts down. If you're not in debug mode this will
+//be ignored. Use it as you will!
+//note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
+    _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
+    _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
+    _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR); 
+
   Debug.open("debug.log");
   environment = new Environment;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 569)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 570)
@@ -88,33 +88,69 @@
 
 
-int ViewCellsManager::Construct(VssRayContainer &rays)
-{
-/*	VssPreprocessor preprocessor;
-
-	VssRayContainer vssRays;
-	preprocessor->GenerateVssRays(mConstructionSamples, vssRays);
-	Construct(preprocessor->mObjects, vssRays);
-
-	// add rays to output rays
-	while (!vssRays.empty)
-	{
-		rays.push_back(vssRays.back());
-		vssRays.pop_back();
-	}
-
-	const int importanceRays = 1000000;
-	preprocessor->GenerateVssRays(max(importanceRays, mPostProcessSamples);
-
-	PostProcess(preprocesor->mObjects, importanceRays);
+int ViewCellsManager::Construct(Preprocessor *preprocessor, VssRayContainer *outRays)
+{
+	SimpleRayContainer simpleRays;
+	VssRayContainer constructionRays;
+
+	preprocessor->GenerateRays(mConstructionSamples,
+							   Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION,
+							   simpleRays);
+
+	Construct(preprocessor->mObjects, constructionRays);
+
+	int numRays = (int)simpleRays.size();
+
+	static Ray traversalRay;
+
+	// shoot simple ray and add it to construction rays
+	while (!simpleRays.empty())
+	{
+		SimpleRay sray = simpleRays.back();
+
+		// TODO: shoot into kdtree
+		constructionRays.push_back(new VssRay(traversalRay));
+
+		if (outRays)
+			outRays->push_back(constructionRays.back());
+
+		simpleRays.pop_back();
+	}
+
+	if (!outRays)
+		CLEAR_CONTAINER(constructionRays);
+
+	// guided rays
+	VssRayContainer importanceRays;
+	const int desiredImportanceRays = max(1000000, mPostProcessSamples);
+	preprocessor->GenerateRays(desiredImportanceRays,
+							   Preprocessor::RSS_BASED_DISTRIBUTION,
+							   simpleRays);
+
+	numRays += (int)simpleRays.size();
+
+	// shoot simple ray and add it to construction rays
+	while (!simpleRays.empty())
+	{
+		SimpleRay sray = simpleRays.back();
+
+		// TODO: shoot into kdtree
+		importanceRays.push_back(new VssRay(traversalRay));
+
+		if (outRays)
+			outRays->push_back(importanceRays.back());
+
+		simpleRays.pop_back();
+	}
+
+	if (!outRays)
+		CLEAR_CONTAINER(importanceRays);
+
+	// merge the view cells
+	PostProcess(preprocessor->mObjects, importanceRays);
+
 	Visualize(preprocessor->mObjects, importanceRays);
 
-	// add rays to output rays
-	while (!importanceRays.empty)
-	{
-		rays.push_back(importanceRays.back());
-		importanceRays.pop_back();
-	}
-*/
-	return (int)rays.size();
+
+	return numRays;
 }
 
@@ -130,5 +166,5 @@
 	  return false;
 	}
-  
+
   return true;
 }
@@ -144,9 +180,9 @@
 void
 ViewCellsManager::SetValidity(
-							  int minPvsSize, 
+							  int minPvsSize,
 							  int maxPvsSize) const
 {
   ViewCellContainer::const_iterator it, it_end = mViewCells.end();
-  
+
   for (it = mViewCells.begin(); it != it_end; ++ it) {
 	SetValidity(*it, minPvsSize, maxPvsSize);
@@ -161,8 +197,8 @@
 {
   sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
-  
+
   int start = mViewCells.size()*minValid;
   int end = mViewCells.size()*maxValid;
-  
+
   for (int i=0; i < mViewCells.size(); i++)
 	mViewCells[i]->SetValid(i >= start && i <= end);
@@ -542,15 +578,13 @@
 }
 
-float
-ViewCellsManager::ComputeSampleContributions(VssRay &ray,
-											 const bool addRays
-											 )
-{
-  ViewCellContainer viewcells;
-
-  ray.mPvsContribution = 0;
-  ray.mRelativePvsContribution = 0.0f;
-
- static Ray hray;
+float ViewCellsManager::ComputeSampleContributions(VssRay &ray,
+												   const bool addRays)
+{
+	ViewCellContainer viewcells;
+
+	ray.mPvsContribution = 0;
+	ray.mRelativePvsContribution = 0.0f;
+
+	static Ray hray;
 	hray.Init(ray);
 	//hray.mFlags |= Ray::CULL_BACKFACES;
@@ -567,18 +601,15 @@
 	CastLineSegment(origin, termination, viewcells);
 	//Debug << "constribution: " << (int)viewcells.size() << endl;
-	CastLineSegment(origin, termination, viewcells);
-
-  //Debug << "constribution: " << (int)viewcells.size() << endl;
-
-  // copy viewcells memory efficiently
-  const bool storeViewcells = !addRays;
-
-  if (storeViewcells)
-  {
-	ray.mViewCells.reserve(viewcells.size());
-	ray.mViewCells = viewcells;
-  }
-
-  ViewCellContainer::const_iterator it = viewcells.begin();
+
+	// copy viewcells memory efficiently
+	const bool storeViewcells = !addRays;
+
+	if (storeViewcells)
+	{
+		ray.mViewCells.reserve(viewcells.size());
+		ray.mViewCells = viewcells;
+	}
+
+	ViewCellContainer::const_iterator it = viewcells.begin();
 
 
@@ -605,6 +636,6 @@
 	  }
 	}
-  
-  return ray.mRelativePvsContribution;
+
+	return ray.mRelativePvsContribution;
 }
 
@@ -1568,4 +1599,5 @@
 }
 
+
 int VspKdViewCellsManager::Construct(const ObjectContainer &objects,
 									 const VssRayContainer &rays)
@@ -1934,6 +1966,10 @@
 }
 
-
-
+<<<<<<< .mine
+
+=======
+
+
+>>>>>>> .r569
 int VspBspViewCellsManager::Construct(const ObjectContainer &objects,
 									  const VssRayContainer &rays)
@@ -2268,5 +2304,5 @@
   // validy update in preprocessor for all managers)
   return ViewCellsManager::ViewPointValid(viewPoint);
-  
+
   //	return mViewSpaceBox.IsInside(viewPoint) &&
   //		   mVspBspTree->ViewPointValid(viewPoint);
@@ -2508,4 +2544,5 @@
 			importance = (float)vc->GetPvs().GetSize() /
 				(float)mViewCellsStats.maxPvs;
+
 		}
 		break;
