Index: trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/scripts/default.env	(revision 313)
@@ -56,5 +56,5 @@
 
 Sampling {
-	totalSamples	100000
+	totalSamples	200000
 	samplesPerPass  10
 }
@@ -62,9 +62,10 @@
 ViewCells {
 	hierarchyType bspTree
-	height 5.0
+	height 7.0
+	maxViewCells 20
 	#hierarchyType kdTree
 	#hierarchyType sceneDependent
-#	filename ../data/atlanta/atlanta_viewcells_large.x3d
-	filename ../data/vienna/viewcells-25-sel.x3d
+	filename ../data/atlanta/atlanta_viewcells_large.x3d
+#	filename ../data/vienna/viewcells-25-sel.x3d
 #	filename ../data/vienna/viewcells-25.x3d
 #	filename ../data/vienna/viewcells-large-sel.x3d
@@ -84,5 +85,5 @@
 	# vertical axis        = 64
 	
-	splitPlaneStrategy 8
+	splitPlaneStrategy 66
 	# least splits + balanced polygons
 	#splitPlaneStrategy 12
@@ -101,5 +102,5 @@
 	
 	maxCandidates 50
-	maxViewCells 999999
+	
 	Termination {
 		maxPolysForAxisAligned 100
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCell.cpp	(revision 313)
@@ -69,5 +69,5 @@
 	// add base vertices and calculate top vertices
 	for (int i = 0; i < 3; ++i)
-		 mesh->mVertices.push_back(baseTri.mVertices[i] + height * 0.5 * triNorm);
+		 mesh->mVertices.push_back(baseTri.mVertices[i]);
 	
 	// add top vertices	
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h	(revision 313)
@@ -30,6 +30,6 @@
     BspRayTraversalData() {}
 
-    BspRayTraversalData(BspNode *n, const Vector3 &p, const float maxt):
-    mNode(n), mExitPoint(p), mMaxT(maxt) 
+    BspRayTraversalData(BspNode *n, const Vector3 &extp, const float maxt):
+    mNode(n), mExitPoint(extp), mMaxT(maxt) 
 	{}
 };
@@ -239,6 +239,5 @@
 protected:
 
-	/// polygonal representation of this viewcell
-	/// if NULL this note does not correspond to feasible viewcell
+	/// if NULL this does not correspond to feasible viewcell
 	ViewCell *mViewCell;
 };
@@ -457,6 +456,6 @@
 		@param coincident container of polygons coincident to the split plane
 		@param splitPlane the split plane which decides about back and front
+		@param extractBack if a back view cell is extracted
 		@param extractFront if a front view cell is extracted
