Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp	(revision 1962)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp	(revision 1963)
@@ -6,4 +6,6 @@
 #include "GlRenderer.h"
 #include "Exporter.h"
+#include "ViewCellsManager.h"
+
 
 // the devil library
@@ -392,5 +394,5 @@
 	
 	const float sceneSize = Magnitude(bbox.Diagonal());
-	mViewPoint = midPoint + Vector3(0.5 * sceneSize, 0, 0);
+	mViewPoint = midPoint + Vector3(0.5 * sceneSize, 0, 0.5 * sceneSize);
 	
 	cout << "mid point: " << midPoint << endl;
@@ -399,5 +401,5 @@
 
 	mNear = 1;
-	mFar = sceneSize;
+	mFar = sceneSize * 2;
 	mWidth = sceneSize;
 
@@ -580,5 +582,6 @@
 
 void GlobalLinesRenderer::ProcessDepthBuffer(VssRayContainer &vssRays, 
-											 const bool oldBufferInitialised)
+											 const bool oldBufferInitialised,
+											 const int pass)
 {
 	GrabDepthBuffer(mNewDepthBuffer, mNewTexture);
@@ -603,5 +606,5 @@
 	}
 
-	cout << "vp: " << "eye: " << mEyeVec << " left: " << mLeftVec << " up: " << mUpVec << endl;
+	//cout << "vp: " << "eye: " << mEyeVec << " left: " << mLeftVec << " up: " << mUpVec << endl;
 
 	for (int y = 0; y < texHeight; ++ y)
@@ -625,8 +628,17 @@
 														 false);
 
+			if (!termObj1 && !termObj2) // we do not create a ray
+				continue;
+
+			Vector3 clippedOldPt, clippedNewPt;
+
+			ClipToViewSpaceBox(oldPt, newPt, clippedOldPt, clippedNewPt);
+			//clippedOldPt = oldPt;
+			//clippedNewPt = newPt;
+
 			// create rays in both directions
 			if (termObj1)
 			{
-				vssRays.push_back(new VssRay(oldPt, newPt, NULL, termObj1));
+				vssRays.push_back(new VssRay(clippedOldPt, clippedNewPt, NULL, termObj1, pass));
 				//cout << "new pt: " << newPt << endl;
 			}
@@ -634,5 +646,5 @@
 			if (termObj2)
 			{
-				vssRays.push_back(new VssRay(newPt, oldPt, NULL, termObj2));
+				vssRays.push_back(new VssRay(clippedNewPt, clippedOldPt, NULL, termObj2, pass));
 				//cout << "old pt: " << oldPt << endl;
 			}
@@ -642,20 +654,44 @@
 
 
+bool GlobalLinesRenderer::ClipToViewSpaceBox(const Vector3 &origin,
+											 const Vector3 &termination,
+											 Vector3 &clippedOrigin,
+											 Vector3 &clippedTermination)
+{
+	Ray ray(origin, termination - origin, Ray::LINE_SEGMENT);	
+	ray.Precompute();
+	
+	float tmin, tmax;
+	
+	//const AxisAlignedBox3 bbox = mPreprocessor->mViewCellsManager->GetViewSpaceBox();
+	// hack
+	const AxisAlignedBox3 bbox = mPreprocessor->mKdTree->GetBox();
+
+	if ((!bbox.ComputeMinMaxT(ray, &tmin, &tmax)) ||
+		tmin>=tmax)
+	{
+		return false;
+	}
+	
+	if (tmin >= 1.0f || tmax <=0.0f)
+		return false;
+
+	if (tmin > 0.0f)
+		clippedOrigin = ray.Extrap(tmin);
+	else
+		clippedOrigin = origin;
+
+	if (tmax < 1.0f)
+		clippedTermination = ray.Extrap(tmax);
+	else
+		clippedTermination = termination;
+
+	return true;
+}
+
+
 void GlobalLinesRenderer::Run()
 {
 	glutMainLoop();
-}
-
-
-void SetupFalseColor(const unsigned int id, 
-					 unsigned char &r,
-					 unsigned char &g,
-					 unsigned char &b
-					 )
-{
-	// swap bits of the color
-	r = id & 255;
-	g = (id >> 8) & 255;
-	b = (id >> 16) & 255;
 }
 
@@ -673,13 +709,24 @@
 
 	VssRayContainer::const_iterator vit, vit_end = vssRays.end();
+	VssRayContainer outRays;
+
+	Intersectable::NewMail();
 
 	for (vit = vssRays.begin(); vit != vit_end; ++ vit)
 	{
 		VssRay *ray = *vit;
-		Intersectable *obj = (*vit)->mTerminationObject;	
-		exporter->ExportIntersectable(obj);
+		Intersectable *obj = (*vit)->mTerminationObject;
+
+		if (!obj->Mailed())
+		{
+			obj->Mail();
+			exporter->ExportIntersectable(obj);
+		}
+
+		if (ray->mPass == 4)
+			outRays.push_back(ray);
 	}	
 
