Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 422)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 429)
@@ -245,2 +245,269 @@
   return false;
 }
+
+
+void Preprocessor::ExportSplits(const ObjectContainer &objects,
+								const RayContainer &sampleRays,
+								const int visSamples)
+{
+	Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
+
+	if (exporter) 
+	{	
+		Material m; 
+		m.mDiffuseColor = RgbColor(1, 0, 0);
+		exporter->SetForcedMaterial(m);
+		exporter->SetWireframe();
+		exporter->ExportBspSplits(*mBspTree, true);
+
+		// take forced material, else big scenes cannot be viewed
+		m.mDiffuseColor = RgbColor(0, 1, 0);
+		exporter->SetForcedMaterial(m);
+		exporter->SetFilled();
+
+		exporter->ResetForcedMaterial();
+		
+		// export rays
+		if (0)
+		{
+			RayContainer outRays;
+		
+			for (int i = 0; i < sampleRays.size(); ++ i)
+			{
+				// only rays piercing geometry
+				if (!sampleRays[i]->intersections.empty())
+					outRays.push_back(sampleRays[i]);
+			}
+			if (BspTree::sConstructionMethod == BspTree::FROM_SAMPLES)
+			{
+				// export rays 
+				exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
+			}
+		}
+
+		if (1)
+			ExportSceneGeometry(exporter, objects);
+
+		delete exporter;
+	}
+}
+
+inline bool vc_gt(ViewCell *a, ViewCell *b) 
+{ 
+	return a->GetPvs().GetSize() > b->GetPvs().GetSize();
+}
+
+void Preprocessor::ExportBspPvs(const ObjectContainer &objects,
+								const RayContainer &sampleRays,
+								const int visSamples)
+{
+	const int leafOut = 10;
+	
+	ViewCell::NewMail();
+
+	//-- some rays for output
+	const int raysOut = min((int)sampleRays.size(), visSamples);
+	cout << "visualization using " << visSamples << " samples" << endl;
+	vector<Ray *> vcRays[leafOut];
+
+	if (0)
+	{
+		//-- some random view cells and rays for output
+		vector<BspLeaf *> bspLeaves;
+
+		for (int i = 0; i < leafOut; ++ i)
+			bspLeaves.push_back(mBspTree->GetRandomLeaf());	
+		
+		for (int i = 0; i < bspLeaves.size(); ++ i)
+		{
+			cout << "creating output for view cell " << i << " ... ";
+			// check whether we can add the current ray to the output rays
+			for (int k = 0; k < raysOut; ++ k) 
+			{
+				Ray *ray = sampleRays[k];
+
+				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+				{
+					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
+
+					if (bspLeaves[i]->GetViewCell() == leaf->GetViewCell()) 
+					{
+						vcRays[i].push_back(ray);
+					}
+				}
+			}
+
+			Intersectable::NewMail();
+
+			BspViewCell *vc = dynamic_cast<BspViewCell *>(bspLeaves[i]->GetViewCell());
+
+			//bspLeaves[j]->Mail();
+			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
+
+			Exporter *exporter = Exporter::GetExporter(s);
+			exporter->SetFilled();
+
+			ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
+
+			exporter->SetWireframe();
+			//exporter->SetFilled();
+
+			Material m;//= RandomMaterial();
+			m.mDiffuseColor = RgbColor(0, 1, 0);
+			exporter->SetForcedMaterial(m);
+
+			if (vc->GetMesh())
+				exporter->ExportViewCell(vc);
+			else
+			{
+				PolygonContainer cell;
+				// export view cell geometry
+				mBspTree->ConstructGeometry(vc, cell);
+				exporter->ExportPolygons(cell);
+				CLEAR_CONTAINER(cell);
+			}
+
+			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
+					<< ", piercing rays=" << (int)vcRays[i].size() << endl;
+
+			// export rays piercing this view cell
+			exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
+
+			m.mDiffuseColor = RgbColor(1, 0, 0);
+			exporter->SetForcedMaterial(m);
+
+			// exporter->SetWireframe();
+			exporter->SetFilled();
+
+			// output PVS of view cell
+			for (; it != vc->GetPvs().mEntries.end(); ++ it) 
+			{
+				Intersectable *intersect = (*it).first;
+				if (!intersect->Mailed())
+				{
+					exporter->ExportIntersectable(intersect);
+					intersect->Mail();
+				}			
+			}
+				
+			// output rest of the objects
+			if (0)
+			{
+				Material m;//= RandomMaterial();
+				m.mDiffuseColor = RgbColor(0, 0, 1);
+				exporter->SetForcedMaterial(m);
+
+				for (int j = 0; j < objects.size(); ++ j)
+					if (!objects[j]->Mailed())
+					{
+						exporter->SetForcedMaterial(m);
+						exporter->ExportIntersectable(objects[j]);
+						objects[j]->Mail();
+					}
+			}
+			DEL_PTR(exporter);
+			cout << "finished" << endl;
+		}
+	}
+	else
+	{
+		ViewCellContainer viewCells;
+
+		mBspTree->CollectViewCells(viewCells);
+		stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
+
+		int limit = min(leafOut, (int)viewCells.size()); 
+		
+		for (int i = 0; i < limit; ++ i)
+		{
+			cout << "creating output for view cell " << i << " ... ";
+			
+            Intersectable::NewMail();
+			BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
+
+			cout << "creating output for view cell " << i << " ... ";
+			// check whether we can add the current ray to the output rays
+			for (int k = 0; k < raysOut; ++ k) 
+			{
+				Ray *ray = sampleRays[k];
+
+				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+				{
+					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
+
+					if (vc == leaf->GetViewCell()) 
+					{
+						vcRays[i].push_back(ray);
+					}
+				}
+			}
+
+			//bspLeaves[j]->Mail();
+			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
+
+			Exporter *exporter = Exporter::GetExporter(s);
+			
+			exporter->SetWireframe();
+
+			Material m;//= RandomMaterial();
+			m.mDiffuseColor = RgbColor(0, 1, 0);
+			exporter->SetForcedMaterial(m);
+
+			if (vc->GetMesh())
+				exporter->ExportViewCell(vc);
+			else
+			{
+				PolygonContainer cell;
+				// export view cell
+				mBspTree->ConstructGeometry(vc, cell);
+				exporter->ExportPolygons(cell);
+				CLEAR_CONTAINER(cell);
+			}
+
+			
+			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
+					<< ", piercing rays=" << (int)vcRays[i].size() << endl;
+
+			
+			// export rays piercing this view cell
+			exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
+	
+			m.mDiffuseColor = RgbColor(1, 0, 0);
+			exporter->SetForcedMaterial(m);
+
+			ViewCellPvsMap::const_iterator it,
+				it_end = vc->GetPvs().mEntries.end();
+
+			// output PVS of view cell
+			for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it) 
+			{
+				Intersectable *intersect = (*it).first;
+				if (!intersect->Mailed())
+				{
+					Material m = RandomMaterial();
+			
+					exporter->SetForcedMaterial(m);
+
+					exporter->ExportIntersectable(intersect);
+					intersect->Mail();
+				}			
+			}
+				
+			DEL_PTR(exporter);
+			cout << "finished" << endl;
+		}
+	}
+}
+
+
+void Preprocessor::ExportSceneGeometry(Exporter *exporter, 
+									   const ObjectContainer &objects)
+{
+	Material m;//= RandomMaterial();
+	m.mDiffuseColor = RgbColor(0, 1, 0);
+	exporter->SetForcedMaterial(m);
+	exporter->SetWireframe();
+
+	for (int j = 0; j < objects.size(); ++ j)
+		exporter->ExportIntersectable(objects[j]);
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 428)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 429)
@@ -72,5 +72,5 @@
 			mOrigin = ray.GetLoc(); 
 
