Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2571)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2575)
@@ -26,5 +26,5 @@
 inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
 {
-	return obj1->mId < obj2->mId;
+  return obj1->mId < obj2->mId;
 }
 
@@ -33,8 +33,8 @@
 mParent(parent), mMailbox(0), mIntersectable(NULL)
 {
-	if (parent)
-		mDepth = parent->mDepth+1;
-	else
-		mDepth = 0;
+  if (parent)
+    mDepth = parent->mDepth+1;
+  else
+    mDepth = 0;
 }
 
@@ -42,7 +42,7 @@
 KdInterior::~KdInterior()
 {
-	// recursivly destroy children
-	DEL_PTR(mFront);
-	DEL_PTR(mBack);
+  // recursivly destroy children
+  DEL_PTR(mFront);
+  DEL_PTR(mBack);
 }
 
@@ -50,5 +50,5 @@
 KdLeaf::~KdLeaf()
 {
-	DEL_PTR(mViewCell);  
+  DEL_PTR(mViewCell);  
 }
 
@@ -57,13 +57,21 @@
 { 
   mRoot = new KdLeaf(NULL, 0);
-  Environment::GetSingleton()->GetIntValue("KdTree.Termination.maxNodes", mTermMaxNodes);
-  Environment::GetSingleton()->GetIntValue("KdTree.Termination.maxDepth", mTermMaxDepth);
-  Environment::GetSingleton()->GetIntValue("KdTree.Termination.minCost", mTermMinCost);
-  Environment::GetSingleton()->GetFloatValue("KdTree.Termination.maxCostRatio", mMaxCostRatio);
-  Environment::GetSingleton()->GetFloatValue("KdTree.Termination.ct_div_ci", mCt_div_ci);
-  Environment::GetSingleton()->GetFloatValue("KdTree.splitBorder", mSplitBorder);
-  Environment::GetSingleton()->GetFloatValue("KdTree.pvsArea", mKdPvsArea);
-
-  Environment::GetSingleton()->GetBoolValue("KdTree.sahUseFaces", mSahUseFaces);
+  Environment::GetSingleton()->GetIntValue("KdTree.Termination.maxNodes",
+					   mTermMaxNodes);
+  Environment::GetSingleton()->GetIntValue("KdTree.Termination.maxDepth",
+					   mTermMaxDepth);
+  Environment::GetSingleton()->GetIntValue("KdTree.Termination.minCost",
+					   mTermMinCost);
+  Environment::GetSingleton()->GetFloatValue("KdTree.Termination.maxCostRatio",
+					     mMaxCostRatio);
+  Environment::GetSingleton()->GetFloatValue("KdTree.Termination.ct_div_ci",
+					     mCt_div_ci);
+  Environment::GetSingleton()->GetFloatValue("KdTree.splitBorder",
+					     mSplitBorder);
+  Environment::GetSingleton()->GetFloatValue("KdTree.pvsArea",
+					     mKdPvsArea);
+
+  Environment::GetSingleton()->GetBoolValue("KdTree.sahUseFaces",
+					    mSahUseFaces);
 
   char splitType[64];
@@ -97,5 +105,4 @@
 KdTree::Construct()
 {
-
   if (!splitCandidates)
     splitCandidates = new vector<SortableEntry *>;
@@ -158,14 +165,13 @@
     
     KdNode *node = SubdivideNode((KdLeaf *) data.mNode,
-								 data.mBox,
-								 backBox,
-								 frontBox
-								 );
-
-	if (result == NULL)
+				 data.mBox,
+				 backBox,
+				 frontBox
+				 );
+
+    if (result == NULL)
       result = node;
     
     if (!node->IsLeaf()) {
-
       KdInterior *interior = (KdInterior *) node;
       // push the children on the stack
@@ -173,5 +179,6 @@
       tStack.push(TraversalData(interior->mFront, frontBox, data.mDepth+1));
       
-    } else {
+    }
+    else {
       EvaluateLeafStats(data);
     }
@@ -186,12 +193,12 @@
 KdTree::TerminationCriteriaMet(const KdLeaf *leaf)
 {
-	const bool criteriaMet =
-		((int)leaf->mObjects.size() <= mTermMinCost) ||
-		 (leaf->mDepth >= mTermMaxDepth);
- 
-	if (0 && criteriaMet)
-		cerr<<"\n OBJECTS="<<(int)leaf->mObjects.size()<<endl;
-
-	return criteriaMet;
+  const bool criteriaMet =
+    ((int)leaf->mObjects.size() <= mTermMinCost) ||
+    (leaf->mDepth >= mTermMaxDepth);
+  
+  if (0 && criteriaMet)
+    cerr<<"\n OBJECTS="<<(int)leaf->mObjects.size()<<endl;
+  
+  return criteriaMet;
 }
 
@@ -199,7 +206,7 @@
 int
 KdTree::SelectPlane(KdLeaf *leaf,
-					const AxisAlignedBox3 &box,
-					float &position
-					)
+		    const AxisAlignedBox3 &box,
+		    float &position
+		    )
 {
   int axis = -1;
@@ -217,33 +224,35 @@
       bool mOnlyDrivingAxis = true;
 
-	  if (mOnlyDrivingAxis) {
-		axis = box.Size().DrivingAxis();
-		costRatio = BestCostRatio(leaf,
-								  box,
-								  axis,
-								  position,
-								  objectsBack,
-								  objectsFront);
-      } else {
-		costRatio = MAX_FLOAT;
-		for (int i=0; i < 3; i++) {
-		  float p;
-		  float r = BestCostRatio(leaf,
-								  box,
-								  i,
-								  p,
-								  objectsBack,
-								  objectsFront);
-		  if (r < costRatio) {
-			costRatio = r;
-			axis = i;
-			position = p;
-		  }
-		}
+      if (mOnlyDrivingAxis) {
+	axis = box.Size().DrivingAxis();
+	costRatio = BestCostRatio(leaf,
+				  box,
+				  axis,
+				  position,
+				  objectsBack,
+				  objectsFront);
+      }
+      else {
+	// for all 3 axes
+	costRatio = MAX_FLOAT;
+	for (int i=0; i < 3; i++) {
+	  float p;
+	  float r = BestCostRatio(leaf,
+				  box,
+				  i,
+				  p,
+				  objectsBack,
+				  objectsFront);
+	  if (r < costRatio) {
+	    costRatio = r;
+	    axis = i;
+	    position = p;
+	  }
+	}
       }
       
       if (costRatio > mMaxCostRatio) {
-		//cout<<"Too big cost ratio "<<costRatio<<endl;
-		axis = -1;
+	//cout<<"Too big cost ratio "<<costRatio<<endl;
+	axis = -1;
       }
       break;
@@ -254,5 +263,5 @@
 }
 
-KdNode *
+KdNode*
 KdTree::SubdivideNode(
 		      KdLeaf *leaf,
@@ -261,8 +270,7 @@
 		      AxisAlignedBox3 &frontBBox
 		      )
-{
-  
+{  
   if (TerminationCriteriaMet(leaf))
-	  return leaf;
+    return leaf;
 
   float position;
@@ -275,5 +283,5 @@
   }
   
-  mStat.nodes+=2;
+  mStat.nodes += 2;
   mStat.splits[axis]++;
   
@@ -309,8 +317,6 @@
   }
 
-  
   KdLeaf *back = new KdLeaf(node, objectsBack);
   KdLeaf *front = new KdLeaf(node, objectsFront);
-
  
   // replace a link from node's parent
@@ -336,16 +342,16 @@
 
 
-	if (box.Max(axis) >= position )
-	{
+    if (box.Max(axis) >= position )
+    {
       front->mObjects.push_back(*mi);
-	  //++ (*mi)->mReferences;
-	}
+      //++ (*mi)->mReferences;
+    }
 	
     if (box.Min(axis) < position )
-	{
+    {
       back->mObjects.push_back(*mi);
-	// matt: no more ref
-	//  ++ (*mi)->mReferences;
-	}
+      // matt: no more ref
+      //  ++ (*mi)->mReferences;
+    }
 
     mStat.objectRefs -= (int)leaf->mObjects.size();
@@ -353,11 +359,11 @@
   }
 
