Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2176)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h	(revision 2615)
@@ -67,4 +67,6 @@
 	float mTotalTime;
 
+	ObjectContainer mKdPvs;
+
 	float RaysPerSec() const { if (!mTotalTime) return 0; return (float)mTotalSamples / mTotalTime * 1e-6f; }
 
@@ -223,6 +225,5 @@
 	//////////////////////
 
-	//int mSamplesPerPass;
-	//int mTotalSamples;
+	bool mUseKdPvs;
 	int mInitialSamples;
 
@@ -236,4 +237,5 @@
 	
 
+	ObjectContainer mKdPvs;
 
 	///////////
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 2610)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.h	(revision 2615)
@@ -9,4 +9,5 @@
 namespace GtpVisibilityPreprocessor {
 
+class SceneGraphLeafIntersectable;
 
 /** Basic scene graph node, we are interested only in bounding boxes and topology
@@ -22,5 +23,7 @@
 	virtual void UpdateBox() = 0;
 
-//protected:
+	virtual AxisAlignedBox3 GetBox() const { return mBox; }
+
+protected:
 
 	AxisAlignedBox3 mBox;
@@ -51,17 +54,28 @@
 public:
 
-	SceneGraphLeaf(bool isDynamic = false): 
-	  mIsDynamic(isDynamic){}
+	SceneGraphLeaf();
+	SceneGraphLeaf(bool isDynamic);
 	~SceneGraphLeaf();
+	
+	void LoadTransform(const Matrix4x4 &m);
 
-	virtual void Transform(const Matrix4x4 &trafo);
+	void GetTransform(Matrix4x4 &m) const;
+
+	virtual AxisAlignedBox3 GetBox() const;
+
+	virtual void ApplyTransform(const Matrix4x4 &trafo);
 	virtual bool IsDynamic() const { return mIsDynamic;}
 	virtual bool IsLeaf() const { return true; }
 	virtual void UpdateBox();
-
+#if DYNAMIC_OBJECT_HACK
+	SceneGraphLeafIntersectable *GetIntersectable() { return mIntersectable;}
+#endif
+	/// used as actual pvs entry
 	ObjectContainer mGeometry;
 
 protected:
-
+#if DYNAMIC_OBJECT_HACK
+	SceneGraphLeafIntersectable *mIntersectable;
+#endif
 	bool mIsDynamic;
 	Matrix4x4 mTrafo;
@@ -88,5 +102,5 @@
 	void GetStatistics(int &intersectables, int &faces) const;
 
-	AxisAlignedBox3 GetBox() const { return mRoot->mBox; }
+	AxisAlignedBox3 GetBox() const { return mRoot->GetBox(); }
 	/** Exports binary version of the scene.
 	*/
Index: GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 2610)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SceneGraph.cpp	(revision 2615)
@@ -5,4 +5,5 @@
 #include "X3dExporter.h"
 #include "Intersectable.h"
+#include "IntersectableWrapper.h"
 
 
@@ -223,5 +224,5 @@
 	{
 		(*ni)->UpdateBox();
-		box.Include((*ni)->mBox);
+		box.Include((*ni)->GetBox());
 	}
 
@@ -277,8 +278,41 @@
 
 
-void SceneGraphLeaf::Transform(const Matrix4x4 &trafo)
-{
-	mTrafo *= trafo;
-}
-
-}
+SceneGraphLeaf::SceneGraphLeaf(): mIsDynamic(false)
+{
+	mTrafo = IdentityMatrix();
+#if DYNAMIC_OBJECTS_HACK
+	mIntersectable = new SceneGraphLeafIntersectable(this, mBox);
+#endif
+}
+
+
+SceneGraphLeaf::SceneGraphLeaf(bool isDynamic): mIsDynamic(isDynamic)
+{
+	mTrafo = IdentityMatrix();
+}
+
+
+void SceneGraphLeaf::ApplyTransform(const Matrix4x4 &trafo)
+{
+	mTrafo = trafo * mTrafo;
+}
+
+
+void SceneGraphLeaf::LoadTransform(const Matrix4x4 &m)
+{
+	mTrafo = m;
+}
+
+
+void SceneGraphLeaf::GetTransform(Matrix4x4 &m) const
+{
+	m = mTrafo;
+}
+
+
+AxisAlignedBox3 SceneGraphLeaf::GetBox() const 
+{
+	return Transform(mBox, mTrafo);
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2614)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.cpp	(revision 2615)
@@ -1209,5 +1209,4 @@
 
 #if DYNAMIC_OBJECTS_HACK
-
 		static Ray hRay;
 		VssRayContainer::iterator vit, vit_end = vssRays.end();
