Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 516)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 569)
@@ -42,4 +42,6 @@
   // total number of nodes
   int nodes;
+  // number of leaves
+  int leaves;
   // number of splits along each of the axes
   int splits[7];
@@ -90,5 +92,5 @@
   int Nodes() const {return nodes;}
   int Interior() const { return nodes/2; }
-  int Leaves() const { return (nodes/2) + 1; }
+  int Leaves() const { return Nodes() - Interior(); }
 
   void Reset() {
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 563)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 569)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.0) on: ?t 19. I 16:46:41 2006
+# Generated by qmake (2.00a) (Qt 4.1.0) on: po 23. I 11:09:15 2006
 # Project:  preprocessor.pro
 # Template: app
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 564)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 569)
@@ -182,5 +182,5 @@
 
 	/** Get a viewcell containing the specified point */
-	virtual ViewCell *GetViewCell(const Vector3 &point) = 0;
+	virtual ViewCell *GetViewCell(const Vector3 &point) const = 0;
   
 	virtual void PrintPvsStatistics(ostream &s);
@@ -267,4 +267,21 @@
 							 int maxPvsSize) const;
 
+  /** sets validy of all viewcells */
+  	virtual void SetValidity(
+							 int minPvsSize, 
+							 int maxPvsSize) const;
+
+  /** set valid viewcells in the range of pvs. sorts the viewcells
+	  according to the pvs and then pickups those in the ranges */
+  
+  void
+  SetValidityPercentage(
+						const float minValid,
+						const float maxValid
+						);
+
+  int
+  CountValidViewcells() const;
+
 	/** Returns maximal allowed pvs size.
 	*/
@@ -417,5 +434,5 @@
 
 	/** Get a viewcell containing the specified point */
-	ViewCell *GetViewCell(const Vector3 &point);
+	ViewCell *GetViewCell(const Vector3 &point) const;
 
 	void CreateMesh(ViewCell *vc);
@@ -482,5 +499,5 @@
 	*/
 	//  virtual void PrintStatistics(ostream &s) const;
-	ViewCell *GetViewCell(const Vector3 &point) { return NULL; }
+	ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
 
 	float GetProbability(ViewCell *viewCell);
@@ -540,5 +557,5 @@
 						ViewCellContainer &viewcells);
 
-	ViewCell *GetViewCell(const Vector3 &point) { return NULL; }
+	ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
 
 	float GetProbability(ViewCell *viewCell);
@@ -600,5 +617,5 @@
 	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
 	
-	ViewCell *GetViewCell(const Vector3 &point);
+	ViewCell *GetViewCell(const Vector3 &point) const;
 
 	bool GetViewPoint(Vector3 &viewPoint) const;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 564)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 569)
@@ -137,4 +137,9 @@
 	bool GetValid() const;
 
+  static bool SmallerPvs(const ViewCell *a,
+						 const ViewCell *b) {
+	return a->GetPvs().GetSize() < b->GetPvs().GetSize();
+  }
+
 protected:
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 567)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 569)
@@ -336,6 +336,6 @@
 	  GetRoot(objects[i])->bbox = objects[i]->GetBox();
 	}
-
 	stat.nodes = i;
+	stat.leaves = i;
   } else {
 	mRoots.resize(1);
@@ -347,4 +347,5 @@
 	mRoots[0] = leaf;
 	stat.nodes = 1;
+	stat.leaves = 1;
   }
 
@@ -356,5 +357,5 @@
 	  
 	  // first construct a leaf that will get subdivide
-	  RssTreeLeaf *leaf = (RssTreeLeaf *) GetRoot(info.GetObject());
+	  RssTreeLeaf *leaf = (RssTreeLeaf *) GetRoot(info.GetSourceObject());
 
 	  leaf->AddRay(info);
@@ -1094,4 +1095,5 @@
   
   stat.nodes+=2;
+  stat.leaves += 1;
   stat.splits[axis]++;
 
@@ -1410,5 +1412,5 @@
   stack<RayTraversalData> tstack;
 
-  RssTreeNode *root = GetRoot(info.GetObject());
+  RssTreeNode *root = GetRoot(info.GetSourceObject());
   tstack.push(RayTraversalData(root, info));
   
@@ -1550,4 +1552,5 @@
 
   stat.nodes -= collapsedNodes - 1;
+  stat.leaves -= collapsedNodes/2 - 1;
   stat.rayRefs -= totalRayCount - rayCount;
   
