Index: /GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh	(revision 2742)
+++ /GTP/trunk/Lib/Vis/Preprocessing/scripts/run_gvs_vps.sh	(revision 2743)
@@ -18,5 +18,5 @@
 SCENE=../data/vienna/city_full_hp.obj
 #SCENE=../data/vienna/vienna_rep.obj
-SCENE=../data/vienna/vienna_cropped.obj
+#SCENE=../data/vienna/vienna_cropped.obj
 
 VIEWCELLS=../data/vienna/vienna_cropped-gradient-viewcells.xml.gz 
@@ -38,13 +38,14 @@
 -preprocessor=gvs \
 -preprocessor_use_vbos- \
--gvs_epsilon=0.0001 \
+-gvs_epsilon=0.001 \
 -gvs_total_samples=1000000 \
 -gvs_samples_per_pass=1000000 \
--gvs_initial_samples=16 \
+-gvs_initial_samples=128 \
 -gvs_max_viewcells=1 \
--gvs_min_contribution=20 \
+-gvs_min_contribution=200 \
 -gvs_per_viewcell=true \
+-gvs_threshold=0.5 \
 -gvs_radius_of_influence=5.0 \
--gvs_use_deterministic_gvs+ \
+-gvs_use_deterministic_gvs- \
 -gvs_initial_jitter=0.1 \
 -preprocessor_detect_empty_viewspace+ \
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 2601)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 2743)
@@ -296,5 +296,5 @@
 
 	// wireframe modes => set indexed lines
-	if (mWireframe)
+	if (mWireFrame)
 	{
 		stream << "geometry IndexedLineSet {" << endl;
@@ -324,5 +324,5 @@
 		}
 	  
-		if (mWireframe) // final line to finish polygon
+		if (mWireFrame) // final line to finish polygon
 		{
 			stream << (*face->mVertexIndices.begin()) << " ";
@@ -390,5 +390,5 @@
 
 	// wireframe modes => set indexed lines
-	if (mWireframe)
+	if (mWireFrame)
 	{
 		stream << "geometry IndexedLineSet {" << endl;
@@ -413,5 +413,5 @@
 	}
 
-	if (mWireframe) // final line to finish polygon
+	if (mWireFrame) // final line to finish polygon
 	{
 		stream << "0 ";
@@ -470,5 +470,5 @@
 
 	// wireframe modes => set indexed lines
-	if (mWireframe)
+	if (mWireFrame)
 	{
 		stream << "geometry IndexedLineSet {" << endl;
@@ -562,5 +562,5 @@
 	}
   
-	bool savedWireframe = mWireframe;
+	bool savedWireframe = mWireFrame;
 
 	SetWireframe();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2742)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2743)
@@ -99,12 +99,12 @@
 
 
-struct ProbablyVisibleTriangleContainer
+struct ProbablyVisibleItem
 {
-	KdNode *node;
+	KdNode *mNode;
 	ObjectContainer mTriangles;
 };
 
 
-typedef FlexibleHeap<ProbablyVisibleTriangleContainer *> ProbablyVisibleTriangleQueue;
+typedef FlexibleHeap<ProbablyVisibleItem *> ProbablyVisibleQueue;
 
 
@@ -172,7 +172,8 @@
 		Does reverse sampling if gap found.
 	*/
