Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 463)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 464)
@@ -553,6 +553,4 @@
   bool mSplitUseOnlyDrivingAxis;
   
-  // use ray space subdivision instead of view space subdivision
-  bool mUseRss;
 
   // interleave directional and spatial splits based on their costs
@@ -853,4 +851,10 @@
 
 
+  void
+  PickEdgeRays(RssTreeLeaf *leaf,
+			   int &r1,
+			   int &r2
+			   );
+
 };
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 463)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 464)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (2.00a) (Qt 4.1.0-rc1) on: út 13. XII 21:49:08 2005
+# Generated by qmake (2.00a) (Qt 4.1.0-rc1) on: st 14. XII 10:37:40 2005
 # Project:  preprocessor.pro
 # Template: app
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 463)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 464)
@@ -1553,5 +1553,4 @@
   RegisterOption("RssTree.splitType", optString, "split=", "queries");
   RegisterOption("RssTree.splitUseOnlyDrivingAxis", optBool, "splitdriving=", "false");
-  RegisterOption("RssTree.useRss", optBool, "rss=", "false");
   
   RegisterOption("RssTree.numberOfEndPointDomains", optInt, "endpoints=", "10000");
@@ -1574,5 +1573,12 @@
   RegisterOption("RssTree.dirSplitDepth", optInt, "dirsplidepth=", "10");
 
-
+  RegisterOption("RssPreprocessor.Export.pvs", optBool, "rss_export_pvs", "false");
+  RegisterOption("RssPreprocessor.Export.rssTree", optBool, "rss_export_rss_tree", "false");
+  RegisterOption("RssPreprocessor.Export.rays", optBool, "rss_export_rays", "false");
+  RegisterOption("RssPreprocessor.Export.numRays", optInt, "rss_export_num_rays=", "5000");
+  RegisterOption("RssPreprocessor.useViewcells", optBool, "rss_use_viewcells", "false");
+  RegisterOption("RssPreprocessor.updateSubdivision", optBool, "rss_update_subdivision", "false");
+
+  
   /************************************************************************************/
   /*               View space partition BSP tree related options                      */
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 463)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 464)
@@ -121,5 +121,4 @@
   environment->GetBoolValue("RssTree.randomize", randomize);
   environment->GetBoolValue("RssTree.splitUseOnlyDrivingAxis", mSplitUseOnlyDrivingAxis);
-  environment->GetBoolValue("RssTree.useRss", mUseRss);
 
   environment->GetBoolValue("RssTree.interleaveDirSplits", mInterleaveDirSplits);
@@ -292,6 +291,4 @@
   dirBBox.Initialize();
 
-  if (mUseRss)
-	forcedBoundingBox = NULL;
 
   mForcedBoundingBox = forcedBoundingBox;
@@ -990,6 +987,5 @@
 		   //			 (leaf->GetAvgRayContribution() > termMaxRayContribution ) ||
 		   (leaf->depth >= termMaxDepth) ||
-		   (SqrMagnitude(GetBBox(leaf).Size()) <= termMinSize) ||
-		   (mUseRss && leaf->mPassingRays == leaf->rays.size())
+		   (SqrMagnitude(GetBBox(leaf).Size()) <= termMinSize) 
 		   );
 }
@@ -1769,4 +1765,32 @@
 
 void