@@ -1216,9 +1215,10 @@
 		{
 			VssRay *vssRay = *vit;
+
+			DynamicObjectsContainer::const_iterator tit, tit_end = mDynamicObjects.end();
+			bool foundIntersect = false;
+
+#if USE_TRANSFORMED_MESH_INSTANCE_HACK
 			hRay.Init(*vssRay);
-
-			DynamicObjectsContainer::const_iterator tit, tit_end = mDynamicObjects.end();
-
-			bool foundIntersect = false;
 
 			for (tit = mDynamicObjects.begin(); tit != tit_end; ++ tit)
@@ -1227,6 +1227,5 @@
 
 				float maxT, minT;
-	
-				// test with bounding box
+					// test with bounding box
 				if (tmi->GetBox().GetMinMaxT(hRay, &minT, &maxT))
 				{
@@ -1239,6 +1238,4 @@
 			}
 
-			//if (foundIntersect)	cout << "x: " << Intersectable::GetTypeName(hRay.intersections[0].mObject);
-
 			if (foundIntersect)
 			{
@@ -1249,4 +1246,22 @@
 
 			hRay.intersections.clear();
+#else
+
+			////////////
+			// TODO Vlastimil
+			
+			for (tit = mDynamicObjects.begin(); tit != tit_end; ++ tit)	
+			{
+				// remove vss ray termination triangle with container object
+				if (foundIntersect)
+				{
+					vssRay->mTerminationObject = (*tit)->GetIntersectable();
+				}
+			}
+
+	
+#endif
+
+
 		}
 #endif
@@ -1640,12 +1655,5 @@
 		inStream.read(reinterpret_cast<char *>(&mesh->mVertices[i + 2]), sizeof(Vector3));
 
-		/*mesh->mVertices[i].x += 20;
-		mesh->mVertices[i + 1].x += 20;
-		mesh->mVertices[i + 2].x += 20;
-
-		mesh->mVertices[i].z -= 10;
-		mesh->mVertices[i + 1].z -= 10;
-		mesh->mVertices[i + 2].z -= 10;
-*/
+		// hack: objects initially very small
 		mesh->mVertices[i] *= 100;
 		mesh->mVertices[i + 1] *= 100;
@@ -1671,4 +1679,6 @@
 {
 	const bool dynamic = true;
+#if DYNAMIC_OBJECTS_HACK
+#if USE_TRANSFORMED_MESH_INSTANCE_HACK
 
 	Mesh *mesh = LoadBinaryObjIntoMesh(filename);
@@ -1682,11 +1692,21 @@
 
 		mSceneGraph->GetRoot()->mChildren.push_back(leaf);
-		//mDynamicObjects.push_back(leaf);
+
 		mDynamicObjects.push_back(tmi);
-
 		return true;
 	}
-
-
+#else
+
+	SceneGraphLeaf *leaf = new SceneGraphLeaf(dynamic);
+
+	if (LoadBinaryObj(filename, leaf, NULL))
+	{
+		mDynamicObjects.push_back(leaf);
+		mSceneGraph->GetRoot()->mChildren.push_back(leaf);
+		return true
+	}
+
+#endif
+#endif
 	return false;
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2614)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.cpp	(revision 2615)
@@ -88,5 +88,4 @@
 									   KdTree *tree):
   QGLPixelBuffer(QSize(w, h)),
-  //QGLWidget(NULL, w, h),
   GlRendererBuffer(sceneGraph, viewcells, tree)
 {
@@ -380,13 +379,19 @@
 	glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
 	glDrawElements(GL_TRIANGLES, mIndexBufferSize, GL_UNSIGNED_INT, mIndices);
-
+#if DYNAMIC_OBJECTS_HACK
 	// handle dynamic objects
 	DynamicObjectsContainer::const_iterator dit, dit_end = mDynamicObjects.end();
 
+
 	for (dit = mDynamicObjects.begin(); dit != dit_end; ++ dit)
 	{
+#if USE_TRANSFORMED_MESH_INSTANCE_HACK
 		//cerr << "here45\n****"<<endl;
 		RenderIntersectable(*dit);
-	}
+#else
+		RenderDynamicObject(*dit);
+#endif
+	}
+#endif
 }
 
@@ -400,7 +405,7 @@
 
 	mPvsSize = mPvsCache.mPvs.GetSize();
-
+#if DYNAMIC_OBJECTS_HACK
 	mDynamicObjects.clear();
-
+#endif
 	ObjectPvsIterator it = mPvsCache.mPvs.GetIterator();
 
@@ -416,7 +421,17 @@
 			}
 			break;
+#if DYNAMIC_OBJECTS_HACK
+#if USE_TRANSFORMED_MESH_INSTANCE_HACK
+
 		case Intersectable::TRANSFORMED_MESH_INSTANCE:
 			mDynamicObjects.push_back(static_cast<TransformedMeshInstance *>(obj));
 			break;
+
+#else
+		case Intersectable::SCENEGRAPHLEAF_INTERSECTABLE:
+			mDynamicObjects.push_back(static_cast<SceneGraphLeafIntersectable *>(obj)->GetItem());
+			break;
+#endif
+#endif
 		default:
 			cerr << "PreparePvs: type " << Intersectable::GetTypeName(obj) << " not handled yet" << endl;
@@ -513,4 +528,5 @@
 				//mPvsCache.mPvs = pvs;
 				//mMutex.unlock();
+				cout << "pvs size: " << mPvsCache.mPvs.GetSize() << endl;
 			}
 			else
@@ -667,4 +683,5 @@
 		mViewPoint.x += (x-mousePoint.x)*MOVE_SENSITIVITY / 2.0;
 	} 
+#if DYNAMIC_OBJECTS_HACK
 	else if (e->modifiers() & Qt::AltModifier)
 	{
@@ -673,4 +690,5 @@
 			Matrix4x4 tm;
 			TransformedMeshInstance *tmi;
+#if USE_TRANSFORMED_MESH_INSTANCE_HACK
 
 			switch (mTrafoType)
@@ -703,7 +721,9 @@
 
 			tmi->ApplyWorldTransform(tm);
+#endif
 			updateGL();
 		}
 	}
