Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 338)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 361)
@@ -153,4 +153,10 @@
 			</File>
 			<File
+				RelativePath="..\src\Halton.cpp">
+			</File>
+			<File
+				RelativePath="..\src\Halton.h">
+			</File>
+			<File
 				RelativePath="..\src\Intersectable.h">
 			</File>
Index: /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 352)
+++ /trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 361)
@@ -56,5 +56,5 @@
 
 Sampling {
-	totalSamples	200000
+	totalSamples	20000
 	samplesPerPass  2
 }
@@ -69,6 +69,6 @@
 	
 #	filename ../data/atlanta/atlanta_viewcells_large.x3d
-#	filename ../data/vienna/viewcells-25-sel.x3d
-	filename ../data/vienna/viewcells-25.x3d
+	filename ../data/vienna/viewcells-25-sel.x3d
+#	filename ../data/vienna/viewcells-25.x3d
 #	filename ../data/vienna/viewcells-large-sel.x3d
 }
@@ -77,7 +77,7 @@
 	Construction {
 	#	input fromRays
-	#	input fromViewCells
-		input fromSceneGeometry
-		samples 150000
+		input fromViewCells
+	#	input fromSceneGeometry
+		samples 15000
 		sideTolerance 0.005
 	}
@@ -123,5 +123,5 @@
 		# autopartition
 		maxRays -1
-		maxPolygons 250
+		maxPolygons 0
 		maxDepth 100
 		
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 360)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 361)
@@ -365,5 +365,6 @@
 	/** finds neighbouring leaves of this tree node.
 	*/
-	int FindNeighbors(BspNode *n, vector<BspNode *> &neighbors, bool onlyUnmailed);
+	int FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, 
+					  const bool onlyUnmailed) const;
 
 	/** Extracts geometry associated with the split plane leading to this node.
@@ -603,5 +604,5 @@
 	/** Extracts the split planes representing the space bounded by node n.
 	*/
-	void ExtractSplitPlanes(BspNode *n, vector<Plane3 *> planes, vector<bool> sides) const;
+	void ExtractSplitPlanes(BspNode *n, vector<Plane3 *> &planes, vector<bool> &sides) const;
 
 	/// Pointer to the root of the tree
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 360)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 361)
@@ -1686,15 +1686,21 @@
 }
 
-void BspTree::ExtractSplitPlanes(BspNode *n, vector<Plane3 *> planes, vector<bool> sides) const
-{
-	while (!n->IsRoot())
-	{
-		if (!n->IsLeaf())
-			planes.push_back(dynamic_cast<BspInterior *>(n)->GetPlane());
-
-		sides.push_back(n->GetParent()->mFront == n);
-
+void BspTree::ExtractSplitPlanes(BspNode *n, vector<Plane3 *> &planes, vector<bool> &sides) const
+{
+	BspNode *lastNode;
+	do
+	{
+		lastNode = n;
 		n = n->GetParent();
-	}
+		
+		if (n)
+		{
+			BspInterior *interior = dynamic_cast<BspInterior *>(n);
+
+			planes.push_back(dynamic_cast<BspInterior *>(interior)->GetPlane());
+			sides.push_back(interior->mFront == lastNode);
+		}
+	}
+	while (n);
 }
 
@@ -1763,12 +1769,13 @@
 						DEL_PTR(frontPoly);
 					}
+					inside = true;
 					break;
 
 				case Polygon3::BACK_SIDE:
-					if (sides[i]) 
+					if (sides[j]) 
 						inside = false;
 					break;
 				case Polygon3::FRONT_SIDE:
-					if (!sides[i])
+					if (!sides[j])
 						inside = false;
 					break;
@@ -1785,5 +1792,6 @@
 }
 