-	int CheckDiscontinuity2(const VssRay &currentRay, 
-		                    const Triangle3 &hitTriangle,
-							const VssRay &oldRay);
+	bool CheckDiscontinuity2(const VssRay &currentRay, 
+		                     const Triangle3 &hitTriangle,
+							 const VssRay &oldRay,
+							 SimpleRay &simpleRay);
 	/** Adds new samples to the ray queue and classifies them
 		with respect to the previous ray.
@@ -191,7 +192,6 @@
 					  const VssRay &ray1,
                       const VssRay &ray2,
-					  const VssRay &oldRay);
-
-	void Visualize();
+					  const VssRay &oldRay,
+					  int level);
 
 	void CreateDisplacedVertices(VertexContainer &vertices,
@@ -310,7 +310,5 @@
 	inline bool IntersectsViewCell(Vector3 &origin, const Vector3 &dir) const;
 
-	void DeleteOldRays();
-
-
+	void CollectProbablyVisibleItems(ProbablyVisibleQueue &vqueue);
 
 	//////////////////////
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 2176)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.h	(revision 2743)
@@ -39,5 +39,5 @@
 protected:
   std::string mFilename;
-  bool mWireframe;
+  bool mWireFrame;
   bool mUseForcedMaterial;
   Material mForcedMaterial;
@@ -49,5 +49,5 @@
   Exporter(const std::string filename): 
 				  mFilename(filename),
-				  mWireframe(false),
+				  mWireFrame(false),
 				  mUseForcedMaterial(false),
 				  mExportRayDensity(false),
@@ -139,6 +139,6 @@
   void SetExportRayDensity(const bool d) { mExportRayDensity = d; }
   
-  void SetWireframe() { mWireframe = true; }
-  void SetFilled() { mWireframe = false; }
+  void SetWireframe() { mWireFrame = true; }
+  void SetFilled() { mWireFrame = false; }
   
   void SetForcedMaterial(const Material &m) 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2741)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2743)
@@ -128,11 +128,9 @@
 									   KdTree *tree):
 QGLPixelBuffer(QSize(w, h), 
-			   QGLFormat(QGL::SampleBuffers)
-			   /*,|
-						 QGL::StencilBuffer | 
+			   QGLFormat(QGL::StencilBuffer | 
 						 QGL::DepthBuffer | 
 						 QGL::DoubleBuffer |
 						 QGL::Rgba)
-						 */),
+						 ),
 GlRendererBuffer(sceneGraph, viewcells, tree)
 {
@@ -172,33 +170,92 @@
 
 
-void QtGlRendererBuffer::RenderTrianglePvs()
-{
-	//Intersectable::NewMail;
-
-	ObjectContainer::const_iterator oit, oit_end = 
-		mViewCellsManager->GetPreprocessor()->mTrianglePvs.end();
-
-	//int sz = mViewCellsManager->GetPreprocessor()->mDummyBuffer.size();
-
-	for (oit = mViewCellsManager->GetPreprocessor()->mTrianglePvs.begin();
-		 oit != oit_end; ++ oit)
+void GlRenderer::RenderVisTriangles()
+{
+	DisableDrawArrays();
+
+	vector<VizStruct>::const_iterator oit, oit_end = GetPreprocessor()->visTriangles.end();
+	int currentId = 0;
+	//glEnable(GL_POINT_SMOOTH);
+	for (oit = GetPreprocessor()->visTriangles.begin(); oit != oit_end; ++ oit)
+	{
+		VizStruct viz = *oit;
+		TriangleIntersectable *triObj = viz.originalTriangle;
+
+		glColor3f(RandomValue(0.3f, 1.0f), RandomValue(0.3f, 1.0f), RandomValue(0.3f, 1.0f));
+		RenderIntersectable(triObj);
+
+		glColor3f(1, 0, 0);
+		glPointSize(1.0);
+		
+		glBegin(GL_POINTS);
+		
+		VertexContainer::const_iterator vit, vit_end = viz.enlargedTriangle.end();
+
+		for (vit = viz.enlargedTriangle.begin(); vit != vit_end; ++ vit)
+		{
+			Vector3 v = *vit;
+			glVertex3f(v.x, v.y, v.z);
+		}
+
+		glEnd();
+	}
+}
+
+
+#if 0
+
+void GlRenderer::RenderTrianglePvs()
+{
+	DisableDrawArrays();
+
+	ObjectContainer::const_iterator oit, oit_end = GetPreprocessor()->mTrianglePvs.end();
+
+	for (oit = GetPreprocessor()->mTrianglePvs.begin(); oit != oit_end; ++ oit)
 	{
 		TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
-
-		//int dum = mViewCellsManager->GetPreprocessor()->mDummyBuffer[i];
-		//glColor3f(0, (float)dum / 20.0f, 1);
+		glColor3f(RandomValue(0.3f, 1.0f), RandomValue(0.3f, 1.0f), RandomValue(0.3f, 1.0f));
 		if (mUseFalseColors) SetupFalseColor(triObj->mId);
 
 		RenderIntersectable(triObj);
-
-		/*if (!triObj->Mailed())
-			triObj->Mail();
-		else
-			cerr << "böser fehler" << endl;
-		*/
-	}
-}
-
-
+	}
+}
+
+#else
+
+void GlRenderer::RenderTrianglePvs()
+{
+	if (GetPreprocessor()->mTrianglePvs.empty())
+		return;
+
+	EnableDrawArrays();
+
+	if (mUseVbos)
+		glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
+
+	unsigned int bufferSize = GetPreprocessor()->mTrianglePvs.size() * 3;
+	unsigned int *indices = new unsigned int[bufferSize];
+
+	for (unsigned int i = 0; i < GetPreprocessor()->mTrianglePvs.size(); ++ i)
+	{
+		const int id = GetPreprocessor()->mTrianglePvs[i]->GetId() * 3;
+
+		indices[i * 3 + 0] = id + 0;
+		indices[i * 3 + 1] = id + 1;
+		indices[i * 3 + 2] = id + 2;
+	}
+
+	size_t offset = mObjects.size() * 3;
+	char *arrayPtr = mUseVbos ? NULL : (char *)mData;
+	
+	glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
+	glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
+	
+	glDrawElements(GL_TRIANGLES, bufferSize, GL_UNSIGNED_INT, indices);
+
+	DisableDrawArrays();
+	delete [] indices;
+}
+
+#endif
 
 // reimplemented here so that we can snap the error windows
@@ -247,4 +304,6 @@
 		return 0.0f;
 
+	//mTopView = false;
+	SetupProjection(GetWidth(), GetHeight());
 	SetupCamera();
 
@@ -257,6 +316,6 @@
 		GLfloat mat_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
 		GLfloat mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
-		GLfloat mat_specular[] = {0.5f, 0.5f, 0.5f, 1.0f};
-		GLfloat mat_shininess[] = {1.0f};
+		GLfloat mat_specular[] = {0.3f, 0.3f, 0.3f, 1.0f};
+		GLfloat mat_shininess[] = {8.0f};
 
 		glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
@@ -266,7 +325,6 @@
 		
 		GLfloat light_ambient[] = {0.3, 0.3, 0.3, 1.0};
-		//GLfloat light_diffuse[] = {0.6, 0.6, 0.6, 1.0};
 		GLfloat light_diffuse[] = {0.6, 0.6, 0.6, 1.0};
-		GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
+		GLfloat light_specular[] = {0.6, 0.6, 0.6, 1.0};
 
 		glEnable(GL_LIGHTING);
@@ -293,4 +351,6 @@
 	glDisable(GL_ALPHA_TEST);
 		
+	glClearDepth(1.0f);
+
 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
@@ -302,5 +362,10 @@
 	glDepthMask(GL_TRUE);
 	glEnable(GL_DEPTH_TEST);
-
+	
+	if (mDetectEmptyViewSpace) 
+		glEnable(GL_CULL_FACE);
+	else
+		glDisable(GL_CULL_FACE);
+	
 	glFrontFace(GL_CCW);
 	glCullFace(GL_BACK);
@@ -308,8 +373,10 @@
 	glStencilFunc(GL_EQUAL, 0x0, 0x1);
 	glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); 
+	glDisable(GL_STENCIL_TEST); 
 
 	KdNode::NewMail2();
-	//Intersectable::NewMail();
-
+	
+	DisableDrawArrays();
+	
 	//mUseFalseColors = true;
 	// hack for gvs
@@ -322,5 +389,5 @@
 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 
-	glEnable(GL_STENCIL_TEST); 
+	//glEnable(GL_STENCIL_TEST); 
 
 	// errors in red
@@ -332,6 +399,5 @@
 
 	KdNode::NewMail2();
-	//Intersectable::NewMail();
-
+	
 	query->BeginQuery();
 
