Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 441)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 444)
@@ -112,5 +112,5 @@
 		minPvs 10
 		maxPvs 150
-		samples 100000
+		samples 10
 	}
 
@@ -167,5 +167,5 @@
 	maxRayCandidates 50
 	
-	maxTests 500
+	maxTests 10000
 	
 	# factors for evaluating split plane costs
@@ -184,8 +184,8 @@
 	Termination {
 		# parameters used for autopartition
-		minRays 300
+		minRays 200
 		minPolygons -1
-		maxDepth 40
-		minPvs 200
+		maxDepth 50
+		minPvs 100
 		minArea 0.01
 		maxRayContribution 0.005
@@ -214,5 +214,5 @@
 		exportSplits true
 		# how much samples should be used in visualization
-		samples 20000
+		samples 90000
 	}
 }
@@ -255,5 +255,5 @@
 	
 	Termination {
-		maxDepth	 	30
+		maxDepth	 	40
 		minPvs			50
 		minRays			1
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 443)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 444)
@@ -335,6 +335,5 @@
 	void AddToPvs(const BoundedRayContainer &rays, 
 				  int &sampleContributions,
-				  int &contributingSamples, 
-				  bool storeLeavesWithRays = false);
+				  int &contributingSamples);
 
 protected:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 443)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 444)
@@ -517,6 +517,6 @@
 		 (data.mPvs <= mTermMinPvs) ||
 		 (data.mArea <= mTermMinArea) ||
-		 (data.mDepth >= mTermMaxDepth) ||
-		 (data.GetAvgRayContribution() < mTermMaxRayContribution));
+		 (data.GetAvgRayContribution() >= mTermMaxRayContribution) ||
+		 (data.mDepth >= mTermMaxDepth));
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 442)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 444)
@@ -153,6 +153,5 @@
 	*/
 	virtual bool ViewCellsConstructed() const = 0;
-	
-	
+		
 
 protected:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h	(revision 438)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h	(revision 444)
@@ -432,5 +432,5 @@
 
   virtual void Print(ostream &s) const {
-    s<<endl<<"L: r="<<rays.size()<<endl;
+    s<<endl<<"L: r="<<(int)rays.size()<<endl;
   };
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 442)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 444)
@@ -226,6 +226,5 @@
 void BspLeaf::AddToPvs(const BoundedRayContainer &rays, 
 					   int &sampleContributions,
-					   int &contributingSamples,
-					   bool storeLeavesWithRays)
+					   int &contributingSamples)
 {
 	sampleContributions = 0;
@@ -246,12 +245,11 @@
 			contribution += mViewCell->GetPvs().AddSample(ray->sourceObject.mObject);
 
-		if (contribution > 0)
+		if (contribution)
 		{
 			sampleContributions += contribution;
 			++ contributingSamples;
 		}
-		
-		if (storeLeavesWithRays)
-			// warning: intersections not ordered
+
+		//if (ray->mFlags & Ray::STORE_BSP_INTERSECTIONS)
 			ray->bspIntersections.push_back(Ray::BspIntersection((*it)->mMinT, this));
 	}
@@ -632,5 +630,5 @@
 
 	// generate view cells
-	mGenerateViewCells = false;
+	mGenerateViewCells = true;
 
 	long startTime = GetTime();
@@ -1452,5 +1450,5 @@
 
 	// bound ray or line segment
-	if ((ray.GetType() == Ray::LOCAL_RAY) && 
+	if (//(ray.GetType() == Ray::LOCAL_RAY) && 
 	    !ray.intersections.empty() && 
 		(ray.intersections[0].mT <= maxT))
@@ -2072,6 +2070,7 @@
 				const float newT = t * bRay->mMaxT;
 
-				backRays.push_back(new BoundedRay(ray, bRay->mMinT, newT));
-				frontRays.push_back(new BoundedRay(ray, newT, bRay->mMaxT));
+				backRays.push_back(new BoundedRay(ray, minT, newT));
+				frontRays.push_back(new BoundedRay(ray, newT, maxT));
+
 				DEL_PTR(bRay);
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 441)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 444)
@@ -210,4 +210,5 @@
 	mViewCellsManager->SetVisualizationSamples(visSamples);
 
+	//Debug << "Visualization samples: " << mViewCellsManager->GetVisualizationSamples() << endl;
 
 	//-- parse view cells construction method
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp	(revision 441)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp	(revision 444)
@@ -222,5 +222,6 @@
 Ray::Ray(const VssRay &vssRay):
 loc(vssRay.mOrigin),
-sourceObject(0, vssRay.mOriginObject, 0)
+sourceObject(0, vssRay.mOriginObject, 0),
+mType(LOCAL_RAY)
 {
 	const float dist = Distance(vssRay.mTermination, vssRay.mOrigin);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 441)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 444)
@@ -68,24 +68,23 @@
 	mOriginObject(ray.sourceObject.mObject),
 	mT(1.0f)
