Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 678)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.h	(revision 694)
@@ -587,4 +587,6 @@
 	inline bool LocalTerminationCriteriaMet(const VspBspTraversalData &data) const;
 
+	/** Returns true if global tree can be terminated.
+	*/
 	inline bool GlobalTerminationCriteriaMet(const VspBspTraversalData &data) const;
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 693)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCell.cpp	(revision 694)
@@ -345,4 +345,5 @@
 	Debug << "max memory: " << mMaxMemory << endl;
 	Debug << "refining view cells: " << mRefineViewCells << endl;
+	Debug << "********* view cell tree options ***************\n";
 
 	MergeCandidate::sRenderCostWeight = mRenderCostWeight;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 693)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 694)
@@ -54,5 +54,5 @@
 static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
 
-
+#define ROTATE_SCENE 1
 
 
@@ -83,19 +83,20 @@
   StrX lname(name);
   string element(lname.LocalForm());
+  
   if (element == "Shape")
     EndShape();
 }
 
-// HACK
-void RotateMesh(Mesh *mesh)
-{
-	VertexContainer::const_iterator it, it_end = mesh->mVertices.end();
+
+static void RotateMesh(Mesh *mesh)
+{
+	VertexContainer::iterator it, it_end = mesh->mVertices.end();
 
 	const float angle = 30.0f * PI / 180.0f;
-	const Matrix4x4 rot = RotationYMatrix(30);
+	const Matrix4x4 rot = RotationYMatrix(angle);
 
 	for (it = mesh->mVertices.begin(); it != it_end; ++ it)
 	{
-		const Vector3 pt = rot * (*it);
+		(*it) = rot * (*it);        
 	}
 }
@@ -124,5 +125,5 @@
 			
 			mesh->mFaces.push_back(new Face(vc));
-			if (0)
+			//if (ROTATE_SCENE)
 				RotateMesh(mesh);
 			mesh->Preprocess();
@@ -142,5 +143,5 @@
 		{
 			// HACK
-			if (0)
+			//if (ROTATE_SCENE)
 				RotateMesh(mCurrentMesh);
 			mCurrentMesh->Preprocess();
@@ -156,4 +157,5 @@
 			delete mCurrentMesh;
 		}
+
 		mCurrentMesh = NULL;
 	}
@@ -246,5 +248,5 @@
 
 
-			while(1) 
+			while (1) 
 			{
 				float x = (float)strtod(ptr, &endptr);
@@ -298,7 +300,7 @@
     StartIndexedFaceSet(attributes);
   }
-
+ 
   if (element == "Shape") {
-    cout<<"+";
+    cout << "+";
 	mCurrentMesh = new Mesh;
   }
@@ -503,4 +505,5 @@
   StrX lname(name);
   string element(lname.LocalForm());
+ 
   if (element == "Shape")
     EndShape();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp	(revision 658)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp	(revision 694)
@@ -15,4 +15,19 @@
 
 
+// HACK
+static void RotateMesh(Mesh *mesh)
+{
+	VertexContainer::iterator it, it_end = mesh->mVertices.end();
+
+	const float angle = 30.0f * PI / 180.0f;
+	const Matrix4x4 rot = RotationYMatrix(angle);
+
+	for (it = mesh->mVertices.begin(); it != it_end; ++ it)
+	{
+		(*it) = rot * (*it);        
+	}
+}
+
+
 struct ltstr
 {
@@ -22,5 +37,4 @@
   }
 };
-
 
 bool
@@ -40,5 +54,4 @@
   float x,y,z;
   int i;
-  
   
   if ((file = fopen(filename.c_str(),"rt")) == NULL)
@@ -145,4 +158,6 @@
       if (meshGrouping != 0 &&
 					currentMesh->mFaces.size() >=  meshGrouping) {
+		if (1)
+			RotateMesh(currentMesh);
 				currentMesh->Preprocess();
 				// make an instance of this mesh
@@ -160,4 +175,7 @@
   
   if (currentMesh->mVertices.size()) {
+	  if (1)
+		RotateMesh(currentMesh);
+  cout << "here9999" << endl;	  
     currentMesh->Preprocess();
     MeshInstance *mi = new MeshInstance(currentMesh);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 693)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h	(revision 694)
@@ -504,5 +504,5 @@
 	// HACK in order to detect empty view cells
 	void CollectEmptyViewCells();
-	void TestEmptyViewCells();
+	void TestEmptyViewCells(const ObjectContainer &obj);
 
 	ViewCellContainer mEmptyViewCells;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 685)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspBspTree.cpp	(revision 694)