-	// store objects referenced in more than one leaf
-	// for easy access
-	ProcessMultipleRefs(back);
-	ProcessMultipleRefs(front);
-
-	delete leaf;
-	return node;
+  // store objects referenced in more than one leaf
+  // for easy access
+  ProcessMultipleRefs(back);
+  ProcessMultipleRefs(front);
+  
+  delete leaf;
+  return node;
 }
 
@@ -365,17 +371,18 @@
 void KdTree::ProcessMultipleRefs(KdLeaf *leaf) const
 {
-	// find objects from multiple kd-leaves
-	ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
-
-	for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
-	{
-		Intersectable *object = *oit;
+  // find objects from multiple kd-leaves
+  ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
+
+  for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
+  {
+    Intersectable *object = *oit;
 		
-		// matt: no more ref
-		/*if (object->mReferences > 1)
-		{
-			leaf->mMultipleObjects.push_back(object);
-		}*/
-	}
+    // matt: no more ref
+    /*
+      if (object->mReferences > 1) {
+        leaf->mMultipleObjects.push_back(object);
+      }
+    */
+  }
 }
 
@@ -464,18 +471,18 @@
 void
 KdTree::SortSubdivisionCandidates(
-			    KdLeaf *node,
-			    const int axis
-			    )
-{
-	CLEAR_CONTAINER(*splitCandidates);
-	//splitCandidates->clear();
-
-    int requestedSize = 2*(int)node->mObjects.size();
-	
-	// creates a sorted split candidates array
-	if (splitCandidates->capacity() > 500000 &&
-		requestedSize < (int)(splitCandidates->capacity()/10) ) {		
-			delete splitCandidates;
-			splitCandidates = new vector<SortableEntry *>;
+				  KdLeaf *node,
+				  const int axis
+				  )
+{
+  CLEAR_CONTAINER(*splitCandidates);
+  //splitCandidates->clear();
+  
+  int requestedSize = 2*(int)node->mObjects.size();
+  
+  // creates a sorted split candidates array
+  if (splitCandidates->capacity() > 500000 &&
+      requestedSize < (int)(splitCandidates->capacity()/10) ) {		
+    delete splitCandidates;
+    splitCandidates = new vector<SortableEntry *>;
   }
   
@@ -487,15 +494,15 @@
       mi++) 
   {
-	  AxisAlignedBox3 box = (*mi)->GetBox();
-
-	  splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MIN,
-								 box.Min(axis),
-								 *mi)
-								 );
+    AxisAlignedBox3 box = (*mi)->GetBox();
+
+    splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MIN,
+						 box.Min(axis),
+						 *mi)
+			       );
     