-int BspTree::FindNeighbors(BspNode *n, vector<BspNode *> &neighbors, bool onlyUnmailed)
+int BspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, 
+						   const bool onlyUnmailed) const
 {
 	PolygonContainer cell;
@@ -1793,4 +1801,10 @@
 	stack<BspNode *> nodeStack;
 	nodeStack.push(mRoot);
+		
+	// planes needed to verify that we found neighbor leaf.
+	vector<Plane3 *> planes;
+	vector<bool> sides;
+
+	ExtractSplitPlanes(n, planes, sides);
 
 	while (!nodeStack.empty()) 
@@ -1799,9 +1813,28 @@
 		nodeStack.pop();
 
-		if (node->IsLeaf()) 
-		{
-			if (node != n && (!onlyUnmailed || !node->Mailed())) 
-				neighbors.push_back(node);
-		} 
+		if (node->IsLeaf())
+		{
+            if (node != n && (!onlyUnmailed || !node->Mailed()))
+			{
+				// test all planes of current node on neighbour
+				PolygonContainer neighborCandidate;
+				ConstructGeometry(node, neighborCandidate);
+				
+				bool isAdjacent = true;
+				for (int i = 0; (i < planes.size()) && isAdjacent; ++ i)
+				{
+					const int cf = 
+						Polygon3::ClassifyPlane(neighborCandidate, *planes[i]);
+
+					if ((cf == Polygon3::BACK_SIDE) && sides[i])
+						isAdjacent = false;
+					else if ((cf == Polygon3::FRONT_SIDE) && !sides[i])
+						isAdjacent = false;
+				}
+
+				if (isAdjacent)
+					neighbors.push_back(dynamic_cast<BspLeaf *>(node));
+			}
+		}
 		else 
 		{
@@ -1811,8 +1844,8 @@
 
 			if (cf == Polygon3::FRONT_SIDE)
-				nodeStack.push(interior->mBack);
+				nodeStack.push(interior->mFront);
 			else
 				if (cf == Polygon3::BACK_SIDE)
-					nodeStack.push(interior->mFront);
+					nodeStack.push(interior->mBack);
 				else 
 				{
@@ -1822,6 +1855,6 @@
 				}
 		}
-	
-	}
+	}
+	
 	return (int)neighbors.size();
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 360)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.cpp	(revision 361)
@@ -383,10 +383,8 @@
 	bool onBackSide = false;
 	
-	int count = 0;
-
-	// find possible line-plane intersections
+	// find intersections
 	for (it = polys.begin(); it != polys.end(); ++ it)
 	{
-        int cf = (*it)->ClassifyPlane(plane);
+        const int cf = (*it)->ClassifyPlane(plane);
 		
         if (cf == FRONT_SIDE)
@@ -395,7 +393,7 @@
 			onBackSide = true;
 		
-		//TODO: check if split goes through vertex
-		if ((cf == SPLIT) || (onFrontSide && onBackSide)) // split 
-		{
+		if ((cf == SPLIT) || (cf == COINCIDENT) || (onFrontSide && onBackSide))
+		{
+			Debug << "here" << endl;
 			return SPLIT;
 		}
@@ -411,4 +409,4 @@
 	}
 	
-	return COINCIDENT; // plane and polygon are coincident
-}
+	return SPLIT;
+}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 360)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 361)
@@ -620,10 +620,48 @@
 	  Exporter *exporter = Exporter::GetExporter("viewCells.x3d");
 
-	  if (exporter)
+	  // export leaves
+	  if (0 && exporter)
 	  {
-          //exporter->ExportLeavesGeometry(mBspTree, leaves);		
-
-		  delete exporter;
+		  vector<BspLeaf *> leaves;
+		  mBspTree->CollectLeaves(leaves);
+		  vector<BspLeaf *> randLeaves;
+		  vector<BspLeaf *> neighbors;
+
+		  for (int k = 0; k < 1; ++ k) 
+		  {
+			  BspLeaf *leaf = leaves[Random((int)leaves.size())];
+			  randLeaves.push_back(leaf);
+			  mBspTree->FindNeighbors(leaf, neighbors, false);
+		  }	
+
+		  exporter->ExportLeavesGeometry(*mBspTree, randLeaves);
+		  exporter->SetWireframe();
+          exporter->ExportLeavesGeometry(*mBspTree, neighbors);	
+			
+          delete exporter;
 	  }
+
+	   vector<BspLeaf *> leaves;
+	   mBspTree->CollectLeaves(leaves);
+	   
+	   for (int k = 0; k < 100; ++ k) 
+	   {
+		   vector<BspLeaf *> randLeaves;
+		   vector<BspLeaf *> neighbors;
+		   
+		   char s[64]; sprintf(s, "bsp-vc%04d.x3d", k);
+		   Exporter *exporter = Exporter::GetExporter(s);
+
+		   BspLeaf *leaf = leaves[Random((int)leaves.size())];
+		   Debug << "\noutput leaf " << k << endl;
+		   randLeaves.push_back(leaf);
+		   mBspTree->FindNeighbors(leaf, neighbors, false);
+
+		   exporter->ExportLeavesGeometry(*mBspTree, randLeaves);
+		   exporter->SetWireframe();
+		   exporter->ExportLeavesGeometry(*mBspTree, neighbors);	
+			
+		   delete exporter;
+	   }	
 
 	  for (int j = 0; j < pvsViewCells.size(); ++ j)
