Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1564)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 1577)
@@ -26,5 +26,5 @@
 #define COUNT_ORIGIN_OBJECTS 1
 
-	
+///////////
 //-- static members
 
@@ -2004,6 +2004,8 @@
 		AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs);
 
-		if (COUNT_ORIGIN_OBJECTS) 
+		if (COUNT_ORIGIN_OBJECTS)
+		{
 			AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs);
+		}
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1563)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1577)
@@ -17,5 +17,7 @@
 
 
+
 namespace GtpVisibilityPreprocessor {
+
 
 bool use2dSampling = false;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1576)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1577)
@@ -25,4 +25,5 @@
 #define PROBABILIY_IS_BV_VOLUME 1
 #define USE_FIXEDPOINT_T 0
+#define COUNT_ORIGIN_OBJECTS 0
 
 int BvhNode::sMailId = 10000; //2147483647;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp	(revision 1486)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp	(revision 1577)
@@ -50,11 +50,10 @@
   map<string, Vector3, ltstr> cht; // hash table for colors
   
-  FILE         *file;
+  FILE *file;
   char str[80];
-  Face     *face;
-  //Vector3  *vptr;
-  
+  Face *face;
+    
   float x,y,z;
-  int i;
+  //int i;
   
   if ((file = fopen(filename.c_str(),"rt")) == NULL)
@@ -66,122 +65,135 @@
   Environment::GetSingleton()->GetIntValue("Unigraphics.meshGrouping",
 			   meshGrouping);
