Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.h	(revision 466)
@@ -208,4 +208,11 @@
 	float GetEpsilon() const;
 
+
+    int
+	CastLineSegment(const Vector3 &origin,
+					const Vector3 &termination,
+					ViewCellContainer &viewcells
+					);
+
 protected:
 
@@ -427,4 +434,7 @@
 				  int &contributingSamples);
 
+
+
+  
 	/// Pointer to the root of the tree
 	BspNode *mRoot;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 465)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 466)
@@ -2035,8 +2035,8 @@
 
 		if (ray->mTerminationObject)
-			vc->GetPvs().AddSample(ray->mTerminationObject);
+		  vc->GetPvs().AddSample(ray->mTerminationObject);
 
 		if (ray->mOriginObject)
-			vc->GetPvs().AddSample(ray->mOriginObject);
+		  vc->GetPvs().AddSample(ray->mOriginObject);
 	}
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 406)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Pvs.h	(revision 466)
@@ -31,24 +31,25 @@
 {
 public:
-	Pvs(): mSamples(0), mEntries() {}
+  Pvs(): mSamples(0), mEntries() {}
+  
+  int mSamples;
+  
+  int Compress() {return 0;}
+  int GetSize() const {return (int)mEntries.size();}
+  bool Empty() const {return mEntries.size() == 0;}
+  /** 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.
+  */
+  int Diff(const Pvs<T> &b);
+  
+  PvsData<T> *Find(T sample);
+  bool AddSample(T sample, float &contribution);
+  int AddSample(T sample);
 
-	int mSamples;
-
-	int Compress() {return 0;}
-	int GetSize() const {return (int)mEntries.size();}
-	bool Empty() const {return mEntries.size() == 0;}
-	/** 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.
-	*/
-	int Diff(const Pvs<T> &b);
-
-	PvsData<T> *Find(T sample);
-	int AddSample(T sample);
-
-	void GetData(const int index, T &entry, PvsData<T> &data);
-	std::map<T, PvsData<T>, LtSample<T> > mEntries;
+  void GetData(const int index, T &entry, PvsData<T> &data);
+  std::map<T, PvsData<T>, LtSample<T> > mEntries;
 };
 
@@ -116,20 +117,23 @@
 int Pvs<T>::AddSample(T sample)
 {
-	int result;
-	PvsData<T> *data = Find(sample);
+  float dummy;
+  return AddSample(sample, dummy) ? 1 : 0;
+}
 
-	if (data) 
-	{
-		data->mVisibleSamples ++;
-		result = 0;
-	
-	} 
-	else 
-	{
-		mEntries[sample] = PvsData<T>(1);
-		result = 1;
-	}
-
-	return  result;
+template <typename T>
+bool Pvs<T>::AddSample(T sample, float &contribution)
+{
+  PvsData<T> *data = Find(sample);
+  
+  if (data)  {
+	data->mVisibleSamples++;
+	contribution = 1.0f/data->mVisibleSamples;
+	return false;
+  }
+  else {
+	mEntries[sample] = PvsData<T>(1);
+	contribution = 1.0f;
+	return true;
+  }
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 466)
@@ -407,5 +407,13 @@
 		@returns the number of intersections with objects stored in the tree.
 	*/
-	int CastRay(Ray &ray);
+  int
+  _CastRay(Ray &ray);
+
+
+  int
+  CastLineSegment(const Vector3 &origin,
+				  const Vector3 &termination,
+				  ViewCellContainer &viewcells
+				  );
 
 	/// bsp tree construction types
@@ -838,3 +846,18 @@
 };
 
+struct BspIntersection {
+  // the point of intersection
+  float mT;
+  
+  BspLeaf *mLeaf;
+  
+  BspIntersection(const float t, BspLeaf *l):
+	mT(t), mLeaf(l) {}
+  
+  BspIntersection() {}
+  
+  bool operator<(const BspIntersection &b) const {
+	return mT	<b.mT; }
+};
+
 #endif
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 464)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.h	(revision 466)
@@ -857,4 +857,12 @@
 			   );
 
+    int
+  CollectRays(VssRayContainer &rays,
+			  const int number);
+
+  int
+  CollectRays(VssRayContainer &rays
+			  );
+
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h	(revision 466)
@@ -29,8 +29,8 @@
 
 public:
-	
-	/// view cell container types
-	enum {BSP, KD, VSP_KD, VSP_BSP};
-
+  
+  /// view cell container types
+  enum {BSP, KD, VSP_KD, VSP_BSP};
+  
 	/** Constructor taking the maximal number of samples used for construction
 	*/
@@ -56,19 +56,18 @@
 		@param contributingSamples returns the number of contributingSamples
 	*/
-	void  ComputeSampleContributions(const RayContainer &rays, 
-									 int &sampleContributions,
-									 int &contributingSamples,
-									 const bool castRays = true);
-
-
-	/** Computes sample contribution of a simgle ray to the view cells PVS.
-		@param ray finds intersections with view cells and holds the contribution
-		@param castRay true if ray should be cast to gain the information, false if ray
-		is already holding the information and need not be recast.
-
-		@returns number of sample contributions
-	*/
-	virtual int ComputeSampleContributions(Ray &ray, const bool castRay = true) = 0;
-
+  void  ComputeSampleContributions(const VssRayContainer &rays
+								   );
+
+
+  /** Computes sample contribution of a simgle ray to the view cells PVS.
+	  @param ray finds intersections with view cells and holds the contribution
+	  @param castRay true if ray should be cast to gain the information, false if ray
+	  is already holding the information and need not be recast.
+	  
+	  @returns number of sample contributions
+  */
+  virtual void ComputeSampleContributions(VssRay &ray
+										  );
+  
 	/** Prints out statistics of the view cells.
 	*/
@@ -77,11 +76,11 @@
 	/** Post processes view cells givemŽa number of rays.
 	*/
-	virtual int PostProcess(const ObjectContainer &objects, 
-							const RayContainer &rays) = 0;
+  virtual int PostProcess(const ObjectContainer &objects, 
+						  const VssRayContainer &rays) = 0;
 
 	/** Show visualization of the view cells.
 	*/
 	virtual void Visualize(const ObjectContainer &objects,
-						   const RayContainer &sampleRays) = 0;
+						   const VssRayContainer &sampleRays) = 0;
 
 	/** type of the view cell container.
@@ -157,5 +156,13 @@
 	*/
 	virtual bool ViewCellsConstructed() const = 0;
-		
+
+  /** cast line segment to get a list of unique viewcells which are intersected
+	  by this line segment */
+  
+  virtual int CastLineSegment(const Vector3 &origin,
+							  const Vector3 &termination,
+							  ViewCellContainer &viewcells
+							  ) = 0;
+
 
 protected:
@@ -205,11 +212,10 @@
 				  AxisAlignedBox3 *sceneBbox);
 
-	int ComputeSampleContributions(Ray &ray, const bool castRay = false);
 
 	int PostProcess(const ObjectContainer &objects, 
-					const RayContainer &rays);
+					const VssRayContainer &rays);
 
 	void Visualize(const ObjectContainer &objects,
-				   const RayContainer &sampleRays);
+				   const VssRayContainer &sampleRays);
 
 	int GetType() const;
@@ -222,4 +228,10 @@
 
 	~BspViewCellsManager();
+
+  int CastLineSegment(const Vector3 &origin,
+					  const Vector3 &termination,
+					  ViewCellContainer &viewcells
+					  );
+
 protected:
 
@@ -241,9 +253,9 @@
 	/** Exports visualization of the BSP splits.
 	*/
-	void ExportSplits(const ObjectContainer &objects, const RayContainer &sampleRays);
+	void ExportSplits(const ObjectContainer &objects, const VssRayContainer &sampleRays);
 
 	/** Exports visualization of the BSP PVS.
 	*/