@@ -340,8 +406,8 @@
 
 	RenderScene();
-
 	glFlush();
 
 	query->EndQuery();
+	
 	glDisable(GL_STENCIL_TEST); 
 	
@@ -396,11 +462,13 @@
 				// render pvs once
 				//RenderPvs(pvs);
-				EnableDrawArrays();
-
+				//EnableDrawArrays();
+				//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 				// prepare pvs for rendering
-				PreparePvs(pvs);
-				_RenderColoredPvs(pvs);
+				//PreparePvs(pvs);
+				//_RenderColoredPvs(pvs);
+				//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 				// hack for gvs visualization
 				//RenderTrianglePvs();
+				RenderVisTriangles();
 
 				glFlush();
@@ -591,12 +659,10 @@
 #if 0
 	for (dit = mDynamicPvsObjects.begin(); dit != dit_end; ++ dit)
-	{
 		_RenderDynamicObject(*dit);
-	}
 #endif
 	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 
 	// show placed dynamic objects as wireframe
-	Preprocessor *p = mViewCellsManager->GetPreprocessor();
+	Preprocessor *p = GetPreprocessor();
 	dit_end = p->mDynamicObjects.end();
 
@@ -722,8 +788,9 @@
 		if (mRenderFilter) 
 		{
+			bool oldWireFrame = mWireFrame;
 			mWireFrame = true;
 			RenderIntersectable(viewcell);
 			
-			mWireFrame = false;
+			mWireFrame = oldWireFrame;
 		}
 	} 
@@ -1013,5 +1080,13 @@
 	{
 		glColor3f(0.6f, 0.6f, 0.6f);
+
+		if (mWireFrame)
+			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+		else
+			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+
 		VisualizePvs();
+
+		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 	}
 
@@ -1068,5 +1143,5 @@
 
 void
-QtGlRendererWidget::keyPressEvent ( QKeyEvent * e )
+QtGlRendererWidget::keyPressEvent(QKeyEvent *e)
 {
 	switch (e->key()) 
@@ -1077,5 +1152,9 @@
 		break;
 	case Qt::Key_R:
-		mUseRandomColorPerPvsObject = !mUseRandomColorPerPvsObject;;
+		mUseRandomColorPerPvsObject = !mUseRandomColorPerPvsObject;
+		updateGL();
+		break;
+	case Qt::Key_W:
+		mWireFrame = !mWireFrame;
 		updateGL();
 		break;
@@ -1094,35 +1173,39 @@
 		updateGL();
 		break;
-	case Qt::Key_S: {
-		// set view poitn and direction
-		QString text;
-		bool ok;
-		text.sprintf("%f %f %f", mViewPoint.x, mViewPoint.y, mViewPoint.z);
-		text = QInputDialog::getText(this,
-			"Enter a view point",
-			"",
-			QLineEdit::Normal,
-			text,
-			&ok);
-		if (!ok)
-			break;
-
-		if (sscanf_s(text.toAscii(), "%f %f %f", &mViewPoint.x, &mViewPoint.y, &mViewPoint.z) == 3) {
-			text.sprintf("%f %f %f", mViewDirection.x, mViewDirection.y, mViewDirection.z);
+	case Qt::Key_S: 
+		{
+			// set view poitn and direction
+			QString text;
+			bool ok;
+			text.sprintf("%f %f %f", mViewPoint.x, mViewPoint.y, mViewPoint.z);
 			text = QInputDialog::getText(this,
-				"Enter a direction",
+				"Enter a view point",
 				"",
 				QLineEdit::Normal,
 				text,
 				&ok);
+
 			if (!ok)
 				break;
-			if (sscanf_s(text.toAscii(), "%f %f %f", &mViewDirection.x,
-				&mViewDirection.y, &mViewDirection.z) == 3) {
-					updateGL();
+
+			if (sscanf_s(text.toAscii(), "%f %f %f", &mViewPoint.x, &mViewPoint.y, &mViewPoint.z) == 3) 
+			{
+				text.sprintf("%f %f %f", mViewDirection.x, mViewDirection.y, mViewDirection.z);
+				text = QInputDialog::getText(this,
+					"Enter a direction",
+					"",
+					QLineEdit::Normal,
+					text,
+					&ok);
+				if (!ok)
+					break;
+				if (sscanf_s(text.toAscii(), "%f %f %f", &mViewDirection.x,
+					&mViewDirection.y, &mViewDirection.z) == 3) {
+						updateGL();
+				}
+				break;
 			}
-			break;
-		}
-					}
+
+		}
 	default:
 		cerr << "unknown key" << endl;
@@ -1156,4 +1239,5 @@
 	//mRenderVisibilityEstimates = true;
 
+	mWireFrame = false;
 	mComputeGVS = false;
 	mUseRandomColorPerPvsObject = false;
@@ -1312,5 +1396,5 @@
 {
 	cerr<<"stop computation called!\n"<<endl;
-	mViewCellsManager->GetPreprocessor()->mStopComputation = true;
+	GetPreprocessor()->mStopComputation = true;
 }
 
@@ -1787,7 +1871,6 @@
 	}
 
-	//mWireFrame = true;
 		
-	// normal rendering
+	// default rendering
 	//if (!mShowPvsSizes && !mShowPiercingRays && !mShowWeightedRays && !mShowWeightedCost && !mShowComparison)
 	if (mUseStandardColors)
@@ -1835,5 +1918,5 @@
 
 	mUseFalseColors = false;
-	mWireFrame = false;
+	//mWireFrame = false;
 
 	glPopAttrib();
@@ -2711,5 +2794,4 @@
 int QtGlRendererWidget::FindDynamicObject(float x, float y) 
 { 
-
 	if (GetPreprocessor()->mDynamicObjects.empty())
 		return -1;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.h	(revision 2657)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.h	(revision 2743)
@@ -45,5 +45,8 @@
 	void keyPressEvent(QKeyEvent * e);
 
-	bool mWireframe;
+
+	///////////////
+
+	bool mWireFrame;
 
 	int timerId;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h	(revision 2736)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h	(revision 2743)
@@ -76,7 +76,4 @@
 	int ComputePvs(ObjectContainer &objects, ObjectContainer &pvs) const;
 
-	/** quick hack in order to be able to render gvs based triangle pvs.
-	*/
-	void RenderTrianglePvs();
 
 	
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp	(revision 2709)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp	(revision 2743)
@@ -28,5 +28,5 @@
 {
 	scale = 1.0f;
-	mWireframe = false;
+	mWireFrame = false;
 	mModelMatrix = IdentityMatrix();
 
@@ -235,5 +235,5 @@
 
   case Qt::Key_W:
-		mWireframe = !mWireframe;
+		mWireFrame = !mWireFrame;
 		updateGL();
 		break;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 2601)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 2743)
@@ -311,5 +311,5 @@
 
 
-  if (mWireframe)
+  if (mWireFrame)
     stream<<"<IndexedLineSet coordIndex=\""<<endl;
   else
@@ -326,5 +326,5 @@
     for (; vi != face->mVertexIndices.end(); vi++)
       stream<<*vi<<" ";
-	if (mWireframe) // final line to finish polygon
+	if (mWireFrame) // final line to finish polygon
 		stream << (*face->mVertexIndices.begin()) << " ";
 
@@ -344,5 +344,5 @@
   stream<<"</Coordinate>"<<endl;
 
-  if (mWireframe)
+  if (mWireFrame)
     stream<<"</IndexedLineSet>"<<endl;
   else
@@ -388,5 +388,5 @@
 
 	//-- create and write indices
-	if (mWireframe)
+	if (mWireFrame)
 		stream<<"<IndexedLineSet coordIndex=\""<<endl;
 	else
@@ -400,5 +400,5 @@
 		stream << index << " ";
 	
-	if (mWireframe) // final line to finish polygon
+	if (mWireFrame) // final line to finish polygon
 		stream << "0 ";
 
@@ -417,5 +417,5 @@
 	stream << "</Coordinate>" << endl;
 
-	if (mWireframe)
+	if (mWireFrame)
 		stream << "</IndexedLineSet>" << endl;
 	else
@@ -456,5 +456,5 @@
 
 	//-- create and write indices
-	if (mWireframe)
+	if (mWireFrame)
 		stream<<"<IndexedLineSet coordIndex=\""<<endl;
 	else
@@ -496,5 +496,5 @@
 	stream << "</Coordinate>" << endl;
 
-	if (mWireframe)
+	if (mWireFrame)
 		stream << "</IndexedLineSet>" << endl;
 	else
@@ -539,5 +539,5 @@
 	}
   