+#endif
 	else
 	{
@@ -1001,6 +1021,13 @@
 	connect(mControlWidget, SIGNAL(SetScale(bool)), this, SLOT(SetScale(bool)));
 	
-	// setting the size here
-	resize(800, 600);
+	setWindowTitle("PVS Visualization");
+
+	// setting the main window size here
+	//resize(800, 600);
+	//resize(640, 480);
+	//resize(512, 384);
+	//resize(512, 320);
+	resize(640, 400);
+	
 	mControlWidget->show();
 }
@@ -1309,7 +1336,8 @@
 	while (pit.HasMoreEntries()) 	
 	{
+		KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
+
 		if (mShowDistanceWeightedPvs)
 		{
-			KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
 			const AxisAlignedBox3 box = kdObj->GetBox();
 
@@ -1319,5 +1347,4 @@
 		else if (mShowDistanceWeightedTriangles)
 		{
-			KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
 			const AxisAlignedBox3 box = kdObj->GetBox();
 
@@ -1327,5 +1354,4 @@
 		else //if (mShowWeightedTriangles)
 		{
-			KdIntersectable *kdObj = static_cast<KdIntersectable *>(pit.Next());
 			renderCost += kdObj->ComputeNumTriangles();
 		}
@@ -1436,5 +1462,7 @@
 
 	ComputeMaxValues(viewcells, maxPvs, maxPiercingRays, maxRelativeRays, maxRcCost);
-
+	// matt: temp hack
+	//maxRcCost = 200.0f;
+	//cout << "maxRcCost: " << maxRcCost << endl;
 	int i;
 
@@ -1488,6 +1516,4 @@
 	else // using specialised colors
 	{
-	
-
 		if (!mShowComparison)
 			AssignImportanceByRelativeValue(viewcells, maxPvs, maxPiercingRays, maxRelativeRays, maxRcCost);
@@ -1557,5 +1583,5 @@
 			importance = rcCost / maxRcCost;
 		}
-
+if (importance > 1.0f) importance = 1.0f;
 		// c = RgbColor(importance, 1.0f - importance, 0.0f);
 		c = RainbowColorMapping(importance);
@@ -1658,5 +1684,5 @@
 	connect(rb5, SIGNAL(toggled(bool)), SIGNAL(SetShowWeightedCost(bool)));
 
-	QGroupBox *groupBox = new QGroupBox("Visualization options");
+	QGroupBox *groupBox = new QGroupBox("PVS Visualization");
 
 	QVBoxLayout *vbox2 = new QVBoxLayout;
@@ -1771,5 +1797,5 @@
 
 	
-	QGroupBox *groupBox = new QGroupBox("Ray visualization options");
+	QGroupBox *groupBox = new QGroupBox("Ray visualization");
 	QVBoxLayout *vbox2 = new QVBoxLayout;
     
@@ -1816,10 +1842,11 @@
 	QVBoxLayout *vl = new QVBoxLayout;
 	setLayout(vl);
-
-	QWidget *vbox = new QGroupBox("ViewCells", this);
-	layout()->addWidget(vbox);
-
-	vl = new QVBoxLayout;
-	vbox->setLayout(vl);
+	
+	//QWidget *vbox;
+
+	//vbox = new QGroupBox("Render Controls", this);
+	//layout()->addWidget(vbox);
+	//vl = new QVBoxLayout;
+	//vbox->setLayout(vl);
 
 	QLabel *label;
@@ -1830,5 +1857,6 @@
 
 	label = new QLabel("Granularity");
-	vbox->layout()->addWidget(label);
+	//vbox->layout()->addWidget(label);
+	vl->addWidget(label);
 
 	slider = new QSlider(Qt::Horizontal, vbox);
@@ -1844,5 +1872,5 @@
 
 	label = new QLabel("Transfer function");
-	vbox->layout()->addWidget(label);
+	vl->addWidget(label);
 
 	slider = new QSlider(Qt::Horizontal, vbox);
@@ -1859,5 +1887,5 @@
 
 	button = new QPushButton("Update all PVSs", vbox);
-	vbox->layout()->addWidget(button);
+	vl->addWidget(button);
 	connect(button, SIGNAL(clicked()), SLOT(UpdateAllPvs()));
 
@@ -1865,8 +1893,8 @@
 
 	label = new QLabel("Filter size");
-	vbox->layout()->addWidget(label);
+	vl->addWidget(label);
 
 	slider = new QSlider(Qt::Horizontal, vbox);
-	vbox->layout()->addWidget(slider);
+	vl->addWidget(slider);
 	slider->show();
 	slider->setRange(1, 100);
@@ -1879,23 +1907,9 @@
 
 
-	//////////////////////////////////////////
-
-	label = new QLabel("Spatial Filter size");
-	vbox->layout()->addWidget(label);
-
-	slider = new QSlider(Qt::Horizontal, vbox);
-	vbox->layout()->addWidget(slider);
-	slider->show();
-	slider->setRange(1, 100);
-	slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
-	slider->setValue(10);
-
-	connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
-
-
+	
 	///////////////////////////////////
 
 
-	QWidget *hbox = new QWidget(vbox);
+	QWidget *hbox = new QWidget();//vbox);
 	vl->addWidget(hbox);
 	QHBoxLayout *hlayout = new QHBoxLayout;
@@ -1906,20 +1920,26 @@
 	cb->setChecked(false);
 	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowViewCells(bool)));
-
+	
+	cb = new QCheckBox("Render errors", hbox);
+	hlayout->layout()->addWidget(cb);
+	cb->setChecked(false);
+	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
+
+#if REMOVE_TEMPORARY
 	cb = new QCheckBox("Render cost curve", hbox);
 	hlayout->addWidget(cb);
 	cb->setChecked(false);
 	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRenderCost(bool)));