-	void ExportBspPvs(const ObjectContainer &objects, const RayContainer &sampleRays);
+	void ExportBspPvs(const ObjectContainer &objects, const VssRayContainer &sampleRays);
 
 };
@@ -263,11 +275,14 @@
 				  AxisAlignedBox3 *sceneBbox);
 
-	int CastRay(const Ray &ray);
+  int CastLineSegment(const Vector3 &origin,
+					  const Vector3 &termination,
+					  ViewCellContainer &viewcells
+					  ) ;
 
 	int PostProcess(const ObjectContainer &objects, 
-					const RayContainer &rays);
+					const VssRayContainer &rays);
 
 	void Visualize(const ObjectContainer &objects,
-				   const RayContainer &sampleRays);
+				   const VssRayContainer &sampleRays);
 
 	int GetType() const;
@@ -284,5 +299,4 @@
 	 KdNode *GetNodeForPvs(KdLeaf *leaf);
 
-	 int ComputeSampleContributions(Ray &ray, const bool castRay = true);
 
 	 /// the BSP tree.
@@ -310,11 +324,10 @@
 				  AxisAlignedBox3 *sceneBbox);
 
-	int ComputeSampleContributions(Ray &ray, const bool castRay = true);
 
 	int PostProcess(const ObjectContainer &objects, 
-					const RayContainer &rays);
+					const VssRayContainer &rays);
 
 	void Visualize(const ObjectContainer &objects,
-				   const RayContainer &sampleRays);
+				   const VssRayContainer &sampleRays);
 
 	int GetType() const;
@@ -325,4 +338,10 @@
 
 	ViewCell *GenerateViewCell(Mesh *mesh) const;
+
+
+    int CastLineSegment(const Vector3 &origin,
+						const Vector3 &termination,
+						ViewCellContainer &viewcells
+						) ;
 
 protected:
@@ -350,11 +369,10 @@
 				  AxisAlignedBox3 *sceneBbox);
 
-	int ComputeSampleContributions(Ray &ray, const bool castRay = false);
 
 	int PostProcess(const ObjectContainer &objects, 
-					const RayContainer &rays);
+					const VssRayContainer &rays);
 
 	void Visualize(const ObjectContainer &objects,
-				   const RayContainer &sampleRays);
+				   const VssRayContainer &sampleRays);
 
 	int GetType() const;
@@ -365,4 +383,9 @@
 
 	void PrintStatistics(ostream &s) const;
+
+  int CastLineSegment(const Vector3 &origin,
+					  const Vector3 &termination,
+					  ViewCellContainer &viewcells
+					  ) ;
 
 protected:
@@ -385,9 +408,9 @@
 	/** Exports visualization of the BSP splits.
 	*/
-	void ExportSplits(const ObjectContainer &objects, const RayContainer &sampleRays);
+	void ExportSplits(const ObjectContainer &objects, const VssRayContainer &sampleRays);
 
 	/** Exports visualization of the BSP PVS.
 	*/
-	void ExportBspPvs(const ObjectContainer &objects, const RayContainer &sampleRays);
+	void ExportBspPvs(const ObjectContainer &objects, const VssRayContainer &sampleRays);
 
 };
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 466)
@@ -1710,2 +1710,92 @@
 	return splits;
 }
+
+
+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;
+  
+  Intersectable::NewMail();
+  
+  Vector3 entp = origin;
+  Vector3 extp = termination;
+  
+  BspNode *node = mRoot;
+  BspNode *farChild = NULL;
+  
+  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);
+	  
+	  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;
+		  
+		  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));
+	  
+	  // 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);
+	  
+	  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;
+      
+	  entp = extp;
+	  mint = maxt; // NOTE: need this?
+	  
+	  BspRayTraversalData &s = tStack.top();
+	  
+	  node = s.mNode;
+	  extp = s.mExitPoint;
+	  maxt = s.mMaxT;
+	  
+	  tStack.pop();
+	}
+  }
+  return hits;
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 448)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.h	(revision 466)
@@ -59,18 +59,4 @@
   };
 
-  struct BspIntersection {
-    // the point of intersection
-    float mT;
-
-    BspLeaf *mLeaf;
-
-    BspIntersection(const float t, BspLeaf *l):
-			mT(t), mLeaf(l) {}
-		
-    BspIntersection() {}
-		
-    bool operator<(const BspIntersection &b) const {
-      return mT	<b.mT; }
-  };
 
   // I should have some abstract cell data type !!! here
@@ -80,5 +66,5 @@
   
   vector<Intersection> intersections;
-  vector<BspIntersection> bspIntersections;
+  //  vector<BspIntersection> bspIntersections;
   vector<KdLeaf *> kdLeaves;
   vector<Intersectable *> testedObjects;
@@ -107,6 +93,17 @@
   /** Construct ray from a vss ray.
   */
-  Ray(const VssRay &vssRay);	
-	
+  Ray(const VssRay &vssRay) {
+	Init(vssRay);
+  }
+
+  void Clear() {
+	intersections.clear();
+	kdLeaves.clear();
+	testedObjects.clear();
+	//	bspIntersections.clear();
+  }
+
+  void Init(const VssRay &vssRay);
+
   Intersectable *GetIntersectionObject(const int i) const {
     return intersections[i].mObject;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h	(revision 446)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.h	(revision 466)
@@ -918,5 +918,12 @@
 				   SimpleRayContainer &rays);
 
-
+  int
+  CollectRays(VssRayContainer &rays,
+			  const int number);
+
+  int
+  CollectRays(VssRayContainer &rays
+			  );
+  
 };
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 462)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 466)
@@ -73,13 +73,13 @@
   virtual bool
   ExportRays(const RayContainer &rays,
-						 const float length=1000,
-						 const RgbColor &color = RgbColor(1,1,1)
-						 ) = 0;
-
-	virtual bool
+			 const float length=1000,
+			 const RgbColor &color = RgbColor(1,1,1)
+			 ) = 0;
+  
+  virtual bool
   ExportRays(const VssRayContainer &rays,
-						 const RgbColor &color = RgbColor(1,1,1)
-						 ) = 0;
-
+			 const RgbColor &color = RgbColor(1,1,1)
+			 ) = 0;
+  
   virtual void 
   ExportViewCells(const ViewCellContainer &viewCells) = 0;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 462)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/KdTree.cpp	(revision 466)
@@ -792,5 +792,6 @@
 	    // $$ JB TEMPORARY solution -> should add object PVS or explictly computed
 	    // kd tree PVS
-	    if (leaf->mKdPvs.AddSample(node))
+		float contribution;
+		if (leaf->mKdPvs.AddSample(node, contribution))
 	      totalPvsSize++;
 	  }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 466)
@@ -1764,5 +1764,6 @@
 }
 
-int BspTree::CastRay(Ray &ray)
+int
+BspTree::_CastRay(Ray &ray)
 {
 	int hits = 0;
@@ -1833,5 +1834,5 @@
 			if (!leaf->mViewCell->Mailed())
 			{
-				ray.bspIntersections.push_back(Ray::BspIntersection(maxt, leaf));
+			  //				ray.bspIntersections.push_back(Ray::BspIntersection(maxt, leaf));
 				leaf->mViewCell->Mail();
 				++ hits;
@@ -1859,4 +1860,94 @@
 
 	return hits;
+}
+
+
+int
+BspTree::CastLineSegment(const Vector3 &origin,
+						 const Vector3 &termination,
+						 vector<ViewCell *> &viewcells
+						 )
+{
+  int hits = 0;
+  stack<BspRayTraversalData> tStack;
+  
+  float mint = 0.0f, maxt = 1.0f;
+  
+  Intersectable::NewMail();
+  
+  Vector3 entp = origin;
+  Vector3 extp = termination;
+  
+  BspNode *node = mRoot;
+  BspNode *farChild = NULL;
+  
+  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);
+	  
+	  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;
+		  
+		  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));
+	  
+	  // 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);
+	  
+	  if (!leaf->mViewCell->Mailed()) {
+		viewcells.push_back(leaf->mViewCell);
+		leaf->mViewCell->Mail();
+		hits++;
+	  }
+	  
+	  //-- fetch the next far child from the stack
+	  if (tStack.empty())
+		break;
+      
+	  entp = extp;
+	  mint = maxt; // NOTE: need this?
+	  
+	  BspRayTraversalData &s = tStack.top();
+	  
+	  node = s.mNode;
+	  extp = s.mExitPoint;
+	  maxt = s.mMaxT;
+	  
+	  tStack.pop();
+	}
+  }
+  return hits;
 }
 