-	bool savedWireframe = mWireframe;
+	bool savedWireframe = mWireFrame;
 
 	SetWireframe();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2736)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2743)
@@ -9,4 +9,5 @@
 #include "SimpleRay.h"
 
+#define USE_RAY_POOL
 
 namespace GtpVisibilityPreprocessor {
@@ -34,4 +35,15 @@
 class GlRendererWidget;
 class IntersectableGroup;
+class TriangleIntersectable;
+
+
+/** Hack: Visualization structure for the GVS algorithm.
+*/
+struct VizStruct
+{
+	VertexContainer enlargedTriangle;
+	TriangleIntersectable *originalTriangle;
+	VssRay *ray;
+};
 
 
@@ -299,4 +311,5 @@
 	// out here for visualization purpose
 	ObjectContainer mTrianglePvs;
+	vector<VizStruct> visTriangles;
 
 	// generic stats vector that can be used for anything
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GtpVisibility05.vcproj
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GtpVisibility05.vcproj	(revision 2729)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GtpVisibility05.vcproj	(revision 2743)
@@ -318,5 +318,5 @@
 		</Filter>
 		<Filter
-			Name="Renderer"
+			Name="renderer"
 			>
 			<File
@@ -342,5 +342,5 @@
 		</Filter>
 		<Filter
-			Name="Loader"
+			Name="loader"
 			>
 			<File
@@ -438,5 +438,5 @@
 		</Filter>
 		<Filter
-			Name="Preprocessors"
+			Name="preprocessors"
 			>
 			<File
@@ -502,5 +502,5 @@
 		</Filter>
 		<Filter
-			Name="Exporters"
+			Name="exporters"
 			>
 			<File
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2742)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2743)
@@ -432,4 +432,5 @@
 	}
 }
+
 	
 void GlRenderer::InitGL()
@@ -465,7 +466,5 @@
 
 	for (; ni != interior->mChildren.end(); ni++) 
-	{
 		CreateVertexArrays(static_cast<SceneGraphLeaf *>(*ni));
-	}
 }
 
@@ -474,9 +473,9 @@
 GlRenderer::SetupProjection(const int w, const int h, const float angle)
 {
-  glViewport(0, 0, w, h);
-  glMatrixMode(GL_PROJECTION);
-  glLoadIdentity();
-  gluPerspective(angle, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
-  glMatrixMode(GL_MODELVIEW);
+	glViewport(0, 0, w, h);
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+	gluPerspective(angle, 1.0, 1.0f, 2.0 * Magnitude(mSceneGraph->GetBox().Diagonal()));
+	glMatrixMode(GL_MODELVIEW);
 }
 
@@ -592,5 +591,5 @@
 	++ mCurrentFrame;
 
-	Intersectable::NewMail();
+	//Intersectable::NewMail();
 
 	Preprocessor *p = mViewCellsManager->GetPreprocessor();
@@ -600,7 +599,5 @@
 
 	for (dit = p->mDynamicObjects.begin(); dit != dit_end; ++ dit)
-	{
 		_RenderDynamicObject(*dit);
-	}
 
 	_RenderSceneTrianglesWithDrawArrays();
@@ -1676,5 +1673,5 @@
 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 	glDepthMask(GL_TRUE);
-	glEnable( GL_CULL_FACE );
+	glEnable(GL_CULL_FACE);
 
 	//	int wait = 0;
@@ -1704,5 +1701,5 @@
 void GlRenderer::RenderViewPoint()
 {
-	mWireFrame = true;
+	//mWireFrame = true;
 	glPushMatrix();
 	glTranslatef(mViewPoint.x, mViewPoint.y, mViewPoint.z);
@@ -1714,5 +1711,5 @@
 	glPopAttrib();
 	glPopMatrix();
-	mWireFrame = false;
+	//mWireFrame = false;
 }
 
@@ -1801,9 +1798,9 @@
 	glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
 	
-	glDrawElements(GL_TRIANGLES, leaf->mIndexBufferSize, GL_UNSIGNED_INT, mIndices + leaf->mIndexBufferStart);
+	glDrawElements(GL_TRIANGLES, leaf->mIndexBufferSize, GL_UNSIGNED_INT, 
+		           mIndices + leaf->mIndexBufferStart);
 }
 
 #endif
