Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1473)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 1486)
@@ -13,6 +13,4 @@
 class VssRay;
 
-
-typedef stack<VssRay *> RayQueue;
 
 /** Sampling based visibility preprocessing. The implementation is 
@@ -31,4 +29,13 @@
 protected:
 
+	struct GvsRayInfo
+	{
+		VssRay *mRay;
+		bool mFoundDiscontinuity;
+	}
+
+	
+	typedef stack<GvsRayInfo> RayQueue;
+
 	/** Runs the adaptive sampling. The method starts with a number of random rays given
 		by the queue and continues as long it finds new visible geometry (i.e., the queue is
@@ -43,5 +50,5 @@
 		@returns the number of samples cast.
 	*/
-	int Pass(const int passSamples);
+	int Pass();
 
 	/** Generates the rays starting the adaptive visibility sampling process.
@@ -61,11 +68,11 @@
 		b) if triangle was found reverse sampling
 	*/
-	int HandleRay(VssRay &ray);
+	int HandleRay(const GvsRayInfo &ray);
 
 	/** 
 	*/	
-	int AdaptiveBorderSampling(VssRay &ray);
+	int AdaptiveBorderSampling(const VssRay &ray);
 	
-	int ReverseSampling(VssRay &ray);
+	int ReverseSampling(const VssRay &ray);
 
 	/** Cast samples according to a specific sampling strategy.
@@ -76,4 +83,12 @@
 		VssRayContainer &passSamples) const;
 
+	void CreateNewSamples(
+		VertexContainer &samples, 
+		const VssRay &ray, 
+		const Triangle3 &hitTriangle, 
+		const int index,
+		const float eps);
+
+	const bool DiscontinuityFound(const VssRay &ray) const;
 
 
@@ -81,9 +96,12 @@
 
 	ofstream mStats;
-	int mSamples;
+
 	int mSamplesPerPass;
 	RayQueue mRayQueue; 
 	int mSamplingType;
 	int mTotalSamples;
+	int mInitialSamples;
+	AxisAlignedBox3 mViewSpaceBox;
+	float mEps;
 };
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1458)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Environment.cpp	(revision 1486)
@@ -1200,10 +1200,10 @@
    RegisterOption("VssPreprocessor.initialSamples",
 				 optInt,
-				 "initial_samples=",
+				 "vss_initial_samples=",
 				 "100000");
   
   RegisterOption("VssPreprocessor.testBeamSampling", 
 				optBool, 
-				"beam_sampling", 
+				"vss_beam_sampling=", 
 				"false");
 
@@ -1220,5 +1220,5 @@
   RegisterOption("VssPreprocessor.samplesPerPass",
 				 optInt,
-				 "samples_per_pass=",
+				 "vss_samples_per_pass=",
 				 "100000");
 
@@ -1243,14 +1243,40 @@
 	  "false");
    
-
-    RegisterOption("VssPreprocessor.useViewSpaceBox",
+   RegisterOption("VssPreprocessor.useViewSpaceBox",
 	  optBool,
 	  "vss_use_viewspace_box=",
-	  "false");
+	  "false");   
    
 
-  /************************************************************************************/
-  /*                         View cells related options                               */
-  /************************************************************************************/
+
+   /************************************************************************************/
+   /*                         GvsPrerpocessor related options                          */
+   /************************************************************************************/
+
+
+   RegisterOption("GvsPreprocessor.totalSamples",
+		 optInt,
+		 "gvs_total_samples=",
+		 "1000000");
+   
+   RegisterOption("GvsPreprocessor.samplesPerPass",
+		 optInt,
+		 "gvs_samples_per_pass=",
+		 "100000");
+   
+   RegisterOption("GvsPreprocessor.initialSamples",
+		 optInt,
+		 "gvs_initial_samples=",
+		 "256");
+
+   RegisterOption("GvsPreprocessor.epsilon",
+		 optFloat,
+		 "gvs_epsilon",
+		 "0.00001");
+
+
+  /***********************************************************************************/
+  /*                         View cells related options                              */
+  /***********************************************************************************/
 
 
@@ -1513,8 +1539,7 @@
 
 
-
-	/************************************************************************************/
-	/*                         Bsp tree related options                                 */
-	/************************************************************************************/
+	/******************************************************************/
+	/*                    Bsp tree related options                    */
+	/******************************************************************/
 
 
@@ -1665,5 +1690,5 @@
 	RegisterOption("Preprocessor.loadMeshes",
 					optBool,
-					"loadMeshes=",
+					"preprocessor_load_meshes=",
 					"true");
 
@@ -1794,5 +1819,5 @@
 	RegisterOption("RssPreprocessor.initialSamples",
 									optInt,
-									"initial_samples=",
+									"rss_initial_samples=",
 									"100000");
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1421)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 1486)
@@ -327,9 +327,8 @@
 Preprocessor::ExportPreprocessedData(const string filename)
 {
-  
-  mViewCellsManager->ExportViewCells(filename, true, mObjects);
-  
-  return true;
-}
+	mViewCellsManager->ExportViewCells(filename, true, mObjects);
+	return true;
+}
+
 
 bool
@@ -352,6 +351,8 @@
   // export the preprocessed information to a file
   if (mExportVisibility)
-	ExportPreprocessedData(mVisibilityFileName);
-  
+  {
+	  ExportPreprocessedData(mVisibilityFileName);
+  }
+
   return true;
 }
@@ -445,5 +446,6 @@
 	}
 	
-	//-- parameters for render heuristics evaluation
+	////////
+	//-- render heuristics evaluation
 	float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
 
