Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1419)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.cpp	(revision 1420)
@@ -125,8 +125,8 @@
 	  const Vector3 a = (*ri)->GetOrigin();
 	  const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
-	  
+
+#if _DEBUG
 	  bool isnan = false;
-
-	  if ((a[0] != a[0]) || (a[1] != a[1]) || (a[2] != a[2]))
+	  if (!a.CheckValidity())
 	  {
 		  cout << "error a"<<endl;
@@ -134,15 +134,14 @@
 	  }
 
-	  if ((b[0] != b[0]) || (b[1] != b[1]) || (b[2] != b[2]))
+	   if (!b.CheckValidity())
 	  {
 		  cout << "error b"<<endl;
 		  isnan = true;
 	  }
-
-	  if (!isnan)
-	  {
-		stream << a.x << " " << a.y << " " << a.z << " ,";
-		stream << b.x << " " << b.y << " " << b.z << " ,\n";
-	  }
+	  if (isnan)
+		  continue;
+#endif
+	  stream << a.x << " " << a.y << " " << a.z << " ,";
+	  stream << b.x << " " << b.y << " " << b.z << " ,\n";
   }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 1419)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/X3dParser.cpp	(revision 1420)
@@ -231,5 +231,5 @@
 	if (!mLoadMeshes)
 	{
-		////////////////////////////////////
+		///////////////////
 		//-- load data as single triangles instead of whole meshes
 
@@ -249,12 +249,19 @@
 			Polygon3 poly(face, &tempMesh);
 			poly.Triangulate(triangles);
-
+			
 			vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
 
 			for (tit = triangles.begin(); tit != tit_end; ++ tit)
 			{
-				TriangleIntersectable *ti = new TriangleIntersectable(*tit);
-				cout << "t: " << (*tit) << endl;
-				mCurrentNode->mGeometry.push_back(ti);
+				if ((*tit).CheckValidity())
+				{
+					TriangleIntersectable *ti = new TriangleIntersectable(*tit);
+					//cout << "t: " << (*tit) << endl;
+					mCurrentNode->mGeometry.push_back(ti);
+				}
+				else
+				{
+					cout << "error tri:\n" << (*tit) << endl;
+				}
 			}
 #if 0
@@ -262,9 +269,9 @@
 			Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
 
-			VertexIndexContainer::const_iterator vit, vit_end = face->mVertexIndices.end();
-
-			int i = 0;
+				
 			// dummy vertex indices container
 			VertexIndexContainer vcIndices;
+			VertexIndexContainer::const_iterator vit, vit_end = face->mVertexIndices.end();
+			int i = 0;
 
 			for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit, ++ i)
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h	(revision 1328)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h	(revision 1420)
@@ -98,4 +98,8 @@
   inline void Normalize();
   
+  /** Returns false if this vector has a nan component.
+  */
+  bool CheckValidity() const;
+
   /**
     ===> Using ArbitraryNormal() for constructing coord systems 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.h	(revision 1344)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.h	(revision 1420)
@@ -35,4 +35,7 @@
 	 friend istream& operator>> (istream &s, Triangle3 &A);
 
+	 /** Checks if this triangle is ill-defined.
+	 */
+	 bool CheckValidity() const;
 
 	 //////////////////////////////
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp	(revision 1419)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp	(revision 1420)
@@ -43,8 +43,7 @@
 {	
 	mVertices.reserve(face->mVertexIndices.size());
-	
 	VertexIndexContainer::iterator it, it_end = face->mVertexIndices.end();
 
-	for (it = face->mVertexIndices.begin(); it != it_end;  ++ it)
+	for (it = face->mVertexIndices.begin(); it != it_end; ++ it)
 	{
 		mVertices.push_back(parentMesh->mVertices[*it]);
@@ -69,8 +68,5 @@
 	VertexContainer::iterator it, it_end = mVertices.end();
 
-	for (it = mVertices.begin(); it != it_end;  ++ it)
-	{
-		cout << (*it) << " ";
-	}cout<<endl<<endl;
+	//for (it = mVertices.begin(); it != it_end;  ++ it){cout << (*it) << " ";}cout<<endl<<endl;
 	//cout << "poly n: " << GetNormal() << " t normal: " << tri.GetNormal() << endl;
 }
@@ -267,4 +263,5 @@
 }
 
