Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 421)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 422)
@@ -68,6 +68,6 @@
 ViewCells {
 	#hierarchy kdTree
-	#hierarchy vspTree
-	hierarchy bspTree
+	hierarchy vspTree
+	#hierarchy bspTree
 	# hierarchy sceneDependent
 	
@@ -91,5 +91,5 @@
 BspTree {
 	Construction {
-		input fromRays
+		input fromSamples
 	#	input fromViewCells
 	#	input fromSceneGeometry
@@ -219,6 +219,5 @@
 
 	splitType regular
-
-	numberOfEndPointDomains	10000
+#	splitType heuristics
 	ct_div_ci	0.0
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 421)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 422)
@@ -243,5 +243,6 @@
 
 // Constructor
-VspKdTree::VspKdTree()
+VspKdTree::VspKdTree():
+mOnlyDrivingAxis(true)
 {	  
 	environment->GetIntValue("VspKdTree.Termination.maxDepth", mTermMaxDepth);
@@ -250,19 +251,16 @@
 	environment->GetFloatValue("VspKdTree.Termination.maxRayContribution", mTermMaxRayContribution);
 	environment->GetFloatValue("VspKdTree.Termination.maxCostRatio", mTermMaxCostRatio);
-
 	environment->GetFloatValue("VspKdTree.Termination.minSize", mTermMinSize);
+
 	mTermMinSize = sqr(mTermMinSize);
 
-	environment->GetFloatValue("VspKdTree.epsilon", epsilon);
-	environment->GetFloatValue("VspKdTree.ct_div_ci", ct_div_ci);
-	
-	environment->GetFloatValue("VspKdTree.maxTotalMemory", maxTotalMemory);
-	environment->GetFloatValue("VspKdTree.maxStaticMemory", maxStaticMemory);
-  
-  
-	environment->GetIntValue("VspKdTree.accessTimeThreshold", accessTimeThreshold);
-	//= 1000;
-	environment->GetIntValue("VspKdTree.minCollapseDepth", minCollapseDepth);
- 
+	environment->GetFloatValue("VspKdTree.epsilon", mEpsilon);
+	environment->GetFloatValue("VspKdTree.ct_div_ci", mCtDivCi);
+	
+	environment->GetFloatValue("VspKdTree.maxTotalMemory", mMaxTotalMemory);
+	environment->GetFloatValue("VspKdTree.maxStaticMemory", mMaxStaticMemory);
+    
+	environment->GetIntValue("VspKdTree.accessTimeThreshold", mAccessTimeThreshold);
+	environment->GetIntValue("VspKdTree.minCollapseDepth", mMinCollapseDepth);
 
 	// split type
@@ -284,7 +282,15 @@
 	}
 
+	Debug << "======= vsp kd tree options ========" << endl;
+	Debug << "max depth: "<< mTermMaxDepth << endl;
+	Debug << "min pvs: "<< mTermMinPvs << endl;
+	Debug << "min rays: "<< mTermMinRays << endl;
+	Debug << "max ray contribution: "<< mTermMaxRayContribution << endl;
+	Debug << "max cost ratio: "<< mTermMaxCostRatio << endl;
+	Debug << "min size: "<<mTermMinSize << endl;
+
 	mRoot = NULL;
 
-	splitCandidates = new vector<SortableEntry>;
+	mSplitCandidates = new vector<SortableEntry>;
 }
 
@@ -403,5 +409,5 @@
 	mStat.Start();
   
-	maxMemory = maxStaticMemory;
+	mMaxMemory = mMaxStaticMemory;
 
 	DEL_PTR(mRoot);
@@ -411,5 +417,4 @@
 	// first construct a leaf that will get subdivide
 	VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(mRoot);
-
 	
 	mStat.nodes = 1;
@@ -428,5 +433,5 @@
 		mBox = *forcedBoundingBox;
 
-	cout << "Bbox = " << mBox << endl;
+	Debug << "bbox = " << mBox << endl;
 	
 	mStat.rays = (int)leaf->mRays.size();
@@ -438,9 +443,9 @@
 	mRoot = Subdivide(TraversalData(leaf, mBox, 0));
 
