Index: trunk/VUT/GtpVisibilityPreprocessor/src/Makefile
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 382)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 401)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (1.07a) (Qt 3.3.2) on: Sat Nov 05 19:00:35 2005
+# Generated by qmake (1.07a) (Qt 3.3.2) on: Wed Nov 09 22:44:25 2005
 # Project:  preprocessor.pro
 # Template: app
@@ -228,4 +228,5 @@
 		common.h \
 		Material.h \
+		VssRay.h \
 		Parser.h \
 		Mesh.h \
@@ -235,4 +236,5 @@
 		Pvs.h \
 		Ray.h \
+		Polygon3.h \
 		
 
@@ -261,4 +263,5 @@
 		Material.h \
 		Exporter.h \
+		VssRay.h \
 		
 
@@ -281,4 +284,5 @@
 		common.h \
 		Ray.h \
+		Polygon3.h \
 		
 
@@ -342,4 +346,5 @@
 		ViewCell.h \
 		Polygon3.h \
+		VssRay.h \
 		Containers.h \
 		AxisAlignedBox3.h \
@@ -367,4 +372,5 @@
 		common.h \
 		Exporter.h \
+		VssRay.h \
 		Material.h \
 		Pvs.h \
@@ -442,4 +448,5 @@
 		common.h \
 		Ray.h \
+		Polygon3.h \
 		VssRay.h \
 		
@@ -466,5 +473,8 @@
 		Exporter.h \
 		Containers.h \
-		Material.h \
+		VssRay.h \
+		Material.h \
+		Vector3.h \
+		common.h \
 		
 
@@ -485,4 +495,5 @@
 		Intersectable.h \
 		Material.h \
+		VssRay.h \
 		
 
@@ -546,4 +557,5 @@
 		common.h \
 		Material.h \
+		VssRay.h \
 		Intersectable.h \
 		
@@ -621,4 +633,5 @@
 		Pvs.h \
 		Rectangle3.h \
+		VssRay.h \
 		
 
@@ -642,4 +655,5 @@
 		VssRay.h \
 		Intersectable.h \
+		Ray.h \
 		AxisAlignedBox3.h \
 		Statistics.h \
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h	(revision 395)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h	(revision 401)
@@ -29,4 +29,5 @@
 
 #include "Statistics.h"
+#include "Ray.h"
 
 // --------------------------------------------------------------
@@ -412,5 +413,5 @@
 
 	void
-	EvalPvsSize();
+	UpdatePvsSize();
 
   void Mail() { mailbox = mailID; }
@@ -423,5 +424,5 @@
 
 	float GetAvgRayContribution() const {
-		return GetPvsSize()/(float)rays.size();
+		return GetPvsSize()/((float)rays.size() + Limits::Small);
 	}
 };
@@ -610,5 +611,13 @@
 													VssRayContainer &add
 													);
-	
+
+	virtual void AddRays(
+											 VssRayContainer &add
+											 )
+	{
+		VssRayContainer remove;
+		UpdateRays(remove, add);
+	}
+
   
 	
@@ -772,5 +781,19 @@
 	int
 	GetPvsSize(VssTreeNode *node, const AxisAlignedBox3 &box) const;
-	
+
+	void
+	GetRayContributionStatistics(
+															 float &minRayContribution,
+															 float &maxRayContribution,
+															 float &avgRayContribution
+															 );
+
+	int
+	GenerateRays(const float ratioPerLeaf,
+							 SimpleRayContainer &rays);
+
+	float
+	GetAvgPvsSize();
+
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 392)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 401)
@@ -290,4 +290,13 @@
 };
 
+struct SimpleRay
+{
+	Vector3 mOrigin;
+	Vector3 mDirection;
+	SimpleRay() {}
+	SimpleRay(const Vector3 &o, const Vector3 &d):mOrigin(o), mDirection(d) {}
+};
+
+typedef vector<SimpleRay> SimpleRayContainer;
 
 #endif 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp	(revision 395)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp	(revision 401)
@@ -23,4 +23,5 @@
 #include "VssRay.h"
 #include "Intersectable.h"
+#include "Ray.h"
 
 
@@ -197,5 +198,5 @@
 
 void