-
 
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 2729)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 2743)
@@ -120,5 +120,5 @@
 {  
   //cout << "intel ray" << endl;
-  VssRay *vssRay  = NULL;
+  VssRay *vssRay = NULL;
   int hits = 0;
   int hittriangle;
@@ -126,5 +126,4 @@
   
   float dist;
-  float dist1, dist2;
   double normal[3];
 
@@ -138,5 +137,5 @@
 
   rawCastTimer.Exit();
-  dist1 = dist;
+  
   
   if (hittriangle != -1 ) {
@@ -145,11 +144,12 @@
     if (intersect)
     {
-      hitA.mObject = intersect;
-      hitA.mNormal = Vector3(normal[0], normal[1], normal[2]);
-      // Get the normal of that face
-      //		Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
-      //		normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
-      //-rays[index+i].mDirection; // $$ temporary
-      hitA.mPoint = simpleRay.Extrap(dist);
+		hitA.mObject = intersect;
+		hitA.mNormal = Vector3(normal[0], normal[1], normal[2]);
+
+		// Get the normal of that face
+		// Mesh *mesh = ((MeshInstance *)objectA)->GetMesh();
+		// normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
+		// -rays[index+i].mDirection; // $$ temporary
+		hitA.mPoint = simpleRay.Extrap(dist);
     }
   }
@@ -169,20 +169,18 @@
 	rawCastTimer.Exit();
 
-    dist2 = dist;
-
-    Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
+	Intersectable *intersect = mPreprocessor.GetParentObject(hittriangle);
 		
     if (intersect)
     {
-      hitB.mObject = intersect;
-      hitB.mNormal = Vector3(normal[0], normal[1], normal[2]);
-      // Get the normal of that face
-      //		Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
-      //		normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
-      //-rays[index+i].mDirection; // $$ temporary
-      hitB.mPoint = simpleRay.Extrap(dist);
-    }
-  }
-
+		hitB.mObject = intersect;
+		hitB.mNormal = Vector3(normal[0], normal[1], normal[2]);
+		// Get the normal of that face
+		// Mesh *mesh = ((MeshInstance *)objectB)->GetMesh();
+        //  normalA = mesh->GetFacePlane(mFaceParents[forward_hit_triangles[i]].mFaceIndex).mNormal;
+		//-rays[index+i].mDirection; // $$ temporary
+		hitB.mPoint = simpleRay.Extrap(dist);
+    }
+  }
+/*
   if (saveRays && preprocessor->mTotalRaysCast > saveRaysStart) {    
     if (castDoubleRay)
@@ -221,5 +219,5 @@
     }
   }      
-  
+  */
   return ProcessRay(
 		    simpleRay,
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 2742)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 2743)
@@ -32,5 +32,6 @@
 {
 	VssRayContainer rays;
-	CastRay(simpleRay, rays, box, castDoubleRay, true);
+	//CastRay(simpleRay, rays, box, castDoubleRay, true);
+	CastRay(simpleRay, rays, box, castDoubleRay, false);
     
 	if (!rays.empty())
@@ -409,10 +410,4 @@
 
 
-/*void RayCaster::DeleteOldRays()
-{
-	CLEAR_CONTAINER(mOldRays);
-}*/
-
-
 int
 RayCaster::ProcessRay(const SimpleRay &simpleRay,
@@ -443,5 +438,5 @@
 	
 	// regardless of the pruneInvalidRays setting reject 
-	// rays whic degenerate to a point
+	// rays which degenerate to a point
 	if (EpsilonEqualV3(hitA.mPoint, hitB.mPoint, Limits::Small)) {
 #if DEBUG_PROCESS_RAY
@@ -462,5 +457,5 @@
 	// reset both contributions
 	if (!validA || !validB) {
-	  if (0 || pruneInvalidRays)
+	  if (pruneInvalidRays)
 		return 0;
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2718)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2743)
@@ -333,6 +333,6 @@
 		// NOTE: render texture should be power of 2 and square
 		// renderer must be initialised
-		const float w = 1024;
-		const float h = 1024;
+		const float w = 2048;//1024;
+		const float h = 2048;//1024;
 
 		QtGlRendererBuffer *glbuf = 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2736)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2743)
@@ -247,5 +247,12 @@
 
 	Preprocessor *GetPreprocessor();
-
+	/** quick hack in order to be able to render gvs based triangle pvs.
+	*/
+	void RenderTrianglePvs();
+
+	void RenderVisTriangles();
+
+
+	///////////////////
 
 	vector<PvsErrorEntry> mPvsErrorBuffer;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2742)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2743)