-      splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX,
-								 box.Max(axis),
-								 *mi)
-								 );
+    splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX,
+						 box.Max(axis),
+						 *mi)
+			       );
   }
   
@@ -506,11 +513,11 @@
 float
 KdTree::BestCostRatio(
-					  KdLeaf *node,
-					  const AxisAlignedBox3 &box,
-					  const int axis,
-					  float &position,
-					  int &objectsBack,
-					  int &objectsFront
-					  )
+		      KdLeaf *node,
+		      const AxisAlignedBox3 &box,
+		      const int axis,
+		      float &position,
+		      int &objectsBack,
+		      int &objectsFront
+		      )
 {
 
@@ -531,5 +538,5 @@
   
   if (nodeId < 100)
-	costStream.open(filename);
+    costStream.open(filename);
 
 #endif
@@ -577,5 +584,5 @@
       intersectionsRight -= (*ci)->intersectable->IntersectionComplexity();
       break;
-    }
+    } // switch
 
     if ((*ci)->value > minBand && (*ci)->value < maxBand) {
@@ -587,7 +594,7 @@
       float sum;
       if (mSahUseFaces)
-		sum = intersectionsLeft*lbox.SurfaceArea() + intersectionsRight*rbox.SurfaceArea();
+	sum = intersectionsLeft*lbox.SurfaceArea() + intersectionsRight*rbox.SurfaceArea();
       else
-		sum = objectsLeft*lbox.SurfaceArea() + objectsRight*rbox.SurfaceArea();
+	sum = objectsLeft*lbox.SurfaceArea() + objectsRight*rbox.SurfaceArea();
       
       //      cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
@@ -595,21 +602,21 @@
 
 #if DEBUG_COST
-  if (nodeId < 100) {
+      if (nodeId < 100) {
 	float oldCost = mSahUseFaces ? totalIntersections : node->mObjects.size();
 	float newCost = mCt_div_ci + sum/boxArea;
 	float ratio = newCost/oldCost;
 	costStream<<(*ci)->value<<" "<<ratio<<endl;
-  }
+      }
 #endif
-	  
+      
       if (sum < minSum) {
-		minSum = sum;
-		position = (*ci)->value;
-		
-		objectsBack = objectsLeft;
-		objectsFront = objectsRight;
-      }
-    }
-  }
+	minSum = sum;
+	position = (*ci)->value;
+	
+	objectsBack = objectsLeft;
+	objectsFront = objectsRight;
+      }
+    }
+  } // for ci
   
   float oldCost = mSahUseFaces ? totalIntersections : node->mObjects.size();
@@ -627,6 +634,6 @@
 int
 KdTree::CastRay(
-				Ray &ray
-				)
+		Ray &ray
+		)
 {
   
@@ -666,25 +673,27 @@
 
       if (entp[axis] <= position) {
-		if (extp[axis] <= position) {
-		  node = in->mBack;
-		  // cases N1,N2,N3,P5,Z2,Z3
-		  continue;
-		} else {
-		  // case N4
-		  node = in->mBack;
-		  farChild = in->mFront;
-		}
+	if (extp[axis] <= position) {
+	  node = in->mBack;
+	  // cases N1,N2,N3,P5,Z2,Z3
+	  continue;
+	}
+	else {
+	  // case N4
+	  node = in->mBack;
+	  farChild = in->mFront;
+	}
       }
       else {
-		if (position <= extp[axis]) {
-		  node = in->mFront;
-		  // cases P1,P2,P3,N5,Z1
-		  continue;
-		} else {
-		  node = in->mFront;
-		  farChild = in->mBack;
-		  // case P4
-		}
-	  }
+	if (position <= extp[axis]) {
+	  node = in->mFront;
+	  // cases P1,P2,P3,N5,Z1
+	  continue;
+	}
+	else {
+	  node = in->mFront;
+	  farChild = in->mBack;
+	  // case P4
+	}
+      }
       // $$ modification 3.5.2004 - hints from Kamil Ghais
       // case N4 or P4
@@ -693,54 +702,55 @@
       extp = ray.GetLoc() + ray.GetDir()*tdist;
       maxt = tdist;