-
+#endif
 	cb = new QCheckBox("Show rays", hbox);
 	hlayout->addWidget(cb);
 	cb->setChecked(false);
 	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowRays(bool)));
-	
+#if REMOVE_TEMPORARY	
 	cb = new QCheckBox("Show Comparison", hbox);
 	hlayout->addWidget(cb);
 	cb->setChecked(false);
 	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetShowComparison(bool)));
-
+#endif
 
 	//////////////////
@@ -1934,8 +1954,8 @@
 	QGroupBox *groupBox = CreateVisualizationPanel(hbox);
 	vh->addWidget(groupBox, 0, 0);
-
+#if REMOVE_TEMPORARY
 	QGroupBox *groupBox2 = CreateRenderCostPanel(hbox);
 	vh->addWidget(groupBox2, 0, 0);
-	
+#endif
 	QGroupBox *groupBox3 = CreateRayVisualizationPanel(hbox);
 	vh->addWidget(groupBox3, 0, 0);
@@ -1948,10 +1968,11 @@
 
 	/*cb = new QRadiobox("Top View", vbox);
-	vbox->layout()->addWidget(cb);
+	vl->addWidget(cb);
 	cb->setChecked(false);
 	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetTopView(bool)));
 */
 
-	vbox->resize(800,150);
+	//vbox->resize(800,150);
+	QWidget *vbox;
 
 	vbox = new QGroupBox("Rendering", this);
@@ -2022,5 +2043,5 @@
 
 	///////////////////////////////////////////
-
+#if REMOVE_TEMPORARY
 	cb = new QCheckBox("Transparency", vbox);
 	vbox->layout()->addWidget(cb);
@@ -2038,5 +2059,5 @@
 	slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
 	slider->setValue(0);
-
+#endif
 	connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetTransparency(int)));
 
@@ -2048,5 +2069,5 @@
 	cb->setChecked(false);
 	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetCutScene(bool)));
-#endif
+
 	cb = new QCheckBox("Render boxes", vbox);
 	vbox->layout()->addWidget(cb);
@@ -2058,10 +2079,5 @@
 	cb->setChecked(false);
 	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderVisibilityEstimates(bool)));
-
-
-	cb = new QCheckBox("Render errors", vbox);
-	vbox->layout()->addWidget(cb);
-	cb->setChecked(false);
-	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetRenderErrors(bool)));
+#endif
 
 	bool tmp;
@@ -2081,5 +2097,5 @@
 	cb->setChecked(tmp);
 	connect(cb, SIGNAL(toggled(bool)), SIGNAL(SetUseSpatialFilter(bool)));
-
+#if REMOVE_TEMPORARY
 	cb = new QCheckBox("Render filter", vbox);
 	vbox->layout()->addWidget(cb);
@@ -2113,5 +2129,5 @@
 	vbox->layout()->addWidget(button);
 	connect(button, SIGNAL(clicked()), SIGNAL(LoadObject()));
