Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.h	(revision 1520)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.h	(revision 1528)
@@ -18,5 +18,5 @@
 struct VssRayContainer;
 class Preprocessor;
-
+struct SimpleRay;
 
 /** This class provides an interface for ray casting.
@@ -36,10 +36,9 @@
 
 	virtual int CastRay(
-		const Vector3 &viewPoint,
-		const Vector3 &direction,
-		const float probability,
+		const SimpleRay &simpleRay,
         VssRayContainer &vssRays,
 		const AxisAlignedBox3 &box,
-		const bool castDoubleRay
+		const bool castDoubleRay,
+		const bool pruneInvalidRays = true
 		);
 
@@ -49,5 +48,6 @@
 		 VssRayContainer &vssRays,
 		 const AxisAlignedBox3 &sbox,
-		 const bool castDoubleRay
+		 const bool castDoubleRay,
+		 const bool pruneInvalidRays = true
 		 );
 	 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.h	(revision 1520)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.h	(revision 1528)
@@ -20,5 +20,5 @@
 struct VssRayContainer;
 class Preprocessor;
-
+struct SimpleRay;
 
 
@@ -36,10 +36,9 @@
 
 	virtual int CastRay(
-		const Vector3 &viewPoint,
-		const Vector3 &direction,
-		const float probability,
+	const SimpleRay &simpleRay,
         VssRayContainer &vssRays,
 		const AxisAlignedBox3 &box,
-		const bool castDoubleRay
+		const bool castDoubleRay,
+		const bool pruneInvalidRays = true
 		);
 
@@ -49,5 +48,6 @@
 		 VssRayContainer &vssRays,
 		 const AxisAlignedBox3 &sbox,
-		 const bool castDoubleRay
+		 const bool castDoubleRay,
+		 const bool pruneInvalidRays = true
 		 );
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1420)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1528)
@@ -97,60 +97,63 @@
 
 	if (rays.empty())
+	{
 		return false;
-
-  stream << "Shape {" << endl;
-  stream << "appearance Appearance {" << endl;
-  stream << "material Material {" << endl;
-  stream << "diffuseColor " << color.r << " " << color.g << " " << color.b << endl;
-  stream << "}" << endl; // end material
-  stream << "}" << endl; // end appearance
- 
-  stream << "geometry IndexedLineSet { coordIndex [" << endl;
-
-  int index = 0;
-  VssRayContainer::const_iterator ri = rays.begin();
-
-  for (; ri != rays.end(); ++ ri) 
-  {
-	  stream << index << " " << index + 1 << " -1\n";
-	  index += 2 ;
-  }
-  
-  stream << "]" << endl;
-  
-  stream << "coord Coordinate { point [" << endl;
-  
-  for (ri = rays.begin(); ri != rays.end(); ++ ri) 
-  {
-	  const Vector3 a = (*ri)->GetOrigin();
-	  const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
-
+	}
+
+	stream << "Shape {" << endl;
+	stream << "appearance Appearance {" << endl;
+	stream << "material Material {" << endl;
+	stream << "diffuseColor " << color.r << " " << color.g << " " << color.b << endl;
+	stream << "}" << endl; // end material
+	stream << "}" << endl; // end appearance
+
+	stream << "geometry IndexedLineSet { coordIndex [" << endl;
+
+	int index = 0;
+	VssRayContainer::const_iterator ri = rays.begin();
+
+	for (; ri != rays.end(); ++ ri) 
+	{
+		stream << index << " " << index + 1 << " -1\n";
+		index += 2 ;
+	}
+
+	stream << "]" << endl;
+
+	stream << "coord Coordinate { point [" << endl;
+
+	for (ri = rays.begin(); ri != rays.end(); ++ ri) 
+	{
+		const Vector3 a = (*ri)->GetOrigin();
+		const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
+
+	//	cout << "here45 origin " << a << " termination: " << b << endl;
 #if _DEBUG
-	  bool isnan = false;
-	  if (!a.CheckValidity())
-	  {
-		  cout << "error a"<<endl;
-		  isnan = true;
-	  }
-
-	   if (!b.CheckValidity())
-	  {
-		  cout << "error b"<<endl;
-		  isnan = true;
-	  }
-	  if (isnan)
-		  continue;
+		bool isnan = false;
+		if (!a.CheckValidity())
+		{
+			cout << "error a"<<endl;
+			isnan = true;
+		}
+
+		if (!b.CheckValidity())
+		{
+			cout << "error b"<<endl;
+			isnan = true;
+		}
+		if (isnan)
+			continue;
 #endif
-	  stream << a.x << " " << a.y << " " << a.z << " ,";
-	  stream << b.x << " " << b.y << " " << b.z << " ,\n";
-  }
-
-  stream << "]" << endl;
-  stream << "}" << endl;
-  stream << "}" << endl;
-  stream << "}" << endl;
-  //stream << "}" << endl;
-	
-  return true;
+		stream << a.x << " " << a.y << " " << a.z << " ,";
+		stream << b.x << " " << b.y << " " << b.z << " ,\n";
+	}
+
+	stream << "]" << endl;
+	stream << "}" << endl;
+	stream << "}" << endl;
+	stream << "}" << endl;
+	//stream << "}" << endl;
+
+	return true;
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1522)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1528)
@@ -129,4 +129,6 @@
 	float mThreshold;
 	VssRayContainer mVssRays;
+	int mCastPassSamples;
+	int mCastTotalSamples;
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h	(revision 1473)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Ray.h	(revision 1528)
@@ -35,35 +35,32 @@
   enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
 	
-  struct Intersection {
-    // the point of intersection
-    float mT;
-
-	// the normal of the intersection
-	Vector3 mNormal;
-	
-    // can be either mesh or a viewcell
-    Intersectable *mObject;
-		
-    // the face of the intersectable
-    int mFace;
-	
-    Intersection(const float t,
-				 const Vector3 &normal,
-				 Intersectable *object,
-				 const int face):mT(t),
-								 mNormal(normal),
-								 mObject(object), mFace(face) {}
-		
-				 Intersection(): mT(0), mNormal(0,0,0), mObject(NULL), mFace(0) {}
-		
-    bool operator<(
-				   const Intersection &b) const {
-
-      return 
-				mT
-				<
-				b.mT;
-    }
-    
+  struct Intersection 
+  {
+	  /// the point of intersection
+	  float mT;
+
+	  /// the normal of the intersection
+	  Vector3 mNormal;
+
+	  /// can be either mesh or a viewcell
+	  Intersectable *mObject;
+
+	  /// the face of the intersectable
+	  int mFace;
+
+	  Intersection(const float t,
+		 		   const Vector3 &normal,
+				   Intersectable *object,
+				   const int face): 
+	  mT(t), mNormal(normal), mObject(object), mFace(face) 
+	  {}
+
+	  Intersection(): mT(0), mNormal(0,0,0), mObject(NULL), mFace(0) 
+	  {}
+
+	  bool operator<(const Intersection &b) const 
+	  {
+		  return mT < b.mT;
+	  }
   };
 
@@ -318,5 +315,5 @@
 	mOrigin(o), mDirection(d), mPdf(p) {}
 
-  Vector3 Extrap(const float t) {
+  Vector3 Extrap(const float t) const {
 	return mOrigin + mDirection * t;
   }
@@ -326,6 +323,5 @@
 {
 public:
-  
-  
+    
   SimpleRayContainer():vector<SimpleRay>() {}
   
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h	(revision 1521)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.h	(revision 1528)
@@ -4,4 +4,6 @@
 #include "Containers.h"
 #include <string>
+#include "Vector3.h"
+
 using namespace std;
 
@@ -14,7 +16,9 @@
 class SimpleRayContainer;
 class AxisAlignedBox3;
-class Vector3;
+//class Vector3;
 struct VssRayContainer;
 class Preprocessor;
+struct SimpleRay;
+
 
 /** This class provides an interface for ray casting.
@@ -22,4 +26,5 @@
 class RayCaster
 {
+
 public:
 	
@@ -38,17 +43,14 @@
 	*/
 	VssRay *CastSingleRay(
-		const Vector3 &viewPoint,
-		const Vector3 &direction,
-		const float probability,
-        const AxisAlignedBox3 &box
+		const SimpleRay &simpleRay,
+	    const AxisAlignedBox3 &box
 		);
 
 	virtual int CastRay(
-		const Vector3 &viewPoint,
-		const Vector3 &direction,
-		const float probability,
+		const SimpleRay &simpleRay,
         VssRayContainer &vssRays,
 		const AxisAlignedBox3 &box,
-		const bool castDoubleRay
+		const bool castDoubleRay,
+		const bool pruneInvalidRays = true
 		) = 0;
 