-		Debug << "origin: " << mOrigin << endl;
+		//Debug << "origin: " << mOrigin << endl;
 
 		if (!ray.intersections.empty())
@@ -81,6 +81,5 @@
 		else
 		{
-			//mTermination = 1e6;
-			mTermination = mOrigin + ray.GetDir() * 1e6;
+			mTermination = mOrigin + ray.GetDir() * 99999;
 			mTerminationObject = NULL;
 		}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 427)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 429)
@@ -24,5 +24,6 @@
   environment->GetIntValue("VssPreprocessor.vssSamples", mVssSamples);
   environment->GetIntValue("VssPreprocessor.vssSamplesPerPass", mVssSamplesPerPass);
-	environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
+  environment->GetBoolValue("VssPreprocessor.useImportanceSampling", mUseImportanceSampling);
+  environment->GetIntValue("BspTree.Construction.samples", mBspConstructionSamples);
 	
   mStats.open("stats.log");
@@ -353,4 +354,6 @@
 	VssTree *vssTree = NULL;
 
+	RayContainer bspRays;
+
   while (totalSamples < mInitialSamples) {
 		int passContributingSamples = 0;
@@ -422,7 +425,21 @@
 
 
-
 	vssTree = new VssTree;
 	
+	const int bspSamples = min((int)mVssRays.size(), mBspConstructionSamples);
+	
+	for (int i = 0; i < bspSamples; ++ i)
+		bspRays.push_back(new Ray(*mVssRays[i]));
+
+	mBspTree = new BspTree(&mUnbounded);	
+
+	mBspTree->SetGenerateViewCells(true);
+	mBspTree->Construct(bspRays);
+
+	for (int i = bspSamples; i < (int)mVssRays.size(); ++ i)
+	{
+		CastRay(*mBspTree, *mVssRays[i]);
+	}
+
 	vssTree->Construct(mVssRays, mViewSpaceBox);
 
@@ -469,4 +486,9 @@
 		}
 
