Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 389)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 392)
@@ -56,5 +56,5 @@
 
 Sampling {
-	totalSamples	300000
+	totalSamples	250000
 	samplesPerPass  3
 }
@@ -73,5 +73,5 @@
 		minPvs 10
 		maxPvs 300
-		samples 100
+		samples 0
 	}
 
@@ -88,5 +88,5 @@
 	#	input fromViewCells
 	#	input fromSceneGeometry
-		samples 200000
+		samples 50000
 		sideTolerance 0.005
 	}
@@ -133,5 +133,5 @@
 	Termination {
 		# autopartition
-		maxRays 300
+		maxRays 100
 		maxPolygons 0
 		maxDepth 50
@@ -152,13 +152,9 @@
 	
 	
-	Visualization
-	{
-		# if split polys are stored for visualization
-		storeSplitPolys false
+	Visualization {
 		# x3d visualization of the split planes
 		exportSplits true
 		# how much samples should be used in visualization
-	
-		samples 2000
+		samples 300000
 	}
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 390)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 392)
@@ -315,12 +315,11 @@
 	void SetViewCell(BspViewCell *viewCell);
 
-	/** Generates new view cell and adds rays to the PVS.
+	/** Adds rays to the PVS.
 		@param sampleContributions the number contributions of the sampels
 		@param contributingSampels the number of contributing rays
 		
 	*/
-	void GenerateViewCell(const BoundedRayContainer &rays, 
-					      int &sampleContributions,
-						  int &contributingSamples);
+	void AddToPvs(const BoundedRayContainer &rays, int &sampleContributions,
+				  int &contributingSamples);
 
 protected:
@@ -392,5 +391,5 @@
 		@param viewCell view cell corresponding to unbounded space
 	*/ 
-	BspTree(ViewCell *viewCell);
+	BspTree(BspViewCell *viewCell);
 
 	~BspTree();
@@ -771,5 +770,5 @@
 
 	/// view cell corresponding to unbounded space
-	ViewCell *mRootCell;
+	BspViewCell *mRootCell;
 
 	/// should view cells be stored or generated in the leaves?
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 392)
@@ -41,6 +41,6 @@
 
     Intersection(const float t,
-								 Intersectable *object,
-								 const int face):mT(t), mObject(object), mFace(face) {}
+				 Intersectable *object,
+				 const int face):mT(t), mObject(object), mFace(face) {}
 		
     Intersection() {}
@@ -112,7 +112,7 @@
   // Inititalize the ray again when already constructed
   void Init(const Vector3 &wherefrom,
-						const Vector3 &whichdir,
-						const int _type, 
-						bool dirNormalized = false) {
+			const Vector3 &whichdir,
+			const int _type, 
+			bool dirNormalized = false) {
     loc = wherefrom;
     dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 391)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 392)
@@ -253,13 +253,10 @@
 }
 
-void BspLeaf::GenerateViewCell(const BoundedRayContainer &rays, 
-							   int &sampleContributions,
-							   int &contributingSamples)
+void BspLeaf::AddToPvs(const BoundedRayContainer &rays, 
+					   int &sampleContributions,
+					   int &contributingSamples)
 {
 	sampleContributions = 0;
 	contributingSamples = 0;
-
-	mViewCell = dynamic_cast<BspViewCell *>(ViewCell::Generate());
-	mViewCell->mBspLeaves.push_back(this);
 
     BoundedRayContainer::const_iterator it, it_end = rays.end();
@@ -273,8 +270,8 @@
 		if (!ray->intersections.empty())
 		{	
-            contribution += mViewCell->GetPvs().AddSample(ray->intersections[0].mObject);
-		}
-
-		contribution += mViewCell->GetPvs().AddSample(ray->sourceObject.mObject);
+            //contribution += mViewCell->GetPvs().AddSample(ray->intersections[0].mObject);
+		}
+
+		//contribution += mViewCell->GetPvs().AddSample(ray->sourceObject.mObject);
 
 		if (contribution > 0)
@@ -294,5 +291,5 @@
 /****************************************************************/
 
-BspTree::BspTree(ViewCell *viewCell): 
+BspTree::BspTree(BspViewCell *viewCell): 
 mRootCell(viewCell), mRoot(NULL), mGenerateViewCells(false),
 mStorePiercingRays(true)
@@ -707,18 +704,28 @@
 		BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
 	
+		BspViewCell *viewCell;
+
 		// generate new view cell for each leaf
 		if (mGenerateViewCells)
 		{
-			int conSamp, sampCon;
-			leaf->GenerateViewCell(*tData.mRays, conSamp, sampCon);
+			viewCell = dynamic_cast<BspViewCell *>(ViewCell::Generate());
+		}
+		else
+		{
+			// add view cell to leaf
+			viewCell = dynamic_cast<BspViewCell *>(tData.mViewCell);
+		}
+
+		leaf->SetViewCell(viewCell);
+		viewCell->mBspLeaves.push_back(leaf);
+
+		//-- add pvs
+		if (viewCell != mRootCell)
+		{
+			int conSamp = 0, sampCon = 0;
+			leaf->AddToPvs(*tData.mRays, conSamp, sampCon);
 			
 			mStat.contributingSamples += conSamp;
 			mStat.sampleContributions += sampCon;
-		}
-		else
-		{
-			// add view cell to leaf
-			leaf->SetViewCell(dynamic_cast<BspViewCell *>(tData.mViewCell));
-			leaf->mViewCell->mBspLeaves.push_back(leaf);
 		}
 