-		@param extractBack if a back view cell is extracted
 	*/
 	void ExtractViewCells(ViewCell **backViewCell, 
@@ -464,6 +463,6 @@
 						  const PolygonContainer &coincident,
 						  const Plane3 splitPlane, 
-						  const bool extractFront, 
-						  const bool extractBack) const;
+						  const bool extractBack,
+						  const bool extractFront) const;
 	
 	/** Computes best cost ratio for the suface area heuristics for axis aligned
@@ -518,5 +517,5 @@
 
 	/// view cell corresponding to unbounded space
-	ViewCell *mViewCell;
+	ViewCell *mRootCell;
 
 public:
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Environment.cpp	(revision 313)
@@ -1195,8 +1195,8 @@
 	  "20");
 
-  RegisterOption("BspTree.maxViewCells",
+  RegisterOption("ViewCells.maxViewCells",
 	  optInt,
-	  "-bsp_max_viewcells=",
-	  "999999");
+	  "-view_cells_max_viewcells=",
+	  "0");
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Exporter.h	(revision 313)
@@ -55,5 +55,5 @@
 
   virtual void 
-  ExportViewCells(ViewCellContainer *viewCells) = 0;
+  ExportViewCells(const ViewCellContainer &viewCells) = 0;
   virtual void 
   ExportViewCell(ViewCell *viewCell) = 0;
@@ -61,6 +61,9 @@
   ExportIntersectable(Intersectable *object) = 0;
 
-  virtual void ExportBspSplits(const BspTree &tree) = 0;
-
+  virtual void 
+  ExportBspSplitPlanes(const BspTree &tree) = 0;
+  virtual void 
+  ExportBspSplits(const BspTree &tree) = 0;
+	
   void SetExportRayDensity(const bool d) { mExportRayDensity = d; }
   
Index: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp	(revision 313)
@@ -261,5 +261,5 @@
 BspTree::BspTree(ViewCell *viewCell): 
 mRoot(NULL), 
-mViewCell(viewCell),
+mRootCell(viewCell),
 //mStoreSplitPolys(true)
 mStoreSplitPolys(false)
@@ -380,5 +380,5 @@
 	BspNode *firstNode = mRoot ? mRoot : new BspLeaf();
 
-	tStack.push(BspTraversalData(firstNode, polys, 0, mViewCell));
+	tStack.push(BspTraversalData(firstNode, polys, 0, mRootCell));
 
 	while (!tStack.empty())
@@ -410,6 +410,6 @@
 				
 				// extract view cells associated with the split polygons
-				ViewCell *frontViewCell = mViewCell;
-				ViewCell *backViewCell = mViewCell;
+				ViewCell *frontViewCell = mRootCell;
+				ViewCell *backViewCell = mRootCell;
 	
 				if (!viewCells)
@@ -517,5 +517,5 @@
 		{
 			mBox.Include(object->GetBox()); // add to BSP tree aabb
-			AddMeshToPolygons(mesh, polys, mViewCell);
+			AddMeshToPolygons(mesh, polys, mRootCell);
 		}
 	}
@@ -561,5 +561,5 @@
 	std::stack<BspTraversalData> tStack;
 	
-	BspTraversalData tData(new BspLeaf(), polys, 0, mViewCell);
+	BspTraversalData tData(new BspLeaf(), polys, 0, mRootCell);
 	tStack.push(tData);
 
@@ -601,13 +601,10 @@
 			  << (int)tData.mPolygons->size() << endl;
 #endif
-
 		EvaluateLeafStats(tData);
-
 		BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
+
+		// add view cell to leaf
+		leaf->SetViewCell(tData.mViewCell);
 		
-		// add view cell to leaf
-		if (!leaf->GetViewCell())
-			leaf->SetViewCell(tData.mViewCell);
-
 		// remaining polygons are discarded or added to node
 		leaf->ProcessPolygons(tData.mPolygons, mStoreSplitPolys);
@@ -629,6 +626,6 @@
 										  &coincident);
 
-	ViewCell *frontViewCell = mViewCell;
-	ViewCell *backViewCell = mViewCell;
+	ViewCell *frontViewCell = mRootCell;
+	ViewCell *backViewCell = mRootCell;
 
 #ifdef _DEBUG	
@@ -639,7 +636,12 @@
 #endif
 
-	//-- extract view cells from coincident polygons according to plane normal
-   	ExtractViewCells(&backViewCell, &frontViewCell, coincident, interior->mPlane, 
-						 frontPolys->empty(), backPolys->empty());
+	// extract view cells from coincident polygons according to plane normal
+    // only if front or back polygons are empty
+   	ExtractViewCells(&backViewCell, 
+					 &frontViewCell, 
+					 coincident, 
+					 interior->mPlane, 
+					 backPolys->empty(),
+					 frontPolys->empty());
 	
 	// don't need coincident polygons anymore
@@ -647,7 +649,6 @@
 
 	// push the children on the stack
-	// split polygon is only needed in the back leaf
+	tStack.push(BspTraversalData(interior->GetBack(), backPolys, tData.mDepth + 1, backViewCell));
 	tStack.push(BspTraversalData(interior->GetFront(), frontPolys, tData.mDepth + 1, frontViewCell));
-	tStack.push(BspTraversalData(interior->GetBack(), backPolys, tData.mDepth + 1, backViewCell));
 
 	// cleanup
@@ -662,6 +663,6 @@
 							   const PolygonContainer &coincident,
 							   const Plane3 splitPlane, 
-							   const bool extractFront, 
-							   const bool extractBack) const
+							   const bool extractBack,
+							   const bool extractFront) const
 {
 	bool foundFront = !extractFront, foundBack = !extractBack;
@@ -673,5 +674,5 @@
 		(it != it_end); ++ it)
 	{
-		if (DotProd((*it)->GetSupportingPlane().mNormal, splitPlane.mNormal > 0))
+		if (DotProd((*it)->GetSupportingPlane().mNormal, splitPlane.mNormal) > 0)
 		{
 			*backViewCell = dynamic_cast<ViewCell *>((*it)->mParent);
@@ -1178,12 +1179,16 @@
 	// test with tree bounding box
 	if (!mBox.GetMinMaxT(ray, &mint, &maxt))
-	{
 		return 0;
-	}
-
-	if (mint < 0)
+
+	if (mint < 0) // start ray from origin
 		mint = 0;
-  
-	maxt += Limits::Threshold;
+
+	// bound ray
+	if ((ray.GetType() == Ray::LOCAL_RAY)   && 
+		((int)ray.intersections.size() > 0) &&
+	    (ray.intersections[0].mT <= maxt))
+	{
+		maxt = ray.intersections[0].mT;
+    }
   
 	Vector3 entp = ray.Extrap(mint);
@@ -1191,5 +1196,5 @@
   
 	BspNode *node = mRoot;
-	BspNode *farChild;
+	BspNode *farChild = NULL;
 	
 	while (1)
@@ -1213,5 +1218,5 @@
 				node = in->GetBack();
 
-				if(extSide <= 0) 
+				if(extSide <= 0) // plane does not split ray => no far child
 					continue;
 					
@@ -1222,8 +1227,8 @@
 				node = in->GetFront();
 
-				if (extSide >= 0)
+				if (extSide >= 0) // plane does not split ray => no far child
 					continue;
 
-				farChild = in->GetBack();	 // plane splits ray			
+				farChild = in->GetBack(); // plane splits ray			
 			}
 			else // ray and plane are coincident // WHAT TO DO IN THIS CASE ?
@@ -1236,30 +1241,31 @@
 			tStack.push(BspRayTraversalData(farChild, extp, maxt));
 
-			// find intersection with plane between ray origin and exit point
+			// find intersection of ray segment with plane
 			extp = splitPlane->FindIntersection(ray.GetLoc(), extp, &maxt);
+			
 		} else // reached leaf => intersection with view cell
 		{
 			BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
-			
-			//ray.mBspLeaves.push_back(leaf);
-			//Debug << "adding view cell" << leaf->mViewCell;
+      
 			if (!leaf->mViewCell->Mailed())
 			{
 				ray.viewCells.push_back(leaf->mViewCell);
-				mViewCell->Mail();
+				leaf->mViewCell->Mail();
 				++ hits;
 			}
-			//Debug << "view cell added" << endl;
-   			// get the next node from the stack
+			
+   			//-- fetch the next far child from the stack
 			if (tStack.empty())
 				break;
       
 			entp = extp;
-			mint = maxt;
-			BspRayTraversalData &s  = tStack.top();
+			mint = maxt; // NOTE: need this?
+			BspRayTraversalData &s = tStack.top();
 
 			node = s.mNode;
 			extp = s.mExitPoint;
 			maxt = s.mMaxT;
+
+			//Debug << "leaf: new entrypt: " << entp << " new extp: " << extp << endl;
 
 			tStack.pop();
Index: trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/Preprocessor.cpp	(revision 313)
@@ -28,7 +28,20 @@
 {
 	X3dParser parser;
+	int maxViewCells = 0;
 	environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight);
-	 
-	return parser.ParseFile(filename, mViewCells);
+	environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
+
+	bool loaded = parser.ParseFile(filename, mViewCells);
+
+	if (maxViewCells > 0)
+	{
+		while (mViewCells.size() > maxViewCells)
+		{
+			ViewCell *vc = mViewCells.back();
+			DEL_PTR(vc);
+			mViewCells.pop_back();
+		}
+	}
+	return loaded;
 }
 
@@ -84,5 +97,5 @@
 
 	int maxViewCells = 0;
-	environment->GetIntValue("BspTree.maxViewCells", maxViewCells);
+	environment->GetIntValue("ViewCells.maxViewCells", maxViewCells);
 
 	mSceneGraph->CollectObjects(&objects);
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.h	(revision 313)
@@ -50,5 +50,5 @@
   ExportPolygon(Polygon3 *poly);
 
-  virtual void ExportPolygons(PolygonContainer *polys);
+  virtual void ExportPolygons(const PolygonContainer &polys);
 
   virtual bool
@@ -68,7 +68,11 @@
 
   virtual void 
-  ExportViewCells(ViewCellContainer *viewCells);
+  ExportViewCells(const ViewCellContainer &viewCells);
 
-  virtual void ExportBspSplits(const BspTree &tree);
+  virtual void 
+  ExportBspSplitPlanes(const BspTree &tree);
+  
+  virtual void 
+  ExportBspSplits(const BspTree &tree);
 
   static ViewCellContainer foundViewCells; // todo: remove this
Index: trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/main.cpp	(revision 313)
@@ -63,6 +63,17 @@
 	  if (exporter) 
 	  {	
-		  Debug << "Exporting bsp splits" << endl;
+          Debug << "Exporting bsp splits" << endl;
+		  Material m; 
+		  m.mDiffuseColor = RgbColor(1, 0, 0);
+		  exporter->SetForcedMaterial(m);
+		  exporter->SetWireframe();
 		  exporter->ExportBspSplits(*p->mBspTree);
+
+		  m.mDiffuseColor = RgbColor(0, 1, 0);
+		  exporter->SetForcedMaterial(m);
+		  exporter->SetFilled();
+
+		  exporter->ExportViewCells(p->mViewCells);
+
 		  delete exporter;
 	  }
@@ -86,5 +97,5 @@
 	  {	
 		  Debug << "Exporting complementary view cells" << endl;
-		  exporter->ExportViewCells(&vc_compl); // export view cells
+		  exporter->ExportViewCells(vc_compl); // export view cells
 		  delete exporter;
 	  }
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp	(revision 313)
@@ -120,11 +120,13 @@
 }
 
-void X3dExporter::ExportViewCells(ViewCellContainer *viewCells)
-{
-	ViewCellContainer::iterator it, it_end = viewCells->end();
-
-	for (it = viewCells->begin(); it != it_end; ++it)
+void 
+X3dExporter::ExportViewCells(const ViewCellContainer &viewCells)
+{
+	ViewCellContainer::const_iterator it, it_end = viewCells.end();
+
+	for (it = viewCells.begin(); it != it_end; ++ it)
 		ExportViewCell(*it);
 }
+
 void
 X3dExporter::ExportViewCell(ViewCell *viewCell)
@@ -274,5 +276,5 @@
 }
 
-void X3dExporter::ExportPolygons(PolygonContainer *polys)
+void X3dExporter::ExportPolygons(const PolygonContainer &polys)
 {
 	stream << "<Shape>" << endl;
@@ -314,5 +316,5 @@
     VertexContainer::const_iterator vi;  
 	
-	for (pit = polys->begin(); pit != polys->end(); ++pit)
+	for (pit = polys.begin(); pit != polys.end(); ++pit)
 	{
 		Polygon3 *poly = *pit;
@@ -327,5 +329,5 @@
 	
 	stream << "<Coordinate  point=\"" << endl;
-	for (pit = polys->begin(); pit != polys->end(); ++pit)
+	for (pit = polys.begin(); pit != polys.end(); ++pit)
 	{
 		Polygon3 *poly = *pit;
@@ -453,5 +455,5 @@
 		ViewCellContainer::iterator new_end = unique(foundViewCells.begin(), foundViewCells.end());
 		foundViewCells.erase(new_end, foundViewCells.end());
-		ExportViewCells(&foundViewCells);
+		ExportViewCells(foundViewCells);
 
 		Debug << "Number of view cells after erasing dublicates: " << (int)foundViewCells.size() << endl;
@@ -533,5 +535,6 @@
 			if (vc->mPassingRays.mRays) 
 			{
-				float importance = vc->mPassingRays.mContributions/(float)vc->mPassingRays.mRays;
+				float importance = 
+					vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
 
 				mForcedMaterial.mDiffuseColor.r = importance;
@@ -616,18 +619,16 @@
 	BspNode *mNode;
 
-	PolygonContainer *mPolygons;
 	vector<Plane3 *> mPlanes;
 	vector<bool> mSides;
 	bool mIsFront;
 
-	BspSplitData(BspNode *node, PolygonContainer *polys): 
-	mNode(node), mPolygons(polys), mIsFront(false)
+	BspSplitData(BspNode *node): 
+	mNode(node), mIsFront(false)
 	{};	
 	BspSplitData(BspNode *node, 
-				 PolygonContainer *polys, 
 				 vector<Plane3 *> planes, 
 				 vector<bool> sides, 
 				 bool isFront): 
-	mNode(node), mPolygons(polys), mPlanes(planes), 
+	mNode(node), mPlanes(planes), 
 	mSides(sides), mIsFront(isFront)
 	{};
@@ -636,11 +637,11 @@
 void X3dExporter::ExportBspSplits(const BspTree &tree)
 {
-	int mask = Random(50000);
-	
 	std::stack<BspSplitData> tStack;
 
-	BspSplitData tData(tree.GetRoot(), new PolygonContainer());
+	BspSplitData tData(tree.GetRoot());
 	tStack.push(tData);
   
+	PolygonContainer polys;
+
 	while (!tStack.empty())
 	{
@@ -652,25 +653,10 @@
 		{
 			BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
-
-			tData.mPlanes.push_back(interior->GetPlane());
-			tData.mSides.push_back(tData.mIsFront);
+			if (tData.mNode != tree.GetRoot())
+				tData.mSides.push_back(tData.mIsFront); // add current side
 
 			// bounded plane is added to the polygons
-			Polygon3 *planePoly = tree.GetBoundingBox().BoundPlane(*tData.mPlanes.back());
+			Polygon3 *planePoly = tree.GetBoundingBox().BoundPlane(*interior->GetPlane());
 		
-			PolygonContainer *frontPolys = new PolygonContainer();
-			PolygonContainer *backPolys = new PolygonContainer();
-			PolygonContainer coincident;
-
-			int splits = 0;
-		
-			// split polygons with respect to split plane
-			interior->SplitPolygons(tData.mPolygons, 
-									frontPolys, 
-									backPolys,
-									&coincident,
-									splits, 
-									false);
-
 			// do all the splits with the previous planes
 			for (int i = 0; i < (int)tData.mPlanes.size(); ++i)
@@ -692,5 +678,4 @@
 					else
 					{
-						Debug << "take backpoly" << endl;
 						planePoly = backPoly;
 						DEL_PTR(frontPoly);
@@ -699,51 +684,49 @@
 			}
 
-			PolygonContainer *polys;
-			BspNode *next;
-			bool side = false;
-
-			// random decision
-			if (mask & 1)
-			{
-				next = interior->GetBack();
-				polys = backPolys;
-				CLEAR_CONTAINER(*frontPolys);
-				DEL_PTR(frontPolys);
-				side = false;
-			}
+			tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
+			if (planePoly->Valid())
+				polys.push_back(planePoly);
 			else
 			{
-				next = interior->GetFront();
-				polys = frontPolys;
-				
-				CLEAR_CONTAINER(*backPolys);
-				DEL_PTR(backPolys);
-				side = true;
+				Debug << "polygon not valid: " << *planePoly << " size: " << planePoly->mVertices.size() << endl;
+				DEL_PTR(planePoly);
 			}
-
-			mask = mask >> 1;
-			polys->push_back(planePoly);
-
 			// push the children on the stack
-			tStack.push(BspSplitData(next, polys, tData.mPlanes, 
-									 tData.mSides, side));
-			//tStack.push(BspSplitData(interior->GetBack(), backPolys, 
-			//						 tData.mPlanes, tData.mSides, false));
-
-			// clean up
-			CLEAR_CONTAINER(coincident); // NOTE: should contain nothing
-			CLEAR_CONTAINER(*tData.mPolygons);
-			DEL_PTR(tData.mPolygons);
+			tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes, tData.mSides, true));
+			tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes, tData.mSides, false));
 		}
-		else // reached leaf
+	}	
+	ExportPolygons(polys);
+	CLEAR_CONTAINER(polys);
+}
+
+void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
+{
+	std::stack<BspNode *> tStack;
+
+	tStack.push(tree.GetRoot());
+  
+	PolygonContainer polys;
+
+	while (!tStack.empty())
+	{
+		// filter polygons donw the tree
+		BspNode *node = tStack.top();
+	    tStack.pop();	
+		
+		if (!node->IsLeaf())
 		{
-			//Debug << "\nsplits: " << endl;
-			//for (int i = 0; i < (int)tData.mSides.size(); ++ i)Debug << " " << tData.mSides[i];
-			ExportPolygons(tData.mPolygons);
-			
-			// clean up
-			CLEAR_CONTAINER(*tData.mPolygons);
-			DEL_PTR(tData.mPolygons);
+			BspInterior *interior = dynamic_cast<BspInterior *>(node);
+
+			// bounded plane is added to the polygons
+			polys.push_back(tree.GetBoundingBox().BoundPlane(*interior->GetPlane()));
+		
+			// push the children on the stack
+			tStack.push(interior->GetBack());
+			tStack.push(interior->GetFront());
 		}
 	}
-}
+
+	ExportPolygons(polys);
+	CLEAR_CONTAINER(polys);
+}
Index: trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/SamplingPreprocessor.cpp	(revision 313)
@@ -67,12 +67,16 @@
  
 	// add object to view cell
-	for (j=0; j < ray.viewCells.size(); j++) 
+	for (j=0; j < ray.viewCells.size(); ++ j) 
 	{ 
 		ViewCell *vc = ray.viewCells[j];
+
+		for (int i = 0; i < 5; ++ i)
+			if (vc == mViewCells[i])
+				Debug << "adding sample to vc " << i << endl;
 		contributingSamples += vc->GetPvs().AddSample(obj);
 	}
  
 	if (mPass > 1)
-		for (j=1; j < ((int)ray.viewCells.size() - 1); j++) 
+		for (j=1; j < ((int)ray.viewCells.size() - 1); ++ j) 
 		{
             ray.viewCells[j]->AddPassingRay(ray, contributingSamples ? 1 : 0);
@@ -112,5 +116,5 @@
 		sampleContributions += AddObjectSamples(object, ray);
 				
-		if (ray.intersections.size() > 0) // view cell found
+		if (ray.intersections.size() > 0) // second intersection found
 		{
 			sampleContributions += 
@@ -230,7 +234,9 @@
 
   int pvsOut = Min((int)objects.size(), 10);
-  
+  int vcPvsOut = Min((int)mViewCells.size(), 5);
+
   vector<Ray> rays[10];
-  
+  vector<Ray> vcRays[5];
+
   while (totalSamples < mTotalSamples) {
 		int passContributingSamples = 0;
@@ -330,4 +336,15 @@
 					sampleContributions = CastRay(object, ray);
 					
+					if (mViewCellsType == BSP_VIEW_CELLS) 
+					{
+						// check whether we can add this to the rays
+						for (int k = 0; k < ray.viewCells.size(); ++ k)
+							for (int j = 0; j < vcPvsOut; ++ j) 
+								if (mViewCells[j] == ray.viewCells[k]) 
+								{
+									Debug << "Pushing back ray" << endl;
+									vcRays[j].push_back(ray);
+								}
+					}
 				}
 			} else {
@@ -350,5 +367,5 @@
 				}
 			}
-		
+			
 			passSamples++;
 			
@@ -369,12 +386,10 @@
 		int pvsSize = 0;
         
-		if (mViewCellsType == BSP_VIEW_CELLS)
-		{
+		if (mViewCellsType == BSP_VIEW_CELLS) {
 			for (i=0; i < mViewCells.size(); i++) {
 				ViewCell *vc = mViewCells[i];
 				pvsSize += vc->GetPvs().GetSize();
 			}
-		} else
-		{
+		} else 	{
 			for (i=0; i < objects.size(); i++) {
 				Intersectable *object = objects[i];
@@ -435,19 +450,15 @@
       if (mViewCellsType == BSP_VIEW_CELLS)
 	  {
-		   int limit = Min((int)mViewCells.size(), 1);
-
-		   //char s[64]; sprintf(s, "bsp-pvs%04d.x3d", k);
-		   Exporter *exporter = Exporter::GetExporter("bsp-pvs.x3d");
-		   exporter->SetFilled();
-
-		   Intersectable::NewMail();
-
-		   for (int j = 0; j < limit; ++ j)
-		   {		       
-			   // j random view cells
-			   int k = Random((int)mViewCells.size());
-
-			   Debug << "next vc: " << k << endl;
-			   ViewCellPvsMap::iterator it = mViewCells[k]->GetPvs().mEntries.begin();
+		   for (int j = 0; j < vcPvsOut; ++ j)
+		   {
+			   ViewCell *vc = mViewCells[j];
+
+			   Intersectable::NewMail();
+    
+			   char s[64]; sprintf(s, "bsp-pvs%04d.x3d", j);
+			   Exporter *exporter = Exporter::GetExporter(s);
+			   exporter->SetFilled();
+
+			   ViewCellPvsMap::iterator it = vc->GetPvs().mEntries.begin();
 
 			   Material m;//= RandomMaterial();
@@ -455,12 +466,22 @@
 			   exporter->SetForcedMaterial(m);
 
-			   exporter->ExportViewCell(mViewCells[k]);
+			   exporter->ExportViewCell(vc);
+
+			   Debug << "pvs size: " << (int)vc->GetPvs().GetSize() << " of " << objects.size();
+			   Debug << " exporting rays: " << (int)vcRays[j].size() << endl;
+
+			   exporter->SetWireframe();
+			   
+			   m.mDiffuseColor = RgbColor(1, 0, 1);
+			   exporter->SetForcedMaterial(m);
+
+			   exporter->ExportBspTree(*mBspTree);
+			   exporter->ExportRays(vcRays[j], 1000, RgbColor(0, 1, 0));
 
 			   m.mDiffuseColor = RgbColor(1, 0, 0);
 			   exporter->SetForcedMaterial(m);
 
-			   Debug << "pvs size: " << mViewCells[k]->GetPvs().GetSize() << " of " << objects.size() << endl;
-
-			   for (; it != mViewCells[k]->GetPvs().mEntries.end(); ++ it) 
+			   // output pvs of view cell
+			   for (; it != vc->GetPvs().mEntries.end(); ++ it) 
 			   {
 				   Intersectable *intersect = (*it).first;
@@ -469,22 +490,23 @@
 					   exporter->ExportIntersectable(intersect);
 					   intersect->Mail();
-				   }
-			   
+				   }			
 			   }
+
+			   // output rest of the objects
+			   if (1)
+			   {
+				   Material m;//= RandomMaterial();
+				   m.mDiffuseColor = RgbColor(0, 0, 1);
+				   exporter->SetForcedMaterial(m);
+                 
+				   for (int j = 0; j < objects.size(); ++ j)
+					   if (!objects[j]->Mailed())
+					   {
+						   exporter->ExportIntersectable(objects[j]);
+						   objects[j]->Mail();
+					   }
+			   }
+			   DEL_PTR(exporter);
 		   }
-			
-		   // output rest of the objects
-		   Material m;//= RandomMaterial();
-		   m.mDiffuseColor = RgbColor(0, 0, 1);
-		   exporter->SetForcedMaterial(m);
-
-		   for (int j = 0; j < objects.size(); ++ j)
-			   if (!objects[j]->Mailed())
-			   {
-				   exporter->ExportIntersectable(objects[j]);
-				   objects[j]->Mail();
-			   }
-
-		   DEL_PTR(exporter);
 	  }	  
 
@@ -529,6 +551,2 @@
   return true;
 }
-
-  
-
-    
Index: trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.cpp	(revision 313)
@@ -1646,4 +1646,96 @@
   return Rectangle3(v[0], v[1], v[2], v[3]);
 }
+
+// TODO: use a table to avoid normal and distance computations
+Polygon3 *AxisAlignedBox3::BoundPlane(const Plane3 &plane)
+{
+	Vector3 ptA, ptB;
+	int sideA = 0, sideB = 0;
+
+	VertexContainer vertices;
+
+	int side[8];
+
+	bool onFrontSide, onBackSide = false;
+
+	Vector3 vtx;
+	for (int i = 0; i < 8; ++i)
+	{
+		GetVertex(i, vtx);
+		side[i] = plane.Side(vtx, SIDE_TOLERANCE);
+		if (side[i] > 0)
+			onFrontSide = true;
+		else if (side[i] < 0)
+			onBackSide = true;
+		else // vertex coincident => push_back
+			vertices.push_back(vtx);
+	}
+
+	// does not intersect
+	if (!onFrontSide && !onBackSide)
+		return NULL;
+
+	for (int i = 0; i < 12; ++ i)
+	{
+		int aIdx, bIdx;
+		GetEdge(i, aIdx, bIdx);
+
+		ptA = GetVertex(aIdx);
+		ptB = GetVertex(bIdx);
+
+		int sideA = side[aIdx];
+		int sideB = side[bIdx];
+
+		if (((sideA > 0) && (sideB < 0)) || (sideA < 0) && (sideB > 0))
+			vertices.push_back(plane.FindIntersection(ptA, ptB));	
+	}
+
+	// illformed polygon => swap
+	// HACK: cannot work in abitrary cases
+	if (vertices.size() > 3)
+	{
+		for (int j = 0; j < (int)vertices.size() - 1; ++j)
+		{
+			float minDist = 99999;
+			int minIdx = j;
+
+			for (int i = j + 1; i < vertices.size(); ++i)
+			{
+				float dist = SqrDistance(vertices[j], vertices[i]);
+				
+				if (dist < minDist)
+				{
+					minDist = dist;
+					minIdx = i;
+				}
+			}
+
+			Vector3 vtx = vertices[minIdx];
+			vertices[minIdx] = vertices[j + 1];
+			vertices[j + 1] = vtx;
+		}
+	}
+
+	Polygon3 *planePoly = new Polygon3();
+	
+	// wrong surface orientation?
+    Vector3 norm = Normalize(CrossProd(vertices[0] - vertices[1],
+									   vertices[2] - vertices[1]));
+
+	
+
+	if (DotProd(norm, plane.mNormal) > 0)
+		for (int i = 0; i < (int)vertices.size(); ++ i)
+			planePoly->mVertices.push_back(vertices[i]);
+	else
+	{
+		for (int i = (int)vertices.size() - 1; i >= 0; -- i)
+			planePoly->mVertices.push_back(vertices[i]);
+	}
+
+	//Debug << *planePoly << "\narea: " << planePoly->GetArea() << endl;
+	
+	return planePoly;
+}
 /*
 const int AxisAlignedBox3::complEdgeTbl[12][3] =
@@ -1663,88 +1755,32 @@
 };
 */