+RssTree::PickEdgeRays(RssTreeLeaf *leaf,
+					  int &r1,
+					  int &r2
+					  )
+{
+  int nrays = leaf->rays.size();
+
+  if (nrays == 2) {
+	r1 = 0;
+	r2 = 1;
+	return;
+  }
+  
+  int tries = min(20, nrays);
+
+  while (--tries) {
+	r1 = Random(nrays);
+	r2 = Random(nrays);
+	if (leaf->rays[r1].mRay->mTerminationObject != leaf->rays[r2].mRay->mTerminationObject)
+	  break;
+  }
+
+  if (r1 == r2)
+	r2 = (r1+1)%leaf->rays.size();
+}
+
+
+void
 RssTree::GenerateLeafRays(RssTreeLeaf *leaf,
 						  const int numberOfRays,
@@ -1776,5 +1800,5 @@
   int nrays = leaf->rays.size();
   for (int i=0; i < numberOfRays; i++) {
-	bool useExtendedConvexCombination = (nrays >= 3) && (i > numberOfRays/2);
+	bool useExtendedConvexCombination = (nrays >= 2) && (i > numberOfRays/2);
 
 		
@@ -1783,24 +1807,17 @@
 	if (useExtendedConvexCombination) {
 	  // pickup 3 random rays
-	  int r1 = Random(nrays);
-	  int r2 = Random(nrays);
-	  int r3 = Random(nrays);
+	  int r1, r2;
+	  PickEdgeRays(leaf, r1, r2);
 	  
 	  Vector3 o1 = leaf->rays[r1].GetOrigin();
-	  
 	  Vector3 o2 = leaf->rays[r2].GetOrigin();
 	  
-	  Vector3 o3 = leaf->rays[r3].GetOrigin();
-	  
-	  const float overlap = 0.0;
-	  
-
-	  float w1, w2, w3;
-	  GenerateExtendedConvexCombinationWeights(w1, w2, w3, overlap);
-	  origin = w1*o1 + w2*o2 + w3*o3;
+	  const float overlap = 0.1f;
+	  float w1, w2;
+	  GenerateExtendedConvexCombinationWeights2(w1, w2, overlap);
+	  origin = w1*o1 + w2*o2;
 	  direction =
 		w1*leaf->rays[r1].mRay->GetDir() +
-		w2*leaf->rays[r2].mRay->GetDir() +
-		w3*leaf->rays[r3].mRay->GetDir();
+		w2*leaf->rays[r2].mRay->GetDir();
 	  // shift the origin a little bit
 	  origin += direction*0.1f;
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 463)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 464)
@@ -44,10 +44,24 @@
   Intersectable *mOriginObject;
   Intersectable *mTerminationObject;
-	
+
+  ////////////////////////
+  // members related to importance sampling
+  // sampling pass in which this ray was generated
+  short mPass;
+
+  // number of cells where this ray made a contribution to the PVS
+  short mPvsContribution;
+  
+  // sum of relative ray contributions per object
+  float mRelativePvsContribution;
+  
   //////////////////////////////
-  VssRay(const Vector3 &origin,
+  VssRay(
+		 const Vector3 &origin,
 		 const Vector3 &termination,
 		 Intersectable *originObject,
-		 Intersectable *terminationObject):
+		 Intersectable *terminationObject,
+		 const int pass = 0
+		 ):
 	mMailbox(-1),
     mOrigin(origin),
@@ -56,5 +70,6 @@
 	mTerminationObject(terminationObject),
 	mRefCount(0),
-    mFlags(0)
+    mFlags(0),
+	mPass(pass)
 	//	mT(1.0f)
   {
@@ -66,5 +81,6 @@
 	mFlags(0),
 	mMailbox(-1),
-	mOriginObject(ray.sourceObject.mObject)
+	mOriginObject(ray.sourceObject.mObject),
+	mPass(0)
 	//	mT(1.0f)
 	{
@@ -223,4 +239,10 @@
 										 );
 
+void
+GenerateExtendedConvexCombinationWeights2(float &w1,
+										  float &w2,
+										  const float overlap
+										  );
+
 // --------------------------------------------------------------
 // For sorting rays
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 463)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 464)
@@ -16,11 +16,4 @@
 static bool fromBoxVisibility = false;
 
-static bool exportPvs = false;
-static bool exportRssTree = false;
-
-static int numExportRays = 5000;
-// static int numExportRays = 0;
-
-static bool useViewcells = false;
 
 RssPreprocessor::RssPreprocessor():
@@ -35,5 +28,13 @@
   environment->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling);
   environment->GetIntValue("BspTree.Construction.samples", mBspConstructionSamples);