-	} else {
-	  // compute intersection with all objects in this leaf
-	  KdLeaf *leaf = (KdLeaf *) node;
-	  if (ray.mFlags & Ray::STORE_KDLEAVES)
-		ray.kdLeaves.push_back(leaf);
+    }
+    else {
+      // compute intersection with all objects in this leaf
+      KdLeaf *leaf = (KdLeaf *) node;
+      if (ray.mFlags & Ray::STORE_KDLEAVES)
+	ray.kdLeaves.push_back(leaf);
 	  
-	  ObjectContainer::const_iterator mi;
-	  for ( mi = leaf->mObjects.begin();
-			mi != leaf->mObjects.end();
-			mi++) {
-		Intersectable *object = *mi;
-		if (!object->Mailed() ) {
-		  object->Mail();
-		  if (ray.mFlags & Ray::STORE_TESTED_OBJECTS)
+      ObjectContainer::const_iterator mi;
+      for ( mi = leaf->mObjects.begin();
+	    mi != leaf->mObjects.end();
+	    mi++) {
+	Intersectable *object = *mi;
+	if (!object->Mailed() ) {
+	  object->Mail();
+	  if (ray.mFlags & Ray::STORE_TESTED_OBJECTS)
 			ray.testedObjects.push_back(object);
 		  
-		  static int oi=1;
-		  if (MeshDebug) 
-			cout<<"Object "<<oi++;
-		  
-		  hits += object->CastRay(ray);
-		  
-		  if (MeshDebug) {
-			if (!ray.intersections.empty()) 
-			  cout<<"nearest t="<<ray.intersections[0].mT<<endl;
-			else
-			  cout<<"nearest t=-INF"<<endl;
-		  }	  
-		}
-	  }
+	  static int oi=1;
+	  if (MeshDebug) 
+	    cout<<"Object "<<oi++;
 	  
-	  if (hits && ray.GetType() == Ray::LOCAL_RAY)
-		if (ray.intersections[0].mT <= maxt)
-		  break;
+	  hits += object->CastRay(ray);
 	  
-	  // get the next node from the stack
-	  if (tStack.empty())
-		break;
+	  if (MeshDebug) {
+	    if (!ray.intersections.empty()) 
+	      cout<<"nearest t="<<ray.intersections[0].mT<<endl;
+	    else
+	      cout<<"nearest t=-INF"<<endl;
+	  }	  
+	}
+      }
 	  
-	  entp = extp;
-	  mint = maxt;
-	  if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
-		break;
-	  
-	  RayTraversalData &s  = tStack.top();
-	  node = s.mNode;
-	  extp = s.mExitPoint;
-	  maxt = s.mMaxT;
-	  tStack.pop();
-	}
+      if (hits && ray.GetType() == Ray::LOCAL_RAY)
+	if (ray.intersections[0].mT <= maxt)
+	  break;
+      
+      // get the next node from the stack
+      if (tStack.empty())
+	break;
+      
+      entp = extp;
+      mint = maxt;
+      if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
+	break;
+      
+      RayTraversalData &s  = tStack.top();
+      node = s.mNode;
+      extp = s.mExitPoint;
+      maxt = s.mMaxT;
+      tStack.pop();
+    }
   }
   return hits;