@@ -472,19 +474,16 @@
 
   
-bool
-Preprocessor::ConstructViewCells()
-{
-  // if not already loaded, construct view cells from file
-  if (!mLoadViewCells) {
-	mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
+bool Preprocessor::ConstructViewCells(const AxisAlignedBox3 &viewSpaceBox)
+{
+	mViewCellsManager->SetViewSpaceBox(viewSpaceBox);
 	
 	// construct view cells using it's own set of samples
 	mViewCellsManager->Construct(this);
-	
-	//-- several visualizations and statistics
-	Debug << "view cells construction finished: " << endl;
+
+	// visualizations and statistics
+	Debug << "finished view cells:" << endl;
 	mViewCellsManager->PrintStatistics(Debug);
-  }
-  return true;
+	    
+	return true;
 }
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 1473)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.vcproj	(revision 1486)
@@ -506,4 +506,10 @@
 			</File>
 			<File
+				RelativePath=".\PreprocessorFactory.cpp">
+			</File>
+			<File
+				RelativePath=".\PreprocessorFactory.h">
+			</File>
+			<File
 				RelativePath="..\src\PreprocessorThread.cpp">
 			</File>
Index: GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorFactory.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorFactory.cpp	(revision 1486)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorFactory.cpp	(revision 1486)
@@ -0,0 +1,48 @@
+#include "PreprocessorFactory.h"
+#include "SamplingPreprocessor.h"
+#include "VssPreprocessor.h"
+#include "RssPreprocessor.h"
+#include "ExactPreprocessor.h"
+#include "RenderSampler.h"
+#include "GvsPreprocessor.h"
+
+
+namespace GtpVisibilityPreprocessor {
+
+
+Preprocessor *PreprocessorFactory::CreatePreprocessor(const string &preprocessorType)
+{
+	if (preprocessorType == "vss")
+	{
+		return new VssPreprocessor();
+	}
+	
+	if (preprocessorType == "rss")
+	{
+		return new RssPreprocessor();
+	}
+	
+	if (preprocessorType == "exact")
+	{
+		return new ExactPreprocessor();
+	}
+
+    if (preprocessorType == "sampling")
+	{
+		return new SamplingPreprocessor();
+	}
+	
+	if (preprocessorType == "render")
+	{
+		return new RenderSampler();
+	}
+	
+	if (preprocessorType == "gvs")
+	{
+		return new GvsPreprocessor();
+	}
+
+	return NULL;
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1418)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.cpp	(revision 1486)
@@ -24,6 +24,5 @@
 
 VssPreprocessor::VssPreprocessor():
-  mVssRays(),
-  mViewSpaceBox(NULL)
+  mVssRays()
 {
   // this should increase coherence of the samples
@@ -43,4 +42,5 @@
   useViewspacePlane = mUseViewSpaceBox; //hack
 
+  mViewSpaceBox.Initialize();
   
   Debug << "*********** vss preprocessor options **************" << endl;
@@ -56,10 +56,5 @@
 {
 	CLEAR_CONTAINER(mVssRays);
-	DEL_PTR(mViewSpaceBox);
-}
-
-
-
-
+}
 
 
@@ -163,9 +158,8 @@
   exporter->SetWireframe();
 
-  if (mViewSpaceBox) {
-	exporter->SetForcedMaterial(RgbColor(1,0,1));
-	exporter->ExportBox(*mViewSpaceBox);
-	exporter->ResetForcedMaterial();
-  }
+  exporter->SetForcedMaterial(RgbColor(1,0,1));
+  exporter->ExportBox(mViewSpaceBox);
+  exporter->ResetForcedMaterial();
+
 
   VssRayContainer rays;
@@ -207,9 +201,7 @@
   exporter->ExportKdTree(*mKdTree);
 
-  if (mViewSpaceBox) {
-	exporter->SetForcedMaterial(RgbColor(1,0,0));
-	exporter->ExportBox(*mViewSpaceBox);
-	exporter->ResetForcedMaterial();
-  }
+  exporter->SetForcedMaterial(RgbColor(1,0,0));
+  exporter->ExportBox(mViewSpaceBox);
+  exporter->ResetForcedMaterial();
 
   exporter->SetForcedMaterial(RgbColor(0,0,1));
@@ -412,18 +404,16 @@
   if (mUseViewSpaceBox)
   {
-	//mViewSpaceBox = ConstructViewSpaceBox();
-
 	  if (!mEnlargeViewSpace)
 	  {
-		  mViewSpaceBox = new AxisAlignedBox3(box);
+		  mViewSpaceBox = AxisAlignedBox3(box);
 	  }
 	  else
 	  {
-		  mViewSpaceBox = new AxisAlignedBox3(mKdTree->GetBox());
+		  mViewSpaceBox = AxisAlignedBox3(mKdTree->GetBox());
 
 		  if (0) 
 		  {
 			  // HACK: enlarge in y directon
-			  Vector3 size = mViewSpaceBox->Size();
+			  Vector3 size = mViewSpaceBox.Size();
 			
 			  size[1] *= 1.25;
@@ -431,48 +421,30 @@
 			  //Vector3 enlarge(size[0] * 4.0f, 0.0f, 0.0f);
 
-			  mViewSpaceBox->Enlarge(enlarge);
-			  mViewSpaceBox->SetMax(mViewSpaceBox->Max() + enlarge);
+			  mViewSpaceBox.Enlarge(enlarge);
+			  mViewSpaceBox.SetMax(mViewSpaceBox.Max() + enlarge);
 		  } 
 		  else 
 		  {
-			  AxisAlignedBox3 tbox(*mViewSpaceBox);
-
-			  Vector3 size = mViewSpaceBox->Size();
-			  tbox.SetMax(0, mViewSpaceBox->Max(0) + size[0] * 0.5f);
-			  tbox.SetMin(0, mViewSpaceBox->Min(0) + size[0]);
-			  *mViewSpaceBox = tbox;
-
-			  // $$ JB temporary
-			  /*AxisAlignedBox3 tempbox = *mViewSpaceBox;
-			  
-			  float s = tempbox.Size(0);
-			  
-			  tempbox.Scale(0.8f);
-			  tempbox.SetMax(0, box.Max(0) + s*0.8f);
-			  tempbox.SetMin(0, box.Min(0) + s*0.8f);
-			  *mViewSpaceBox = tempbox;*/
+			  AxisAlignedBox3 tbox(mViewSpaceBox);
+
+			  Vector3 size = mViewSpaceBox.Size();
+			  tbox.SetMax(0, mViewSpaceBox.Max(0) + size[0] * 0.5f);
+			  tbox.SetMin(0, mViewSpaceBox.Min(0) + size[0]);
+			  mViewSpaceBox = tbox;
 		  }		
 	  }
-	  //Debug << "view space box: " << *mViewSpaceBox << endl;
   }
   else
   {
-	  mViewSpaceBox = NULL;
-  }
-  
-  AxisAlignedBox3 vbox = mViewSpaceBox ? *mViewSpaceBox : mKdTree->GetBox();
-
+	  mViewSpaceBox = AxisAlignedBox3(mKdTree->GetBox());
+  }
+  
   mSceneGraph->CollectObjects(&mObjects);
 
   if (!mLoadViewCells)
   {
-	  //-- generate new view cells or reconstruct them
+	  //-- generate new view cells from the scratch
 	  //-- for construction the manager uses it's own set of samples
-	  mViewCellsManager->SetViewSpaceBox(vbox);
-	  mViewCellsManager->Construct(this);
-
-	  // output visualizations and statistics
-	  Debug << "view cells construction finished: " << endl;
-	  mViewCellsManager->PrintStatistics(Debug);
+	  ConstructViewCells(mViewSpaceBox);
   }
 #if 0
