Index: GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1259)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1297)
@@ -46,4 +46,11 @@
 	}
 
+	/** Position in queue.
+	*/
+	int GetPosition() const
+	{
+		return mPosition;
+	}
+
 protected:
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1297)
@@ -2316,7 +2316,7 @@
 
 
-/***********************************************************************/
-/*            Bounding Volume Hierarchy related options                */
-/***********************************************************************/
+/**********************************************************************/
+/*            Bounding Volume Hierarchy related options               */
+/**********************************************************************/
 
 	RegisterOption("BvHierarchy.Construction.randomize",
@@ -2365,14 +2365,4 @@
 					"4");
 
-	RegisterOption("BvHierarchy.Termination.ct_div_ci",
-					optFloat,
-					"bvh_term_ct_div_ci=",
-					"0");
-	
-	RegisterOption("BvHierarchy.Construction.epsilon",
-				   optFloat,
-				   "bvh_construction_epsilon=",
-				   "0.00001");
-	
 	// if only the driving axis is used for axis aligned split
 	RegisterOption("BvHierarchy.splitUseOnlyDrivingAxis", 
@@ -2395,9 +2385,4 @@
 					"bvh_subdivision_stats=",
 					"bvhSubdivisionStats.log");
-
-	RegisterOption("BvHierarchy.Construction.splitBorder",
-					optFloat,
-					"bvh_construction_split_border=",
-					"0.01");
 
 	RegisterOption("BvHierarchy.Construction.renderCostDecreaseWeight",
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1292)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1297)
@@ -695,5 +695,5 @@
 	  
 	  CastRays(rays, tmpVssRays);
-	  castRays += rays.size();
+	  castRays += (int)rays.size();
 #if ADD_RAYS_IN_PLACE 
 	  contributions[0] = mViewCellsManager->ComputeSampleContributions(tmpVssRays, true, false);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1297)
@@ -37,5 +37,5 @@
 class KdTreeStatistics;
 class SubdivisionCandidate;
-
+class VspSubdivisionCandidate;
 
 /** View space partition statistics.
@@ -161,21 +161,16 @@
 	*/
 	virtual bool IsRoot() const;
-
 	/** Returns parent node.
 	*/
 	VspInterior *GetParent();
-
 	/** Sets parent node.
 	*/
 	void SetParent(VspInterior *parent);
-
 	/** Returns true if this node is a sibling of node n.
 	*/
 	bool IsSibling(VspNode *n) const;
-
 	/** returns depth of the node.
 	*/
 	int GetDepth() const;
-
 	/** returns true if the whole subtree is valid
 	*/
@@ -297,22 +292,25 @@
 	*/
 	ViewCellLeaf *GetViewCell() const;
-
 	/** Sets pointer to view cell.
 	*/
 	void SetViewCell(ViewCellLeaf *viewCell);
 
-	SubdivisionCandidate *GetSubdivisionCandidate() const 
+	SubdivisionCandidate *GetSubdivisionCandidate()
 	{ 
 		return mSubdivisionCandidate; 
 	}
 
+	void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
+	{ 
+		mSubdivisionCandidate = candidate; 
+	}
+
+
 public:
 
 	/// Rays piercing this leaf.
 	VssRayContainer mVssRays;
-	
 	/// leaf pvs
 	ObjectPvs *mPvs;
-
 	/// Probability that the view point lies in this leaf
 	float mProbability;
@@ -322,5 +320,4 @@
 	/// pointer to a split plane candidate splitting this leaf
 	SubdivisionCandidate *mSubdivisionCandidate;
-
 	/// if NULL this does not correspond to feasible viewcell
 	ViewCellLeaf *mViewCell;
@@ -400,5 +397,12 @@
 		{
 			DEL_PTR(mRays);
-			DEL_PTR(mNode);
+
+			if (mNode)
+			{
+				// delete old view cell
+				delete mNode->GetViewCell();
+				delete mNode;
+				mNode = NULL;
+			}
 		}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1297)
@@ -26,5 +26,5 @@
 static float debugVol = 0;
 
-int BvhNode::sMailId = 2147483647;
+int BvhNode::sMailId = 10000;//2147483647;
 int BvhNode::sReservedMailboxes = 1;
 
@@ -44,10 +44,10 @@
 /***************************************************************/
 