-	if (splitCandidates) 
-	{
-		// force realease of this vector
-		delete splitCandidates;
-		splitCandidates = new vector<SortableEntry>;
+	if (mSplitCandidates) 
+	{
+		// force release of this vector
+		delete mSplitCandidates;
+		mSplitCandidates = new vector<SortableEntry>;
 	}
   
@@ -476,5 +481,5 @@
 		lastMem = (int)mem;
 		
-		if (  mem > maxMemory ) 
+		if (mem > mMaxMemory) 
 		{
 			// count statistics on unprocessed leafs
@@ -528,5 +533,6 @@
 	float costRatio;
 	
-	if (splitType == ESplitRegular) {
+	if (splitType == ESplitRegular) 
+	{
 		costRatio = BestCostRatioRegular(leaf,
 										 axis,
@@ -556,14 +562,14 @@
 	if (costRatio > mTermMaxCostRatio) 
 	{
-		//		cout<<"Too big cost ratio "<<costRatio<<endl;
+		cout<<"Too big cost ratio " << costRatio << endl;
 		return -1;
 	}
 
-#if 0	
-	cout<<
-		"pvs="<<leaf->mPvsSize<<
-		" rays="<<leaf->mRays.size()<<
-		" rc="<<leaf->GetAvgRayContribution()<<
-		" axis="<<axis<<endl;
+#if 1	
+	cout <<
+		"pvs=" << leaf->mPvsSize <<
+		" rays=" << (int)leaf->mRays.size() <<
+		" rc=" << leaf->GetAvgRayContribution() <<
+		" axis=" << axis << endl;
 #endif
 	
@@ -624,7 +630,7 @@
 		
 	//		float sum = raysBack*(position - minBox) + raysFront*(maxBox - position);
-	float sum = pvsBack*(position - minBox) + pvsFront*(maxBox - position);
-
-	newCost = ct_div_ci + sum/sizeBox;
+	float sum = pvsBack*(position - minBox) + pvsFront * (maxBox - position);
+
+	newCost = mCtDivCi + sum  /sizeBox;
 	
 	//	cout<<axis<<" "<<pvsSize<<" "<<pvsBack<<" "<<pvsFront<<endl;
@@ -637,17 +643,17 @@
 }
 
-float
-VspKdTree::BestCostRatioRegular(VspKdTreeLeaf *leaf,
-								int &axis,
-								float &position,
-								int &raysBack,
-								int &raysFront,
-								int &pvsBack,
-								int &pvsFront)
-{
-	int nRaysBack[6], nRaysFront[6];
-	int nPvsBack[6], nPvsFront[6];
-	float nPosition[6];
-	float nCostRatio[6];
+float VspKdTree::BestCostRatioRegular(VspKdTreeLeaf *leaf,
+									  int &axis,
+									  float &position,
+									  int &raysBack,
+									  int &raysFront,
+									  int &pvsBack,
+									  int &pvsFront)
+{
+	int nRaysBack[3], nRaysFront[3];
+	int nPvsBack[3], nPvsFront[3];
+
+	float nPosition[3];
+	float nCostRatio[3];
 	int bestAxis = -1;
 	
@@ -656,10 +662,8 @@
 	// int sAxis = box.Size().DrivingAxis();
 	int sAxis = sBox.Size().DrivingAxis();
-	
-	bool onlyDrivingAxis = true; 
-
-	for (axis = 0; axis < 6; ++ axis) 
-	{
-		if (!onlyDrivingAxis || axis == sAxis) 
+
+	for (axis = 0; axis < 3; ++ axis) 
+	{
+		if (!mOnlyDrivingAxis || axis == sAxis) 
 		{
 			nPosition[axis] = (sBox.Min()[axis] + sBox.Max()[axis])*0.5f;
@@ -746,6 +750,6 @@
 	Intersectable::NewMail();
 	
-	for (vector<SortableEntry>::const_iterator ci = splitCandidates->begin();
-		ci < splitCandidates->end(); ++ ci) 
+	for (vector<SortableEntry>::const_iterator ci = mSplitCandidates->begin();
+		ci < mSplitCandidates->end(); ++ ci) 
 	{
 		VssRay *ray;
@@ -805,10 +809,9 @@
 
 	float oldCost = (float)leaf->GetPvsSize();
-	float newCost = ct_div_ci + minSum / sizeBox;
+	float newCost = mCtDivCi + minSum / sizeBox;
 	float ratio = newCost / oldCost;
   
-	//  cout<<"===================="<<endl;
-	//  cout<<"costRatio="<<ratio<<" pos="<<position<<" t="<<(position - minBox)/(maxBox - minBox)
-	//      <<"\t q=("<<queriesBack<<","<<queriesFront<<")\t r=("<<raysBack<<","<<raysFront<<")"<<endl;
+	//Debug << "costRatio=" << ratio << " pos=" << position << " t=" << (position - minBox) / (maxBox - minBox)
+	//     <<"\t q=(" << queriesBack << "," << queriesFront << ")\t r=(" << raysBack << "," << raysFront << ")" << endl;
 	
 	return ratio;
@@ -818,16 +821,16 @@
 									const int axis)
 {
-	splitCandidates->clear();
+	mSplitCandidates->clear();
   
 	int requestedSize = 2 * (int)(node->mRays.size());
 	// creates a sorted split candidates array
-	if (splitCandidates->capacity() > 500000 &&
-		requestedSize < (int)(splitCandidates->capacity()/10) ) 
-	{
-    	delete splitCandidates;
-   		splitCandidates = new vector<SortableEntry>;
-	}
-  
-	splitCandidates->reserve(requestedSize);
+	if (mSplitCandidates->capacity() > 500000 &&
+		requestedSize < (int)(mSplitCandidates->capacity()/10) ) 
+	{
+    	delete mSplitCandidates;
+   		mSplitCandidates = new vector<SortableEntry>;
+	}
+  
+	mSplitCandidates->reserve(requestedSize);
 
 	// insert all queries 
@@ -836,12 +839,12 @@
 	{
 		bool positive = (*ri).mRay->HasPosDir(axis);
-		splitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax, 
-												 (*ri).ExtrapOrigin(axis), (void *)&*ri));
+		mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax, 
+												  (*ri).ExtrapOrigin(axis), (void *)&*ri));
 		
-		splitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,
-												 (*ri).ExtrapTermination(axis), (void *)&*ri));
-	}
-	
-	stable_sort(splitCandidates->begin(), splitCandidates->end());
+		mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,
+												  (*ri).ExtrapTermination(axis), (void *)&*ri));
+	}
+	
+	stable_sort(mSplitCandidates->begin(), mSplitCandidates->end());
 }
 