@@ -509,12 +481,12 @@
 
 		int s = Min(mSamplesPerPass, mInitialSamples);
-		for (int k=0; k < s; k++) {
-			// changed by matt
+		for (int k=0; k < s; k++) 
+		{
 			Vector3 viewpoint; 
-			//			viewpoint = GetViewpoint(mViewSpaceBox);
+		
 			mViewCellsManager->GetViewPoint(viewpoint);
-			Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
-
-			sampleContributions = CastRay(viewpoint, direction, 1, mVssRays, vbox);
+			Vector3 direction = GetDirection(viewpoint, &mViewSpaceBox);
+
+			sampleContributions = CastRay(viewpoint, direction, 1, mVssRays, mViewSpaceBox);
 
 			if (sampleContributions) {
@@ -589,5 +561,5 @@
   // viewcells = Construct(mVssRays);
 
-  vssTree->Construct(mVssRays, mViewSpaceBox);
+  vssTree->Construct(mVssRays, NULL);
   cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
 
@@ -635,9 +607,7 @@
 	if (!mUseImportanceSampling) {
 	  for (int j=0; j < num; j++) {
-	    // changed by matt
-		//Vector3 viewpoint = GetViewpoint(mViewSpaceBox);
-		Vector3 viewpoint; 
+	   	Vector3 viewpoint; 
 		mViewCellsManager->GetViewPoint(viewpoint);
-		Vector3 direction = GetDirection(viewpoint, mViewSpaceBox);
+		Vector3 direction = GetDirection(viewpoint, NULL);
 		rays.push_back(SimpleRay(viewpoint, direction));
 	  }
@@ -647,5 +617,5 @@
 
 	for (int i=0; i < rays.size(); i++)
-	  CastRay(rays[i].mOrigin, rays[i].mDirection, 1, vssRays, vbox);
+	  CastRay(rays[i].mOrigin, rays[i].mDirection, 1, vssRays, mViewSpaceBox);
 
 	vssTree->AddRays(vssRays);
@@ -698,6 +668,6 @@
 
 
-  //////////////////////////////////
-  //-- render simulation after 
+  ////////////////////
+  //-- render simulation after construction
 
   mRenderSimulator->RenderScene();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1404)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp	(revision 1486)
@@ -26,4 +26,5 @@
   
 }
+
 
 X3dExporter::~X3dExporter()
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1421)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 1486)
@@ -88,7 +88,7 @@
   bool PrepareViewCells();
 