-  
-  while(fscanf(file,"%s",str)!=EOF) {
-    switch (str[0]) {
-      
-    case 'c':
-      if (strcmp(str,"c_rgb")==0)
-	{
-	  cout<<"c";
-	  cout.flush();
-	  /* vertex */
-	  fscanf(file,"%s",str); // label
-	  fscanf(file,"%f %f %f",&x,&y,&z);
-	  
-	  cht[str] = Vector3(x,y,z); // swap the file coordinates
-	}
-      break;
-      
-      
-    case 'v':            // is it v ?
-      cout<<".";
-      cout.flush();
-      /* vertex */
-      fscanf(file,"%s",str); // label
-      fscanf(file,"%f %f %f",&x,&y,&z);
-      
-      vht[str] = Vector3(x,z,-y); // swap the file coordinates
-      //         vptr = new Vector3C(x,y,z); // swap the file coordinates
-      break;
-      
-    case 'f': {
-      cout<<"+";
-      vector<Vector3> vertices;
-      VertexIndexContainer vertexIndices;
-
-      /* face */
-      fscanf(file,"%s",str); // label may  be ?
-      if (str[0]!='(')
-	fscanf(file,"%s",str);
-      
-      i = 0;                              // index of the vertex
-      
-      
-      map<string, Vector3, ltstr>::const_iterator c = vht.find(&str[1]);
-      
-      if (c == vht.end()) {
-	cout<<"error reading file : "<<
-	  filename<<" ---missing vertex:"<<str<<endl; exit(1);
-      }
-      
-      vertices.push_back((*c).second);
-      
-      fscanf(file,"%s",str);      // get the vertex label
-      
-      while(str[0]!=')') {
-	
-	c = vht.find(str);
-	
-	if (c == vht.end())
+
+  while(fscanf(file,"%s",str)!=EOF) 
+  {
+	  switch (str[0]) 
 	  {
-	    cout<<"error reading file : "<<filename<<" -- missing vertex\n";
-	    exit(1);  }
-	
-	vertices.push_back((*c).second);
-	fscanf(file,"%s",str);   // get the next vertex label
-      }
-
-      // add vertices to the mesh vertex list
-      int index = (int)currentMesh->mVertices.size();
-      for (i=0; i < vertices.size(); i++, index++) {
-		currentMesh->mVertices.push_back(vertices[i]);
-		vertexIndices.push_back(index);
-      }
-    
-      face = new Face(vertexIndices);
-      
-      fscanf(file,"%s",str);   // get the next vertex label
-    
-      if (str[0]!=';') {
-	c = cht.find(str);
-	if (currentMesh->mMaterial == NULL) {
-		Material *mat = MaterialManager::GetSingleton()->CreateResource();
-	  if (c != cht.end()) {
-	    mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z);
-	    currentMesh->mMaterial = mat;
+	  case 'c':
+		  if (strcmp(str,"c_rgb")==0)
+		  {
+			  cout<<"c";
+			  cout.flush();
+			  // vertex
+			  fscanf(file,"%s",str); // label
+			  fscanf(file,"%f %f %f",&x,&y,&z);
+
+			  cht[str] = Vector3(x,y,z); // swap the file coordinates
+		  }
+		  break;
+
+
+	  case 'v':            // is it v ?
+		  cout<<".";
+		  cout.flush();
+		  // vertex
+		  fscanf(file,"%s",str); // label
+		  fscanf(file,"%f %f %f",&x,&y,&z);
+
+		  vht[str] = Vector3(x,z,-y); // swap the file coordinates
+		  // vptr = new Vector3C(x,y,z); // swap the file coordinates
+		  break;
+
+	  case 'f': 
+		  {
+			  cout << "+";
+			  vector<Vector3> vertices;
+			  VertexIndexContainer vertexIndices;
+
+			  // face
+			  fscanf(file,"%s",str); // label may  be ?
+			  if (str[0]!='(')
+				  fscanf(file,"%s",str);
+
+			   map<string, Vector3, ltstr>::const_iterator c = vht.find(&str[1]);
+
+			  if (c == vht.end()) 
+			  {
+				  cout<<"error reading file : "<< filename 
+					  <<" ---missing vertex:"<<str<<endl; exit(1);
+			  }
+
+			  vertices.push_back((*c).second);
+
+			  fscanf(file,"%s",str); // get the vertex label
+
+			  while(str[0]!=')') 
+			  {
+				  c = vht.find(str);
+				  if (c == vht.end())
+				  {
+					cout << "error reading file : " << filename << " -- missing vertex\n";
+					exit(1);  
+				}
+
+				vertices.push_back((*c).second);
+				fscanf(file,"%s",str); // get the next vertex label
+			}
+
+			// add vertices to the mesh vertex list
+			int index = (int)currentMesh->mVertices.size();
+
+			for (int i=0; i < (int)vertices.size(); ++ i, ++ index) 
+			{
+				currentMesh->mVertices.push_back(vertices[i]);
+				vertexIndices.push_back(index);
+			}
+
+			face = new Face(vertexIndices);
+
+			fscanf(file,"%s",str);   // get the next vertex label
+
+			if (str[0]!=';') 
+			{
+				c = cht.find(str);
+				if (currentMesh->mMaterial == NULL) 
+				{
+					Material *mat = MaterialManager::GetSingleton()->CreateResource();
+					if (c != cht.end()) 
+			  {
+				  mat->mDiffuseColor = RgbColor((*c).second.x, (*c).second.y, (*c).second.z);
+				  currentMesh->mMaterial = mat;
+			  }
+					else
+			  {
+				  currentMesh->AssignRandomMaterial();
+			  }
+				}
+			}
+
+			currentMesh->AddFace(face);
+			if (meshGrouping != 0 && currentMesh->mFaces.size() >=  meshGrouping) 
+			{
+				if (ROTATE_SCENE)
+				{
+					RotateMesh(currentMesh);
+				}
+
+				currentMesh->Preprocess();
+				// make an instance of this mesh
+				MeshInstance *mi = new MeshInstance(currentMesh);
+				root->mGeometry.push_back(mi);	
+				currentMesh = MeshManager::GetSingleton()->CreateResource();
+			}
+		}
+		break;
+	}      /* end face */
+
+	// get the rest of the line
+	fgets(str,80,file);
+  }
+
+  if (!currentMesh->mVertices.empty()) 
+  {
+	  if (ROTATE_SCENE) 
+	  {
+		  RotateMesh(currentMesh);
 	  }
-	  else
-	    currentMesh->AssignRandomMaterial();
-	}
-      }
-      
-      currentMesh->AddFace(face);
-      if (meshGrouping != 0 && currentMesh->mFaces.size() >=  meshGrouping) {
-		if (ROTATE_SCENE)
-			RotateMesh(currentMesh);
-
-		currentMesh->Preprocess();
-		// make an instance of this mesh
-		MeshInstance *mi = new MeshInstance(currentMesh);
-		root->mGeometry.push_back(mi);	
-		currentMesh = MeshManager::GetSingleton()->CreateResource();
-      }
-    }
-    break;
-    }      /* end face */
-    
-    // get the rest of the line
-    fgets(str,80,file);
-  }
-  
-  if (currentMesh->mVertices.size()) 
-  {
-	  if (ROTATE_SCENE) RotateMesh(currentMesh);
- 
+
 	  currentMesh->Preprocess();
 	  MeshInstance *mi = new MeshInstance(currentMesh);
 	  root->mGeometry.push_back(mi);
   }