@@ -891,8 +894,9 @@
 	{
 
-#if 0
-		if (leaf->mDepth >= termMaxDepth) {
-			cout<<"Warning: max depth reached depth="<<(int)leaf->mDepth<<" rays="<<leaf->mRays.size()<<endl;
-			cout<<"Bbox: "<<GetBBox(leaf)<<" dirbbox:"<<GetDirBBox(leaf)<<endl;
+#if 1
+		if (leaf->mDepth >= mTermMaxDepth) 
+		{
+			Debug << "Warning: max depth reached depth="<<(int)leaf->mDepth<<" rays=" << (int)leaf->mRays.size() << endl;
+			Debug << "Bbox: " << GetBBox(leaf) << endl;
 		}
 #endif
@@ -1031,5 +1035,5 @@
 
 	// find a node in the tree which subtree will be collapsed
-	int maxAccessTime = time - accessTimeThreshold;
+	int maxAccessTime = time - mAccessTimeThreshold;
 	int released = 0;
 
@@ -1045,5 +1049,5 @@
 			VspKdTreeInterior *in = dynamic_cast<VspKdTreeInterior *>(node);
 			// cout<<"depth="<<(int)in->depth<<" time="<<in->lastAccessTime<<endl;
-			if (in->mDepth >= minCollapseDepth && in->mLastAccessTime <= maxAccessTime) 
+			if (in->mDepth >= mMinCollapseDepth && in->mLastAccessTime <= maxAccessTime) 
 			{
 				released = CollapseSubtree(node, time);
@@ -1092,5 +1096,5 @@
 	{
     	// memory check and realese...
-		if (GetMemUsage() > maxTotalMemory) 
+		if (GetMemUsage() > mMaxTotalMemory) 
 			ReleaseMemory(pass);
 		
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 421)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 422)
@@ -447,5 +447,5 @@
 
 	/// bsp tree construction types
-	enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_RAYS};
+	enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES};
 
 	/** Returns statistics.
@@ -501,4 +501,6 @@
 	/// BSP tree construction method
 	static int sConstructionMethod;
+
+
 protected:
 
@@ -803,5 +805,5 @@
 
 	/// axis aligned split criteria
-	float mCt_div_ci;
+	float mCtDivCi;
 	float mSplitBorder;
 	float mMaxCostRatio;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 421)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 422)
@@ -300,5 +300,5 @@
 
 	//-- termination criteria for axis aligned split
-	environment->GetFloatValue("BspTree.Termination.AxisAligned.ct_div_ci", mCt_div_ci);
+	environment->GetFloatValue("BspTree.Termination.AxisAligned.ct_div_ci", mCtDivCi);
 	environment->GetFloatValue("BspTree.Termination.AxisAligned.maxCostRatio", mMaxCostRatio);
 	environment->GetIntValue("BspTree.Termination.AxisAligned.maxPolys", 
@@ -370,6 +370,6 @@
 	else if (strcmp(constructionMethodStr, "fromSceneGeometry") == 0)
 		sConstructionMethod = FROM_SCENE_GEOMETRY;
-	else if (strcmp(constructionMethodStr, "fromRays") == 0)
-		sConstructionMethod = FROM_RAYS;
+	else if (strcmp(constructionMethodStr, "fromSamples") == 0)
+		sConstructionMethod = FROM_SAMPLES;
 	else 
 	{
@@ -826,6 +826,5 @@
 						 tBackData,
 						 coincident,
-						 interior->mPlane);
-						 
+						 interior->mPlane);			 
 	}
 
@@ -1036,5 +1035,5 @@
   
 	float oldCost = (float)polys.size();
-	float newCost = mCt_div_ci + minSum / boxArea;
+	float newCost = mCtDivCi + minSum / boxArea;
 	float ratio = newCost / oldCost;
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 419)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 422)
@@ -11,4 +11,5 @@
 mKdTree(NULL),
 mBspTree(NULL),
+mVspKdTree(NULL),
 mRenderSimulator(NULL)
 {
@@ -18,10 +19,10 @@
 Preprocessor::~Preprocessor()
 {
-	DeleteViewCells();
-		
 	DEL_PTR(mBspTree);
 	DEL_PTR(mKdTree);
 
 	DEL_PTR(mRenderSimulator);
+
+	DeleteViewCells();
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 411)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssRay.h	(revision 422)
@@ -61,13 +61,22 @@
   }
 	
-	VssRay(const Ray &ray)
-		:mOrigin(ray.GetLoc()), 
-		mTermination(ray.Extrap(ray.intersections[0].mT)),
+	VssRay(const Ray &ray):
+		mOrigin(ray.GetLoc()), 
 		mOriginObject(ray.sourceObject.mObject),
-		mTerminationObject(ray.intersections[0].mObject),
 		mRefCount(0),
 		mFlags(0),
 		mMailbox(-1)
-	{}
+	{
+		if (!ray.intersections.empty())
+		{
+			mTermination = ray.intersections[0].mT;
+			mTerminationObject = ray.intersections[0].mObject;
+		}
+		else
+		{
+			mTermination = Limits::Infinity;
+			mTerminationObject = NULL;
+		}
+	}
 
   void Precompute() {
Index: trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 421)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 422)
@@ -23,6 +23,5 @@
   environment->Parse(argc, argv, USE_EXE_PATH);
   MeshKdTree::ParseEnvironment();
-  BspTree::ParseEnvironment();
- 
+   
   char buff[128];
   
@@ -58,23 +57,27 @@
   p->ParseViewCellsOptions();
 
-  // construct tree immediately for "from view cells" or "from scene geometry"
-  // construction method. For "from rays" construction, wait until there is
-  // a certain number of rays collected
-  if (ViewCell::sHierarchy == ViewCell::BSP &&
-	  !(BspTree::sConstructionMethod == BspTree::FROM_RAYS)) 
+  if (ViewCell::sHierarchy == ViewCell::BSP)
   {
-	  if (BspTree::sConstructionMethod == BspTree::FROM_INPUT_VIEW_CELLS)
-	  {
-		  // view cells input file name
-		  environment->GetStringValue("ViewCells.filename", buff);
-		  string vcFilename(buff);
-		  p->LoadViewCells(vcFilename);
-		  Debug << (int)p->mViewCells.size() << " view cells loaded" << endl;
+	  BspTree::ParseEnvironment();
+
+	  // We construct BSP tree immediately for "from view cells" or "from scene geometry"
+	  // construction method. 
+	  // For the "from samples" option, construction is delayed until enough samples were collected
+	  if (BspTree::sConstructionMethod != BspTree::FROM_SAMPLES)
+	  {		
+		  if (BspTree::sConstructionMethod == BspTree::FROM_INPUT_VIEW_CELLS)
+		  {
+			  //-- load view cells from file
+			  environment->GetStringValue("ViewCells.filename", buff);
+			  string vcFilename(buff);
+			  p->LoadViewCells(vcFilename);
+			  Debug << (int)p->mViewCells.size() << " view cells loaded" << endl;
+		  }
+		  
+		  p->BuildBspTree();
+	
+		  p->Export("vc_bsptree.x3d", false, false, true);
+		  p->BspTreeStatistics(Debug);
 	  }
-	
-	  p->BuildBspTree();
-	
-	  p->Export("vc_bsptree.x3d", false, false, true);
-	  p->BspTreeStatistics(Debug);
   }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 419)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 422)
@@ -587,50 +587,48 @@
 	 /*if (mExportRayDensity) {	 
 	 return ExportKdTreeRayDensity(tree); }*/
