Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 1166)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 1328)
@@ -28,29 +28,32 @@
 public:
 
+	SceneGraph();
 	~SceneGraph();
+	
+	bool Export(const string filename);
+  
+	int CollectObjects(ObjectContainer *instances);
+  
+	int AssignObjectIds();
+  
+	void GetStatistics(int &intersectables, int &faces) const;
 
-  SceneGraphNode *mRoot;
-  bool Export(const string filename);
+	AxisAlignedBox3 GetBox() const { return mRoot->mBox; }
   
-  int
-  CollectObjects(ObjectContainer *instances);
-  
-  int
-  AssignObjectIds();
-  
-  void
-  GetStatistics(int &intersectables, int &faces) const;
+	/** Exports binary version of the scene.
+	*/
+	void ExportScene(const string filename);
 
-  AxisAlignedBox3 GetBox() const { return mRoot->mBox; }
-  
-  /** Exports binary version of the scene.
-  */
-  void ExportScene(const string filename);
+	/** Loads binary version of the scene.
+	*/
+	void LoadScene(const string filename);
 
-  /** Loads binary version of the scene.
-  */
-  void LoadScene(const string filename);
+	SceneGraphNode *GetRoot();
+
+	void SetRoot(SceneGraphNode *sgNnode);
 
 protected:
+
+	 SceneGraphNode *mRoot;
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h	(revision 863)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Vector3.h	(revision 1328)
@@ -437,7 +437,8 @@
 CrossProd (const Vector3 &A, const Vector3 &B)
 {
-  return Vector3(A.y * B.z - A.z * B.y,
-		 A.z * B.x - A.x * B.z,
-		 A.x * B.y - A.y * B.x);
+  return 
+	  Vector3(A.y * B.z - A.z * B.y,
+			  A.z * B.x - A.x * B.z,
+			  A.x * B.y - A.y * B.x);
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 1194)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 1328)
@@ -28,4 +28,24 @@
 
 
+SceneGraphNode::~SceneGraphNode()
+{
+	CLEAR_CONTAINER(mGeometry);
+	
+	// recursivly delete all children
+	CLEAR_CONTAINER(mChildren);
+}
+
+
+/************************************************************/
+/*                 SceneGraph implementation                */
+/************************************************************/
+
+
+SceneGraph::SceneGraph():
+mRoot(NULL)
+{
+}
+
+
 SceneGraph::~SceneGraph()
 {
@@ -34,11 +54,13 @@
 
 
-SceneGraphNode::~SceneGraphNode()
+SceneGraphNode *SceneGraph::GetRoot()
 {
-	CLEAR_CONTAINER(mGeometry);
-	SceneGraphNodeContainer::iterator it, it_end = mChildren.end();
+	return mRoot;
+}
 
-	// recursivly delete all children
-	CLEAR_CONTAINER(mChildren);
+
+void SceneGraph::SetRoot(SceneGraphNode *root)
+{
+	mRoot = root;
 }
 
@@ -48,25 +70,28 @@
 {
 	instances->clear();
-  int number = 0;
-  stack<SceneGraphNode *> nodeStack;
-  
-  nodeStack.push(mRoot);
-  
-  while (!nodeStack.empty()) {
-    SceneGraphNode *node = nodeStack.top();
-    nodeStack.pop();
+	int number = 0;
 
-    ObjectContainer::const_iterator mi = node->mGeometry.begin();
-    for (; mi != node->mGeometry.end(); mi++)
-	  instances->push_back(*mi);
-	
-    SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
-    for (; ni != node->mChildren.end(); ni++) {
-      nodeStack.push(*ni);
-      number++;
-    }
-    
-  }
-  return number;
+	stack<SceneGraphNode *> nodeStack;
+	nodeStack.push(mRoot);
+
+	while (!nodeStack.empty()) {
+		SceneGraphNode *node = nodeStack.top();
+		nodeStack.pop();
+		cout << "here3.958" << endl;cout << "here3.9587654" << endl;
+		ObjectContainer::const_iterator mi = node->mGeometry.begin();	cout << "here3.958" << endl;
+		for (; mi != node->mGeometry.end(); mi++)
+		{
+			cout << "here3.9886" << endl;
+			instances->push_back(*mi);
+		}
+		cout << "here3.969999" << endl;
+		SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
+		for (; ni != node->mChildren.end(); ni++) {
+			nodeStack.push(*ni);	cout << "here3.959" << endl;
+			number++;
+		}
+		cout << "here3.96" << endl;
+	}
+	return number;
 }
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1299)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1328)
@@ -38,5 +38,5 @@
 static void AddGeometry(SceneGraph *scene)
 {
-	scene->mRoot->UpdateBox();
+	scene->GetRoot()->UpdateBox();
 
 	AxisAlignedBox3 sceneBox = scene->GetBox();
@@ -62,5 +62,5 @@
 
 				MeshInstance *mi = new MeshInstance(mesh);
-				scene->mRoot->mGeometry.push_back(mi);
+				scene->GetRoot()->mGeometry.push_back(mi);
 			}
 		}