-	
+
+  environment->GetBoolValue("RssPreprocessor.Export.pvs", mExportPvs);
+  environment->GetBoolValue("RssPreprocessor.Export.rssTree", mExportRssTree);
+  environment->GetBoolValue("RssPreprocessor.Export.rays", mExportRays);
+  environment->GetIntValue("RssPreprocessor.Export.numRays", mExportNumRays);
+  environment->GetBoolValue("RssPreprocessor.useViewcells", mUseViewcells);
+
+  environment->GetBoolValue("RssPreprocessor.updateSubdivision", mUpdateSubdivision);
+  
   mStats.open("stats.log");
 }
@@ -130,5 +131,7 @@
 						  pointA,
 						  objectB,
-						  objectA);
+						  objectA,
+						  mPass
+						  );
 	  vssRays.push_back(vssRay);
 	  hits ++;
@@ -139,5 +142,7 @@
 						  pointB,
 						  objectA,
-						  objectB);
+						  objectB,
+						  mPass
+						  );
 	  vssRays.push_back(vssRay);
 	  hits ++;
@@ -385,5 +390,6 @@
 RssPreprocessor::ComputeVisibility()
 {
-  
+  cout<<"Rss Preprocessor started\n"<<flush;
+  cout<<"Memory/ray "<<sizeof(VssRay)+sizeof(RssTreeNode::RayInfo)<<endl;
   mSceneGraph->CollectObjects(&mObjects);
 	
@@ -487,16 +493,15 @@
   
 
-  if (numExportRays) {
+  if (mExportRays) {
 	char filename[64];
 	sprintf(filename, "rss-rays-initial.x3d");
-	ExportRays(filename, mVssRays, numExportRays);
-  }
-  
-  if (useViewcells)
+	ExportRays(filename, mVssRays, mExportNumRays);
+  }
+  
+  if (mUseViewcells)
 	// construct view cells
 	mViewCellsManager->Construct(mObjects, mVssRays, mViewSpaceBox);
 
   rssTree = new RssTree;
-  // viewcells = Construct(mVssRays);
 	
   if (fromBoxVisibility)
@@ -507,5 +512,5 @@
   cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
 
-  if (exportRssTree) {
+  if (mExportRssTree) {
 	ExportRssTree("rss-tree-100.x3d", rssTree, Vector3(1,0,0));
 	ExportRssTree("rss-tree-001.x3d", rssTree, Vector3(0,0,1));
@@ -515,5 +520,5 @@
   }
 
-  if (exportPvs) {
+  if (mExportPvs) {
 	ExportPvs("rss-pvs-initial.x3d", rssTree);
   }
@@ -522,4 +527,5 @@
   // get viewcells as kd tree boxes
   vector<AxisAlignedBox3> kdViewcells;
+
   if (0) {
 	vector<KdLeaf *> leaves;
@@ -560,5 +566,5 @@
 	rssTree->AddRays(vssRays);
 		
-	if (1) {
+	if (mUpdateSubdivision) {
 	  int subdivided = rssTree->UpdateSubdivision();
 	  cout<<"subdivided leafs = "<<subdivided<<endl;
@@ -567,10 +573,10 @@
 	}
 
-	if (useViewcells) {
+	if (mUseViewcells) {
 	  float avgPvs = GetAvgPvsSize(rssTree, kdViewcells);
 	  cout<<"Average PVS size = "<<avgPvs<<endl;
 	}
 
-	if (numExportRays) {
+	if (mExportRays) {
 	  char filename[64];
 	  if (mUseImportanceSampling)
@@ -579,10 +585,10 @@
 		sprintf(filename, "rss-rays-%04d.x3d", pass);
 	  
-	  ExportRays(filename, vssRays, numExportRays);
+	  ExportRays(filename, vssRays, mExportNumRays);
 	}
 
 	samples+=num;
 
-	if (useViewcells) {
+	if (mUseViewcells) {
 	  
 	  //-- prepare traversal rays for view cell intersections
@@ -625,5 +631,5 @@
 	}
 	
-	if (exportPvs) {
+	if (mExportPvs) {
 	  char filename[64];
 	  sprintf(filename, "rss-pvs-%04d.x3d", pass);
@@ -638,5 +644,5 @@
   }
   
-  if (useViewcells) {
+  if (mUseViewcells) {
 	
 	//-- post process view cells
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 450)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 464)
@@ -26,5 +26,4 @@
    
   char buff[128];
-  
   environment->GetStringValue("Preprocessor.type", buff);
   string preprocessorType(buff);
@@ -32,22 +31,22 @@
   Preprocessor *p;
 
-	if (preprocessorType == "vss")
-		p = new VssPreprocessor();
+  if (preprocessorType == "vss")
+	p = new VssPreprocessor();
+  else
+	if (preprocessorType == "rss")
+	  p = new RssPreprocessor();
 	else
-	  if (preprocessorType == "rss")
-		p = new RssPreprocessor();
+	  if (preprocessorType == "exact")
+		p = new ExactPreprocessor();
 	  else
-		if (preprocessorType == "exact")
-		  p = new ExactPreprocessor();
-		else
-		  if (preprocessorType == "sampling")
-				p = new SamplingPreprocessor();
-		  else {
-			cerr<<"Unknown preprocessor type"<<endl;
-			Debug<<"Unknown preprocessor type"<<endl;
-			exit(1);
-		  }
-	
-	
+		if (preprocessorType == "sampling")
+		  p = new SamplingPreprocessor();
+		else {
+		  cerr<<"Unknown preprocessor type"<<endl;
+		  Debug<<"Unknown preprocessor type"<<endl;
+		  exit(1);
+		}
+  
+  
 	environment->GetStringValue("Scene.filename", buff);
 	string filename(buff);
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.cpp	(revision 459)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.cpp	(revision 464)
@@ -158,4 +158,14 @@
 
 void
+GenerateExtendedConvexCombinationWeights2(float &w1,
+										  float &w2,
+										  const float overlap
+										  )
+{
+  w1 = RandomValue(-overlap, 1.0f + overlap);
+  w2 = 1.0f - w1;
+}
+
+void
 GenerateExtendedConvexCombinationWeights(float &w1,
 										 float &w2,
@@ -164,6 +174,22 @@
 										 )
 {
-  w1 = RandomValue(-overlap, 1.0f + overlap);
-  w2 = RandomValue(-overlap, 1.0f + overlap);
-  w3 = 1.0f - w1 - w2;
+
+  while (1) {
+	w1 = RandomValue(-overlap, 1.0f + overlap);
+	w2 = RandomValue(-overlap, 1.0f + overlap);
+	w3 = 1.0f + overlap - w1 - w2;
+	if (w3 >= -overlap && w3 <= 1.0f + overlap)
+	  break;
+	/*
+	w3 = RandomValue(0.0f, 1.0f + overlap);
+	
+	float sum = w1 + w2 + w3;
+	if (sum>0.0f) {
+	  w1/=sum;
+	  w2/=sum;
+	  w3/=sum;
+	  break;
+	}
+	*/
+  }
 }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 463)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 464)
@@ -65,11 +65,21 @@
 	samplesPerPass  100000
 	initialSamples 500000
-	vssSamples 50000000
+	vssSamples 10000000
 	vssSamplesPerPass 500000
 	useImportanceSampling true
+
+	Export {
+		pvs false
+		rssTree false
+		rays true
+		numRays 5000
+	}
+
+	useViewcells false
+	updateSubdivision false
 }
 
 RssTree {
-	useRss    false
+
 	epsilon		1e-6
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.h	(revision 451)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.h	(revision 464)
@@ -21,4 +21,12 @@
   bool mUseImportanceSampling;
 
+  bool mExportPvs;
+  bool mExportRssTree;
+  bool mExportRays;
+  int mExportNumRays;
+
+  bool mUseViewcells;
+  bool mUpdateSubdivision;
+  
   AxisAlignedBox3 *mViewSpaceBox;
 