-     
-	 stack<VspKdTreeNode *> tStack;
-
-	 tStack.push(tree.GetRoot());
-
-	 Mesh *mesh = new Mesh;
-  
-	 while (!tStack.empty()) 
-	 {
-		 VspKdTreeNode *node = tStack.top();
+
+	stack<VspKdTreeNode *> tStack;
+
+	tStack.push(tree.GetRoot());
+
+	Mesh *mesh = new Mesh;
+  
+	while (!tStack.empty()) 
+	{
+		VspKdTreeNode *node = tStack.top();
     
-		 tStack.pop();
-		 AxisAlignedBox3 box = tree.GetBBox(node);
-
-		 // add 6 vertices of the box
-
-		 int index = (int)mesh->mVertices.size();
+		tStack.pop();
+		AxisAlignedBox3 box = tree.GetBBox(node);
+
+		// add 6 vertices of the box
+		int index = (int)mesh->mVertices.size();
    
-		 for (int i=0; i < 8; ++ i) 
-		 {
-			 Vector3 v;
-			 box.GetVertex(i, v);
-			 mesh->mVertices.push_back(v);
-		 }
-
-		 mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
-		 mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
-		 mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
-
-   
-		 mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
-		 mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
-		 mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
-
-		 if (!node->IsLeaf()) 
-		 {
-			 VspKdTreeInterior *interior = dynamic_cast<VspKdTreeInterior *>(node);
-			 tStack.push(interior->GetFront());
-			 tStack.push(interior->GetBack());
-		 }
-	 }
-  
-	 ExportMesh(mesh);
-	 DEL_PTR(mesh);
-
-	 return true;
+		for (int i=0; i < 8; ++ i) 
+		{
+			Vector3 v;
+			box.GetVertex(i, v);
+			mesh->mVertices.push_back(v);
+		}
+
+		mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
+		mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
+		mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
+
+		mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
+		mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
+		mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
+
+        if (!node->IsLeaf()) 
+		{
+			VspKdTreeInterior *interior = dynamic_cast<VspKdTreeInterior *>(node);
+			tStack.push(interior->GetFront());
+			tStack.push(interior->GetBack());
+		}
+	}
+  
+	ExportMesh(mesh);
+	DEL_PTR(mesh);
+
+	return true;
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 421)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 422)
@@ -258,10 +258,4 @@
 	void UpdatePvsSize();
 