@@ -22,14 +22,15 @@
 {
   
-#define GVS_DEBUG 0
+#define GVS_DEBUG 1
 #define NOT_ACCOUNTED_OBJECT 0
 #define ACCOUNTED_OBJECT 2
-
-
-static const float MIN_DIST = 0.001f;
+#define USE_REVERSE_SAMPLING 1
+#define USE_SUBDIVISION 1
+
+static const float MIN_DIST = 1e-8f;
 
 static ObjectContainer myobjects;
 static int sInvalidSamples = 0;
-
+static VizStruct currentViz;
 
 
@@ -53,15 +54,4 @@
 
 
-/** Visualization structure for the GVS algorithm.
-*/
-struct VizStruct
-{
-	Polygon3 *enlargedTriangle;
-	Triangle3 originalTriangle;
-	VssRay *ray;
-};
-
-static vector<VizStruct> vizContainer;
-
 
 
@@ -216,7 +206,8 @@
 
 
-int GvsPreprocessor::CheckDiscontinuity2(const VssRay &currentRay,
-										 const Triangle3 &hitTriangle,
-										 const VssRay &oldRay)
+bool GvsPreprocessor::CheckDiscontinuity2(const VssRay &currentRay,
+										  const Triangle3 &hitTriangle,
+										  const VssRay &oldRay,
+										  SimpleRay &reverseRay)
 {
 	// the predicted hitpoint is the point where the ray would have intersected the hit triangle
@@ -229,20 +220,7 @@
 	// => this is likely to be a discontinuity
 	if (fabs(predictedLen - len) < mThreshold)
-		return 0;
-
-	static SimpleRayContainer simpleRays;
-	simpleRays.clear();
-
-	CreateRandomizedReverseRays(currentRay, predictedHit, simpleRays, 16, hitTriangle);
-
-	static VssRayContainer reverseRays;
-	reverseRays.clear();
-	CastRays(simpleRays, reverseRays, false, false);
-
-	mGvsStats.mReverseSamples += (int)reverseRays.size();
-
-	EnqueueRays(reverseRays);
-
-	return (int)reverseRays.size();
+		return false;
+
+	return CreateRandomizedReverseRay(currentRay, predictedHit, reverseRay, hitTriangle);
 }
 
@@ -353,6 +331,9 @@
 	}
 	
-	if (0 && GVS_DEBUG)
-		mVssRays.push_back(new VssRay(*vssRay));
+	if (GVS_DEBUG)
+	{
+		if (mVssRays.size() < 1000)
+			mVssRays.push_back(new VssRay(*vssRay));
+	}
 
 	// add new ray to ray queue
@@ -361,5 +342,5 @@
 	++ mGvsStats.mTotalContribution;
 
-	if (mRayQueue.size() % 10000 == 0)
+	if (mRayQueue.size() % 50000 == 0)
 		cout << "ray queue size: " << mRayQueue.size() << endl;
 
@@ -376,28 +357,44 @@
 											  const Triangle3 &hitTriangle,
 											  const VssRay &ray, 
-											  const int index) const
+											  int index) const
 {
 	const int indexU = (index + 1) % 3;
-	const int indexL = (index == 0) ? 2 : index - 1;
-
-	const Vector3 a = hitTriangle.mVertices[index] - ray.GetOrigin();
-	const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
-	const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
+	const int indexL = (index - 1) % 3; //(index == 0) ? 2 : index - 1;
+
+	const Vector3 v  = hitTriangle.mVertices[index];
+	const Vector3 vu = hitTriangle.mVertices[indexU];
+	const Vector3 vl = hitTriangle.mVertices[indexL];
+
+	const Vector3 a = v - ray.GetOrigin();
+	const Vector3 b = vu - v;
+	const Vector3 c = v - vl;
 	
 	const float len = Magnitude(a);
-	
-	const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
-	const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi- pi-1))
+	//if (len > Limits::Small) a /= len;
+
+	const Vector3 dir1 = Normalize(CrossProd(a, b)); // N((pi-xp)×(pi+1 - pi));
+	const Vector3 dir2 = Normalize(CrossProd(a, c)); // N((pi-xp)×(pi - pi-1))
 	const Vector3 dir3 = DotProd(dir2, dir1) > 0 ? // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
 		Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir1) + CrossProd(dir2, a)); 
+#if 1
 
 	// compute the new three hit points
-	// pi, i + 1:  pi+ e·|pi-xp|·di, j
-	const Vector3 pt1 = hitTriangle.mVertices[index] + mEps * len * dir1;
-	// pi, i - 1:  pi+ e·|pi-xp|·di, j
-    const Vector3 pt2 = hitTriangle.mVertices[index] + mEps * len * dir2;
-	// pi, i:  pi+ e·|pi-xp|·di, j
-	const Vector3 pt3 = hitTriangle.mVertices[index] + mEps * len * dir3;
-	
+	// pi, i + 1:  pi + e·|pi-xp|·di, j
+	const Vector3 pt1 = v + mEps * len * dir1;
+	// pi, i - 1:  pi + e·|pi-xp|·di, j
+    const Vector3 pt2 = v + mEps * len * dir2;
+	// pi, i:      pi + e·|pi-xp|·di, j
+	const Vector3 pt3 = v + mEps * len * dir3;
+
+#else
+
+	const Vector3 pt1 = v + mEps * dir1;
+	// pi, i - 1:  pi + e·|pi-xp|·di, j
+    const Vector3 pt2 = v + mEps * dir2;
+	// pi, i:      pi + e·|pi-xp|·di, j
+	const Vector3 pt3 = v + mEps * dir3;
+
+#endif
+
 	vertices.push_back(pt2);
 	vertices.push_back(pt3);
@@ -431,5 +428,5 @@
 
 
-static bool EqualVisibility(const VssRay &a, const VssRay &b)
+bool EqualVisibility(const VssRay &a, const VssRay &b)
 {
 	return a.mTerminationObject == b.mTerminationObject;
@@ -442,17 +439,20 @@
 								   const VssRay &ray1, 
 								   const VssRay &ray2,
-								   const VssRay &oldRay)
-{
-	//cout <<"y"<<Magnitude(p1 - p2) << " ";
+								   const VssRay &oldRay,
+								   int level)
+{
 	int castRays = 0;
-
-	if (EqualVisibility(ray1, ray2) || (SqrMagnitude(p1 - p2) <= MIN_DIST))
+	
+	if (EqualVisibility(ray1, ray2) || (SqrDistance(p1, p2) <= MIN_DIST)  || (level ++ > 10))
+	{
+		//if (SqrMagnitude(p1 - p2) <= MIN_DIST)
+		//	cerr << "min dist reached!! " << SqrMagnitude(p1 - p2) << " " << MIN_DIST << " " << ray1.mTerminationObject << " " << ray2.mTerminationObject << " level: " << level << " " << EqualVisibility(ray1, ray2) << endl;
+
 		return castRays;
-	
+	}
 
 	// the new subdivision point
 	const Vector3 p = (p1 + p2) * 0.5f;
-	//cout << "p: " << p << " " << p1 << " " << p2 << endl;
-
+	
 	SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin, SamplingStrategy::GVS, 1.0f);
 