@@ -1028,7 +1028,7 @@
 
 			if (frontData.mProbability < -0.00001)
-				Debug << "here2 error f: " << frontData.mProbability << endl;
+				Debug << "fatal error f: " << frontData.mProbability << endl;
 			if (backData.mProbability < -0.00001)
-				Debug << "here2 error b: " << backData.mProbability << endl;
+				Debug << "fatal error b: " << backData.mProbability << endl;
 
 			// clamp because of precision issues
@@ -2903,4 +2903,5 @@
 }
 
+
 BspLeaf *VspBspTree::GetRandomLeaf(const bool onlyUnmailed)
 {
@@ -2938,4 +2939,5 @@
 }
 
+
 int VspBspTree::ComputePvsSize(const RayInfoContainer &rays) const
 {
@@ -2970,4 +2972,5 @@
 	return pvsSize;
 }
+
 
 float VspBspTree::GetEpsilon() const
@@ -3039,7 +3042,6 @@
 
 	float t;
-	//const float thresh = 1 ? 1e-6f : 0.0f;
-	const float thresh = 0.01f;
-
+	const float thresh = 1e-6f; // matt: change this
+	
 	while (1)
 	{
@@ -3072,11 +3074,12 @@
 				farChild = in->GetBack(); // plane splits ray
 			}
-			else // one of the ray end points on the plane
+			else // one of the ray end points is on the plane
 			{	// NOTE: what to do if ray is coincident with plane?
 				if (extSide < 0)
 					node = in->GetBack();
-				else 
+				else //if (extSide > 0)
 					node = in->GetFront();
-								
+				//else break; // coincident => count no intersections
+
 				continue; // no far child
 			}
@@ -3367,5 +3370,5 @@
 		
 		vector<BspLeaf *> neighbors;
-		if (0)
+		if (1)
 			FindNeighbors(leaf, neighbors, true);
 		else
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 684)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellBsp.cpp	(revision 694)
@@ -2127,7 +2127,8 @@
 				if (extSide < 0)
 					node = in->GetBack();
-				else 
+				else //if (extSide > 0)
 					node = in->GetFront();
-								
+				//else break;
+
 				continue; // no far child
 			}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 685)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 694)
@@ -31,4 +31,7 @@
   environment->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish);
   environment->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility);
+  environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
+  
+  Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
   Debug << "load polygons as meshes: " << mLoadPolygonsAsMeshes << endl;
 
@@ -226,5 +229,5 @@
 	environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
 	environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
-
+	
 	mRenderSimulator = 
 		new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
@@ -240,7 +243,4 @@
 		//		renderer->makeCurrent();
 	}
-
-
-	environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
 	
 	return true;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 693)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 694)
@@ -34,5 +34,6 @@
   environment->GetBoolValue("VssPreprocessor.testBeamSampling", mTestBeamSampling);
   environment->GetBoolValue("VssPreprocessor.enlargeViewSpace", mEnlargeViewSpace);
-
+  environment->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
+  
   useViewspacePlane = mUseViewSpaceBox; //hack
 
@@ -41,6 +42,4 @@
   Debug << "use view space box=" << mUseViewSpaceBox << endl;
   Debug << "enlarge view space=" << mEnlargeViewSpace << endl;
-  Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl;
-
   Debug << "*********** end vss preprocessor options **************" << endl;
 
@@ -102,5 +101,8 @@
 	// cast ray to KD tree to find intersection with other objects
 	Intersectable *objectA, *objectB;
-	Vector3 pointA, pointB;
+	// change matt
+	Vector3 pointA;
+	Vector3 pointB;
+
 	float bsize = Magnitude(box.Size());
 
@@ -126,6 +128,6 @@
 	}
 
-	
-	if (mDetectEmptyViewSpace) {
+	// matt: point A could be undefined?
+	if (1 && mDetectEmptyViewSpace) {
 		SetupRay(ray, pointA, -direction);
 	} else
@@ -153,6 +155,8 @@
 	}
   
+	//bool thesame = objectA && (objectA == objectB);
+	
 	//  if (objectA == NULL && objectB != NULL) {
