Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/Preprocessor.vcproj	(revision 469)
@@ -410,4 +410,7 @@
 			</File>
 		</Filter>
+		<File
+			RelativePath=".\VTune\Preprocessor.vpj">
+		</File>
 	</Files>
 	<Globals>
Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 465)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 469)
@@ -265,4 +265,6 @@
 		# x3d visualization of the split planes
 		exportSplits true
+		exportRays true
+		exportGeometry false
 	}
 }
@@ -303,4 +305,10 @@
 		minViewCells 100
 		maxPvsSize   50000
+	}
+	
+	
+	Visualization {
+		exportRays true
+		exportGeometry false
 	}
 }
@@ -366,4 +374,6 @@
 		# x3d visualization of the split planes
 		exportSplits true
-	}
-}
+		exportRays true
+		exportGeometry false
+	}
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 469)
@@ -10,7 +10,8 @@
 #include "RayInfo.h"
 #include "ViewCellBsp.h"
+#include "ViewCell.h"
 
 class ViewCell;
-class BspViewCell;
+//class BspViewCell;
 class Plane3;
 class VspBspTree;  
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 469)
@@ -25,5 +25,4 @@
 #include "Ray.h"
 #include "RayInfo.h"
-#include "ViewCell.h"
 #include "ViewCellsManager.h"
 #include "ViewCellBsp.h"
@@ -1914,11 +1913,4 @@
 	Intersectable::NewMail();
 
-	//if (!mBox.GetMinMaxT(ray, &mint, &maxt)) return 0;
-
-	if (mint < 0)
-		mint = 0;
-
-	maxt += Limits::Threshold;
-
 	Vector3 entp = origin;
 	Vector3 extp = termination;
@@ -1985,4 +1977,5 @@
 				vc->Mail();
 				viewcells.push_back(vc);
+				++ hits;
 			}
 
@@ -2013,6 +2006,6 @@
 	leaf->SetViewCell(vc);
 
-	vc->SetSize(GetBBox(leaf).GetVolume());
-	vc->mVspKdLeaves.push_back(leaf);
+	vc->SetVolume(GetBBox(leaf).GetVolume());
+	vc->mLeaves.push_back(leaf);
 
 	for (it = leaf->GetRays().begin(); it != it_end; ++ it)
@@ -2067,6 +2060,6 @@
 					vcStat.minPvs = pvsSize;
 
-				if ((int)viewCell->mVspKdLeaves.size() > vcStat.maxLeaves)
-					vcStat.maxLeaves = (int)viewCell->mVspKdLeaves.size();
+				if ((int)viewCell->mLeaves.size() > vcStat.maxLeaves)
+					vcStat.maxLeaves = (int)viewCell->mLeaves.size();
 			}
 		}
@@ -2096,8 +2089,8 @@
 
 	// set new size of view cell
-	vc->SetSize(fVc->GetSize() + bVc->GetSize());
-
-	vector<VspKdLeaf *> fLeaves = fVc->mVspKdLeaves;
-	vector<VspKdLeaf *> bLeaves = bVc->mVspKdLeaves;
+	vc->SetVolume(fVc->GetVolume() + bVc->GetVolume());
+
+	vector<VspKdLeaf *> fLeaves = fVc->mLeaves;
+	vector<VspKdLeaf *> bLeaves = bVc->mLeaves;
 
 	vector<VspKdLeaf *>::const_iterator it;
@@ -2107,5 +2100,5 @@
 	{
 		(*it)->SetViewCell(vc);
-		vc->mVspKdLeaves.push_back(*it);
+		vc->mLeaves.push_back(*it);
 	}
 
@@ -2113,5 +2106,5 @@
 	{
 		(*it)->SetViewCell(vc);
-		vc->mVspKdLeaves.push_back(*it);
+		vc->mLeaves.push_back(*it);
 	}
 
@@ -2136,4 +2129,5 @@
 
 	int vcSize = (int)leaves.size();
+	int savedVcSize = vcSize;
 
 	VspKdLeaf::NewMail();
@@ -2208,5 +2202,7 @@
 	ValidateViewCellLeaves();
 
-
+	Debug << "merged " << merged << " of " << savedVcSize << " leaves" << endl;
+
+	//TODO: should return sample contributions
 	return merged;
 }
@@ -2235,9 +2231,9 @@
 			{
 				Debug << "jhere2" << endl;
-				viewCell->mVspKdLeaves.clear();
+				viewCell->mLeaves.clear();
 				viewCell->Mail();
 			}
 
-			viewCell->mVspKdLeaves.push_back(leaf);
+			viewCell->mLeaves.push_back(leaf);
 		}
 		else
@@ -2318,9 +2314,9 @@
 	//-- to new rendering cost (size of merged view cell times pvs size)
 	const float oldCost =
-		(float)vc1->GetPvs().GetSize() * vc1->GetSize() +
-		(float)vc2->GetPvs().GetSize() * vc2->GetSize();
+		(float)vc1->GetPvs().GetSize() * vc1->GetVolume() +
+		(float)vc2->GetPvs().GetSize() * vc2->GetVolume();
 
 	const float newCost =
-		(float)vcPvs * (vc1->GetSize() + vc2->GetSize());
+		(float)vcPvs * (vc1->GetVolume() + vc2->GetVolume());
 
 	mMergeCost = newCost - oldCost;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 469)
@@ -15,10 +15,10 @@
 }
 
-const ViewCellPvs &ViewCell::GetPvs() const
+const ObjectPvs &ViewCell::GetPvs() const
 {
 	return mPvs;
 }
 
-ViewCellPvs &ViewCell::GetPvs()
+ObjectPvs &ViewCell::GetPvs()
 {
 	return mPvs;
@@ -33,4 +33,14 @@
 {
 	mPassingRays.AddRay(ray, contributions);
+}
+
+float ViewCell::GetVolume() const
+{
+	return mVolume;
+}
+
+void ViewCell::SetVolume(float volume) 
+{
+	mVolume = volume;
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp	(revision 439)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp	(revision 469)
@@ -170,13 +170,13 @@
       char *endptr;
       while(1) {
-	float x = strtod(ptr, &endptr);
+	float x = (float)strtod(ptr, &endptr);
 	if (ptr == endptr)
 	  break;
 	ptr = endptr;
-	float y = strtod(ptr, &endptr);
+	float y = (float)strtod(ptr, &endptr);
 	if (ptr == endptr)
 	  break;
 	ptr = endptr;
-	float z = strtod(ptr, &endptr);
+	float z = (float)strtod(ptr, &endptr);
 	if (ptr == endptr)
 	  break;
@@ -477,5 +477,5 @@
 			while (1) 
 			{
-				float x = strtod(ptr, &endptr);
+				float x = (float)strtod(ptr, &endptr);
                 
 				if (ptr == endptr)
@@ -483,5 +483,5 @@
 				ptr = endptr;
 				
-				float y = (float)strtod(ptr, &endptr);
+				float y = (float)(float)strtod(ptr, &endptr);
 
 				
@@ -490,5 +490,5 @@
 				ptr = endptr;
 
-				float z = (float)strtod(ptr, &endptr);
+				float z = (float)(float)strtod(ptr, &endptr);
 
 				if (ptr == endptr)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 469)
@@ -3,4 +3,5 @@
 
 #include <map>
+#include <vector>
 
 class KdNode;
@@ -18,5 +19,7 @@
 };
 
-//typedef std::map<T *, PvsData<T>, LtSample<T>> PvsMap<T>;
+/** Information stored with a PVS entry. Consists of the number
+	the object was seen from the view cell.
+*/
 
 template<typename T> 
@@ -27,18 +30,25 @@
 };
 
+/** Template class representing the Potentially Visible Set (PVS) 
+	mainly from a view cell, but also e.g., from objects.
+*/
 template<typename T>
 class Pvs
 {
 public:
-  Pvs(): mSamples(0), mEntries() {}
+  Pvs(): /*mSamples(0), */mEntries() {}
   
-  int mSamples;
+  //int mSamples;
   
+  /** Compresses PVS lossless or lossy.
+  */
   int Compress() {return 0;}
   int GetSize() const {return (int)mEntries.size();}
-  bool Empty() const {return mEntries.size() == 0;}
+  bool Empty() const {return mEntries.empty();}
+  
   /** Merges pvs of a and pvs of b into this pvs.
    */
   void Merge(const Pvs<T> &a, const Pvs<T> &b);
+  
   /** Difference of pvs to pvs b.
 	  @returns number of different entries.
@@ -46,9 +56,30 @@
   int Diff(const Pvs<T> &b);
   
+  /** Finds sample in PVS.
+	  @returns sample if found, NULL otherwise.
+  */
   PvsData<T> *Find(T sample);
+  
+  /** Adds sample to PVS. 
+	  @contribution contribution of sample (0 or 1)
+	  @returns true if sample was not already in PVS.
+  */
   bool AddSample(T sample, float &contribution);
+
+  /** Adds sample to PVS.
+	  @returns contribution of sample (0 or 1)
+  */
   int AddSample(T sample);
 
+  /** Returns PVS data, i.e., how often it was seen from the view cell, 
+	  and the object itsef.
+  */
   void GetData(const int index, T &entry, PvsData<T> &data);
+
+  /** Collects the PVS entries and returns them in the vector.
+  */
+  void CollectEntries(std::vector<T> &entries);
+
+  /// Map of PVS entries
   std::map<T, PvsData<T>, LtSample<T> > mEntries;
 };
@@ -138,4 +169,14 @@
 }
 
+template <typename T>
+void Pvs<T>::CollectEntries(std::vector<T> &entries)
+{
+	PvsMap<T>::const_iterator it, it_end = mEntries.end();
+
+	// output PVS of view cell
+	for (it = mEntries.begin(); it != it_end; ++ it) 
+		entries.push_back((*it)->first);
+}
+
 /** Class instantiating the Pvs template for kd tree nodes.
 */
@@ -146,10 +187,9 @@
 
 typedef std::map<KdNode *, PvsData<KdNode *>, LtSample<KdNode *> > KdPvsMap;
-typedef std::map<ViewCellKdNode *, PvsData<ViewCellKdNode *>, LtSample<ViewCellKdNode *> > ViewCellKdPvsMap;
-typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ViewCellPvsMap;
-typedef PvsData<Intersectable *> ViewCellPvsData;
+typedef std::map<Intersectable *, PvsData<Intersectable *>, LtSample<Intersectable *> > ObjectPvsMap;
+typedef PvsData<Intersectable *> ObjectPvsData;
 typedef PvsData<KdNode *> KdPvsData;
-typedef Pvs<ViewCellKdNode *> ViewCellKdPvs;
-typedef Pvs<Intersectable *> ViewCellPvs;
+
+typedef Pvs<Intersectable *> ObjectPvs;
 
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 469)
@@ -8,8 +8,8 @@
 #include "Statistics.h"
 #include "VssRay.h"
-
+#include "ViewCell.h"
 
 class ViewCell;
-class BspViewCell;
+//class BspViewCell;
 class Plane3;
 class BspTree;  
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 469)
@@ -327,5 +327,5 @@
   virtual void Print(ostream &s) const {
     s<<
-	  "L: rays="<<rays.size()<<
+	  "L: rays="<<(int)rays.size()<<
 	  " pvs="<<mPvsSize<<" pEntropy="<<mPvsEntropy<<" lEntropy="<<mRayLengthEntropy<<endl;
   };
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Camera.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Camera.h	(revision 386)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Camera.h	(revision 469)
@@ -24,5 +24,5 @@
     mWidth = 640;
     mHeight = 480;
-    mFovy = 60.0*M_PI/180.0;
+    mFovy = 60.0f*(float)M_PI/180.0f;
   }
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 469)
@@ -172,9 +172,9 @@
   /** Returns probability that view point lies in one view cell.
   */
-  virtual float GetProbability(ViewCell *viewCell) = 0;
+  virtual float GetProbability(ViewCell *viewCell) const = 0;
 
   /** Returns render cost of a single view cell given the render cost of an object.
   */
-  virtual float GetRendercost(ViewCell *viewCell, float objRendercost) = 0;
+  virtual float GetRendercost(ViewCell *viewCell, float objRendercost) const = 0;
 
   /** Returns vector of loaded / generated view cells.
@@ -182,13 +182,13 @@
   ViewCellContainer &GetViewCells();
 
+  /** Helper function used to split ray set into one used for view cell
+      construction and one cast after construction.
+  */
+  void GetRaySets(const VssRayContainer &sourceRays,
+				  VssRayContainer &constructionRays, 
+				  VssRayContainer &savedRays) const;
+
 protected:
-	
-
-	/** Initialises the render time simulator.
-	*/
-	void InitRenderSimulator();
-	/////////////////////
-
-	
+		
 	/// the view cell corresponding to unbounded space
 	ViewCell *mUnbounded;
@@ -198,5 +198,5 @@
 
 	/// Loaded view cells
-  ViewCellContainer mViewCells;
+	ViewCellContainer mViewCells;
 
 	/// maximum number of samples taken for construction of the view cells
@@ -242,12 +242,10 @@
 	void PrintStatistics(ostream &s) const;
 
-	~BspViewCellsManager();
-
 	int CastLineSegment(const Vector3 &origin,
 						const Vector3 &termination,
 						ViewCellContainer &viewcells);
 	
-	float GetProbability(ViewCell *viewCell);
-	float GetRendercost(ViewCell *viewCell, float objRendercost);
+	float GetProbability(ViewCell *viewCell) const;
+	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
 
 protected:
@@ -312,6 +310,6 @@
 	virtual void PrintStatistics(ostream &s) const;
 
-	float GetProbability(ViewCell *viewCell);
-	float GetRendercost(ViewCell *viewCell, float objRendercost);
+	float GetProbability(ViewCell *viewCell) const;
+	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
 
 protected:
@@ -338,6 +336,4 @@
 	VspKdViewCellsManager(VspKdTree *vspKdTree, int constructionSamples);
 
-	~VspKdViewCellsManager();
-
 	int Construct(const ObjectContainer &objects, 
 				  const VssRayContainer &rays,
@@ -364,6 +360,6 @@
 						ViewCellContainer &viewcells);
 
