Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 433)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 436)
@@ -126,5 +126,5 @@
 	#	input fromViewCells
 	#	input fromSceneGeometry
-		samples 150000
+		samples 50000
 		sideTolerance 0.005
 	}
@@ -164,8 +164,8 @@
 	#splitPlaneStrategy 130
 	
-	splitPlaneStrategy 1
-	
-	maxPolyCandidates 70
-	maxRayCandidates 100
+	splitPlaneStrategy 1024
+	
+	maxPolyCandidates 50
+	maxRayCandidates 50
 	
 	# factors for evaluating split plane costs
@@ -186,6 +186,6 @@
 		minRays 200
 		minPolygons -1
-		maxDepth 20
-		minPvs 100
+		maxDepth 30
+		minPvs 150
 		minArea 0.01
 		maxRayContribution 0.005
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 404)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 436)
@@ -50,10 +50,8 @@
 		@param front returns the front the front polygon
 		@param back returns the back polygon
-		@param splitPts returns the split points
 	*/
 	void Split(const Plane3 &partition, 
 			   Polygon3 &front, 
-			   Polygon3 &back, 
-			   VertexContainer &splitPts);
+			   Polygon3 &back);
 
 	/** Returns the area of this polygon.
@@ -65,5 +63,4 @@
 	*/
 	int ClassifyPlane(const Plane3 &plane) const;
-
 
 	
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 428)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 436)
@@ -434,4 +434,7 @@
 	void Construct(const ObjectContainer &objects);
 