@@ -748,110 +758,109 @@
 
 int KdTree::CastLineSegment(const Vector3 &origin,
-							const Vector3 &termination,
-							ViewCellContainer &viewcells)
-{
-	int hits = 0;
-
-	float mint = 0.0f, maxt = 1.0f;
-	const Vector3 dir = termination - origin;
-
-	stack<RayTraversalData> tStack;
-
-	Intersectable::NewMail();
-
-	//maxt += Limits::Threshold;
-
-	Vector3 entp = origin;
-	Vector3 extp = termination;
-
-	KdNode *node = mRoot;
-	KdNode *farChild;
-
-	float position;
-	int axis;
-
-	while (1)
+			    const Vector3 &termination,
+			    ViewCellContainer &viewcells)
+{
+  int hits = 0;
+
+  float mint = 0.0f, maxt = 1.0f;
+  const Vector3 dir = termination - origin;
+  
+  stack<RayTraversalData> tStack;
+  
+  Intersectable::NewMail();
+  
+  //maxt += Limits::Threshold;
+  
+  Vector3 entp = origin;
+  Vector3 extp = termination;
+  
+  KdNode *node = mRoot;
+  KdNode *farChild;
+  
+  float position;
+  int axis;
+
+  while (1)
+  {
+    if (!node->IsLeaf())
+    {
+      KdInterior *in = static_cast<KdInterior *>(node);
+      position = in->mPosition;
+      axis = in->mAxis;
+      
+      if (entp[axis] <= position)
+      {
+	if (extp[axis] <= position)
 	{
-		if (!node->IsLeaf())
-		{
-			KdInterior *in = static_cast<KdInterior *>(node);
-			position = in->mPosition;
-			axis = in->mAxis;
-
-			if (entp[axis] <= position)
-			{
-				if (extp[axis] <= position)
-				{
-					node = in->mBack;
-					// cases N1,N2,N3,P5,Z2,Z3
-					continue;
-				} 
-				else
-				{
-					// case N4
-					node = in->mBack;
-					farChild = in->mFront;
-				}
-			}
-			else
-			{
-				if (position <= extp[axis])
-				{
-					node = in->mFront;
-					// cases P1,P2,P3,N5,Z1
-					continue;
-				}
-				else
-				{
-					node = in->mFront;
-					farChild = in->mBack;
-					// case P4
-				}
-			}
-
- 			// $$ modification 3.5.2004 - hints from Kamil Ghais
-			// case N4 or P4
-			float tdist = (position - origin[axis]) / dir[axis];
-			//tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
-			extp = origin + dir * tdist;
-			maxt = tdist;
-		}
-		else
-		{
-			// compute intersection with all objects in this leaf
-			KdLeaf *leaf = static_cast<KdLeaf *>(node);
-
-			// add view cell to intersections
-			ViewCell *vc = leaf->mViewCell;
-
-			if (!vc->Mailed())
-			{
-				vc->Mail();
-				viewcells.push_back(vc);
-				++ hits;
-			}
-
-			// get the next node from the stack
-			if (tStack.empty())
-				break;
-
-			entp = extp;
-			mint = maxt;
-			
-			RayTraversalData &s  = tStack.top();
-			node = s.mNode;
-			extp = s.mExitPoint;
-			maxt = s.mMaxT;
-			tStack.pop();
-		}
-	}
-
-	return hits;
+	  node = in->mBack;
+	  // cases N1,N2,N3,P5,Z2,Z3
+	  continue;
+	} 
+	else
+	{
+	  // case N4
+	  node = in->mBack;
+	  farChild = in->mFront;
+	}
+      }
+      else
+      {
+	if (position <= extp[axis])
+	{
+	  node = in->mFront;
+	  // cases P1,P2,P3,N5,Z1
+	  continue;
+	}
+	else
+	{
+	  node = in->mFront;
+	  farChild = in->mBack;
+	  // case P4
+	}
+      }
+      
+      // $$ modification 3.5.2004 - hints from Kamil Ghais
+      // case N4 or P4
+      float tdist = (position - origin[axis]) / dir[axis];
+      //tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
+      extp = origin + dir * tdist;
+      maxt = tdist;
+    }
+    else
+    {
+      // compute intersection with all objects in this leaf
+      KdLeaf *leaf = static_cast<KdLeaf *>(node);
+      
+      // add view cell to intersections
+      ViewCell *vc = leaf->mViewCell;
+      
+      if (!vc->Mailed())
+      {
+	vc->Mail();
+	viewcells.push_back(vc);
+	++ hits;
+      }
+      
+      // get the next node from the stack
+      if (tStack.empty())
+	break;
+      
+      entp = extp;
+      mint = maxt;
+      
+      RayTraversalData &s  = tStack.top();
+      node = s.mNode;
+      extp = s.mExitPoint;
+      maxt = s.mMaxT;
+      tStack.pop();
+    }
+  }
+  return hits;
 }
 
 void
 KdTree::CollectKdObjects(const AxisAlignedBox3 &box,
-						 ObjectContainer &objects
-						 )
+			 ObjectContainer &objects
+			)
 {
   stack<KdNode *> nodeStack;
@@ -862,25 +871,26 @@
     KdNode *node = nodeStack.top();
     nodeStack.pop();
-	if (node->IsLeaf() || node->mPvsTermination == 1)  {
-	  Intersectable *object = GetOrCreateKdIntersectable(node);
-	  if (!node->Mailed()) {
-		node->Mail();
-		objects.push_back(object);
-	  }
-	} else {
+    if (node->IsLeaf() || node->mPvsTermination == 1)  {
+      Intersectable *object = GetOrCreateKdIntersectable(node);
+      if (!node->Mailed()) {
+	node->Mail();
+	objects.push_back(object);
+      }
+    }
+    else {
       KdInterior *interior = (KdInterior *)node;
 	  
-	  if ( box.Max()[interior->mAxis] > interior->mPosition )
-		nodeStack.push(interior->mFront);
-	  
-	  if (box.Min()[interior->mAxis] < interior->mPosition)
-		nodeStack.push(interior->mBack);
-    }
-  }
+      if ( box.Max()[interior->mAxis] > interior->mPosition )
+	nodeStack.push(interior->mFront);
+      
+      if (box.Min()[interior->mAxis] < interior->mPosition)
+	nodeStack.push(interior->mBack);
+    }
+  } // while
 }
 
 void
 KdTree::CollectObjects(const AxisAlignedBox3 &box,
-					   ObjectContainer &objects)
+		       ObjectContainer &objects)
 {
   stack<KdNode *> nodeStack;
@@ -894,18 +904,18 @@
       KdLeaf *leaf = (KdLeaf *)node;
       for (int j=0; j < leaf->mObjects.size(); j++) {
-		Intersectable *object = leaf->mObjects[j];
-		if (!object->Mailed() && Overlap(box, object->GetBox())) {
-		  object->Mail();
-		  objects.push_back(object);
-		}
+	Intersectable *object = leaf->mObjects[j];
+	if (!object->Mailed() && Overlap(box, object->GetBox())) {
+	  object->Mail();
+	  objects.push_back(object);
+	}
       }
     } else {
       KdInterior *interior = (KdInterior *)node;
 
-	  if ( box.Max()[interior->mAxis] > interior->mPosition )
-		nodeStack.push(interior->mFront);
- 
-	  if (box.Min()[interior->mAxis] < interior->mPosition)
-		nodeStack.push(interior->mBack);
+      if ( box.Max()[interior->mAxis] > interior->mPosition )
+	nodeStack.push(interior->mFront);
+      
+      if (box.Min()[interior->mAxis] < interior->mPosition)
+	nodeStack.push(interior->mBack);
     }
   }