-// TODO: use a table to avoid normal and distance computations
-Polygon3 *AxisAlignedBox3::BoundPlane(const Plane3 &plane)
-{
-	Vector3 ptA, ptB;
-	int sideA = 0, sideB = 0;
-
-	VertexContainer vertices;
-
-	int side[8];
-
-	bool onFrontSide, onBackSide = false;
-
-	Vector3 vtx;
-	for (int i = 0; i < 8; ++i)
-	{
-		GetVertex(i, vtx);
-		side[i] = plane.Side(vtx, SIDE_TOLERANCE);
-		if (side[i] > 0)
-			onFrontSide = true;
-		else if (side[i] < 0)
-			onBackSide = true;
-		else // vertex coincident => push_back
-			vertices.push_back(vtx);
-	}
-
-	// does not intersect
-	if (!onFrontSide && !onBackSide)
-		return NULL;
-
-	for (int i = 0; i < 12; ++ i)
-	{
-		int aIdx, bIdx;
-		GetEdge(i, aIdx, bIdx);
-
-		ptA = GetVertex(aIdx);
-		ptB = GetVertex(bIdx);
-
-		int sideA = side[aIdx];
-		int sideB = side[bIdx];
-
-		if (((sideA > 0) && (sideB < 0)) || (sideA < 0) && (sideB > 0))
-			vertices.push_back(plane.FindIntersection(ptA, ptB));	
-	}
-
-	if (vertices.size() == 4)
-	{
-		float maxDist = 0;
-		int maxIdx = 0;
-
-		for (int i = 1; i < 4; ++ i)
-		{
-			float dist = SqrDistance(vertices[0], vertices[i]);
-			if (dist > maxDist)
-			{
-				maxDist = dist;
-				maxIdx = i;
-			}
-		}
-		// illformed polygon = swap
-		if ((maxIdx == 1) || (maxIdx == 3) )
-		{
-			Vector3 vtx = vertices[maxIdx];
-			vertices[maxIdx] = vertices[2];
-			vertices[2] = vtx;
-		}
-	}
-
-	// wrong surface orientation?
-    Vector3 norm = Normalize(CrossProd(vertices[0] - vertices[1],
-									   vertices[2] - vertices[1]));
-
-	Polygon3 *planePoly = new Polygon3();
-
-	if (DotProd(norm, plane.mNormal) > 0)
-		for (int i = 0; i < (int)vertices.size(); ++ i)
-			planePoly->mVertices.push_back(vertices[i]);
-	else
-	{
-		for (int i = (int)vertices.size() - 1; i >= 0; -- i)
-			planePoly->mVertices.push_back(vertices[i]);
-	}
-
-	//Debug << *planePoly << "\narea: " << planePoly->GetArea() << endl;
-	
-	return planePoly;
-}
+
+/*
+inline void addEdge(int idx, int ptAIdx, int ptBIdx)
+{
+	edgeTable[idx].push_back(edge(ptAIdx, ptBIdx));
+}
+int AxisAlignedBox3::CreateEdgeTable()
+{
+	//-- four vertices pattern 1 (3 + reflection)
+	int idx = getIdx(1) + getIdx(2) + getIdx(5) + getIdx(6); 
+	addEdge(idx, 1, 0); addEdge(idx, 3, 2); addEdge(idx, 7, 6; addEdge(idx, 4, 5);
+	int idx = 255 - idx;
+	addEdge(idx, 4, 5); addEdge(idx, 7, 6);  addEdge(idx, 3, 2); addEdge(idx, 1, 0); 
+
+	int idx = getIdx(0) + getIdx(1) + getIdx(2) + getIdx(3); 
+	addEdge(idx, 0, 4); addEdge(idx, 3, 7); addEdge(idx, 2, 6); addEdge(idx, 1, 5);
+	int idx = 255 - idx;
+	addEdge(idx, 1, 5); addEdge(idx, 2, 6); addEdge(idx, 3, 7); addEdge(idx, 0, 4); 
+
+	int idx = getIdx(2) + getIdx(6) + getIdx(7) + getIdx(3);
+	addEdge(idx, 0, 3); addEdge(idx, 4, 7); addEdge(idx, 5, 6); addEdge(idx, 1, 2);
+	int idx = 255 - idx;
+	addEdge(idx, 1, 2); addEdge(idx, 5, 6); addEdge(idx, 4, 7); addEdge(idx, 0, 3); 
+
+
+	//-- four vertices pattern 2 (12 + reflection
+	int idx = getIdx(0) + getIdx(4);
+	addEdge(idx, 0, 1); addEdge(idx, 0, 3); addEdge(idx, 4, 7; addEdge(idx, 4, 5);
+}
+*/
Index: trunk/VUT/GtpVisibilityPreprocessor/src/X3dParserXerces.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/X3dParserXerces.h	(revision 312)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/X3dParserXerces.h	(revision 313)
@@ -114,5 +114,6 @@
   //  Constructors and Destructor
   // -----------------------------------------------------------------------
-  X3dViewCellsParseHandlers(ViewCellContainer *mViewCells, float viewCellHeight);
+  X3dViewCellsParseHandlers(ViewCellContainer *mViewCells, 
+							float viewCellHeight);
   ~X3dViewCellsParseHandlers();
   