+	void Construct(const ObjectContainer &objects,
+				   const RayContainer &sampleRays);
+
 	/** Constructs the tree from a given set of rays.
 		@param sampleRays the set of sample rays the construction is based on
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 433)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 436)
@@ -143,6 +143,4 @@
 		Polygon3 *front_piece = NULL;
 		Polygon3 *back_piece = NULL;
-	
-		VertexContainer splitVertices;
 
 		switch (cf)
@@ -164,6 +162,5 @@
 				poly->Split(mPlane, 
 							*front_piece, 
-							*back_piece, 
-							splitVertices);
+							*back_piece);
 					
 				++ splits; // increase number of splits
@@ -698,5 +695,5 @@
 	facePolyMap.clear();
 
-	// compue bounding box
+	// compute bounding box
 	Polygon3::IncludeInBox(*polys, mBox);
 
@@ -721,4 +718,32 @@
 }
 
+void BspTree::Construct(const ObjectContainer &objects, const RayContainer &sampleRays)
+{
+    mStat.nodes = 1;
+	mBox.Initialize(); 	// initialise BSP tree bounding box
+	
+	BoundedRayContainer *rays = new BoundedRayContainer();
+	PolygonContainer *polys = new PolygonContainer();
+	
+	// copy mesh instance polygons into one big polygon soup
+	mStat.polys = AddToPolygonSoup(objects, *polys);
+
+	RayContainer::const_iterator rit, rit_end = sampleRays.end();
+
+	//-- store rays
+	for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
+	{
+		Ray *ray = *rit;
+        ray->SetId(-1); // reset id
+
+		float minT, maxT;
+		if (BoundRay(*ray, minT, maxT))
+			rays->push_back(new BoundedRay(ray, minT, maxT));
+	}
+
+	Debug << "tree has " << (int)polys->size() << " polys, " << (int)sampleRays.size() << " rays" << endl;
+	Construct(polys, rays);
+}
+
 void BspTree::Construct(PolygonContainer *polys, BoundedRayContainer *rays)
 {
@@ -736,14 +761,20 @@
 
 	mStat.Start();
-	cout << "**** Contructing bsp tree ****\n";
-
+	cout << "Contructing bsp tree ... ";
+	long startTime = GetTime();
 	while (!tStack.empty()) 
 	{
 		tData = tStack.top();
+
 	    tStack.pop();
 
 		// subdivide leaf node
-		BspNode *subRoot = Subdivide(tStack, tData);
-	}
+		BspNode *r = Subdivide(tStack, tData);
+
+		if (r == mRoot)
+			Debug << "BSP tree construction time spent at root: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
+	}
+
+	cout << "finished\n";
 
 	mStat.Stop();
@@ -1452,5 +1483,5 @@
 	float pFront = 0;
 	float pBack = 0;
-Debug << "here" << endl;
+
 	if (mSplitPlaneStrategy & PVS)
 	{
@@ -1573,5 +1604,5 @@
 
 		// give penalty to unbalanced split
-		if (0)
+		if (1)
 		if (((pFront * 0.2 + Limits::Small) > pBack) || (pFront < (pBack * 0.2 + Limits::Small)))
 			val += 0.5;
@@ -1583,5 +1614,4 @@
 		  << " backpvs: " << backPvs << " pBack: " << pBack << endl << endl;
 #endif
-	Debug << "here2" << endl;
 	return val;
 }
@@ -2195,6 +2225,7 @@
 					backPoly = new Polygon3();
 
-					candidatePolys[i]->Split(halfSpaces[j], *frontPoly, 
-											 *backPoly, splitPts);
+					candidatePolys[i]->Split(halfSpaces[j], 
+											 *frontPoly, 
+											 *backPoly);
 
 					DEL_PTR(candidatePolys[i]);
@@ -2450,7 +2481,5 @@
 					Polygon3 *backPoly = new Polygon3();
 				
-					VertexContainer splitPts;
-						
-					poly->Split(splitPlane, *frontPoly, *backPoly, splitPts);
+					poly->Split(splitPlane, *frontPoly, *backPoly);
 
 					DEL_PTR(poly);
@@ -2507,10 +2536,8 @@
 			case Polygon3::SPLIT:
 				{
-					VertexContainer splitPts;
-				
 					Polygon3 *frontPoly = new Polygon3();
 					Polygon3 *backPoly = new Polygon3();
 
-					planePoly->Split(plane, *frontPoly, *backPoly, splitPts);
+					planePoly->Split(plane, *frontPoly, *backPoly);
 					DEL_PTR(planePoly);
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp	(revision 433)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp	(revision 436)
@@ -229,7 +229,4 @@
 		dir = vssRay.GetDir() / dist;
 
-	if (!vssRay.mTerminationObject)
-		Debug << "error!" << endl;
-
 	intersections.push_back(Intersection(dist, vssRay.mTerminationObject, 0));
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 434)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 436)
@@ -440,6 +440,6 @@
   cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush;
 	
-  int numExportRays = 10000;
-  //int numExportRays = 0;
+  //int numExportRays = 10000;
+  int numExportRays = 0;
 
   if (numExportRays) {
@@ -451,24 +451,19 @@
   //-- construct BSP view cells
   if (ViewCell::sHierarchy == ViewCell::BSP) 
-	{
+  {
 	  const int bspSamples = min((int)mVssRays.size(), mBspConstructionSamples);
 		
+	  Debug << "bpssamples: " << bspSamples << endl;
 	  for (int i = 0; i < bspSamples; ++ i)
-		bspRays.push_back(new Ray(*mVssRays[i]));
-
+	 	  bspRays.push_back(new Ray(*mVssRays[i]));
+	 
 	  //-- construct BSP tree using the samples
 	  mBspTree = new BspTree(&mUnbounded);	
 
+	  ObjectContainer objects;
+	  mSceneGraph->CollectObjects(&objects);
+	  
 	  mBspTree->SetGenerateViewCells(true);
-	  mBspTree->Construct(bspRays);
-
-	  Exporter *exporter = Exporter::GetExporter("vccbsprays.x3d");
-			
-	  // export rays piercing this view cell
-	  exporter->ExportRays(bspRays, 1000, RgbColor(0, 1, 0));
-
-	  // cast remaining initial rays into BSP tree
-	  for (int i = bspSamples; i < (int)mVssRays.size(); ++ i)
-		CastRay(*mBspTree, *mVssRays[i]);
+	  mBspTree->Construct(objects, bspRays);
 	}
 
@@ -587,11 +582,4 @@
   //-- cast ray to BSP tree to get intersection with view cells
   Ray ray(vssRay);
-
-  Debug << ray << endl;
-  if (ray.intersections.empty())
-	Debug << "empty ray" << endl;
-  else 
-	Debug << "intersection: " << ray.intersections[0].mT << " " << ray.intersections[0].mObject << endl;
-
   mBspTree->CastRay(ray);
 				
@@ -601,8 +589,8 @@
 			
   if (!ray.intersections.empty()) // second intersection found
-	{
+  {
 	  //sampleContributions += 
 	  AddObjectSamples(ray.intersections[0].mObject, ray);
-	}
+  }
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 435)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 436)
@@ -965,7 +965,5 @@
 			// do all the splits with the previous planes
 			for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
-			{
-				VertexContainer splitPts;
-				
+			{				
 				if (planePoly->ClassifyPlane(*tData.mPlanes[i]) == Polygon3::SPLIT)
 				{
@@ -973,5 +971,5 @@
 					Polygon3 *backPoly = new Polygon3();
 
-					planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly, splitPts);
+					planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly);
 					DEL_PTR(planePoly);
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 404)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 436)
@@ -42,6 +42,5 @@
 void Polygon3::Split(const Plane3 &partition, 
 					 Polygon3 &front, 
-					 Polygon3 &back, 
-					 VertexContainer &splitPts)
+					 Polygon3 &back)
 {
 	Vector3 ptA = mVertices.back();
@@ -51,4 +50,6 @@
 	VertexContainer::const_iterator it;
 	
+	Vector3 lastSplit;
+
 	bool foundSplit = false;
 	// find line - plane intersections
@@ -68,5 +69,5 @@
 				// test if split point not too close to previous split point
 				if (!foundSplit || 
-					(SqrDistance(splitPt, splitPts.back()) > Vector3::sDistToleranceSqrt))
+					(SqrDistance(splitPt, lastSplit) > Vector3::sDistToleranceSqrt))
 				{
 					// add vertex to both polygons
@@ -74,5 +75,5 @@
 					back.mVertices.push_back(splitPt);
 					
-					splitPts.push_back(splitPt);
+					lastSplit = splitPt;
 					foundSplit = true;
 				}
@@ -89,5 +90,5 @@
 					// test if split point not too close to previous split point
 				if (!foundSplit || 
-					(SqrDistance(splitPt, splitPts.back()) > Vector3::sDistToleranceSqrt))
+					(SqrDistance(splitPt, lastSplit) > Vector3::sDistToleranceSqrt))
 				{
 					// add vertex to both polygons
@@ -95,5 +96,5 @@
 					back.mVertices.push_back(splitPt);
 
-					splitPts.push_back(splitPt);
+					lastSplit = splitPt;
 					foundSplit = true;
 				}	