-VssTreeLeaf::EvalPvsSize()
+VssTreeLeaf::UpdatePvsSize()
 {
 	if (!mValidPvs) {
@@ -273,5 +274,5 @@
 
   stat.rays = leaf->rays.size();
-	leaf->EvalPvsSize();
+	leaf->UpdatePvsSize();
   stat.initialPvsSize = leaf->GetPvsSize();
   // Subdivide();
@@ -1372,2 +1373,112 @@
 	return pvsSize;
 }
+
+void
+VssTree::GetRayContributionStatistics(
+																			float &minRayContribution,
+																			float &maxRayContribution,
+																			float &avgRayContribution
+																			)
+{
+	stack<VssTreeNode *> tstack;
+  tstack.push(root);
+	
+	minRayContribution = 1.0f;
+	maxRayContribution = 0.0f;
+	float sumRayContribution = 0.0f;
+	int leaves = 0;
+
+  while (!tstack.empty()) {
+    VssTreeNode *node = tstack.top();
+    tstack.pop();
+		
+    if (node->IsLeaf()) {
+			leaves++;
+			VssTreeLeaf *leaf = (VssTreeLeaf *)node;
+			float c = leaf->GetAvgRayContribution();
+			if (c > maxRayContribution)
+				maxRayContribution = c;
+			if (c < minRayContribution)
+				minRayContribution = c;
+			sumRayContribution += c;
+			
+		} else {
+			VssTreeInterior *in = (VssTreeInterior *)node;
+			// both nodes for directional splits
+			tstack.push(in->front);
+			tstack.push(in->back);
+		}
+	}
+	
+	cout<<"sum="<<sumRayContribution<<endl;
+	cout<<"leaves="<<leaves<<endl;
+	avgRayContribution = sumRayContribution/(float)leaves;
+}
+
+
+int
+VssTree::GenerateRays(const float ratioPerLeaf,
+											SimpleRayContainer &rays)
+{
+	stack<VssTreeNode *> tstack;
+  tstack.push(root);
+	
+  while (!tstack.empty()) {
+    VssTreeNode *node = tstack.top();
+    tstack.pop();
+		
+    if (node->IsLeaf()) {
+			VssTreeLeaf *leaf = (VssTreeLeaf *)node;
+			float c = leaf->GetAvgRayContribution();
+			int num = (c*ratioPerLeaf + 0.5);
+			//			cout<<num<<" ";
+
+			for (int i=0; i < num; i++) {
+				Vector3 origin = GetBBox(leaf).GetRandomPoint();
+				Vector3 dirVector = GetDirBBox(leaf).GetRandomPoint();
+				Vector3 direction = Vector3(sin(dirVector.x), sin(dirVector.y), cos(dirVector.x));
+				//cout<<"dir vector.x="<<dirVector.x<<"direction'.x="<<atan2(direction.x, direction.y)<<endl;
+				rays.push_back(SimpleRay(origin, direction));
+			}
+			
+		} else {
+			VssTreeInterior *in = (VssTreeInterior *)node;
+			// both nodes for directional splits
+			tstack.push(in->front);
+			tstack.push(in->back);
+		}
+	}
+
+	return rays.size();
+}
+
+
+float
+VssTree::GetAvgPvsSize()
+{
+	stack<VssTreeNode *> tstack;
+  tstack.push(root);
+
+	int sumPvs = 0;
+	int leaves = 0;
+  while (!tstack.empty()) {
+    VssTreeNode *node = tstack.top();
+    tstack.pop();
+		
+    if (node->IsLeaf()) {
+			VssTreeLeaf *leaf = (VssTreeLeaf *)node;
+			// update pvs size
+			leaf->UpdatePvsSize();
+			sumPvs += leaf->GetPvsSize();
+			leaves++;
+		} else {
+			VssTreeInterior *in = (VssTreeInterior *)node;
+			// both nodes for directional splits
+			tstack.push(in->front);
+			tstack.push(in->back);
+		}
+	}
+
+
+	return sumPvs/(float)leaves;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 387)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 401)
@@ -39,5 +39,6 @@
 VssPreprocessor::CastRay(
 												 Vector3 &viewPoint,
-												 Vector3 &direction
+												 Vector3 &direction,
+												 VssRayContainer &vssRays
 												 )
 {
@@ -60,6 +61,6 @@
 		box.ComputeMinMaxT(ray, &tmin, &tmax);
 		if (tmax > bsize) {
-			cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl;
-			cerr<<"ray"<<ray<<endl;
+			//			cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl;
+			//			cerr<<"ray"<<ray<<endl;
 		}
 		pointA = ray.Extrap(tmax);
@@ -85,6 +86,6 @@
 		box.ComputeMinMaxT(ray, &tmin, &tmax);
 		if (tmax > bsize) {
-			cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl;
-			cerr<<"ray"<<ray<<endl;
+			//			cerr<<"Warning: tmax > box size tmax="<<tmax<<" tmin="<<tmin<<" size="<<bsize<<endl;
+			//			cerr<<"ray"<<ray<<endl;
 		}
 		
@@ -108,5 +109,5 @@
 													objectB,
 													objectA);