-  
+
   fclose(file);
   return true;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1576)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1577)
@@ -28,6 +28,7 @@
 
 #define USE_FIXEDPOINT_T 0
-
-
+#define COUNT_ORIGIN_OBJECTS 0
+
+/////////////
 //-- static members
 
@@ -618,6 +619,7 @@
 	///////////////
 	//-- create new view cell
+
 	VspLeaf *leaf = tData.mNode;
-	
+
 	VspViewCell *viewCell = new VspViewCell();
     leaf->SetViewCell(viewCell);
@@ -643,4 +645,5 @@
 		///////////
 		//-- store sampling rays
+
         RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
 
@@ -793,8 +796,7 @@
 			splitCandidate.mParentData.mMaxCostMisses + 1:
 			splitCandidate.mParentData.mMaxCostMisses);
-	
+
+	// compute global decrease in render cost
 	float oldRenderCost;
-
-	// compute global decrease in render cost
 	const float renderCostDecr = EvalRenderCostDecrease(splitCandidate.mSplitPlane, 
 														splitCandidate.mParentData,
@@ -843,4 +845,7 @@
 
 		UpdatePvsEntriesContribution(*ray, true, cf, fPvsSize, bPvsSize, oldPvsSize);
+#if COUNT_ORIGIN_OBJECTS
+		UpdatePvsEntriesContribution(*ray, false, cf, fPvsSize, bPvsSize, oldPvsSize);
+#endif
 	}
 	
@@ -968,5 +973,5 @@
 	ViewCellLeaf *vc = leaf->GetViewCell();
 
-	// add contributions from samples to the PVS
+	// add contributions from samples to the pvs
 	for (it = rays.begin(); it != it_end; ++ it)
 	{
@@ -1040,10 +1045,12 @@
 	RayInfoContainer::const_iterator rit, rit_end = rays.end();
 
+	const bool delayMinEvent = false;
+
+	////////////
 	//-- insert all queries
 	for (rit = rays.begin(); rit != rit_end; ++ rit)
 	{
 		const bool positive = (*rit).mRay->HasPosDir(axis);
-		const bool delayMinEvent = false;
-
+		
 		// origin point
 		pos = (*rit).ExtrapOrigin(axis);
@@ -1051,5 +1058,5 @@
 
 		if (delayMinEvent && oType == SortableEntry::ERayMin)
-			pos += mEpsilon; // for walls
+			pos += mEpsilon; // could be useful feature for walls
 
 		mLocalSubdivisionCandidates->push_back(SortableEntry(oType, pos, (*rit).mRay));
@@ -1060,5 +1067,5 @@
 
 		if (delayMinEvent && tType == SortableEntry::ERayMin)
-			pos += mEpsilon; // for walls
+			pos += mEpsilon; // could be useful feature for walls
 
 		mLocalSubdivisionCandidates->push_back(SortableEntry(tType, pos, (*rit).mRay));
@@ -1491,5 +1498,7 @@
 		// with respect to the classification
 		UpdateContributionsToPvs(*ray, true, cf, pvsFront, pvsBack, totalPvs);	
+#if COUNT_ORIGIN_OBJECTS
 		UpdateContributionsToPvs(*ray, false, cf, pvsFront, pvsBack, totalPvs);
+#endif
 	}
 
@@ -1585,4 +1594,55 @@
 
 	return  (mCtDivCi + newCost) / oldCost;
+}
+
+
+void VspTree::UpdateContributionsToPvs(Intersectable *obj,
+									   const int cf,
+									   float &frontPvs,
+									   float &backPvs,
+									   float &totalPvs) const
+{
+	if (!obj) return;
+
+	//const float renderCost = mViewCellsManager->SimpleRay &raynderCost(obj);
+	const int renderCost = 1;
+
+	// object in no pvs => new
+	if (!obj->Mailed() && !obj->Mailed(1) && !obj->Mailed(2))
+	{
+		totalPvs += renderCost;
+	}
+
+	// QUESTION matt: is it safe to assume that 
+	// the object belongs to no pvs in this case?
+	//if (cf == Ray::COINCIDENT) return;
+
+	if (cf >= 0) // front pvs
+	{
+		if (!obj->Mailed() && !obj->Mailed(2))
+		{
+			frontPvs += renderCost;
+		
+			// already in back pvs => in both pvss
+			if (obj->Mailed(1))
+				obj->Mail(2);
+			else
+				obj->Mail();
+		}
+	}
+
+	if (cf <= 0) // back pvs
+	{
+		if (!obj->Mailed(1) && !obj->Mailed(2))
+		{
+			backPvs += renderCost;
+		
+			// already in front pvs => in both pvss
+			if (obj->Mailed())
+				obj->Mail(2);
+			else
+				obj->Mail(1);
+		}
+	}
 }
 