-	float GetProbability(ViewCell *viewCell);
-	float GetRendercost(ViewCell *viewCell, float objRendercost);
+	float GetProbability(ViewCell *viewCell) const;
+	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
 
 protected:
@@ -385,6 +381,4 @@
 	VspBspViewCellsManager(VspBspTree *tree, int constructionSamples);
 
-	~VspBspViewCellsManager();
-
 	int Construct(const ObjectContainer &objects, 
 				  const VssRayContainer &rays,
@@ -410,6 +404,6 @@
 						ViewCellContainer &viewcells);
 
-	float GetProbability(ViewCell *viewCell);
-	float GetRendercost(ViewCell *viewCell, float objRendercost);
+	float GetProbability(ViewCell *viewCell) const;
+	float GetRendercost(ViewCell *viewCell, float objRendercost) const;
 
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 469)
@@ -324,5 +324,5 @@
 		
 		leaf->SetViewCell(viewCell);
-		viewCell->mBspLeaves.push_back(leaf);
+		viewCell->mLeaves.push_back(leaf);
 
 		//-- add pvs
@@ -1262,6 +1262,6 @@
 					stat.minPvs = pvsSize;
 
-				if ((int)viewCell->mBspLeaves.size() > stat.maxLeaves)
-					stat.maxLeaves = (int)viewCell->mBspLeaves.size();		
+				if ((int)viewCell->mLeaves.size() > stat.maxLeaves)
+					stat.maxLeaves = (int)viewCell->mLeaves.size();		
 			}
 		}
@@ -1461,5 +1461,5 @@
 void VspBspTree::ConstructGeometry(BspViewCell *vc, PolygonContainer &vcGeom) const
 {
-	vector<BspLeaf *> leaves = vc->mBspLeaves;
+	vector<BspLeaf *> leaves = vc->mLeaves;
 
 	vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
@@ -1712,90 +1712,93 @@
 
 
-int
-VspBspTree::CastLineSegment(const Vector3 &origin,
-						 const Vector3 &termination,
-						 vector<ViewCell *> &viewcells
-						 )
-{
-  int hits = 0;
-  stack<BspRayTraversalData> tStack;
+int VspBspTree::CastLineSegment(const Vector3 &origin,
+								const Vector3 &termination,
+								vector<ViewCell *> &viewcells)
+{
+	int hits = 0;
+	stack<BspRayTraversalData> tStack;
   
-  float mint = 0.0f, maxt = 1.0f;
+	float mint = 0.0f, maxt = 1.0f;
   
-  Intersectable::NewMail();
+	Intersectable::NewMail();
   
-  Vector3 entp = origin;
-  Vector3 extp = termination;
+	Vector3 entp = origin;
+	Vector3 extp = termination;
   
-  BspNode *node = mRoot;
-  BspNode *farChild = NULL;
+	BspNode *node = mRoot;
+	BspNode *farChild = NULL;
   
-  while (1) {
-	if (!node->IsLeaf())  {
-	  BspInterior *in = dynamic_cast<BspInterior *>(node);
+	while (1) 
+	{
+		if (!node->IsLeaf())  
+		{
+			BspInterior *in = dynamic_cast<BspInterior *>(node);
 	  
-	  Plane3 splitPlane = in->GetPlane();
-	  const int entSide = splitPlane.Side(entp);
-	  const int extSide = splitPlane.Side(extp);
+			Plane3 splitPlane = in->GetPlane();
+			const int entSide = splitPlane.Side(entp);
+			const int extSide = splitPlane.Side(extp);
 	  
-	  if (entSide < 0) {
-		node = in->GetBack();
-		
-		if(extSide <= 0) // plane does not split ray => no far child
-		  continue;
-		
-		farChild = in->GetFront(); // plane splits ray
-		
-	  } else
-		if (entSide > 0) {
-		  node = in->GetFront();
+			if (entSide < 0) 
+			{
+				node = in->GetBack();
+		
+				if(extSide <= 0) // plane does not split ray => no far child
+					continue;
+		
+				farChild = in->GetFront(); // plane splits ray
+			} else if (entSide > 0) 
+			{
+				node = in->GetFront();
 		  
-		  if (extSide >= 0) // plane does not split ray => no far child
-			continue;
+				if (extSide >= 0) // plane does not split ray => no far child
+					continue;
 		  
-		  farChild = in->GetBack(); // plane splits ray			
-		}
-		else // ray and plane are coincident
-		  {
-			// WHAT TO DO IN THIS CASE ?
-			//break;
-			node = in->GetFront();
-			continue;
-		  }
+				farChild = in->GetBack(); // plane splits ray			
+			}
+			else // ray and plane are coincident
+			{
+				// WHAT TO DO IN THIS CASE ?
+				//break;
+				node = in->GetFront();
+				continue;
+			}
 	  
-	  // push data for far child
-	  tStack.push(BspRayTraversalData(farChild, extp, maxt));
+			// push data for far child
+			tStack.push(BspRayTraversalData(farChild, extp, maxt));
 	  
-	  // find intersection of ray segment with plane
-	  float t;
-	  extp = splitPlane.FindIntersection(origin, extp, &t);
-	  maxt *= t;
+			// find intersection of ray segment with plane
+			float t;
+			extp = splitPlane.FindIntersection(origin, extp, &t);
+			maxt *= t;
 	  
-	} else {
-	  // reached leaf => intersection with view cell
-	  BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
+		} else 
+		{
+			// reached leaf => intersection with view cell
+			BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
 	  
-	  if (!leaf->GetViewCell()->Mailed()) {
-		viewcells.push_back(leaf->GetViewCell());
-		leaf->GetViewCell()->Mail();
-		hits++;
-	  }
+			if (!leaf->GetViewCell()->Mailed()) 
+			{
+				viewcells.push_back(leaf->GetViewCell());
+				leaf->GetViewCell()->Mail();
+				++ hits;
+			}
 	  
-	  //-- fetch the next far child from the stack
-	  if (tStack.empty())
-		break;
+			//-- fetch the next far child from the stack
+			if (tStack.empty())
+				break;
       
-	  entp = extp;
-	  mint = maxt; // NOTE: need this?
+			entp = extp;
+			mint = maxt; // NOTE: need this?
 	  
-	  BspRayTraversalData &s = tStack.top();
+			
+			BspRayTraversalData &s = tStack.top();
+            
+			node = s.mNode;
+			extp = s.mExitPoint;
+			maxt = s.mMaxT;
 	  
-	  node = s.mNode;
-	  extp = s.mExitPoint;
-	  maxt = s.mMaxT;
-	  
-	  tStack.pop();
-	}
-  }
-  return hits;
-}
+			tStack.pop();
+		}
+	}
+	return hits;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 465)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 469)
@@ -1150,28 +1150,29 @@
 		 "10");
 
-	RegisterOption("VssPreprocessor.initialSamples",
-								 optInt,
-								 "-initial_samples=",
-								 "100000");
-
-	RegisterOption("VssPreprocessor.vssSamples",
-								 optInt,
-								 "-vss_samples=",
-								 "1000000");
+  RegisterOption("VssPreprocessor.initialSamples",
+	  optInt,
+	  "-initial_samples=",
+	  "100000");
+
+  RegisterOption("VssPreprocessor.vssSamples",
+	  optInt,
+	  "-vss_samples=",
+	  "1000000");
 	
   RegisterOption("VssPreprocessor.vssSamplesPerPass",
-								 optInt,
-								 "-vss_samples_per_pass=",
-								 "1000");
-
-	  RegisterOption("VssPreprocessor.samplesPerPass",
-								 optInt,
-								 "-samples_per_pass=",
-								 "100000");
+	  optInt,
+	  "-vss_samples_per_pass=",
+	  "1000");
+
+  RegisterOption("VssPreprocessor.samplesPerPass",
+	  optInt,
+	  "-samples_per_pass=",								
+	  "100000");
+
 
   RegisterOption("VssPreprocessor.useImportanceSampling",
-								 optBool,
-								 "-vss_use_importance=",
-								 "true");
+	  optBool,
+	  "-vss_use_importance=",
+	  "true");
 
 
@@ -1366,22 +1367,32 @@
   RegisterOption("BspTree.Visualization.exportSplits",
 	  optBool,
-	  "-bsp_visualization.exportSplits",
+	  "-bsp_visualization.export_splits",
 	  "false");
 