@@ -2355,10 +2446,10 @@
 		int contribution = 0;
 		Ray *ray = (*it)->mRay;
-			
+		float relContribution;
 		if (!ray->intersections.empty())
-			contribution += vc->GetPvs().AddSample(ray->intersections[0].mObject);
+		  contribution += vc->GetPvs().AddSample(ray->intersections[0].mObject, relContribution);
 		
 		if (ray->sourceObject.mObject)
-			contribution += vc->GetPvs().AddSample(ray->sourceObject.mObject);
+			contribution += vc->GetPvs().AddSample(ray->sourceObject.mObject, relContribution);
 
 		if (contribution)
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 464)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 466)
@@ -2051,2 +2051,56 @@
   return box;
 }
+
+
+int
+RssTree::CollectRays(VssRayContainer &rays,
+					 const int number)
+{
+  VssRayContainer allRays;
+  CollectRays(allRays);
+  
+  int desired = min(number, (int)allRays.size());
+  float prob = desired/(float)allRays.size();
+  while (rays.size() < desired) {
+	VssRayContainer::const_iterator it = allRays.begin();
+	for (; it != allRays.end() && rays.size() < desired; it++) {
+	  if (Random(1.0f) < prob)
+		rays.push_back(*it);
+	}
+  }
+  return rays.size();
+}
+
+
+int
+RssTree::CollectRays(VssRayContainer &rays
+					 )
+{
+  VssRay::NewMail();
+
+  stack<RssTreeNode *> tstack;
+  tstack.push(root);
+  
+  while (!tstack.empty()) {
+    RssTreeNode *node = tstack.top();
+    tstack.pop();
+	
+    if (node->IsLeaf()) {
+	  RssTreeLeaf *leaf = (RssTreeLeaf *)node;
+	  // update pvs size
+	  RssTreeNode::RayInfoContainer::const_iterator it = leaf->rays.begin();
+	  for (;it != leaf->rays.end(); ++it)
+		if (!(*it).mRay->Mailed()) {
+		  (*it).mRay->Mail();
+		  rays.push_back((*it).mRay);
+		}
+	} else {
+	  RssTreeInterior *in = (RssTreeInterior *)node;
+	  // both nodes for directional splits
+	  tstack.push(in->front);
+	  tstack.push(in->back);
+	}
+  }
+  
+  return rays.size();
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssTree.cpp	(revision 466)
@@ -1954,2 +1954,56 @@
   //  cout<<"ei="<<mEntropyImportance<<" ";
 }
+
+
+int
+VssTree::CollectRays(VssRayContainer &rays,
+					 const int number)
+{
+  VssRayContainer allRays;
+  CollectRays(allRays);
+  
+  int desired = min(number, (int)allRays.size());
+  float prob = desired/(float)allRays.size();
+  while (rays.size() < desired) {
+	VssRayContainer::const_iterator it = allRays.begin();
+	for (; it != allRays.end() && rays.size() < desired; it++) {
+	  if (Random(1.0f) < prob)
+		rays.push_back(*it);
+	}
+  }
+  return rays.size();
+}
+
+
+int
+VssTree::CollectRays(VssRayContainer &rays
+					 )
+{
+  VssRay::NewMail();
+
+  stack<VssTreeNode *> tstack;
+  tstack.push(root);
+  
+  while (!tstack.empty()) {
+    VssTreeNode *node = tstack.top();
+    tstack.pop();
+	
+    if (node->IsLeaf()) {
+	  VssTreeLeaf *leaf = (VssTreeLeaf *)node;
+	  // update pvs size
+	  VssTreeNode::RayInfoContainer::const_iterator it = leaf->rays.begin();
+	  for (;it != leaf->rays.end(); ++it)
+		if (!(*it).mRay->Mailed()) {
+		  (*it).mRay->Mail();
+		  rays.push_back((*it).mRay);
+		}
+	} else {
+	  VssTreeInterior *in = (VssTreeInterior *)node;
+	  // both nodes for directional splits
+	  tstack.push(in->front);
+	  tstack.push(in->back);
+	}
+  }
+  
+  return rays.size();
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp	(revision 448)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Ray.cpp	(revision 466)
@@ -220,19 +220,21 @@
 }
 
-Ray::Ray(const VssRay &vssRay):
-loc(vssRay.mOrigin),
-sourceObject(0, vssRay.mOriginObject, 0),
-mType(LOCAL_RAY)
-{
-	float len = vssRay.Length();
-
-	if (!len)
-		len = Limits::Small;
-
-	dir = vssRay.GetDir() / len;
-
-	if (vssRay.mTerminationObject)
-		intersections.push_back(Intersection(len, vssRay.mTerminationObject, 0));
-
-	Precompute();
-}
+void
+Ray::Init(const VssRay &vssRay)
+{
+  loc = vssRay.mOrigin;
+  sourceObject = Intersection(0, vssRay.mOriginObject, 0);
+  mType = LOCAL_RAY;
+  
+  float len = vssRay.Length();
+  
+  if (!len)
+	len = Limits::Small;
+  
+  dir = vssRay.GetDir() / len;
+  
+  if (vssRay.mTerminationObject)
+	intersections.push_back(Intersection(len, vssRay.mTerminationObject, 0));
+  
+  Precompute();
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 464)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssPreprocessor.cpp	(revision 466)
@@ -525,22 +525,5 @@
 
   // viewcells->UpdatePVS(newVssRays);
-  // get viewcells as kd tree boxes
-  vector<AxisAlignedBox3> kdViewcells;
-
-  if (0) {
-	vector<KdLeaf *> leaves;
-	mKdTree->CollectLeaves(leaves);
-	vector<KdLeaf *>::const_iterator it;
-	int targetLeaves = 50;
-	float prob = targetLeaves/(float)leaves.size();
-	for (it = leaves.begin(); it != leaves.end(); ++it)
-	  if (RandomValue(0.0f,1.0f) < prob)
-		kdViewcells.push_back(mKdTree->GetBox(*it));
-		
-	float avgPvs = GetAvgPvsSize(rssTree, kdViewcells);
-	cout<<"Initial average PVS size = "<<avgPvs<<endl;
-  }
-
-	
+  
   int samples = 0;
   int pass = 0;
@@ -563,17 +546,33 @@
 	for (int i=0; i < rays.size(); i++)
 	  CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
-		
+	
+	if (mUseViewcells) {
+	  
+	  
+	  /// compute view cell contribution of rays
+	  mViewCellsManager->ComputeSampleContributions(vssRays
+													);
+	  
+	  
+	  float pvs = rssTree->GetAvgPvsSize();
+	  cout<<"*****************************\n";
+	  cout<<samples<<" avgPVS ="<<pvs<<endl;
+	  cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
+	  cout<<"*****************************\n";
+	}
+
+
+
 	rssTree->AddRays(vssRays);