@@ -469,14 +469,20 @@
 
 	if (!newRay)
+	{
+		//cout<<"w";
 		return castRays;
+	}
 
 	//newRay->mFlags |= VssRay::BorderSample;
 
 	// cast reverse rays if necessary
-	castRays += CheckDiscontinuity(*newRay, hitTriangle, oldRay);
-	
+	if (USE_REVERSE_SAMPLING) castRays += CheckDiscontinuity(*newRay, hitTriangle, oldRay);
+	
+	if (GVS_DEBUG)
+		currentViz.enlargedTriangle.push_back(p);
+
 	// subdivide further
-	castRays += SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay);
-	castRays += SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay);
+	castRays += SubdivideEdge(hitTriangle, p1, p, ray1, *newRay, oldRay, level);
+	castRays += SubdivideEdge(hitTriangle, p, p2, *newRay, ray2, oldRay, level);
 
 	rayTimer.Entry();
@@ -526,6 +532,8 @@
 
 		SimpleRay sr(currentRay.GetOrigin(), rayDir, SamplingStrategy::GVS, 1.0f);
-		simpleRays.AddRay(sr);	
-	}
+		simpleRays.AddRay(sr);
+	}
+
+	//if (rand() < RAND_MAX / 10)	cout << "o: " << currentRay.GetOrigin() << endl;
 
 	//////////
@@ -560,33 +568,57 @@
 	int castRays = (int)simpleRays.size();
 
-	if (0)
-	{
-		// visualize enlarged triangles
-		VizStruct dummy;
-		dummy.enlargedTriangle = new Polygon3(enlargedTriangle);
-		dummy.originalTriangle = hitTriangle;
-	
-		vizContainer.push_back(dummy);
-	
+	if (GVS_DEBUG)
+	{
+		/*visTriangles.push_back(tObj);
+
+		for (int i = 0; i < 3; ++ i)
+		{
+			Triangle3 t1(hitTriangle.mVertices[i], enlargedTriangle[3 * i + 0], enlargedTriangle[3 * i + 1]);
+			Triangle3 t2(hitTriangle.mVertices[i], enlargedTriangle[3 * i + 1], enlargedTriangle[3 * i + 2]);
+
+			TriangleIntersectable *to1 = new TriangleIntersectable(t1); to1->SetId(tObj->GetId());
+			TriangleIntersectable *to2 = new TriangleIntersectable(t2); to2->SetId(tObj->GetId());
+
+			visTriangles.push_back(to1);
+			visTriangles.push_back(to2);
+		}
+		
 		// set flags
 		VssRayContainer::const_iterator rit, rit_end = vssRays.end();
-	
+
 		for (rit = vssRays.begin(); rit != rit_end; ++ rit)
 			(*rit)->mFlags |= VssRay::BorderSample;
-	}
-#if 0
-    // recursivly subdivide each edge
-	for (int i = 0; i < numBorderSamples; ++ i)
-	{
-		castRays += CheckDiscontinuity(*vssRays[i], hitTriangle, currentRay);
-
-		castRays += SubdivideEdge(hitTriangle,
-								  enlargedTriangle[i], 
-								  enlargedTriangle[(i + 1) % numBorderSamples], 
-								  *vssRays[i], 
-								  *vssRays[(i + 1) % numBorderSamples],
-								  currentRay);
-	}
-#endif	
+		*/
+
+		// visualize enlarged triangles
+		currentViz.enlargedTriangle = enlargedTriangle;
+		currentViz.originalTriangle = static_cast<TriangleIntersectable *>(tObj);
+	}
+	
+	if (USE_SUBDIVISION)
+	{
+		//int oldsize = mTrianglePvs.size();
+
+		// recursivly subdivide each edge
+		for (int i = 0; i < numBorderSamples; ++ i)
+		{
+			if (USE_REVERSE_SAMPLING)
+				castRays += CheckDiscontinuity(*vssRays[i], hitTriangle, currentRay);
+
+			castRays += SubdivideEdge(hitTriangle,
+				                      enlargedTriangle[i], 
+									  enlargedTriangle[(i + 1) % numBorderSamples], 
+									  *vssRays[i], 
+									  *vssRays[(i + 1) % numBorderSamples],
+									  currentRay,
+									  0);
+		}
+
+		//int newsize = mTrianglePvs.size();if(newsize - oldsize) mcout<<"found with subdiv: " << newsize - oldsize << endl;
+	}
+
+	if (GVS_DEBUG)
+		visTriangles.push_back(currentViz);
+
 	mGvsStats.mBorderSamples += castRays;
 	
@@ -652,8 +684,9 @@
 
 #endif	
+
 	//////////
 	//-- fill up simple rays with random rays so we can cast 16 rays at a time
 
-	const int numRandomRays = 16 - ((int)simpleRays.size() % 16);
+	const int numRandomRays = 128;//16 - ((int)simpleRays.size() % 16);
 
 	GenerateImportanceSamples(currentRay, hitTriangle, numRandomRays, simpleRays);
@@ -687,11 +720,29 @@
 	int castRays = (int)simpleRays.size();
 
-#if 0
-	for (size_t i = 0; i < vssRays.size(); ++ i)
-	{
-		// check for discontinuity and cast reverse rays if necessary
-		castRays += CheckDiscontinuity2(*vssRays[i], hitTriangle, currentRay);
-	}
-#endif
+	mGvsStats.mBorderSamples += castRays;
+
+	if (USE_REVERSE_SAMPLING)
+	{
+		static SimpleRayContainer simpleRays;
+		simpleRays.clear();
+
+		SimpleRay simpleRay;
+		for (size_t i = 0; i < vssRays.size(); ++ i)
+		{
+			// check for discontinuity and cast reverse rays if necessary
+			//castRays += CheckDiscontinuity2(*vssRays[i], hitTriangle, currentRay);
+			if (CheckDiscontinuity2(*vssRays[i], hitTriangle, currentRay, simpleRay))
+				simpleRays.push_back(simpleRay);
+		}
+		
+		static VssRayContainer reverseRays;
+		reverseRays.clear();
+		CastRays(simpleRays, reverseRays, false, false);
+
+		mGvsStats.mReverseSamples += (int)reverseRays.size();
+	
+		castRays +=(int)reverseRays.size();
+	}
+
 #if 0
 
@@ -704,11 +755,8 @@
 								  *vssRays[i], 
 								  *vssRays[(i + 1) % numBorderSamples],
-								  currentRay);
+								  currentRay, 0);
 	}
 	
 #endif
