Index: /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 209)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 218)
@@ -90,114 +90,121 @@
   vector<Ray> rays[10];
   
-  while (totalSamples < mTotalSamples) {
-
-    int passContributingSamples = 0;
-    int passSampleContributions = 0;
-    int passSamples = 0;
-    int index = 0;
-
-    for (i =0; i < objects.size(); i++) {
-      KdNode *nodeToSample = NULL;
-      Intersectable *object = objects[i];
-      
-      int pvsSize = object->mKdPvs.GetSize();
-      
-      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();
+  while (totalSamples < mTotalSamples)
+  {
+	  int passContributingSamples = 0;
+	  int passSampleContributions = 0;
+	  int passSamples = 0;
+	  int index = 0;
+
+	  for (i =0; i < objects.size(); i++) {
+		  KdNode *nodeToSample = NULL;
+		  Intersectable *object = objects[i];
+		  
+		  int pvsSize = object->mKdPvs.GetSize();
+      
+		  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 == 50)  {
+			  // 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;
+				  }
+		
+				  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, pass);
+	  
+				  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++;
+			
+				  if (sampleContributions) {
+					  passContributingSamples++;
+					  passSampleContributions += sampleContributions;
+				  }
+			  }
+		  }
 	}
-	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 == 50 ) {
-	// 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;
-	  }
-		
-	  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, pass);
-	  
-	  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++;
-			
-	  if (sampleContributions) {
-	    passContributingSamples++;
-	    passSampleContributions += sampleContributions;
-	  }
-	}
-      }
-    }
-
 
     totalSamples += passSamples;
@@ -211,72 +218,74 @@
     int pvsSize = 0;
     for (i=0; i < objects.size(); i++) {
-      Intersectable *object = objects[i];
-      pvsSize += object->mKdPvs.GetSize();
+		Intersectable *object = objects[i];
+		pvsSize += object->mKdPvs.GetSize();
     }
     
-    cout<<"pass "<<pass<<" : t = "<<TimeDiff(startTime, GetTime())*1e-3<<"s"<<endl;
-    cout<<"#totalSamples="<<totalSamples/1000<<
-      "k   #sampleContributions="<<passSampleContributions<<
-      " ("<<100*passContributingSamples/(float)passSamples<<"%)"<<
-      " avgPVS="<<pvsSize/(float)objects.size()<<endl<<
-      "avg ray contrib="<<passSampleContributions/(float)passContributingSamples<<
-      endl;
+	cout << "pass " << pass<<" : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
+	cout << "#totalSamples=" << totalSamples/1000 
+		 << "k   #sampleContributions=" << passSampleContributions << " (" 
+		 << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS="
+		 << pvsSize/(float)objects.size() << endl 
+		 << "avg ray contrib=" << passSampleContributions/(float)passContributingSamples << endl;
   }
 
   int totalPvsSize = mKdTree->CollectLeafPvs();
-  cout<<"#totalPvsSize="<<totalPvsSize<<endl;
+  cout << "#totalPvsSize=" << totalPvsSize << endl;
   
   //  HoleSamplingPass();
   if (1) {
-    Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
-    exporter->SetExportRayDensity(true);
-    exporter->ExportKdTree(*mKdTree);
-    delete exporter;
+	  Exporter *exporter = Exporter::GetExporter("ray-density.x3d");
+	  exporter->SetExportRayDensity(true);
+	  exporter->ExportKdTree(*mKdTree);
+	  delete exporter;
   }
   
   bool exportRays = false;
   if (exportRays) {
-    Exporter *exporter = NULL;
-    exporter = Exporter::GetExporter("sample-rays.x3d");
-    exporter->SetWireframe();
-    exporter->ExportKdTree(*mKdTree);
-    for (i=0; i < pvsOut; i++) 
-      exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0));
-    exporter->SetFilled();
-    delete exporter;
+	  Exporter *exporter = NULL;
+	  exporter = Exporter::GetExporter("sample-rays.x3d");
+	  exporter->SetWireframe();
+	  exporter->ExportKdTree(*mKdTree);
+
+	  for (i=0; i < pvsOut; i++) 
+		  exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0));
+	  exporter->SetFilled();
+	  
+	  delete exporter;
   }
 
   if (1) {
-    for (int k=0; k < pvsOut; k++) {
-      Intersectable *object = objects[k];
-      char s[64];
-      sprintf(s, "sample-pvs%04d.x3d", k);
-      Exporter *exporter = Exporter::GetExporter(s);
-      exporter->SetWireframe();
-      KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
-      Intersectable::NewMail();
-      // avoid adding the object to the list
-      object->Mail();
-      ObjectContainer visibleObjects;
-      for (; i != object->mKdPvs.mEntries.end(); i++) {
-	KdNode *node = (*i).first;
-	exporter->ExportBox(mKdTree->GetBox(node));
-	mKdTree->CollectObjects(node, visibleObjects);
-      }
-      
-      exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0));
-      exporter->SetFilled();
-
-      for (int j = 0; j < visibleObjects.size(); j++)
-	exporter->ExportIntersectable(visibleObjects[j]);
-	
-      Material m;
-      m.mDiffuseColor = RgbColor(1, 0, 0);
-      exporter->SetForcedMaterial(m);
-      exporter->ExportIntersectable(object);
-      
-      delete exporter;
-    }
-    
+	  for (int k=0; k < pvsOut; k++) {
+		  Intersectable *object = objects[k];
+		  char s[64];
+		  sprintf(s, "sample-pvs%04d.x3d", k);
+		  Exporter *exporter = Exporter::GetExporter(s);
+		  exporter->SetWireframe();
+		  KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
+		  
+		  Intersectable::NewMail();
+		  
+		  // avoid adding the object to the list
+		  object->Mail();
+		  ObjectContainer visibleObjects;
+		  for (; i != object->mKdPvs.mEntries.end(); i++) {
+			  KdNode *node = (*i).first;
+			  exporter->ExportBox(mKdTree->GetBox(node));
+			  mKdTree->CollectObjects(node, visibleObjects);
+		  }
+      
+		  exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0));
+		  exporter->SetFilled();
+
+		  for (int j = 0; j < visibleObjects.size(); j++)
+			  exporter->ExportIntersectable(visibleObjects[j]);
+	
+		  Material m;
+		  m.mDiffuseColor = RgbColor(1, 0, 0);
+		  exporter->SetForcedMaterial(m);
+		  exporter->ExportIntersectable(object);
+      
+		  delete exporter;
+	  }
   }
   