-  /** Construct viewcells (if not already loaded) */
-  bool
-  ConstructViewCells();
+  /** Construct viewcells from the scratch 
+  */
+  bool ConstructViewCells(const AxisAlignedBox3 &viewSpaceBox);
 
   /** Returns the specified sample strategy, NULL if no valid strategy.
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1449)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1486)
@@ -263,5 +263,5 @@
 
 
-static void AssociateObjectsWithLeaf(BvhLeaf *leaf)
+void BvHierarchy::AssociateObjectsWithLeaf(BvhLeaf *leaf)
 {
 	ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
@@ -272,4 +272,5 @@
 }
 
+
 static int CountRays(const ObjectContainer &objects)
 {
@@ -285,4 +286,5 @@
 	return nRays;
 }
+
 
 BvhInterior *BvHierarchy::SubdivideNode(const BvhSubdivisionCandidate &sc,
@@ -1535,6 +1537,6 @@
 {
 	// search nodes
-	std::map<BvhNode *, BvhIntersectable *>::
-		const_iterator it = mBvhIntersectables.find(node);
+	std::map<BvhNode *, BvhIntersectable *>::const_iterator it 
+		= mBvhIntersectables.find(node);
 
 	if (it != mBvhIntersectables.end()) 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h	(revision 1404)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VrmlExporter.h	(revision 1486)
@@ -26,4 +26,6 @@
 class Beam;
 class OspTree;
+
+
 
 class VrmlExporter : public Exporter
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1292)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SamplingPreprocessor.cpp	(revision 1486)
@@ -124,12 +124,5 @@
   if (!mLoadViewCells)
   {
-	mViewCellsManager->SetViewSpaceBox(mKdTree->GetBox());
-
-	// construct view cells using it's own set of samples
-	mViewCellsManager->Construct(this);
-	
-	//-- several visualizations and statistics
-	Debug << "view cells construction finished: " << endl;
-	mViewCellsManager->PrintStatistics(Debug);
+	  ConstructViewCells(mKdTree->GetBox());
   }
   
Index: GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorFactory.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorFactory.h	(revision 1486)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/PreprocessorFactory.h	(revision 1486)
@@ -0,0 +1,31 @@
+#ifndef _PreprocessorFactory_H__
+#define _PreprocessorFactory_H__
+
+#include <string>
+using namespace std;
+
+namespace GtpVisibilityPreprocessor {
+
+
+class Preprocessor;
+
+/** Factory for creating preprocessors
+*/
+class PreprocessorFactory
+{
+public:
+	/** Returns the preprocessor with the given name or NULL if such a preprocessor
+		does not exist.
+	*/
+	static Preprocessor *CreatePreprocessor(const string &preprocessorName);
+
+protected:
+	/** Hidden constructor. Class cannot be instantiated.
+	*/
+	PreprocessorFactory();
+  
+};
+
+}
+
+#endif
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.h	(revision 1281)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VssPreprocessor.h	(revision 1486)
@@ -26,5 +26,5 @@
   bool mUseImportanceSampling;
   bool mEnlargeViewSpace;
-  AxisAlignedBox3 *mViewSpaceBox;
+  AxisAlignedBox3 mViewSpaceBox;
 
   ofstream mStats;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1473)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 1486)
@@ -27,4 +27,7 @@
 #define SAMPLE_ORIGIN_OBJECTS 0
 
+
+
+
 namespace GtpVisibilityPreprocessor {
 
@@ -32,6 +35,5 @@
 // HACK
 const static bool SAMPLE_AFTER_SUBDIVISION = true;
-const static bool TEST_EMPTY_VIEW_CELLS = false;
-
+const static bool CLAMP_TO_BOX = false;
 
 template <typename T> class myless
@@ -223,5 +225,4 @@
 		CLEAR_CONTAINER(mViewCells);
 	}
-	//DEL_PTR(mViewCellsTree);
 }
 
@@ -1872,7 +1873,5 @@
 			if (viewcell->GetValid())
 			{
-				// if ray not outside of view space
-
-				// add new object to the pvs
+				// if view point is valid, add new object to the pvs
 				if (ray.mTerminationObject)
 				{
@@ -2737,6 +2736,6 @@
 	if (1) FinalizeViewCells(true);
 
-	// write view cells to disc (this moved to preprocessor)
-	if (0 && mExportViewCells)
+	// write view cells to disc
+	if (1 && mExportViewCells)
 	{
 		char filename[100];
@@ -3906,6 +3905,5 @@
 	SimulationStatistics ss;
 	dynamic_cast<RenderSimulator *>(mRenderer)->GetStatistics(ss);
- 
-	cout << ss << endl;
+ 	cout << ss << endl;
 	
 
@@ -3935,5 +3933,5 @@
 
 	// write view cells to disc
-	if (0 && mExportViewCells)
+	if (1 && mExportViewCells)
 	{
 		char filename[100];
@@ -4903,4 +4901,5 @@
 	cout << ss << endl;
 	
+
 	///////////////
 	//-- compression
@@ -4917,10 +4916,11 @@
 	}
 
-	///////////////
-	//-- compute final meshes and volume / area
+	/////////////
+	//-- some tasks still to do on the view cells:
+	//-- Compute meshes from view cell geometry, evaluate volume and / or area
 	if (1) FinalizeViewCells(true);
 
 	// write out view cells (this moved to preprocessor)
-	if (0 && mExportViewCells)
+	if (1 && mExportViewCells)
 	{
 		char filename[100];
@@ -5093,12 +5093,17 @@
 			AxisAlignedBox3 bbox = mHierarchyManager->GetViewSpaceBox();
 			bbox.Scale(Vector3(0.5, 1, 0.5));
-			
-			exporter->SetWireframe(); 
-			exporter->ExportBox(bbox);
-			exporter->SetFilled(); 
 				
+			if (CLAMP_TO_BOX)
+			{	
+				exporter->SetWireframe(); 
+				exporter->ExportBox(bbox);
+				exporter->SetFilled(); 
+			}
+				
+			///////////////////
+
 			if (0 && mExportGeometry)
 			{
-				exporter->ExportGeometry(objects, true, &bbox);
+				exporter->ExportGeometry(objects, true, CLAMP_TO_BOX ? &bbox : NULL);
 			}
 
@@ -5109,6 +5114,6 @@
 			}
 		
-			mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, &bbox, false);
-			ExportViewCellsForViz(exporter, &bbox, GetClipPlane());
+			mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, CLAMP_TO_BOX ? &bbox : NULL, false);
+			ExportViewCellsForViz(exporter, CLAMP_TO_BOX ? &bbox : NULL, GetClipPlane());
 
 			delete exporter;
@@ -5128,10 +5133,10 @@
 			const long starttime = GetTime();
 			
-			// matt: hack
+			// matt: hack for making visualization smaller in size
 			AxisAlignedBox3 bbox = mHierarchyManager->GetObjectSpaceBox();
 			bbox.Scale(Vector3(0.5, 1, 0.5));
 
 			cout << "exporting object space hierarchy ... ";
-			mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, &bbox);
+			mHierarchyManager->ExportObjectSpaceHierarchy(exporter, objects, CLAMP_TO_BOX ? &bbox : NULL);
 		
 			delete exporter;
