Index: GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj	(revision 722)
+++ GTP/trunk/Lib/Vis/Preprocessing/scripts/Preprocessor.vcproj	(revision 726)
@@ -65,4 +65,7 @@
 			<Tool
 				Name="VCCLCompilerTool"
+				InlineFunctionExpansion="2"
+				FavorSizeOrSpeed="1"
+				OptimizeForWindowsApplication="TRUE"
 				AdditionalIncludeDirectories="..\support;..\support\devil\include;..\support\zlib\include;..\include;&quot;$(QTDIR)\include\QtCore&quot;;&quot;$(QTDIR)\include\QtGui&quot;;&quot;$(QTDIR)\include&quot;;&quot;$(QTDIR)\include\QtOpenGl&quot;;..\src;&quot;$(CG_INC_PATH)&quot;;..\..\..\..\..\..\NonGTP\Devil\include;..\..\..\..\..\..\NonGTP\Zlib\include;..\..\..\..\..\..\NonGTP\Xerces"
 				PreprocessorDefinitions="WIN32;NDEBUG;_LIB;"
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 712)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 726)
@@ -57,4 +57,16 @@
 
 
+static void RotateMesh(Mesh *mesh)
+{
+	VertexContainer::iterator it, it_end = mesh->mVertices.end();
+
+	const float angle = 30.0f * PI / 180.0f;
+	const Matrix4x4 rot = RotationYMatrix(angle);
+
+	for (it = mesh->mVertices.begin(); it != it_end; ++ it)
+	{
+		(*it) = rot * (*it);        
+	}
+}
 
 // ---------------------------------------------------------------------------
@@ -96,16 +108,4 @@
 
 
-static void RotateMesh(Mesh *mesh)
-{
-	VertexContainer::iterator it, it_end = mesh->mVertices.end();
-
-	const float angle = 30.0f * PI / 180.0f;
-	const Matrix4x4 rot = RotationYMatrix(angle);
-
-	for (it = mesh->mVertices.begin(); it != it_end; ++ it)
-	{
-		(*it) = rot * (*it);        
-	}
-}
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp	(revision 704)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp	(revision 726)
@@ -156,6 +156,5 @@
       
       currentMesh->AddFace(face);
-      if (meshGrouping != 0 &&
-					currentMesh->mFaces.size() >=  meshGrouping) {
+      if (meshGrouping != 0 && currentMesh->mFaces.size() >=  meshGrouping) {
 		if (ROTATE_SCENE)
 			RotateMesh(currentMesh);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 719)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 726)
@@ -458,8 +458,5 @@
 
 	EvalPriority(tData);
-	
-
-	if (mSimulateOctree)
-        tData.mAxis = 0;
+
 
 	// first node is kd node, i.e. an axis aligned box
@@ -569,5 +566,4 @@
 							  geom);
 
-	
 
 	// compute first split candidate
@@ -695,10 +691,4 @@
 		VspBspTraversalData tBackData;
 
-		if (mSimulateOctree)
-		{	// choose axes in circular motion
-			tFrontData.mAxis = (tData.mAxis + 1) % 3;
-			tBackData.mAxis = (tData.mAxis + 1) % 3;
-		}
-
 		// create new interior node and two leaf nodes
 		// or return leaf as it is (if maxCostRatio missed)
@@ -709,5 +699,6 @@
 		Plane3 splitPlane;
 		BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
-
+		
+		// choose next split plane
 		if (!SelectPlane(splitPlane, leaf, tData, tFrontData, tBackData, splitAxis))
 		{
@@ -722,4 +713,5 @@
 		}
 	
+		// if this a valid split => subdivide this node further
 		if (splitFurther) //-- continue subdivision
 		{
@@ -732,4 +724,5 @@
 
 			tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && (splitAxis < 3));
+			tFrontData.mAxis = tBackData.mAxis = splitAxis;
 
 			// how often was max cost ratio missed in this branch?
@@ -740,7 +733,7 @@
 			EvalPriority(tBackData);
 
+			// evaluate subdivision stats
 			if (1)
 			{
-
 				float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
 				float cBack = (float)tBackData.mPvs * tBackData.mProbability;
@@ -823,5 +816,5 @@
 }
 
-
+// subdivide using a split plane queue
 BspNode *VspBspTree::Subdivide(VspBspSplitQueue &tQueue,
 							   VspBspSplitCandidate &splitCandidate)
@@ -839,4 +832,5 @@
 
 		//-- continue subdivision
+		
 		// create new interior node and two leaf node
 		const Plane3 splitPlane = splitCandidate.mSplitPlane;
@@ -853,5 +847,6 @@
 
 		tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && (splitAxis < 3));
-		
+		tFrontData.mAxis = tBackData.mAxis = splitAxis;
+
 		// how often was max cost ratio missed in this branch?
 		tFrontData.mMaxCostMisses = maxCostMisses;
@@ -960,5 +955,5 @@
 
 	// compute locally best split plane