-  RegisterOption("BspTree.Factor.verticalSplits", optFloat, "-bsp_factor_vertical=", "1.0");
-  RegisterOption("BspTree.Factor.largestPolyArea", optFloat, "-bsp_factor_largest_poly=", "1.0");
-  RegisterOption("BspTree.Factor.blockedRays", optFloat, "-bsp_factor_blocked=", "1.0");
-  RegisterOption("BspTree.Factor.leastSplits", optFloat, "-bsp_factor_least_splits=", "1.0");
-  RegisterOption("BspTree.Factor.balancedPolys", optFloat, "-bsp_factor_balanced_polys=", "1.0");
-  RegisterOption("BspTree.Factor.balancedViewCells", optFloat, "-bsp_factor_balanced_view_cells=", "1.0");
-  RegisterOption("BspTree.Factor.leastRaySplits", optFloat, "-bsp_factor_least_ray_splits=", "1.0");
-  RegisterOption("BspTree.Factor.balancedRays", optFloat, "-bsp_factor_balanced_rays=", "1.0");
-  RegisterOption("BspTree.Factor.pvs", optFloat, "-bsp_factor_pvs=", "1.0");
-
-  /************************************************************************************/
-  /*                         Preprocessor related options                             */
-  /************************************************************************************/
-
-  RegisterOption("Preprocessor.type",
+   RegisterOption("BspTree.Visualization.exportRays",
+	  optBool,
+	  "-bsp_visualization.export_rays",
+	  "false");
+   
+   RegisterOption("BspTree.Visualization.exportGeometry",
+	  optBool,
+	  "-bsp_visualization.export_geometry",
+	  "false");
+
+   RegisterOption("BspTree.Factor.verticalSplits", optFloat, "-bsp_factor_vertical=", "1.0");
+   RegisterOption("BspTree.Factor.largestPolyArea", optFloat, "-bsp_factor_largest_poly=", "1.0");
+   RegisterOption("BspTree.Factor.blockedRays", optFloat, "-bsp_factor_blocked=", "1.0");
+   RegisterOption("BspTree.Factor.leastSplits", optFloat, "-bsp_factor_least_splits=", "1.0");
+   RegisterOption("BspTree.Factor.balancedPolys", optFloat, "-bsp_factor_balanced_polys=", "1.0");
+   RegisterOption("BspTree.Factor.balancedViewCells", optFloat, "-bsp_factor_balanced_view_cells=", "1.0");
+   RegisterOption("BspTree.Factor.leastRaySplits", optFloat, "-bsp_factor_least_ray_splits=", "1.0");
+   RegisterOption("BspTree.Factor.balancedRays", optFloat, "-bsp_factor_balanced_rays=", "1.0");
+   RegisterOption("BspTree.Factor.pvs", optFloat, "-bsp_factor_pvs=", "1.0");
+
+   /************************************************************************************/
+   /*                         Preprocessor related options                             */
+   /************************************************************************************/
+
+   RegisterOption("Preprocessor.type",
 				 optString,
 				 "-preprocessor=",
@@ -1389,322 +1400,347 @@
 
 
-  /**************************************************************************************/
-  /*                  View space partition KD tree related options                      */
-  /**************************************************************************************/
-
-  RegisterOption("VspKdTree.Construction.samples",
-		 optInt,
-		 "-vsp_kd_construction_samples=",
-		 "100000");
-
-  RegisterOption("VspKdTree.Termination.maxDepth", 
+   /**************************************************************************************/
+   /*                  View space partition KD tree related options                      */
+   /**************************************************************************************/
+
+   RegisterOption("VspKdTree.Construction.samples",
+	   optInt,
+	   "-vsp_kd_construction_samples=",
+	   "100000");
+
+   RegisterOption("VspKdTree.Termination.maxDepth", 
+	   optInt, 
+	   "vsp_term_maxdepth=", "30");
+
+   RegisterOption("VspKdTree.Termination.minPvs", 
+	   optInt, 
+	   "vsp_minpvs=", 
+	   "1");
+
+   RegisterOption("VspKdTree.Termination.minRays", 
+	   optInt, 
+	   "vsp_term_minrays=", 
+	   "10");
+
+   RegisterOption("VspKdTree.Termination.minSize", 
+	   optFloat, 
+	   "vsp_term_minsize=", 
+	   "0.001");
+
+   RegisterOption("VspKdTree.Termination.maxCostRatio", 
+	   optFloat, 
+	   "vsp_term_maxcost=",
+	   "0.95");
+
+   RegisterOption("VspKdTree.Termination.maxRayContribution", 
+	   optFloat, 
+	   "vsp_term_max_ray_contrib=", 
+	   "0.5");
+
+   RegisterOption("VspKdTree.epsilon", 
+	   optFloat, 
+	   "kd_eps=", 
+	   "1e-6");
+
+   RegisterOption("VspKdTree.ct_div_ci", 
+	   optFloat, 
+	   "vsp_ctdivci=", "1.0");
+   
+   RegisterOption("VspKdTree.splitType", 
+	   optString, 
+	   "split=", 
+	   "queries");
+
+	RegisterOption("VspKdTree.numberOfEndPointDomains", 
 		optInt, 
-		"vsp_term_maxdepth=", "30");
-
-  RegisterOption("VspKdTree.Termination.minPvs", 
-	  optInt, 
-	  "vsp_minpvs=", 
-	  "1");
-
-  RegisterOption("VspKdTree.Termination.minRays", 
-	  optInt, 
-	  "vsp_term_minrays=", 
-	  "10");
-
-  RegisterOption("VspKdTree.Termination.minSize", 
-	  optFloat, 
-	  "vsp_term_minsize=", 
-	  "0.001");
-
-  RegisterOption("VspKdTree.Termination.maxCostRatio", 
-	  optFloat, 
-	  "vsp_term_maxcost=", 
-	  "0.95");
-
-  RegisterOption("VspKdTree.Termination.maxRayContribution", 
-	  optFloat, 
-	  "vsp_term_max_ray_contrib=", 
-	  "0.5");
-
-  RegisterOption("VspKdTree.epsilon", 
-	  optFloat, 
-	  "kd_eps=", 
-	  "1e-6");
-
-  RegisterOption("VspKdTree.ct_div_ci", 
-	  optFloat, 
-	  "vsp_ctdivci=", "1.0");
-  
-  RegisterOption("VspKdTree.splitType", 
-	  optString, 
-	  "split=", 
-	  "queries");
-
-  RegisterOption("VspKdTree.numberOfEndPointDomains", 
-	  optInt, 
-	  "endpoints=", 
-	  "10000");
-
-  RegisterOption("VspKdTree.maxTotalMemory", 
-	  optFloat, 
-	  "vsp_max_total_mem=", 
-	  "60.0");
-
-  RegisterOption("VspKdTree.maxStaticMemory", 
-	  optFloat, 
-	  "vsp_max_static_mem=", 
-	  "8.0");
-
-  RegisterOption("VspKdTree.queryType", 
-	  optString, 
-	  "qtype=", 
-	  "static");
-
-  RegisterOption("VspKdTree.queryPosWeight", 
+		"endpoints=", 
+		"10000");
+
+	RegisterOption("VspKdTree.maxTotalMemory", 
 		optFloat, 
-		"qposweight=", 
+		"vsp_max_total_mem=", 
+		"60.0");
+
+	RegisterOption("VspKdTree.maxStaticMemory", 
+		optFloat, 
+		"vsp_max_static_mem=", 
+		"8.0");
+
+	RegisterOption("VspKdTree.queryType", 
+		optString, 
+		"qtype=", 
+		"static");
+
+	RegisterOption("VspKdTree.queryPosWeight", 
+		optFloat, 
+		"vsp_kd_qposweight=", 
 		"0.0");
-  
-  RegisterOption("VspKdTree.accessTimeThreshold", 
-	  optInt, 
-	  "accesstime=", 
-	  "1000");
-
-  RegisterOption("VspKdTree.minCollapseDepth", 
-	  optInt, 
-	  "vsp_min_colldepth=", 
-	  "4");
-
-  RegisterOption("VspKdTree.PostProcess.maxCostRatio",
-		 optFloat,
-		 "-vsp_kd_post_process_max_cost_ratio=",
-		 "1.5");
-
-  RegisterOption("VspKdTree.PostProcess.minViewCells", 
-	  optInt, 
-	  "vsp_term_post_process_min_view_cells=", 
-	  "1000");
-
-  RegisterOption("VspKdTree.PostProcess.maxPvsSize", 
-	  optInt, 
-	  "vsp_term_post_process_max_pvs_size=", 
-	  "100");
-
-  /************************************************************************************/
-  /*                   VSS Preprocessor cells related options                         */
-  /************************************************************************************/
-
-  RegisterOption("VssTree.maxDepth", optInt, "kd_depth=", "12");
-  RegisterOption("VssTree.minPvs", optInt, "kd_minpvs=", "1");
-  RegisterOption("VssTree.minRays", optInt, "kd_minrays=", "10");
-  RegisterOption("VssTree.maxCostRatio", optFloat, "maxcost=", "0.95");
-  RegisterOption("VssTree.maxRayContribution", optFloat, "maxraycontrib=", "0.5");
-
-  RegisterOption("VssTree.epsilon", optFloat, "kd_eps=", "1e-6");
-  RegisterOption("VssTree.ct_div_ci", optFloat, "kd_ctdivci=", "1.0");
-  RegisterOption("VssTree.randomize", optBool, "randomize", "false");
-  RegisterOption("VssTree.splitType", optString, "split=", "queries");
-  RegisterOption("VssTree.splitUseOnlyDrivingAxis", optBool, "splitdriving=", "false");
-  RegisterOption("VssTree.useRss", optBool, "rss=", "false");
-  RegisterOption("VssTree.numberOfEndPointDomains", optInt, "endpoints=", "10000");
-
-  RegisterOption("VssTree.minSize", optFloat, "minsize=", "0.001");
-
-  RegisterOption("VssTree.maxTotalMemory", optFloat, "mem=", "60.0");
-  RegisterOption("VssTree.maxStaticMemory", optFloat, "statmem=", "8.0");
-
-  RegisterOption("VssTree.queryType", optString, "qtype=", "static");
-
-  RegisterOption("VssTree.queryPosWeight", optFloat, "qposweight=", "0.0");
-  RegisterOption("VssTree.useRefDirSplits", optBool, "refdir", "false");
-  RegisterOption("VssTree.refDirAngle", optFloat, "refangle=", "10");
-  RegisterOption("VssTree.refDirBoxMaxSize", optFloat, "refboxsize=", "0.1");
-  RegisterOption("VssTree.accessTimeThreshold", optInt, "accesstime=", "1000");
-  RegisterOption("VssTree.minCollapseDepth", optInt, "colldepth=", "4");
-
-  RegisterOption("VssTree.interleaveDirSplits", optBool, "interleavedirsplits", "true");
-  RegisterOption("VssTree.dirSplitDepth", optInt, "dirsplidepth=", "10");
-
-  
-  	RegisterOption("RssPreprocessor.initialSamples",
-								 optInt,
-								 "-initial_samples=",
-								 "100000");
+
+	RegisterOption("VspKdTree.accessTimeThreshold", 
+		optInt, 
+		"vsp_kd_accesstime=", 
+		"1000");
+
+	RegisterOption("VspKdTree.minCollapseDepth", 
+		optInt, 
+		"vsp_kd_min_colldepth=", 
+		"4");
+
+	RegisterOption("VspKdTree.PostProcess.maxCostRatio",
+			optFloat,
+			"-vsp_kd_post_process_max_cost_ratio=",
+			"1.5");
+
+	RegisterOption("VspKdTree.PostProcess.minViewCells", 
+		optInt, 
+		"vsp_kd_term_post_process_min_view_cells=", 
+		"1000");
+
+	RegisterOption("VspKdTree.PostProcess.maxPvsSize", 
+		optInt, 
+		"vsp_kd_term_post_process_max_pvs_size=", 
+		"100");
+	
+	RegisterOption("VspKdTree.Visualization.exportRays",
+		optBool,
+		"-vsp_kd_visualization.export_rays",
+		"false");
+   
+	RegisterOption("VspKdTree.Visualization.exportGeometry",
+		optBool,
+		"-vsp_kd_visualization.export_geometry",
+		"false");
+
+
+	/************************************************************************************/
+	/*                   VSS Preprocessor cells related options                         */
+	/************************************************************************************/
+
+	RegisterOption("VssTree.maxDepth", optInt, "kd_depth=", "12");
+	RegisterOption("VssTree.minPvs", optInt, "kd_minpvs=", "1");
+	RegisterOption("VssTree.minRays", optInt, "kd_minrays=", "10");
+	RegisterOption("VssTree.maxCostRatio", optFloat, "maxcost=", "0.95");
+	RegisterOption("VssTree.maxRayContribution", optFloat, "maxraycontrib=", "0.5");
+
+	RegisterOption("VssTree.epsilon", optFloat, "kd_eps=", "1e-6");
+	RegisterOption("VssTree.ct_div_ci", optFloat, "kd_ctdivci=", "1.0");
+	RegisterOption("VssTree.randomize", optBool, "randomize", "false");
+	RegisterOption("VssTree.splitType", optString, "split=", "queries");
+	RegisterOption("VssTree.splitUseOnlyDrivingAxis", optBool, "splitdriving=", "false");
+	RegisterOption("VssTree.useRss", optBool, "rss=", "false");
+	RegisterOption("VssTree.numberOfEndPointDomains", optInt, "endpoints=", "10000");
+
+	RegisterOption("VssTree.minSize", optFloat, "minsize=", "0.001");
+
+	RegisterOption("VssTree.maxTotalMemory", optFloat, "mem=", "60.0");
+	RegisterOption("VssTree.maxStaticMemory", optFloat, "statmem=", "8.0");
+
+	RegisterOption("VssTree.queryType", optString, "qtype=", "static");
+
+	RegisterOption("VssTree.queryPosWeight", optFloat, "qposweight=", "0.0");
+	RegisterOption("VssTree.useRefDirSplits", optBool, "refdir", "false");
+	RegisterOption("VssTree.refDirAngle", optFloat, "refangle=", "10");
+	RegisterOption("VssTree.refDirBoxMaxSize", optFloat, "refboxsize=", "0.1");
+	RegisterOption("VssTree.accessTimeThreshold", optInt, "accesstime=", "1000");
+	RegisterOption("VssTree.minCollapseDepth", optInt, "colldepth=", "4");
+
+	RegisterOption("VssTree.interleaveDirSplits", optBool, "interleavedirsplits", "true");
+	RegisterOption("VssTree.dirSplitDepth", optInt, "dirsplidepth=", "10");
+
+
+	RegisterOption("RssPreprocessor.initialSamples",
+									optInt,
+									"-initial_samples=",
+									"100000");
 
 	RegisterOption("RssPreprocessor.vssSamples",
-								 optInt,
-								 "-vss_samples=",
-								 "1000000");
-	
-  RegisterOption("RssPreprocessor.vssSamplesPerPass",
-								 optInt,
-								 "-vss_samples_per_pass=",
-								 "1000");
-
-	  RegisterOption("RssPreprocessor.samplesPerPass",
-								 optInt,
-								 "-samples_per_pass=",
-								 "100000");
-
-  RegisterOption("RssPreprocessor.useImportanceSampling",
-								 optBool,
-								 "-vss_use_importance=",
-								 "true");
-
-  RegisterOption("RssTree.maxDepth", optInt, "kd_depth=", "12");
-  RegisterOption("RssTree.minPvs", optInt, "kd_minpvs=", "1");
-  RegisterOption("RssTree.minRays", optInt, "kd_minrays=", "10");
-  RegisterOption("RssTree.maxCostRatio", optFloat, "maxcost=", "0.95");
-  RegisterOption("RssTree.maxRayContribution", optFloat, "maxraycontrib=", "0.5");
-
-  RegisterOption("RssTree.epsilon", optFloat, "kd_eps=", "1e-6");
-  RegisterOption("RssTree.ct_div_ci", optFloat, "kd_ctdivci=", "1.0");
-  RegisterOption("RssTree.randomize", optBool, "randomize", "false");
-  RegisterOption("RssTree.splitType", optString, "split=", "queries");
-  RegisterOption("RssTree.splitUseOnlyDrivingAxis", optBool, "splitdriving=", "false");
-  
-  RegisterOption("RssTree.numberOfEndPointDomains", optInt, "endpoints=", "10000");
-
-  RegisterOption("RssTree.minSize", optFloat, "minsize=", "0.001");
-
-  RegisterOption("RssTree.maxTotalMemory", optFloat, "mem=", "60.0");
-  RegisterOption("RssTree.maxStaticMemory", optFloat, "statmem=", "8.0");
-
-  RegisterOption("RssTree.queryType", optString, "qtype=", "static");
-
-  RegisterOption("RssTree.queryPosWeight", optFloat, "qposweight=", "0.0");
-  RegisterOption("RssTree.useRefDirSplits", optBool, "refdir", "false");
-  RegisterOption("RssTree.refDirAngle", optFloat, "refangle=", "10");
-  RegisterOption("RssTree.refDirBoxMaxSize", optFloat, "refboxsize=", "0.1");
-  RegisterOption("RssTree.accessTimeThreshold", optInt, "accesstime=", "1000");
-  RegisterOption("RssTree.minCollapseDepth", optInt, "colldepth=", "4");
-
-  RegisterOption("RssTree.interleaveDirSplits", optBool, "interleavedirsplits", "true");
-  RegisterOption("RssTree.dirSplitDepth", optInt, "dirsplidepth=", "10");
-
-  RegisterOption("RssPreprocessor.Export.pvs", optBool, "rss_export_pvs", "false");
-  RegisterOption("RssPreprocessor.Export.rssTree", optBool, "rss_export_rss_tree", "false");
-  RegisterOption("RssPreprocessor.Export.rays", optBool, "rss_export_rays", "false");
-  RegisterOption("RssPreprocessor.Export.numRays", optInt, "rss_export_num_rays=", "5000");
-  RegisterOption("RssPreprocessor.useViewcells", optBool, "rss_use_viewcells", "false");
-  RegisterOption("RssPreprocessor.updateSubdivision", optBool, "rss_update_subdivision", "false");
-
-  
-  /************************************************************************************/
-  /*               View space partition BSP tree related options                      */
-  /************************************************************************************/
-
-
-  RegisterOption("VspBspTree.Termination.minPolygons",
-		 optInt,
-		 "-vsp_bsp_term_min_polygons=",
-		 "5");
-
-  RegisterOption("VspBspTree.Termination.minPvs",
-		 optInt,
-		 "-vsp_bsp_term_min_pvs=",
-		 "20");
-
-  RegisterOption("VspBspTree.Termination.minArea",
-		 optFloat,
-		 "-vsp_bsp_term_min_area=",
-		 "0.001");
-  
-  RegisterOption("VspBspTree.Termination.maxRayContribution",
-		 optFloat,
-		 "-vsp_bsp_term_ray_contribution=",
-		 "0.005");
-
-  RegisterOption("VspBspTree.Termination.minAccRayLenght",
-		 optFloat,
-		 "-vsp_bsp_term_min_acc_ray_length=",
-		 "50");
-
-   RegisterOption("VspBspTree.Termination.minRays",
-		 optInt,
-		 "-vsp_bsp_term_min_rays=",
-		 "-1");
-
-   RegisterOption("VspBspTree.Termination.ct_div_ci",
-		 optFloat,
-		 "-vsp_bsp_term_ct_div_ci=",
-		 "0.0");
-  
-  RegisterOption("VspBspTree.Termination.maxDepth",
-		 optInt,
-		 "-vsp_bsp_term_max_depth=",
-		 "100");
-
-  RegisterOption("VspBspTree.Termination.AxisAligned.maxCostRatio",
-		 optFloat,
-		 "-vsp_bsp_term_axis_aligned_max_cost_ratio=",
-		 "1.5");
-
-  
-  RegisterOption("VspBspTree.Termination.AxisAligned.ct_div_ci",
-		 optFloat,
-		 "-vsp_bsp_term_axis_aligned_ct_div_ci=",
-		 "0.5");
-
-  RegisterOption("VspBspTree.AxisAligned.splitBorder",
-		 optFloat,
-		 "-vsp_bsp__axis_aligned_split_border=",
-		 "0.1");
-
-  RegisterOption("VspBspTree.Termination.AxisAligned.minPolys",
-		 optInt,
-		 "-vsp_bsp_term_axis_aligned_max_polygons=",
-		 "50");
-
-   RegisterOption("VspBspTree.Termination.AxisAligned.minObjects",
-		 optInt,
-		 "-vsp_bsp_term_min_objects=",
-		 "3");
-
-  RegisterOption("VspBspTree.Termination.AxisAligned.minRays",
-		 optInt,
-		 "-vsp_bsp_term_axis_aligned_min_rays=",
-		 "-1");
-
-  RegisterOption("VspBspTree.splitPlaneStrategy",
-		 optString,
-		 "-vsp_bsp_split_method=",
-		 "leastSplits");
-
-  RegisterOption("VspBspTree.maxPolyCandidates",
-	  optInt,
-	  "-vsp_bsp_max_poly_candidates=",
-	  "20");
-
-  RegisterOption("VspBspTree.maxRayCandidates",
-	  optInt,
-	  "-vsp_bsp_max_plane_candidates=",
-	  "20");
-
-  RegisterOption("VspBspTree.maxTests",
-	  optInt,
-	  "-vsp_bsp_max_tests=",
-	  "5000");
-
-  RegisterOption("VspBspTree.Visualization.exportSplits",
-	  optBool,
-	  "-vsp_bsp_visualization.exportSplits",
-	  "false");
-
-  RegisterOption("VspBspTree.Construction.samples",
-	  optInt,
-	  "-bsp_construction_samples=",
-	  "100000");
-
-  RegisterOption("VspBspTree.Construction.epsilon",
-	  optFloat,
-	  "-vsp_bsp_construction_side_tolerance=",
-	  "0.002");
-
-  RegisterOption("VspBspTree.Factor.leastRaySplits", optFloat, "-vsp_bsp_factor_least_ray_splits=", "1.0");
-  RegisterOption("VspBspTree.Factor.balancedRays", optFloat, "-vsp_bsp_factor_balanced_rays=", "1.0");
-  RegisterOption("VspBspTree.Factor.pvs", optFloat, "-vsp_bsp_factor_pvs=", "1.0");
-
-   //////////////////////////////////////////////////////////////////////////////////
+									optInt,
+									"-vss_samples=",
+									"1000000");
+
+	RegisterOption("RssPreprocessor.vssSamplesPerPass",
+									optInt,
+									"-vss_samples_per_pass=",
+									"1000");
+
+		RegisterOption("RssPreprocessor.samplesPerPass",
+									optInt,
+									"-samples_per_pass=",
+									"100000");
+
+	RegisterOption("RssPreprocessor.useImportanceSampling",
+									optBool,
+									"-vss_use_importance=",
+									"true");
+
+	RegisterOption("RssTree.maxDepth", optInt, "kd_depth=", "12");
+	RegisterOption("RssTree.minPvs", optInt, "kd_minpvs=", "1");
+	RegisterOption("RssTree.minRays", optInt, "kd_minrays=", "10");
+	RegisterOption("RssTree.maxCostRatio", optFloat, "maxcost=", "0.95");
+	RegisterOption("RssTree.maxRayContribution", optFloat, "maxraycontrib=", "0.5");
+
+	RegisterOption("RssTree.epsilon", optFloat, "kd_eps=", "1e-6");
+	RegisterOption("RssTree.ct_div_ci", optFloat, "kd_ctdivci=", "1.0");
+	RegisterOption("RssTree.randomize", optBool, "randomize", "false");
+	RegisterOption("RssTree.splitType", optString, "split=", "queries");
+	RegisterOption("RssTree.splitUseOnlyDrivingAxis", optBool, "splitdriving=", "false");
+
+	RegisterOption("RssTree.numberOfEndPointDomains", optInt, "endpoints=", "10000");
+
+	RegisterOption("RssTree.minSize", optFloat, "minsize=", "0.001");
+
+	RegisterOption("RssTree.maxTotalMemory", optFloat, "mem=", "60.0");
+	RegisterOption("RssTree.maxStaticMemory", optFloat, "statmem=", "8.0");
+
+	RegisterOption("RssTree.queryType", optString, "qtype=", "static");
+
+	RegisterOption("RssTree.queryPosWeight", optFloat, "qposweight=", "0.0");
+	RegisterOption("RssTree.useRefDirSplits", optBool, "refdir", "false");
+	RegisterOption("RssTree.refDirAngle", optFloat, "refangle=", "10");
+	RegisterOption("RssTree.refDirBoxMaxSize", optFloat, "refboxsize=", "0.1");
+	RegisterOption("RssTree.accessTimeThreshold", optInt, "accesstime=", "1000");
+	RegisterOption("RssTree.minCollapseDepth", optInt, "colldepth=", "4");
+
+	RegisterOption("RssTree.interleaveDirSplits", optBool, "interleavedirsplits", "true");
+	RegisterOption("RssTree.dirSplitDepth", optInt, "dirsplidepth=", "10");
+
+	RegisterOption("RssPreprocessor.Export.pvs", optBool, "rss_export_pvs", "false");
+	RegisterOption("RssPreprocessor.Export.rssTree", optBool, "rss_export_rss_tree", "false");
+	RegisterOption("RssPreprocessor.Export.rays", optBool, "rss_export_rays", "false");
+	RegisterOption("RssPreprocessor.Export.numRays", optInt, "rss_export_num_rays=", "5000");
+	RegisterOption("RssPreprocessor.useViewcells", optBool, "rss_use_viewcells", "false");
+	RegisterOption("RssPreprocessor.updateSubdivision", optBool, "rss_update_subdivision", "false");
+
+
+	/************************************************************************************/
+	/*               View space partition BSP tree related options                      */
+	/************************************************************************************/
+
+
+	RegisterOption("VspBspTree.Termination.minPolygons",
+			optInt,
+			"-vsp_bsp_term_min_polygons=",
+			"5");
+
+	RegisterOption("VspBspTree.Termination.minPvs",
+			optInt,
+			"-vsp_bsp_term_min_pvs=",
+			"20");
+
+	RegisterOption("VspBspTree.Termination.minArea",
+			optFloat,
+			"-vsp_bsp_term_min_area=",
+			"0.001");
+
+	RegisterOption("VspBspTree.Termination.maxRayContribution",
+			optFloat,
+			"-vsp_bsp_term_ray_contribution=",
+			"0.005");
+
+	RegisterOption("VspBspTree.Termination.minAccRayLenght",
+			optFloat,
+			"-vsp_bsp_term_min_acc_ray_length=",
+			"50");
+
+	RegisterOption("VspBspTree.Termination.minRays",
+			optInt,
+			"-vsp_bsp_term_min_rays=",
+			"-1");
+
+	RegisterOption("VspBspTree.Termination.ct_div_ci",
+			optFloat,
+			"-vsp_bsp_term_ct_div_ci=",
+			"0.0");
+
+	RegisterOption("VspBspTree.Termination.maxDepth",
+			optInt,
+			"-vsp_bsp_term_max_depth=",
+			"100");
+
+	RegisterOption("VspBspTree.Termination.AxisAligned.maxCostRatio",
+			optFloat,
+			"-vsp_bsp_term_axis_aligned_max_cost_ratio=",
+			"1.5");
+
+
+	RegisterOption("VspBspTree.Termination.AxisAligned.ct_div_ci",
+			optFloat,
+			"-vsp_bsp_term_axis_aligned_ct_div_ci=",
+			"0.5");
+
+	RegisterOption("VspBspTree.AxisAligned.splitBorder",
+			optFloat,
+			"-vsp_bsp__axis_aligned_split_border=",
+			"0.1");
+
+	RegisterOption("VspBspTree.Termination.AxisAligned.minPolys",
+			optInt,
+			"-vsp_bsp_term_axis_aligned_max_polygons=",
+			"50");
+
+	RegisterOption("VspBspTree.Termination.AxisAligned.minObjects",
+			optInt,
+			"-vsp_bsp_term_min_objects=",
+			"3");
+
+	RegisterOption("VspBspTree.Termination.AxisAligned.minRays",
+			optInt,
+			"-vsp_bsp_term_axis_aligned_min_rays=",
+			"-1");
+
+	RegisterOption("VspBspTree.splitPlaneStrategy",
+			optString,
+			"-vsp_bsp_split_method=",
+			"leastSplits");
+
+	RegisterOption("VspBspTree.maxPolyCandidates",
+		optInt,
+		"-vsp_bsp_max_poly_candidates=",
+		"20");
+
+	RegisterOption("VspBspTree.maxRayCandidates",
+		optInt,
+		"-vsp_bsp_max_plane_candidates=",
+		"20");
+
+	RegisterOption("VspBspTree.maxTests",
+		optInt,
+		"-vsp_bsp_max_tests=",
+		"5000");
+
+	RegisterOption("VspBspTree.Visualization.exportSplits",
+		optBool,
+		"-vsp_bsp_visualization.exportSplits",
+		"false");
+
+	RegisterOption("VspBspTree.Construction.samples",
+		optInt,
+		"-bsp_construction_samples=",
+		"100000");
+
+	RegisterOption("VspBspTree.Construction.epsilon",
+		optFloat,
+		"-vsp_bsp_construction_side_tolerance=",
+		"0.002");
+
+	RegisterOption("VspBspTree.Visualization.exportSplits",
+		optBool,
+		"-vsp_bsp_visualization.export_splits",
+		"false");
+	RegisterOption("BspTree.Visualization.exportRays",
+		optBool,
+		"-vsp_bsp_visualization.export_rays",
+		"false");
+   
+	RegisterOption("VspBspTree.Visualization.exportGeometry",
+		optBool,
+		"-vsp_bsp_visualization.export_geometry",
+		"false");
+
+	RegisterOption("VspBspTree.Factor.leastRaySplits", optFloat, "-vsp_bsp_factor_least_ray_splits=", "1.0");
+	RegisterOption("VspBspTree.Factor.balancedRays", optFloat, "-vsp_bsp_factor_balanced_rays=", "1.0");
+	RegisterOption("VspBspTree.Factor.pvs", optFloat, "-vsp_bsp_factor_pvs=", "1.0");
+
+	//////////////////////////////////////////////////////////////////////////////////
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 469)
@@ -5,5 +5,5 @@
 #include "Mesh.h"
 #include "KdTree.h"
-
+#include "ViewCell.h"
 
 int KdNode::mailID = 1;
@@ -209,6 +209,6 @@
   
   if (TerminationCriteriaMet(leaf))
-    return leaf;
-  
+	  return leaf;
+
   float position;
   
@@ -277,5 +277,5 @@
       back->mObjects.push_back(*mi);
     
-    mStat.objectRefs -= leaf->mObjects.size();
+    mStat.objectRefs -= (int)leaf->mObjects.size();
     mStat.objectRefs += objectsBack + objectsFront;
   }