@@ -5304,5 +5309,5 @@
 
 
-	//////////////////////////////////////////////////////////////
+	///////////////
 	//-- export bounding boxes
 	//-- The bounding boxes are used to identify 
@@ -5478,5 +5483,5 @@
 				// todo: maybe not correct for kd node pvs
 				if (viewcell->GetPvs().GetSampleContribution(ray.mTerminationObject,
-					ray.mPdf, contribution))
+															 ray.mPdf, contribution))
 				{
 					++ ray.mPvsContribution;
@@ -5541,4 +5546,5 @@
 											float &contribution) const
 {
+	// The hierarchy manager decides about the type of sample cast
 	return mHierarchyManager->AddSampleToPvs(obj, hitPoint, vc, pdf, contribution);
 }
@@ -5609,18 +5615,18 @@
 	
 	cout << "computing sample contributions of " << (int)evaluationSamples.size()  << " samples ... ";
-
+	
 	ComputeSampleContributions(evaluationSamples, true, false);
-
+	
 	cout << "finished" << endl;
 	
 	cout << "compute new statistics ... ";
-	Debug << "compute new statistics ... ";
-
+	
 	// propagate pvs or pvs size information
 	ObjectPvs pvs;
 	UpdatePvsForEvaluation(mViewCellsTree->GetRoot(), pvs);
 
-	///////////////////////////////////////
-	//-- temporary matt: test render cost
+
+	/////////////////////
+	// $§temporary matt: test render cost
 
 	sprintf(s, "-%09d-eval.log", castSamples);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1449)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1486)
@@ -650,4 +650,7 @@
 	void CreateRoot(const ObjectContainer &objects);
 
+	static void AssociateObjectsWithLeaf(BvhLeaf *leaf);
+
+
 	/////////////////////////////
 	// Helper functions for local cost heuristics
Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 1415)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h	(revision 1486)
@@ -35,71 +35,42 @@
   /** initialization to the non existing bounding box
   */
-  void Initialize() {
-    mMin = Vector3(MAXFLOAT);
-    mMax = Vector3(-MAXFLOAT);
-  }
+  void Initialize();
 
   /** The center of the box
   */
-  Vector3 Center() const { return 0.5 * (mMin + mMax); }
+  Vector3 Center() const;
   
   /** The diagonal of the box
   */
-  Vector3 Diagonal() const { return (mMax -mMin); }
-
-  float Center(const int axis) const {
-    return  0.5f * (mMin[axis] + mMax[axis]);
-  }
-
-  float Min(const int axis) const {
-    return mMin[axis];
-  }
-
-  float Max(const int axis) const {
-    return  mMax[axis];
-  }
-
-  float Size(const int axis) const {
-    return  Max(axis) - Min(axis);
-  }
+  Vector3 Diagonal() const;
+
+  float Center(const int axis) const;
+
+  float Min(const int axis) const;
+
+  float Max(const int axis) const;
+
+  float Size(const int axis) const;
 
   // Read-only const access tomMin and max vectors using references
-  const Vector3& Min() const { return mMin;}
-  const Vector3& Max() const { return mMax;}
-
-  void Enlarge (const Vector3 &v) {
-    mMax += v;
-    mMin -= v;
-  }
-
-  void SetMin(const Vector3 &v) {
-    mMin = v;
-  }
-
-  void SetMax(const Vector3 &v) {
-    mMax = v;
-  }
-
-  void SetMin(int axis, const float value) {
-   mMin[axis] = value;
-  }
-
-  void SetMax(int axis, const float value) {
-    mMax[axis] = value;
-  }
+  const Vector3& Min() const;
+  const Vector3& Max() const;
+
+  void Enlarge (const Vector3 &v);
+
+  void SetMin(const Vector3 &v);
+
+  void SetMax(const Vector3 &v);
+
+  void SetMin(int axis, const float value);
+
+  void SetMax(int axis, const float value);
 
   // Decrease box by given splitting plane
-  void Reduce(int axis, int right, float value) {
-    if ( (value >=mMin[axis]) && (value <= mMax[axis]) )
-      if (right)
-		mMin[axis] = value;
-      else
-        mMax[axis] = value;
-  }
-
+  void Reduce(int axis, int right, float value); 
   
 
   // the size of the box along all the axes
-  Vector3 Size() const { return mMax - mMin; }
+  Vector3 Size() const;
 
   // Return whether the box is unbounded.  Unbounded boxes appear
Index: GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp	(revision 1379)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/UnigraphicsParser.cpp	(revision 1486)
@@ -175,11 +175,11 @@
   }
   
-  if (currentMesh->mVertices.size()) {
-	  if (1)
-		RotateMesh(currentMesh);
+  if (currentMesh->mVertices.size()) 
+  {
+	  if (ROTATE_SCENE) RotateMesh(currentMesh);
  
-    currentMesh->Preprocess();
-    MeshInstance *mi = new MeshInstance(currentMesh);
-    root->mGeometry.push_back(mi);
+	  currentMesh->Preprocess();
+	  MeshInstance *mi = new MeshInstance(currentMesh);
+	  root->mGeometry.push_back(mi);
   }
   
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1457)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 1486)
@@ -18,17 +18,10 @@
 #include <crtdbg.h>
 
-#include "SamplingPreprocessor.h"
-#include "VssPreprocessor.h"
-#include "RssPreprocessor.h"
-#include "ExactPreprocessor.h"
+#include "PreprocessorFactory.h"
 #include "Parser.h"