-		
+	
 	if (mUpdateSubdivision) {
 	  int subdivided = rssTree->UpdateSubdivision();
 	  cout<<"subdivided leafs = "<<subdivided<<endl;
 	  cout<<"#total leaves = "<<rssTree->stat.Leaves()<<endl;
-
 	}
 
 	if (mUseViewcells) {
-	  float avgPvs = GetAvgPvsSize(rssTree, kdViewcells);
-	  cout<<"Average PVS size = "<<avgPvs<<endl;
+	  //	  float avgPvs = GetAvgPvsSize(rssTree, kdViewcells);
+	  //	  cout<<"Average PVS size = "<<avgPvs<<endl;
 	}
 
@@ -590,44 +589,4 @@
 	samples+=num;
 
-	if (mUseViewcells) {
-	  
-	  //-- prepare traversal rays for view cell intersections
-	  RayContainer passRays;
-	  
-	  VssRayContainer::const_iterator it, it_end = vssRays.end();
-	  
-	  for (it = vssRays.begin(); it != it_end; ++ it)
-		passRays.push_back(new Ray(*(*it)));
-	  
-	  int sampleContributions = 0;
-	  int contributingSamples = 0;
-	  
-	  /// compute view cell contribution of rays
-	  mViewCellsManager->ComputeSampleContributions(passRays,
-													sampleContributions, 
-													contributingSamples);
-	  
-	  //-- save rays for post processing
-	  if (((int)storedRays.size() < mViewCellsManager->GetPostProcessSamples()) ||
-		  ((int)storedRays.size() < mViewCellsManager->GetVisualizationSamples()))
-		{
-		  RayContainer::const_iterator it, it_end = passRays.end();
-		  
-		  for (it = passRays.begin(); it != it_end; ++ it)
-			storedRays.push_back(new Ray(*(*it)));
-		}
-	  else
-		{
-		  CLEAR_CONTAINER(passRays);
-		}
-	  
-	  float pvs = rssTree->GetAvgPvsSize();
-	  cout<<"*****************************\n";
-	  cout<<samples<<" avgPVS ="<<pvs<<endl;
-	  cout<<"sample contributions ="<<sampleContributions<<endl;
-	  cout<<"contributing sample ="<<contributingSamples<<endl;
-	  cout<<"RssTree root PVS size = "<<rssTree->GetRootPvsSize()<<endl;
-	  cout<<"*****************************\n";
-	}
 	
 	if (mExportPvs) {
@@ -645,4 +604,9 @@
   
   if (mUseViewcells) {
+
+	VssRayContainer storedRays;
+	rssTree->CollectRays(storedRays, Max(
+										 mViewCellsManager->GetPostProcessSamples(),
+										 mViewCellsManager->GetVisualizationSamples()));
 	
 	//-- post process view cells
@@ -651,18 +615,20 @@
 	//-- several visualizations and statistics
 	mViewCellsManager->PrintStatistics(Debug);
-	
-	//-- render simulation after merge
-	cout << "\nevaluating render time of final view cells ... ";
-	
-	const SimulationStatistics ss = mViewCellsManager->SimulateRendering();
-	
-	cout << " finished" << endl;
-	
-	cout << ss << endl;
-	Debug << ss << endl;
-	
+
 	mViewCellsManager->Visualize(mObjects, storedRays);
-  }
-  
+
+	CLEAR_CONTAINER(storedRays);
+
+  //-- render simulation after merge
+  cout << "\nevaluating bsp view cells render time after merge ... ";
+  
+  const SimulationStatistics ss = mViewCellsManager->SimulateRendering();
+  
+  cout << " finished" << endl;
+  cout << ss << endl;
+  Debug << ss << endl;
+
+  }
+
   delete rssTree;
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 465)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VssPreprocessor.cpp	(revision 466)
@@ -42,5 +42,5 @@
 						  )
 {
-  ray.intersections.clear();
+  ray.Clear();
   // do not store anything else then intersections at the ray
   ray.Init(point, direction, Ray::LOCAL_RAY);
@@ -450,9 +450,8 @@
   }
 	
-  ObjectContainer objects;
-  mSceneGraph->CollectObjects(&objects);
+  mSceneGraph->CollectObjects(&mObjects);
 
   // construct view cells
-  mViewCellsManager->Construct(objects, mVssRays, mViewSpaceBox);
+  mViewCellsManager->Construct(mObjects, mVssRays, mViewSpaceBox);
 
   vssTree = new VssTree;
@@ -509,5 +508,5 @@
 	  num = GenerateImportanceRays(vssTree, num, rays);
 	}
-			
+	
 	for (int i=0; i < rays.size(); i++)
 	  CastRay(rays[i].mOrigin, rays[i].mDirection, vssRays);
@@ -534,39 +533,11 @@
 
 	//-- prepare traversal rays for view cell intersections
-	RayContainer passRays;
-
-	VssRayContainer::const_iterator it, it_end = vssRays.end();
-	
-	for (it = vssRays.begin(); it != it_end; ++ it)
-		passRays.push_back(new Ray(*(*it)));
-	
-	int sampleContributions = 0;
-	int contributingSamples = 0;
-
 	/// compute view cell contribution of rays
-/*	mViewCellsManager->ComputeSampleContributions(passRays,
-												  sampleContributions, 
-												  contributingSamples);
-	
-	//-- save rays for post processing
-	if (((int)storedRays.size() < mViewCellsManager->GetPostProcessSamples()) ||
-		((int)storedRays.size() < mViewCellsManager->GetVisualizationSamples()))
-	{
-		RayContainer::const_iterator it, it_end = passRays.end();
-
-		for (it = passRays.begin(); it != it_end; ++ it)
-			storedRays.push_back(new Ray(*(*it)));
-	}
-	else
-	{
-		CLEAR_CONTAINER(passRays);
-	}
-*/
+	mViewCellsManager->ComputeSampleContributions(vssRays);
+
 	samples+=num;
 	float pvs = vssTree->GetAvgPvsSize();
 	cout<<"*****************************\n";
 	cout<<samples<<" avgPVS ="<<pvs<<endl;
-	cout<<"sample contributions ="<<sampleContributions<<endl;
-	cout<<"contributing sample ="<<contributingSamples<<endl;
 	cout<<"VssTree root PVS size = "<<vssTree->GetRootPvsSize()<<endl;
 	cout<<"*****************************\n";
@@ -576,26 +547,33 @@
   }
 
-  cout << "here" << endl;
-  //-- post process view cells
-  mViewCellsManager->PostProcess(objects, storedRays);
-
-  //-- several visualizations and statistics
-  mViewCellsManager->PrintStatistics(Debug);
+  {
+	VssRayContainer storedRays;
+	vssTree->CollectRays(storedRays, Max(
+										 mViewCellsManager->GetPostProcessSamples(),
+										 mViewCellsManager->GetVisualizationSamples()));
+	
+	//-- post process view cells
+	mViewCellsManager->PostProcess(mObjects, storedRays);
+	
+	//-- several visualizations and statistics
+	mViewCellsManager->PrintStatistics(Debug);
+
+	mViewCellsManager->Visualize(mObjects, storedRays);
+
+	CLEAR_CONTAINER(storedRays);
+  }
 
   //-- render simulation after merge
   cout << "\nevaluating bsp view cells render time after merge ... ";
-		 
+  
   const SimulationStatistics ss = mViewCellsManager->SimulateRendering();
-		 
+  
   cout << " finished" << endl;
   cout << ss << endl;
   Debug << ss << endl;
-
-  mViewCellsManager->Visualize(objects, storedRays);
-
-  CLEAR_CONTAINER(storedRays);
+  
   
   delete vssTree;
-
+  
   return true;
 }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 463)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.h	(revision 466)
@@ -28,20 +28,18 @@
 		   const Vector3 &point, 
 		   const Vector3 &direction,