@@ -2771,7 +2774,10 @@
 {
   if (mPerObjectTree && object) {
-	int id = object->GetId();
-	if (id >= mRoots.size())
+	int id = object->GetId()-1;
+	if (id < 0 || id >= mRoots.size()) {
+	  Debug<<"Error: object Id out of range, Id="<<id<<" roots.size()="<<mRoots.size()<<
+		endl<<flush;
 	  id = mRoots.size()-1; // $$ last tree is used by all unsigned objects
+	}
 	return mRoots[id];
   } else
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 567)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 569)
@@ -489,6 +489,11 @@
 	  "#TotalSamples\n" <<totalSamples<<endl<<
 	  "#RssSamples\n" <<rssSamples<<endl;
-	
-
+
+	{
+	  VssRayContainer contributingRays;
+	  mVssRays.GetContributingRays(contributingRays, mPass);
+	  mStats<<"#NUM_CONTRIBUTING_RAYS\n"<<(int)contributingRays.size()<<endl;
+	}
+	
 	mVssRays.PrintStatistics(mStats);
 	mViewCellsManager->PrintPvsStatistics(mStats);
@@ -543,6 +548,9 @@
   
   // viewcells->UpdatePVS(newVssRays);
-  
-
+  Debug<<"Valid viewcells before set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
+  // cull viewcells with PVS > median (0.5f)
+  mViewCellsManager->SetValidityPercentage(0, 0.5f); 
+  Debug<<"Valid viewcells after set validity: "<<mViewCellsManager->CountValidViewcells()<<endl;
+  
   while (1) {
 	SimpleRayContainer rays;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 567)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 569)
@@ -13,7 +13,7 @@
 #;../data/vienna/vienna-plane.x3d
 # filename ../data/vienna/viewcells-25-sel.x3d
-# filename ../data/atlanta/atlanta2.x3d
+filename ../data/atlanta/atlanta2.x3d
 #filename ../data/soda/soda.dat
-filename ../data/soda/soda5.dat
+# filename ../data/soda/soda5.dat
 }
 
@@ -25,5 +25,5 @@
 #	type vss
 	type rss
-	detectEmptyViewSpace true
+	detectEmptyViewSpace false
 	pvsRenderErrorSamples 10000
 
@@ -97,5 +97,5 @@
 RssTree {
 	epsilon		1e-6
-	perObjectTree true
+	perObjectTree false
 
 	maxDepth 	40
@@ -200,6 +200,6 @@
 	}
 	
-	filename ../data/soda/viewcells_soda5-2.xml
-#	filename ../data/atlanta/viewcells_atlanta2.xml
+#	filename ../data/soda/viewcells_soda5-2.xml
+	filename ../data/atlanta/viewcells_atlanta2.xml
 #	filename ../data/atlanta/atlanta_viewcells_large.x3d
 #	filename ../data/vienna/viewcells-25-sel.x3d
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 568)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 569)
@@ -124,11 +124,12 @@
 									 int maxPvsSize) const
 {
-	if ((vc->GetPvs().GetSize() > mMaxPvsSize) ||
-		(vc->GetPvs().GetSize() < mMinPvsSize))
-	{
-		return false;
-	}
-
-	return true;
+
+  if ((vc->GetPvs().GetSize() > maxPvsSize) ||
+	  (vc->GetPvs().GetSize() < minPvsSize))
+	{
+	  return false;
+	}
+  
+  return true;
 }
 
@@ -141,4 +142,42 @@
 }
 
+void
+ViewCellsManager::SetValidity(
+							  int minPvsSize, 
+							  int maxPvsSize) const
+{
+  ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+  
+  for (it = mViewCells.begin(); it != it_end; ++ it) {
+	SetValidity(*it, minPvsSize, maxPvsSize);
+  }
+}
+
+void
+ViewCellsManager::SetValidityPercentage(
+										const float minValid,
+										const float maxValid
+										)
+{
+  sort(mViewCells.begin(), mViewCells.end(), ViewCell::SmallerPvs);
+  
+  int start = mViewCells.size()*minValid;
+  int end = mViewCells.size()*maxValid;
+  
+  for (int i=0; i < mViewCells.size(); i++)
+	mViewCells[i]->SetValid(i >= start && i <= end);
+}
+
+int
+ViewCellsManager::CountValidViewcells() const
+{
+  ViewCellContainer::const_iterator it, it_end = mViewCells.end();
+  int valid = 0;
+  for (it = mViewCells.begin(); it != it_end; ++ it) {
+	if ((*it)->GetValid())
+	  valid++;
+  }
+  return valid;
+}
 
 bool ViewCellsManager::LoadViewCells(const string filename, ObjectContainer *objects)