+		
+		for (int i = 0; i < (int)vssRays.size(); ++ i)
+		{
+			CastRay(*mBspTree, *mVssRays[i]);
+		}
 		
 		samples+=num;
@@ -482,6 +504,58 @@
 
 	delete vssTree;
-	
-  return true;
-}
-
+
+	ObjectContainer objects;
+	ExportSplits(objects, bspRays, 10000);
+	ExportBspPvs(objects, bspRays, 10000);
+
+	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 (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/SamplingPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 428)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 429)
@@ -83,12 +83,4 @@
 	bool BuildBspTree();
 
-	void ExportSplits(const ObjectContainer &objects);
-
-	void ExportBspPvs(const ObjectContainer &objects);
-
-	/** Export scene geometry.
-	*/
-	void ExportSceneGeometry(Exporter *exporter, const ObjectContainer &objects);
-
 	/** Post processes view cells (i.e., merges or subdivides view cells based
 		on the PVS and the ray sets.
@@ -111,5 +103,5 @@
 	RayContainer mSampleRays;
 	VssRayContainer mVspSampleRays;
-	int mBspConstructionSamples;
+	
 	int mVspConstructionSamples;
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 419)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 429)
@@ -13,4 +13,5 @@
 class RenderSimulator;
 class SceneGraph;
+class Exporter;
 
 /** Namespace for the external visibility preprocessor
@@ -92,4 +93,5 @@
   bool ParseViewCellsOptions();
 
+
   /// scene graph loaded from file
   SceneGraph *mSceneGraph;
@@ -117,4 +119,19 @@
   */
   VspKdTree *mVspKdTree;
+
+protected:
+  void ExportBspPvs(const ObjectContainer &objects,
+					const RayContainer &sampleRays,
+					const int visSamples);
+
+  void ExportSplits(const ObjectContainer &objects,
+					const RayContainer &sampleRays,
+					const int visSamples);
+
+  /** Export scene geometry.
+  */
+  void ExportSceneGeometry(Exporter *exporter, const ObjectContainer &objects);
+
+  int mBspConstructionSamples;
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 428)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 429)
@@ -122,4 +122,17 @@
 
 
+void
+SamplingPreprocessor::HoleSamplingPass()
+{
+  vector<KdLeaf *> leaves;
+  mKdTree->CollectLeaves(leaves);
+  
+  // go through all the leaves and evaluate their passing contribution
+  for (int i=0 ; i < leaves.size(); i++) {
+    KdLeaf *leaf = leaves[i];
+    cout<<leaf->mPassingRays<<endl;
+  }
+}
+
 int SamplingPreprocessor::AddObjectSamples(Intersectable *obj, const Ray &ray)
 {
@@ -149,18 +162,4 @@
  
 	return contributingSamples;
-}
-
-
-void
-SamplingPreprocessor::HoleSamplingPass()
-{
-  vector<KdLeaf *> leaves;
-  mKdTree->CollectLeaves(leaves);
-  
-  // go through all the leaves and evaluate their passing contribution
-  for (int i=0 ; i < leaves.size(); i++) {
-    KdLeaf *leaf = leaves[i];
-    cout<<leaf->mPassingRays<<endl;
-  }
 }
 
@@ -450,5 +449,5 @@
 							direction = point - pointToSample;
 							point = pointToSample;
-							Debug << "point: " << pointToSample << endl;
+							//Debug << "point: " << pointToSample << endl;
 						}
 					}
@@ -650,10 +649,10 @@
 		cout << "exporting splits ... ";
 		if (exportSplits)
-	  		ExportSplits(objects);
+	  		ExportSplits(objects, mSampleRays, mVisualizationSamples);
 		cout << "finished" << endl;
 
 		// export the PVS of sample view cells
 		if (1)