-#include "UnigraphicsParser.h"
-#include "X3dParser.h"
 #include "Environment.h"
-#include "Camera.h"
 #include "MeshKdTree.h"
-#include "Exporter.h"
-#include "ViewCell.h"
-#include "SceneGraph.h"
+#include "Preprocessor.h"
+
 
 #include "PreprocessorThread.h"
@@ -37,5 +30,4 @@
 #endif
 
-#include "RenderSampler.h"
 #include "ResourceManager.h"
 #include "GlRenderer.h"
@@ -115,42 +107,9 @@
 	string preprocessorType(buff);
 
-	if (preprocessorType == "vss")
-	{
-		preprocessor = new VssPreprocessor();
-	}
-	else
-	{
-		if (preprocessorType == "rss")
-		{
-			preprocessor = new RssPreprocessor();
-		}
-		else
-		{
-			if (preprocessorType == "exact")
-			{
-				preprocessor = new ExactPreprocessor();
-			}
-			else
-			{
-				if (preprocessorType == "sampling")
-				{
-					preprocessor = new SamplingPreprocessor();
-				}
-				else
-				{	
-					if (preprocessorType == "render")
-					{
-						preprocessor = new RenderSampler();
-					}
-					else 
-					{
-						Environment::DelSingleton();
-						cerr<<"Unknown preprocessor type"<<endl;
-						Debug<<"Unknown preprocessor type"<<endl;
-						exit(1);
-					}
-				}
-			}
-		}
+	if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
+	{
+		Environment::DelSingleton();
+		cerr << "Unknown preprocessor type" << endl;
+		exit(1);
 	}
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/MeshKdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/MeshKdTree.h	(revision 1233)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/MeshKdTree.h	(revision 1486)
@@ -127,17 +127,12 @@
 public:
 
-  enum {SPLIT_OBJECT_MEDIAN,
-	SPLIT_SPATIAL_MEDIAN,
-	SPLIT_SAH};
+  enum {
+	  SPLIT_OBJECT_MEDIAN,
+	  SPLIT_SPATIAL_MEDIAN,
+	  SPLIT_SAH};
   
 
   MeshKdTree(Mesh *mesh);
-  ~MeshKdTree() {
-    if (mSubdivisionCandidates)
-      delete mSubdivisionCandidates;
-    
-    if (mRoot)
-      delete mRoot;
-  }
+  ~MeshKdTree();
   
   virtual bool Construct();
@@ -155,10 +150,9 @@
   virtual MeshKdNode *Subdivide(const TraversalData &tdata);
   
-  /** Get the root of the tree */
-  MeshKdNode *GetRoot() const {
-    return mRoot;
-  }
-  
-  AxisAlignedBox3 GetBox() const { return mMesh->mBox; }
+  /** Get the root of the tree 
+  */
+  MeshKdNode *GetRoot() const;
+  
+  AxisAlignedBox3 GetBox() const;
 
   int
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1473)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 1486)
@@ -4,4 +4,5 @@
 #include "VssRay.h"
 #include "ViewCellsManager.h"