@@ -171,5 +210,14 @@
 bool ViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
 {
+  if (!ViewCellsConstructed())
 	return mViewSpaceBox.IsInside(viewPoint);
+  else {
+	if (!mViewSpaceBox.IsInside(viewPoint))
+	  return false;
+	ViewCell *viewcell = GetViewCell(viewPoint);
+	if (!viewcell || !viewcell->GetValid())
+	  return false;
+  }
+  return true;
 }
 
@@ -487,6 +535,8 @@
   for (it = viewcells->begin(); it != viewcells->end(); ++it) {
 	ViewCell *viewcell = *it;
-	// if ray not outside of view space
-	viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
+	if (viewcell->GetValid()) {
+	  // if ray not outside of view space
+	  viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
+	}
   }
 }
@@ -526,7 +576,7 @@
   if (storeViewcells)
   {
-	  ray.mViewCells.reserve(viewcells.size());
-	  ray.mViewCells = viewcells;
-	  }
+	ray.mViewCells.reserve(viewcells.size());
+	ray.mViewCells = viewcells;
+  }
 
   ViewCellContainer::const_iterator it = viewcells.begin();
@@ -535,12 +585,14 @@
   for (; it != viewcells.end(); ++it) {
 	ViewCell *viewcell = *it;
-	// if ray not outside of view space
-	float contribution;
-	if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
-												 ray.mPdf,
-												 contribution
-												 ))
-	  ray.mPvsContribution++;
-	ray.mRelativePvsContribution += contribution;
+	if (viewcell->GetValid()) {
+	  // if ray not outside of view space
+	  float contribution;
+	  if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
+												   ray.mPdf,
+												   contribution
+												   ))
+		ray.mPvsContribution++;
+	  ray.mRelativePvsContribution += contribution;
+	}
   }
 
@@ -548,8 +600,10 @@
 	for (it = viewcells.begin(); it != viewcells.end(); ++it) {
 	  ViewCell *viewcell = *it;
-	  // if ray not outside of view space
-	  viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
-	}
-
+	  if (viewcell->GetValid()) {
+		// if ray not outside of view space
+		viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
+	  }
+	}
+  
   return ray.mRelativePvsContribution;
 }
@@ -1179,5 +1233,5 @@
 
 
-ViewCell *BspViewCellsManager::GetViewCell(const Vector3 &point)
+ViewCell *BspViewCellsManager::GetViewCell(const Vector3 &point) const
 {
   if (!mBspTree)
@@ -1880,4 +1934,6 @@
 }
 
+
+
 int VspBspViewCellsManager::Construct(const ObjectContainer &objects,
 									  const VssRayContainer &rays)
@@ -2209,6 +2265,10 @@
 bool VspBspViewCellsManager::ViewPointValid(const Vector3 &viewPoint) const
 {
-	return mViewSpaceBox.IsInside(viewPoint) &&
-		   mVspBspTree->ViewPointValid(viewPoint);
+  // $$JB -> implemented in viewcellsmanager (slower, but allows dynamic
+  // validy update in preprocessor for all managers)
+  return ViewCellsManager::ViewPointValid(viewPoint);
+  
+  //	return mViewSpaceBox.IsInside(viewPoint) &&
+  //		   mVspBspTree->ViewPointValid(viewPoint);
 }
 
@@ -2518,5 +2578,5 @@
 
 
-ViewCell *VspBspViewCellsManager::GetViewCell(const Vector3 &point)
+ViewCell *VspBspViewCellsManager::GetViewCell(const Vector3 &point) const
 {
 	if (!mVspBspTree)
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 563)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 569)
@@ -202,7 +202,7 @@
 							0.0f);
   
-  //  viewcells->GetViewPoint(mViewPoint);
-
-  mViewPoint =  mSceneGraph->GetBox().GetPoint(pVector);
+  mViewCellsManager->GetViewPoint(mViewPoint);
+
+  // mViewPoint =  mSceneGraph->GetBox().GetPoint(pVector);
   
   mViewDirection = Normalize(Vector3(sin(dVector.x),