-			 const int type,
-			 const Ray::Intersection &source);
-
-	/** Refined sampling for finding "holes", i.e., difficult visibility.
-	*/
+		   const int type,
+		   const Ray::Intersection &source);
+  
+  /** Refined sampling for finding "holes", i.e., difficult visibility.
+   */
   void
   HoleSamplingPass();
-
-	/** Casts a bundle of sample rays into the scene.
+  
+  /** Casts a bundle of sample rays into the scene.
 		@param sampleContributions the number of sample contributions
 		@param contributingSamples the number of samples contributing
 	*/
 	void
-	CastRays(const RayContainer &rays,
-			 int &sampleContributions, 
-			 int &contributingSamples);
+	CastRays(const RayContainer &rays);
 	
 	/** Casts a single ray into the scene.
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 462)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 466)
@@ -32,8 +32,5 @@
 							   const Ray::Intersection &origin)
 {
-	ray.intersections.clear();
-	ray.kdLeaves.clear();
-	ray.testedObjects.clear();
-	ray.bspIntersections.clear();
+  ray.Clear();
   
 	ray.mFlags |= Ray::STORE_KDLEAVES | Ray::STORE_BSP_INTERSECTIONS;
@@ -59,23 +56,17 @@
  
 void
-SamplingPreprocessor::CastRays(const RayContainer &rays, 
-							   int &sampleContributions, 
-							   int &contributingSamples)
+SamplingPreprocessor::CastRays(const RayContainer &rays)
 { 	
-	// cast ray to KD tree to find intersection with other objects
-	RayContainer::const_iterator it, it_end = rays.end();
- 
-	for (it = rays.begin(); it != it_end; ++it)
-        mKdTree->CastRay(*(*it));
-
-	// for KD tree, intersecting view cells are already known, so no 
-	// additional ray must be cast to them
-	const bool castRaysToViewCells = 
-		(mViewCellsManager->GetType() != ViewCellsManager::KD);
-
-	mViewCellsManager->ComputeSampleContributions(rays, 
-												  sampleContributions, 
-												  contributingSamples,
-												  castRaysToViewCells);
+  // cast ray to KD tree to find intersection with other objects
+  RayContainer::const_iterator it, it_end = rays.end();
+
+  VssRayContainer vssRays;
+  for (it = rays.begin(); it != it_end; ++it) {
+	mKdTree->CastRay(*(*it));
+	vssRays.push_back(new VssRay(*(*it)));
+  }
+  
+  mViewCellsManager->ComputeSampleContributions(vssRays);
+  CLEAR_CONTAINER(vssRays);
 }
 
@@ -84,9 +75,8 @@
 {
 	// cast ray to KD tree to find intersection with other objects
-	mKdTree->CastRay(ray);
-
-	bool castRayToViewCells = mViewCellsManager->GetType() != ViewCellsManager::KD;
-
-	return mViewCellsManager->ComputeSampleContributions(ray, castRayToViewCells);
+  mKdTree->CastRay(ray);
+  VssRay vssRay(ray);
+  mViewCellsManager->ComputeSampleContributions(vssRay);
+  return vssRay.mPvsContribution;
 }
 
@@ -395,5 +385,6 @@
 		mViewCellsManager->Construct(objects, mVssSampleRays);
 
-	mViewCellsManager->PostProcess(objects, mSampleRays);
+	// $$JB temporary removed
+	//	mViewCellsManager->PostProcess(objects, mSampleRays);
 	
 	//-- several visualizations and statistics
@@ -409,5 +400,6 @@
 	Debug << ss << endl;
 	
-	mViewCellsManager->Visualize(objects, mSampleRays);	
+	// $$JB temporary removed
+	//mViewCellsManager->Visualize(objects, mSampleRays);	
    
 	return true;
@@ -419,34 +411,34 @@
 											int &contributingSamples)
 {
-	// cast rays to view cells
-	CastRays(newRays, sampleContributions, contributingSamples);
-
-	// save rays for view cells construction
-	if (!mViewCellsManager->ViewCellsConstructed())
+  // cast rays to view cells
+  CastRays(newRays);
+
+  // save rays for view cells construction
+  if (!mViewCellsManager->ViewCellsConstructed())
 	{
-		if ((int)mVssSampleRays.size() < mViewCellsManager->GetConstructionSamples())
+	  if ((int)mVssSampleRays.size() < mViewCellsManager->GetConstructionSamples())
 		{
-			RayContainer::const_iterator it, it_end = newRays.end();
-
-			for (it = newRays.begin(); it != it_end; ++ it)
-				mVssSampleRays.push_back(new VssRay(*(*it)));
-		}
-		else
+		  RayContainer::const_iterator it, it_end = newRays.end();
+		  
+		  for (it = newRays.begin(); it != it_end; ++ it)
+			mVssSampleRays.push_back(new VssRay(*(*it)));
+		}
+	  else
 		{
-			// construct view cells
-			mViewCellsManager->Construct(objects, mVssSampleRays);
-		
-			// throw away samples 
-			//CLEAR_CONTAINER(mVssSampleRays);
-		}
-	}
-	// Need rays (with ordered intersections) for post processing => collect new rays
-	if (((int)mSampleRays.size() < mViewCellsManager->GetPostProcessSamples()) ||
-	    ((int)mSampleRays.size() < mViewCellsManager->GetVisualizationSamples()))
+		  // construct view cells
+		  mViewCellsManager->Construct(objects, mVssSampleRays);
+		  
+		  // throw away samples 
+		  //CLEAR_CONTAINER(mVssSampleRays);
+		}
+	}
+  // Need rays (with ordered intersections) for post processing => collect new rays
+  if (((int)mSampleRays.size() < mViewCellsManager->GetPostProcessSamples()) ||
+	  ((int)mSampleRays.size() < mViewCellsManager->GetVisualizationSamples()))
 	{
-		RayContainer::const_iterator it, it_end = newRays.end();
-
-		for (it = newRays.begin(); it != it_end; ++ it)
-				mSampleRays.push_back(new Ray(*(*it)));
-	}
-}
+	  RayContainer::const_iterator it, it_end = newRays.end();
+	  
+	  for (it = newRays.begin(); it != it_end; ++ it)
+		mSampleRays.push_back(new Ray(*(*it)));
+	}
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Camera.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Camera.cpp	(revision 386)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Camera.cpp	(revision 466)
@@ -131,9 +131,5 @@
   Vector3 target = xv + yv + mDirection;
 
-  ray.intersections.clear();
-  ray.kdLeaves.clear();
-  ray.testedObjects.clear();
-  ray.bspIntersections.clear();
-
+  ray.Clear();
   ray.Init(mPosition, target, Ray::LOCAL_RAY);
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 465)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.cpp	(revision 466)
@@ -67,23 +67,16 @@
 }
 
-void ViewCellsManager::ComputeSampleContributions(const RayContainer &rays, 
-												  int &sampleContributions,
-												  int &contributingSamples,
-												  const bool castRays)
-{
-	// view cells not yet constructed
-	if (!ViewCellsConstructed())
-		return;
-
-	RayContainer::const_iterator it, it_end = rays.end();
-
-	sampleContributions = 0;
-	contributingSamples = 0;
-
-    for (it = rays.begin(); it != it_end; ++ it)
-	{
-		sampleContributions += ComputeSampleContributions(*(*it), castRays);
-		contributingSamples += sampleContributions > 0;
-	}
+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));
+  }
 }
 
@@ -277,65 +270,47 @@
 }
 
-int BspViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay)
-{
-	// view cells not yet constructed
-	if (!ViewCellsConstructed())
-		return 0;
-
-	int contributingSamples = 0;
-
-	if (castRay)
-		mBspTree->CastRay(ray);
-	
-	//if (mBspTree->bspIntersections.empty()) return 0;
-
-	Intersectable *tObject = 
-		!ray.intersections.empty() ? ray.intersections[0].mObject : NULL;
-
-	Intersectable *sObject = ray.sourceObject.mObject;
-
-	if (sObject || tObject)
-	{
-		// object can be seen from the view cell => add to view cell pvs
-		for (int j = 0; j < (int)ray.bspIntersections.size(); ++ j)
-		{ 	
-			BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
-		
-			// if ray not outside of view space
-			if (leaf->GetViewCell() != mBspTree->GetRootCell())
-			{
-				if (sObject)
-				{
-					contributingSamples += 
-						leaf->GetViewCell()->GetPvs().AddSample(sObject);
-				}
-
-				if (tObject)
-				{
-					contributingSamples += 
-						leaf->GetViewCell()->GetPvs().AddSample(tObject);
-				}
-			}
-		}
-	}
-
-	// rays passing through this viewcell
-	if (0)
-		for (int j = 1; j < ((int)ray.bspIntersections.size() - 1); ++ j) 
-		{
-			BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
-
-			if (leaf->GetViewCell() != mBspTree->GetRootCell())
-				leaf->GetViewCell()->
-					AddPassingRay(ray, contributingSamples ? 1 : 0);
-		}
-
-	return contributingSamples;
-}
-
-int BspViewCellsManager::PostProcess(const ObjectContainer &objects, 
-									 const RayContainer &rays)
-{
-	if (!ViewCellsConstructed())
+void
+ViewCellsManager::ComputeSampleContributions(VssRay &ray)
+{
+
+  ViewCellContainer viewcells;
+  
+  CastLineSegment(ray.mOrigin,
+				  ray.mTermination,
+				  viewcells
+				  );
+  
+  ViewCellContainer::const_iterator it = viewcells.begin();
+  for (; it != viewcells.end(); ++it) { 	
+	ViewCell *viewcell = *it;
+	
+	// if ray not outside of view space
+	float contribution;
+	bool added = 
+	  viewcell->GetPvs().AddSample(ray.mTerminationObject,
+								   contribution
+								   );
+	if (added)
+	  ray.mPvsContribution++;
+	
+	ray.mRelativePvsContribution += contribution;
+  }
+  
+}
+
+int
+BspViewCellsManager::CastLineSegment(const Vector3 &origin,
+									 const Vector3 &termination,
+									 ViewCellContainer &viewcells
+									 )
+{
+  return mBspTree->CastLineSegment(origin, termination, viewcells);
+}
+
+int
+BspViewCellsManager::PostProcess(const ObjectContainer &objects, 
+								 const VssRayContainer &rays)
+{
+  if (!ViewCellsConstructed())
 	{
 		Debug << "view cells not constructed" << endl;
@@ -391,6 +366,10 @@
 	long startTime = GetTime();
 
+	// $$JB we do not have connectivity information from the ray in the moment
+	// perhaps we could recast the rays or rember the cells traversed inside the
+	// vssray (which would on other hand create some overhead)
 	//-- merge or subdivide view cells
 	int merged = 0;
+#if 0
 
 	RayContainer::const_iterator rit, rit_end = rays.end();
@@ -427,5 +406,5 @@
 		}
 	}
-
+#endif
 	//-- stats and visualizations
 	cout << "finished" << endl;
@@ -443,40 +422,40 @@
 
 void BspViewCellsManager::Visualize(const ObjectContainer &objects,
-									const RayContainer &sampleRays)
-{
-	if (!ViewCellsConstructed())
-		return;
-
-	//-- 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 (exporter)
-		{
-			exporter->ExportBspViewCellPartition(*mBspTree, vcStats.maxPvs);
-			//exporter->ExportBspViewCellPartition(*mBspTree, 0);
-			delete exporter;
-		}
-
-		cout << "finished" << endl;
+									const VssRayContainer &sampleRays)
+{
+  if (!ViewCellsConstructed())
+	return;
+  
+  //-- 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 (exporter)
+		{
+		  exporter->ExportBspViewCellPartition(*mBspTree, vcStats.maxPvs);
+		  //exporter->ExportBspViewCellPartition(*mBspTree, 0);
+		  delete exporter;
+		}
+	  
+	  cout << "finished" << endl;
 	}	
-
-	//-- 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;
-	}
-
-	ExportBspPvs(objects, sampleRays);
+  
+  //-- 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;
+	}
+  
+  ExportBspPvs(objects, sampleRays);
 }
 
@@ -488,5 +467,5 @@
 
 void BspViewCellsManager::ExportSplits(const ObjectContainer &objects,
-									   const RayContainer &sampleRays)
+									   const VssRayContainer &sampleRays)
 {
 	Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
@@ -510,22 +489,21 @@
 		if (0)
 		{
-			RayContainer outRays;
-		
-			int raysSize = min((int)sampleRays.size(), mVisualizationSamples);
-
-			for (int i = 0; i < raysSize; ++ i)
-			{
-				// only rays piercing geometry
-				if (!sampleRays[i]->intersections.empty())
-					outRays.push_back(sampleRays[i]);
-			}
-			
-			// export rays 
-			exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
-		}
-
+		  VssRayContainer outRays;
+		  
+		  int raysSize = min((int)sampleRays.size(), mVisualizationSamples);
+		  
+		  for (int i = 0; i < raysSize; ++ i)
+			{
+			  // only rays piercing geometry
+			  outRays.push_back(sampleRays[i]);
+			}
+		  
+		  // export rays 
+		  exporter->ExportRays(outRays, RgbColor(1, 1, 0));
+		}
+		
 		if (0)
-			exporter->ExportGeometry(objects);
-
+		  exporter->ExportGeometry(objects);
+		
 		delete exporter;
 	}
@@ -533,116 +511,119 @@
 
 void BspViewCellsManager::ExportBspPvs(const ObjectContainer &objects,
-									   const RayContainer &sampleRays)
-{
-	const int leafOut = 10;
-	
-	ViewCell::NewMail();
-
-	//-- some rays for output
-	const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
-	Debug << "visualization using " << raysOut << " samples" << endl;
-	
-
-	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());	
-		
-		for (int i = 0; i < bspLeaves.size(); ++ i)
-		{
-			BspLeaf *leaf = bspLeaves[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) 
-			{
-				Ray *ray = sampleRays[k];
-
-				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
+									   const VssRayContainer &sampleRays)
+{
+  const int leafOut = 10;
+  
+  ViewCell::NewMail();
+  
+  //-- some rays for output
+  const int raysOut = min((int)sampleRays.size(), mVisualizationSamples);
+  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());	
+
+	  
+	  for (int i = 0; i < bspLeaves.size(); ++ i)
+		{
+		  BspLeaf *leaf = bspLeaves[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) 
+			{
+			  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()) 
+				  BspLeaf *leaf2 = ray->bspIntersections[j].mLeaf;
+				  
+				  if (leaf->GetViewCell() == leaf2->GetViewCell()) 
 					{
-						vcRays.push_back(ray);
+					  vcRays.push_back(ray);
 					}
 				}
 			}
-
-            Intersectable::NewMail();
-
-			BspViewCell *vc = dynamic_cast<BspViewCell *>(leaf->GetViewCell());
-
-			//bspLeaves[j]->Mail();
-			char s[64]; sprintf(s, "bsp-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);
-
-			if (vc->GetMesh())
-				exporter->ExportViewCell(vc);
-			else
-			{
-				PolygonContainer vcGeom;
-				// export view cell geometry
-				mBspTree->ConstructGeometry(vc, vcGeom);
-				exporter->ExportPolygons(vcGeom);
+		  
+		  Intersectable::NewMail();
+		  
+		  BspViewCell *vc = dynamic_cast<BspViewCell *>(leaf->GetViewCell());
+		  
+		  //bspLeaves[j]->Mail();
+		  char s[64]; sprintf(s, "bsp-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);
+		  
+		  if (vc->GetMesh())
+			exporter->ExportViewCell(vc);
+		  else
+			{
+			  PolygonContainer vcGeom;
+			  // export view cell geometry
+			  mBspTree->ConstructGeometry(vc, vcGeom);
+			  exporter->ExportPolygons(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));	
-			
-			m.mDiffuseColor = RgbColor(1, 0, 0);
-			exporter->SetForcedMaterial(m);
-
-			// exporter->SetWireframe();
-			exporter->SetFilled();
-
-			ViewCellPvsMap::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())
+		  
+		  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();
+		  
+		  ViewCellPvsMap::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();
+				  exporter->ExportIntersectable(intersect);
+				  intersect->Mail();
 				}			
 			}
-
-			DEL_PTR(exporter);
-			cout << "finished" << endl;
-		}
-	}
-	else
-	{
-		ViewCellContainer viewCells;
-		RayContainer vcRays;
-
-		mBspTree->CollectViewCells(viewCells);
-		stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
-
-		int limit = min(leafOut, (int)viewCells.size()); 
-		
-		for (int i = 0; i < limit; ++ i)
-		{
-			cout << "creating output for view cell " << i << " ... ";
+		  
+		  DEL_PTR(exporter);
+		  cout << "finished" << endl;
+		}
+	}
+  else
+	{
+	  ViewCellContainer viewCells;
+	  RayContainer vcRays;
+	  
+	  mBspTree->CollectViewCells(viewCells);
+	  stable_sort(viewCells.begin(), viewCells.end(), vc_gt);
+	  
+	  int limit = min(leafOut, (int)viewCells.size()); 
+	  
+	  for (int i = 0; i < limit; ++ i)
+		{
+		  cout << "creating output for view cell " << i << " ... ";
 			
             Intersectable::NewMail();
@@ -721,4 +702,5 @@
 		}
 	}
+#endif
 }
 
@@ -822,5 +804,5 @@
 
 int KdViewCellsManager::PostProcess(const ObjectContainer &objects, 
-									const RayContainer &rays)
+									const VssRayContainer &rays)
 {
 	return 0;
@@ -828,5 +810,5 @@
 
 void KdViewCellsManager::Visualize(const ObjectContainer &objects,
-								   const RayContainer &sampleRays)
+								   const VssRayContainer &sampleRays)
 {
 	if (!ViewCellsConstructed())
@@ -837,26 +819,24 @@
 	int limit = min(mVisualizationSamples, (int)sampleRays.size());
 
-	RayContainer *rays = new RayContainer[pvsOut];
+	VssRayContainer *rays = new VssRayContainer[pvsOut];
 
 	for  (int i = 0; i < limit; ++ i)
 	{
-		Ray *ray = sampleRays[i];
-
-		if (!ray->intersections.empty()) 
-		{
-			// check whether we can add this to the rays
-			for (int j = 0; j < pvsOut; j++) 
-			{
-				if (objects[j] == ray->intersections[0].mObject) 
-				{
-					rays[j].push_back(ray);
-				}
-			}
-		}
-	}
+	  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 = NULL;
 		exporter = Exporter::GetExporter("sample-rays.x3d");
 		exporter->SetWireframe();
@@ -864,5 +844,5 @@
 		
 		for (i=0; i < pvsOut; i++) 
-			exporter->ExportRays(rays[i], 1000, RgbColor(1, 0, 0));
+			exporter->ExportRays(rays[i], RgbColor(1, 0, 0));
 		exporter->SetFilled();
 		
@@ -893,5 +873,5 @@
 		}
 
-		exporter->ExportRays(rays[k], 1000, RgbColor(0, 1, 0));
+		exporter->ExportRays(rays[k],  RgbColor(0, 1, 0));
 		exporter->SetFilled();
 		 
@@ -914,52 +894,4 @@
 }
 
-int KdViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay)
-{
-	// view cells not yet constructed
-	if (!ViewCellsConstructed())
-		return 0;
-
-	if (castRay)
-		mKdTree->CastRay(ray);
-
-	if (ray.kdLeaves.empty())
-		return 0;
-
-	Intersectable *tObject =
-		!ray.intersections.empty() ? ray.intersections[0].mObject : NULL;
-
-	Intersectable *sObject =
-		ray.sourceObject.mObject;
-
-	int contributingSamples = 0;
-	
-	int objects = 0;
-
-	if (sObject)
-		objects++;
-	if (tObject)
-		objects++;
-
-	for (int j = 1; j < ((int)ray.kdLeaves.size() - 1); ++ j) 
-	{
-		ray.kdLeaves[j]->AddPassingRay2(ray, objects,
-										(int)ray.kdLeaves.size());
-	}
-
-	if (!objects)
-		return 0;
-	
-	for (int j=0; j < ray.kdLeaves.size(); j++) 
-	{
-		KdNode *node = GetNodeForPvs(ray.kdLeaves[j]);
-
-		if (sObject)
-			contributingSamples += sObject->mKdPvs.AddSample(node);
-		if (tObject)
-			contributingSamples += tObject->mKdPvs.AddSample(node);
-	}
-
-	return contributingSamples;
-}
 
 
@@ -976,4 +908,15 @@
 void KdViewCellsManager::PrintStatistics(ostream &s) const
 {
+}
+
+
+int
+KdViewCellsManager::CastLineSegment(const Vector3 &origin,
+									const Vector3 &termination,
+									ViewCellContainer &viewcells
+									)
+{
+
+  return 0;
 }
 
@@ -1031,34 +974,21 @@
 }
 
-int VspKdViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay)
-{
-	// view cells not yet constructed
+
+ViewCell *VspKdViewCellsManager::GenerateViewCell(Mesh *mesh) const
+{
+	return new VspKdViewCell(mesh);
+}
+
+int VspKdViewCellsManager::PostProcess(const ObjectContainer &objects, 
+									   const VssRayContainer &rays)
+{
 	if (!ViewCellsConstructed())
 		return 0;
 
-//	if (castRay)
-//		mVspKdTree->CastRay(ray);
-
-	int sampleContributions = 0;
-
-	return sampleContributions;
-}
-
-ViewCell *VspKdViewCellsManager::GenerateViewCell(Mesh *mesh) const
-{
-	return new VspKdViewCell(mesh);
-}
-
-int VspKdViewCellsManager::PostProcess(const ObjectContainer &objects, 
-									   const RayContainer &rays)
-{
-	if (!ViewCellsConstructed())
-		return 0;
-
 	return mVspKdTree->MergeLeaves();
 }
 
 void VspKdViewCellsManager::Visualize(const ObjectContainer &objects,
-									  const RayContainer &sampleRays)
+									  const VssRayContainer &sampleRays)
 {
 	if (!ViewCellsConstructed())
@@ -1079,5 +1009,5 @@
 
 		if (exportRays) 
-		{
+		  {
 			int raysSize = 2000;
 			float prob = raysSize / (float)sampleRays.size();
@@ -1085,5 +1015,5 @@
 			exporter->SetWireframe();
 			
-			RayContainer rays;
+			VssRayContainer rays;
 		
 			for (int i = 0; i < sampleRays.size(); ++ i)
@@ -1093,5 +1023,5 @@
 			}
 
-			exporter->ExportRays(rays, 1000, RgbColor(1, 0, 0));
+			exporter->ExportRays(rays, RgbColor(1, 0, 0));
 		}
 
@@ -1165,12 +1095,12 @@
 		exporter->SetWireframe();
 			
-		RayContainer rays;
+		VssRayContainer rays;
 		
 		for (int i = 0; i < sampleRays.size(); ++ i)
 		{
-			if (RandomValue(0,1) < prob)
-				rays.push_back(sampleRays[i]);
-		}
-		exporter->ExportRays(rays, 1000, RgbColor(1, 0, 0));
+		  if (RandomValue(0,1) < prob)
+			rays.push_back(sampleRays[i]);
+		}
+		exporter->ExportRays(rays, RgbColor(1, 0, 0));
 	}
 
@@ -1191,4 +1121,14 @@
 }
 
+
+int
+VspKdViewCellsManager::CastLineSegment(const Vector3 &origin,
+									   const Vector3 &termination,
+									   ViewCellContainer &viewcells
+									   )
+{
+  
+  return 0;
+}
 
 /**********************************************************************/