-BvhNode::BvhNode(): mParent(NULL)
+BvhNode::BvhNode(): mParent(NULL), mMailbox(0)
 {
 }
 
 BvhNode::BvhNode(const AxisAlignedBox3 &bbox):
-mParent(NULL), mBoundingBox(bbox)
+mParent(NULL), mBoundingBox(bbox), mMailbox(0)
 {
 }
@@ -55,5 +55,5 @@
 
 BvhNode::BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent):
-mBoundingBox(bbox), mParent(parent)
+mBoundingBox(bbox), mParent(parent), mMailbox(0)
 {
 }
@@ -217,8 +217,4 @@
 
 	//-- factors for bsp tree split plane heuristics
-	Environment::GetSingleton()->GetFloatValue("BvHierarchy.Termination.ct_div_ci", mCtDivCi);
-
-	//-- partition criteria
-	Environment::GetSingleton()->GetFloatValue("BvHierarchy.Construction.epsilon", mEpsilon);
 
 	// if only the driving axis is used for axis aligned split
@@ -227,10 +223,8 @@
 	Environment::GetSingleton()->GetBoolValue("BvHierarchy.useCostHeuristics", mUseCostHeuristics);
 
-
 	char subdivisionStatsLog[100];
 	Environment::GetSingleton()->GetStringValue("BvHierarchy.subdivisionStats", subdivisionStatsLog);
 	mSubdivisionStats.open(subdivisionStatsLog);
 