@@ -81,5 +81,5 @@
 
 				MeshInstance *mi = new MeshInstance(mesh);
-				scene->mRoot->mGeometry.push_back(mi);
+				scene->GetRoot()->mGeometry.push_back(mi);
 			}
 		}
@@ -100,8 +100,8 @@
 
 			MeshInstance *mi = new MeshInstance(mesh);
-			scene->mRoot->mGeometry.push_back(mi);
+			scene->GetRoot()->mGeometry.push_back(mi);
 		}
 
-		scene->mRoot->UpdateBox();
+		scene->GetRoot()->UpdateBox();
 	}
 
@@ -122,5 +122,5 @@
 
 		MeshInstance *planeMi = new MeshInstance(planeMesh);
-		scene->mRoot->mGeometry.push_back(planeMi);
+		scene->GetRoot()->mGeometry.push_back(planeMi);
 	}	
 }
@@ -136,5 +136,6 @@
 mRenderSimulator(NULL),
 mPass(0),
-mRayCastMethod(0)
+mRayCastMethod(0),
+mSceneGraph(NULL)
 {
 	Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
@@ -242,18 +243,26 @@
 			  parser = new UnigraphicsParser;
 
-		cout<<filename<<endl;
+		cout << filename << endl;
+
+		SceneGraphNode *sroot = mSceneGraph->GetRoot();
 
 		if (mRayCastMethod == Preprocessor::INTEL_RAYCASTER)
-		  result = parser->ParseFile(filename, &mSceneGraph->mRoot,
-									 mLoadPolygonsAsMeshes,
-									 &mFaceParents);
+		{ cout << "here5800" << endl;
+			result = parser->ParseFile(
+				filename, 
+				&sroot,
+				mLoadPolygonsAsMeshes,
+				&mFaceParents);
+		}
 		else
-		  result = parser->ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes);
-		
+		{ cout << "here776645" << endl;
+			result = parser->ParseFile(filename, &sroot, mLoadPolygonsAsMeshes);
+		}
+		 cout << "here98809845" << endl;
 		delete parser;
 
 	} else {
 		// root for different files
-		mSceneGraph->mRoot = new SceneGraphNode;
+		mSceneGraph->SetRoot(new SceneGraphNode());
 		for (int i= 0; i < filenames.size(); i++) {
 		  if (strstr(filenames[i].c_str(), ".x3d"))
@@ -266,14 +275,22 @@
 		  
 		  if (mRayCastMethod == Preprocessor::INTEL_RAYCASTER)
-			success = parser->ParseFile(filename, &node,
-										mLoadPolygonsAsMeshes,
-										&mFaceParents);
+		  { cout << "here4500" << endl;
+			  success = parser->ParseFile(
+				  filename, 
+				  &node,
+				  mLoadPolygonsAsMeshes,
+				  &mFaceParents);
+		  }
 		  else
-			success = parser->ParseFile(filename, &node, mLoadPolygonsAsMeshes);
-		  
-		  if (success) {
-			mSceneGraph->mRoot->mChildren.push_back(node);
-			// at least one file parsed
-			result = true;
+		  {
+			  cout << "here45" << endl;
+			  success = parser->ParseFile(filename, &node, mLoadPolygonsAsMeshes);
+		  }
+
+		  if (success) 
+		  { cout << "here4509999" << endl;
+			  mSceneGraph->GetRoot()->mChildren.push_back(node);
+			  // at least one file parsed
+			  result = true;
 		  }
 		  
@@ -281,27 +298,26 @@
 		}
 	}
-	
-	
+	 cout << "here8888" << endl;
+
 	if (result) 
-	  {
+	{ cout << "here199" << endl;
 		// HACK 
 		if (ADDITIONAL_GEOMETRY_HACK)
 			AddGeometry(mSceneGraph);
-		
+
 		mSceneGraph->AssignObjectIds();
-	
+ cout << "here99" << endl;
 		int intersectables, faces;
 		mSceneGraph->GetStatistics(intersectables, faces);
-        
+ cout << "here999" << endl;
 		cout<<filename<<" parsed successfully."<<endl;
 		cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
 		cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
 		mSceneGraph->CollectObjects(&mObjects);
-		mSceneGraph->mRoot->UpdateBox();
-
+		mSceneGraph->GetRoot()->UpdateBox();
+ cout << "here9999" << endl;
 		if (0)
 		{
 			Exporter *exporter = Exporter::GetExporter("testload.x3d");
-
 			if (exporter)
 			{
@@ -311,6 +327,5 @@
 		}
 	}
-	
-	
+
 	return result;
 }
@@ -356,6 +371,7 @@
   // add mesh instances of the scene graph to the root of the tree
   KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
+  	cout << "here3.95" << endl;
   mSceneGraph->CollectObjects(&root->mObjects);
-  
+  	cout << "here3.97" << endl;
   long startTime = GetTime();
 
@@ -389,5 +405,5 @@
   if (exporter) {
     if (scene)
-      exporter->ExportScene(mSceneGraph->mRoot);
+      exporter->ExportScene(mSceneGraph->GetRoot());
 
     if (kdtree) {
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1302)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RssPreprocessor.cpp	(revision 1328)
@@ -278,5 +278,5 @@
   // $$JB temporarily do not export the scene
   if (0)
-	exporter->ExportScene(mSceneGraph->mRoot);
+	exporter->ExportScene(mSceneGraph->GetRoot());
   exporter->SetWireframe();
 
@@ -316,5 +316,5 @@
   // $$JB temporarily do not export the scene
   if (1)
-	exporter->ExportScene(mSceneGraph->mRoot);
+	exporter->ExportScene(mSceneGraph->GetRoot());
   exporter->SetWireframe();
 
@@ -343,5 +343,5 @@
   Exporter *exporter = Exporter::GetExporter(filename);
   exporter->SetFilled();
-  exporter->ExportScene(mSceneGraph->mRoot);
+  exporter->ExportScene(mSceneGraph->GetRoot());
   //  exporter->SetWireframe();
   bool result = exporter->ExportRssTree2( *tree, dir );
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1311)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1328)
@@ -160,5 +160,5 @@
   exporter->ExportKdTree(*mKdTree);
   exporter->SetFilled();
-  exporter->ExportScene(mSceneGraph->mRoot);
+  exporter->ExportScene(mSceneGraph->GetRoot());
   exporter->SetWireframe();
 
@@ -190,5 +190,5 @@
   Exporter *exporter = Exporter::GetExporter(filename);
   exporter->SetFilled();
-  exporter->ExportScene(mSceneGraph->mRoot);
+  exporter->ExportScene(mSceneGraph->GetRoot());
   //  exporter->SetWireframe();
   bool result = exporter->ExportVssTree2( *tree, dir );
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.h	(revision 860)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.h	(revision 1328)
@@ -4,44 +4,36 @@
 #include "Vector3.h"
 
+
 namespace GtpVisibilityPreprocessor {
 
-struct Triangle3 {
-  
-  Vector3 mVertices[3];
-  
-  Triangle3() {}
-  Triangle3(const Vector3 &a,
-		 const Vector3 &b,
-		 const Vector3 &c
-		 ) {
-    Init(a, b, c);
-  }
+class AxisAlignedBox3;
+class Ray;
 
-  void
-  Init(const Vector3 &a,
-       const Vector3 &b,
-       const Vector3 &c
-       ) {
-    mVertices[0] = a;
-    mVertices[1] = b;
-    mVertices[2] = c;
-    
-  }
 
-  Vector3 GetNormal() const {
-    Vector3 v1=mVertices[0]-mVertices[1], v2=mVertices[2]-mVertices[1];
-    return Normalize(CrossProd(v2,v1));
-  }
+struct Triangle3 
+{
+	Triangle3() {};
+	Triangle3(const Vector3 &a, const Vector3 &b, const Vector3 &c);
 
-  Vector3 GetCenter() const {
-    return (mVertices[0] + mVertices[1] + mVertices[2])/3.0f;
-  }
+	void Init(const Vector3 &a, const Vector3 &b, const Vector3 &c);
 
-  float GetSpatialAngle(const Vector3 &point) const;
+	Vector3 GetNormal() const;
 
-  float GetArea() const {
-	  Vector3 v1=mVertices[0]-mVertices[1], v2=mVertices[2]-mVertices[1];
-	  return 0.5f * Magnitude(CrossProd(v2, v1));
-  }
+	Vector3 GetCenter() const;
+
+	float GetSpatialAngle(const Vector3 &point) const;
+
+	float GetArea() const;
+
+	/// returns bounding box around this triangle
+	AxisAlignedBox3 GetBoundingBox() const;
+
+	/// Casts ray into this triangle. Returns hit
+	int CastRay(const Ray &ray, float &t, const float nearestT) const;
+
+	//////////////////////////////
+
+	/// the triangle vertices
+	Vector3 mVertices[3];
 };
 
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp	(revision 1076)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.cpp	(revision 1328)
@@ -316,20 +316,25 @@
 int Polygon3::CastRay(const Ray &ray, float &t, const float nearestT)
 {
-	Plane3 plane = GetSupportingPlane();
-	float dot = DotProd(plane.mNormal, ray.GetDir());
+	const Plane3 plane = GetSupportingPlane();
+	const float dot = DotProd(plane.mNormal, ray.GetDir());
 
 	// Watch for near-zero denominator
 	// ONLY single sided polygons!!!!!
 	if (dot > -Limits::Small)
-	//  if (fabs(dot) < Limits::Small)
-	return Ray::NO_INTERSECTION;
+	{
+		//  if (fabs(dot) < Limits::Small)
+		return Ray::NO_INTERSECTION;
+	}
 
 	t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot;
 
 	if (t <= Limits::Small)
-	return Ray::INTERSECTION_OUT_OF_LIMITS;
-
-	if (t >= nearestT) {
-	return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found
+	{
+		return Ray::INTERSECTION_OUT_OF_LIMITS;
+	}
+
+	if (t >= nearestT) 
+	{
+		return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found
 	}
 
@@ -338,5 +343,5 @@
 	int i;
 
-	int paxis = plane.mNormal.DrivingAxis();
+	const int paxis = plane.mNormal.DrivingAxis();
 
 	// Project the intersection point onto the coordinate plane
@@ -344,5 +349,5 @@
 	ray.Extrap(t).ExtractVerts(&u, &v, paxis);
 
-	int size = (int)mVertices.size();
+	const int size = (int)mVertices.size();
 
 	mVertices.back().ExtractVerts(&u1, &v1, paxis );
@@ -354,15 +359,14 @@
 		{
       		mVertices[i].ExtractVerts(&u2, &v2, paxis);
-	    
+	   
 			// line u1, v1, u2, v2
-	    
-			if ((v2 - v1)*(u1 - u) > (u2 - u1)*(v1 - v))
+	   		if ((v2 - v1) * (u1 - u) > (u2 - u1)*(v1 - v))
 				return Ray::NO_INTERSECTION;
 
 			u1 = u2;
 			v1 = v2;
-	}
-
-	return Ray::INTERSECTION;
+		}
+		
+		return Ray::INTERSECTION;
 	}
 
@@ -537,7 +541,11 @@
 
 		if ((count ++) % 2)
+		{
 			j = i ++;
+		}
 		else
+		{
 			j = k --;
+		}
 	}
 }
@@ -575,3 +583,20 @@
 }
 
