Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 422)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 423)
@@ -118,5 +118,6 @@
 }
 
-void VspKdTreeInterior::ReplaceChildLink(VspKdTreeNode *oldChild, VspKdTreeNode *newChild)
+void VspKdTreeInterior::ReplaceChildLink(VspKdTreeNode *oldChild, 
+										 VspKdTreeNode *newChild)
 {
 	if (mBack == oldChild)
@@ -139,11 +140,14 @@
 void VspKdTreeInterior::Print(ostream &s) const 
 {
-	if (mAxis == 0)
-		s << "x ";
-	else
-		if (mAxis == 1)
-			s << "y ";
-		else
-			s << "z ";
+	switch (mAxis)
+	{
+	case 0:
+		s << "x "; break;
+	case 1:
+		s << "y "; break;
+	case 2:
+		s << "z "; break;
+	}
+
 	s << mPosition << " ";
 
@@ -268,18 +272,5 @@
 	environment->GetStringValue("VspKdTree.splitType", sname);
 	string name(sname);
-	
-	if (name.compare("regular") == 0)
-		splitType = ESplitRegular;
-	else
-	{
-		if (name.compare("heuristic") == 0)
-			splitType = ESplitHeuristic;
-		else 
-		{
-			cerr << "Invalid VspKdTree split type " << name << endl;
-			exit(1);
-		}
-	}
-
+		
 	Debug << "======= vsp kd tree options ========" << endl;
 	Debug << "max depth: "<< mTermMaxDepth << endl;
@@ -289,4 +280,23 @@
 	Debug << "max cost ratio: "<< mTermMaxCostRatio << endl;
 	Debug << "min size: "<<mTermMinSize << endl;
+
+	if (name.compare("regular") == 0)
+	{
+		Debug << "using regular split" << endl;
+		splitType = ESplitRegular;
+	}
+	else
+	{
+		if (name.compare("heuristic") == 0)
+		{
+			Debug << "using heuristic split" << endl;
+			splitType = ESplitHeuristic;
+		}
+		else 
+		{
+			cerr << "Invalid VspKdTree split type " << name << endl;
+			exit(1);
+		}
+	}
 
 	mRoot = NULL;
@@ -415,5 +425,5 @@
 	mRoot = new VspKdTreeLeaf(NULL, (int)rays.size());
 
-	// first construct a leaf that will get subdivide
+	// first construct a leaf that will get subdivided
 	VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(mRoot);
 	
@@ -453,5 +463,5 @@
 
 	mStat.Print(cout);
-	cout<<"#Total memory=" << GetMemUsage() << endl;
+	Debug << "#Total memory=" << GetMemUsage() << endl;
 }
 
@@ -463,5 +473,5 @@
 
 	priority_queue<TraversalData> tStack;
-	//  stack<TraversalData> tStack;
+	//stack<TraversalData> tStack;
   
 	tStack.push(tdata);
@@ -471,9 +481,10 @@
   
 	int lastMem = 0;
+
 	while (!tStack.empty()) 
 	{
 		float mem = GetMemUsage();
 		
-		if ( lastMem/10 != ((int)mem)/10) 
+		if (lastMem / 10 != ((int)mem) / 10) 
 		{
 			cout << mem << " MB" << endl;
@@ -481,6 +492,7 @@
 		lastMem = (int)mem;
 		
-		if (mem > mMaxMemory) 
-		{
+		if (1 && mem > mMaxMemory) 
+		{
+			Debug << "memory limit reached: " << mem << endl;
 			// count statistics on unprocessed leafs
 			while (!tStack.empty()) 
@@ -493,6 +505,5 @@
     
 		TraversalData data = tStack.top();
-		tStack.pop();
-    
+		tStack.pop();    
 		
 		VspKdTreeNode *node = SubdivideNode((VspKdTreeLeaf *) data.mNode,
@@ -503,5 +514,5 @@
 		if (!node->IsLeaf()) 
 		{
-			VspKdTreeInterior *interior = (VspKdTreeInterior *) node;
+			VspKdTreeInterior *interior = dynamic_cast<VspKdTreeInterior *>(node);
 
 			// push the children on the stack
@@ -542,9 +553,7 @@
 										 pvsBack,
 										 pvsFront);
-
 	} 
-	else 
-	{
-		if (splitType == ESplitHeuristic)
+	else if (splitType == ESplitHeuristic)
+	{
 			costRatio = BestCostRatioHeuristic(leaf,
 											   axis,	
@@ -554,18 +563,19 @@
 											   pvsBack,
 											   pvsFront);
-		else {
-			cerr << "VspKdTree: Unknown split heuristics\n";
-			exit(1);
-		}
-  }
+	}
+	else 
+	{
+		cerr << "VspKdTree: Unknown split heuristics\n";
+		exit(1);
+	}
 
 	if (costRatio > mTermMaxCostRatio) 
 	{
-		cout<<"Too big cost ratio " << costRatio << endl;
+		cout << "Too big cost ratio " << costRatio << endl;
 		return -1;
 	}
 
 #if 1	
-	cout <<
+	Debug <<
 		"pvs=" << leaf->mPvsSize <<
 		" rays=" << (int)leaf->mRays.size() <<
@@ -880,7 +890,9 @@
 											  const AxisAlignedBox3 &box) const
 {
-	return ((leaf->GetPvsSize() < mTermMinPvs) || (leaf->mRays.size() < mTermMinRays) ||
+	return ((leaf->GetPvsSize() < mTermMinPvs) || 
+		    //(leaf->mRays.size() < mTermMinRays) ||
 			// (leaf->GetAvgRayContribution() > termMaxRayContribution ) ||
-			 (leaf->mDepth >= mTermMaxDepth) || SqrMagnitude(box.Size()) <= mTermMinSize);
+			(leaf->mDepth >= mTermMaxDepth) || 
+			(SqrMagnitude(box.Size()) <= mTermMinSize));
 }
 
@@ -893,13 +905,14 @@
 	if (TerminationCriteriaMet(leaf, box))
 	{
-
-#if 1
-		if (leaf->mDepth >= mTermMaxDepth) 
-		{
-			Debug << "Warning: max depth reached depth="<<(int)leaf->mDepth<<" rays=" << (int)leaf->mRays.size() << endl;
-			Debug << "Bbox: " << GetBBox(leaf) << endl;
-		}
-#endif
+		if (1)
+		{
+			if (leaf->mDepth >= mTermMaxDepth) 
+			{
+				Debug << "Warning: max depth reached depth=" << (int)leaf->mDepth<<" rays=" << (int)leaf->mRays.size() << endl;
+				Debug << "Bbox: " << GetBBox(leaf) << endl;
+			}
 		
+			Debug << "depth: " << (int)leaf->mDepth << " pvs: " << leaf->GetPvsSize() << " rays: " << leaf->mRays.size() << endl;
+		}
 		return leaf;
 	}
@@ -913,8 +926,7 @@
 	
 	// select subdivision axis
-	int axis = SelectPlane( leaf, box, position, raysBack, raysFront, pvsBack, pvsFront);
-
-	//	cout<<"rays back="<<raysBack<<" rays front="<<raysFront<<" pvs back="<<pvsBack<<" pvs front="<<
-	//		pvsFront<<endl;
+	int axis = SelectPlane(leaf, box, position, raysBack, raysFront, pvsBack, pvsFront);
+
+	Debug << "rays back=" << raysBack << " rays front=" << raysFront << " pvs back=" << pvsBack << " pvs front=" <<	pvsFront << endl;
 
 	if (axis == -1) 
@@ -1040,5 +1052,5 @@
 	tstack.push(mRoot);
 
-	while (!tstack.empty()) 
+	while (!tstack.empty())
 	{
 		VspKdTreeNode *node = tstack.top();
@@ -1078,6 +1090,4 @@
 
 
-
-
 VspKdTreeNode *VspKdTree::SubdivideLeaf(VspKdTreeLeaf *leaf,
 										const float sizeThreshold)
@@ -1095,5 +1105,5 @@
 		(SqrMagnitude(leafBBox.Size()) > sizeThreshold) )
 	{
-    	// memory check and realese...
+    	// memory check and release
 		if (GetMemUsage() > mMaxTotalMemory) 
 			ReleaseMemory(pass);
@@ -1109,7 +1119,6 @@
 
 
-void
-VspKdTree::UpdateRays(VssRayContainer &remove,
-					  VssRayContainer &add)
+void VspKdTree::UpdateRays(VssRayContainer &remove,
+						   VssRayContainer &add)
 {
 	VspKdTreeLeaf::NewMail();
@@ -1135,6 +1144,5 @@
 
 	//  cout<<"all/inactive"<<remove.size()<<"/"<<inactive<<endl;
-  
-	for(VssRayContainer::const_iterator ri = add.begin(); ri != add.end(); ++ ri) 
+	for (VssRayContainer::const_iterator ri = add.begin(); ri != add.end(); ++ ri) 
 	{
 		AddRay(*ri);
@@ -1256,4 +1264,5 @@
 			// find the ray in the leaf and swap it with the last ray
 			VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(data.mNode);
+
 			leaf->AddRay(data.mRayData);
 			++ mStat.addedRayRefs;
@@ -1416,5 +1425,5 @@
 
 #if DEBUG_COLLAPSE
-	cout<<"Total memory before="<<GetMemUsage()<<endl;
+	cout << "Total memory before=" << GetMemUsage() << endl;
 #endif
 
@@ -1537,7 +1546,7 @@
 	}
 	
-	cout << "sum=" << sumRayContribution << endl;
-	cout << "leaves=" << leaves << endl;
-	avgRayContribution = sumRayContribution/(float)leaves;
+	Debug << "sum=" << sumRayContribution << endl;
+	Debug << "leaves=" << leaves << endl;
+	avgRayContribution = sumRayContribution / (float)leaves;
 }
 
@@ -1556,5 +1565,6 @@
 		if (node->IsLeaf()) 
 		{
-			VspKdTreeLeaf *leaf = (VspKdTreeLeaf *)node;
+			VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(node);
+
 			float c = leaf->GetAvgRayContribution();
 			int num = (int)(c*ratioPerLeaf + 0.5);