-	Environment::GetSingleton()->GetFloatValue("BvHierarchy.Construction.splitBorder", mSplitBorder);
 	Environment::GetSingleton()->GetFloatValue(
 		"BvHierarchy.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight);
@@ -240,5 +234,4 @@
 
 	Debug << "******* Bvh hierarchy options ******** " << endl;
-
     Debug << "max depth: " << mTermMaxDepth << endl;
 	Debug << "min probabiliy: " << mTermMinProbability<< endl;
@@ -247,7 +240,5 @@
 	Debug << "miss tolerance: " << mTermMissTolerance << endl;
 	Debug << "max leaves: " << mTermMaxLeaves << endl;
-
 	Debug << "randomize: " << randomize << endl;
-
 	Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
 	Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
@@ -387,4 +378,8 @@
 		EvalSubdivisionCandidate(*frontCandidate);
 		EvalSubdivisionCandidate(*backCandidate);
+	
+		// cross reference
+		tFrontData.mNode->SetSubdivisionCandidate(frontCandidate); 
+		tBackData.mNode->SetSubdivisionCandidate(backCandidate);
 
 		tQueue.Push(frontCandidate);
@@ -397,11 +392,10 @@
 
 	//-- terminate traversal
-	if (newNode->IsLeaf())
-	{
+    if (newNode->IsLeaf())
+	{
+		//-- store additional info
 		EvaluateLeafStats(tData);
         
-		const bool mStoreRays= true;
-
-		//-- store additional info
+		const bool mStoreRays = true;
 		if (mStoreRays)
 		{
@@ -429,9 +423,9 @@
 
 	// cost ratio violated?
-	const bool maxCostRatioViolated = ratio < mTermMaxCostRatio;
+	const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
 
 	splitCandidate.mMaxCostMisses = maxCostRatioViolated ? 
-		splitCandidate.mParentData.mMaxCostMisses : 
-		splitCandidate.mParentData.mMaxCostMisses + 1;
+		splitCandidate.mParentData.mMaxCostMisses + 1 :
+		splitCandidate.mParentData.mMaxCostMisses;
 
 	const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
@@ -451,5 +445,5 @@
 	splitCandidate.SetRenderCostDecrease(renderCostDecr);
 
-#if 1
+#if 0
 	const float priority = (float)-splitCandidate.mParentData.mDepth;
 #else	
@@ -520,4 +514,5 @@
 
 
+#if 0
 float BvHierarchy::EvalLocalObjectPartition(const BvhTraversalData &tData,
 											const int axis,
@@ -535,5 +530,5 @@
 	{
 		Intersectable *obj = *oit;
-		AxisAlignedBox3 box = obj->GetBox();
+		const AxisAlignedBox3 box = obj->GetBox();
 
 		const float objMid = (box.Max(axis) + box.Min(axis)) * 0.5f;
@@ -553,4 +548,38 @@
 	return ratio;
 }
+
+#else
+
+float BvHierarchy::EvalLocalObjectPartition(const BvhTraversalData &tData,
+											const int axis,
+											ObjectContainer &objectsFront,
+											ObjectContainer &objectsBack)
+{
+	SortSubdivisionCandidates(tData, axis);
+	
+	vector<SortableEntry>::const_iterator cit, cit_end = mSubdivisionCandidates->end();
+
+	int i = 0;
+	const int border = (int)tData.mNode->mObjects.size() / 2;
+
+    for (cit = mSubdivisionCandidates->begin(); cit != cit_end; ++ cit, ++ i)
+	{
+		Intersectable *obj = (*cit).mObject;
+
+		// object mailed => belongs to back objects
+		if (i < border) 
+			objectsBack.push_back(obj);
+		else
+			objectsFront.push_back(obj);
+	}
+
+	const float oldRenderCost = tData.mProbability * (float)tData.mNode->mObjects.size();
+	const float newRenderCost = 
+		EvalRenderCost(tData, objectsFront, objectsBack);
+
+	const float ratio = newRenderCost / oldRenderCost;
+	return ratio;
+}
+#endif
 
 
@@ -719,5 +748,4 @@
 	// mail view cells on the left side
 	ViewCell::NewMail();
-	
 	// mail the objects on the left side
 	Intersectable::NewMail();
@@ -762,5 +790,4 @@
 			// => add volume to left node
 			volLeft += vol;
-			//volRight -= vol;
 		}
 
@@ -815,5 +842,5 @@
 		{
 			if (mUseCostHeuristics)
-			{
+			{cout << "h";
 				//-- partition objects using heuristics
 				nCostRatio[axis] =
@@ -825,5 +852,5 @@
 			}
 			else
-			{
+			{cout << "m";
 				nCostRatio[axis] =
 					EvalLocalObjectPartition(
@@ -1072,9 +1099,11 @@
 	ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
 
+	Debug << "collecting " << (int)viewCells.size() << " dirty candidates" << endl;
 	for (vit = viewCells.begin(); vit != vit_end; ++ vit)
 	{
 		VspViewCell *vc = dynamic_cast<VspViewCell *>(*vit);
-
 		VspLeaf *leaf = vc->mLeaf;
+		SubdivisionCandidate *candidate = leaf->GetSubdivisionCandidate();
+		
 		dirtyList.push_back(leaf->GetSubdivisionCandidate());
 	}
@@ -1108,16 +1137,13 @@
 {
 	// rather use the simple version
-	if (object->mBvhLeaf)
-	{
-		return object->mBvhLeaf;
-	}
-
+	if (!object) return NULL;
+	return object->mBvhLeaf;
+	
 	///////////////////////////////////////
 	// start from root of tree
+	
 	if (node == NULL)
-	{
 		node = mRoot;
-	}
-
+	
 	vector<BvhLeaf *> leaves;
 
@@ -1182,73 +1208,4 @@
 
 
-/*
-int BvHierarchy::UpdateViewCellsPvs(BvhLeaf *leaf, 
-								const RayInfoContainer &rays) const
-
-{
-	MailablePvsData::NewMail();
-	
-	ViewCellContainer touchedViewCells;
-	CollectTouchedViewCells(rays, touchedViewCells);
-
-	ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
-
-	for (oit = leaf->mObjects.begin(); oit < oit_end; ++ oit)
-	{
-		Intersectable *obj = *oit;
-		ViewCellContainer::const_iterator vit, vit_end = touchedViewCells.end();
-
-		// traverse through view cells and classify them according
-		// to them being seen from to back / front / front and back node
-		for (vit = touchedViewCells.begin(); vit != vit_end; ++ vit)
-		{
-			ViewCell *vc = *vit;
-			float contri;
-			AddViewCellToObjectPvs(obj, vc, contri, true);
-		}
-	}
-
-	return 0;
-}
-
-
-int BvHierarchy::RemoveParentViewCellsPvs(BvhLeaf *leaf, 
-									  const RayInfoContainer &rays
-									  ) const
-
-{
-	MailablePvsData::NewMail();
-	
-	ViewCellContainer touchedViewCells;
-	CollectTouchedViewCells(rays, touchedViewCells);
-
-	ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
-
-	for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
-	{
-		Intersectable *obj = *oit;
-
-		// traverse through view cells and classify them according
-		// to them being seen from to back / front / front and back node
-        ViewCellContainer::const_iterator vit, vit_end = touchedViewCells.end();
-
-		for (vit = touchedViewCells.begin(); vit != vit_end; ++ vit)
-		{
-			ViewCell *vc = *vit;
-			
-			MailablePvsData *vdata = obj->mViewCellPvs.Find(vc);
-
-			if (vdata && !vdata->Mailed())
-			{
-				vdata->Mail();
-				obj->mViewCellPvs.RemoveSample(vc, 1);
-			}
-		}
-	}
-
-	return 0;
-}
-*/
-
 bool BvHierarchy::Export(OUT_STREAM &stream)
 {
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1287)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 1297)
@@ -32,5 +32,5 @@
 					   myless<vector<ViewCell *>::value_type> > TraversalQueue;
 
-int ViewCell::sMailId = 2147483647;
+int ViewCell::sMailId = 1;//2147483647;
 int ViewCell::sReservedMailboxes = 1;
 
@@ -112,4 +112,5 @@
 mEntriesInPvs(0),
 mPvsSizeValid(false)
+//mMailbox(0)
 {
 }
@@ -124,4 +125,5 @@
 mPvsSize(0),
 mPvsSizeValid(false)
+//mMailbox(0)
 {
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1297)
@@ -35,4 +35,6 @@
 class VspTree;
 class ViewCellsContainer;
+class BvhSubdivisionCandidate;
+
 
 /** View space partition statistics.
@@ -45,10 +47,8 @@
 	// number of splits
 	int splits;
-	
 	// maximal reached depth
 	int maxDepth;
 	// minimal depth
 	int minDepth;
-	
 	// max depth nodes
 	int maxDepthNodes;
@@ -123,5 +123,5 @@
 	enum {Interior, Leaf};
 
-	BvhNode::BvhNode();
+	BvhNode();
 	BvhNode(const AxisAlignedBox3 &bbox);
 	BvhNode(const AxisAlignedBox3 &bbox, BvhInterior *parent);
@@ -183,5 +183,4 @@
 	/// the bounding box of the node
 	AxisAlignedBox3 mBoundingBox;
-
 	/// parent of this node
 	BvhInterior *mParent;
@@ -246,20 +245,22 @@
 	bool IsLeaf() const;
 	
-	SubdivisionCandidate *GetSubdivisionCandidate() const 
+	SubdivisionCandidate *GetSubdivisionCandidate()// const
 	{ 
 		return mSubdivisionCandidate; 
 	}
 
+	void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
+	{ 
+		mSubdivisionCandidate = candidate; 
+	}
+
 public:
 
 	/// Rays piercing this leaf.
 	VssRayContainer mVssRays;
-	
 	/// objects
 	ObjectContainer mObjects;
-
 	/// universal counter
 	int mCounter;
-
 
 protected:
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1286)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.h	(revision 1297)
@@ -233,5 +233,5 @@
 
 	////////////////////////////////////////////
-	//       mailing stuff
+	//-- mailing stuff
 
 
@@ -255,5 +255,5 @@
 	static int sMailId;
 	static int sReservedMailboxes;
-
+	//int mMailbox;
 	
 protected:
@@ -261,9 +261,9 @@
 	/// parent view cell in the view cell hierarchy
 	ViewCellInterior *mParent;
-
 	/// the potentially visible objects
 	ObjectPvs mPvs;
 	/// the volume of this view cell
 	float mVolume;
+	
 	float mArea;
 	/// the cost that were paid for merging this view cells from two others.
@@ -271,10 +271,9 @@
 	/// if the view cell is valid view space
 	bool mValid;
-
 	/// color used for consistent visualization
 	RgbColor mColor;
-
 	/// store pvs size, used for evaluation purpose when pvss are stored only in the leaves
 	int mPvsSize;
+
 	/** stores number of entries in pvs
 	    this variable has the same value as mPvsSize for object pvs, 
@@ -283,7 +282,9 @@
 	int mEntriesInPvs;
 
-	/// if the pvs size scalar (+ entries into pvs)
-	/// is up to date and corresponding to the real pvs size
+	/** if the pvs size scalar (+ entries into pvs)
+		is up to date and corresponding to the real pvs size
+	*/
 	bool mPvsSizeValid;
+
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1297)
@@ -535,5 +535,5 @@
 		);
 
-	if (0 && localTerminationCriteriaMet)
+	if (1 && localTerminationCriteriaMet)
 	{
 		Debug << "********local termination *********" << endl;
@@ -628,24 +628,22 @@
 {
 	// todo remove dynamic cast
-	VspSubdivisionCandidate *sc = dynamic_cast<VspSubdivisionCandidate *>(splitCandidate);
+	VspSubdivisionCandidate *sc = 
+		dynamic_cast<VspSubdivisionCandidate *>(splitCandidate);
 	VspTraversalData &tData = sc->mParentData;
 
 	VspNode *newNode = tData.mNode;
-
 
 	if (!LocalTerminationCriteriaMet(tData) && !globalCriteriaMet)
 	{	
+		//-- continue subdivision
 		VspTraversalData tFrontData;
 		VspTraversalData tBackData;
-
-		//-- continue subdivision
 		
 		// create new interior node and two leaf node
 		const AxisAlignedPlane splitPlane = sc->mSplitPlane;
+		const int maxCostMisses = sc->mMaxCostMisses;
+
 		newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData);
 	
-		const int maxCostMisses = sc->mMaxCostMisses;
-
-
 		// how often was max cost ratio missed in this branch?
 		tFrontData.mMaxCostMisses = maxCostMisses;
@@ -666,10 +664,11 @@
 		EvalSubdivisionCandidate(*backCandidate);
 
+		// cross reference
+		tFrontData.mNode->SetSubdivisionCandidate(frontCandidate); 
+		tBackData.mNode->SetSubdivisionCandidate(backCandidate);
+
 		tQueue.Push(frontCandidate);
 		tQueue.Push(backCandidate);
 		
-		// delete old view cell
-		delete tData.mNode->mViewCell;
-
 		// delete old leaf node
 		//DEL_PTR(tData.mNode);
@@ -689,5 +688,4 @@
 #if 0
 		//-- store pvs optained from rays
-
 		AddSamplesToPvs(leaf, *tData.mRays, sampCon, conSamp);
 
@@ -734,9 +732,11 @@
 										 backProb);
 
-	const bool success = ratio < mTermMaxCostRatio;
+	const bool maxCostRatioViolated = mTermMaxCostRatio < ratio;
 
 	// max cost threshold violated?
 	splitCandidate.mMaxCostMisses = 
-		success ? splitCandidate.mParentData.mMaxCostMisses : splitCandidate.mParentData.mMaxCostMisses + 1;
+		maxCostRatioViolated  ? 
+			splitCandidate.mParentData.mMaxCostMisses + 1:
+			splitCandidate.mParentData.mMaxCostMisses;
 	
 	float oldRenderCost;
@@ -749,5 +749,5 @@
 	splitCandidate.SetRenderCostDecrease(renderCostDecr);
 
-#if 1
+#if 0
 	const float priority = (float)-splitCandidate.mParentData.mDepth;
 #else	
@@ -783,6 +783,4 @@
 			  *frontData.mRays,
 			  *backData.mRays);
-	
-	//Debug << "f: " << frontData.mRays->size() << " b: " << backData.mRays->size() << "d: " << tData.mRays->size() << endl;
 
 	//-- compute pvs
@@ -790,13 +788,16 @@
 	backData.mPvs = EvalPvsSize(*backData.mRays);
 
+	Debug << "f pvs: " << frontData.mPvs << " b pvs: " << backData.mPvs << " pvs " << tData.mPvs << endl;
+
 	// split front and back node geometry and compute area
-	tData.mBoundingBox.Split(splitPlane.mAxis, splitPlane.mPosition, 
-							 frontData.mBoundingBox, backData.mBoundingBox);
-
+	tData.mBoundingBox.Split(splitPlane.mAxis, 
+							 splitPlane.mPosition, 
+							 frontData.mBoundingBox, 
+							 backData.mBoundingBox);
 
 	frontData.mProbability = frontData.mBoundingBox.GetVolume();
 	backData.mProbability = tData.mProbability - frontData.mProbability;
 
-	
+
     ///////////////////////////////////////////
 	// subdivide further
@@ -805,5 +806,6 @@
 	if (tData.mDepth > mVspStats.maxDepth)
 	{
-		Debug << "max depth increases to " << tData.mDepth << " at " << mVspStats.Leaves() << " leaves" << endl;
+		Debug << "max depth increases to " << tData.mDepth 
+			  << " at " << mVspStats.Leaves() << " leaves" << endl;
 		mVspStats.maxDepth = tData.mDepth;
 	}
@@ -1054,5 +1056,5 @@
 	Intersectable::NewMail();
 	KdNode::NewMail();
-	BvhNode::NewMail();
+	BvhLeaf::NewMail();
 
 	int pvsSize = 0;
@@ -1366,9 +1368,4 @@
 	float totalPvs = 0;
 
-	// probability that view point lies in back / front node
-	float pOverall = data.mProbability;
-	float pFront = 0;
-	float pBack = 0;
-
 	const float viewSpaceVol = mBoundingBox.GetVolume();
 
@@ -1376,4 +1373,5 @@
 	Intersectable::NewMail(3);
 	KdLeaf::NewMail(3);
+	//for (int i=0;i<25;++i) BvhLeaf::NewMail();
 	BvhLeaf::NewMail(3);
 
@@ -1394,6 +1392,9 @@
 		// evaluate contribution of ray endpoint to front and back pvs
 		// with respect to the classification
-		UpdateContributionsToPvs(*ray, true, cf, pvsFront, pvsBack, totalPvs);
+		//Debug << "mail1:  " << BvhLeaf::sMailId << endl;
+		UpdateContributionsToPvs(*ray, true, cf, pvsFront, pvsBack, totalPvs);	
+		//Debug << "mail2:  " << BvhLeaf::sMailId << endl;
 		UpdateContributionsToPvs(*ray, false, cf, pvsFront, pvsBack, totalPvs);
+		//Debug << "mail3:  " << BvhLeaf::sMailId << endl;
 	}
 
@@ -1404,9 +1405,11 @@
 	data.mBoundingBox.Split(candidatePlane.mAxis, candidatePlane.mPosition, frontBox, backBox);
 
-	pFront = frontBox.GetVolume();
-	pBack = pOverall - pFront;	
+	// probability that view point lies in back / front node
+	float pOverall = data.mProbability;
+	float pFront = pFront = frontBox.GetVolume();
+	float pBack = pOverall - pFront;
+
 
 	//-- pvs rendering heuristics
-
 	const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
 	const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
@@ -1449,4 +1452,6 @@
 	// create unique ids for pvs heuristics
 	Intersectable::NewMail(3);
+	BvhLeaf::NewMail(3);
+	KdLeaf::NewMail(3);
 
 	const int pvsSize = data.mPvs;
@@ -1563,5 +1568,5 @@
 		{
 			frontPvs += renderCost;
-		
+	
 			// already in back pvs => in both pvss
 			if (leaf->Mailed(1))
@@ -1580,5 +1585,7 @@
 			// already in front pvs => in both pvss
 			if (leaf->Mailed())
+			{
 				leaf->Mail(2);
+			}
 			else
 				leaf->Mail(1);
@@ -2061,5 +2068,5 @@
 	Intersectable::NewMail();
 	KdNode::NewMail();
-	BvhNode::NewMail();
+	BvhLeaf::NewMail();
 
 	RayInfoContainer::const_iterator rit, rit_end = rays.end();
@@ -2627,5 +2634,4 @@
 	Debug <<  "rays size: " << (int)rays.size() << endl;
 
-
 	//-- prepare view space partition
 	const float prop = mBoundingBox.GetVolume();
@@ -2642,5 +2648,4 @@
 		
 #if WORK_WITH_VIEWCELL_PVS
-	
 	// add first view cell to all the objects view cell pvs
 	ObjectPvsMap::const_iterator oit, 
@@ -2951,5 +2956,5 @@
 			BvhLeaf *bvhleaf = mHierarchyManager->mBvHierarchy->GetLeaf(obj);
 
-			if (bvhleaf && !bvhleaf->Mailed())
+			if (!bvhleaf->Mailed())
 			{
 				bvhleaf->Mail();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssTree.cpp	(revision 1233)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssTree.cpp	(revision 1297)
@@ -543,6 +543,7 @@
 	float sum = pvsBack*(position - minBox) + pvsFront*(maxBox - position);
 	float newCost = ct_div_ci + sum/sizeBox;
-	float oldCost = pvsSize;
+	float oldCost = (float)pvsSize;
 	ratio = newCost/oldCost;
+
   } else {
 	// importance based cost
@@ -559,5 +560,5 @@
 #else
 #if 1
-	float newCost = raysBack*pvsBack  + raysFront*pvsFront;
+	float newCost = float(raysBack*pvsBack  + raysFront*pvsFront);
 	float oldCost = (float)leaf->rays.size()*pvsSize;
 	ratio = newCost/oldCost;
@@ -807,5 +808,5 @@
   float maxBand = minBox + 0.9*(maxBox - minBox);
 	
-  float minRatio = 1e20;
+  float minRatio = 1e20f;
 	
   Intersectable::NewMail();
@@ -1663,5 +1664,5 @@
 	  VssTreeLeaf *leaf = (VssTreeLeaf *)node;
 	  float c = leaf->GetImportance();
-	  int num = (c*ratioPerLeaf + 0.5);
+	  int num = (c*ratioPerLeaf + 0.5f);
 	  //			cout<<num<<" ";
 
@@ -1720,9 +1721,9 @@
 {
   int nrays = (int)leaf->rays.size();
-  for (int i=0; i < numberOfRays; i++) {
+  for (int i = 0; i < numberOfRays; ++ i) {
 	// pickup 3 random rays
-	int r1 = (int)RandomValue(0, nrays-1);
-	int r2 = (int)RandomValue(0, nrays-1);
-	int r3 = (int)RandomValue(0, nrays-1);
+	int r1 = (int)RandomValue(0, (float)(nrays-1));
+	int r2 = (int)RandomValue(0, (float)(nrays-1));
+	int r3 = (int)RandomValue(0, (float)(nrays-1));
 		
 	Vector3 o1 = leaf->rays[r1].Extrap(RandomValue(leaf->rays[r1].GetMinT(), 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h	(revision 1233)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/FlexibleHeap.h	(revision 1297)
@@ -214,5 +214,5 @@
 
 	Swap(i, (int)mBuffer.size() - 1);
-
+	
 	mBuffer.pop_back();
 	t->NotInHeap();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1295)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1297)
@@ -163,6 +163,6 @@
 			osc = mOspTree->PrepareConstruction(sampleRays, objects, objectSpaceRays);
 			mTQueue.Push(osc);
-		}
-		break;
+			break;
+		}
 	case BV_BASED_OBJ_SUBDIV:
 		{
@@ -176,6 +176,6 @@
 			osc = mBvHierarchy->PrepareConstruction(sampleRays, objects);
 			mTQueue.Push(osc);
-		}
-		break;
+			break;
+		}
 	default:
 		break;
@@ -183,5 +183,5 @@
 
 	mTotalCost = (float)objects.size();
-	Debug << " setting total cost to " << mTotalCost << endl;
+	Debug << "setting total cost to " << mTotalCost << endl;
 }
 
@@ -273,6 +273,5 @@
 	const long startTime = GetTime();	
 	
-	const bool repairQueue = true;
-
+	const bool repairQueue = false;
 	// process object space candidates
 	RunConstruction(repairQueue);
@@ -306,5 +305,8 @@
 
 	const long startTime = GetTime();
+
 	const bool repairQueue = false;
+	// process object space candidates
+	RunConstruction(repairQueue);
 
 	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
@@ -340,6 +342,9 @@
 	const long startTime = GetTime();
 		
+	const bool repairQueue = false;
+	// process object space candidates
+	RunConstruction(repairQueue);
+
 	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-
 	mOspTree->mOspStats.Stop();
 
@@ -348,5 +353,4 @@
 
 	const float rc = mOspTree->EvalRenderCost(sampleRays);
-
 	Debug << "My render cost evalulation: " << rc << endl;
 }
@@ -472,14 +476,11 @@
 void HierarchyManager::CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList)
 {	
-	Debug << "here65" << endl;
 	// we have either a object space or view space split
 	if (mCurrentCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
 	{
-		Debug << "collected view space candidate" << endl;
 		CollectViewSpaceDirtyList(dirtyList);
 	}
 	else // object space split
 	{	
-		Debug << "collecting object space candidate" << endl;
 		CollectObjectSpaceDirtyList(dirtyList);
 	}
@@ -515,12 +516,9 @@
 	for (sit = dirtyList.begin(); sit != sit_end; ++ sit)
 	{
-		Debug << "here4423" << endl;
 		SubdivisionCandidate* sc = *sit;
-
+		// erase from queue
 		mTQueue.Erase(sc);
-
 		// reevaluate
 		sc->EvalPriority();
-
 		// reinsert
 		mTQueue.Push(sc);
@@ -668,11 +666,8 @@
 	mVspTree->mVspStats.Start();
 
-	int i = 0;
-
 	// all objects can be seen from everywhere
 	mTotalCost = (float)dynamic_cast<VspTree::VspSubdivisionCandidate *>(vsc)->mParentData.mPvs;
 
 	const bool repairQueue = false;
-
 	// process view space candidates
 	RunConstruction(repairQueue);
@@ -705,9 +700,6 @@
 		break;
 	}
-
-	// process object space candidates
-	RunConstruction(repairQueue);
-}
-
-
-}
+}
+
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1297)
@@ -164,7 +164,4 @@
 			mHierarchyManager->mOspTree->mRoot, *mObjects);
 	}
-	else
-		cout << "here209" << endl;
-
 }
 