-			mVssRays.push_back(vssRay);
+			vssRays.push_back(vssRay);
 			hits ++;
 		}
@@ -117,5 +118,5 @@
 													objectA,
 													objectB);
-			mVssRays.push_back(vssRay);
+			vssRays.push_back(vssRay);
 			hits ++;
 		}
@@ -150,4 +151,34 @@
 }
 
+int
+VssPreprocessor::RandomizedImportanceSampling(VssTree *vssTree,
+																							const int desiredSamples)
+{
+	float minRayContribution;
+	float maxRayContribution;
+	float avgRayContribution;
+
+	vssTree->GetRayContributionStatistics(minRayContribution,
+																				maxRayContribution,
+																				avgRayContribution);
+
+	cout<<
+		"#MIN_RAY_CONTRIB\n"<<minRayContribution<<endl<<
+		"#MAX_RAY_CONTRIB\n"<<maxRayContribution<<endl<<
+		"#AVG_RAY_CONTRIB\n"<<avgRayContribution<<endl;
+	
+	float p = desiredSamples/(float)(avgRayContribution*vssTree->stat.Leaves());
+	SimpleRayContainer rays;
+	int num = vssTree->GenerateRays(p, rays);
+	cout<<"Generated "<<num<<" rays."<<endl;
+	
+	VssRayContainer  vssRays;
+	
+	for (int i=0; i < rays.size(); i++)
+		CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
+
+	vssTree->AddRays(vssRays);
+	return num;
+}
 
 
@@ -175,8 +206,10 @@
 	}
 	
-	bool useViewSpaceBox = true;
+	bool useViewSpaceBox = false;
 	if (useViewSpaceBox)
 		viewSpaceBox = &box;
-	
+
+	VssTree *vssTree = NULL;
+
   while (totalSamples < mTotalSamples) {
 		int passContributingSamples = 0;
@@ -193,5 +226,5 @@
 			Vector3 direction = GetDirection(viewpoint);
 			
-			sampleContributions = CastRay(viewpoint, direction);
+			sampleContributions = CastRay(viewpoint, direction, mVssRays);
 			
 			
@@ -226,4 +259,8 @@
 			"#AvgPVS\n"<< pvsSize/(float)mObjects.size() << endl <<
 			"#AvgRayContrib\n" << avgRayContrib << endl;
+
+
+
+		
 	}
 	
@@ -257,12 +294,35 @@
 	cout<<"done."<<endl<<flush;
 
-	VssTree *vssTree = new VssTree;
-
+
+
+
+	vssTree = new VssTree;
+	
 	vssTree->Construct(mVssRays, viewSpaceBox);
 
 	cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
-	
-
-
+
+	
+	int samples = 0;
+	for (int i=0; i < 50; i++) {
+		int num = 100000;
+		if (0) {
+			VssRayContainer vssRays;
+			for (int j=0; j < num; j++) {
+				Vector3 viewpoint = GetViewpoint(viewSpaceBox);
+				Vector3 direction = GetDirection(viewpoint);
+				CastRay(viewpoint, direction, vssRays);
+			}
+			vssTree->AddRays(vssRays);
+		} else {
+			num = RandomizedImportanceSampling(vssTree, num);
+		}
+		samples+=num;
+		float pvs = vssTree->GetAvgPvsSize();
+		cout<<samples<<" avgPVS ="<<pvs<<endl;
+	}
+
+	delete vssTree;
+	
   return true;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 387)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 401)
@@ -24,5 +24,5 @@
 
 VssPreprocessor {
-	totalSamples 100000
+	totalSamples 200000
 	samplesPerPass  50000
 }
@@ -32,8 +32,8 @@
 
 	maxDepth 	40
-	minPvs		10
+	minPvs		5
 	minSize		0.00001
 	maxCostRatio	0.95
-	maxRayContribution 0.2
+	maxRayContribution 0.3
 	
 	maxTotalMemory	400
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 386)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 401)
@@ -7,4 +7,5 @@
 #include "Preprocessor.h"
 #include "VssRay.h"
+class VssTree;
 
 /** Sampling based visibility preprocessing. The implementation is based on heuristical
@@ -44,5 +45,7 @@
 	CastRay(
 					Vector3 &viewPoint,
-					Vector3 &direction
+					Vector3 &direction,
+					VssRayContainer &vssRays
+
 					);
 		
@@ -51,4 +54,7 @@
 	virtual bool BuildBspTree() { return false; }
 
+	int
+	RandomizedImportanceSampling(VssTree *vssTree, const int desiredSamples);
+
 };
 