@@ -925,9 +935,9 @@
       KdLeaf *leaf = (KdLeaf *)node;
       for (int j=0; j < leaf->mObjects.size(); j++) {
-				Intersectable *object = leaf->mObjects[j];
-				if (!object->Mailed()) {
-					object->Mail();
-					objects.push_back(object);
-				}
+	Intersectable *object = leaf->mObjects[j];
+	if (!object->Mailed()) {
+	  object->Mail();
+	  objects.push_back(object);
+	}
       }
     } else {
@@ -942,6 +952,6 @@
 KdNode *
 KdTree::FindRandomNeighbor(KdNode *n,
-						   bool onlyUnmailed
-						   )
+			   bool onlyUnmailed
+			   )
 {
   stack<KdNode *> nodeStack;
@@ -997,16 +1007,18 @@
       if ( node != n && (!onlyUnmailed || !node->Mailed()) ) 
 	neighbors.push_back(node);
-    } else {
+    }
+    else {
       KdInterior *interior = (KdInterior *)node;
       if (interior->mPosition > box.Max(interior->mAxis))
-				nodeStack.push(interior->mBack);
-      else
-				if (interior->mPosition < box.Min(interior->mAxis))
-					nodeStack.push(interior->mFront);
-				else {
-					// random decision
-					nodeStack.push(interior->mBack);
-					nodeStack.push(interior->mFront);
-				}
+	nodeStack.push(interior->mBack);
+      else {
+	if (interior->mPosition < box.Min(interior->mAxis))
+	  nodeStack.push(interior->mFront);
+	else {
+	  // random decision
+	  nodeStack.push(interior->mBack);
+	  nodeStack.push(interior->mFront);
+	}
+      }
     }
   }
@@ -1181,53 +1193,53 @@
 void KdTree::ExportBinLeaf(OUT_STREAM &stream, KdLeaf *leaf)
 {
-	ObjectContainer::const_iterator it, it_end = leaf->mObjects.end();
+  ObjectContainer::const_iterator it, it_end = leaf->mObjects.end();
+  
+  int type = TYPE_LEAF;
+  int size = (int)leaf->mObjects.size();
+  
+  stream.write(reinterpret_cast<char *>(&type), sizeof(int));
+  stream.write(reinterpret_cast<char *>(&size), sizeof(int));
+  
+  for (it = leaf->mObjects.begin(); it != it_end; ++ it)
+    {	
+      Intersectable *obj = *it;
+      int id = obj->mId;		
+      
+      //stream.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
+      stream.write(reinterpret_cast<char *>(&id), sizeof(int));
+    }
+}
+
+
+KdLeaf *KdTree::ImportBinLeaf(IN_STREAM &stream, 
+			      KdInterior *parent,
+			      const ObjectContainer &objects)
+{
+  int leafId = TYPE_LEAF;
+  int objId = leafId;
+  int size;
+  
+  stream.read(reinterpret_cast<char *>(&size), sizeof(int));
+  KdLeaf *leaf = new KdLeaf(parent, size);
+
+  MeshInstance dummyInst(NULL);
 	
-	int type = TYPE_LEAF;
-	int size = (int)leaf->mObjects.size();
-
-	stream.write(reinterpret_cast<char *>(&type), sizeof(int));
-	stream.write(reinterpret_cast<char *>(&size), sizeof(int));
-
-	for (it = leaf->mObjects.begin(); it != it_end; ++ it)
-	{	
-		Intersectable *obj = *it;
-		int id = obj->mId;		
-	
-		//stream.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
-		stream.write(reinterpret_cast<char *>(&id), sizeof(int));
-    }
-}
-
-
-KdLeaf *KdTree::ImportBinLeaf(IN_STREAM &stream, 
-							  KdInterior *parent,
-							  const ObjectContainer &objects)
-{
-	int leafId = TYPE_LEAF;
-	int objId = leafId;
-	int size;
-
-	stream.read(reinterpret_cast<char *>(&size), sizeof(int));
-	KdLeaf *leaf = new KdLeaf(parent, size);
-
-	MeshInstance dummyInst(NULL);
-	
-	// read object ids
-	// note: this can also be done geometrically
-	for (int i = 0; i < size; ++ i)
-	{	
-		stream.read(reinterpret_cast<char *>(&objId), sizeof(int));
-		dummyInst.SetId(objId);
-
-		ObjectContainer::const_iterator oit =
-			lower_bound(objects.begin(), objects.end(), (Intersectable *)&dummyInst, ilt);
-								
-		if ((oit != objects.end()) && ((*oit)->GetId() == objId))
-			leaf->mObjects.push_back(*oit);
-		else
-			Debug << "error: object with id " << objId << " does not exist" << endl;
-	}
-
-	return leaf;
+  // read object ids
+  // note: this can also be done geometrically
+  for (int i = 0; i < size; ++ i)
+    {	
+      stream.read(reinterpret_cast<char *>(&objId), sizeof(int));
+      dummyInst.SetId(objId);
+      
+      ObjectContainer::const_iterator oit =
+	lower_bound(objects.begin(), objects.end(), (Intersectable *)&dummyInst, ilt);
+      
+      if ((oit != objects.end()) && ((*oit)->GetId() == objId))
+	leaf->mObjects.push_back(*oit);
+      else
+	Debug << "error: object with id " << objId << " does not exist" << endl;
+    }
+  
+  return leaf;
 }
 
