Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 495)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp	(revision 500)
@@ -75,5 +75,5 @@
 
 // Inline constructor
-VspKdNode::VspKdNode(VspKdInterior *p):
+VspKdNode::VspKdNode(VspKdNode *p):
 mParent(p), mAxis(-1), mDepth(p ? p->mDepth + 1 : 0)
 {}
@@ -83,10 +83,10 @@
 };
 
-inline VspKdInterior *VspKdNode::GetParent() const
+inline VspKdNode *VspKdNode::GetParent() const
 {
 	return mParent;
 }
 
-inline void VspKdNode::SetParent(VspKdInterior *p)
+inline void VspKdNode::SetParent(VspKdNode *p)
 {
 	mParent = p;
@@ -126,5 +126,5 @@
 
 void VspKdInterior::ReplaceChildLink(VspKdNode *oldChild,
-										 VspKdNode *newChild)
+									 VspKdNode *newChild)
 {
 	if (mBack == oldChild)
@@ -184,5 +184,5 @@
 
 
-VspKdLeaf::VspKdLeaf(VspKdInterior *p, 
+VspKdLeaf::VspKdLeaf(VspKdNode *p, 
 					 const int nRays, 
 					 const int maxCostMisses):
@@ -339,7 +339,66 @@
 }
 
-/*********************************************************/
-/*            class VspKdTree implementation             */
-/*********************************************************/
+
+/***************************************************************/
+/*          class VspKdIntermediate implementation             */
+/***************************************************************/
+
+
+VspKdIntermediate::VspKdIntermediate(VspKdInterior *p):
+VspKdNode(p), mBack(NULL), mFront(NULL), mAccesses(0), mLastAccessTime(-1)
+{
+}
+
+
+VspKdIntermediate::~VspKdIntermediate()
+{
+	DEL_PTR(mFront);
+	DEL_PTR(mBack);
+}
+    
+	
+int VspKdIntermediate::Type() const
+{
+	return EIntermediate;
+}
+
+
+VspKdLeaf *VspKdIntermediate::GetBack() const
+{
+	return mBack;
+}
+
+
+VspKdLeaf *VspKdIntermediate::GetFront() const
+{
+	return mFront;
+}
+
+
+void VspKdIntermediate::Print(ostream &s) const
+{
+	s << endl << mSplitPlane << endl;
+}
+
+
+void VspKdIntermediate::SetupChildLinks(VspKdLeaf *b, VspKdLeaf *f)
+{
+	mBack = b;
+	mFront = f;
+
+	b->SetParent(this);
+	f->SetParent(this);
+}
+
+
+int VspKdIntermediate::ComputeRayIntersection(const RayInfo &rayData, float &t)
+{
+	return rayData.ComputeRayIntersection(mSplitPlane, t);
+}
+
+
+/*************************************************************/
+/*              class VspKdTree implementation               */
+/*************************************************************/
 
 
@@ -1131,5 +1190,6 @@
 
 	// add the new nodes to the tree
-	VspKdInterior *node = new VspKdInterior(leaf->mParent);
+	VspKdInterior *node = 
+		new VspKdInterior(dynamic_cast<VspKdInterior *>(leaf->mParent));
 
 	node->mAxis = axis;
@@ -1147,5 +1207,8 @@
 	// replace a link from node's parent
 	if (leaf->mParent)
-		leaf->mParent->ReplaceChildLink(leaf, node);
+	{
+		VspKdInterior *parent = dynamic_cast<VspKdInterior *>(leaf->mParent);
+		parent->ReplaceChildLink(leaf, node);
+	}
 	// and setup child links
 	node->SetupChildLinks(back, front);
@@ -1570,7 +1633,10 @@
 	VspKdLeaf *newLeaf = new VspKdLeaf(sroot->mParent, rayCount);
 
-	if (newLeaf->mParent)
-	{
-		newLeaf->mParent->ReplaceChildLink(sroot, newLeaf);
+	VspKdInterior *parent = 
+		dynamic_cast<VspKdInterior *>(newLeaf->mParent);
+
+	if (parent)
+	{
+		parent->ReplaceChildLink(sroot, newLeaf);
 	}
 	tstack.push(sroot);
@@ -1834,14 +1900,21 @@
 
 	if (!node->IsLeaf())
-		return (dynamic_cast<VspKdInterior *>(node))->mBox;
-
-	if (node->mParent->mAxis >= 3)
-		return node->mParent->mBox;
-
-	AxisAlignedBox3 box(node->mParent->mBox);
-	if (node->mParent->GetFront() == node)
-		box.SetMin(node->mParent->mAxis, node->mParent->mPosition);
+	{
+		if (node->Type() == VspKdNode::EIntermediate)
+            return (dynamic_cast<VspKdInterior *>(node))->mBox;
+		else
+			return (dynamic_cast<VspKdIntermediate *>(node))->mBox;
+	}
+
+	VspKdInterior *parent = dynamic_cast<VspKdInterior *>(node->mParent);
+
+	if (parent->mAxis >= 3)
+		return parent->mBox;
+
+	AxisAlignedBox3 box(parent->mBox);
+	if (parent->GetFront() == node)
+		box.SetMin(parent->mAxis, parent->mPosition);
 	else
-		box.SetMax(node->mParent->mAxis, node->mParent->mPosition);
+		box.SetMax(parent->mAxis, parent->mPosition);
 
 	return box;
@@ -2348,7 +2421,9 @@
 			leaf->SetViewCell(vc);
 
+			VspKdInterior *parent = 
+				dynamic_cast<VspKdInterior *>(leaf->mParent);
 			// replace a link from node's parent
-			if (leaf->mParent)
-				leaf->mParent->ReplaceChildLink(node, leaf);
+			if (parent)
+				parent->ReplaceChildLink(node, leaf);
 
 			delete interior;
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 498)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspBspTree.cpp	(revision 500)
@@ -1751,5 +1751,6 @@
 	nodeStack.push(mRoot);
 
-	// planes needed to verify that we found neighbor leaf.
+	// split planes from the root to this node
+	// needed to verify that we found neighbor leaf
 	vector<Plane3> halfSpaces;
 	ExtractHalfSpaces(n, halfSpaces);
@@ -1783,5 +1784,5 @@
 						isAdjacent = false;
 				}