+#include "Triangle3.h"
 
 
@@ -13,37 +14,116 @@
 GvsPreprocessor::GvsPreprocessor(): Preprocessor(), mSamplingType(0)
 {
-	Environment::GetSingleton()->GetIntValue("RenderSampler.samples", mSamples);
-	//Environment::GetSingleton()->GetIntValue("Render.samples", mTotalSamples);
-
-	cout << "number of render samples: " << mSamples << endl;
-}
-
-
-int GvsPreprocessor::HandleRay(VssRay &ray)
-{
-	int sampleContributions = 0;
-
-	if (mViewCellsManager->ComputeSampleContribution(ray, true, false))
-	{
-		return AdaptiveBorderSampling(ray);
+	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.totalSamples", mTotalSamples);
+	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.initialSamples", mInitialSamples);
+	Environment::GetSingleton()->GetIntValue("GvsPreprocessor.samplesPerPass", mSamplesPerPass);
+	Environment::GetSingleton()->GetFloatValue("GvsPreprocessor.epsilon", mEps);
+		
+
+	Debug << "Gvs preprocessor options" << endl;
+	Debug << "number of total samples: " << mTotalSamples << endl;
+	Debug << "number of initial samples: " << mInitialSamples << endl;
+	Debug << "number of samples per pass: " << mSamplesPerPass << endl;
+
+	mStats.open("gvspreprocessor.log");
+}
+
+
+const bool GvsPreprocessor::DiscontinuityFound(const VssRay &ray) const
+{
+	return false;
+}
+
+
+int GvsPreprocessor::HandleRay(const GvsRayInfo &gvsRay)
+{
+	VssRay *ray = gvsRay.mRay;
+
+	if (!mViewCellsManager->ComputeSampleContribution(*ray, true, false))
+		return 1;
+
+	if (gvsRay.mFoundDiscontinuity)
+	{
+		return ReverseSampling(*ray);
 	}
 	else
 	{
-		return ReverseSampling(ray);
-	}
-}
-
-
-int GvsPreprocessor::AdaptiveBorderSampling(VssRay &ray)
-{
+		return AdaptiveBorderSampling(*ray);
+	}
+}
+
+
+void GvsPreprocessor::CreateNewSamples(VertexContainer &samples, 
+									   const VssRay &ray, 
+									   const Triangle3 &hitTriangle, 
+									   const int index,
+									   const float eps)
+{
+	const int indexU = (index + 1) % 3;
+	const int indexL = (index == 0) ? 2 : index - 1;
+
+	const Vector3 a = hitTriangle.mVertices[index] - ray.GetDir();
+	const Vector3 b = hitTriangle.mVertices[indexU] - hitTriangle.mVertices[index];
+	const Vector3 c = hitTriangle.mVertices[index] - hitTriangle.mVertices[indexL];
+	const float len = Magnitude(a);
+
+	// compute the new three hit points
+	// pi, i + 1
+	const Vector3 dir1 = CrossProd(a, b); //N((pi-xp)×(pi+1- pi));
+	const Vector3 pt1 = hitTriangle.mVertices[index] + eps * len * dir1; //pi+ e·|pi-xp|·di, j
+	samples.push_back(pt1);
+
+	// pi, i - 1
+    const Vector3 dir2 = CrossProd(a, c); // N((pi-xp)×(pi- pi-1))
+	const Vector3 pt2 = hitTriangle.mVertices[index] + eps * len * dir2; //pi+ e·|pi-xp|·di, j
+	samples.push_back(pt2);
+
+	// pi, i
+	const Vector3 dir3 = DotProd(dir1, dir2) > 0 ? 
+		Normalize(dir2 + dir1) : Normalize(CrossProd(a, dir2) + CrossProd(dir1, a)); // N((pi-xp)×di,i-1+di,i+1×(pi-xp))
+	const Vector3 pt3 = hitTriangle.mVertices[index] + eps * len * dir3; //pi+ e·|pi-xp|·di, j
+	samples.push_back(pt3);
+}
+
+
+int GvsPreprocessor::AdaptiveBorderSampling(const VssRay &ray)
+{
+	cout << "a";
+	//Intersectable *tObj = ray->GetTerminationObject;
+	Triangle3 hitTriangle;
+	vector<Vector3> samples;
+	samples.reserve(9);
+
+	CreateNewSamples(samples, ray, hitTriangle, 0, mEps);
+	CreateNewSamples(samples, ray, hitTriangle, 1, mEps);
+	CreateNewSamples(samples, ray, hitTriangle, 2, mEps);
+
+	VertexContainer::const_iterator vit, vit_end = samples.end();
+	
+	for (vit = samples.begin(); vit != vit_end; ++ vit)
+	{
+		VssRay *ray;// = CreateRay(*vit);
+
+		if (DiscontinuityFound(*ray))
+		{
+			// schedule for reverse sampling
+			mRayQueue.push(GvsRayInfo(ray, true));
+		}
+		else
+		{
+			// schedule for adaptive border sampling
+			mRayQueue.push(GvsRayInfo(ray, false));
+		}
+	}
+
 	// TODO
-	return 0;
-}
-
-
-int GvsPreprocessor::ReverseSampling(VssRay &ray)
-{
+	return 1;
+}
+
+
+int GvsPreprocessor::ReverseSampling(const VssRay &ray)
+{
+	cout << "r" << endl;
 	// TODO
-	return 0;
+	return 1;
 }
 
@@ -58,13 +138,12 @@
 	Debug << "generated " <<  samplesPerPass << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
 
+	// cast the rays and optain hitpoints with geometry
 	VssRayContainer samples;
-
-	// cast the rays and optain hitpoints with geometry
 	CastRays(simpleRays, samples);
+
 	Debug << "cast " <<  samplesPerPass << " samples in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
 
-	// add to ray queue
+	// add samples to ray queue
 	VssRayContainer::const_iterator vit, vit_end = samples.end();