-
+#endif
 	/*cb = new QCheckBox("Stats", vbox);
 	vbox->layout()->addWidget(cb);
@@ -2142,4 +2158,18 @@
 	//connect(button, SIGNAL(clicked(void)), SLOT(StoreStatistics(void)));
 	//mHidingCost = 0.1f;
+//////////////////////////////////////////
+
+	label = new QLabel("Spatial Filter size");
+	vbox->layout()->addWidget(label);
+
+	slider = new QSlider(Qt::Horizontal, vbox);
+	vbox->layout()->addWidget(slider);
+	slider->show();
+	slider->setRange(1, 100);
+	slider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
+	slider->setValue(10);
+
+	connect(slider, SIGNAL(valueChanged(int)), SIGNAL(SetSpatialFilterSize(int)));
+
 
 	setWindowTitle("Preprocessor Control Widget");
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h	(revision 2614)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlRenderer.h	(revision 2615)
@@ -232,7 +232,7 @@
 
 	bool mUseRandomColorPerPvsObject;
-
+#if DYNAMIC_OBJECTS_HACK
 	DynamicObjectsContainer mDynamicObjects;
-
+#endif
 	QtGlRendererWidget(SceneGraph *sceneGraph,
 		               ViewCellsManager *viewcells,
Index: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp	(revision 2614)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/QtGlViewer.cpp	(revision 2615)
@@ -31,4 +31,7 @@
 	mModelMatrix = IdentityMatrix();
 
+	//setWindowTitle("PVS visualization");
+	setWindowTitle("Global View Cell Visualization");
+
 	trackball(manipulatorLastQuat, 0.0f, 0.0f, 0.0f, 0.0f);
 	//connect(renderer, SIGNAL(PvsUpdated()), this, SLOT(updateGL()));
@@ -46,5 +49,9 @@
 QSize QtGlViewer::sizeHint() const 
 {
-	return QSize(640, 480);
+	// set viewer size here
+	return QSize(512, 320);
+
+	//return QSize(512, 384);
+	//return QSize(640, 480);
 }
   
@@ -228,7 +235,14 @@
 {
   switch (e->key()) {
+  case Qt::Key_V:
+		mRenderer->mRenderViewCells = !mRenderer->mRenderViewCells;
+		updateGL();
+		break;
+
   case Qt::Key_W:
 		mWireframe = !mWireframe;
 		updateGL();
+		break;
+
   default:
 		e->ignore();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2609)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Preprocessor.h	(revision 2615)
@@ -219,8 +219,7 @@
 	/// the view cells manager
 	ViewCellsManager *mViewCellsManager;
-
-	//SceneGraphNodeContainer mDynamicObjects;
+#if DYNAMIC_OBJECTS_HACK
 	DynamicObjectsContainer mDynamicObjects;
-
+#endif
 	/// greedy optimized hierarchy for both objects and view cells
 	VspOspTree *mVspOspTree;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Containers.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Containers.h	(revision 2609)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Containers.h	(revision 2615)
@@ -5,4 +5,5 @@
 #include <queue>
 #include <map>
+#include "common.h"
 
 using std::vector;
@@ -14,4 +15,5 @@
 class HierarchyNode;
 class SceneGraphNode;
+class SceneGraphLeaf;
 class Intersectable;
 class Polygon3;
@@ -73,6 +75,10 @@
 
 /// a container for dynamic objects
+
+#if USE_TRANSFORMED_MESH_INSTANCE_HACK
 typedef std::vector<TransformedMeshInstance *> DynamicObjectsContainer;
-
+#else
+typedef vector<SceneGraphLeaf *> DynamicObjectsContainer;
+#endif
 
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2614)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.cpp	(revision 2615)
@@ -369,6 +369,13 @@
 {
 #if DYNAMIC_OBJECTS_HACK
+
+#if USE_TRANSFORMED_MESH_INSTANCE_HACK
 	if (ray.mTerminationObject->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
 		return ray.mTerminationObject;
+#else
+	if (ray.mTerminationObject->Type() == Intersectable::SCENEGRAPHLEAF_INTERSECTABLE)
+		return ray.mTerminationObject;
+#endif
+
 #endif
 	if (mUseKdPvs)
@@ -3359,6 +3366,6 @@
 			// $$ warning collect objects takes only unmailed ones!
 			if (mUseKdPvs) {
-			  //			  GetPreprocessor()->mKdTree->CollectKdObjects(box, objects);
-			  GetPreprocessor()->mKdTree->CollectSmallKdObjects(box, objects, 0.1f);
+			  			  GetPreprocessor()->mKdTree->CollectKdObjects(box, objects);
+			  //GetPreprocessor()->mKdTree->CollectSmallKdObjects(box, objects, 0.1f);
 
 			} else
@@ -3370,4 +3377,5 @@
 			  {
 				Intersectable *o = *noi;
+				
 				// $$ JB warning: pdfs are not correct at this point!	  
 				pvs.AddSampleDirty(o, Limits::Small);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2614)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp	(revision 2615)
@@ -471,4 +471,37 @@
 
 
+void GlRenderer::_RenderDynamicObject(SceneGraphLeaf *leaf)
+{
+	// apply world transform before rendering
+	Matrix4x4 m;
+	leaf->GetTransform(m);
+
+	glPushMatrix();
+	glMultMatrixf((float *)m.x);
+
+	glBegin(GL_TRIANGLES);
+
+	ObjectContainer::const_iterator oi = leaf->mGeometry.begin();
+	for (; oi != leaf->mGeometry.end(); oi++) 
+	{
+		TriangleIntersectable *object = (TriangleIntersectable *)*oi;
+		Triangle3 *t = &(object->GetItem());
+
+		Vector3 normal = t->GetNormal();
+		glNormal3f(normal.x, normal.y, normal.z);
+
+		glVertex3f(t->mVertices[0].x, t->mVertices[0].y, t->mVertices[0].z);
+		glVertex3f(t->mVertices[1].x, t->mVertices[1].y, t->mVertices[1].z);
+		glVertex3f(t->mVertices[2].x, t->mVertices[2].y, t->mVertices[2].z);
+
+	}
+
+	glEnd();
+
+	glPopMatrix();
+}
+
+
+
 void GlRenderer::_RenderSceneTriangles()
 {
@@ -499,5 +532,5 @@
 {
 	Intersectable::NewMail();
-
+#if DYNAMIC_OBJECTS_HACK
 	Preprocessor *p = mViewCellsManager->GetPreprocessor();
 	// handle dynamic objects
@@ -506,7 +539,11 @@
 	for (dit = p->mDynamicObjects.begin(); dit != dit_end; ++ dit)
 	{
-		RenderIntersectable(*dit);
-	}
-
+#if USE_TRANSFORMED_MESH_INSTANCE_HACK
+        RenderIntersectable(*dit);
+#else
+		_RenderDynamicObject(*dit);
+#endif
+	}
+#endif
 #if 1
 	_RenderSceneTrianglesWithDrawArrays();
Index: GTP/trunk/Lib/Vis/Preprocessing/src/common.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 2609)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/common.h	(revision 2615)
@@ -23,5 +23,6 @@
 #include <vector>
 
-#define DYNAMIC_OBJECTS_HACK 1
+#define DYNAMIC_OBJECTS_HACK 0
+#define USE_TRANSFORMED_MESH_INSTANCE_HACK 1
 #define USE_HAVRAN_RAYCASTER
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp	(revision 2604)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.cpp	(revision 2615)
@@ -225,3 +225,9 @@
 }
 
-}
+
+SceneGraphLeafIntersectable::SceneGraphLeafIntersectable(SceneGraphLeaf *item, const AxisAlignedBox3 &box):
+  IntersectableWrapper<SceneGraphLeaf *>(item), mBox(box)
+{
+}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2606)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/KdTree.cpp	(revision 2615)
@@ -1372,82 +1372,82 @@
 bool KdTree::ImportBinTree(const string &filename, ObjectContainer &objects)
 {
-  // export binary version of mesh
-  queue<TraversalData> tStack;
-  IN_STREAM stream(filename.c_str(), IN_BIN_MODE);
-  
-  if (!stream.is_open()) return false;
-  
-  // sort objects by their id
-  //	if (!is_sorted(objects.begin(), objects.end(), ilt))
-  sort(objects.begin(), objects.end(), ilt);
-  
-  mBox.Initialize();
-  ObjectContainer::const_iterator oit, oit_end = objects.end();
-  
-  ///////////////////////////
-  //-- compute bounding box of object space
-  
-  for (oit = objects.begin(); oit != oit_end; ++ oit)
-  {
-    const AxisAlignedBox3 box = (*oit)->GetBox();
-    mBox.Include(box);
-  }
-  
-  // hack: we make a new root
-  DEL_PTR(mRoot);
-  
-  mRoot = ImportNextNode(stream, NULL, objects);
-  
-  tStack.push(TraversalData(mRoot, mBox, 0));
-  mStat.Reset();
-  mStat.nodes = 1;
-  
-  while(!tStack.empty())
-  {
-    TraversalData tData = tStack.front();
-    tStack.pop();
-    
-    KdNode *node = tData.mNode;
-    
-    if (!node->IsLeaf())
-    {
-      mStat.nodes += 2;
-      
-      //Debug << "i" ;
-      KdInterior *interior = static_cast<KdInterior *>(node);
-      interior->mBox = tData.mBox;
-      
-      KdNode *front = ImportNextNode(stream, interior, objects);
-      KdNode *back = ImportNextNode(stream, interior, objects);
-      
-      interior->SetupChildLinks(back, front);
-      
-      ++ mStat.splits[interior->mAxis];
-      
-      // compute new bounding box
-      AxisAlignedBox3 frontBox, backBox;
-      
-      tData.mBox.Split(interior->mAxis, 
-		       interior->mPosition, 
-		       frontBox, 
-		       backBox);
-      
-      tStack.push(TraversalData(front, frontBox, tData.mDepth + 1));			
-      tStack.push(TraversalData(back, backBox, tData.mDepth + 1));
-    }
-    else
-    {
-      EvaluateLeafStats(tData);
-      //cout << "l";
-    }
-  }
-
-  float area = GetBox().SurfaceArea()*mKdPvsArea;
-  
-  SetPvsTerminationNodes(area);
-  
-  Debug << mStat << endl;
-  
-  return true;
+	// export binary version of mesh
+	queue<TraversalData> tStack;
+	IN_STREAM stream(filename.c_str(), IN_BIN_MODE);
+
+	if (!stream.is_open()) return false;
+
+	// sort objects by their id
+	//	if (!is_sorted(objects.begin(), objects.end(), ilt))
+	sort(objects.begin(), objects.end(), ilt);
+
+	mBox.Initialize();
+	ObjectContainer::const_iterator oit, oit_end = objects.end();
+
+	///////////////////////////
+	//-- compute bounding box of object space
+
+	for (oit = objects.begin(); oit != oit_end; ++ oit)
+	{
+		const AxisAlignedBox3 box = (*oit)->GetBox();
+		mBox.Include(box);
+	}
+
+	// hack: we make a new root
+	DEL_PTR(mRoot);
+
+	mRoot = ImportNextNode(stream, NULL, objects);
+
+	tStack.push(TraversalData(mRoot, mBox, 0));
+	mStat.Reset();
+	mStat.nodes = 1;
+
+	while(!tStack.empty())
+	{
+		TraversalData tData = tStack.front();
+		tStack.pop();
+
+		KdNode *node = tData.mNode;
+
+		if (!node->IsLeaf())
+		{
+			mStat.nodes += 2;
+
+			//Debug << "i" ;
+			KdInterior *interior = static_cast<KdInterior *>(node);
+			interior->mBox = tData.mBox;
+
+			KdNode *front = ImportNextNode(stream, interior, objects);
+			KdNode *back = ImportNextNode(stream, interior, objects);
+
+			interior->SetupChildLinks(back, front);
+
+			++ mStat.splits[interior->mAxis];
+
+			// compute new bounding box
+			AxisAlignedBox3 frontBox, backBox;
+
+			tData.mBox.Split(interior->mAxis, 
+				interior->mPosition, 
+				frontBox, 
+				backBox);
+
+			tStack.push(TraversalData(front, frontBox, tData.mDepth + 1));			
+			tStack.push(TraversalData(back, backBox, tData.mDepth + 1));
+		}
+		else
+		{
+			EvaluateLeafStats(tData);
+			//cout << "l";
+		}
+	}
+
+	float area = GetBox().SurfaceArea()*mKdPvsArea;
+
+	SetPvsTerminationNodes(area);
+
+	Debug << mStat << endl;
+
+	return true;
 }
 
@@ -1456,6 +1456,6 @@
 KdTree::GetOrCreateKdIntersectable(KdNode *node)
 {
-  if (node == NULL)
-    return NULL;
+	if (node == NULL)
+		return NULL;
 
 	if (node->mIntersectable == NULL) 
@@ -1465,14 +1465,14 @@
 		KdIntersectable *kdObj = new KdIntersectable(node, GetBox(node));
 		node->mIntersectable = kdObj;
-		
+
 		mKdIntersectables.push_back(kdObj);
 		kdObj->SetId(id);
 #ifdef USE_BIT_PVS
-    // hack: for kd pvs the kd intersecables are the pvs objects
-    ObjectPvsIterator::sObjects.push_back(kdObj);
+		// hack: for kd pvs the kd intersecables are the pvs objects
+		ObjectPvsIterator::sObjects.push_back(kdObj);
 #endif
-  }
-
-  return node->mIntersectable;
+	}
+
+	return node->mIntersectable;
 }
 
@@ -1519,4 +1519,24 @@
 }
 
+
+/*KdNode *
+KdTree::GetPvsNode(const Triangle3 &tri) const
+{
+  KdNode *node = mRoot;
+  
+  while (node->mPvsTermination == 0 ) 
+  {
+	  KdInterior *inter = (KdInterior *)node;
+	  if (point[inter->mAxis] < inter->mPosition)
+		  node = inter->mBack;
+	  else
+		  node = inter->mFront;
+  }
+
+  return node;
+}*/
+
+
+
 KdNode *
 KdTree::GetPvsNode(const Vector3 &point) const