-	if (mDetectEmptyViewSpace) 
+	if (1 && mDetectEmptyViewSpace) 
 	{
 		// cast again to ensure that there is no objectA
@@ -171,4 +175,6 @@
 
 	bool validSample = (objectA != objectB);
+	
+	//if (validSample && thesame) Debug << "warning!!" << endl;
 
 	if (0 && mDetectEmptyViewSpace) 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 685)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 694)
@@ -14,4 +14,5 @@
 #include "RssTree.h"
 #include "Beam.h"
+
 
 
@@ -210,4 +211,5 @@
 }
 
+
 void
 X3dExporter::ExportMeshInstance(MeshInstance *object)
@@ -218,4 +220,5 @@
   ExportMesh(object->GetMesh());
 }
+
 
 void 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.cpp	(revision 639)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/RayInfo.cpp	(revision 694)
@@ -141,5 +141,5 @@
 	// non-intersecting, otherwise polygon-plane intersections of bsp tree
 	// approaches are not eliminating any rays intersecting the polygon!
-	const float thresh = 1 ? 1e-6f : 0.0f;
+	const float thresh = 1e-6f;
 
 	// segment is not intersecting plane: fond out if on front or back side
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.h	(revision 657)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.h	(revision 694)
@@ -14,5 +14,7 @@
   bool ParseFile(const string filename, SceneGraphNode **root, const bool loadPolygonsAsMeshes = false);
   bool ParseFile(const string filename, ViewCellsManager &viewCells);
-  
+ 
+
+
   /// height of a loaded view cell
   float mViewCellHeight;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 693)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 694)
@@ -17,5 +17,5 @@
 
 #define SAMPLE_AFTER_SUBDIVISION 1
-
+#define TEST_EMPTY_VIEW_CELLS 0
 
 ViewCellsManager::ViewCellsManager():
@@ -145,21 +145,18 @@
 
 	cout << "collecting empty view cells" << endl;
-	int i = 0;
+	
 	for (it = leaves.begin(); it != it_end; ++ it)
 	{
 		if ((*it)->GetPvs().Empty())
 		{
-			++ i;
 			mEmptyViewCells.push_back(*it);
 		}
 	}
-	cout << "view cells found: " << i << endl;
-}
-
-
-void ViewCellsManager::TestEmptyViewCells()
-{
-	mViewCellsTree->CollectLeaves(mViewCellsTree->GetRoot(), mEmptyViewCells);
-
+	Debug << "empty view cells found: " << (int)mEmptyViewCells.size() << endl;
+}
+
+
+void ViewCellsManager::TestEmptyViewCells(const ObjectContainer &obj)
+{
 	ViewCellContainer::const_iterator it, it_end = mEmptyViewCells.end();
 
@@ -170,17 +167,40 @@
 		if (!(*it)->GetPvs().Empty())
 		{
-			sprintf(buf, "empty-viewcells-%09d.x3d", (*it)->GetId());
+			sprintf(buf, "empty-viewcells-%09d.x3d", i/*(*it)->GetId()*/);
 			Exporter *exporter = Exporter::GetExporter(buf);
 			
 			if (exporter && i < 20)
 			{
-				Debug << "view cell " << (*it)->GetId() << " not empty, pvs: " << (*it)->GetPvs().GetSize() << endl;
+				Ray *pray = (*it)->mPiercingRays[0];
+				Debug << "view cell " << (*it)->GetId() << " not empty, pvs: " << (*it)->GetPvs().GetSize() << " " << pray->intersections.size() << endl;
+
+				exporter->ExportRays((*it)->mPiercingRays);
+							
+				exporter->SetFilled();
+				exporter->SetForcedMaterial(RgbColor(0,0,1));
+
+				for (int j = 0; j < (int)pray->intersections.size(); ++ j)
+				{
+					if (pray->intersections[j].mObject)
+						exporter->ExportIntersectable(pray->intersections[j].mObject);
+				}
+
+				//exporter->SetWireframe();
+				exporter->SetForcedMaterial(RgbColor(0,1,0));
+				exporter->ExportGeometry(obj);
+
+				exporter->SetFilled();
+
+				exporter->SetForcedMaterial(RgbColor(1,0,0));
 				ExportViewCellGeometry(exporter, *it);
+
+				delete exporter;		
 			}
 
+		
 			++ i;
 		}
 	}
-	Debug << "\nfound " << i << " new view cells (" << " of " << mEmptyViewCells.size() << ")" << endl << endl;
+	Debug << "\nSampled " << i << " new view cells (" << " of " << (int)mEmptyViewCells.size() << ")" << endl << endl;
 }
 