-	void Mail();
-
-	bool Mailed() const;
-  
-	bool Mailed(const int mail);
-
 	/** Returns average contribution of a ray to the PVS
 	*/
@@ -270,4 +264,12 @@
 	*/
 	inline float GetSqrRayContribution() const;
+
+	//-- mailing options
+
+	void Mail();
+
+	bool Mailed() const;
+  
+	bool Mailed(const int mail);
 
 	static void NewMail();
@@ -520,5 +522,5 @@
 
 	// total number of nodes of the tree
-	int nodes;
+	int mNumNodes;
 	
 	// axis aligned bounding box of the scene
@@ -526,8 +528,8 @@
 
 	// epsilon used for the construction
-	float epsilon;
+	float mEpsilon;
 
 	// ratio between traversal and intersection costs
-	float ct_div_ci;
+	float mCtDivCi;
 	
 	// type of the splitting to use for the tree construction
@@ -536,22 +538,21 @@
 	
 	// maximum alovable memory in MB
-	float maxTotalMemory;
+	float mMaxTotalMemory;
 
 	// maximum alovable memory for static kd tree in MB
-	float maxStaticMemory;
+	float mMaxStaticMemory;
 
 	// this is used during the construction depending
 	// on the type of the tree and queries...
-	float maxMemory;
+	float mMaxMemory;
 
 	// minimal acess time for collapse
-	int accessTimeThreshold;
+	int mAccessTimeThreshold;
 
 	// minimal depth at which to perform collapse
-	int minCollapseDepth;
+	int mMinCollapseDepth;
 	
 	// reusable array of split candidates