@@ -1237,5 +1234,4 @@
 	for (int i = 0; i < len; ++ i) 
 	{
-		cout << "here5" << endl;
 		string attrName(StrX(attributes.getName(i)).LocalForm());
 		StrX attrValue(attributes.getValue(i));
@@ -1256,5 +1252,4 @@
 	if (mCurrentBvhNode) // replace NULL child of parent with current node
 	{
-		cout << "here8" << endl;
 		BvhInterior *parent = dynamic_cast<BvhInterior *>(mCurrentBvhNode);
 		parent->ReplaceChildLink(NULL, interior);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 1294)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 1297)
@@ -643,5 +643,5 @@
 	float sum = (float)totalVol * sizeBox;
 
-	Debug << "here82 render cost: " << renderCost / viewSpaceVol << endl;
+	Debug << "total render cost: " << renderCost / viewSpaceVol << endl;
 
 	/////////////////////////////////
@@ -690,5 +690,5 @@
 		{
 			float currentPos;
-			Debug << "here29 : " << minRenderCost / viewSpaceVol << endl;
+			Debug << "current min: " << minRenderCost / viewSpaceVol << endl;
 
 			// HACK: current positition is BETWEEN visibility events
@@ -895,5 +895,4 @@
 	}
 
-	Debug << "here32 " << (int)touchedViewCells.size() << endl;
 	return renderCost;
 }