-  {
-	if (ray.sourceObject.mObject)
-	  mOrigin = ray.Extrap(ray.sourceObject.mT);
-	else
-	  mOrigin = ray.GetLoc(); 
-
-	//Debug << "origin: " << mOrigin << endl;
-
-	if (!ray.intersections.empty())
 	{
-		mTermination = ray.Extrap(ray.intersections[0].mT);
-		mTerminationObject = ray.intersections[0].mObject;
-	}
-	else
-	{
-		mTermination = ray.Extrap(1e6);//TODO: should be Limits::Infinity
-		mTerminationObject = NULL;
-	}
-
-	Precompute();
+		if (ray.sourceObject.mObject)
+			mOrigin = ray.Extrap(ray.sourceObject.mT);
+		else
+			mOrigin = ray.GetLoc(); 
+
+		//Debug << "origin: " << mOrigin << endl;
+		if (!ray.intersections.empty())
+		{
+			mTermination = ray.Extrap(ray.intersections[0].mT);
+			mTerminationObject = ray.intersections[0].mObject;
+		}
+		else
+		{
+			mTermination = ray.Extrap(1e6);//TODO: should be Limits::Infinity
+			mTerminationObject = NULL;
+		}
+
+		Precompute();
   }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 440)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 444)
@@ -80,6 +80,6 @@
 bool
 X3dExporter::ExportRays(const RayContainer &rays,
-												const float length,
-												const RgbColor &color)
+						const float length,
+						const RgbColor &color)
 {
   RayContainer::const_iterator ri = rays.begin();
@@ -130,5 +130,5 @@
 bool
 X3dExporter::ExportRays(const VssRayContainer &rays,
-												const RgbColor &color)
+						const RgbColor &color)
 {
   VssRayContainer::const_iterator ri = rays.begin();
@@ -154,9 +154,9 @@
   ri = rays.begin();
   for (; ri != rays.end(); ri++) {
-    Vector3 a = (*ri)->GetOrigin();
-		Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize(b - a);
+    const Vector3 a = (*ri)->GetOrigin();
+	const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
 	
     stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
-		stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
+	stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
   }
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 441)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 444)
@@ -32,12 +32,13 @@
 							   const Ray::Intersection &origin)
 {
-  ray.intersections.clear();
-  ray.kdLeaves.clear();
-  ray.testedObjects.clear();
-  ray.bspIntersections.clear();
+	ray.intersections.clear();
+	ray.kdLeaves.clear();
+	ray.testedObjects.clear();
+	ray.bspIntersections.clear();
   
 	ray.mFlags |= Ray::STORE_KDLEAVES | Ray::STORE_BSP_INTERSECTIONS;
-  //  cout<<point<<" "<<direction<<endl;
-  ray.Init(point, direction, type);
+	//  cout<<point<<" "<<direction<<endl;
+
+	ray.Init(point, direction, type);
 	ray.sourceObject = origin;
 }
@@ -319,10 +320,8 @@
 					Ray *ray = new Ray();
 
-					// the source object
-					Ray::Intersection source = 
-						Ray::Intersection(0, reverseSample ? NULL : object, faceIndex);
-			
 					// construct a ray
-					SetupRay(*ray, point, direction, Ray::LOCAL_RAY, source);
+					SetupRay(*ray, point, direction, Ray::LOCAL_RAY, 
+							 Ray::Intersection(0, reverseSample ? NULL : object, faceIndex));
+
 					passRays.push_back(ray);
 					
@@ -428,17 +427,15 @@
 		{
 			for (it = newRays.begin(); it != it_end; ++ it)
+			{
 				mVssSampleRays.push_back(new VssRay(*(*it)));
+			}
 		}
 		else
 		{
+			// construct view cells using the collected samples
+			cout << "building view cells from " << (int)mVssSampleRays.size() << " samples " << endl;
+
 			mViewCellsManager->Construct(objects, mVssSampleRays);
-	
-			// construct view cells using the collected samples
-			cout << "building view cells from " << (int)mSampleRays.size() << " samples " << endl;
-		
-			// add contributions of saved samples to PVS
-			//contributingSamples += mBspTree->GetStat().contributingSamples;
-			//sampleContributions += mBspTree->GetStat().sampleContributions;
-
+			
 			// throw away samples 
 			CLEAR_CONTAINER(mVssSampleRays);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 443)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 444)
@@ -80,5 +80,5 @@
     for (it = rays.begin(); it != it_end; ++ it)
 	{
-		sampleContributions +=ComputeSampleContributions(*(*it), castRays);
+		sampleContributions += ComputeSampleContributions(*(*it), castRays);
 		contributingSamples += sampleContributions > 0;
 	}
@@ -182,5 +182,5 @@
 void ViewCellsManager::SetVisualizationSamples(const int visSamples)
 {
-	int mVisualizationSamples = visSamples;
+	mVisualizationSamples = visSamples;
 }
 
@@ -257,6 +257,5 @@
 
 	Debug << mBspTree->GetStatistics() << endl;
-	CLEAR_CONTAINER(sampleRays);
-
+	
 	return sampleContributions;
 }
@@ -287,5 +286,5 @@
 			BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
 		