@@ -1334,16 +1341,11 @@
 
 		if (1) // use front and back cell areas to approximate volume
-		{
-			//-- compute area
-		
+		{	
 			// construct child geometry with regard to the candidate split plane
 			frontData.mCell = cell.ConstructChild(*this, candidatePlane, true);
 			backData.mCell = cell.ConstructChild(*this, candidatePlane, false);
 
-			frontData.mArea = frontData.mCell->GetArea();
-			backData.mArea = backData.mCell->GetArea();
-
-			pFront = frontData.mArea;
-			pBack = backData.mArea;
+			pFront = frontData.mArea = frontData.mCell->GetArea();
+			pBack = backData.mArea = backData.mCell->GetArea();
 
 			pOverall = area;
@@ -1409,7 +1411,8 @@
 
 	if ((sSplitPlaneStrategy & PVS) && area && pvs)
-		val += sPvsFactor * (frontData.mPvs * pFront + (backData.mPvs * pBack)) /	(pOverall * (float)pvs * 2);
-
-	Debug << "pvs: " << pvs << " totalArea: " << area 
+		val += sPvsFactor * (frontData.mPvs * pFront + (backData.mPvs * pBack)) /
+			   (pOverall * (float)pvs * 2);
+
+	Debug << "totalpvs: " << pvs << " ptotal: " << pOverall
 		  << " frontpvs: " << frontData.mPvs << " pFront: " << pFront 
 		  << " backpvs: " << backData.mPvs << " pBack: " << pBack 
@@ -2486,7 +2489,7 @@
 	if (!planePoly)
 	{
-		Debug << "returning 'same' geometry " << mPolys.size();
 		//geometry is not changed at all, hence just copy polygons
 		PolygonContainer::const_iterator it, it_end = mPolys.end();
+
 		for (it = mPolys.begin(); it != it_end; ++ it)
 			childCell->mPolys.push_back(new Polygon3((*it)->mVertices));
@@ -2513,4 +2516,5 @@
 						
 					poly->Split(splitPlane, *frontPoly, *backPoly, splitPts);
+
 					DEL_PTR(poly);
 
@@ -2554,6 +2558,7 @@
 	childCell->mSides.push_back(side);
 
-	Debug << "returning new geometry " << mPolys.size();
-
+	Debug << "returning new geometry " << mPolys.size() << " CHILD: " << childCell->mPolys.size() << endl;
+
+	Debug << "old area " << this->GetArea() << " new: " << childCell->GetArea() << endl;
 	return childCell;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 390)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 392)
@@ -75,7 +75,7 @@
 		@param sampleContributions contribution of the samples
 	*/
-	void ProcessBspViewCells(Ray &ray,
+	void ProcessBspViewCells(const Ray &ray,
 							 Intersectable *object,
-							 int faceIndex,
+							 const int faceIndex,
 							 int &contributingSamples,
 							 int &sampleContributions);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 372)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.h	(revision 392)
@@ -114,5 +114,5 @@
 
   /// the view cell corresponding to unbounded space
-  ViewCell mUnbounded;
+  BspViewCell mUnbounded;
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 391)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 392)
@@ -569,5 +569,5 @@
 		}
 
-		cout << "starting post processing using " << (int)mSampleRays.size() << " samples ... ";
+		cout << "starting post processing using " << mPostProcessSamples << " samples ... ";
 		
 		long startTime = GetTime();
@@ -690,7 +690,7 @@
 }
 
-void SamplingPreprocessor::ProcessBspViewCells(Ray &ray,
-											   Intersectable *object,
-											   int faceIndex,
+void SamplingPreprocessor::ProcessBspViewCells(const Ray &ray,
+                                               Intersectable *object,
+											   const int faceIndex,
 											   int &contributingSamples,
 											   int &sampleContributions)
@@ -702,13 +702,15 @@
 			((int)mSampleRays.size() < mBspConstructionSamples))
 		{
-			// also add origin to sample in order to extract it as input polygons
 			MeshInstance *mi = dynamic_cast<MeshInstance *>(object);
-			ray.sourceObject = Ray::Intersection(0.0, mi, faceIndex);
-							
-			mSampleRays.push_back(new Ray(ray));
+			
+			Ray *sRay = new Ray(ray);
+			mSampleRays.push_back(sRay);
+		
+			// also add origin to sample
+			sRay->sourceObject = Ray::Intersection(0.0, object, faceIndex);
 		}
 		else
 		{
-			// construct BSP tree using the samples
+			// construct BSP tree using the collected samples
 			cout << "building bsp tree from " << (int)mSampleRays.size() << " samples " << endl;
 			BuildBspTree();
@@ -910,5 +912,5 @@
 			exporter->SetForcedMaterial(m);
 
-			exporter->SetWireframe();
+			//exporter->SetWireframe();
 
 			// output PVS of view cell