@@ -362,5 +362,5 @@
   
   if ( (int)(leaf->mObjects.size()) > mStat.maxObjectRefs)
-    mStat.maxObjectRefs = leaf->mObjects.size();
+    mStat.maxObjectRefs = (int)leaf->mObjects.size();
   
 }
@@ -376,5 +376,5 @@
   splitCandidates->clear();
   
-  int requestedSize = 2*node->mObjects.size();
+  int requestedSize = 2*(int)node->mObjects.size();
   // creates a sorted split candidates array
   if (splitCandidates->capacity() > 500000 &&
@@ -438,5 +438,5 @@
   float intersectionsRight = totalIntersections;
 	
-  int objectsLeft = 0, objectsRight = node->mObjects.size();
+  int objectsLeft = 0, objectsRight = (int)node->mObjects.size();
   
   float minBox = box.Min(axis);
@@ -447,5 +447,5 @@
   float maxBand = minBox + (1.0f - mSplitBorder)*(maxBox - minBox);
   
-  float minSum = 1e20;
+  float minSum = 1e20f;
   
   for(ci = splitCandidates->begin();
@@ -606,4 +606,108 @@
 }
 
+int KdTree::CastLineSegment(const Vector3 &origin,
+							const Vector3 &termination,
+							vector<ViewCell *> &viewcells)
+{
+	int hits = 0;
+
+	float mint = 0.0f, maxt = 1.0f;
+	const Vector3 dir = termination - origin;
+
+	stack<RayTraversalData > tStack;
+
+	Intersectable::NewMail();
+
+	//maxt += Limits::Threshold;
+
+	Vector3 entp = origin;
+	Vector3 extp = termination;
+
+	KdNode *node = mRoot;
+	KdNode *farChild;
+
+	float position;
+	int axis;
+
+	while (1)
+	{
+		if (!node->IsLeaf())
+		{
+			KdInterior *in = dynamic_cast<KdInterior *>(node);
+			position = in->mPosition;
+			axis = in->mAxis;
+
+			if (entp[axis] <= position)
+			{
+				if (extp[axis] <= position)
+				{
+					node = in->mBack;
+					// cases N1,N2,N3,P5,Z2,Z3
+					continue;
+				} 
+				else
+				{
+					// case N4
+					node = in->mBack;
+					farChild = in->mFront;
+				}
+			}
+			else
+			{
+				if (position <= extp[axis])
+				{
+					node = in->mFront;
+					// cases P1,P2,P3,N5,Z1
+					continue;
+				}
+				else
+				{
+					node = in->mFront;
+					farChild = in->mBack;
+					// case P4
+				}
+			}
+
+ 			// $$ modification 3.5.2004 - hints from Kamil Ghais
+			// case N4 or P4
+			float tdist = (position - origin[axis]) / dir[axis];
+			//tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
+			extp = origin + dir * tdist;
+			maxt = tdist;
+		}
+		else
+		{
+			// compute intersection with all objects in this leaf
+			KdLeaf *leaf = dynamic_cast<KdLeaf *>(node);
+
+			// add view cell to intersections
+			ViewCell *vc = leaf->mViewCell;
+
+			if (!vc->Mailed())
+			{
+				vc->Mail();
+				viewcells.push_back(vc);
+				++ hits;
+			}
+
+			// get the next node from the stack
+			if (tStack.empty())
+				break;
+
+			entp = extp;
+			mint = maxt;
+			
+			RayTraversalData &s  = tStack.top();
+			node = s.mNode;
+			extp = s.mExitPoint;
+			maxt = s.mMaxT;
+			tStack.pop();
+		}
+	}
+
+	return hits;
+}
+
+
 void
 KdTree::CollectObjects(KdNode *n, ObjectContainer &objects)
@@ -706,5 +810,5 @@
   }
   