-	bool success = SelectPlane(splitData.mSplitPlane, leaf, tData, 
+    bool success = SelectPlane(splitData.mSplitPlane, leaf, tData, 
 							   frontData, backData, splitData.mSplitAxis);
 
@@ -1403,5 +1398,5 @@
 		sAxis = Random(3);
 	else if (mSimulateOctree)
-		sAxis = tData.mAxis;
+		sAxis = (tData.mAxis + 1) % 3;
 		
 	//Debug << "use special axis: " << useSpecialAxis << endl;
@@ -2012,5 +2007,5 @@
 
 	const int pvsSize = data.mPvs;
-//cout << "here433" << endl;
+
 	RayInfoContainer::const_iterator rit, rit_end = data.mRays->end();
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 719)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 726)
@@ -2441,5 +2441,4 @@
 		{
 			geom.Add(candidatePolys[i], candidatePlanes[i]);
-			//	geom.mPolys.push_back(candidates[i]);
 		}
 	}
@@ -2456,6 +2455,6 @@
 
 
-int BspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> 
-						   &neighbors,
+int BspTree::FindNeighbors(BspNode *n, 
+						   vector<BspLeaf *> &neighbors,
 						   const bool onlyUnmailed) const
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 712)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 726)
@@ -1145,9 +1145,7 @@
 		for (oit = objects.begin(); oit != oit_end; ++ oit)
 		{
-			if (1)
-			{
+			if (0)
 				SetForcedMaterial(RandomMaterial());
-			}
-
+			
 			ExportIntersectable(*oit);
 		}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 722)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 726)
@@ -2104,52 +2104,71 @@
 	{
 		exporter->ExportMesh(vc->GetMesh());
+	
 		return;
 	}
 
+	
+	if (clipPlane)
+	{
+		ViewCellContainer leaves;
+		mViewCellsTree->CollectLeaves(vc, leaves);
+		ViewCellContainer::const_iterator it, it_end = leaves.end();
+
+		for (it = leaves.begin(); it != it_end; ++ it)
+		{
+			BspNodeGeometry geom;
+
+			BspNodeGeometry front;
+			BspNodeGeometry back;
+
+			BspLeaf *leaf = dynamic_cast<BspViewCell *>(*it)->mLeaf;
+			mBspTree->ConstructGeometry(leaf, geom);
+
+			const float eps = 0.00000001f;
+			const int cf = geom.Side(*clipPlane, eps);
+
+			if (cf == -1)
+			{
+				exporter->ExportPolygons(geom.GetPolys());
+			}
+			else if (cf == 0)
+			{
+				geom.SplitGeometry(front,
+								   back,
+								   *clipPlane,
+								   mViewSpaceBox, 
+								   eps);
+	
+				//Debug << "geo size: " << geom.Size() << endl;
+				//Debug << "size b: " << back.Size() << " f: " << front.Size() << endl;
+				if (back.Valid())
+				{
+					exporter->ExportPolygons(back.GetPolys());
+				}			
+			}
+		}
+	}
+	else
+	{
+		BspNodeGeometry geom;
+		mBspTree->ConstructGeometry(vc, geom);
+			
+		exporter->ExportPolygons(geom.GetPolys());
+	}
+}
+
+
+void BspViewCellsManager::CreateMesh(ViewCell *vc)
+{
+	// delete previous mesh
+	///DEL_PTR(vc->GetMesh());
 	BspNodeGeometry geom;
 	mBspTree->ConstructGeometry(vc, geom);
 
-	if (clipPlane)
-	{
-		const int cf = Polygon3::ClassifyPlane(geom.GetPolys(), *clipPlane, 0.0001f);
-
-		if (cf == Polygon3::BACK_SIDE)
-		{
-			exporter->ExportPolygons(geom.GetPolys());
-		}
-		else if (cf == Polygon3::SPLIT)
-		{
-			BspNodeGeometry front;
-			BspNodeGeometry back;
-
-			geom.SplitGeometry(front,
-							   back,
-							   *clipPlane,
-							   mViewSpaceBox, 
-							   0.0001f);
-
-			if (back.Valid())
-				exporter->ExportPolygons(back.GetPolys());
-		}
-	}
-	else
-	{
-		exporter->ExportPolygons(geom.GetPolys());
-	}
-}
-
-
-void BspViewCellsManager::CreateMesh(ViewCell *vc)
-{
-	if (vc->GetMesh())
-		delete vc->GetMesh();
-
-	BspNodeGeometry geom;
-	
-	mBspTree->ConstructGeometry(vc, geom);
-
 	Mesh *mesh = new Mesh();
+
 	geom.AddToMesh(*mesh);
 	vc->SetMesh(mesh);
+	// put mesh into mesh container so we can savely delete it
 	mMeshContainer.push_back(mesh);
 }
@@ -2785,4 +2804,5 @@
 }
 
+
 void VspKdViewCellsManager::Visualize(const ObjectContainer &objects,
 									  const VssRayContainer &sampleRays)
@@ -2882,6 +2902,4 @@
 	Exporter *exporter = Exporter::GetExporter("vspkdtree_merged.x3d");
 
-	//if (exportGeometry) exporter->SetWireframe();
-	//else exporter->SetFilled();
 
 	ExportViewCellsForViz(exporter);
@@ -4000,8 +4018,5 @@
 void VspBspViewCellsManager::CreateMesh(ViewCell *vc)
 {
-	if (vc->GetMesh())
-		delete vc->GetMesh();
-
-	
+	//if (vc->GetMesh()) delete vc->GetMesh();
 	BspNodeGeometry geom;
 
@@ -4011,4 +4026,5 @@
 	geom.AddToMesh(*mesh);
 	vc->SetMesh(mesh);
+	// put mesh into mesh container so we can savely delete it
 	mMeshContainer.push_back(mesh);
 }