@@ -58,24 +60,45 @@
 		 VssRayContainer &vssRays,
 		 const AxisAlignedBox3 &sbox,
-		 const bool castDoubleRay
+		 const bool castDoubleRay,
+		 const bool pruneInvalidRays = true
 		 ) = 0;
 	
 
 protected:
+	struct Intersection
+	{
+		Intersection(): mObject(NULL), mFaceId(0)
+		{}
+
+		Intersection(const Vector3 &p, const Vector3 &n, Intersectable *o, const int f):
+		mPoint(p), mNormal(n), mObject(o), mFaceId(f)
+		{}
+
+		Intersection(const Vector3 &p): mPoint(p), mObject(NULL), mFaceId(0)
+		{}
+		Vector3 mPoint;
+		Vector3 mNormal;
+		Intersectable *mObject;
+		int mFaceId;
+	};
 
 	int ProcessRay(
-			 const Vector3 &viewPoint,
-			 const Vector3 &direction,
-			 Intersectable *objectA,
-			 Vector3 &pointA,
-			 const Vector3 &normalA,
-			 Intersectable *objectB,
-			 Vector3 &pointB,
-			 const Vector3 &normalB,
-			 const float probability,
+			 const SimpleRay &ray,
+			 Intersection &hitA,
+			 Intersection &hitB,
 			 VssRayContainer &vssRays,
-			 const AxisAlignedBox3 &box
+			 const AxisAlignedBox3 &box,
+			 const bool castDoubleRay,
+			 const bool pruneInvalidRays = true
 			 );
 
+	/** Checks if ray is valid.
+		I.e., the ray is in valid view space.
+		@note: clamps the ray to valid view space.
+	*/
+	bool ValidateRay(const Vector3 &origin,
+					 const Vector3 &direction,
+        			 const AxisAlignedBox3 &box,
+					 Intersection &hit);
 	
   	const Preprocessor &mPreprocessor;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1524)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1528)
@@ -846,5 +846,6 @@
 					   SimpleRayContainer &rays,
 					   VssRayContainer &vssRays,
-					   const bool castDoubleRays
+					   const bool castDoubleRays,
+					   const bool pruneInvalidRays
 					   )
 {
@@ -860,5 +861,6 @@
 				vssRays,
 				mViewCellsManager->GetViewSpaceBox(),
-				castDoubleRays);
+				castDoubleRays,
+				pruneInvalidRays);
 			i += 16;
 		}
