Index: GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1733)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1895)
@@ -22,20 +22,29 @@
 	enum {OBJECT_SPACE, VIEW_SPACE};
 
-	SubdivisionCandidate(): mRenderCostDecrease(0), mDirty(true) {};
+	SubdivisionCandidate(): 
+		mRenderCostDecrease(0), 
+		mAvgRayContribution(0), 
+		mDirty(true) 
+		{}
 
 	virtual ~SubdivisionCandidate() {};
+	
 	/** Evaluate this subdivision candidate.
 	*/
 	virtual void EvalCandidate(bool computeSplitplane = true) = 0;
+	
 	/** Returns type of this subdivision candidate.
 	*/
 	virtual int Type() const = 0;
+	
 	/** Evaluate this candidate and put results into queue for further traversal.
 	*/
 	virtual bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet) = 0;
+	
 	/** Returns true of the global termination criteria of this split were met,
 		false otherwise.
 	*/
 	virtual bool GlobalTerminationCriteriaMet() const = 0;
+	
 	/** Collects subdivision candidates that were affected by the
 		application of this one.
@@ -54,4 +63,18 @@
 	{
 		return mRenderCostDecrease;
+	}
+
+	/** The average ray contribution of this candidate .
+		This is somewhat of a confidence value into the computed values. If
+		it is high, there is likely to be a lot of undersampling.
+	*/
+	inline void SetAvgRayContribution(const float rayContri)
+	{
+		mAvgRayContribution = rayContri;
+	}
+	
+	inline float GetAvgRayContribution() const
+	{
+		return mAvgRayContribution;
 	}
 
@@ -139,4 +162,7 @@
 	int mPvsEntriesIncr;
 
+	/// the average ray contribution of this candidate 
+	float mAvgRayContribution;
+
 	int mMailbox;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1887)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1895)
@@ -2645,5 +2645,11 @@
 					"true");
 
+	RegisterOption("Hierarchy.Construction.maxAvgRayContri",
+					optFloat,
+					"hierarchy_construction_max_avg_raycontri=",
+					"99999.0");
+	
 	/////////////////////////////////////////////////////////////////
+
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1893)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h	(revision 1895)
@@ -575,8 +575,11 @@
 	OspTree *mOspTree;
 
-	public:
+	// quick hack:
+public:
 	/// bounding volume hierarchy
 	BvHierarchy *mBvHierarchy;
 	
+	float mMaxAvgRayContri;
+
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1893)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1895)
@@ -587,5 +587,5 @@
 static float AvgRayContribution(const int pvs, const int nRays)
 {
-	return (float)pvs / (float)nRays;
+	return (float)pvs / ((float)nRays + Limits::Small);
 }
 
@@ -646,18 +646,29 @@
 								  renderCostDecr);
 	
-	// avg ray contri: this leaf is a pvs entry in all the view cells
-	// that see one of the objects.
-	const int numViewCells = CountViewCells(leaf->mObjects);
-	
-	const float avgRayContri = 
-		AvgRayContribution(numViewCells, splitCandidate.mParentData.mNumRays);
-
-	cout << "avgRayContri: " << avgRayContri << endl;
-
-	// avg ray contri very high. i.e., the result is influenced by undersampling
-	// => decrease priority
-	if (avgRayContri > 99925)
-	{
-		priority /= avgRayContri;
+	if (0)
+	{
+		// this leaf is a pvs entry in all the view cells
+		// that see one of the objects.
+		const int pvs = CountViewCells(leaf->mObjects);
+		//const int pvs = (int)leaf->mObjects.size();
+	
+		// avg contribution of a ray to a pvs
+		const float avgRayContri = 
+			AvgRayContribution(pvs, splitCandidate.mParentData.mNumRays);
+
+		Debug << "bvh avgRayContri: " << avgRayContri << " #pvs: " << pvs << " #rays: " << splitCandidate.mParentData.mNumRays << endl;
+		cout << "bvh avgRayContri: " << avgRayContri << endl;
+
+		// high avg ray contri, the result is influenced by undersampling
+		// => decrease priority
+		if (0 && (avgRayContri > mHierarchyManager->mMaxAvgRayContri))
+		{
+			const float factor = 1.0f + avgRayContri - mHierarchyManager->mMaxAvgRayContri;
+			cout << "here5 " << factor << endl;
+
+			priority /= factor;
+		}
+
+		//splitCandidate.SetAvgRayContri(avgRayContri);
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1891)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1895)
@@ -5108,5 +5108,4 @@
 : ViewCellsManager(vcTree), mHierarchyManager(hm)
 {
-	//cout<<"here4"<<endl;
 	Environment::GetSingleton()->GetIntValue("Hierarchy.Construction.samples", mInitialSamples);
 	Environment::GetSingleton()->GetBoolValue("ViewCells.compressObjects", mCompressObjects);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1893)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1895)