-
-
-	mGvsStats.mBorderSamples += castRays;
 
 	// handle cast rays
@@ -886,4 +934,6 @@
 
 	castTimer.Exit();
+//for (int i = 0; i< simpleRays.size();++ i)
+//	if (rand() < RAND_MAX / 10)	cout << "o: " << simpleRays[i].mOrigin << endl;
 
 	// add to ray queue
@@ -922,4 +972,5 @@
 	while (!mRayQueue.empty())
 	{
+		size_t oldContri = mTrianglePvs.size();
 		// handle next ray
 		VssRay *ray = mRayQueue.top();
@@ -927,7 +978,24 @@
 		
 		if (mUseDeterministicGvs)
+		{
 			castSamples += AdaptiveBorderSampling(*ray);
+			mRayCaster->SheduleRayForDeletion(ray);
+		}
 		else
+		{
 			castSamples += AdaptiveBorderSamplingOpt(*ray);
+
+			// ray still gives contribution => enqeue again
+			if ((mTrianglePvs.size() - oldContri) > 0)
+			{
+				mRayQueue.push(ray);
+			}
+			else
+			{
+				mRayCaster->SheduleRayForDeletion(ray);
+			}
+		}
+
+
 	}
 
@@ -1374,5 +1442,4 @@
 		// provide list of view cells to compute
 		CompileViewCellsList();
-
 		// start per view cell gvs
 		PerViewCellComputation();
@@ -1391,5 +1458,4 @@
 	if (GVS_DEBUG)
 	{
-		Visualize();
 		CLEAR_CONTAINER(mVssRays);
 	}
@@ -1411,37 +1477,4 @@
 	// store triangle directly
 	mViewCellsManager->DeterminePvsObjects(rays, true);
-}
-
-
-void GvsPreprocessor::Visualize()
-{
-	Exporter *exporter = Exporter::GetExporter("gvs.wrl");
-
-	if (!exporter)
-		return;
-	
-	vector<VizStruct>::const_iterator vit, vit_end = vizContainer.end();
-	
-	for (vit = vizContainer.begin(); vit != vit_end; ++ vit)
-	{
-		exporter->SetWireframe();
-		exporter->ExportPolygon((*vit).enlargedTriangle);
-		//Material m;
-		exporter->SetFilled();
-		Polygon3 poly = Polygon3((*vit).originalTriangle);
-		exporter->ExportPolygon(&poly);
-	}
-
-	VssRayContainer::const_iterator rit, rit_end = mVssRays.end();
-
-	for (rit = mVssRays.begin(); rit != rit_end; ++ rit)
-	{
-		Intersectable *obj = (*rit)->mTerminationObject;
-		exporter->ExportIntersectable(obj);
-	}
-
-	ExportVssRays(exporter, mVssRays);
-	
-	delete exporter;
 }
 
@@ -1545,5 +1578,5 @@
 		if (passSamples >= mGvsSamplesPerPass)
 		{
-			if (GVS_DEBUG) VisualizeViewCell(mTrianglePvs);
+			if (0 && GVS_DEBUG) VisualizeViewCell(mTrianglePvs);
 
 			//const bool convertPerPass = true;
@@ -2049,4 +2082,37 @@
 
 
+void GvsPreprocessor::CollectProbablyVisibleItems(ProbablyVisibleQueue &vqueue)
+{
+	ObjectPvsIterator pit = mCurrentViewCell->GetPvs().GetIterator();
+	static ObjectContainer tmpTriangles;
+
+	while (pit.HasMoreEntries())
+	{
+		tmpTriangles.clear();
+
+		Intersectable::NewMail();
+
+		ProbablyVisibleItem *item = new ProbablyVisibleItem();
+
+		KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
+		mKdTree->CollectObjects(kdObj->GetItem(), tmpTriangles);
+		item->mNode = kdObj->GetItem();
+
+		ObjectContainer::const_iterator oit, oit_end = tmpTriangles.end();
+
+		for (oit = tmpTriangles.begin(); oit != oit_end; ++ oit)
+		{
+			TriangleIntersectable *triObj = static_cast<TriangleIntersectable *>(*oit);
+
+			// find objects which are not yet accounted for yet contained in kd pvs objects
+			if (triObj->mCounter < ACCOUNTED_OBJECT)
+			{
+				item->mTriangles.push_back(triObj);
+			}
+		}
+	}
+}
+
+
 void GvsPreprocessor::CreateRandomizedReverseRays(const VssRay &ray, 
 												  const Vector3 &predictedPoint, 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 2742)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SamplingStrategy.cpp	(revision 2743)
@@ -830,7 +830,7 @@
 	{
 		//mViewCell->GetRandomEdgePoint(origin, normal);
-		//origin = mViewCell->GetBox().GetRandomSurfacePoint();
-		origin = mViewCell->GetBox().GetRandomPoint();
-//origin = Vector3(680.682, 189.552, -278.177);
+		origin = mViewCell->GetBox().GetRandomSurfacePoint();
+		//origin = mViewCell->GetBox().GetRandomPoint();
+		//origin = Vector3(680.682, 189.552, -278.177);
 		// move a little bit back to avoid piercing through walls
 		// that bound the view cell