@@ -866,10 +868,9 @@
 		{
 			mRayCaster->CastRay(
-				rays[i].mOrigin,
-				rays[i].mDirection,
-				rays[i].mPdf,
+				rays[i],
 				vssRays,
 				mViewCellsManager->GetViewSpaceBox(),
-				castDoubleRays);
+				castDoubleRays,
+				pruneInvalidRays);
 			i ++;
 		}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1521)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1528)
@@ -485,7 +485,8 @@
 		
 			mViewCellsManager->GetViewPoint(viewpoint);
-			Vector3 direction = GetDirection(viewpoint, &mViewSpaceBox);
-
-			sampleContributions = mRayCaster->CastRay(viewpoint, direction, 1, mVssRays, mViewSpaceBox, true);
+			const Vector3 direction = GetDirection(viewpoint, &mViewSpaceBox);
+
+			const SimpleRay sray(viewpoint, direction);
+			sampleContributions = mRayCaster->CastRay(sray, mVssRays, mViewSpaceBox, true);
 
 			if (sampleContributions) {
@@ -615,9 +616,5 @@
 	}
 
-	for (int i=0; i < rays.size(); i++)
-	{
-		mRayCaster->CastRay(rays[i].mOrigin, rays[i].mDirection, 1, vssRays, mViewSpaceBox, true);
-	}
-
+	CastRays(rays, vssRays, true);
 	vssTree->AddRays(vssRays);
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1521)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1528)
@@ -138,5 +138,6 @@
   virtual void CastRays(SimpleRayContainer &rays,
 						VssRayContainer &vssRays,
-						const bool castDoubleRays);
+						const bool castDoubleRays,
+						const bool pruneInvalidRays = true);
 
   /** Returns a view cells manager of the given name.
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1522)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1528)
@@ -1318,5 +1318,4 @@
 	
 	float sum = 0.0f;
-
 	VssRayContainer::const_iterator it, it_end = rays.end();
 	
@@ -1831,5 +1830,5 @@
 		ViewCell *viewcell = *it;
 		
-		if (viewcell->GetValid()) // tests if view cellis in valid view space
+		if (viewcell->GetValid()) // tests if view cell is in valid view space
 		{
 			float contribution;
@@ -1838,7 +1837,7 @@
 			{
 				if (viewcell->GetPvs().GetSampleContribution(
-						ray.mTerminationObject,
-						ray.mPdf,
-						contribution))
+							ray.mTerminationObject,
+							ray.mPdf,
+							contribution))
 				{
 					++ ray.mPvsContribution;
@@ -1865,5 +1864,5 @@
 	}
 
-	// if addrays is true, sampled entities are stored in the pvs
+	// if true, the sampled entities are stored in the pvs
 	if (addRays)
 	{
@@ -1876,5 +1875,5 @@
 				// if view point is valid, add new object to the pvs
 				if (ray.mTerminationObject)
-				{
+				{cout <<"here22" << endl;
 					viewcell->GetPvs().AddSample(ray.mTerminationObject, ray.mPdf);
 				}				
@@ -5483,6 +5482,8 @@
 			{
 				// todo: maybe not correct for kd node pvs
-				if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
-															 ray.mPdf, contribution))
+				Intersectable *obj = mHierarchyManager->GetIntersectable(ray, true);
+				if (viewcell->GetPvs().GetSampleContribution(obj,
+															 ray.mPdf, 
+															 contribution))
 				{
 					++ ray.mPvsContribution;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1189)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1528)
@@ -392,6 +392,6 @@
 bool
 Pvs<T, S>::GetSampleContribution(T sample,
-							  const float pdf,
-							  float &contribution) 
+								 const float pdf,
+								 float &contribution) 
 {
   S *data = Find(sample);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 1521)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntelRayCaster.cpp	(revision 1528)
@@ -32,10 +32,9 @@
 
 int IntelRayCaster::CastRay(
-							const Vector3 &viewPoint,
-							const Vector3 &direction,
-							const float probability,
+							const SimpleRay &simpleRay,
 							VssRayContainer &vssRays,
 							const AxisAlignedBox3 &box,
-							const bool castDoubleRay
+							const bool castDoubleRay,
+							const bool pruneInvalidRays
 							)
 {
@@ -44,13 +43,12 @@
 	int hits = 0;
 	int hittriangle;
-	Vector3 pointA, pointB;
-	Vector3 normalA, normalB;
-	Intersectable *objectA = NULL, *objectB = NULL;
+	Intersection hitA, hitB;
+
 	float dist;
 	double normal[3];
 
 	hittriangle = mlrtaIntersectAS(
-		&viewPoint.x,
-		&direction.x,
+		&simpleRay.mOrigin.x,
+		&simpleRay.mDirection.x,
 		normal,
 		dist);
@@ -60,11 +58,11 @@
 			cerr<<"Warning: triangle index out of range! "<<hittriangle<<endl;
 		else {
-			objectA = mPreprocessor.mFaceParents[hittriangle].mObject;
-			normalA = Vector3(normal[0], normal[1], normal[2]);
+			hitA.mObject = mPreprocessor.mFaceParents[hittriangle].mObject;
+			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
-			pointA = viewPoint + direction*dist;
+			hitA.mPoint = simpleRay.Extrap(dist);
 		}
 	}
@@ -72,7 +70,7 @@
 	if (castDoubleRay)
 	{
-		Vector3 dir = -direction;
+		Vector3 dir = -simpleRay.mDirection;
 		hittriangle = mlrtaIntersectAS(
-			&viewPoint.x,
+			&simpleRay.mOrigin.x,
 			&dir.x,
 			normal,
@@ -84,11 +82,11 @@
 			else 
 			{
-				objectB = mPreprocessor.mFaceParents[hittriangle].mObject;
-				normalB = Vector3(normal[0], normal[1], normal[2]);
+				hitB.mObject = mPreprocessor.mFaceParents[hittriangle].mObject;
+				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
-				pointB = viewPoint + dir * dist;
+				hitB.mPoint = simpleRay.Extrap(dist);
 			}
 		}
@@ -96,11 +94,11 @@
 
 	return ProcessRay(
-		viewPoint,
-		direction,
-		objectA, pointA, normalA,
-		objectB, pointB, normalB,
-		probability,
+		simpleRay,
+		hitA,
+		hitB,
 		vssRays,
-		box
+		box,
+		castDoubleRay,
+		pruneInvalidRays
 		);
 }
@@ -112,5 +110,6 @@
 								VssRayContainer &vssRays,
 								const AxisAlignedBox3 &sbox,
-								const bool castDoubleRay)
+								const bool castDoubleRay,
+								const bool pruneInvalidRays)
 {
 	int i;
@@ -160,17 +159,15 @@
 	for (i=0; i < num; i++) 
 	{
-		Intersectable *objectA = NULL, *objectB = NULL;
-		Vector3 pointA, pointB;
-		Vector3 normalA, normalB;
-
+		Intersection hitA, hitB;
+		
 		if (forward_hit_triangles[i] != -1 ) {
 			if (forward_hit_triangles[i] >= mPreprocessor.mFaceParents.size())
 				cerr<<"Warning: triangle index out of range! "<<forward_hit_triangles[i]<<endl;
 			else { 
-				objectA = mPreprocessor.mFaceParents[forward_hit_triangles[i]].mObject;
+				hitA.mObject = mPreprocessor.mFaceParents[forward_hit_triangles[i]].mObject;
 				// Get the normal of that face
-				normalA = objectA->GetNormal(mPreprocessor.mFaceParents[forward_hit_triangles[i]].mFaceIndex);
+				hitA.mNormal = hitA.mObject->GetNormal(mPreprocessor.mFaceParents[forward_hit_triangles[i]].mFaceIndex);
 				//-rays[index+i].mDirection; // $$ temporary
-				pointA = rays[index+i].Extrap(forward_dist[i]);
+				hitA.mPoint = rays[index+i].Extrap(forward_dist[i]);
 			}
 		}
@@ -180,20 +177,20 @@
 				cerr<<"Warning: triangle  index out of range! "<<backward_hit_triangles[i]<<endl;
 			else { 
-				objectB = mPreprocessor.mFaceParents[backward_hit_triangles[i]].mObject;
-				normalB = objectB->GetNormal(mPreprocessor.mFaceParents[forward_hit_triangles[i]].mFaceIndex);
+				hitB.mObject = mPreprocessor.mFaceParents[backward_hit_triangles[i]].mObject;
+				hitB.mNormal = hitB.mObject->GetNormal(mPreprocessor.mFaceParents[forward_hit_triangles[i]].mFaceIndex);
 
 				// normalB = rays[index+i].mDirection; // $$ temporary
-				pointB = rays[index+i].Extrap(-backward_dist[i]);
+				hitB.mPoint = rays[index+i].Extrap(-backward_dist[i]);
 			}
 		}
 
 		ProcessRay(
-			rays[index+i].mOrigin,
-			rays[index+i].mDirection,
-			objectA, pointA, normalA,
-			objectB, pointB, normalB,
-			rays[index+i].mPdf,
+			rays[index + i],
+			hitA,
+			hitB,
 			vssRays,
-			sbox
+			sbox,
+			castDoubleRay,
+			pruneInvalidRays
 			);
 	}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1259)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.h	(revision 1528)
@@ -14,7 +14,9 @@
 class KdNode;
 
+
 class VssRay {
 public:
-  // various flags
+
+ // various flags
   enum {
     FPosDirX = 1,  // the direction of ray in X-axis is positive
@@ -87,72 +89,10 @@
 		 const int pass = 0,
 		 const float pdf = 1.0f
-		 ):
-	mMailbox(-1),
-    mOrigin(origin),
-    mTermination(termination),
-	mOriginObject(originObject),
-	mTerminationObject(terminationObject),
-	mRefCount(0),
-    mFlags(0),
-	mPass(pass),
-	mViewCells(0),
-	mWeightedPvsContribution(0),
-	mPdf(pdf),
-	mTerminationNode(NULL),
-	mOriginNode(NULL)
-	{
-		Precompute();
-	}
-	
-  VssRay(const Ray &ray):
-	mRefCount(0),
-	mFlags(0),
-	mMailbox(-1),
-	mOriginObject(ray.sourceObject.mObject),
-	mPass(0),
-	mViewCells(0),
-	mPdf(1.0f),
-	mTerminationNode(NULL),
-	mOriginNode(NULL)
-  {
-	if (ray.sourceObject.mObject)
-	  mOrigin = ray.Extrap(ray.sourceObject.mT);
-	else
-	  mOrigin = ray.GetLoc(); 
-	
-	//Debug << "origin: " << mOrigin << endl;
-	if (!ray.intersections.empty())
-	  {
-		mTermination = ray.Extrap(ray.intersections[0].mT);
-		mTerminationObject = ray.intersections[0].mObject;
-	  }
-	else
-	  {
-		mTermination = ray.Extrap(1e6);//TODO: should be Limits::Infinity
-		mTerminationObject = NULL;
-	  }
-	
-	Precompute();
-  }
-  
-  void Precompute() {
-    mFlags = 0;
-	Vector3 dir = GetDir();
-
-#define BIDIRECTIONAL_RAY 0
-#if BIDIRECTIONAL_RAY
-	if (dir.y < 0) {
-	  // swap objects and poits	
-	  swap(mOriginObject, mTerminationObject);
-	  swap(mOrigin, mTermination);
-	  dir = -dir;
-	}
-#endif
-	if (dir.x > 0.0f) mFlags |= FPosDirX;
-    if (dir.y > 0.0f) mFlags |= FPosDirY;
-    if (dir.z > 0.0f) mFlags |= FPosDirZ;
-
-    mInvSize = 1.0f/Magnitude(GetDir());
-  }
+		 );
+	
+  VssRay(const Ray &ray);
+	
+  
+  void Precompute();
 
   void Mail() { mMailbox = mailID; }
@@ -283,4 +223,6 @@
   }
 
+  /** Returns the data sampled on either the ray origin or termination.
+  */
   void GetSampleData(
 	  const bool isTerminaton, 
@@ -292,4 +234,23 @@
     
 };