-	vector<SortableEntry> *splitCandidates;
-
+	vector<SortableEntry> *mSplitCandidates;
 	
 	/////////////////////////////
@@ -577,4 +578,5 @@
 
 
+	bool mOnlyDrivingAxis;
 	/////////////////////////////
 	VspKdStatistics mStat;	
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 421)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 422)
@@ -73,5 +73,5 @@
 		mBspTree->Construct(objects);
 		break;
-	case BspTree::FROM_RAYS:
+	case BspTree::FROM_SAMPLES:
 		DeleteViewCells(); // we generate new view cells
 		mBspTree->SetGenerateViewCells(true);
@@ -490,5 +490,4 @@
 											passSampleContributions);
 
-						//mVspKdTree->Construct(mSampleRays, mBoundingBox);
 					}
 					else if (ViewCell::sHierarchy == ViewCell::VSP)
@@ -555,12 +554,17 @@
   
 	//-- render simulation
-	cout << "\nevaluating bsp view cells render time before merge ... ";
-	GetRenderSimulator()->SimulateRendering();
-	
-	cout << " finished" << endl;
-
-	cout << GetRenderSimulator()->mStat << endl;
-	Debug << GetRenderSimulator()->mStat << endl;
-
+	RenderSimulator *rs = GetRenderSimulator();
+
+	if (rs)
+	{
+		cout << "\nevaluating bsp view cells render time before merge ... ";
+
+		rs->SimulateRendering();
+	
+		cout << " finished" << endl;
+
+		cout << GetRenderSimulator()->mStat << endl;
+		Debug << GetRenderSimulator()->mStat << endl;
+	}
 
 	if (mBspTree)
@@ -618,11 +622,18 @@
 
 		//-- render simulation
-		cout << "\nevaluating render time after merge ... ";
-			
-		GetRenderSimulator()->SimulateRendering();
-		cout << " finished" << endl;
-
-		cout << GetRenderSimulator()->mStat << endl;
-		Debug << GetRenderSimulator()->mStat << endl;
+		
+		RenderSimulator *rs = GetRenderSimulator();
+
+		if (rs)
+		{
+			cout << "\nevaluating render time after merge ... ";
+			
+			rs->SimulateRendering();
+
+			cout << " finished" << endl;
+
+			cout << GetRenderSimulator()->mStat << endl;
+			Debug << GetRenderSimulator()->mStat << endl;
+		}
 
 		if (1) // export view cells
@@ -659,8 +670,12 @@
 		exporter->SetExportRayDensity(true);
 		exporter->ExportKdTree(*mKdTree);
-
-		if (mBspTree && (ViewCell::sHierarchy == ViewCell::VSP))
-			exporter->ExportVspKdTree(*mVspKdTree);
-
+		delete exporter;
+	}
+
+	if (mVspKdTree)
+	{
+		Exporter *exporter = Exporter::GetExporter("vspkdtree.x3d"); 
+		exporter->SetWireframe();
+		exporter->ExportVspKdTree(*mVspKdTree);
 		delete exporter;
 	}
@@ -732,13 +747,12 @@
 											   int &sampleContributions)
 {
-	// save rays for bsp tree construction
 	if (!mVspKdTree)
 	{
+		// store samples for vsp kd tree construction
 		if ((int)mVspSampleRays.size() < mVspConstructionSamples)
 		{
-			MeshInstance *mi = dynamic_cast<MeshInstance *>(object);
-			
 			VssRay *sRay = new VssRay(ray);
-			mVspSampleRays.push_back(sRay);
+		
+            mVspSampleRays.push_back(sRay);
 		
 			// also add origin to sample
@@ -749,4 +763,5 @@
 			// construct VSP tree using the collected samples
 			cout << "building VSP tree from " << (int)mVspSampleRays.size() << " samples " << endl;
+			mVspKdTree = new VspKdTree();
 			mVspKdTree->Construct(mVspSampleRays);
 		
@@ -779,5 +794,5 @@
 	if (!mBspTree)
 	{
-		if ((BspTree::sConstructionMethod == BspTree::FROM_RAYS) &&
+		if ((BspTree::sConstructionMethod == BspTree::FROM_SAMPLES) &&
 			((int)mSampleRays.size() < mBspConstructionSamples))
 		{
@@ -889,5 +904,5 @@
 					outRays.push_back(mSampleRays[i]);
 			}
-			if (BspTree::sConstructionMethod == BspTree::FROM_RAYS)
+			if (BspTree::sConstructionMethod == BspTree::FROM_SAMPLES)
 			{
 				// export rays 