@@ -1561,30 +1581,30 @@
   
   while (!tStack.empty())
-    {
-      KdNode *node = tStack.top();
-      tStack.pop();
-      
-      if (node->IsLeaf())
-	{
-	  leaves.push_back(static_cast<KdLeaf *>(node));
-	}
-      else // interior
-	{
-	  KdInterior *interior = static_cast<KdInterior *>(node);
-	  
-	  if (box.Max(interior->mAxis) >= interior->mPosition)
-	    {
-	      tStack.push(interior->mFront);
-	    }
-	  
-	  if (box.Min(interior->mAxis) < interior->mPosition)
-	    {
-	      tStack.push(interior->mBack);
-	    }
-	}
-    }
-}
-
-
-
-}
+  {
+	  KdNode *node = tStack.top();
+	  tStack.pop();
+
+	  if (node->IsLeaf())
+	  {
+		  leaves.push_back(static_cast<KdLeaf *>(node));
+	  }
+	  else // interior
+	  {
+		  KdInterior *interior = static_cast<KdInterior *>(node);
+
+		  if (box.Max(interior->mAxis) >= interior->mPosition)
+		  {
+			  tStack.push(interior->mFront);
+		  }
+
+		  if (box.Min(interior->mAxis) < interior->mPosition)
+		  {
+			  tStack.push(interior->mBack);
+		  }
+	  }
+  }
+}
+
+
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2611)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp	(revision 2615)
@@ -348,4 +348,6 @@
 			rendererWidget->Show();
 