@@ -1252,61 +1192,7 @@
 }
 
-int VspBspViewCellsManager::ComputeSampleContributions(Ray &ray, const bool castRay)
-{
-	// view cells not yet constructed
-	if (!ViewCellsConstructed())
-		return 0;
-
-	int contributingSamples = 0;
-
-	if (castRay)
-		mVspBspTree->CastRay(ray);
-
-	Intersectable *tObject = 
-		!ray.intersections.empty() ? ray.intersections[0].mObject : NULL;
-
-	Intersectable *sObject = ray.sourceObject.mObject;
-
-	if (sObject || tObject)
-	{
-		// object can be seen from the view cell => add to view cell pvs
-		for (int j = 0; j < (int)ray.bspIntersections.size(); ++ j)
-		{ 	
-			BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
-		
-			// if ray not in unbounded space
-			if (leaf->GetViewCell() != mVspBspTree->GetRootCell())
-			{
-				if (sObject)
-				{
-					contributingSamples += 
-						leaf->GetViewCell()->GetPvs().AddSample(sObject);
-				}
-
-				if (tObject)
-				{
-					contributingSamples += 
-						leaf->GetViewCell()->GetPvs().AddSample(tObject);
-				}
-			}
-		}
-	}
-
-	// rays passing through this viewcell
-	if (0)
-		for (int j = 1; j < ((int)ray.bspIntersections.size() - 1); ++ j) 
-		{
-			BspLeaf *leaf = ray.bspIntersections[j].mLeaf;
-
-			if (leaf->GetViewCell() != mVspBspTree->GetRootCell())
-				leaf->GetViewCell()->
-					AddPassingRay(ray, contributingSamples ? 1 : 0);
-		}
-
-	return contributingSamples;
-}
 
 int VspBspViewCellsManager::PostProcess(const ObjectContainer &objects, 
-										const RayContainer &rays)
+										const VssRayContainer &rays)
 {
 	if (!ViewCellsConstructed())
@@ -1356,6 +1242,10 @@
 
 
+	// $$JB we do not have connectivity information from the ray in the moment
+	// perhaps we could recast the rays or rember the cells traversed inside the
+	// vssray (which would on other hand create some overhead)
 	//-- merge or subdivide view cells
 	int merged = 0;
+#if 0
 
 	RayContainer::const_iterator rit, rit_end = rays.end();
@@ -1366,12 +1256,12 @@
 	for (int i = 0; i < limit; ++ i)
 	{  
-		Ray *ray = rays[i];
-
-		// traverse leaves stored in the rays and compare and merge consecutive
-		// leaves (i.e., the neighbors in the tree)
-		if (ray->bspIntersections.size() < 2)
-			continue;
-
-		iit = ray->bspIntersections.begin();
+	  VssRay *ray = rays[i];
+	  
+	  // traverse leaves stored in the rays and compare and merge consecutive
+	  // leaves (i.e., the neighbors in the tree)
+	  if (ray->bspIntersections.size() < 2)
+		continue;
+	  
+	  iit = ray->bspIntersections.begin();
 
 		BspLeaf *previousLeaf = (*iit).mLeaf;
@@ -1392,5 +1282,5 @@
 		}
 	}