@@ -1386,5 +1385,5 @@
 			if (!vdata)
 			{
-				Debug << "here86 error!!"<<endl;
+				Debug << "error!!" << endl;
 				continue;
 			}
@@ -1793,14 +1792,14 @@
 		}
 	}
-	Debug << "here65 " << (int)touchedObjects.size() << endl;
+	Debug << "touched view cells: " << (int)touchedObjects.size() << endl;
 	ObjectContainer::const_iterator it, it_end = touchedObjects.end();
 	for (it = touchedObjects.begin(); it != it_end; ++ it)
 	{
-		Debug << "\nhere94 obj: " << (*it) << " size: " << (*it)->mViewCellPvs.GetSize() << endl << endl;
+		Debug << "\nobj: " << (*it) << " vc pvs size: " << (*it)->mViewCellPvs.GetSize() << endl << endl;
 		ViewCellPvsMap::const_iterator mit, mit_end = (*it)->mViewCellPvs.mEntries.end();
 
 		for (mit = (*it)->mViewCellPvs.mEntries.begin(); mit != mit_end; ++ mit)
 		{
-			Debug << "newsumpdf: " << (*mit).second.mSumPdf << endl;
+			Debug << "new sumpdf: " << (*mit).second.mSumPdf << endl;
 		}
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 1292)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 1297)
@@ -9,5 +9,5 @@
 bool MeshDebug = false;
 
-int Intersectable::sMailId = 2147483647;
+int Intersectable::sMailId = 1;//2147483647;
 int Intersectable::sReservedMailboxes = 1;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1280)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 1297)
@@ -103,6 +103,6 @@
 	mPreprocessor.mViewCellsManager->GetViewPoint(origin);
 
-	const float alpha = RandomValue(0.0f, 2.0f * M_PI);
-	const float beta = RandomValue(-M_PI * 0.5f, M_PI * 0.5f);
+	const float alpha = RandomValue(0.0f, 2.0f * (float)M_PI);
+	const float beta = RandomValue((float)-M_PI * 0.5f, (float)M_PI * 0.5f);
 	
 	direction = VssRay::GetDirection(alpha, beta);