+			//rendererWidget->SetWindowTitle("Global Visualization");
+
 			if (1) // not working with vbo
 			{
Index: GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 2575)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Intersectable.h	(revision 2615)
@@ -39,14 +39,15 @@
 
 	enum {MESH_INSTANCE, 
-		TRANSFORMED_MESH_INSTANCE, 
-		SPHERE, 
-		VIEW_CELL, 
-		OGRE_MESH_INSTANCE,
-		KD_INTERSECTABLE,
-		BVH_INTERSECTABLE,
-		TRIANGLE_INTERSECTABLE,
-		DUMMY_INTERSECTABLE,
-		ENGINE_INTERSECTABLE,
-		CONTAINER_INTERSECTABLE
+		  TRANSFORMED_MESH_INSTANCE, 
+		  SPHERE, 
+		  VIEW_CELL, 
+		  OGRE_MESH_INSTANCE,
+		  KD_INTERSECTABLE,
+		  BVH_INTERSECTABLE,
+		  TRIANGLE_INTERSECTABLE,
+		  DUMMY_INTERSECTABLE,
+		  ENGINE_INTERSECTABLE,
+		  CONTAINER_INTERSECTABLE,
+		  SCENEGRAPHLEAF_INTERSECTABLE
 	};
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2600)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.h	(revision 2615)
@@ -161,4 +161,6 @@
   RenderViewPoint();
 
+  void _RenderDynamicObject(SceneGraphLeaf *leaf);
+
   virtual void
   SetupProjection(const int w, const int h, const float angle = 70.0f);
Index: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2601)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.cpp	(revision 2615)
@@ -48,4 +48,7 @@
 	Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", mEvaluatePixelError);
 
+	Environment::GetSingleton()->GetBoolValue("ViewCells.useKdPvs", mUseKdPvs);
+
+
 	char gvsStatsLog[100];
 	Environment::GetSingleton()->GetStringValue("GvsPreprocessor.stats", gvsStatsLog);