-
+				// neighbor was found
 				if (isAdjacent)
 					neighbors.push_back(dynamic_cast<BspLeaf *>(node));
Index: trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 492)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/RssTree.cpp	(revision 500)
@@ -1922,4 +1922,5 @@
 	positions--;
   }
+  return true; // corr. matt
 }
 
Index: trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 498)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 500)
@@ -4,4 +4,5 @@
 #include <QtOpenGL>
 #include <QWaitCondition>
+//#include <QGLPixelBuffer>
 
 #include "Vector3.h"
Index: trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h
===================================================================
--- trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 495)
+++ trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.h	(revision 500)
@@ -203,9 +203,9 @@
 	friend class VspKdTree;
 
-	enum {EInterior, ELeaf};
+	enum {EInterior, EIntermediate, ELeaf};
 
 	/** Constructs new interior node from the parent node.
 	*/
-	inline VspKdNode(VspKdInterior *p);
+	inline VspKdNode(VspKdNode *p);
 
 	/** Destroys this node and the subtree.
@@ -226,14 +226,15 @@
 
 	/** Returns time needed to access this node.
-	*/
-	virtual int GetAccessTime(); // NOTE: don't really know how it is used!
+		@NOTE: don't really know how it is used!
+	*/
+	virtual int GetAccessTime(); 
 
 	/** Returns parent node.
 	*/
-	VspKdInterior *GetParent() const;
+	VspKdNode *GetParent() const;
 
 	/** Sets parent node.
 	*/
-	void SetParent(VspKdInterior *p);
+	void SetParent(VspKdNode *p);
 
 protected:
@@ -242,5 +243,5 @@
   
 	/// link to the parent
-	VspKdInterior *mParent;
+	VspKdNode *mParent;
 
 	enum {SPLIT_X = 0, SPLIT_Y, SPLIT_Z};
@@ -308,4 +309,55 @@
 
 
+/**
+	Node type just before leaf holding abitrary split plane
+*/
+class VspKdIntermediate: public VspKdNode
+{
+public:
+	friend class VspKdTree;
+
+	/** Constructs new interior node from parent node.
+	*/
+	VspKdIntermediate(VspKdInterior *p);
+			
+	//virtual int GetAccessTime();
+	
+	virtual int Type() const;
+  
+	virtual ~VspKdIntermediate();
+    
+	virtual void Print(ostream &s) const;
+
+	/** Returns back child.
+	*/
+	inline VspKdLeaf *GetBack() const;
+	/** Returns front child.
+	*/
+	inline VspKdLeaf *GetFront() const;
+
+protected:
+
+	/** Sets pointers to back child and front child.
+	*/
+	void SetupChildLinks(VspKdLeaf *b, VspKdLeaf *f);
+	/** Computes intersection of the ray with the node boundaries.
+	*/
+	int ComputeRayIntersection(const RayInfo &rayData, float &t);
+
+	// plane in local modelling coordinates
+	Plane3 mSplitPlane;
+
+	// pointers to children
+	VspKdLeaf *mBack;
+	VspKdLeaf *mFront;
+
+	// the bbox of the node
+	AxisAlignedBox3 mBox;
+
+	// data for caching
+	long mAccesses;
+	long mLastAccessTime;
+};
+
 // --------------------------------------------------------------
 // KD-tree node - leaf node
@@ -322,5 +374,5 @@
 		@parma maxMisses how many times the max cost ratio was missed on the path to the leaf
 	*/
-	VspKdLeaf(VspKdInterior *p,	const int nRays, const int maxCostMisses = 0);
+	VspKdLeaf(VspKdNode *p,	const int nRays, const int maxCostMisses = 0);
 	
 	virtual ~VspKdLeaf();
@@ -385,5 +437,4 @@
 	*/
 	int GetMaxCostMisses();
-
 
 	////////////////////////////////////////////
@@ -413,9 +464,8 @@
 	int mMaxCostMisses;
 
-private:
+//private:
 	/// stores PVS size so we have to evaluate PVS size only once
 	int mPvsSize;
 };
-
 
 
@@ -456,4 +506,6 @@
 		VspKdNode *mNode;
 		AxisAlignedBox3 mBox;
+		//TODO PolygonContainer *mPolys;
+
 		int mDepth;
 		//float mPriority;
@@ -480,6 +532,6 @@
 		{
 			// return a.node->queries.size() < b.node->queries.size();
-			VspKdLeaf *leafa = (VspKdLeaf *) a.mNode;
-			VspKdLeaf *leafb = (VspKdLeaf *) b.mNode;
+			VspKdLeaf *leafa = dynamic_cast<VspKdLeaf *>(a.mNode);
+			VspKdLeaf *leafb = dynamic_cast<VspKdLeaf *>(b.mNode);
 #if 0
 			return