-}
+
+AxisAlignedBox3 Polygon3::GetBoundingBox() const
+{
+	VertexContainer::const_iterator vit, vit_end = mVertices.end();
+	
+	AxisAlignedBox3 box;
+	box.Initialize();
+
+	for (vit = mVertices.begin(); vit != vit_end; ++ vit)
+	{
+		box.Include(*vit);
+	}
+
+	return box;
+}
+
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.h	(revision 1076)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.h	(revision 1328)
@@ -38,6 +38,6 @@
 	Polygon3(MeshInstance *parent);
 
-	// creates an "infinite" polygon from this plane
-	//Polygon3(Plane3 plane);
+	~Polygon3() {DEL_PTR(mPlane);}
+	
 	/** Copies all the vertices of the face.
 	*/
@@ -62,9 +62,4 @@
 	*/
 	float GetArea() const;
-
-	/** Classify polygon with respect to the plane.
-	    @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
-	*/
-	//int ClassifyPlane(const Plane3 &plane) const;
 	
 	/** Classify polygon with respect to the plane.
@@ -87,4 +82,5 @@
 	 */
 	Vector3 Center() const;
+
 	/** Checks if the polygon is valid, i.e., not degenerated.
 		@returns true if polygon is valid.
@@ -120,4 +116,7 @@
 	Polygon3 *CreateReversePolygon() const;
 
+	AxisAlignedBox3 GetBoundingBox() const;
+
+	////////////////////////////////////////////
 
 	/** Classify polygons with respect to the plane.
@@ -127,5 +126,4 @@
 							 const Plane3 &plane,
 							 const float epsilon);
-
 
 	/** Counts the number of different intersectables associated with the polygons.
@@ -137,4 +135,6 @@
 	static float GetArea(const PolygonContainer &cell);
 
+	//////////////////////////////////////////////////////
+
 	/**
 		Adds polygon to mesh description as a face
@@ -143,4 +143,5 @@
 
 
+	//////////////////////////////////////////
 	
 	/// vertices are connected in counterclockwise order.
@@ -156,8 +157,9 @@
 	RayContainer mPiercingRays;
 
-	~Polygon3() {DEL_PTR(mPlane);}
+protected:
+
 	Plane3 *mPlane;
+};
 
-};
 
 // Overload << operator for C++-style output
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp	(revision 1327)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp	(revision 1328)
@@ -1,3 +1,5 @@
 #include "IntersectableWrapper.h"
+#include "Mesh.h"
+#include "Triangle3.h"
 
 
@@ -5,5 +7,5 @@
 
 
-AxisAlignedBox3 FaceIntersectable::GetBox() const
+AxisAlignedBox3 TriangleIntersectable::GetBox() const
 {	
 	return mItem->GetBoundingBox();
@@ -11,55 +13,15 @@
 
 
-int FaceIntersectable::CastRay(Ray &ray)
+int TriangleIntersectable::CastRay(Ray &ray)
 {	// TODO matt
 	return 0;
 }
 	
-template<typename T>
-bool IntersectableWrapper<T>::IsConvex() const
+
+int TriangleIntersectable::NumberOfFaces() const
 {
-	return true;
-}
-
-template<typename T>
-bool IntersectableWrapper<T>::IsWatertight() const
-{
-	return true;
-}
-
-template<typename T>
-float IntersectableWrapper<T>::IntersectionComplexity()
-{
-	return 1.0f;
-}
-
-template<typename T>
-int IntersectableWrapper<T>::NumberOfFaces() const
-{
-	return 0;
-}
-
-template<typename T>
-int IntersectableWrapper<T>::GetRandomSurfacePoint(Vector3 &point,
-												Vector3 &normal)
-{
-	return 0;
-}
-
-template<typename T>
-int IntersectableWrapper<T>::GetRandomVisibleSurfacePoint(Vector3 &point,
-													   Vector3 &normal,
-													   const Vector3 &viewpoint,
-													   const int maxTries)
-{
-	return 0;
+	return 1;
 }
   
-template<typename T>
-ostream &IntersectableWrapper<T>::Describe(ostream &s)
-{
-	s << mItem;
-	return s;
-}
 
 }
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.h	(revision 1327)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.h	(revision 1328)
@@ -52,17 +52,5 @@
   }
   
-  AxisAlignedBox3 GetBoundingBox() const
-  {
-	  VertexIndexContainer::const_iterator vit, vit_end = (int)mVertexIndices.size();
-	  AxisAlignedBox3 box;
-	  box.Initialize();
-
-	  for (vit = mVertexIndices.begin(); vit != vit_end; ++ vit)
-	  {
-		  box.Include(*vit);
-	  }
-
-	  return box;
-  }
+  ////////////////////////////
 
   /// list of vertex pointers
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/MutualVisibility.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/MutualVisibility.cpp	(revision 863)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/MutualVisibility.cpp	(revision 1328)
@@ -519,5 +519,5 @@
     
     exporter->SetWireframe();
-    //    exporter->ExportScene(mSceneGraph->mRoot);
+    //    exporter->ExportScene(mSceneGraph->GetRoot());
 
     exporter->ExportBox(mSource);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1233)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/RssTree.cpp	(revision 1328)
@@ -2202,5 +2202,5 @@
 	//	exporter->ExportKdTree(*mKdTree);
 	exporter->SetWireframe();
-//	exporter->ExportScene(preprocessor->mSceneGraph->mRoot);
+//	exporter->ExportScene(preprocessor->mSceneGraph->GetRoot());
 	exporter->SetWireframe();
 	exporter->ExportBox(box);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp	(revision 863)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Triangle3.cpp	(revision 1328)
@@ -1,11 +1,139 @@
 #include "Triangle3.h"
+#include "Ray.h"
+#include "AxisAlignedBox3.h"
+
 
 namespace GtpVisibilityPreprocessor {
 
-float
-Triangle3::GetSpatialAngle(const Vector3 &point) const
+	
+Triangle3::Triangle3(const Vector3 &a, const Vector3 &b, const Vector3 &c) 
 {
-  return 0.0f;
+	Init(a, b, c);
 }
 
+
+void Triangle3::Init(const Vector3 &a, const Vector3 &b, const Vector3 &c) 
+{
+	mVertices[0] = a;
+	mVertices[1] = b;
+	mVertices[2] = c;
+
 }
+
+
+float Triangle3::GetSpatialAngle(const Vector3 &point) const
+{
+	return 0.0f;
+}
+
+
+int Triangle3::CastRay(const Ray &ray, float &t, const float nearestT) const
+{
+	// get triangle edge vectors and plane normal
+	const Vector3 u = mVertices[1] - mVertices[0];
+    const Vector3 v = mVertices[2] - mVertices[0];
+
+    const Vector3 n = u * v;  // cross product
+
+    const Vector3 dir = ray.GetDir();           // ray direction vector
+    const Vector3 w0 = ray.GetLoc() - mVertices[0];
+
+	// params to calc ray-plane intersect
+    const float a = - DotProd(n, w0);
+    const float b = DotProd(n, dir);
+
+	if (fabs(b) < Limits::Small) 
+	{   
+		// ray is parallel to 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
+		}
+    }
+
+    // get intersect point of ray with triangle plane
+    t = a / b;
+
+    if (t < 0.0) // ray goes away from triangle
+	{
+        return Ray::NO_INTERSECTION; // => no intersect
+	}
+
+    // for a segment, also test if (r > 1.0) => no intersect
+
+	// intersect point of ray and plane
+    const Vector3 pt = ray.GetLoc() + t * dir; 
+
+
+	///////////////////////////////////////////////
+    //-- is point inside triangle?
+   
+	const Vector3 w = pt - mVertices[0];
+
+	const float uu = DotProd(u, u);
+    const float uv = DotProd(u, v);
+    const float vv = DotProd(v, v);
+    
+	const float wu = DotProd(w, u);
+    const float wv = DotProd(w, v);
+    const float D = uv * uv - uu * vv;
+
+    // get and test parametric coords
+    const float s = (uv * wv - vv * wu) / D;
+
+    if (s < 0.0 || s > 1.0) // pt is outside triangle
+	{
+        return Ray::NO_INTERSECTION;
+	}
+
+	const float s2 = (uv * wu - uu * wv) / D;
+
+    if (s2 < 0.0 || (s + s2) > 1.0) // pt is outside triangle
+	{
+        return Ray::NO_INTERSECTION;
+	}
+
+	return Ray::INTERSECTION; // I is in T
+}
+
+
+AxisAlignedBox3 Triangle3::GetBoundingBox() const
+{
+	AxisAlignedBox3 box;
+	box.Initialize();
+
+	box.Include(mVertices[0]);
+	box.Include(mVertices[1]);
+	box.Include(mVertices[2]);
+
+	return box;
+}
+
+
+Vector3 Triangle3::GetNormal() const 
+{
+	const Vector3 v1 = mVertices[0] - mVertices[1];
+	const Vector3 v2 = mVertices[2]-mVertices[1];
+
+	return Normalize(CrossProd(v2, v1));
+}
+
+
+Vector3 Triangle3::GetCenter() const 
+{
+	return (mVertices[0] + mVertices[1] + mVertices[2]) / 3.0f;
+}
+
+
+float Triangle3::GetArea() const 
+{
+	Vector3 v1=mVertices[0]-mVertices[1], v2=mVertices[2]-mVertices[1];
+	return 0.5f * Magnitude(CrossProd(v2, v1));
+}
+
+
+}
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1294)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1328)
@@ -112,5 +112,5 @@
   string result = filename;
 
-  int pos = filename.rfind(a, (int)filename.size() - 1);
+  int pos = (int)filename.rfind(a, (int)filename.size() - 1);
   if (pos == filename.size() - a.size()) {
 	result.replace(pos, a.size(), b);
@@ -149,7 +149,7 @@
 
 	if (preprocessorType == "vss")
-	  {
+	{
 		preprocessor = new VssPreprocessor();
-	  }
+	}
 	else
 	{
@@ -176,5 +176,6 @@
 						preprocessor = new RenderSampler();
 					}
-					else {
+					else 
+					{
 						Environment::DelSingleton();
 						cerr<<"Unknown preprocessor type"<<endl;
@@ -186,29 +187,36 @@
 		}
 	}
-	
-
+
+	////////////////////////////////////////
 	//-- load scene 
 
 	Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
 	string filename(buff);
-	preprocessor->LoadScene(filename);
-	
+
+	if (!preprocessor->LoadScene(filename))
+	{
+		cout << "loading file " << filename << " failed" << endl;
+		Cleanup();
+		exit(1);
+	}
+
 	string rayCastFile = ReplaceSuffix(filename, ".obj", ".kdf");
-	
+
 	//-- initialize external ray casters
 	if (preprocessor->InitRayCast(rayCastFile))
-	  {
+	{
 		cout << "ray casting initialized!" << endl;
-	  }
+	}
 	else
 	{
 		cout << "ray casting initialization failed" << endl;
+		Cleanup();
 		exit(1);
 	}
-
+	cout << "here3.9" << endl;
 	//-- build kd tree from scene geometry
 	preprocessor->BuildKdTree();
 	preprocessor->KdTreeStatistics(cout);
-	
+	cout << "here3" << endl;
 	/*preprocessor->mKdTree->ExportBinTree("kd.bin.gz");
 	MeshManager::GetSingleton()->ExportEntries("meshes.bin");
@@ -234,5 +242,5 @@
 
 	DEL_PTR(kdTree2);
-*/
+*/cout << "here4" << endl;
 	// parse view cells related options
 	preprocessor->PrepareViewCells();