@@ -183,9 +186,15 @@
 			obj->mCounter = 1;
 			mTrianglePvs.push_back(obj);
-
-			/*mViewCellsManager->ComputeSampleContribution(ray, 
-													 true, 
-													 mCurrentViewCell,
-													 true);*/
+		
+			if (mUseKdPvs)
+			{
+				KdNode *node = mKdTree->GetPvsNode(ray.mTermination);
+
+				if (!node->Mailed())
+				{
+					node->Mail();
+					mKdPvs.push_back(mKdTree->GetOrCreateKdIntersectable(node));
+				}			
+			}
 			
 			result = true;
@@ -863,5 +872,9 @@
 	for (lit = leaves.begin(); lit != leaves.end(); ++ lit)
 	{
-		KdLeaf *leaf = *lit;
+        KdLeaf *leaf = *lit;
+		
+		if (mUseKdPvs)
+			mKdPvs.push_back(mKdTree->GetOrCreateKdIntersectable(leaf));
+
 		ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
 
@@ -892,5 +905,6 @@
 		(*oit)->mCounter = 0;
 	}
-	int dummy = 0;
+	
+	
 	while (NextViewCell())// && (dummy < 3))
 	{
@@ -898,4 +912,10 @@
 		//if (mProcessedViewCells < 53) continue;
 	
+		if (mUseKdPvs)
+		{
+			KdNode::NewMail();
+			mKdPvs.clear();
+		}
+
 		long startTime = GetTime();
 		cout << "\n***********************\n" 
@@ -910,21 +930,36 @@
 		mGvsStats.mTotalTrianglePvs += mGvsStats.mTrianglePvs;
 
-		ObjectContainer objectPvs;
-
-		// exchange triangle pvs with objects
-		//UpdatePvs(mCurrentViewCell);
-        GetObjectPvs(objectPvs);
-
-		// add pvs
-		ObjectContainer::const_iterator it, it_end = objectPvs.end();
-
-		for (it = objectPvs.begin(); it != it_end; ++ it)
-		{
-			mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
-		}
-
-		cout << "triangle pvs of " << (int)mTrianglePvs.size() 
-			 << " was converted to object pvs of " << (int)objectPvs.size() << endl;
-		
+		if (!mUseKdPvs)
+		{
+			ObjectContainer objectPvs;
+
+			// optain object pvs
+			GetObjectPvs(objectPvs);
+
+			// add pvs
+			ObjectContainer::const_iterator it, it_end = objectPvs.end();
+
+			for (it = objectPvs.begin(); it != it_end; ++ it)
+			{
+				mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
+			}
+
+			cout << "triangle pvs of " << (int)mTrianglePvs.size() 
+				<< " was converted to object pvs of " << (int)objectPvs.size() << endl;
+
+			mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
+		}
+		else
+		{
+			// add pvs
+			ObjectContainer::const_iterator it, it_end = mKdPvs.end();
+
+			for (it = mKdPvs.begin(); it != it_end; ++ it)
+			{
+				mCurrentViewCell->GetPvs().AddSampleDirty(*it, 1.0f);
+			}
+
+			mGvsStats.mPerViewCellPvs = mKdPvs.size();
+		}
 
 		////////
@@ -932,6 +967,5 @@
 		
 		mGvsStats.mViewCells = mProcessedViewCells;//mPass;
-		//mGvsStats.mPerViewCellPvs = mCurrentViewCell->GetPvs().GetSize();
-		mGvsStats.mPerViewCellPvs = (int)objectPvs.size();
+		
 
 		mGvsStats.mTotalPvs += mGvsStats.mPerViewCellPvs;
@@ -950,5 +984,5 @@
 
         mTrianglePvs.clear();
-
+#if 0
 		if (GVS_DEBUG)
 		{
@@ -957,12 +991,11 @@
 			CLEAR_CONTAINER(mVssRays);
 		}
-
-		// is this really necessary?
-		//ClearRayQueue();
 		cout << "finished" << endl;
+
 		if (mEvaluatePixelError || mExportVisibility)
 		{
 			StorePvs(objectPvs);
 		}
+#endif
 	}
 }
@@ -1039,4 +1072,5 @@
 
 	BvhLeaf::NewMail();
+	//KdNode::NewMail();
 
 	ObjectContainer::const_iterator oit, oit_end = mTrianglePvs.end();
@@ -1053,7 +1087,6 @@
 		if (!bv || bv->Mailed())
 			continue;
-		
+
 		bv->Mail();
-
 		objectPvs.push_back(bv);
 	}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 2604)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/IntersectableWrapper.h	(revision 2615)
@@ -267,4 +267,31 @@
 };
 
+
+class SceneGraphLeafIntersectable: public IntersectableWrapper<SceneGraphLeaf *>
+{
+public:
+	SceneGraphLeafIntersectable(SceneGraphLeaf *item, const AxisAlignedBox3 &box);
+    
+	int ComputeNumTriangles() { return 0;}
+	
+	int Type() const
+	{
+		return Intersectable::SCENEGRAPHLEAF_INTERSECTABLE;
+	}
+
+    AxisAlignedBox3 GetBox() const 
+	{
+		return mBox;
+	}
+
+	/// the bounding box of this intersectable
+	AxisAlignedBox3 mBox;
+
+protected:
+
+//	int mNumTriangles;
+};
+
+
 }
 