+
 bool Polygon3::Valid(const float epsilon) const
 {
@@ -272,7 +269,7 @@
 		return false;
 
-	//TODO: remove for performance
-#if 0
-	if (1)
+	// matt: removed for performance issues
+#if _DEBUG
+	if (0)
 	{
 		// check if area exceeds certain size
@@ -291,7 +288,7 @@
 		for (it = mVertices.begin(); it != it_end; ++it)
 		{
-			if (EpsilonEqualV3(vtx, *it, 0.0001))
+			if (EpsilonEqualV3(vtx, *it, epsilon))
 			{
-				//Debug << "Malformed vertices:\n" << *this << endl;
+				//cout << "Malformed vertices:\n" << *this << endl;
 				return false;
 			}
@@ -590,4 +587,5 @@
 	const int n = (int)mesh.mVertices.size();
 
+
 	/////////////
     //-- add the vertices to the mesh
@@ -609,10 +607,12 @@
 	poly.Triangulate(indices);
 
+	//if (indices.size() < 3) return; // something is wrong
+	
 	// add indices of triangle strip
-	for (int i = n; i < (int)indices.size() + n; i += 3)
-	{
-		Face *face = new Face(indices[i], 
-							  indices[i + 1], 
-							  indices[i + 2]);
+	for (int i = 0; i < (int)indices.size(); i += 3)
+	{
+		Face *face = new Face(indices[i] + n, 
+							  indices[i + 1] + n, 
+							  indices[i + 2] + n);
 		mesh.AddFace(face);
 	}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1419)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1420)
@@ -5097,5 +5097,4 @@
 			exporter->ExportBox(bbox);
 			exporter->SetFilled(); 
-
 				
 			if (0 && mExportGeometry)
@@ -5112,4 +5111,5 @@
 			mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, &bbox, false);
 			ExportViewCellsForViz(exporter, &bbox, GetClipPlane());
+
 			delete exporter;
 			cout << "finished in " << TimeDiff(starttime, GetTime()) * 1e-3f << " secs" << endl;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1419)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Exporter.cpp	(revision 1420)
@@ -152,4 +152,5 @@
 	}
 
+	// reset to filled
 	SetFilled();
 