@@ -243,5 +251,5 @@
 		preprocessor->Export(filename + "-kdtree.x3d", false, true, false);	
 	}
-
+cout << "here5" << endl;
 	// create a preprocessor thread (note: capsulates calls to boost fuctions!)
 	//PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/ObjParser.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/ObjParser.cpp	(revision 1327)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/ObjParser.cpp	(revision 1328)
@@ -11,4 +11,5 @@
 #include "ObjParser.h"
 //#include "Material.h"
+#include "Triangle3.h"
 #include "Environment.h"
 #include "ResourceManager.h"
@@ -18,40 +19,37 @@
 namespace GtpVisibilityPreprocessor {
 
+#define CONNECT_SEQUENTIAL_FACES 1
 #define ROTATE_SCENE 0
+
 // HACK
-	static void RotateMesh(Mesh *mesh)
-	{
-		VertexContainer::iterator it, it_end = mesh->mVertices.end();
-
-		const float angle = 30.0f * PI / 180.0f;
-		const Matrix4x4 rot = RotationYMatrix(angle);
-
-		for (it = mesh->mVertices.begin(); it != it_end; ++ it)
-		{
-			(*it) = rot * (*it);        
-		}
-	}
-
-
-	struct ltstr
-	{
-		bool operator()(const string s1, const string s2) const
-		{
-			return s1 < s2;
-		}
-	};
+static void RotateMesh(Mesh *mesh)
+{
+	VertexContainer::iterator it, it_end = mesh->mVertices.end();
+
+	const float angle = 30.0f * PI / 180.0f;
+	const Matrix4x4 rot = RotationYMatrix(angle);
+
+	for (it = mesh->mVertices.begin(); it != it_end; ++ it)
+	{
+		(*it) = rot * (*it);        
+	}
+}
+
+struct ltstr
+{
+	bool operator()(const string s1, const string s2) const
+	{
+		return s1 < s2;
+	}
+};
 
 //const static int nMaxFaces = 6;
 
 
-Face *LoadFace(char *str, 
-			   const VertexContainer &vertices, 
-			   map<int, Vector3> &hashTable)
-{
-  //	cout << "f";
-
-	char *pch;
-	
-	pch = strtok(str + 1, " ");
+static Face *LoadFace(char *str, 
+					  const VertexContainer &vertices, 
+					  map<int, Vector3> &hashTable)
+{
+	char *pch = strtok(str + 1, " ");
 	
 	VertexIndexContainer indices;
@@ -59,5 +57,4 @@
 	{
 	   	const int index = (int)strtol(pch, NULL, 10) - 1;
-
 		//Debug << index << " x ";
 		
@@ -74,5 +71,31 @@
 
 
-Mesh *CreateMesh(FaceContainer &faces, const map<int, Vector3> &hashTable)
+static Triangle3 *LoadTriangle(char *str, 
+							   const VertexContainer &vertices, 
+							   map<int, Vector3> &hashTable)
+{
+	char *pch = strtok(str + 1, " ");
+	
+	VertexIndexContainer indices;
+	
+	while (pch != NULL)
+	{
+	   	const int index = (int)strtol(pch, NULL, 10) - 1;
+		//Debug << index << " x ";
+		
+		// store vertex in hash table
+		hashTable[index] = vertices[index];
+		indices.push_back(index);
+		
+		pch = strtok(NULL, " ");
+	}
+	//if (indices.size() > 4) return NULL;
+
+	return new Triangle3(indices[0], indices[1], indices[2]); 
+}
+
+
+static Mesh *CreateMesh(FaceContainer &faces, 
+						const map<int, Vector3> &hashTable)
 {
 	Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
@@ -117,6 +140,6 @@
 
 // HACK: associate mesh instances with triangles
-void AssociateFacesWithInstance(MeshInstance *mi,
-								vector<FaceParentInfo> &parents)
+static void AssociateFacesWithInstance(MeshInstance *mi,
+									   vector<FaceParentInfo> &parents)
 {
   Mesh *mesh = mi->GetMesh();
@@ -131,4 +154,27 @@
 
 
+static void ProcessMesh(FaceContainer &faces, 
+						map<int, Vector3> &hashTable,
+						SceneGraphNode *root,
+						vector<FaceParentInfo> *parents)
+{
+	Mesh *mesh = CreateMesh(faces, hashTable);
+
+	// make an instance of this mesh
+	MeshInstance *mi = new MeshInstance(mesh);
+		
+	if (parents) 
+	{
+		AssociateFacesWithInstance(mi, *parents);
+	}
+
+	root->mGeometry.push_back(mi);	
+
+	// reset tables
+	hashTable.clear();
+	faces.clear();
+}
+
+
 bool ObjParser::ParseFile(const string filename,
 						  SceneGraphNode **proot,
@@ -139,5 +185,5 @@
 	if ((file = fopen(filename.c_str(), "rt")) == NULL)
 		return false;
-	
+	cout << "here2" << endl;
 	map<int, Vector3> hashTable; // table associating indices with vectors
 	
@@ -169,27 +215,19 @@
 		case 'f': 
 			{
+				cout << "f";
+#if CONNECT_SEQUENTIAL_FACES
 				Face *face = LoadFace(str, vertices, hashTable);
 				if (!face) break;
-#if 0
+
 				faces.push_back(face);
+
 				if (faces.size() >= nMaxFaces)
 				{
-					Mesh *mesh = CreateMesh(faces, hashTable);
-
-					// make an instance of this mesh
-					MeshInstance *mi = new MeshInstance(mesh);
-					root->mGeometry.push_back(mi);
-					
-					if (parents) 
-						AssociateFacesWithInstance(mi, *parents);
-										
-					// reset tables
-					hashTable.clear();
-					faces.clear();
+					ProcessMesh(faces, hashTable, root, parents);
 				}
 #else
-				cout << "f";
-				root->mGeometry.push_back(new FaceIntersectable(face));
-				hashTable.clear();
+				
+				Triangle3 *triangle = LoadTriangle(str, vertices, hashTable);
+				root->mGeometry.push_back(new TriangleIntersectable(triangle));
 #endif
 				break;
@@ -200,21 +238,11 @@
 	}
 
+#if CONNECT_SEQUENTIAL_FACES
 	// there could be faces remaining
 	if (!faces.empty())
 	{	
-		Mesh *mesh = CreateMesh(faces, hashTable);
-
-		// make an instance of this mesh
-		MeshInstance *mi = new MeshInstance(mesh);
-		
-		if (parents) 
-			AssociateFacesWithInstance(mi, *parents);
-		
-		root->mGeometry.push_back(mi);	
-	}
-
-	// reset tables
-	hashTable.clear();
-	faces.clear();
+		ProcessMesh(faces, hashTable, root, parents);
+	}
+#endif
 
 	fclose(file);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1314)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 1328)
@@ -64,5 +64,5 @@
 		 KD_INTERSECTABLE,
 		 BVH_INTERSECTABLE,
-		 FACE_INTERSECTABLE
+		 TRIANGLE_INTERSECTABLE
 		};
   
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Camera.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Camera.cpp	(revision 878)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Camera.cpp	(revision 1328)
@@ -121,5 +121,5 @@
 
 	if (sceneGraph) 
-	  exporter->ExportScene(sceneGraph->mRoot);
+	  exporter->ExportScene(sceneGraph->GetRoot());
 	
     //exporter->ExportKdTree(*tree);
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1315)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 1328)
@@ -12,4 +12,7 @@
 class BvhNode;
 struct Face;
+class Ray;
+struct Triangle3;
+
 
 
@@ -164,13 +167,18 @@
 };
 
-class FaceIntersectable: public IntersectableWrapper<Face>
+class TriangleIntersectable: public IntersectableWrapper<Triangle3>
 {
 public:
-	FaceIntersectable(Face *item):
-	IntersectableWrapper<Face>(item) {}
+	TriangleIntersectable(Triangle3 *item):
+	IntersectableWrapper<Triangle3>(item) {}
+
+	int CastRay(Ray &ray);
+	AxisAlignedBox3 GetBox() const;
+
+	int NumberOfFaces() const;
 
 	int Type() const
 	{
-		return Intersectable::FACE_INTERSECTABLE;
+		return Intersectable::TRIANGLE_INTERSECTABLE;
 	}
 };
Index: /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp
===================================================================
--- /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 1297)
+++ /GTP/trunk/Lib/Vis/Preprocessing/src/Mesh.cpp	(revision 1328)
@@ -798,3 +798,4 @@
 }
 
-}
+
+}