-  return neighbors.size();
+  return (int)neighbors.size();
 }
 
@@ -768,4 +872,25 @@
 }
 
+void
+KdTree::CreateAndCollectViewCells(ViewCellContainer &vc) const
+{
+  stack<KdNode *> nodeStack;
+  nodeStack.push(mRoot);
+
+  while (!nodeStack.empty()) {
+    KdNode *node = nodeStack.top();
+    nodeStack.pop();
+    if (node->IsLeaf()) {
+      KdLeaf *leaf = (KdLeaf *)node;
+	  // kdtree used as view cell container => create view cell
+	  leaf->mViewCell = new KdViewCell();
+      vc.push_back(leaf->mViewCell);
+    } else {
+      KdInterior *interior = (KdInterior *)node;
+      nodeStack.push(interior->mBack);
+      nodeStack.push(interior->mFront);
+    }
+  }
+}
 
 int
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 469)
@@ -776,5 +776,5 @@
 
 		leaf->SetViewCell(viewCell);
-		viewCell->mBspLeaves.push_back(leaf);
+		viewCell->mLeaves.push_back(leaf);
 
 		//-- add pvs
@@ -2036,6 +2036,6 @@
 					vcStat.minPvs = pvsSize;
 
-				if ((int)viewCell->mBspLeaves.size() > vcStat.maxLeaves)
-					vcStat.maxLeaves = (int)viewCell->mBspLeaves.size();		
+				if ((int)viewCell->mLeaves.size() > vcStat.maxLeaves)
+					vcStat.maxLeaves = (int)viewCell->mLeaves.size();		
 			}
 		}
@@ -2176,5 +2176,5 @@
 void BspTree::ConstructGeometry(BspViewCell *vc, PolygonContainer &cell) const
 {
-	vector<BspLeaf *> leaves = vc->mBspLeaves;
+	vector<BspLeaf *> leaves = vc->mLeaves;
 
 	vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.h	(revision 469)
@@ -7,12 +7,13 @@
 #include "Statistics.h"
 //namespace GtpVisibilityPreprocessor {
-  
+
+struct Triangle3;
+
 class BspInterior;
 class BspPvs;
-struct Triangle3;
 class BspLeaf;
 class VspKdTree;
 class VspKdLeaf;
-
+class KdLeaf;
 
 /**
@@ -28,10 +29,11 @@
 	ViewCell(Mesh *mesh);
 
-
+	/** Default destructor.
+	*/
 	virtual ~ViewCell() {}
 	/** Returns Pvs. 
 	*/
-	const ViewCellPvs &GetPvs() const;
-	ViewCellPvs &GetPvs();
+	const ObjectPvs &GetPvs() const;
+	ObjectPvs &GetPvs();
 
 	int Type() const;
@@ -41,4 +43,11 @@
 	void AddPassingRay(const Ray &ray, const int contributions);	
 
+	/** Returns volume of the view cell.
+	*/
+	virtual float GetVolume() const;
+
+	/** Sets volume of the view cell.
+	*/
+	void SetVolume(float size);
 
 	/// Ray set description of the rays passing through this node.	
@@ -51,35 +60,50 @@
 
 	/// the potentially visible objects
-	ViewCellPvs mPvs;
+	ObjectPvs mPvs;
+
+	float mVolume;
 };
 
-class BspViewCell: public ViewCell
+/**
+	View cell belonging to a hierarchy.
+*/
+template<typename T>
+class HierarchyViewCell: public ViewCell
+{
+public:
+	HierarchyViewCell<T>(): mLeaves(0) {}
+	HierarchyViewCell<T>(Mesh *mesh): 
+		ViewCell(mesh), mLeaves(0) {}
+
+	/// Leaves of the hierarchy which are part of this view cell.
+	std::vector<T> mLeaves;
+};
+
+typedef HierarchyViewCell<BspLeaf *> BspViewCell;
+typedef HierarchyViewCell<KdLeaf *> KdViewCell;
+typedef HierarchyViewCell<VspKdLeaf *> VspKdViewCell;
+
+/*class BspViewCell: public ViewCell
 {
 public:
 	BspViewCell(): mBspLeaves(0) {}
 	BspViewCell(Mesh *mesh): 
-	ViewCell(mesh), mBspLeaves(0) {}
+		ViewCell(mesh), mBspLeaves(0) {}
 
-	/// Leaves which hold this view cell.
-	vector<BspLeaf *> mBspLeaves;
+	/// Leaves of the hierarchy which are part of this view cell.
+	std::vector<BspLeaf *> mBspLeaves;
 };
-
-class VspKdViewCell: public ViewCell
+*/
+/*class VspKdViewCell: public ViewCell
 {
 public:
-	VspKdViewCell(): mVspKdLeaves(0) {}
+	VspKdViewCell(): mLeaves(0) {}
 	VspKdViewCell(Mesh *mesh): 
-	ViewCell(mesh), mVspKdLeaves(0) {}
+		ViewCell(mesh), mLeaves(0) {}
 
-	float GetSize() {return mSize;}
-	void SetSize(float size) {mSize = size;}
-
-	/// Leaves which hold this view cell.
-	vector<VspKdLeaf *> mVspKdLeaves;
-
-protected:
-	float mSize;
+	/// Leaves of the hierarchy which are part of this view cell.
+	std::vector<VspKdLeaf *> mLeaves;
 };
-
+*/
 class ViewCellsStatistics: public StatisticsBase
 {
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 469)
@@ -10,4 +10,5 @@
 #include "VspBspTree.h"
 #include "VspKdTree.h"
+#include "RenderSimulator.h"
 
 Preprocessor::Preprocessor():
@@ -37,5 +38,5 @@
 
 	while(1) {
-		int npos = str.find(';', pos);
+		int npos = (int)str.find(';', pos);
 		
 		if (npos < 0 || npos - pos < 1)
@@ -175,4 +176,12 @@
 	int constructionSamples = 0;
 
+	float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
+
+	environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
+	environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
+	environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
+
+	mRenderSimulator = new RenderSimulator(objRenderCost, vcOverhead, moveSpeed);
+	
 	if (strcmp(viewCellsStr, "kdTree") == 0)
 	{
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp	(revision 469)
@@ -972,5 +972,5 @@
 
   if ( (int)(leaf->rays.size()) > stat.maxRayRefs)
-    stat.maxRayRefs = leaf->rays.size();
+    stat.maxRayRefs = (int)leaf->rays.size();
 
 }
@@ -1128,5 +1128,5 @@
   
   // update stats
-  stat.rayRefs -= leaf->rays.size();
+  stat.rayRefs -= (int)leaf->rays.size();
   stat.rayRefs += raysBack + raysFront;
 
@@ -1717,5 +1717,5 @@
 						  SimpleRayContainer &rays)
 {
-  int nrays = leaf->rays.size();
+  int nrays = (int)leaf->rays.size();
   for (int i=0; i < numberOfRays; i++) {
 	// pickup 3 random rays
@@ -1733,5 +1733,5 @@
 												   leaf->rays[r3].GetMaxT()));
 
-	const float overlap = 0.1;
+	const float overlap = 0.1f;
 		
 	Vector3 origin, direction;
@@ -1789,9 +1789,9 @@
 	  VssTreeLeaf *leaf = leaves[i];
 	  float c = leaf->GetImportance();
-	  int num = (c*ratioPerLeaf + 0.5);
+	  int num = (int)(c*ratioPerLeaf + 0.5f);
 	  GenerateLeafRays(leaf, num, rays);
 	}
 
-  return rays.size();
+  return (int)rays.size();
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 462)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 469)
@@ -11,4 +11,5 @@
 #include "Containers.h"
 #include "VssRay.h"
+#include "ViewCell.h"
 
 class SceneGraphNode;
@@ -21,5 +22,5 @@
 class VspBspTree;
 class BspNode;
-class VspKdViewCell;
+//class VspKdViewCell;
 
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Statistics.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Statistics.h	(revision 374)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Statistics.h	(revision 469)
@@ -34,5 +34,5 @@
 
   float Time() const {
-    return TimeDiff(startTime, stopTime);
+    return (float)TimeDiff(startTime, stopTime);
   }
 };
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 469)
@@ -412,10 +412,10 @@
 	box->Enlarge(box->Size()*Vector3(-s));
 	//	Vector3 translation = Vector3(-olds*0.2f, 0, 0);
-	Vector3 translation = Vector3(-0.05*olds, 0, 0);
+	Vector3 translation = Vector3(-0.05f*olds, 0, 0);
 	box->SetMin(box->Min() + translation);
 	box->SetMax(box->Max() + translation);
 
-	box->SetMin(1,  m + bsize*0.1);
-	box->SetMax(1,  m + bsize*0.6);
+	box->SetMin(1,  m + bsize*0.1f);
+	box->SetMax(1,  m + bsize*0.6f);
 
 	
@@ -425,6 +425,6 @@
 	float m = box->Min(1);
 	float bsize = box->Size(1);
-	box->SetMin(1,  m + bsize*0.2);
-	box->SetMax(1,  m + bsize*0.3);
+	box->SetMin(1,  m + bsize*0.2f);
+	box->SetMax(1,  m + bsize*0.3f);
   }
 
@@ -489,5 +489,5 @@
   
   cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
-  cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush;
+  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
   
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 469)
@@ -370,6 +370,6 @@
 
 	// sample city like heights
-	box->SetMin(1, box->Min(1) + box->Size(1)*0.2);
-	box->SetMax(1, box->Min(1) + box->Size(1)*0.3);
+	box->SetMin(1, box->Min(1) + box->Size(1)*0.2f);
+	box->SetMax(1, box->Min(1) + box->Size(1)*0.3f);
   }
 
@@ -439,5 +439,5 @@
 
   cout << "#totalPvsSize=" << mKdTree->CollectLeafPvs() << endl;
-  cout << "#totalRayStackSize=" << mVssRays.size() << endl <<flush;
+  cout << "#totalRayStackSize=" << (int)mVssRays.size() << endl <<flush;
 
   //int numExportRays = 10000;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h	(revision 407)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.h	(revision 469)
@@ -9,5 +9,5 @@
 #include "Ray.h"
 #include "Pvs.h"
-
+#include "Viewcell.h"
   
 class KdNode;
@@ -15,5 +15,5 @@
 class KdInterior;
 class Intersectable;
-
+//class KdViewCell;
 
 // --------------------------------------------------------------
