Index: /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 323)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 333)
@@ -72,5 +72,5 @@
 	
 	// add top vertices	
-	for (int i = 0; i < 3; ++ i)
+	for (i = 0; i < 3; ++ i)
 		mesh->mVertices.push_back(baseTri.mVertices[i] + height * triNorm);
 	
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp	(revision 318)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp	(revision 333)
@@ -506,7 +506,6 @@
 	}
 
-
-    for (int i = 0; i < mCurrentVertexIndices.size(); i += 3)
-	{
+	for (i = 0; i < mCurrentVertexIndices.size(); i += 3)
+		{
 		Triangle3 baseTri(vertices[mCurrentVertexIndices[i + 0]], 
 						  vertices[mCurrentVertexIndices[i + 1]],
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 329)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Polygon3.h	(revision 333)
@@ -24,6 +24,10 @@
 public:
 	Polygon3();
+
 	Polygon3(const VertexContainer &vertices);
 	Polygon3(MeshInstance *parent);
+
+	~Polygon3();
+
 	// creates an "infinite" polygon from this plane
 	//Polygon3(Plane3 plane);
@@ -75,5 +79,5 @@
 	/** Returns the surface normal.
 	*/
-	inline Vector3 GetNormal() const;
+	Vector3 GetNormal() const;
 
 	/** Includes polygons to axis aligned box.
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 258)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Makefile	(revision 333)
@@ -1,5 +1,5 @@
 #############################################################################
 # Makefile for building: preprocessor
-# Generated by qmake (1.07a) (Qt 3.3.2) on: Tue Sep 06 13:22:58 2005
+# Generated by qmake (1.07a) (Qt 3.3.2) on: Tue Sep 06 13:46:00 2005
 # Project:  preprocessor.pro
 # Template: app
@@ -13,11 +13,11 @@
 LEX		= flex
 YACC		= byacc
-CFLAGS	=	-nologo -Zm200 -W0 -MDd -Zi -GX  -DUNICODE -DQT_DLL -DQT_THREAD_SUPPORT
-CXXFLAGS	=	-nologo -Zm200 -W0 -MDd -Zi -GX  -DUNICODE -DQT_DLL -DQT_THREAD_SUPPORT
+CFLAGS	=	-nologo -Zm200 -W0 -MD -O2 -GX  -DUNICODE -DQT_DLL -DQT_THREAD_SUPPORT -DQT_NO_DEBUG
+CXXFLAGS	=	-nologo -Zm200 -W0 -MD -O2 -GX  -DUNICODE -DQT_DLL -DQT_THREAD_SUPPORT -DQT_NO_DEBUG
 LEXFLAGS	=
 YACCFLAGS	=-d
 INCPATH	=	 -I"..\src" -I"..\support\xerces\include" -I"..\support\zlib\include" -I"..\support\boost" -I"..\support\devil\include" -I"$(QTDIR)\include" -I"d:\gametools\svn\trunk\VUT\GtpVisibilityPreprocessor\src" -I"C:\Qt\3.3.2\mkspecs\win32-msvc"
 LINK	=	link
-LFLAGS	=	/NOLOGO /DEBUG /SUBSYSTEM:console /LIBPATH:"../support/xerces/lib" /LIBPATH:"../support/devil/lib" /LIBPATH:"$(QTDIR)\lib"
+LFLAGS	=	/NOLOGO delayimp.lib /DELAYLOAD:comdlg32.dll /DELAYLOAD:oleaut32.dll /DELAYLOAD:winmm.dll /DELAYLOAD:wsock32.dll /DELAYLOAD:winspool.dll /SUBSYSTEM:console /LIBPATH:"../support/xerces/lib" /LIBPATH:"../support/devil/lib" /LIBPATH:"$(QTDIR)\lib"
 LIBS	=	 "qt-mt332.lib" "qtmain.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "imm32.lib" "winmm.lib" "wsock32.lib" "winspool.lib" "xerces-c_2.lib" "devil.lib" "ilu.lib" "ilut.lib" "opengl32.lib" "glu32.lib" "delayimp.lib"
 MOC		=	$(QTDIR)\bin\moc.exe
@@ -188,9 +188,4 @@
 	-$(DEL_FILE) ViewCell.obj
 	-$(DEL_FILE) ViewCellBsp.obj
-	-$(DEL_FILE) preprocessor.pdb
-	-$(DEL_FILE) preprocessor.ilk
-	-$(DEL_FILE) vc*.pdb
-	-$(DEL_FILE) vc*.idb
-
 
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.h	(revision 327)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.h	(revision 333)
@@ -107,31 +107,31 @@
   int
   CastRay(
-	  Ray &ray,
-	   MeshInstance *instance
-	  );
-
+					Ray &ray,
+					MeshInstance *instance
+					);
+	
   int
   CastRayToSelectedFaces(
-			 Ray &ray,
-			 const vector<int> &faces,
-			 Intersectable *instance
-			 );
-
+												 Ray &ray,
+												 const vector<int> &faces,
+												 Intersectable *instance
+												 );
+	
   int
   CastRayToFace(
-		const int faceIndex,
-		Ray &ray,
-		float &nearestT,
-		int &nearestFace,
-		Intersectable *instance
-		);
-
+								const int faceIndex,
+								Ray &ray,
+								float &nearestT,
+								int &nearestFace,
+								Intersectable *instance
+								);
+	
   
   int
   RayFaceIntersection(const int faceIndex,
-		      const Ray &ray,
-		      float &t,
-		      const float nearestT
-		      );
+											const Ray &ray,
+											float &t,
+											const float nearestT
+											);
   
   Plane3 GetFacePlane(const int faceIndex);
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 311)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 333)
@@ -163,32 +163,32 @@
       bool mOnlyDrivingAxis = false;
       if (mOnlyDrivingAxis) {
-	axis = box.Size().DrivingAxis();
-	costRatio = BestCostRatio(leaf,
-				  box,
-				  axis,
-				  position,
-				  objectsBack,
-				  objectsFront);
+				axis = box.Size().DrivingAxis();
+				costRatio = BestCostRatio(leaf,
+																	box,
+																	axis,
+																	position,
+																	objectsBack,
+																	objectsFront);
       } else {
-	costRatio = MAX_FLOAT;
-	for (int i=0; i < 3; i++) {
-	  float p;
-	  float r = BestCostRatio(leaf,
-				  box,
-				  i,
-				  p,
-				  objectsBack,
-				  objectsFront);
-	  if (r < costRatio) {
-	    costRatio = r;
-	    axis = i;
-	    position = p;
-	  }
-	}
+				costRatio = MAX_FLOAT;
+				for (int i=0; i < 3; i++) {
+					float p;
+					float r = BestCostRatio(leaf,
+																	box,
+																	i,
+																	p,
+																	objectsBack,
+																	objectsFront);
+					if (r < costRatio) {
+						costRatio = r;
+						axis = i;
+						position = p;
+					}
+				}
       }
       
       if (costRatio > mMaxCostRatio) {
-	//	cout<<"Too big cost ratio "<<costRatio<<endl;
-	axis = -1;
+				//	cout<<"Too big cost ratio "<<costRatio<<endl;
+				axis = -1;
       }
       break;
@@ -415,11 +415,11 @@
 float
 KdTree::BestCostRatio(
-		      KdLeaf *node,
-		      const AxisAlignedBox3 &box,
-		      const int axis,
-		      float &position,
-		      int &objectsBack,
-		      int &objectsFront
-		      )
+											KdLeaf *node,
+											const AxisAlignedBox3 &box,
+											const int axis,
+											float &position,
+											int &objectsBack,
+											int &objectsFront
+											)
 {
 
@@ -439,8 +439,8 @@
       totalIntersections += (*ci).intersectable->IntersectionComplexity();
     }
-
+	
   float intersectionsLeft = 0;
   float intersectionsRight = totalIntersections;
-
+	
   int objectsLeft = 0, objectsRight = node->mObjects.size();
   
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 332)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 333)
@@ -96,5 +96,5 @@
   
    
-  if (1) {
+  if (0) {
     p->ComputeVisibility();
     p->ExportPreprocessedData("scene.vis");
@@ -104,12 +104,12 @@
     Camera camera;
     //camera.LookAtBox(p->mKdTree->GetBox());
-	camera.LookInBox(p->mKdTree->GetBox());
-	camera.SetPosition(camera.mPosition + Vector3(0,300,0));
+		camera.LookInBox(p->mKdTree->GetBox());
+		camera.SetPosition(camera.mPosition + Vector3(0,300,0));
     camera.SnapImage("camera.jpg", p->mKdTree);
-
+		
     
     camera.LookInBox(p->mKdTree->GetBox());
     camera.SetPosition(camera.mPosition - Vector3(0,100,0));
-    camera.SnapImage("camera2.png", p->mKdTree);
+    camera.SnapImage("camera2.jpg", p->mKdTree);
   }
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 331)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 333)
@@ -26,4 +26,5 @@
   int mBspConstructionSamples;
 
+	
   SamplingPreprocessor();
 
@@ -47,9 +48,9 @@
 	CastRay(Intersectable *object, Ray &ray);
 
-	void
+	int
 	CastEdgeSamples(
 									Intersectable *object,
 									const Vector3 &point,
-									Mesh &mesh,
+									MeshInstance *mi,
 									const int samples
 									);
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 329)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 333)
@@ -249,5 +249,5 @@
 	VertexContainer::const_iterator vi;  
 	
-	for (int index = 0; index < (int)poly->mVertices.size(); ++ index) 
+	for (index = 0; index < (int)poly->mVertices.size(); ++ index) 
 		stream << index << " ";
 	
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/default.env
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 326)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/default.env	(revision 333)
@@ -30,7 +30,7 @@
 	sahUseFaces true
 	Termination {
-		minCost 2
+		minCost 1
 		maxDepth 18
-		maxCostRatio 0.9
+		maxCostRatio 0.95
 		ct_div_ci 0.5
 	}
@@ -43,7 +43,7 @@
 MeshKdTree {
 	Termination {
-		minCost 4
-		maxDepth 18
-		maxCostRatio 0.9
+		minCost 1
+		maxDepth 12
+		maxCostRatio 0.95
 		ct_div_ci 0.5
 	}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro	(revision 258)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/preprocessor.pro	(revision 333)
@@ -13,5 +13,5 @@
 
 # debuc config
-CONFIG		+= console warn_off thread debug
+CONFIG		+= console warn_off thread release
 
 # RELEASE CONFIG
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 332)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 333)
@@ -234,19 +234,23 @@
 
 
-void
+int
 SamplingPreprocessor::CastEdgeSamples(
 																			Intersectable *object,
 																			const Vector3 &point,
-																			Mesh &mesh,
+																			MeshInstance *mi,
 																			const int samples
 																			)
 {
 	Ray ray;
+	int maxTries = samples*10;
 	int i;
-	for (i=0; i < samples; i++) {
+	int rays = 0;
+	int edgeSamplesContributions = 0;
+	for (i=0; i < maxTries && rays < samples; i++) {
 		// pickup a random face of each mesh 
-		int face = RandomValue(0, mesh.mFaces.size()-1);
-		
-		Polygon3 poly(mesh.mFaces[face], &mesh);
+		Mesh *mesh = mi->GetMesh();
+		int face = RandomValue(0, mesh->mFaces.size()-1);
+		
+		Polygon3 poly(mesh->mFaces[face], mesh);
 		poly.Scale(1.001);
 		// now extend a random edge of the face
@@ -256,6 +260,12 @@
 																																		 poly.mVertices.size()];
 		SetupRay(ray, point, target - point);
-	  CastRay(object, ray);
+		if (!mesh->CastRay(ray, mi)) {
+			// the rays which intersect the mesh have been discarded since they are not tangent
+			// to the mesh
+			rays++;
+			edgeSamplesContributions += CastRay(object, ray);
+		}
 	}
+	return edgeSamplesContributions;
 }
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 329)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 333)
@@ -1719,5 +1719,5 @@
 
 		// compute angle to reference point
-		for (int i = 1; i < (int)planePoly->mVertices.size(); ++ i)
+		for (i = 1; i < (int)planePoly->mVertices.size(); ++ i)
 		{
 			float angle = 
@@ -1730,5 +1730,5 @@
 
 		// update vertices
-		for (int i = 1; i < (int)planePoly->mVertices.size(); ++ i)
+		for (i = 1; i < (int)planePoly->mVertices.size(); ++ i)
 			planePoly->mVertices[i] = vertexData[i - 1].mVertex;
 	}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 308)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 333)
@@ -50,10 +50,10 @@
 int
 Mesh::CastRayToFace(
-		    const int faceIndex,
-		    Ray &ray,
-		    float &nearestT,
-		    int &nearestFace,
-		    Intersectable *instance
-		    )
+										const int faceIndex,
+										Ray &ray,
+										float &nearestT,
+										int &nearestFace,
+										Intersectable *instance
+										)
 {
   float t;
@@ -72,6 +72,6 @@
     case Ray::LINE_SEGMENT:
       if (t <= 1.0f) {
-	ray.intersections.push_back(Ray::Intersection(t, instance, faceIndex));
-	hit++;
+				ray.intersections.push_back(Ray::Intersection(t, instance, faceIndex));
+				hit++;
       }
       break;
@@ -83,7 +83,7 @@
 int
 Mesh::CastRay(
-	      Ray &ray,
-	      MeshInstance *instance
-	      )
+							Ray &ray,
+							MeshInstance *instance
+							)
 {
   if (mKdTree) {
@@ -100,6 +100,6 @@
 
   for ( ;
-	faceIndex < mFaces.size();
-	faceIndex++) {
+				faceIndex < mFaces.size();
+				faceIndex++) {
     hits += CastRayToFace(faceIndex, ray, nearestT, nearestFace, instance);
     if (mIsConvex && nearestFace != -1)
@@ -119,8 +119,8 @@
 int
 Mesh::CastRayToSelectedFaces(
-			     Ray &ray,
-			     const vector<int> &faces,
-			     Intersectable *instance
-			     )
+														 Ray &ray,
+														 const vector<int> &faces,
+														 Intersectable *instance
+														 )
 {
   vector<int>::const_iterator fi;
@@ -134,6 +134,6 @@
 
   for ( fi = faces.begin();
-	fi != faces.end();
-	fi++) {
+				fi != faces.end();
+				fi++) {
     hits += CastRayToFace(*fi, ray, nearestT, nearestFace, instance);
     if (mIsConvex && nearestFace != -1)
@@ -157,9 +157,9 @@
 inline int
 int_lineseg(float px,
-	    float py,
-	    float u1,
-	    float v1,
-	    float u2,
-	    float v2)
+						float py,
+						float u1,
+						float v1,
+						float u2,
+						float v2)
 {
   float t;
@@ -180,5 +180,5 @@
   if (fabs(ydiff) < Limits::Small) {	  // denominator near 0
     if (((fabs(v1) > Limits::Small) ||
-	 (u1 > 0) || (u2 > 0)))
+				 (u1 > 0) || (u2 > 0)))
       return 0;
     return 1;
@@ -195,8 +195,8 @@
 int
 Mesh::RayFaceIntersection(const int faceIndex,
-			  const Ray &ray,
-			  float &t,
-			  const float nearestT
-			  )
+													const Ray &ray,
+													float &t,
+													const float nearestT
+													)
 {
   Face *face  = mFaces[faceIndex];
@@ -242,5 +242,5 @@
       // line u1, v1, u2, v2
       if ((v2 - v1)*(u1 - u) > (u2 - u1)*(v1 - v))
-	return Ray::NO_INTERSECTION;
+				return Ray::NO_INTERSECTION;
       u1 = u2;
       v1 = v2;
@@ -288,6 +288,6 @@
 int
 MeshInstance::CastRay(
-		      Ray &ray
-		      )
+											Ray &ray
+											)
 {
   int res = mMesh->CastRay(ray, this);
@@ -297,7 +297,7 @@
 int
 MeshInstance::CastRay(
-		      Ray &ray,
-		      const vector<int> &faces
-		      )
+											Ray &ray,
+											const vector<int> &faces
+											)
 {
   return mMesh->CastRayToSelectedFaces(ray, faces, this);
@@ -325,12 +325,12 @@
   Face *face = mFaces[faceIndex];
   return Plane3(mVertices[face->mVertexIndices[0]],
-		mVertices[face->mVertexIndices[1]],
-		mVertices[face->mVertexIndices[2]]);
+								mVertices[face->mVertexIndices[1]],
+								mVertices[face->mVertexIndices[2]]);
 }
 
 int
 TransformedMeshInstance::CastRay(
-				 Ray &ray
-				 )
+																 Ray &ray
+																 )
 {
   ray.ApplyTransform(Invert(mWorldTransform));