-			ExportBspPvs(objects);
+			ExportBspPvs(objects, mSampleRays, mVisualizationSamples);
 	}
 	
@@ -731,6 +730,11 @@
 
 				for (it = vssRays.begin(); it != it_end; ++ it)
-					if (!(*it)->mOriginObject)
-						rays.push_back(*it);
+				{
+					//if (!(*it)->mOriginObject && !(*it)->mTerminationObject)
+                        rays.push_back(*it);
+
+					//if (!(*it)->mOriginObject && !(*it)->mTerminationObject)
+					//	Debug << "ERR: " << (*it)->mOrigin << " " << (*it)->mTermination << endl;
+				}
 
 				exporter->ExportRays(rays, RgbColor(1, 0, 0));
@@ -816,5 +820,6 @@
 	{
 		// store samples for vsp kd tree construction
-		if ((int)mVspSampleRays.size() < mVspConstructionSamples)
+		if ((object || !ray.intersections.empty()) && 
+			((int)mVspSampleRays.size() < mVspConstructionSamples))
 		{
 			ray.sourceObject = Ray::Intersection(0.0, object, faceIndex);
@@ -936,262 +941,2 @@
 	return merged;
 }
-
-// export scene geometry
-void SamplingPreprocessor::ExportSceneGeometry(Exporter *exporter, const ObjectContainer &objects)
-{
-	Material m;//= RandomMaterial();
-	m.mDiffuseColor = RgbColor(0, 1, 0);
-	exporter->SetForcedMaterial(m);
-	exporter->SetWireframe();
-
-	for (int j = 0; j < objects.size(); ++ j)
-		exporter->ExportIntersectable(objects[j]);
-}
-
-void SamplingPreprocessor::ExportSplits(const ObjectContainer &objects)
-{
-	Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
-
-	if (exporter) 
-	{	
-		Material m; 
-		m.mDiffuseColor = RgbColor(1, 0, 0);
-		exporter->SetForcedMaterial(m);
-		exporter->SetWireframe();
-		exporter->ExportBspSplits(*mBspTree, true);
-
-		// take forced material, else big scenes cannot be viewed
-		m.mDiffuseColor = RgbColor(0, 1, 0);
-		exporter->SetForcedMaterial(m);
-		exporter->SetFilled();
-
-		exporter->ResetForcedMaterial();
-		
-		// export rays
-		if (0)
-		{
-			RayContainer outRays;
-		
-			for (int i = 0; i < mSampleRays.size(); ++ i)
-			{
-				// only rays piercing geometry
-				if (!mSampleRays[i]->intersections.empty())
-					outRays.push_back(mSampleRays[i]);
-			}
-			if (BspTree::sConstructionMethod == BspTree::FROM_SAMPLES)
-			{
-				// export rays 
-				exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
-			}
-		}
-
-		if (1)
-			ExportSceneGeometry(exporter, objects);
-
-		delete exporter;
-	}
-}
-
-inline bool vc_gt(ViewCell *a, ViewCell *b) 
-{ 
-	return a->GetPvs().GetSize() > b->GetPvs().GetSize();
-}
-
-void SamplingPreprocessor::ExportBspPvs(const ObjectContainer &objects)
-{
-	const int leafOut = 10;
-	
-	ViewCell::NewMail();
-
-	//-- some rays for output
-	const int raysOut = min((int)mSampleRays.size(), mVisualizationSamples);
-	cout << "visualization using " << mVisualizationSamples << " samples" << endl;
-	vector<Ray *> vcRays[leafOut];
-
-	if (0){
-		//-- some random view cells and rays for output
-		vector<BspLeaf *> bspLeaves;
-
-		for (int i = 0; i < leafOut; ++ i)
-			bspLeaves.push_back(mBspTree->GetRandomLeaf());	
-		
-		for (int i = 0; i < bspLeaves.size(); ++ i)
-		{
-			cout << "creating output for view cell " << i << " ... ";
-			// check whether we can add the current ray to the output rays
-			for (int k = 0; k < raysOut; ++ k) 
-			{
-				Ray *ray = mSampleRays[k];
-
-				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
-				{
-					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
-
-					if (bspLeaves[i]->GetViewCell() == leaf->GetViewCell()) 
-					{
-						vcRays[i].push_back(ray);
-					}
-				}
-			}
-
-			Intersectable::NewMail();
-
-			BspViewCell *vc = dynamic_cast<BspViewCell *>(bspLeaves[i]->GetViewCell());
-
-			//bspLeaves[j]->Mail();
-			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
-
-			Exporter *exporter = Exporter::GetExporter(s);
-			exporter->SetFilled();
-
-			ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
-
-			exporter->SetWireframe();
-			//exporter->SetFilled();
-
-			Material m;//= RandomMaterial();
-			m.mDiffuseColor = RgbColor(0, 1, 0);
-			exporter->SetForcedMaterial(m);
-
-			if (vc->GetMesh())
-				exporter->ExportViewCell(vc);
-			else
-			{
-				PolygonContainer cell;
-				// export view cell geometry
-				mBspTree->ConstructGeometry(vc, cell);
-				exporter->ExportPolygons(cell);
-				CLEAR_CONTAINER(cell);
-			}
-
-			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
-					<< ", piercing rays=" << (int)vcRays[i].size() << endl;
-
-			// export rays piercing this view cell
-			exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
-
-			m.mDiffuseColor = RgbColor(1, 0, 0);
-			exporter->SetForcedMaterial(m);
-
-			// exporter->SetWireframe();
-			exporter->SetFilled();
-
-			// output PVS of view cell
-			for (; it != vc->GetPvs().mEntries.end(); ++ it) 
-			{
-				Intersectable *intersect = (*it).first;
-				if (!intersect->Mailed())
-				{
-					exporter->ExportIntersectable(intersect);
-					intersect->Mail();
-				}			
-			}
-				
-			// output rest of the objects
-			if (0)
-			{
-				Material m;//= RandomMaterial();
-				m.mDiffuseColor = RgbColor(0, 0, 1);
-				exporter->SetForcedMaterial(m);
-
-				for (int j = 0; j < objects.size(); ++ j)
-					if (!objects[j]->Mailed())
-					{
-						exporter->SetForcedMaterial(m);
-						exporter->ExportIntersectable(objects[j]);
-						objects[j]->Mail();
-					}
-			}
-			DEL_PTR(exporter);
-			cout << "finished" << endl;
-		}
-	}
-	else
-	{
-		ViewCellContainer viewCells;
-
-		mBspTree->CollectViewCells(viewCells);
-		stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
-
-		int limit = min(leafOut, (int)viewCells.size()); 
-		
-		for (int i = 0; i < limit; ++ i)
-		{
-			cout << "creating output for view cell " << i << " ... ";
-			
-            Intersectable::NewMail();
-			BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
-
-			cout << "creating output for view cell " << i << " ... ";
-			// check whether we can add the current ray to the output rays
-			for (int k = 0; k < raysOut; ++ k) 
-			{
-				Ray *ray = mSampleRays[k];
-
-				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
-				{
-					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
-
-					if (vc == leaf->GetViewCell()) 
-					{
-						vcRays[i].push_back(ray);
-					}
-				}
-			}
-
-			//bspLeaves[j]->Mail();
-			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
-
-			Exporter *exporter = Exporter::GetExporter(s);
-			
-			exporter->SetWireframe();
-
-			Material m;//= RandomMaterial();
-			m.mDiffuseColor = RgbColor(0, 1, 0);
-			exporter->SetForcedMaterial(m);
-
-			if (vc->GetMesh())
-				exporter->ExportViewCell(vc);
-			else
-			{
-				PolygonContainer cell;
-				// export view cell
-				mBspTree->ConstructGeometry(vc, cell);
-				exporter->ExportPolygons(cell);
-				CLEAR_CONTAINER(cell);
-			}
-
-			
-			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
-					<< ", piercing rays=" << (int)vcRays[i].size() << endl;
-
-			
-			// export rays piercing this view cell
-			exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
-	
-			m.mDiffuseColor = RgbColor(1, 0, 0);
-			exporter->SetForcedMaterial(m);
-
-			ViewCellPvsMap::const_iterator it,
-				it_end = vc->GetPvs().mEntries.end();
-
-			// output PVS of view cell
-			for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it) 
-			{
-				Intersectable *intersect = (*it).first;
-				if (!intersect->Mailed())
-				{
-					Material m = RandomMaterial();
-			
-					exporter->SetForcedMaterial(m);
-
-					exporter->ExportIntersectable(intersect);
-					intersect->Mail();
-				}			
-			}
-				
-			DEL_PTR(exporter);
-			cout << "finished" << endl;
-		}
-	}
-}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 427)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 429)
@@ -85,5 +85,7 @@
 	ExportVssTreeLeaves(VssTree *tree, const int number);
 	
+	void CastRay(const BspTree &tree, const VssRay & vssRay);
 	
+	int AddObjectSamples(Intersectable *obj, const Ray &ray);
 };
 