@@ -3045,10 +3105,14 @@
 										   const bool isTermination,
 										   const int cf,
-										   float &frontPvs,
-										   float &backPvs,
+										   float &pvsFront,
+										   float &pvsBack,
 										   float &totalPvs) const
 {
-	/*ray.GetSampleData(isTermination, pt, &obj, &node);
-	if (!obj) return 0;
+	Intersectable *obj;
+	Vector3 pt;
+	KdNode *node;
+
+	ray.GetSampleData(isTermination, pt, &obj, &node);
+	if (!obj) return;
 
 	switch (mHierarchyManager->GetObjectSpaceSubdivisionType())
@@ -3060,11 +3124,11 @@
 		{
 			BvhLeaf *leaf = mHierarchyManager->mBvHierarchy->GetLeaf(obj);
-			UpdateContributionsToPvs(leaf, true, cf, pvsFront, pvsBack, totalPvs, true);	        
+			UpdateContributionsToPvs(leaf, cf, pvsFront, pvsBack, totalPvs, true);	        
 			break;
 		}
 	default:
-		UpdateContributionsToPvs(obj, true, cf, pvsFront, pvsBack, totalPvs, true);	
+		UpdateContributionsToPvs(obj, cf, pvsFront, pvsBack, totalPvs);	
 		break;
-	}*/
+	}
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1576)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1577)
@@ -201,9 +201,10 @@
 {
 	const float costDecr = tData.GetRenderCostDecrease();
-
+cout << "here44 " << mHierarchyStats.pvsEntries << endl;
 	AddSubdivisionStats(mHierarchyStats.Leaves(),
 						costDecr,
 						mTotalCost,
-						mTotalPvsEntries
+						//mPvsEntries
+						mHierarchyStats.pvsEntries
 						);
 }
@@ -218,5 +219,5 @@
 			<< "#Splits\n" << splits << endl
 			<< "#RenderCostDecrease\n" << renderCostDecr << endl 
-			<< "#TotalPvsEntries\n" << pvsEntries << endl
+			<< "#TotalEntriesInPvs\n" << pvsEntries << endl
 			<< "#TotalRenderCost\n" << totalRenderCost << endl;
 }
@@ -301,9 +302,4 @@
 	cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
 
-/*#if _DEBUG
-	cout << "view space: " << GetViewSpaceBox() << endl;
-	cout << "object space:  " << GetObjectSpaceBox() << endl;
-#endif*/
-
 	mObjectSpaceSubdivisionType = mSavedObjectSpaceSubdivisionType;
 	mViewSpaceSubdivisionType = mSavedViewSpaceSubdivisionType;
@@ -517,5 +513,7 @@
 			// update stats
 			mHierarchyStats.nodes += 2;
-			mHierarchyStats.pvsEntries += mCurrentCandidate->GetPvsEntriesIncr();
+			const int pvsEntries = mCurrentCandidate->GetPvsEntriesIncr();
+			mHierarchyStats.pvsEntries += pvsEntries;
+			cout << "pvs entries: " << pvsEntries << " " << mHierarchyStats.pvsEntries << endl;
 			mHierarchyStats.memory += 0; // TODO
 			// subdivision successful
@@ -590,5 +588,8 @@
 			// reevaluate candidates affected by the split for view space splits, 
 			// this would be object space splits and other way round
-			if (repairQueue) RepairQueue();
+			if (repairQueue) 
+			{
+				RepairQueue();
+			}
 		}
 