@@ -157,10 +157,10 @@
 class KdLeaf : public KdNode {
 public:
-  KdLeaf(KdInterior *parent, const int objects):KdNode(parent) {
+  KdLeaf(KdInterior *parent, const int objects):
+	  KdNode(parent), mViewCell(NULL) {
     mObjects.reserve(objects);
   }
   
-  void AddPassingRay(const Ray &ray,
-										 const int contributions) {
+  void AddPassingRay(const Ray &ray, const int contributions) {
     mPassingRays.AddRay(ray, contributions);
 		//		Debug << "adding passing ray" << endl;
@@ -169,7 +169,7 @@
 	
 	void AddPassingRay2(const Ray &ray,
-											const int objects,
-											const int viewcells
-											) {
+						const int objects,
+						const int viewcells
+						) {
     mPassingRays.AddRay2(ray, objects, viewcells);
 		//		Debug << "adding passing ray" << endl;
@@ -183,7 +183,4 @@
   ObjectContainer mObjects;
   
-  /** pointers to viewcells contained in this node */
-  //  ViewCellContainer mViewCells;
-
   /** Ray set description of the rays passing through this node */
   PassingRaySet mPassingRays;
@@ -192,6 +189,7 @@
   KdPvs mKdPvs;
 	
-	/** PVS consisting of visible objects */
-  ViewCellPvs mPvs;
+  /** pointer to view cell.
+  */
+  KdViewCell *mViewCell;
 };
 
@@ -287,4 +285,13 @@
 	  );
 
+
+  /** Casts line segment into tree.
+	  @returns intersected view cells.
+  */
+  int CastLineSegment(const Vector3 &origin,
+					  const Vector3 &termination,
+					  vector<ViewCell *> &viewcells);
+
+
   const KdTreeStatistics &GetStatistics() const {
     return mStat;
@@ -297,4 +304,11 @@
   CollectLeaves(vector<KdLeaf *> &leaves);
 	
+  /** If the kd tree is used as view cell container, this
+	  methods creates the view cells.
+	  @returns the newly created view cells in a view cell container
+  */
+  void
+  CreateAndCollectViewCells(ViewCellContainer &viewCells) const;
+
   AxisAlignedBox3 GetBox(const KdNode *node) const {
     KdInterior *parent = node->mParent;
@@ -315,6 +329,6 @@
   KdNode *
   FindRandomNeighbor(KdNode *n,
-										 bool onlyUnmailed
-										 );
+					 bool onlyUnmailed
+					 );
   
   KdNode *
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 466)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 469)
@@ -75,8 +75,4 @@
 						  int &contributingSamples);
 
-	/** Adds objects samples to kd and bsp view cells.
-	*/
-	int AddObjectSamples(Intersectable *obj, const Ray &ray);
-
 	/**
 		Returns random node as target for the current sample ray.
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 465)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 469)
@@ -698,7 +698,7 @@
 bool X3dExporter::ExportVspKdTreeViewCell(const VspKdTree &tree, const VspKdViewCell &vc)
 {
-	vector<VspKdLeaf *>::const_iterator it, it_end = vc.mVspKdLeaves.end();
-
-	for (it = vc.mVspKdLeaves.begin(); it != it_end; ++ it)
+	vector<VspKdLeaf *>::const_iterator it, it_end = vc.mLeaves.end();
+
+	for (it = vc.mLeaves.begin(); it != it_end; ++ it)
 		ExportBox(tree.GetBBox(*it));
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp	(revision 311)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.cpp	(revision 469)
@@ -1,8 +1,4 @@
 #include <iostream>
 #include "Pvs.h"
-
-using namespace std;
-
-
 
 int
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 469)
@@ -33,8 +33,8 @@
 #include "RayInfo.h"
 #include "Containers.h"
+#include "ViewCell.h"
 
 class VspKdLeaf;
 class ViewCellsManager;
-class VspKdViewCell;
 class ViewCellsStatistics;
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 469)
@@ -75,8 +75,10 @@
 {
 	// cast ray to KD tree to find intersection with other objects
-  mKdTree->CastRay(ray);
-  VssRay vssRay(ray);
-  mViewCellsManager->ComputeSampleContributions(vssRay);
-  return vssRay.mPvsContribution;
+	mKdTree->CastRay(ray);
+	VssRay vssRay(ray);
+
+	mViewCellsManager->ComputeSampleContributions(vssRay);
+	
+	return vssRay.mPvsContribution;
 }
 
@@ -149,11 +151,11 @@
 		// pickup a random face of each mesh 
 		Mesh *mesh = mi->GetMesh();
-		int face = RandomValue(0, mesh->mFaces.size()-1);
+		int face = (int)Random((int)mesh->mFaces.size());
 		
 		Polygon3 poly(mesh->mFaces[face], mesh);
-		poly.Scale(1.001);
+		poly.Scale(1.001f);
 		// now extend a random edge of the face
-		int edge = RandomValue(0, poly.mVertices.size()-1);
-		float t = RandomValue(0.0f,1.0f);
+		int edge = Random((int)poly.mVertices.size());
+		float t = RandomValue(0.0f, 1.0f);
 		Vector3 target = t*poly.mVertices[edge] + (1.0f-t)*poly.mVertices[(edge + 1)%
 			poly.mVertices.size()];
@@ -190,5 +192,5 @@
 		Debug << "Finding random neighbour" << endl;	
 		for (int tries = 0; tries < 10; tries++) {
-			int index = RandomValue(0, pvsSize - 1);
+			int index = Random(pvsSize);
 			KdPvsData data;
 			KdNode *node;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 469)
@@ -679,5 +679,5 @@
   Vector3 ext = mMax - mMin;
  
-  return 2.0 * (ext.x * ext.y +
+  return 2.0f * (ext.x * ext.y +
 	        ext.x * ext.z +
 	        ext.y * ext.z);
@@ -1056,5 +1056,5 @@
   if (y < 0.0) {
     if (y < -1.0)
-      return c * (-2.0 + x / y); // for angle in <-PI/2, -PI/4)
+      return c * (-2.0f + x / y); // for angle in <-PI/2, -PI/4)
     else
       return c * (y / x); // for angle in <-PI/4 , 0>
@@ -1062,5 +1062,5 @@
   else {
     if (y > 1.0)
-      return c * (2.0 - x / y); // for angle in <PI/4, PI/2>
+      return c * (2.0f - x / y); // for angle in <PI/4, PI/2>
     else
       return c * (y / x); // for angle in <0, PI/2>
@@ -1107,6 +1107,6 @@
     //  FATAL_ABORT;
     //}
-    SA +=atan22(d*b/sqrt(b*b + d*d + 1.0)) - atan22(b*c/sqrt(b*b + c*c + 1.0))
-      - atan22(d*a/sqrt(a*a + d*d + 1.0)) + atan22(a*c/sqrt(a*a + c*c + 1.0));
+    SA +=atan22(d*b/sqrt(b*b + d*d + 1.0f)) - atan22(b*c/sqrt(b*b + c*c + 1.0f))
+      - atan22(d*a/sqrt(a*a + d*d + 1.0f)) + atan22(a*c/sqrt(a*a + c*c + 1.0f));
   }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 439)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.h	(revision 469)
@@ -95,6 +95,4 @@
   
   void CastRay(const BspTree &tree, const VssRay & vssRay);
-	
-  int AddObjectSamples(Intersectable *obj, const Ray &ray);
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 468)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 469)
@@ -40,17 +40,6 @@
 {
 	DEL_PTR(mRenderSimulator);
-}
-
-void ViewCellsManager::InitRenderSimulator()
-{
-	float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
-
-	environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
-	environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
-	environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
-
-	mRenderSimulator->SetObjectRenderCost(objRenderCost);
-	mRenderSimulator->SetVcOverhead(vcOverhead);
-	mRenderSimulator->SetMoveSpeed(moveSpeed);
+
+	CLEAR_CONTAINER(mViewCells);
 }
 
@@ -67,16 +56,16 @@
 }
 
-void
-ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays
-											 )
-{
-  // view cells not yet constructed
-  if (!ViewCellsConstructed())
-	return;
+void ViewCellsManager::ComputeSampleContributions(const VssRayContainer &rays)
+{
+	// view cells not yet constructed
+	if (!ViewCellsConstructed())
+		return;
   
-  VssRayContainer::const_iterator it, it_end = rays.end();
-  for (it = rays.begin(); it != it_end; ++ it) {
-	ComputeSampleContributions(*(*it));
-  }
+	VssRayContainer::const_iterator it, it_end = rays.end();
+	
+	for (it = rays.begin(); it != it_end; ++ it) 
+	{
+		ComputeSampleContributions(*(*it));
+	}
 }
 
@@ -271,9 +260,27 @@
 
 
+void ViewCellsManager::GetRaySets(const VssRayContainer &sourceRays,
+								  VssRayContainer &constructionRays, 
+								  VssRayContainer &savedRays) const
+{
+	const int limit = min (mConstructionSamples, (int)sourceRays.size());
+
+	VssRayContainer::const_iterator it, it_end = sourceRays.end();
+
+	for (it = sourceRays.begin(); it != it_end; ++ it)
+	{
+		if (Random((int)constructionRays.size()) > limit)
+			constructionRays.push_back(*it);
+		else
+			savedRays.push_back(*it);
+	}
+}
+
 /**********************************************************************/
 /*                   BspViewCellsManager implementation               */
 /**********************************************************************/
 
-BspViewCellsManager::BspViewCellsManager(BspTree *bspTree, int constructionSamples):
+BspViewCellsManager::BspViewCellsManager(BspTree *bspTree, 
+										 int constructionSamples):
 ViewCellsManager(constructionSamples), 
 mBspTree(bspTree)
@@ -281,12 +288,4 @@
 }
 
-
-BspViewCellsManager::~BspViewCellsManager()
-{
-	ViewCellContainer vc;
-	mBspTree->CollectViewCells(vc);
-
-	CLEAR_CONTAINER(vc);
-}
 
 bool BspViewCellsManager::ViewCellsConstructed() const
@@ -310,29 +309,46 @@
 	int sampleContributions = 0;
 	
-	RayContainer sampleRays;
-
-	int limit = min (mConstructionSamples, (int)rays.size());
-
 	// construct view cells using the collected samples
-	Debug << "construcing bsp view cells from " 
-		  << limit << " samples " << endl;
-
-    for (int i = 0; i < limit; ++ i)
-		sampleRays.push_back(new Ray(*rays[i]));
-
-	if (mViewCells.empty()) { // no view cells loaded 
-	  mBspTree->Construct(objects, sampleRays);
-	  mBspTree->CollectViewCells(mViewCells);
-	} else
-	  mBspTree->Construct(mViewCells);
-	
-	
+	RayContainer constructionRays;
+	VssRayContainer savedRays;
+
+	const int limit = min(mConstructionSamples, (int)rays.size());
+
+	VssRayContainer::const_iterator it, it_end = rays.end();
+
+	for (it = rays.begin(); it != it_end; ++ it)
+	{
+		if (Random((int)constructionRays.size()) > limit)
+			constructionRays.push_back(new Ray(*(*it)));
+		else
+			savedRays.push_back(*it);
+	}
+
+    if (mViewCells.empty()) 
+	{ 
+		// no view cells loaded 
+		mBspTree->Construct(objects, constructionRays);
+
+		// collect final view cells
+		mBspTree->CollectViewCells(mViewCells);
+	} 
+	else
+	{
+		mBspTree->Construct(mViewCells);
+	}
+
+	// destroy rays created only for construction
+	CLEAR_CONTAINER(constructionRays);
+
 	Debug << mBspTree->GetStatistics() << endl;
 	
+	// recast rest of the rays
+	ComputeSampleContributions(savedRays);
+
 	return sampleContributions;
 }
 
 