-
+#endif
 	//-- stats and visualizations
 	cout << "finished" << endl;
@@ -1409,5 +1299,5 @@
 
 void VspBspViewCellsManager::Visualize(const ObjectContainer &objects,
-									   const RayContainer &sampleRays)
+									   const VssRayContainer &sampleRays)
 {
 	if (!ViewCellsConstructed())
@@ -1448,5 +1338,5 @@
 
 void VspBspViewCellsManager::ExportSplits(const ObjectContainer &objects,
-										  const RayContainer &sampleRays)
+										  const VssRayContainer &sampleRays)
 {
 	Exporter *exporter = Exporter::GetExporter("bsp_splits.x3d");
@@ -1471,5 +1361,5 @@
 		if (0)
 		{
-			RayContainer outRays;
+		  VssRayContainer outRays;
 		
 			int raysSize = min((int)sampleRays.size(), mVisualizationSamples);
@@ -1478,10 +1368,9 @@
 			{
 				// only rays piercing geometry
-				if (!sampleRays[i]->intersections.empty())
-					outRays.push_back(sampleRays[i]);
+			  outRays.push_back(sampleRays[i]);
 			}
 			
 			// export rays 
-			exporter->ExportRays(outRays, 1000, RgbColor(1, 1, 0));
+			exporter->ExportRays(outRays, RgbColor(1, 1, 0));
 		}
 
@@ -1494,5 +1383,5 @@
 
 void VspBspViewCellsManager::ExportBspPvs(const ObjectContainer &objects,
-										  const RayContainer &sampleRays)
+										  const VssRayContainer &sampleRays)
 {
 	const int leafOut = 10;
@@ -1514,11 +1403,13 @@
 		for (int i = 0; i < (int)vspBspLeaves.size(); ++ i)
 		{
-			RayContainer vcRays;
-			cout << "creating output for view cell " << i << " ... ";
-			
+		  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) 
 			{
-				Ray *ray = sampleRays[k];
+			  VssRay *ray = sampleRays[k];
 
 				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
@@ -1532,5 +1423,5 @@
 				}
 			}
