Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 181)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 191)
@@ -4,4 +4,6 @@
 #include "X3dExporter.h"
 #include "Environment.h"
+#include "MutualVisibility.h"
+
 SamplingPreprocessor::SamplingPreprocessor()
 {
@@ -33,14 +35,35 @@
 
 int
-SamplingPreprocessor::AddNodeSamples(Intersectable *object, const Ray &ray)
+SamplingPreprocessor::AddNodeSamples(Intersectable *object,
+				     const Ray &ray,
+				     const int pass)
 {
   int contributingSamples = 0;
-
-  for (int j=0; j < ray.leaves.size(); j++) {
+  int j;
+  for (j=0; j < ray.leaves.size(); j++) {
     KdNode *node = GetNodeForPvs( ray.leaves[j] );
     contributingSamples += object->mKdPvs.AddNodeSample(node);
   }
   
+  if (pass > 10)
+    for (j=1; j < ray.leaves.size() - 1; j++) {
+      ray.leaves[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
+    }
+  
   return contributingSamples;
+}
+
+
+void
+SamplingPreprocessor::HoleSamplingPass()
+{
+  vector<KdLeaf *> leaves;
+  mKdTree->CollectLeaves(leaves);
+  
+  // go through all the leaves and evaluate their passing contribution
+  for (int i=0 ; i < leaves.size(); i++) {
+    KdLeaf *leaf = leaves[i];
+    cout<<leaf->mPassingRays<<endl;
+  }
 }
 
@@ -63,5 +86,4 @@
   int totalSamples = 0;
 
-
   int pvsOut = Min((int)objects.size(), 10);
   
@@ -73,108 +95,118 @@
     int passSampleContributions = 0;
     int passSamples = 0;
-	int index = 0;
-    KdNode *visibleNode = NULL;
+    int index = 0;
 
     for (i =0; i < objects.size(); i++) {
       KdNode *nodeToSample = NULL;
       Intersectable *object = objects[i];
-
+      
       int pvsSize = object->mKdPvs.GetSize();
-
-      
-    if (pvsSize) {
-		// mail all nodes from the pvs
-		Intersectable::NewMail();
-		KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
-		for (; i != object->mKdPvs.mEntries.end(); i++) {
-			KdNode *node = (*i).first;
-			node->Mail();
-		}
-		int maxTries = 2*pvsSize;
-	
-		for (int tries = 0; tries < 10; tries++) {
-			index = RandomValue(0, pvsSize - 1);
-			KdPvsData data;
-			object->mKdPvs.GetData(index, visibleNode, data);
-			nodeToSample = mKdTree->FindRandomNeighbor(visibleNode, true);
-			if (nodeToSample)
-			break;
-		}
-	}
-
-    if (0 && pvsSize) {
-		// mail all nodes from the pvs
-		Intersectable::NewMail();
-		KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
-		for (; i != object->mKdPvs.mEntries.end(); i++) {
-			KdNode *node = (*i).first;
-			node->Mail();
-		}
-	
-		vector<KdNode *> invisibleNeighbors;
-		// get all neighbors of all PVS nodes
-		i = object->mKdPvs.mEntries.begin();
-		for (; i != object->mKdPvs.mEntries.end(); i++) {
-			KdNode *node = (*i).first;
-			mKdTree->FindNeighbors(visibleNode, invisibleNeighbors, true);
-		}
-
-		KdPvsData data;
-		KdNode *visibleNode;
-		object->mKdPvs.GetData(index, visibleNode, data);
-		nodeToSample = mKdTree->FindRandomNeighbor(visibleNode, true);
-		if (nodeToSample)
-			break;
-      }
-      
+      
+      if (0 && pvsSize) {
+	// mail all nodes from the pvs
+	Intersectable::NewMail();
+	KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
+	for (; i != object->mKdPvs.mEntries.end(); i++) {
+	  KdNode *node = (*i).first;
+	  node->Mail();
+	}
+	int maxTries = 2*pvsSize;
+	
+	for (int tries = 0; tries < 10; tries++) {
+	  index = RandomValue(0, pvsSize - 1);
+	  KdPvsData data;
+	  KdNode *node;
+	  object->mKdPvs.GetData(index, node, data);
+	  nodeToSample = mKdTree->FindRandomNeighbor(node, true);
+	  if (nodeToSample)
+	    break;
+	}
+      }
+      
+      if (0 && pvsSize && pass == 100 ) {
+	// mail all nodes from the pvs
+	Intersectable::NewMail();
+	KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
+	for (; i != object->mKdPvs.mEntries.end(); i++) {
+	  KdNode *node = (*i).first;
+	  node->Mail();
+	}
+	
+	vector<KdNode *> invisibleNeighbors;
+	// get all neighbors of all PVS nodes
+	i = object->mKdPvs.mEntries.begin();
+	for (; i != object->mKdPvs.mEntries.end(); i++) {
+	  KdNode *node = (*i).first;
+	  mKdTree->FindNeighbors(node, invisibleNeighbors, true);
+	  AxisAlignedBox3 box = object->GetBox();
+	  for (int j=0; j < invisibleNeighbors.size(); j++) {
+	    int visibility = ComputeBoxVisibility(mKdTree,
+						  box,
+						  mKdTree->GetBox(invisibleNeighbors[j]),
+						  1.0f);
+	    
+	  }
+	  
+	
+	  // now rank all the neighbors according to probability that a new
+	  // sample creates some contribution
+	}
+      }
 
       for (int k=0; k < mSamplesPerPass; k++) {
-		  object->GetRandomSurfacePoint(point, normal);
-		  if (nodeToSample) {
-			  int maxTries = 5;
-			  for (int tries = 0; tries < maxTries; tries++) {
-				  direction = mKdTree->GetBox(nodeToSample).GetRandomPoint() - point;
-				  if (DotProd(direction, normal) > Limits::Small)
-					break;
-			  }
+	object->GetRandomSurfacePoint(point, normal);
+	if (nodeToSample) {
+	  int maxTries = 5;
+	  for (int tries = 0; tries < maxTries; tries++) {
+	    direction = mKdTree->GetBox(nodeToSample).GetRandomPoint() - point;
+	    if (DotProd(direction, normal) > Limits::Small)
+	      break;
+	  }
 		
-			  if (tries == maxTries)
-				  direction = UniformRandomVector(normal);
-		  } else
-			  direction = UniformRandomVector(normal);
-	
-		  // construct a ray
-		  SetupRay(ray, point, direction);
-		  mKdTree->CastRay(ray);
-
-		  if (i < pvsOut)
-			  rays[i].push_back(ray);
-	
-		  int sampleContributions = 0;
+	  if (tries == maxTries)
+	    direction = UniformRandomVector(normal);
+	} else
+	  direction = UniformRandomVector(normal);
+	
+	// construct a ray
+	SetupRay(ray, point, direction);
+	mKdTree->CastRay(ray);
+
+	if (i < pvsOut)
+	  rays[i].push_back(ray);
+	
+	int sampleContributions = 0;
 	
 		  
-		  if (ray.leaves.size()) {
-			  sampleContributions += AddNodeSamples(object, ray);
+	if (ray.leaves.size()) {
+	  sampleContributions += AddNodeSamples(object, ray, pass);
 	  
-			  if (ray.intersections.size()) {
-				  sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray);
-				  // check whether we can add this to the rays
-				  for (int j = 0; j < pvsOut; j++) {
-					  if (objects[j] == ray.intersections[0].mObject) {
-						  rays[j].push_back(ray);
-					  }
-				  }
-			  }
+	  if (ray.intersections.size()) {
+	    sampleContributions += AddNodeSamples(ray.intersections[0].mObject, ray, pass);
+	    // check whether we can add this to the rays
+	    for (int j = 0; j < pvsOut; j++) {
+	      if (objects[j] == ray.intersections[0].mObject) {
+		rays[j].push_back(ray);
+	      }
+	    }
+	  }
 		
-			  passSamples++;
+	  passSamples++;
 			
-			  if (sampleContributions) {
-				  passContributingSamples++;
-				  passSampleContributions += sampleContributions;
-			  }
-		  }
-	  }
-	}
+	  if (sampleContributions) {
+	    passContributingSamples++;
+	    passSampleContributions += sampleContributions;
+	  }
+	}
+      }
+    }
+
+
     totalSamples += passSamples;
+
+    //    if (pass>10)
+    //      HoleSamplingPass();
+    
+
     pass++;
     
@@ -193,4 +225,16 @@
       endl;
   }
+
+  int totalPvsSize = mKdTree->CollectLeafPvs();
+  cout<<"#totalPvsSize="<<totalPvsSize<<endl;
+  
+  //  HoleSamplingPass();
+  if (1) {
+    Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
+    exporter->SetExportRayDensity(true);
+    exporter->ExportKdTree(*mKdTree);
+    delete exporter;
+  }
+  
   bool exportRays = false;
   if (exportRays) {
@@ -214,5 +258,5 @@
       KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
       Intersectable::NewMail();
-	// avoid adding the object to the list
+      // avoid adding the object to the list
       object->Mail();
       ObjectContainer visibleObjects;