-float BspViewCellsManager::GetProbability(ViewCell *viewCell)
+float BspViewCellsManager::GetProbability(ViewCell *viewCell) const
 {
 	PolygonContainer geom;
@@ -350,5 +366,5 @@
 
 
-float BspViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost)
+float BspViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
 {
 	return viewCell->GetPvs().GetSize() * objRendercost;
@@ -367,5 +383,5 @@
 									 const VssRayContainer &rays)
 {
-  if (!ViewCellsConstructed())
+	if (!ViewCellsConstructed())
 	{
 		Debug << "view cells not constructed" << endl;
@@ -484,38 +500,38 @@
 									const VssRayContainer &sampleRays)
 {
-  if (!ViewCellsConstructed())
-	return;
+	if (!ViewCellsConstructed())
+		return;
   
-  //-- recount pvs
-  ViewCellsStatistics vcStats;
-  mBspTree->EvaluateViewCellsStats(vcStats);
+	//-- recount pvs
+	ViewCellsStatistics vcStats;
+	mBspTree->EvaluateViewCellsStats(vcStats);
   
-  if (1) // export view cells
-	{
-	  cout << "exporting view cells after merge ... ";
-	  Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
+	if (1) // export view cells
+	{
+		cout << "exporting view cells after merge ... ";
+		Exporter *exporter = Exporter::GetExporter("merged_view_cells.x3d");
 	  
-	  if (exporter)
+		if (exporter)
 		{
-		  exporter->ExportBspViewCellPartition(*mBspTree, vcStats.maxPvs);
-		  //exporter->ExportBspViewCellPartition(*mBspTree, 0);
-		  delete exporter;
+			exporter->ExportBspViewCellPartition(*mBspTree, vcStats.maxPvs);
+			//exporter->ExportBspViewCellPartition(*mBspTree, 0);
+			delete exporter;
 		}
 	  
-	  cout << "finished" << endl;
+		cout << "finished" << endl;
 	}	
   
-  //-- visualization of the BSP splits
-  bool exportSplits = false;
-  environment->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
+	//-- visualization of the BSP splits
+	bool exportSplits = false;
+	environment->GetBoolValue("BspTree.Visualization.exportSplits", exportSplits);
   
-  if (exportSplits)
-	{
-	  cout << "exporting splits ... ";
-	  ExportSplits(objects, sampleRays);
-	  cout << "finished" << endl;
+	if (exportSplits)
+	{
+		cout << "exporting splits ... ";
+		ExportSplits(objects, sampleRays);
+		cout << "finished" << endl;
 	}
   
-  ExportBspPvs(objects, sampleRays);
+	ExportBspPvs(objects, sampleRays);
 }
 
@@ -546,23 +562,28 @@
 		exporter->ResetForcedMaterial();
 		
+		bool exportRays = false;
+		bool exportGeometry = false;
+
+		environment->GetBoolValue("BspViewCellsManager.Visualization.exportRays", exportRays);
+		environment->GetBoolValue("BspViewCellsManager.Visualization.exportGeometry", exportGeometry);
+
 		// export rays
-		if (0)
+		if (exportRays)
 		{
-		  VssRayContainer outRays;
+			VssRayContainer outRays;
 		  
-		  int raysSize = min((int)sampleRays.size(), mVisualizationSamples);
-		  
-		  for (int i = 0; i < raysSize; ++ i)
+			int raysSize = min((int)sampleRays.size(), mVisualizationSamples);
+			for (int i = 0; i < raysSize; ++ i)
 			{
-			  // only rays piercing geometry
-			  outRays.push_back(sampleRays[i]);
+				// only rays piercing geometry
+				outRays.push_back(sampleRays[i]);			
 			}
-		  
-		  // export rays 
-		  exporter->ExportRays(outRays, RgbColor(1, 1, 0));
+		  	
+			// export rays 
+			exporter->ExportRays(outRays, RgbColor(1, 1, 0));
 		}
 		
-		if (0)
-		  exporter->ExportGeometry(objects);
+		if (exportGeometry)
+			exporter->ExportGeometry(objects);
 		
 		delete exporter;
@@ -581,39 +602,36 @@
   Debug << "visualization using " << raysOut << " samples" << endl;
 
-  //$$ JB
-#if 0
-
   if (1)
-	{
+  {
 	  //-- some random view cells and rays for output
 	  vector<BspLeaf *> bspLeaves;
 	  
 	  for (int i = 0; i < leafOut; ++ i)
-		bspLeaves.push_back(mBspTree->GetRandomLeaf());	
+		  bspLeaves.push_back(mBspTree->GetRandomLeaf());	
 
 	  
 	  for (int i = 0; i < bspLeaves.size(); ++ i)
-		{
+	  {
 		  BspLeaf *leaf = bspLeaves[i];
-		  
 		  RayContainer vcRays;
 		  
 		  cout << "creating output for view cell " << i << " ... ";
 		  
+		  // TODO matt
 		  // check whether we can add the current ray to the output rays
-		  for (int k = 0; k < raysOut; ++ k) 
-			{
+		  /*for (int k = 0; k < raysOut; ++ k) 
+		  {
 			  Ray *ray = sampleRays[k];
 			  
-			  for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
-				{
+			  for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+			  {
 				  BspLeaf *leaf2 = ray->bspIntersections[j].mLeaf;
 				  
 				  if (leaf->GetViewCell() == leaf2->GetViewCell()) 
-					{
+				  {
 					  vcRays.push_back(ray);
-					}
-				}
-			}
+				  }
+			  }
+		  }*/
 		  
 		  Intersectable::NewMail();
@@ -635,19 +653,19 @@
 		  
 		  if (vc->GetMesh())
-			exporter->ExportViewCell(vc);
+			  exporter->ExportViewCell(vc);
 		  else
-			{
+		  {
 			  PolygonContainer vcGeom;
 			  // export view cell geometry
 			  mBspTree->ConstructGeometry(vc, vcGeom);
 			  exporter->ExportPolygons(vcGeom);
-				CLEAR_CONTAINER(vcGeom);
-			}
-		  
+			  CLEAR_CONTAINER(vcGeom);
+		  }		  
+
 		  Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
 				<< ", piercing rays=" << (int)vcRays.size() << endl;
 		  
 		  // export rays piercing this view cell
-		  exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));	
+		  //exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));	
 		  
 		  m.mDiffuseColor = RgbColor(1, 0, 0);
@@ -657,22 +675,22 @@
 		  exporter->SetFilled();
 		  
-		  ViewCellPvsMap::iterator it, it_end = vc->GetPvs().mEntries.end();
+		  ObjectPvsMap::iterator it, it_end = vc->GetPvs().mEntries.end();
 		  // output PVS of view cell
 		  for (it = vc->GetPvs().mEntries.begin(); it !=  it_end; ++ it) 
-			{
+		  {
 			  Intersectable *intersect = (*it).first;
 			  if (!intersect->Mailed())
-				{
+			  {
 				  exporter->ExportIntersectable(intersect);
 				  intersect->Mail();
-				}			
-			}
+			  }			
+		  }
 		  
 		  DEL_PTR(exporter);
 		  cout << "finished" << endl;
-		}
-	}
+	  }
+  }
   else
-	{
+  {
 	  ViewCellContainer viewCells;
 	  RayContainer vcRays;
@@ -681,86 +699,81 @@
 	  stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
 	  
-	  int limit = min(leafOut, (int)viewCells.size()); 
+	  const int limit = min(leafOut, (int)viewCells.size()); 
 	  
 	  for (int i = 0; i < limit; ++ i)
-		{
+	  {
 		  cout << "creating output for view cell " << i << " ... ";
 			
-            Intersectable::NewMail();
-			BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
-
-			cout << "creating output for view cell " << i << " ... ";
-			// check whether we can add the current ray to the output rays
-			for (int k = 0; k < raysOut; ++ k) 
-			{
-				Ray *ray = sampleRays[k];
-
-				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
-				{
-					BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
-
-					if (vc == leaf->GetViewCell()) 
-					{
-						vcRays.push_back(ray);
-					}
-				}
-			}
-
-			//bspLeaves[j]->Mail();
-			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
-
-			Exporter *exporter = Exporter::GetExporter(s);
-			
-			exporter->SetWireframe();
-
-			Material m;//= RandomMaterial();
-			m.mDiffuseColor = RgbColor(0, 1, 0);
-			exporter->SetForcedMaterial(m);
-
-			if (vc->GetMesh())
-				exporter->ExportViewCell(vc);
-			else
-			{
-				PolygonContainer cell;
-				// export view cell
-				mBspTree->ConstructGeometry(vc, cell);
-				exporter->ExportPolygons(cell);
-				CLEAR_CONTAINER(cell);
-			}
-
-			
-			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
-				  << ", piercing rays=" << (int)vcRays.size() << endl;
-
-			
-			// export rays piercing this view cell
-			exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));
-	
-			m.mDiffuseColor = RgbColor(1, 0, 0);
-			exporter->SetForcedMaterial(m);
-
-			ViewCellPvsMap::const_iterator it,
-				it_end = vc->GetPvs().mEntries.end();
-
-			// output PVS of view cell
-			for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it) 
-			{
-				Intersectable *intersect = (*it).first;
-				if (!intersect->Mailed())
-				{
-					Material m = RandomMaterial();
-			
-					exporter->SetForcedMaterial(m);
-
-					exporter->ExportIntersectable(intersect);
-					intersect->Mail();
-				}			
-			}
+		  Intersectable::NewMail();
+		  BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
+
+		  cout << "creating output for view cell " << i << " ... ";
+
+		  // TODO matt
+		  // check whether we can add the current ray to the output rays
+		  /*for (int k = 0; k < raysOut; ++ k) 
+		  {
+			  Ray *ray = sampleRays[k];
+
+			  for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+			  {
+				  BspLeaf *leaf = ray->bspIntersections[j].mLeaf;
+
+				  if (vc == leaf->GetViewCell()) 
+					  vcRays.push_back(ray);
+			  }
+		  }*/
+
+		  //bspLeaves[j]->Mail();
+		  char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
+
+		  Exporter *exporter = Exporter::GetExporter(s);	
+		  exporter->SetWireframe();
+
+		  Material m;//= RandomMaterial();
+		  m.mDiffuseColor = RgbColor(0, 1, 0);
+		  exporter->SetForcedMaterial(m);
+
+		  if (vc->GetMesh())
+			  exporter->ExportViewCell(vc);
+		  else
+		  {
+			  PolygonContainer cell;
+			  // export view cell
+			  mBspTree->ConstructGeometry(vc, cell);
+			  exporter->ExportPolygons(cell);
+			  CLEAR_CONTAINER(cell);
+		  }
+		  Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
+			    << ", piercing rays=" << (int)vcRays.size() << endl;
+
+		  // export rays piercing this view cell
+		  exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));
+	
+		  m.mDiffuseColor = RgbColor(1, 0, 0);
+		  exporter->SetForcedMaterial(m);
+
+		  ObjectPvsMap::const_iterator it, it_end = vc->GetPvs().mEntries.end();
+
+		  // output PVS of view cell
+		  for (it = vc->GetPvs().mEntries.begin(); it != it_end; ++ it) 
+		  {
+			  Intersectable *intersect = (*it).first;
+
+			  if (!intersect->Mailed())
+			  {
+				  Material m = RandomMaterial();
+				  exporter->SetForcedMaterial(m);
+
+				  exporter->ExportIntersectable(intersect);
+			
+				  intersect->Mail();
+			  }			
+		  }
 				
-			DEL_PTR(exporter);
-			cout << "finished" << endl;
-		}
-	}
-#endif
+		  DEL_PTR(exporter);
+		  cout << "finished" << endl;
+	  }
+  }
 }
 
@@ -778,6 +791,6 @@
 	BspViewCell *bVc = back->GetViewCell();
 
-	vector<BspLeaf *> fLeaves = fVc->mBspLeaves;
-	vector<BspLeaf *> bLeaves = bVc->mBspLeaves;
+	vector<BspLeaf *> fLeaves = fVc->mLeaves;
+	vector<BspLeaf *> bLeaves = bVc->mLeaves;
 
 	vector<BspLeaf *>::const_iterator it;
@@ -786,10 +799,10 @@
 	{
 		(*it)->SetViewCell(viewCell);
-		viewCell->mBspLeaves.push_back(*it);
+		viewCell->mLeaves.push_back(*it);
 	}
 	for (it = bLeaves.begin(); it != bLeaves.end(); ++ it)
 	{
 		(*it)->SetViewCell(viewCell);
-		viewCell->mBspLeaves.push_back(*it);
+		viewCell->mLeaves.push_back(*it);
 	}
 	
@@ -836,19 +849,21 @@
 
 KdViewCellsManager::KdViewCellsManager(KdTree *kdTree):
-ViewCellsManager(), 
-mKdTree(kdTree), 
-mKdPvsDepth(100)
-{
-}
-
-float KdViewCellsManager::GetProbability(ViewCell *viewCell)
-{
-	return 0;
-}
-
-
-float KdViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost)
-{
-	return 0;
+ViewCellsManager(), mKdTree(kdTree), mKdPvsDepth(100)
+{
+}
+
+float KdViewCellsManager::GetProbability(ViewCell *viewCell) const
+{
+	KdViewCell *vc = dynamic_cast<KdViewCell *>(viewCell);
+
+	AxisAlignedBox3 box = mKdTree->GetBox(vc->mLeaves[0]);
+
+	return box.SurfaceArea();
+}
+
+
+float KdViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
+{
+	return viewCell->GetPvs().GetSize() * objRendercost;
 }
 
@@ -861,7 +876,8 @@
 		return 0;
 
-	Debug << "Constructing bsp view cells" << endl;
-	
 	mKdTree->Construct();
+
+	// create the view cells
+	mKdTree->CreateAndCollectViewCells(mViewCells);
 
 	return 0;
@@ -885,77 +901,175 @@
 		return;
 
+	// using view cells instead of the kd PVS of objects
+	const bool useViewCells = true;
+	bool exportRays = false;
+
+	int limit = min(mVisualizationSamples, (int)sampleRays.size());
 	const int pvsOut = min((int)objects.size(), 10);
-
-	int limit = min(mVisualizationSamples, (int)sampleRays.size());
-
 	VssRayContainer *rays = new VssRayContainer[pvsOut];
 
-	for  (int i = 0; i < limit; ++ i)
-	{
-	  VssRay *ray = sampleRays[i];
-	  
-	  // check whether we can add this to the rays
-	  for (int j = 0; j < pvsOut; j++) 
+  //$$ JB
+//#if 0
+
+	if (useViewCells)
+	{
+		const int leafOut = 10;
+		
+		ViewCell::NewMail();
+  
+		//-- some rays for output
+		const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
+		Debug << "visualization using " << raysOut << " samples" << endl;
+
+		//-- some random view cells and rays for output
+		vector<KdLeaf *> kdLeaves;
+		
+		for (int i = 0; i < leafOut; ++ i)
+			kdLeaves.push_back(dynamic_cast<KdLeaf *>(mKdTree->GetRandomLeaf()));
+		
+		for (int i = 0; i < kdLeaves.size(); ++ i)
 		{
-		  if (objects[j] == ray->mTerminationObject)
+			KdLeaf *leaf = kdLeaves[i];
+			RayContainer vcRays;
+			
+			cout << "creating output for view cell " << i << " ... ";
+			
+			// check whether we can add the current ray to the output rays
+			/*for (int k = 0; k < raysOut; ++ k) 
 			{
-			  rays[j].push_back(ray);
+				Ray *ray = sampleRays[k];
+				
+				for (int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+				{
+					BspLeaf *leaf2 = ray->bspIntersections[j].mLeaf;
+					
+					if (leaf->GetViewCell() == leaf2->GetViewCell()) 
+					{
+						vcRays.push_back(ray);
+					}
+				}
+			}*/
+			
+			Intersectable::NewMail();
+			
+			ViewCell *vc = leaf->mViewCell;
+			
+			//bspLeaves[j]->Mail();
+			char s[64]; sprintf(s, "kd-pvs%04d.x3d", i);
+			
+			Exporter *exporter = Exporter::GetExporter(s);
+			exporter->SetFilled();
+
+			exporter->SetWireframe();
+			//exporter->SetFilled();
+			
+			Material m;//= RandomMaterial();
+			m.mDiffuseColor = RgbColor(1, 1, 0);
+			exporter->SetForcedMaterial(m);
+			
+			AxisAlignedBox3 box = mKdTree->GetBox(leaf);
+			exporter->ExportBox(box);
+
+			Debug << i << ": pvs size=" << (int)vc->GetPvs().GetSize() 
+				<< ", piercing rays=" << (int)vcRays.size() << endl;
+			
+			// export rays piercing this view cell
+			exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));	
+			
+			m.mDiffuseColor = RgbColor(1, 0, 0);
+			exporter->SetForcedMaterial(m);
+			
+			// exporter->SetWireframe();
+			exporter->SetFilled();
+			
+			ObjectPvsMap::iterator it, it_end = vc->GetPvs().mEntries.end();
+			// output PVS of view cell
+			for (it = vc->GetPvs().mEntries.begin(); it !=  it_end; ++ it) 
+			{
+				Intersectable *intersect = (*it).first;
+				if (!intersect->Mailed())
+				{
+					exporter->ExportIntersectable(intersect);
+					intersect->Mail();
+				}			
+			}
+			
+			DEL_PTR(exporter);
+			cout << "finished" << endl;
+		}
+
+		DEL_PTR(rays);
+	}
+	else // using kd PVS of objects
+	{
+		for (int i = 0; i < limit; ++ i)
+		{
+			VssRay *ray = sampleRays[i];
+		
+			// check whether we can add this to the rays
+			for (int j = 0; j < pvsOut; j++) 
+			{
+				if (objects[j] == ray->mTerminationObject)
+				{
+					rays[j].push_back(ray);
+				}
 			}
 		}
-	}
-	
-
-	bool exportRays = false;
-	if (exportRays) {
-	  Exporter *exporter = NULL;
-		exporter = Exporter::GetExporter("sample-rays.x3d");
-		exporter->SetWireframe();
-		exporter->ExportKdTree(*mKdTree);
-		
-		for (i=0; i < pvsOut; i++) 
-			exporter->ExportRays(rays[i], RgbColor(1, 0, 0));
-		exporter->SetFilled();
-		
-		delete exporter;
-	}
-
-	for (int k=0; k < pvsOut; k++) 
-	{
-		Intersectable *object = objects[k];
-		char s[64];	
-		sprintf(s, "sample-pvs%04d.x3d", k);
-
-		Exporter *exporter = Exporter::GetExporter(s);
-		exporter->SetWireframe();
-	 
-        KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
-		Intersectable::NewMail();
-			
-		// avoid adding the object to the list
-		object->Mail();
-		ObjectContainer visibleObjects;
-            
-		for (; i != object->mKdPvs.mEntries.end(); i++) 
+
+		if (exportRays) 
 		{
-			 KdNode *node = (*i).first;
-			 exporter->ExportBox(mKdTree->GetBox(node));
-			 mKdTree->CollectObjects(node, visibleObjects);
+			Exporter *exporter = NULL;
+			exporter = Exporter::GetExporter("sample-rays.x3d");
+			exporter->SetWireframe();
+			exporter->ExportKdTree(*mKdTree);
+		
+			for (i=0; i < pvsOut; i++) 
+				exporter->ExportRays(rays[i], RgbColor(1, 0, 0));
+
+			exporter->SetFilled();
+		
+			delete exporter;
 		}
 
-		exporter->ExportRays(rays[k],  RgbColor(0, 1, 0));
-		exporter->SetFilled();
-		 
-		for (int j = 0; j < visibleObjects.size(); j++)
-			exporter->ExportIntersectable(visibleObjects[j]);
-		 
-		Material m;
-		m.mDiffuseColor = RgbColor(1, 0, 0);
-		exporter->SetForcedMaterial(m);
-		exporter->ExportIntersectable(object);
-	 
-		delete exporter;
-	}
-	DEL_PTR(rays);
-}
+		for (int k=0; k < pvsOut; k++) 
+		{
+			Intersectable *object = objects[k];
+			char s[64];	
+			sprintf(s, "sample-pvs%04d.x3d", k);
+
+			Exporter *exporter = Exporter::GetExporter(s);
+			exporter->SetWireframe();
+		
+			KdPvsMap::iterator i = object->mKdPvs.mEntries.begin();
+			Intersectable::NewMail();
+			
+			// avoid adding the object to the list
+			object->Mail();
+			ObjectContainer visibleObjects;
+	    
+			for (; i != object->mKdPvs.mEntries.end(); i++) 
+			{
+				KdNode *node = (*i).first;
+				exporter->ExportBox(mKdTree->GetBox(node));
+		
+				mKdTree->CollectObjects(node, visibleObjects);
+			}
+		
+			exporter->ExportRays(rays[k],  RgbColor(0, 1, 0));
+			exporter->SetFilled();
+			
+			for (int j = 0; j < visibleObjects.size(); j++)
+				exporter->ExportIntersectable(visibleObjects[j]);
+			
+			Material m;
+			m.mDiffuseColor = RgbColor(1, 0, 0);
+			exporter->SetForcedMaterial(m);
+			exporter->ExportIntersectable(object);
+		
+			delete exporter;
+		}
+	}		
+}
+
 
 int KdViewCellsManager::GetType() const