-
 	for (vit = samples.begin(); vit != vit_end; ++ vit)
 	{
@@ -76,15 +155,18 @@
 
 
-int GvsPreprocessor::Pass(const int passSamples)
+int GvsPreprocessor::Pass()
 {
 	int castSamples = 0;
-	const int uniformSamples = 1000;
-
-	while (castSamples < passSamples) 
+	
+	while (castSamples < mSamplesPerPass) 
 	{
 		// Ray queue empty => 
 		// cast a number of uniform samples to fill ray Queue
-		CastAvsSamples(uniformSamples, mSamplingType, mRayQueue);
-		castSamples += Pass(passSamples);
+		CastAvsSamples(mInitialSamples, mSamplingType, mRayQueue);
+		
+		const int gvsSamples = RunSampling();
+		castSamples += gvsSamples;
+		//cout << "\ngvs samples: " << gvsSamples << endl;
+		//cout << "cast " << castSamples << " of " << mSamplesPerPass << endl;
 	}
 
@@ -116,18 +198,22 @@
 	const long startTime = GetTime();
 	
-	cout<<"Gvs Preprocessor started\n"<<flush;
-	
-	/// construct the view cells first
-	ConstructViewCells();
-
-	int samples = 0;
-
-	while (samples < mTotalSamples) 
-	{
-		samples += Pass(mSamplesPerPass);
-		
-		cout<<"+";
-		
-		// mVssRays.PrintStatistics(mStats);
+	mViewSpaceBox = mKdTree->GetBox();
+	cout << "Gvs Preprocessor started\n" << flush;
+	
+	if (!mLoadViewCells)
+	{	/// construct the view cells from the scratch
+		ConstructViewCells(mViewSpaceBox);
+		cout << "view cells loaded" << endl;
+	}
+
+	int castSamples = 0;
+
+	while (castSamples < mTotalSamples) 
+	{
+		const int passSamples = Pass();
+		castSamples += passSamples;
+		cout << "+";
+		cout << "\nsamples cast " << passSamples << " (=" << castSamples << " of " << mTotalSamples << ")" << endl;
+		//mVssRays.PrintStatistics(mStats);
 		//mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3 << endl
 		//	   << "#TotalSamples\n" << samples << endl;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1315)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp	(revision 1486)
@@ -91,13 +91,11 @@
 
 		node->Mail();
-
-		return (int)(((BvhLeaf *)node)->mObjects.size());
+		BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(node);
+		return (int)leaf->mObjects.size();
 	}			 
 
 	// compute leaf pvs
 	int pvs = 0;
-
 	stack<BvhNode *> tStack;
-
 	tStack.push(bvhobj->GetItem());
 
@@ -146,5 +144,5 @@
 	{
 		Intersectable *obj = (*it).first;
-
+	
 		switch (obj->Type())
 		{
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1473)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1486)
@@ -329,5 +329,5 @@
 												   const ObjectContainer &objects)
 {
-	cout << "starting view space hierarchy construction ... " << endl;
+	cout << "\nstarting view space hierarchy construction ... " << endl;
 
 	RayInfoContainer *viewSpaceRays = new RayInfoContainer();
@@ -604,5 +604,5 @@
 {
 	Debug << "old bv hierarchy:\n " << mBvHierarchy->mBvhStats << endl;
-	cout << "resetting bv hierarchy" << endl;
+	cout << "\nresetting bv hierarchy" << endl;
 	mHierarchyStats.nodes -= mBvHierarchy->mBvhStats.nodes;
 	mHierarchyStats.mGlobalCostMisses = 0; // hack: reset global cost misses
@@ -856,6 +856,8 @@
 	{
 	case NO_OBJ_SUBDIV:
-		// potentially visible objects
-		return vc->AddPvsSample(obj, pdf, contribution);
+		{
+			// potentially visible objects
+			return vc->AddPvsSample(obj, pdf, contribution);
+		}
 	case KD_BASED_OBJ_SUBDIV:
 		{
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1421)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp	(revision 1486)
@@ -919,5 +919,4 @@
 	{
 		Debug << "creating view cells manager: VspBsp" << endl;
-
 		mViewCellsManager = new VspBspViewCellsManager(mViewCellsTree, mVspBspTree);
 	}
@@ -925,5 +924,4 @@
 	{
 		Debug << "creating view cells manager: VspOsp" << endl;
-		// hack
 		mViewCellsManager = new VspOspViewCellsManager(mViewCellsTree, mHierarchyManager);
 	}
@@ -998,5 +996,4 @@
 
 		//cout << "\nsearching view cell with id " << viewCellId << endl;
-
 		ViewCellContainer::iterator vit =
 			lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
@@ -1183,4 +1180,5 @@
 
 	leaf->mObjects = objects;
+	BvHierarchy::AssociateObjectsWithLeaf(leaf);	
 }
 
@@ -1190,5 +1188,4 @@
 {
 	vector<int> objIndices;
-
 	char *endptr;
 			
@@ -1221,5 +1218,5 @@
 		if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
 		{
-			objects.push_back(*oit);	
+			objects.push_back(*oit);
 		}
 		else
Index: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp	(revision 1415)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.cpp	(revision 1486)
@@ -2165,3 +2165,98 @@
 
 
-}
+void AxisAlignedBox3::Initialize() {
+    mMin = Vector3(MAXFLOAT);
+    mMax = Vector3(-MAXFLOAT);
+  }
+
+
+Vector3 AxisAlignedBox3::Center() const 
+{ 
+	return 0.5 * (mMin + mMax); 
+}
+  
+Vector3 AxisAlignedBox3::Diagonal() const 
+{ 
+	return (mMax -mMin); 
+}
+
+
+float AxisAlignedBox3::Center(const int axis) const 
+{
+	return  0.5f * (mMin[axis] + mMax[axis]);
+}
+
+
+float AxisAlignedBox3::Min(const int axis) const 
+{
+	return mMin[axis];
+}
+
+float AxisAlignedBox3::Max(const int axis) const {
+	return  mMax[axis];
+}
+
+float AxisAlignedBox3::Size(const int axis) const {
+	return  Max(axis) - Min(axis);
+}
+
+// Read-only const access tomMin and max vectors using references
+const Vector3& AxisAlignedBox3::Min() const 
+{ 
+	return mMin;
+}
+
+
+const Vector3& AxisAlignedBox3::Max() const 
+{ 
+	return mMax;
+}
+
+
+void AxisAlignedBox3::Enlarge (const Vector3 &v) 
+{
+	mMax += v;
+	mMin -= v;
+}
+
+
+void AxisAlignedBox3::SetMin(const Vector3 &v) 
+{
+	mMin = v;
+}
+
+
+void AxisAlignedBox3::SetMax(const Vector3 &v) 
+{
+	mMax = v;
+}
+
+
+void AxisAlignedBox3::SetMin(int axis, const float value) 
+{
+	mMin[axis] = value;
+}
+
+
+void AxisAlignedBox3::SetMax(int axis, const float value) 
+{
+	mMax[axis] = value;
+}
+
+// Decrease box by given splitting plane
+void AxisAlignedBox3::Reduce(int axis, int right, float value) 
+{
+	if ( (value >=mMin[axis]) && (value <= mMax[axis]) )
+		if (right)
+			mMin[axis] = value;
+		else
+			mMax[axis] = value;
+}
+
+
+Vector3 AxisAlignedBox3::Size() const 
+{ 
+	return mMax - mMin; 
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/MeshKdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/MeshKdTree.cpp	(revision 1233)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/MeshKdTree.cpp	(revision 1486)
@@ -463,6 +463,28 @@
   
   return result;
-
-}
-
-}
+}
+
+
+AxisAlignedBox3 MeshKdTree::GetBox() const 
+{ 
+	return mMesh->mBox; 
+}
+
+
+MeshKdTree::~MeshKdTree() 
+{
+    if (mSubdivisionCandidates)
+		delete mSubdivisionCandidates;
+
+	if (mRoot)
+		delete mRoot;
+}
+  
+
+MeshKdNode *MeshKdTree::GetRoot() const 
+{
+	return mRoot;
+}
+
+
+}