+
+inline void VssRay::GetSampleData(const bool isTermination, 
+								  Vector3 &pt, 
+								  Intersectable **obj,
+								  KdNode **node) const
+{
+	if (isTermination)
+	{
+		pt = mTermination;
+		*obj = mTerminationObject;
+		*node = mTerminationNode;
+	}
+	else
+	{
+		pt = mOrigin;
+		*obj = mOriginObject;
+		*node = mOriginNode;
+	}
+}
 
 void
@@ -318,5 +279,5 @@
 // For sorting rays
 // --------------------------------------------------------------
-struct  SortableEntry
+struct SortableEntry
 {
   enum EType {
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1522)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RayCaster.cpp	(revision 1528)
@@ -22,11 +22,9 @@
 
 
-VssRay *RayCaster::CastSingleRay(const Vector3 &viewPoint,
-								 const Vector3 &direction,
-								 const float probability,
+VssRay *RayCaster::CastSingleRay(const SimpleRay &simpleRay,
 								 const AxisAlignedBox3 &box)
 {
 	VssRayContainer rays;
-	CastRay(viewPoint, direction, probability, rays, box, false);
+	CastRay(simpleRay, rays, box, false);
 
 	if (!rays.empty())
@@ -37,95 +35,121 @@
 
 
-int RayCaster::ProcessRay(const Vector3 &viewPoint,
-						  const Vector3 &direction,
-						  Intersectable *objectA,
-						  Vector3 &pointA,
-						  const Vector3 &normalA,
-						  Intersectable *objectB,
-						  Vector3 &pointB,
-						  const Vector3 &normalB,
-						  const float probability,
+/** Checks if ray is valid, (e.g., not in empty view space or outside the view space)
+*/
+bool RayCaster::ValidateRay(const Vector3 &origin,
+							const Vector3 &direction,
+							const AxisAlignedBox3 &box,
+							Intersection &hit)
+{
+	if (!hit.mObject) 
+	{
+		// compute intersection with the scene bounding box
+		static Ray ray;
+		mPreprocessor.SetupRay(ray, origin, direction);
+
+		float tmin, tmax;
+		if (box.ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax))
+		{
+			hit.mPoint = ray.Extrap(tmax);
+		}
+		else
+		{
+			return false;
+		}		
+	} 
+	else if (mPreprocessor.mDetectEmptyViewSpace)
+	{
+		if (DotProd(hit.mNormal, direction) >= 0) 
+		{	
+			hit.mObject = NULL;
+			return false;
+		}
+	}
+
+	return true;
+}
+
+
+int RayCaster::ProcessRay(const SimpleRay &simpleRay,
+						  Intersection &hitA,
+						  Intersection &hitB,
 						  VssRayContainer &vssRays,
-						  const AxisAlignedBox3 &box
-						  )
+						  const AxisAlignedBox3 &box,
+						  const bool castDoubleRay,
+						  const bool pruneInvalidRays)
 {
 	int hits = 0;
+
 #if DEBUG_RAYCAST
 	Debug<<"PR ";
 #endif
-	if (objectA == NULL && objectB == NULL)
+	
+	if (pruneInvalidRays)
+	{
+		if (!hitA.mObject && !hitB.mObject)
+		{
+			return 0;
+		}
+	
+		// inside test for bounding box
+		// enlarge box slightly so the view point fits for sure
+		AxisAlignedBox3 sbox = box;
+		sbox.Enlarge(Vector3(-Limits::Small));
+
+		if (!sbox.IsInside(simpleRay.mOrigin))
+		{
+			return 0;
+		}
+	}
+//	cout << "here81 " << hitA.mObject << endl;
+	const bool validA = 
+		ValidateRay(simpleRay.mOrigin, simpleRay.mDirection, box, hitA);
+	if (!validA && pruneInvalidRays)
+	{
 		return 0;
-
-	AxisAlignedBox3 sbox = box;
-	sbox.Enlarge(Vector3(-Limits::Small));
-
-	if (!sbox.IsInside(viewPoint))
-		return 0;
-
-	if (objectA == NULL) {
-		// compute intersection with the scene bounding box
-		static Ray ray;
-		mPreprocessor.SetupRay(ray, viewPoint, direction);
-
-		float tmin, tmax;
-		if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
-			pointA = ray.Extrap(tmax);
-		else
-			return 0;
-	} else {
-		if (mPreprocessor.mDetectEmptyViewSpace)
-			if (DotProd(normalA, direction) >= 0) {
-				// discard the sample
-				return 0;
-			}
 	}
 
-	if (objectB == NULL) {
-		// compute intersection with the scene bounding box
-		static Ray ray;
-		mPreprocessor.SetupRay(ray, viewPoint, -direction);
+	const bool validB = castDoubleRay && 
+		ValidateRay(simpleRay.mOrigin, -simpleRay.mDirection, box, hitB);
 
-		float tmin, tmax;
-		if (box.ComputeMinMaxT(ray, &tmin, &tmax) && tmin < tmax)
-			pointB = ray.Extrap(tmax);
-		else
-			return 0;
-	} else {
-		if (mPreprocessor.mDetectEmptyViewSpace)
-			if (DotProd(normalB, direction) <= 0) {
-				// discard the sample
-				return 0;
-			}
+	if (!validB && pruneInvalidRays)
+	{
+		return 0;
 	}
 
-	VssRay *vssRay  = NULL;
-	bool validSample = (objectA != objectB);
-	if (validSample) {
-		if (objectA) {
-			vssRay = new VssRay(
-				pointB,
-				pointA,
-				objectB,
-				objectA,
+	const bool validSample = !pruneInvalidRays || (hitA.mObject != hitB.mObject);
+	
+	if (validSample) 
+	{
+		if (!pruneInvalidRays || hitA.mObject) 
+		{
+			VssRay *vssRay = new VssRay(
+				hitB.mPoint,
+				hitA.mPoint,
+				hitB.mObject,
+				hitA.mObject,
 				mPreprocessor.mPass,
-				probability
+				simpleRay.mPdf
 				);
 			
 			vssRays.push_back(vssRay);
-			hits ++;
+			++ hits;
+			//cout << "here70 vssray 1: " << *vssRay << " " << vssRay->mTermination - vssRay->mOrigin << endl;
 		}
 
-		if (objectB) {
-			vssRay = new VssRay(
-				pointA,
-				pointB,
-				objectA,
-				objectB,
+		if (castDoubleRay && (!pruneInvalidRays || hitB.mObject))
+		{
+			VssRay *vssRay = new VssRay(
+				hitA.mPoint,
+				hitB.mPoint,
+				hitA.mObject,
+				hitB.mObject,
 				mPreprocessor.mPass,
-				probability
+				simpleRay.mPdf
 				);
 			
 			vssRays.push_back(vssRay);
-			hits ++;
+			++ hits;
+			//cout << "here71 vssray 2: " << *vssRay << endl;
 		}
 	}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp	(revision 1259)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VssRay.cpp	(revision 1528)
@@ -7,4 +7,86 @@
 int
 VssRay::mailID = 0;
+
+
+VssRay::VssRay(
+		 const Vector3 &origin,
+		 const Vector3 &termination,
+		 Intersectable *originObject,
+		 Intersectable *terminationObject,
+		 const int pass,
+		 const float pdf
+		 ):
+	mMailbox(-1),
+    mOrigin(origin),
+    mTermination(termination),
+	mOriginObject(originObject),
+	mTerminationObject(terminationObject),
+	mRefCount(0),
+    mFlags(0),
+	mPass(pass),
+	mViewCells(0),
+	mWeightedPvsContribution(0),
+	mPdf(pdf),
+	mTerminationNode(NULL),
+	mOriginNode(NULL),
+	mPvsContribution(0)
+{
+	Precompute();
+}
+
+
+VssRay::VssRay(const Ray &ray):
+	mRefCount(0),
+	mFlags(0),
+	mMailbox(-1),
+	mOriginObject(ray.sourceObject.mObject),
+	mPass(0),
+	mViewCells(0),
+	mPdf(1.0f),
+	mTerminationNode(NULL),
+	mOriginNode(NULL),
+	mPvsContribution(0)
+{
+	if (ray.sourceObject.mObject)
+		mOrigin = ray.Extrap(ray.sourceObject.mT);
+	else
+		mOrigin = ray.GetLoc(); 
+
+	//Debug << "origin: " << mOrigin << endl;
+	if (!ray.intersections.empty())
+	{
+		mTermination = ray.Extrap(ray.intersections[0].mT);
+		mTerminationObject = ray.intersections[0].mObject;
+	}
+	else
+	{
+		mTermination = ray.Extrap(1e6);//TODO: should be Limits::Infinity
+		mTerminationObject = NULL;
+	}
+
+	Precompute();
+}
+
+
+void VssRay::Precompute() 
+{
+	mFlags = 0;
+	Vector3 dir = GetDir();
+
+#define BIDIRECTIONAL_RAY 0
+#if BIDIRECTIONAL_RAY
+	if (dir.y < 0) {
+		// swap objects and poits	
+		swap(mOriginObject, mTerminationObject);
+		swap(mOrigin, mTermination);
+		dir = -dir;
+	}
+#endif
+	if (dir.x > 0.0f) mFlags |= FPosDirX;
+	if (dir.y > 0.0f) mFlags |= FPosDirY;
+	if (dir.z > 0.0f) mFlags |= FPosDirZ;
+
+	mInvSize = 1.0f/Magnitude(GetDir());
+}
 
 
@@ -265,23 +347,4 @@
 
 
-void VssRay::GetSampleData(const bool isTermination, 
-						   Vector3 &pt, 
-						   Intersectable **obj,
-						   KdNode **node) const
-{
-	if (isTermination)
-	{
-		pt = mTermination;
-		*obj = mTerminationObject;
-		*node = mTerminationNode;
-	}
-	else
-	{
-		pt = mOrigin;
-		*obj = mOriginObject;
-		*node = mOriginNode;
-	}
-}
-
-
-}
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1521)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/InternalRayCaster.cpp	(revision 1528)
@@ -21,10 +21,9 @@
 
 
-int InternalRayCaster::CastRay(const Vector3 &viewPoint,
-							   const Vector3 &direction,
-							   const float probability,
+int InternalRayCaster::CastRay(const SimpleRay &simpleRay,
 							   VssRayContainer &vssRays,
 							   const AxisAlignedBox3 &box,
-							   const bool castDoubleRay
+							   const bool castDoubleRay,
+							   const bool pruneInvalidRays
 							   )
 {
@@ -32,43 +31,35 @@
 	int hits = 0;
 	static Ray ray;
-	Intersectable *objectA = NULL, *objectB = NULL;
-	Vector3 pointA, pointB;
-	Vector3 normalA, normalB;
-
-#if 0
-	AxisAlignedBox3 sbox = box;
-	sbox.Enlarge(Vector3(-Limits::Small));
-	if (!sbox.IsInside(viewPoint))
-		return 0;
-#endif
-
-	mPreprocessor.SetupRay(ray, viewPoint, direction);
+	Intersection hitA(simpleRay.mOrigin), hitB(simpleRay.mOrigin);
+	
+	mPreprocessor.SetupRay(ray, simpleRay.mOrigin, simpleRay.mDirection);
 	ray.mFlags &= ~Ray::CULL_BACKFACES;
 
 	if (mKdTree->CastRay(ray)) 
 	{
-		objectA = ray.intersections[0].mObject;
-		pointA = ray.Extrap(ray.intersections[0].mT);
-		normalA = ray.intersections[0].mNormal;
+		hitA.mObject = ray.intersections[0].mObject;
+		hitA.mPoint = ray.Extrap(ray.intersections[0].mT);
+		hitA.mNormal = ray.intersections[0].mNormal; 
+	//	cout << "here91 " << hitA.mPoint << " !obj: " << hitA.mObject << endl;
 	}
 	
-	mPreprocessor.SetupRay(ray, viewPoint, -direction);
+	mPreprocessor.SetupRay(ray, simpleRay.mOrigin, -simpleRay.mDirection);
 	ray.mFlags &= ~Ray::CULL_BACKFACES;
 
 	if (castDoubleRay && mKdTree->CastRay(ray)) 
 	{
-		objectB = ray.intersections[0].mObject;
-		pointB = ray.Extrap(ray.intersections[0].mT);
-		normalB = ray.intersections[0].mNormal;
+		hitB.mObject = ray.intersections[0].mObject;
+		hitB.mPoint = ray.Extrap(ray.intersections[0].mT);
+		hitB.mNormal = ray.intersections[0].mNormal;
 	}
-	
+		
 	return ProcessRay(
-		viewPoint,
-		direction,
-		objectA, pointA, normalA,
-		objectB, pointB, normalB,
-		probability,
-		vssRays,
-		box
+		simpleRay,
+		hitA,
+		hitB,
+        vssRays,
+		box,
+		castDoubleRay,
+		pruneInvalidRays
 		);
 }
@@ -79,5 +70,6 @@
 								   VssRayContainer &vssRays,
 								   const AxisAlignedBox3 &sbox,
-								   const bool castDoubleRays)
+								   const bool castDoubleRays,
+								   const bool pruneInvalidRays)
 {
 	const int num = 16;
@@ -90,10 +82,5 @@
 	for (int i=index; i < index + num; i++) 
 	{
-		CastRay(rays[i].mOrigin,
-			rays[i].mDirection,
-			rays[i].mPdf,
-			vssRays,
-			sbox,
-			castDoubleRays);
+		CastRay(rays[i], vssRays, sbox,	castDoubleRays, pruneInvalidRays);
 	}
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1524)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1528)
@@ -23,5 +23,5 @@
 static vector<VizStruct> vizContainer;
 
-GvsPreprocessor::GvsPreprocessor(): Preprocessor(), mSamplingType(0)
+GvsPreprocessor::GvsPreprocessor(): Preprocessor(), mSamplingType(0), mCastPassSamples(0), mCastTotalSamples(0)
 {
 	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
@@ -57,5 +57,7 @@
 		VssRay *newRay = ReverseSampling(currentRay, hitTriangle, oldRay);
 		if (!HandleRay(newRay))
+		{
 			delete newRay;
+		}
 
 		return true;
@@ -68,13 +70,16 @@
 bool GvsPreprocessor::HandleRay(VssRay *vssRay)
 {
-	const int oldContri = vssRay->mPvsContribution;
 	mViewCellsManager->ComputeSampleContribution(*vssRay, true, false);
-
-	 // some pvs contribution for this ray?
-	if ((vssRay->mPvsContribution - oldContri) > 0)
-	{
-		//cout << "h";
+		
+	// some pvs contribution for this ray?
+	if (vssRay->mPvsContribution > 0)
+	{
+		cout << " h " << mCastPassSamples << " " << mSamplesPerPass << " " << mTotalSamples << " q: " << mRayQueue.size() << endl;
+		
+		cout << "error!!! " << vssRay->mPvsContribution << " " << vssRay->mTerminationObject << endl;
+
 		mRayQueue.push(vssRay);
 		mVssRays.push_back(new VssRay(*vssRay));
+		++ mCastPassSamples;
 		return true;
 	}
@@ -99,5 +104,5 @@
 	const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
 	
-	const float len = Magnitude(a);
+	const float len = 1;//Magnitude(a);
 
 	const Vector3 dir1 = Normalize(CrossProd(a, b)); //N((pi-xp)×(pi+1- pi));
@@ -172,5 +177,5 @@
 		SimpleRay sray(oldRay.mOrigin, p - oldRay.mOrigin);
 	
-		VssRay *newRay = mRayCaster->CastSingleRay(sray.mOrigin, sray.mDirection, 1, mViewSpaceBox);
+		VssRay *newRay = mRayCaster->CastSingleRay(sray, mViewSpaceBox);
 
 		if (!newRay) return 0;
@@ -217,5 +222,9 @@
 	{
 		const Vector3 rayDir = (*vit) - currentRay.GetOrigin();
-		simpleRays.push_back(SimpleRay(currentRay.GetOrigin(), rayDir));
+		SimpleRay sr(currentRay.GetOrigin(), rayDir);
+		simpleRays.AddRay(sr);
+
+		//cout << "here244 " << (*vit) << " " << currentRay.GetOrigin() << endl;
+		//cout << " sr" << sr.mOrigin << sr.mDirection << endl;
 	}
 
@@ -228,11 +237,13 @@
 	// cast rays to triangle vertices and determine visibility
 	VssRayContainer vssRays;
-	CastRays(simpleRays, vssRays, false);
-cout << "here211 " << simpleRays.size() << " " << vssRays.size() << endl;
+	CastRays(simpleRays, vssRays, false, false);
+	//CastRays(simpleRays, vssRays, true, true);
+	//cout << "here211 " << simpleRays.size() << " " << vssRays.size() << endl;
 	// add to ray queue
 	EnqueueRays(vssRays);
+	
 	const int n = (int)enlargedTriangle.size();
 	
-#if 1
+#if 0
     // recursivly subdivide each edge
 	for (int i = 0; i < n; ++ i)
@@ -303,5 +314,8 @@
 	SimpleRayContainer simpleRays;
 	GenerateRays(numSamples, sampleType, simpleRays);
-	
+
+	//cout << "here909 " << simpleRays.size() << endl;
+	//for (int i = 0; i < simpleRays.size(); ++ i)
+	//	cout << "here544 " << simpleRays[i].mOrigin << endl;
 	// generate vss rays
 	VssRayContainer samples;
@@ -310,5 +324,6 @@
 	// add to ray queue
 	EnqueueRays(samples);
-
+//for (int i = 0; i < samples.size(); ++ i)
+//		cout << "here1544 " << samples[i]->mOrigin << endl;
 	Debug << "generated " <<  numSamples << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
 
@@ -331,22 +346,21 @@
 int GvsPreprocessor::Pass()
 {
-	int castSamples = 0;
 	const int mSampleType = 0;
-	while (castSamples < mSamplesPerPass) 
-	{
+	// reset samples
+	mCastPassSamples = 0;
+	
+	while (mCastPassSamples < mSamplesPerPass) 
+	{	
 		// Ray queue empty => 
 		// cast a number of uniform samples to fill ray Queue
 		CastInitialSamples(mInitialSamples, mSampleType);
-
-		const int gvsSamples = ProcessQueue();
-#if 0
-		castSamples += gvsSamples;
-#else
-		castSamples += mInitialSamples;
-#endif
-		cout << "\nround finished, cast " << castSamples << " of " << mSamplesPerPass << endl;
-	}
-
-	return castSamples;
+		ProcessQueue();
+
+		cout << "\nround finished, cast " << mCastPassSamples << " of " << mSamplesPerPass << endl;
+	}
+
+	mCastTotalSamples += mCastPassSamples;
+
+	return mCastPassSamples;
 }
 
@@ -361,10 +375,9 @@
 		VssRay *ray = mRayQueue.top();
 		mRayQueue.pop();
-		
+	
 		castSamples += AdaptiveBorderSampling(*ray);
-
 		delete ray;
 	}
-
+	
 	return castSamples;
 }
@@ -385,18 +398,15 @@
 	}
 
-	int castSamples = 0;
-
-	while (castSamples < mTotalSamples) 
-	{
-		const int passSamples = Pass();
-		castSamples += passSamples;
-		
+	while (mCastTotalSamples < mTotalSamples) 
+	{
+		Pass();
+				
 		////////
 		//-- stats
 		cout << "+";
-		cout << "\nsamples cast " << passSamples << " (=" << castSamples << " of " << mTotalSamples << ")" << endl;
+		cout << "\nsamples cast " << mCastPassSamples << " (=" << mCastTotalSamples << " of " << mTotalSamples << ")" << endl;
 		//mVssRays.PrintStatistics(mStats);
 		mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
-			   << "#TotalSamples\n" << castSamples << endl;
+			   << "#TotalSamples\n" << mCastTotalSamples << endl;
 
 		mViewCellsManager->PrintPvsStatistics(mStats);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 1444)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/OspTree.cpp	(revision 1528)
@@ -1386,5 +1386,5 @@
 			if (!vdata)
 			{
-				Debug << "error!!" << endl;
+				Debug << "error! should not come here" << endl;
 				continue;
 			}