@@ -1319,82 +1331,82 @@
 bool KdTree::ImportBinTree(const string &filename, ObjectContainer &objects)
 {
-	// export binary version of mesh
-	queue<TraversalData> tStack;
-	IN_STREAM stream(filename.c_str(), IN_BIN_MODE);
-
-	if (!stream.is_open()) return false;
-
-	// sort objects by their id
-//	if (!is_sorted(objects.begin(), objects.end(), ilt))
-		sort(objects.begin(), objects.end(), ilt);
-
-	mBox.Initialize();
-	ObjectContainer::const_iterator oit, oit_end = objects.end();
-
-	///////////////////////////
-	//-- compute bounding box of object space
-
-    for (oit = objects.begin(); oit != oit_end; ++ oit)
-	{
-		const AxisAlignedBox3 box = (*oit)->GetBox();
-		mBox.Include(box);
-	}
-
-	// hack: we make a new root
-	DEL_PTR(mRoot);
-  
-	mRoot = ImportNextNode(stream, NULL, objects);
-
-	tStack.push(TraversalData(mRoot, mBox, 0));
-	mStat.Reset();
-	mStat.nodes = 1;
-
-	while(!tStack.empty())
-	{
-		TraversalData tData = tStack.front();
-		tStack.pop();
-
-		KdNode *node = tData.mNode;
-
-		if (!node->IsLeaf())
-		{
-			mStat.nodes += 2;
-
-			//Debug << "i" ;
-			KdInterior *interior = static_cast<KdInterior *>(node);
-			interior->mBox = tData.mBox;
-
-            KdNode *front = ImportNextNode(stream, interior, objects);
-			KdNode *back = ImportNextNode(stream, interior, objects);
-	
-			interior->SetupChildLinks(back, front);
-
-			++ mStat.splits[interior->mAxis];
-
-			// compute new bounding box
-			AxisAlignedBox3 frontBox, backBox;
-			
-			tData.mBox.Split(interior->mAxis, 
-							 interior->mPosition, 
-							 frontBox, 
-							 backBox);
-
-			tStack.push(TraversalData(front, frontBox, tData.mDepth + 1));			
-			tStack.push(TraversalData(back, backBox, tData.mDepth + 1));
-		}
-		else
-		{
-			EvaluateLeafStats(tData);
-			//cout << "l";
-		}
-	}
-
-	float area = GetBox().SurfaceArea()*mKdPvsArea;
-	
-	SetPvsTerminationNodes(area);
-
-	Debug << mStat << endl;
-
-	return true;
+  // export binary version of mesh
+  queue<TraversalData> tStack;
+  IN_STREAM stream(filename.c_str(), IN_BIN_MODE);
+  
+  if (!stream.is_open()) return false;
+  
+  // sort objects by their id
+  //	if (!is_sorted(objects.begin(), objects.end(), ilt))
+  sort(objects.begin(), objects.end(), ilt);
+  
+  mBox.Initialize();
+  ObjectContainer::const_iterator oit, oit_end = objects.end();
+  
+  ///////////////////////////
+  //-- compute bounding box of object space
+  
+  for (oit = objects.begin(); oit != oit_end; ++ oit)
+  {
+    const AxisAlignedBox3 box = (*oit)->GetBox();
+    mBox.Include(box);
+  }
+  
+  // hack: we make a new root
+  DEL_PTR(mRoot);
+  
+  mRoot = ImportNextNode(stream, NULL, objects);
+  
+  tStack.push(TraversalData(mRoot, mBox, 0));
+  mStat.Reset();
+  mStat.nodes = 1;
+  
+  while(!tStack.empty())
+  {
+    TraversalData tData = tStack.front();
+    tStack.pop();
+    
+    KdNode *node = tData.mNode;
+    
+    if (!node->IsLeaf())
+    {
+      mStat.nodes += 2;
+      
+      //Debug << "i" ;
+      KdInterior *interior = static_cast<KdInterior *>(node);
+      interior->mBox = tData.mBox;
+      
+      KdNode *front = ImportNextNode(stream, interior, objects);
+      KdNode *back = ImportNextNode(stream, interior, objects);
+      
+      interior->SetupChildLinks(back, front);
+      
+      ++ mStat.splits[interior->mAxis];
+      
+      // compute new bounding box
+      AxisAlignedBox3 frontBox, backBox;
+      
+      tData.mBox.Split(interior->mAxis, 
+		       interior->mPosition, 
+		       frontBox, 
+		       backBox);
+      
+      tStack.push(TraversalData(front, frontBox, tData.mDepth + 1));			
+      tStack.push(TraversalData(back, backBox, tData.mDepth + 1));
+    }
+    else
+    {
+      EvaluateLeafStats(tData);
+      //cout << "l";
+    }
+  }
+
+  float area = GetBox().SurfaceArea()*mKdPvsArea;
+  
+  SetPvsTerminationNodes(area);
+  
+  Debug << mStat << endl;
+  
+  return true;
 }
 
@@ -1403,6 +1415,6 @@
 KdTree::GetOrCreateKdIntersectable(KdNode *node)
 {
-	if (node == NULL)
-		return NULL;
+  if (node == NULL)
+    return NULL;
 
 	if (node->mIntersectable == NULL) 
@@ -1415,18 +1427,16 @@
 		mKdIntersectables.push_back(kdObj);
 		kdObj->SetId(id);
-
 #ifdef USE_BIT_PVS
-		// hack: for kd pvs the kd intersecables are the pvs objects
-		ObjectPvsIterator::sObjects.push_back(kdObj);
+    // hack: for kd pvs the kd intersecables are the pvs objects
+    ObjectPvsIterator::sObjects.push_back(kdObj);
 #endif
-	}
-
-	return node->mIntersectable;
+  }
+
+  return node->mIntersectable;
 }
 
 
 void