@@ -856,15 +856,25 @@
 	}
 
+
 	/////////////
 	// avg ray contri
 
-	const float avgRayContri = splitCandidate.mParentData.GetAvgRayContribution();
-	cout << "vsp avgRayContri: " << avgRayContri << endl;
-
-	// avg ray contri very high. i.e., the result is influenced by undersampling
-	// => decrease priority
-	if (avgRayContri > 99925)
-	{
-		priority /= avgRayContri;
+	if (0)
+	{
+		const int pvs = EvalPvsEntriesSize(*splitCandidate.mParentData.mRays);
+		const float avgRayContri = (float)pvs / ((float)splitCandidate.mParentData.mRays->size() + Limits::Small);
+
+		Debug << "vsp avgRayContri: " << avgRayContri << " #pvs: " << pvs << " #rays: " << splitCandidate.mParentData.mRays->size() << endl;
+		cout << "vsp avgRayContri: " << avgRayContri << endl;
+
+		// high avg ray contri very, the result is influenced by undersampling
+		// => decrease priority
+		if (0 && (avgRayContri > mHierarchyManager->mMaxAvgRayContri))
+		{
+			const float factor = 1.0f + avgRayContri - mHierarchyManager->mMaxAvgRayContri;
+			priority /= factor;
+		}
+
+		splitCandidate.SetAvgRayContribution(avgRayContri);
 	}
 
@@ -966,4 +976,5 @@
 		parent->ReplaceChildLink(leaf, interior);
 		interior->SetParent(parent);
+
 #if WORK_WITH_VIEWCELLS
 		// remove "parent" view cell from pvs of all objects (traverse trough rays)
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1893)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1895)
@@ -31,4 +31,6 @@
 #define STUPID_METHOD 0
 
+
+
 /*******************************************************************/
 /*              class HierarchyManager implementation              */
@@ -151,5 +153,8 @@
 		"Hierarchy.Construction.maxRepairs", mMaxRepairs);
 
-	// compare to bytes
+	Environment::GetSingleton()->GetFloatValue(
+		"Hierarchy.Construction.maxAvgRayContri", mMaxAvgRayContri);
+
+	// for comparing it with byte - value
 	mTermMaxMemory *= (1024.0f * 1024.0f);
 
@@ -168,4 +173,6 @@
 	Debug << "max steps of same kind: " << mMaxStepsOfSameType << endl;
 	Debug << "max repairs: " << mMaxRepairs << endl;
+	Debug << "max avg ray contribution: " << mMaxAvgRayContri << endl;
+
 
 	switch (mConstructionType)
@@ -769,8 +776,23 @@
 
 	// memory size in byte
-	mHierarchyStats.mMemory += (float)ObjectPvs::GetEntrySizeByte() * pvsEntriesIncr;
+	float mem = (float)ObjectPvs::GetEntrySizeByte() * pvsEntriesIncr;
+
+	// high avg ray contri, the result is influenced by undersampling
+	// => decrease priority
+	if (0 && (sc->GetAvgRayContribution() > mMaxAvgRayContri))
+	{
+		const float factor = 1.0f + sc->GetAvgRayContribution() - mMaxAvgRayContri;
+		cout << "here5 " << factor << endl;
+
+		mem *= factor;
+	}
+	
+	mHierarchyStats.mMemory += mem;
 	mHierarchyStats.mRenderCostDecrease = sc->GetRenderCostDecrease();
 	
 	mPriority = sc->GetPriority();
+
+	//////////
+	// show current memory
 
 	static float memoryCount = 0;