-			// if ray not in unbounded space
+			// if ray not outside of view space
 			if (leaf->GetViewCell() != mBspTree->GetRootCell())
 			{
@@ -518,8 +517,8 @@
 	//-- some rays for output
 	const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
-	cout << "visualization using " << mVisualizationSamples << " samples" << endl;
-	vector<Ray *> vcRays[leafOut];
-
-	if (0)
+	Debug << "visualization using " << raysOut << " samples" << endl;
+	
+
+	if (1)
 	{
 		//-- some random view cells and rays for output
@@ -531,5 +530,10 @@
 		for (int i = 0; i < bspLeaves.size(); ++ i)
 		{
+			BspLeaf *leaf = bspLeaves[i];
+
+			RayContainer vcRays;
+		
 			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) 
@@ -539,16 +543,16 @@
 				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
 				{
-					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
-
-					if (bspLeaves[i]->GetViewCell() == leaf->GetViewCell()) 
+					BspLeaf *leaf2 = ray->bspIntersections[j].mLeaf;
+
+					if (leaf->GetViewCell() == leaf2->GetViewCell()) 
 					{
-						vcRays[i].push_back(ray);
+						vcRays.push_back(ray);
 					}
 				}
 			}
 
-			Intersectable::NewMail();
-
-			BspViewCell *vc = dynamic_cast<BspViewCell *>(bspLeaves[i]->GetViewCell());
+            Intersectable::NewMail();
+
+			BspViewCell *vc = dynamic_cast<BspViewCell *>(leaf->GetViewCell());
 
 			//bspLeaves[j]->Mail();
@@ -558,11 +562,9 @@
 			exporter->SetFilled();
 
-			ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
-
 			exporter->SetWireframe();
 			//exporter->SetFilled();
 
 			Material m;//= RandomMaterial();
-			m.mDiffuseColor = RgbColor(0, 1, 0);
+			m.mDiffuseColor = RgbColor(1, 1, 0);
 			exporter->SetForcedMaterial(m);
 
@@ -571,17 +573,17 @@
 			else
 			{
-				PolygonContainer cell;
+				PolygonContainer vcGeom;
 				// export view cell geometry
-				mBspTree->ConstructGeometry(vc, cell);
-				exporter->ExportPolygons(cell);
-				CLEAR_CONTAINER(cell);
+				mBspTree->ConstructGeometry(vc, vcGeom);
+				exporter->ExportPolygons(vcGeom);
+				CLEAR_CONTAINER(vcGeom);
 			}
 
 			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
-					<< ", piercing rays=" << (int)vcRays[i].size() << endl;
+				  << ", piercing rays=" << (int)vcRays.size() << endl;
 
 			// export rays piercing this view cell
-			exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
-
+			exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));	
+			
 			m.mDiffuseColor = RgbColor(1, 0, 0);
 			exporter->SetForcedMaterial(m);
@@ -590,6 +592,7 @@
 			exporter->SetFilled();
 
+			ViewCellPvsMap::iterator it, it_end = vc->GetPvs().mEntries.end();
 			// output PVS of view cell
-			for (; it != vc->GetPvs().mEntries.end(); ++ it) 
+			for (it = vc->GetPvs().mEntries.begin(); it !=  it_end; ++ it) 
 			{
 				Intersectable *intersect = (*it).first;
@@ -600,20 +603,5 @@
 				}			
 			}
-				
-			// 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;
@@ -623,4 +611,5 @@
 	{
 		ViewCellContainer viewCells;
+		RayContainer vcRays;
 
 		mBspTree->CollectViewCells(viewCells);
@@ -648,5 +637,5 @@
 					if (vc == leaf->GetViewCell()) 
 					{
-						vcRays[i].push_back(ray);
+						vcRays.push_back(ray);
 					}
 				}
@@ -677,9 +666,9 @@
 			
 			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
-					<< ", piercing rays=" << (int)vcRays[i].size() << endl;
+				  << ", piercing rays=" << (int)vcRays.size() << endl;
 
 			
 			// export rays piercing this view cell
-			exporter->ExportRays(vcRays[i], 1000, RgbColor(0, 1, 0));
+			exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));
 	
 			m.mDiffuseColor = RgbColor(1, 0, 0);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 382)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 444)
@@ -85,7 +85,7 @@
 int
 Mesh::CastRay(
-							Ray &ray,
-							MeshInstance *instance
-							)
+			  Ray &ray,
+			  MeshInstance *instance
+			  )
 {
   if (mKdTree) {
@@ -325,6 +325,6 @@
 int
 MeshInstance::CastRay(
-											Ray &ray
-											)
+					  Ray &ray
+					  )
 {
   int res = mMesh->CastRay(ray, this);
@@ -334,7 +334,7 @@
 int
 MeshInstance::CastRay(
-											Ray &ray,
-											const vector<int> &faces
-											)
+					  Ray &ray,
+					  const vector<int> &faces
+					  )
 {
   return mMesh->CastRayToSelectedFaces(ray, faces, this);