-KdTree::SetPvsTerminationNodes(
-							   const float maxArea)
+KdTree::SetPvsTerminationNodes(const float maxArea)
 {
   stack<KdNode *> nodeStack;
@@ -1440,28 +1450,28 @@
   while (!nodeStack.empty()) {
     KdNode *node = nodeStack.top();
-	nodeStack.pop();
-
-	node->mPvsTermination = 0;
-	if (node->IsLeaf() || (GetSurfaceArea(node) <= maxArea) ) {
-	  area += GetSurfaceArea(node);
-	  radius += GetBox(node).Radius();
-	  nodes++;
-	  node->mPvsTermination = 1;
-	  // create dummy kd intersectable
-	  Intersectable *object = GetOrCreateKdIntersectable(node);
-	} else {
-	  KdInterior *interior = (KdInterior *)node;
-	  nodeStack.push(interior->mFront);
-	  nodeStack.push(interior->mBack);
-    }
-  }
-
+    nodeStack.pop();
+
+    node->mPvsTermination = 0;
+    if (node->IsLeaf() || (GetSurfaceArea(node) <= maxArea) ) {
+      area += GetSurfaceArea(node);
+      radius += GetBox(node).Radius();
+      nodes++;
+      node->mPvsTermination = 1;
+      // create dummy kd intersectable
+      Intersectable *object = GetOrCreateKdIntersectable(node);
+    } else {
+      KdInterior *interior = (KdInterior *)node;
+      nodeStack.push(interior->mFront);
+      nodeStack.push(interior->mBack);
+    }
+  }
+  
   if (nodes) {
-	area /= nodes;
-	radius /= nodes;
-	cout<<"Number of nodes for storing in the PVS = "<<nodes<<endl;
-	cout<<"Average rel. node area = "<<area/GetSurfaceArea(mRoot)<<endl;
-	cout<<"Average rel. node radius = "<<radius/GetBox(mRoot).Radius()<<endl;
-	cout<<"Avg node radius = "<<radius<<endl;
+    area /= nodes;
+    radius /= nodes;
+    cout<<"Number of nodes for storing in the PVS = "<<nodes<<endl;
+    cout<<"Average rel. node area = "<<area/GetSurfaceArea(mRoot)<<endl;
+    cout<<"Average rel. node radius = "<<radius/GetBox(mRoot).Radius()<<endl;
+    cout<<"Avg node radius = "<<radius<<endl;
   }
   
@@ -1474,9 +1484,9 @@
   
   while (node->mPvsTermination == 0 ) {
-	KdInterior *inter = (KdInterior *)node;
-	if (point[inter->mAxis] < inter->mPosition)
-	  node = inter->mBack;
-	else
-	  node = inter->mFront;
+    KdInterior *inter = (KdInterior *)node;
+    if (point[inter->mAxis] < inter->mPosition)
+      node = inter->mBack;
+    else
+      node = inter->mFront;
   }
   
@@ -1491,9 +1501,9 @@
   
   while (!node->IsLeaf() && (GetSurfaceArea(node) > maxArea) ) {
-	KdInterior *inter = (KdInterior *)node;
-	if (point[inter->mAxis] < inter->mPosition)
-	  node = inter->mBack;
-	else
-	  node = inter->mFront;
+    KdInterior *inter = (KdInterior *)node;
+    if (point[inter->mAxis] < inter->mPosition)
+      node = inter->mBack;
+    else
+      node = inter->mFront;
   }
   
@@ -1503,37 +1513,37 @@
 
 void KdTree::GetBoxIntersections(const AxisAlignedBox3 &box,
-								 vector<KdLeaf *> &leaves)
-{
-	stack<KdNode *> tStack;
-
-	tStack.push(mRoot);
-
-	while (!tStack.empty())
+				 vector<KdLeaf *> &leaves)
+{
+  stack<KdNode *> tStack;
+  
+  tStack.push(mRoot);
+  
+  while (!tStack.empty())
+    {
+      KdNode *node = tStack.top();
+      tStack.pop();
+      
+      if (node->IsLeaf())
 	{
-		KdNode *node = tStack.top();
-		tStack.pop();
-		
-		if (node->IsLeaf())
-		{
-			leaves.push_back(static_cast<KdLeaf *>(node));
-		}
-		else // interior
-		{
-			KdInterior *interior = static_cast<KdInterior *>(node);
-
-			if (box.Max(interior->mAxis) >= interior->mPosition)
-			{
-				tStack.push(interior->mFront);
-			}
-
-			if (box.Min(interior->mAxis) < interior->mPosition)
-			{
-				tStack.push(interior->mBack);
-			}
-		}
-	}
-}
-
-
-
-}
+	  leaves.push_back(static_cast<KdLeaf *>(node));
+	}
+      else // interior
+	{
+	  KdInterior *interior = static_cast<KdInterior *>(node);
+	  
+	  if (box.Max(interior->mAxis) >= interior->mPosition)
+	    {
+	      tStack.push(interior->mFront);
+	    }
+	  
+	  if (box.Min(interior->mAxis) < interior->mPosition)
+	    {
+	      tStack.push(interior->mBack);
+	    }
+	}
+    }
+}
+
+
+
+}