@@ -981,13 +1095,12 @@
 
 
-int
-KdViewCellsManager::CastLineSegment(const Vector3 &origin,
-									const Vector3 &termination,
-									ViewCellContainer &viewcells
-									)
-{
-
-  return 0;
-}
+int KdViewCellsManager::CastLineSegment(const Vector3 &origin,
+										const Vector3 &termination,
+										ViewCellContainer &viewcells)
+{
+	return mKdTree->CastLineSegment(origin, termination, viewcells);
+}
+
+
 
 /**********************************************************************/
@@ -1003,7 +1116,7 @@
 }
 
-float VspKdViewCellsManager::GetProbability(ViewCell *viewCell)
-{
-	/*AxisAlignedBox3 box = mKdTree->GetBox(*it);
+float VspKdViewCellsManager::GetProbability(ViewCell *viewCell) const
+{
+/*	AxisAlignedBox3 box = mKdTree->GetBox(*it);
 			
 	// volume or area substitutes for view point probability
@@ -1011,21 +1124,13 @@
 		return box.GetVolume(); 
 	else
-		return box.SurfaceArea();*/
-
-	return dynamic_cast<VspKdViewCell *>(viewCell)->GetSize();
-}
-
-
-float VspKdViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost)
-{
-	return 0;//leaf->mKdPvs.GetSize() * objRendercost;
-}
-
-VspKdViewCellsManager::~VspKdViewCellsManager()
-{
-	ViewCellContainer vc;
-	mVspKdTree->CollectViewCells(vc);
-
-	CLEAR_CONTAINER(vc);
+		return box.SurfaceArea();
+*/
+	return dynamic_cast<VspKdViewCell *>(viewCell)->GetVolume();
+}
+
+
+float VspKdViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
+{
+	return viewCell->GetPvs().GetSize() * objRendercost;
 }
 
@@ -1039,17 +1144,26 @@
 		return 0;
 		
-	VssRayContainer sampleRays;
-
-	int limit = min (mConstructionSamples, (int)rays.size());
-
-    for (int i = 0; i < limit; ++ i)
-		sampleRays.push_back(rays[i]);
-
-	Debug << "constructing vsp kd tree using " << (int)sampleRays.size() << " samples" << endl;
-	mVspKdTree->Construct(rays, sceneBbox);
+	VssRayContainer constructionRays;
+	VssRayContainer savedRays;
+
+	GetRaySets(rays, constructionRays, savedRays);
+	
+	Debug << "constructing vsp kd tree using " 
+		  << (int)constructionRays.size() << " samples" << endl;
+
+	mVspKdTree->Construct(constructionRays, sceneBbox);
+
+	// collect view cells
+	mVspKdTree->CollectViewCells(mViewCells);
 
 	Debug << mVspKdTree->GetStatistics() << endl;
 
-	return 0;
+	// finally merge kd leaf building blocks to view cells
+	const int merged = mVspKdTree->MergeLeaves();
+
+	// recast rest of rays
+	ComputeSampleContributions(savedRays);
+
+	return merged;
 }
 
@@ -1071,5 +1185,5 @@
 		return 0;
 
-	return mVspKdTree->MergeLeaves();
+	return 0;
 }
 
@@ -1077,8 +1191,12 @@
 									  const VssRayContainer &sampleRays)
 {
+	bool exportRays = false;
+	bool exportGeometry = false;
+
+	environment->GetBoolValue("BspViewCellsManager.Visualization.exportRays", exportRays);
+	environment->GetBoolValue("BspViewCellsManager.Visualization.exportGeometry", exportGeometry);
+
 	if (!ViewCellsConstructed())
 		return;
-
-	bool exportRays = true;
 
 	//-- export tree leaves
@@ -1173,4 +1291,5 @@
 		exporter->ExportGeometry(objects);
 	}
+
 	if (exportRays) 
 	{
@@ -1207,12 +1326,9 @@
 
 
-int
-VspKdViewCellsManager::CastLineSegment(const Vector3 &origin,
-									   const Vector3 &termination,
-									   ViewCellContainer &viewcells
-									   )
-{
-  
-  return 0;
+int VspKdViewCellsManager::CastLineSegment(const Vector3 &origin,
+										   const Vector3 &termination,
+										   ViewCellContainer &viewcells)
+{
+	return mVspKdTree->CastLineSegment(origin, termination, viewcells);
 }
 
@@ -1228,5 +1344,5 @@
 }
 
-float VspBspViewCellsManager::GetProbability(ViewCell *viewCell)
+float VspBspViewCellsManager::GetProbability(ViewCell *viewCell) const
 {
 	PolygonContainer geom;
@@ -1244,16 +1360,9 @@
 
 
-float VspBspViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost)
+float VspBspViewCellsManager::GetRendercost(ViewCell *viewCell, float objRendercost) const
 {
 	return viewCell->GetPvs().GetSize() * objRendercost;
 }
 
-VspBspViewCellsManager::~VspBspViewCellsManager()
-{
-	ViewCellContainer vc;
-	mVspBspTree->CollectViewCells(vc);
-
-	CLEAR_CONTAINER(vc);
-}
 
 bool VspBspViewCellsManager::ViewCellsConstructed() const
@@ -1280,15 +1389,16 @@
 	int sampleContributions = 0;
 	
-	VssRayContainer sampleRays;
-
-	int limit = min (mConstructionSamples, (int)rays.size());
-
-    for (int i = 0; i < limit; ++ i)
-		sampleRays.push_back(rays[i]);
-
-	mVspBspTree->Construct(sampleRays);
+	VssRayContainer constructionRays;
+	VssRayContainer savedRays;
+
+	GetRaySets(rays, constructionRays, savedRays);
+
+	mVspBspTree->Construct(constructionRays);
 	mVspBspTree->CollectViewCells(mViewCells);
 	
 	Debug << mVspBspTree->GetStatistics() << endl;
+	
+	// recast rest of rays
+	ComputeSampleContributions(savedRays);
 
 	return sampleContributions;
@@ -1488,4 +1598,10 @@
 										  const VssRayContainer &sampleRays)
 {
+	bool exportRays = false;
+	bool exportGeometry = false;
+
+	environment->GetBoolValue("BspViewCellsManager.Visualization.exportRays", exportRays);
+	environment->GetBoolValue("BspViewCellsManager.Visualization.exportGeometry", exportGeometry);
+
 	const int leafOut = 10;
 	
@@ -1506,13 +1622,13 @@
 		for (int i = 0; i < (int)vspBspLeaves.size(); ++ i)
 		{
-		  VssRayContainer vcRays;
-		  cout << "creating output for view cell " << i << " ... ";
-
-		  //$$JB
+			VssRayContainer vcRays;
+			cout << "creating output for view cell " << i << " ... ";
+
+			//$$JB
 #if 0
 			// check whether we can add the current ray to the output rays
 			for (int k = 0; k < raysOut; ++ k) 
 			{
-			  VssRay *ray = sampleRays[k];
+				VssRay *ray = sampleRays[k];
 
 				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
@@ -1539,5 +1655,5 @@
 			exporter->SetFilled();
 
-			ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
+			ObjectPvsMap::iterator it = vc->GetPvs().mEntries.begin();
 
 			exporter->SetWireframe();
@@ -1564,12 +1680,15 @@
 
 			// export rays piercing this view cell
-			exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
-			exporter->ExportRays(vspBspLeaves[i]->mVssRays, RgbColor(1, 1, 1));
-			
+			if (exportRays)
+			{
+				//exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
+				exporter->ExportRays(vspBspLeaves[i]->mVssRays, RgbColor(1, 1, 1));
+			}
+
 			m.mDiffuseColor = RgbColor(0, 1, 1);
 			exporter->SetForcedMaterial(m);
 
-			exporter->SetWireframe();
-			//exporter->SetFilled();
+			//exporter->SetWireframe();
+			exporter->SetFilled();
 
 			// output PVS of view cell
@@ -1585,5 +1704,5 @@
 				
 			// output rest of the objects
-			if (0)
+			if (exportGeometry)
 			{
 				Material m;//= RandomMaterial();
@@ -1671,5 +1790,5 @@
 			exporter->SetForcedMaterial(m);
 
-			ViewCellPvsMap::const_iterator it,
+			ObjectPvsMap::const_iterator it,
 				it_end = vc->GetPvs().mEntries.end();
 
@@ -1720,6 +1839,6 @@
 	BspViewCell *bVc = back->GetViewCell();
 
-	vector<BspLeaf *> fLeaves = fVc->mBspLeaves;
-	vector<BspLeaf *> bLeaves = bVc->mBspLeaves;
+	vector<BspLeaf *> fLeaves = fVc->mLeaves;
+	vector<BspLeaf *> bLeaves = bVc->mLeaves;
 
 	vector<BspLeaf *>::const_iterator it;
@@ -1728,10 +1847,10 @@
 	{
 		(*it)->SetViewCell(viewCell);
-		viewCell->mBspLeaves.push_back(*it);
+		viewCell->mLeaves.push_back(*it);
 	}
 	for (it = bLeaves.begin(); it != bLeaves.end(); ++ it)
 	{
 		(*it)->SetViewCell(viewCell);
-		viewCell->mBspLeaves.push_back(*it);
+		viewCell->mLeaves.push_back(*it);
 	}
 	
@@ -1768,10 +1887,8 @@
 
 
-int
-VspBspViewCellsManager::CastLineSegment(const Vector3 &origin,
-										const Vector3 &termination,
-										ViewCellContainer &viewcells
-										)
-{
-  return mVspBspTree->CastLineSegment(origin, termination, viewcells);
-}
+int VspBspViewCellsManager::CastLineSegment(const Vector3 &origin,
+											const Vector3 &termination,
+											ViewCellContainer &viewcells)
+{
+	return mVspBspTree->CastLineSegment(origin, termination, viewcells);
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/MeshKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/MeshKdTree.cpp	(revision 376)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/MeshKdTree.cpp	(revision 469)
@@ -181,5 +181,5 @@
   mSplitCandidates->clear();
   
-  int requestedSize = 2*node->mFaces.size();
+  int requestedSize = 2*(int)node->mFaces.size();
   // creates a sorted split candidates array
   if (mSplitCandidates->capacity() > 500000 &&
@@ -232,5 +232,5 @@
   // C = ct_div_ci  + (ol + or)/queries
 
-  int objectsLeft = 0, objectsRight = node->mFaces.size();
+  int objectsLeft = 0, objectsRight = (int)node->mFaces.size();
   
   float minBox = box.Min(axis);
@@ -241,5 +241,5 @@
   float maxBand = minBox + (1.0f - mSplitBorder)*(maxBox - minBox);
   
-  float minSum = 1e20;
+  float minSum = 1e20f;
   vector<SortableEntry>::const_iterator ci;
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 444)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Mesh.cpp	(revision 469)
@@ -234,5 +234,5 @@
 
 
-  int size = face->mVertexIndices.size();
+  int size = (int)face->mVertexIndices.size();
 
   mVertices[face->mVertexIndices[size - 1]].
@@ -430,5 +430,5 @@
 Mesh::AddTriangle(const Triangle3 &triangle)
 {
-  int index = mVertices.size();
+  int index = (int)mVertices.size();
 
   for (int i=0; i < 3; i++) {
@@ -442,5 +442,5 @@
 Mesh::AddRectangle(const Rectangle3 &rect)
 {
-  int index = mVertices.size();
+  int index = (int)mVertices.size();
 
   for (int i=0; i < 4; i++) {