@@ -266,5 +267,5 @@
 		{
 		case Intersectable::TRIANGLE_INTERSECTABLE:
-			{
+			{cout << "y";
 				TriangleIntersectable *ti = dynamic_cast<TriangleIntersectable *>(obj);
 				polys.push_back(new Polygon3(ti->GetItem()));
@@ -272,5 +273,5 @@
 			}
 		case Intersectable::MESH_INSTANCE:
-			{
+			{cout << "x";
 				MeshInstance *mi = dynamic_cast<MeshInstance *>(obj);
 				ExportMesh(mi->GetMesh());
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.h	(revision 1404)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.h	(revision 1420)
@@ -174,5 +174,5 @@
 	//s << setprecision(6) << "Polygon:\n";
 	for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it)
-		s << *it << endl;
+		s << *it << " ";
 	
 	return s;
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.cpp	(revision 863)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.cpp	(revision 1420)
@@ -2,4 +2,6 @@
 #include "Vector3.h"
 #include "Halton.h"
+#include "float.h"
+
 
 namespace GtpVisibilityPreprocessor {
@@ -253,3 +255,10 @@
 }
 
-}
+
+bool Vector3::CheckValidity() const
+{
+	return !(_isnan(x) || _isnan(y) || _isnan(z));
+	 //return ((x != x) || (y != y) || (z != z));
+}
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp	(revision 1344)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp	(revision 1420)
@@ -31,5 +31,6 @@
 int Triangle3::CastRay(const Ray &ray, float &t, const float nearestT, Vector3 &normal) const
 {
-	/*VertexContainer vertices;
+#if 0
+	VertexContainer vertices;
 	vertices.push_back(mVertices[0]);
 	vertices.push_back(mVertices[1]);
@@ -40,9 +41,12 @@
 	int dummy = poly.CastRay(ray, t, nearestT);
 
-	 cout << "polyversion code: " << dummy << " t: " << t << " nearestT: " << nearestT << endl;
-return dummy;*/
-	/////////////////////////////////////////////
-	// specialised ray casting version
+	cout << "polyversion code: " << dummy << " t: " << t << " nearestT: " << nearestT << endl;
+	return dummy;
+#endif
+	
+	//////////////
+	// specialised triangle ray casting version
 
+	///////////
 	//-- calc ray-plane intersection
 	
@@ -51,7 +55,9 @@
     const Vector3 v = mVertices[2] - mVertices[1];
 
-    normal = Normalize(CrossProd(v, u)); // cross product
+	// cross product
+    normal = Normalize(CrossProd(v, u));
 
-    const Vector3 dir = ray.GetDir(); // ray direction vector
+	// ray direction vector
+    const Vector3 dir = ray.GetDir();
     const Vector3 w0 = ray.GetLoc() - mVertices[1];
 
@@ -64,11 +70,13 @@
 	{   
 		// ray is parallel to triangle plane
-        if (a == 0) // ray lies in triangle plane
+        if (a == 0)
 		{
+			// ray lies in triangle plane
             return Ray::INTERSECTION_OUT_OF_LIMITS;
 		}
         else
 		{
-			return Ray::NO_INTERSECTION; // ray disjoint from plane
+			// ray disjoint from plane
+			return Ray::NO_INTERSECTION;
 		}
     }
@@ -87,9 +95,15 @@
 	}
 
-	///////////////////////////////////////////////
+	/////////////////
     //-- found intersection point
 	//-- check if it is inside triangle
   
 	const Vector3 pt = ray.GetLoc() + t * dir; 
+
+	if (!pt.CheckValidity())
+	{
+		cout << "tr: " << *this << endl;
+		cout << "v: " << pt << " t: " << t << " a: " << a << " b: " << b << " n: " << normal << endl;
+	}
 
 	const Vector3 w = pt - mVertices[1];
@@ -157,3 +171,12 @@
 
 
+bool Triangle3::CheckValidity() const
+{
+	return !(
+		EpsilonEqualV3(mVertices[0], mVertices[1]) ||
+		EpsilonEqualV3(mVertices[0], mVertices[2]) ||
+		EpsilonEqualV3(mVertices[1], mVertices[2])
+		);
 }
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1418)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1420)
@@ -283,6 +283,9 @@
 	cout << "\nfinished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
 
-	cout << "here5 " << GetViewSpaceBox() << endl;
-	cout << "here6 " << GetObjectSpaceBox() << endl;
+#if _DEBUG
+	cout << "view space: " << GetViewSpaceBox() << endl;
+	cout << "object space:  " << GetObjectSpaceBox() << endl;
+#endif
+
 	mHierarchyStats.Stop();
 	mVspTree->mVspStats.Stop();
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 1419)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 1420)
@@ -521,4 +521,19 @@
 
 
+static void PrintFace(const Mesh *mesh, const int idx, ostream &app)
+{
+	Face *face = mesh->mFaces[idx];
+
+	VertexIndexContainer::iterator it, it_end = face->mVertexIndices.end();
+
+	cout << "face " << idx << endl;
+	for (it = face->mVertexIndices.begin(); it != it_end; ++ it)
+	{
+		cout << (*it) << ": " << mesh->mVertices[*it] << " ";
+	}
+	cout << endl;
+}
+
+
 void Mesh::Print(ostream &app) const
 {
@@ -531,11 +546,8 @@
 	app << endl;
 
-	/*FaceContainer::const_iterator fit, fit_end = mVertices.end();
-
-	for (vit = mVertices.begin(); vit != vit_end; ++ vit)
+	for (int i = 0; i < (int)mFaces.size(); ++ i)
 	{
-		app << (*vit);
-	}
-	app << endl;*/
+		PrintFace(this, i, app);
+	}
 }
 