-	exporter->ExportRays(vssRays);
+	exporter->ExportRays(outRays);
 
 	delete exporter;
@@ -691,5 +738,5 @@
 	rt->BindDepth();
 	rt->EnableTextureTarget();
-	cout << "depth: " << mNewTexture->GetDepthBits() << endl;
+	//cout << "depth: " << mNewTexture->GetDepthBits() << endl;
 
 	const int texFormat = GL_DEPTH_COMPONENT;
@@ -727,10 +774,4 @@
 	glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
 
-	/*for (int i = texWidth * 123; i < texWidth * 124; ++ i)
-	{
-		cout << data[i] << " ";
-	}
-	cout << "Saving image " << texWidth << " " << texHeight << endl;
-*/
 	string filename("depth.tga");
 	ilRegisterType(IL_FLOAT);
@@ -749,5 +790,5 @@
 }
 
-/*
+
 void GlobalLinesRenderer::ExportItemBuffer()
 {
@@ -758,17 +799,16 @@
 	const int components = 4;//mNewTexture->GetDepthBits() / 8;
 
-	float *data = new float[texWidth * texHeight * components*8];
+	unsigned char *data = new unsigned char [texWidth * texHeight * components];
+	//const int texFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
 	const int texFormat = GL_RGBA;
-	glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
-
-	string filename("items.tga");
-	ilRegisterType(IL_FLOAT);
+	glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_UNSIGNED_BYTE, data);
+
+	string filename("items.jpg");
+	ilRegisterType(IL_UNSIGNED_BYTE);
 
 	const int depth = 1;
 	const int bpp = components;
 
-	cout << "saving items to file " << endl;
-
-	ilTexImage(16, 16, depth, bpp, IL_RGBA, IL_FLOAT, data);
+	ilTexImage(texWidth, texHeight, depth, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data);
 	ilSaveImage((char *const)filename.c_str());
 
@@ -779,33 +819,4 @@
 	PrintGLerror("grab texture");
 }
-*/
-void GlobalLinesRenderer::ExportItemBuffer()
-{
-	mNewTexture->Bind();
-	mNewTexture->EnableTextureTarget();
-	cout << "depth: " << mNewTexture->GetDepthBits() << endl;
-
-	const int components = 4;//mNewTexture->GetDepthBits() / 8;
-
-	unsigned char *data = new unsigned char [texWidth * texHeight * components];
-	//const int texFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
-	const int texFormat = GL_RGBA;
-	glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_UNSIGNED_BYTE, data);
-
-	string filename("items.jpg");
-	ilRegisterType(IL_UNSIGNED_BYTE);
-
-	const int depth = 1;
-	const int bpp = components;
-
-	ilTexImage(texWidth, texHeight, depth, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data);
-	ilSaveImage((char *const)filename.c_str());
-
-	cout << "finished" << endl;
-	delete data;
-	cout << "data deleted" << endl;
-	mNewTexture->DisableTextureTarget();
-	PrintGLerror("grab texture");
-}
 
 
@@ -826,7 +837,7 @@
 
 	// process the buffers for the first layer
-	ProcessDepthBuffer(rays, false);
-
-	for(int i = 0; i < mMaxDepth; ++ i) 
+	ProcessDepthBuffer(rays, false, 0);
+
+	for(int i = 1; i < mMaxDepth; ++ i) 
 	{
 		// Peel another layer
@@ -836,7 +847,4 @@
 		mNewTexture->BeginCapture();
 		{
-			//if (mNewTexture->IsDoubleBuffered()) 
-			//	glDrawBuffer(GL_BACK);
-
 			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         
 
@@ -846,9 +854,6 @@
 			cgGLEnableTextureParameter(sTextureParam);
 
-			//glutSolidTorus(0.25, 1, 32, 64);
 			DrawGeometry();
 			
-			glPopMatrix();
-
 			cgGLDisableTextureParameter(sTextureParam);
 			cgGLDisableProfile(sCgFragmentProfile);
@@ -856,7 +861,6 @@
 		mNewTexture->EndCapture();
 
-		CLEAR_CONTAINER(rays);
 		// process the buffers for following layer
-		ProcessDepthBuffer(rays, true);
+		ProcessDepthBuffer(rays, true, i);
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h	(revision 1960)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.h	(revision 1963)
@@ -54,7 +54,12 @@
 	void ExportDepthBuffer();
 	void ExportItemBuffer();
-	void ProcessDepthBuffer(VssRayContainer &vssRays, const bool oldBufferInitialised);
+	void ProcessDepthBuffer(VssRayContainer &vssRays, const bool oldBufferInitialised, const int pass);
 	void DisplayBuffer(const bool isDepth);
 	
+	bool ClipToViewSpaceBox(const Vector3 &origin,
+							const Vector3 &termination,
+							Vector3 &clippedOrigin,
+							Vector3 &clippedTermination);
+
 	Intersectable *ExtractSamplePoint(float *depthBuffer,
 									  unsigned char *itemBuffer,