-			
+#endif
 
 			Intersectable::NewMail();
@@ -1570,5 +1461,5 @@
 
 			// export rays piercing this view cell
-			exporter->ExportRays(vcRays, 1000, RgbColor(1, 0, 0));
+			exporter->ExportRays(vcRays, RgbColor(1, 0, 0));
 			exporter->ExportRays(vspBspLeaves[i]->mVssRays, RgbColor(1, 1, 1));
 			
@@ -1621,13 +1512,15 @@
 		{
 			cout << "creating output for view cell " << i << " ... ";
-			RayContainer vcRays;
+			VssRayContainer vcRays;
             Intersectable::NewMail();
 			BspViewCell *vc = dynamic_cast<BspViewCell *>(viewCells[i]);
 
 			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) 
 			{
-				Ray *ray = sampleRays[k];
+			  Ray *ray = sampleRays[k];
 
 				for	(int j = 0; j < (int)ray->bspIntersections.size(); ++ j)
@@ -1641,5 +1534,5 @@
 				}
 			}
-
+#endif
 			//bspLeaves[j]->Mail();
 			char s[64]; sprintf(s, "bsp-pvs%04d.x3d", i);
@@ -1670,5 +1563,5 @@
 			
 			// export rays piercing this view cell
-			exporter->ExportRays(vcRays, 1000, RgbColor(0, 1, 0));
+			exporter->ExportRays(vcRays, RgbColor(0, 1, 0));
 	
 			m.mDiffuseColor = RgbColor(1, 0, 0);
@@ -1771,2 +1664,11 @@
 }
 
+
+int
+VspBspViewCellsManager::CastLineSegment(const Vector3 &origin,
+										const Vector3 &termination,
+										ViewCellContainer &viewcells
+										)
+{
+  return mVspBspTree->CastLineSegment(origin, termination, viewcells);
+}