@@ -289,5 +309,5 @@
 
 	// should we use directional samples?
-	bool dirSamples = mSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION;
+	bool dirSamples = (mSamplingType == Preprocessor::DIRECTION_BASED_DISTRIBUTION);
 
 	while (numSamples < n)
@@ -296,8 +316,8 @@
 		VssRayContainer constructionSamples;
 
-		const int samplingType =
-			dirSamples ? 
+		const int samplingType = mSamplingType;
+			/*dirSamples ? 
 						Preprocessor::DIRECTION_BASED_DISTRIBUTION :
-				 		Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;
+				 		Preprocessor::SPATIAL_BOX_BASED_DISTRIBUTION;*/
 
 		if (0)
@@ -327,7 +347,7 @@
 	VssRayContainer postProcessSamples;
 
-	//-- construction rays => we use uniform samples for this
+	//-- construction rays 
 	CastPassSamples(mPostProcessSamples,
-				    Preprocessor::DIRECTION_BASED_DISTRIBUTION, 
+					mSamplingType, 
 					postProcessSamples);
 
@@ -359,5 +379,6 @@
 
 	// only for testing
-	CollectEmptyViewCells();
+	if (TEST_EMPTY_VIEW_CELLS)
+		CollectEmptyViewCells();
 
 
@@ -495,5 +516,6 @@
 
 	// find empty view cells bug
-	TestEmptyViewCells();
+	if (TEST_EMPTY_VIEW_CELLS)
+		TestEmptyViewCells(preprocessor->mObjects);
 }
 
@@ -1083,6 +1105,18 @@
 	{
 		ViewCell *viewcell = *it;
+
 		if (viewcell->GetValid()) 
 		{
+			// HACK
+			if (TEST_EMPTY_VIEW_CELLS)
+				for (int i = 0; i < mEmptyViewCells.size(); ++i)
+				{
+					if (viewcell == mEmptyViewCells[i])
+					{
+						viewcell->mPiercingRays.push_back(new Ray(ray));
+						Debug << "empty view cell ray found: " << ray.mOriginObject << ", " << ray.mTerminationObject << endl;
+					}
+				}
+
 			// if ray not outside of view space
 			float contribution;
@@ -1773,4 +1807,9 @@
 		newVol += lVol;
 		subdivVol += (*it)->GetVolume();
+
+		float thres = 0.9;
+		if ((lVol < ((*it)->GetVolume() * thres)) ||
+			(lVol * thres > ((*it)->GetVolume())))
+			Debug << "warning: " << lVol << " " << (*it)->GetVolume() << endl;
 	}
 	
@@ -3032,6 +3071,4 @@
 	MergeViewCells(postProcessRays, objects);
 	
-
-	Debug << "here4" << endl;
 	// only for testing
 	TestSubdivision();
@@ -3341,6 +3378,8 @@
 	Debug << "\nOutput view cells: " << endl;
 
+	const bool sortViewCells = true;
+
 	// sort view cells to visualize the largest view cells
-	if (0)
+	if (sortViewCells)
 		stable_sort(mViewCells.begin(), mViewCells.end(), vc_gt);
 	
@@ -3354,8 +3393,7 @@
 		cout << "creating output for view cell " << i << " ... ";
 
-	
 		ViewCell *vc;
 	
-		if (0) // largest view cell pvs first
+		if (sortViewCells) // largest view cell pvs first
 			vc = mViewCells[i];
 		else
@@ -3370,4 +3408,5 @@
 		if (1 || mExportRays)
 		{
+			// output rays stored with the view cells during subdivision
 			if (0)
 			{
@@ -3408,5 +3447,5 @@
 			}
 		
-
+			// associate new rays with output view cell
 			if (1)
 			{
@@ -3598,11 +3637,11 @@
 								   0.00000001f);
 	
-				//Debug << "here geo size: " << geom.Size() << endl;
-				//Debug << "here size b: " << back.Size() << " f: " << front.Size() << endl;
+				//Debug << "geo size: " << geom.Size() << endl;
+				//Debug << "size b: " << back.Size() << " f: " << front.Size() << endl;
 				if (back.Valid())
 				{
 					exporter->ExportPolygons(back.GetPolys());
 				}
-				//else Debug << "here2 size: " << back.Size() << endl;
+			
 			}
 		}
