
#include "ClipmapRegion.h"

namespace Ogre {

ClipmapRegion::ClipmapRegion(int outerWidth, int outerHeight, int innerWidth, int innerHeight)
{
	this->clipSize = 0;
	this->innerHeight = innerHeight;
	this->innerWidth = innerWidth;
	this->outerHeight = outerHeight;
	this->outerWidth = outerWidth;
	quadList = NULL;
	upperLeftQuad = Vector3ix(-1,-1,-1);
	lowerRightQuad = Vector3ix(-1,-1,-1);
	innerUpperLeftQuad = Vector3ix(-1,-1,-1);
	innerLowerRightQuad = Vector3ix(-1,-1,-1);
	changed = true;
	ulex = 0;
	ulez = 0;
	lrex = 0;
	lrez = 0;
}
ClipmapRegion::~ClipmapRegion()
{}

void ClipmapRegion::addExteriorRows(int amount, int direction){
//	std::cout << "Adding " << amount << " exterior rows in direction " << direction << std::endl;
//	std::cout << "outerHeight was " << outerHeight << std::endl;
//	std::cout << "outerWidth was " << outerWidth << std::endl;
	int i;
	if (amount > 0){
		switch (direction) {
			case LEFT : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(upperLeftQuad.getLeft())==NULL) {
						break;
					}
					upperLeftQuad = upperLeftQuad.getLeft();
				}
				outerWidth+=i;
				break;
			case RIGHT : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(lowerRightQuad.getRight())==NULL) {
						break;
					}
					lowerRightQuad = lowerRightQuad.getRight();
				}
				outerWidth+=i;
				break;
			case UP : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(upperLeftQuad.getTop())==NULL) {
						break;
					}
					upperLeftQuad = upperLeftQuad.getTop();
				}
				outerHeight+=i;
				break;
			case DOWN : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(lowerRightQuad.getBottom())==NULL) {
						break;
					}
					lowerRightQuad = lowerRightQuad.getBottom();
				}
				outerHeight+=i;
				break;
		}
		changed = true;
	} else if (amount < 0) {
		removeExteriorRows(-amount,direction);
	} 
//	std::cout << "outerHeight changed to " << outerHeight << std::endl;
//	std::cout << "outerWidth changed to " << outerWidth << std::endl;
}
void ClipmapRegion::addInteriorRows(int amount, int direction){
//	std::cout << "Adding " << amount << " interior rows in direction " << direction << std::endl;
//	std::cout << "innerHeight was " << innerHeight << std::endl;
//	std::cout << "innerWidth was " << innerWidth << std::endl;
	int i;
	if (amount > 0){
		switch (direction) {
			case LEFT : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(innerUpperLeftQuad.getRight())==NULL) {
						ulex=-1;
						break;
					}
					innerUpperLeftQuad = innerUpperLeftQuad.getRight();
				}
				innerWidth-=i;
				break;
			case RIGHT : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(innerLowerRightQuad.getLeft())==NULL) {
						lrex=-1;
						break;
					}
					innerLowerRightQuad = innerLowerRightQuad.getLeft();
				}
				innerWidth-=i;
				break;
			case UP : 
				for (i=0;i<amount;i++){
//					std::cout << "Changing " << i << " : " << innerUpperLeftQuad << " to " << innerUpperLeftQuad->bottom << std::endl;
					if (quadIndex->getQuad(innerUpperLeftQuad.getBottom())==NULL) {
						ulez=-1;
						break;
					}
					innerUpperLeftQuad = innerUpperLeftQuad.getBottom();
				}
				innerHeight-=i;
				break;
			case DOWN : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(innerLowerRightQuad.getTop())==NULL) {
						lrez=-1;
						break;
					}
					innerLowerRightQuad = innerLowerRightQuad.getTop();
				}
				innerHeight-=i;
				break;
		}
		changed = true;
	} else if (amount < 0) {
		removeInteriorRows(-amount,direction);
	} 
//	std::cout << "innerHeight changed to " << innerHeight << std::endl;
//	std::cout << "innerWidth changed to " << innerWidth << std::endl;
}
void ClipmapRegion::removeExteriorRows(int amount, int direction){
//	std::cout << "Removing " << amount << " exterior rows in direction " << direction << std::endl;
//	std::cout << "outerHeight was " << outerHeight << std::endl;
//	std::cout << "outerWidth was " << outerWidth << std::endl;
	int i;
	if (amount > 0){
		switch (direction) {
			case LEFT : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(upperLeftQuad.getRight())==NULL) {
						break;
					}
					upperLeftQuad = upperLeftQuad.getRight();
				}
				outerWidth-=i;
				break;
			case RIGHT : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(lowerRightQuad.getLeft())==NULL) {
						break;
					}
					lowerRightQuad = lowerRightQuad.getLeft();
				}
				outerWidth-=i;
				break;
			case UP : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(upperLeftQuad.getBottom())==NULL) {
						break;
					}
					upperLeftQuad = upperLeftQuad.getBottom();
				}
				outerHeight-=i;
				break;
			case DOWN : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(lowerRightQuad.getTop())==NULL) {
						break;
					}
					lowerRightQuad = lowerRightQuad.getTop();
				}
				outerHeight-=i;
				break;
		}
		changed = true;
	} else if (amount < 0) {
		addExteriorRows(-amount,direction);
	} 
//	std::cout << "outerHeight changed to " << outerHeight << std::endl;
//	std::cout << "outerWidth changed to " << outerWidth << std::endl;
}
void ClipmapRegion::removeInteriorRows(int amount, int direction){
//	std::cout << "Removing " << amount << " interior rows in direction " << direction << std::endl;
//	std::cout << "innerHeight was " << innerHeight << std::endl;
//	std::cout << "innerWidth was " << innerWidth << std::endl;
	int i;
	if (amount > 0){
		switch (direction) {
			case LEFT : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(innerUpperLeftQuad.getLeft())==NULL) {
						ulex=1;
						break;
					}
					innerUpperLeftQuad = innerUpperLeftQuad.getLeft();					
				}
				innerWidth+=i;
				break;
			case RIGHT : 
				for (i=0;i<amount;i++){
					if (quadIndex->getQuad(innerLowerRightQuad.getRight())==NULL) {
						lrex=1;
						break;
					}
					innerLowerRightQuad = innerLowerRightQuad.getRight();
				}
				innerWidth+=i;
				break;
			case UP : 
				for (i=0;i<amount;i++){
//					std::cout << "IULQ " << innerUpperLeftQuad << ", IULQ-top " << innerUpperLeftQuad->top << std::endl;
					if (quadIndex->getQuad(innerUpperLeftQuad.getTop())==NULL) {
						ulez=1;
						break;
					}
					innerUpperLeftQuad = innerUpperLeftQuad.getTop();
				}
				innerHeight+=i;
				break;
			case DOWN : 
				for (i=0;i<amount;i++){
//					std::cout << "Changing " << i << " : " << innerLowerRightQuad << " to " << innerLowerRightQuad->bottom << std::endl;
					if (quadIndex->getQuad(innerLowerRightQuad.getBottom())==NULL) {
						lrez=1;
						break;
					}
					innerLowerRightQuad = innerLowerRightQuad.getBottom();
				}
				innerHeight+=i;
				break;
		}
		changed = true;
	} else if (amount < 0) {
		addInteriorRows(-amount,direction);
	} 
//	std::cout << "innerHeight changed to " << innerHeight << std::endl;
//	std::cout << "innerWidth changed to " << innerWidth << std::endl;
}

Quad** ClipmapRegion::getQuads(){
//	std::cout << "getDepth() " << quadIndex->getDepth() << std::endl;
	if (changed) {
		calculateQuads();
	}
	return quadList;
}

void ClipmapRegion::calculateQuads(){
	bool print = false;
	if (print){
		std::cout << "Calculating quads..." << std::endl;
	}
//	std::cout << "getDepth() " << quadIndex->getDepth() << std::endl;
	clearBuffer();
//	std::cout << "getDepth() " << quadIndex->getDepth() << std::endl;
	int interior=0;
	int tinnerHeight;
	int tinnerWidth;

	if ((ulex!=0)||(ulez!=0)||(lrex!=0)||(lrex!=0)){
//		std::cout << "innerSquare is outside outerSquare" << std::endl;
		tinnerHeight = 0;
		tinnerWidth = 0;
	} else {
		tinnerHeight = innerHeight;
		tinnerWidth = innerWidth;
	}
	
//	std::cout << "Initialising array with size " << outerWidth*outerHeight-innerWidth*innerHeight << std::endl;
				if (print)
	std::cout << "outerWidth " << outerWidth << ", outerHeight " << outerHeight << " , innerWidth " << innerWidth << ", innerHeight " << innerHeight;
				if (print)
	std::cout << "tinnerWidth " << tinnerWidth << ", tinnerHeight " << tinnerHeight << std::endl;
	quadList = new Quad*[outerWidth*outerHeight-tinnerWidth*tinnerHeight];
	
	Vector3ix topQuad = upperLeftQuad;
	Vector3ix quad = upperLeftQuad;
	clipSize = 0;
//	std::cout << "Connecting and counting, width " << outerWidth << ", height " << outerHeight << std::endl;
	bool newline = true;
	int intstart = -1;
//	std::cout << "level " << (quad.z) << std::endl;
//	std::cout << "getDepth() " << quadIndex->getDepth() << std::endl;
	if (outerWidth*outerHeight-tinnerWidth*tinnerHeight <= 0){
	} else if (outerWidth*outerHeight-tinnerWidth*tinnerHeight==1) {
		quadList[clipSize]=quadIndex->getQuad(quad);
	} else {
		for (int i=0;i<outerWidth;i++){
//			std::cout << "getDepth() " << quadIndex->getDepth() << std::endl;
			newline = true;		
			// first quad in this row, so it is an upper
			if (quadIndex->getQuad(quad)!=NULL){
				quadIndex->getQuad(quad)->upper	= true;
			}
			for (int j=0;j<outerHeight;j++){
				if (print){
					std::cout << "Quad " << i << "," << j << std::endl;
				}
				// Check if we find the first Quad we have to be careful of.
				// That quad means we are at the edge of the interior boundary
				// If we passed that quad we have to count the number of interior
				// rows we passed
				if ((tinnerHeight*tinnerWidth>0)&&(quad == innerUpperLeftQuad)||((interior>0)&&(newline)&&(j==intstart))){
					if (print)
					std::cout << "Skipping inners" << std::endl;
					if (quad == innerUpperLeftQuad){
						if (print){
							std::cout << "Because upperInner found" << std::endl;
						}
						interior = tinnerWidth;
						intstart = j;
						newline = false;
					}
				if (print)
					std::cout << "Getting bottoms" << std::endl;
					for (int k=0;k<tinnerHeight;k++){
				if (print)
						std::cout << "Old quad " << quadIndex->getQuad(quad) << std::endl;
						quad = quad.getBottom();
				if (print)
						std::cout << "New quad " << quadIndex->getQuad(quad) << std::endl;
						if (quadIndex->getQuad(quad) == NULL){
							k = tinnerHeight;
						}
					}
				if (print)
					std::cout << "Got bottom" << quadIndex->getQuad(quad) << std::endl;
					j+=innerHeight;
					interior--;
					// If there are rows skipped, this one is an upper quad
					if (quadIndex->getQuad(quad)!=NULL){
				if (print)
						std::cout << "Setting it upper " << quadIndex->getQuad(quad) << std::endl;
						quadIndex->getQuad(quad)->upper	= true;
				if (print)
						std::cout << "It upper setted" << std::endl;
					}
				}
				if (quadIndex->getQuad(quad)==NULL){
				if (print)
					std::cout << "OldBottomQuad is NULL" << std::endl;
					j=outerHeight;				
				} else {
					if (j<outerHeight){
						if (print) {
							std::cout << "adding quad " << quadIndex->getQuad(quad) << " to " << quadList << " at " << clipSize << std::endl;
						}
						quadList[clipSize]=quadIndex->getQuad(quad);
//						std::cout << "indices " << quad->indices << std::endl;
						clipSize++;
						if (print) {
							std::cout << "Getting new quad, old : " << &quad << quad.x << "," << quad.y << std::endl;
						}
						quad = quad.getBottom();
						if (print) {
							std::cout << "Got it " << &quad << " : " << quad.x << "," << quad.y << " quadIndex " << quadIndex << std::endl;
						}						
						Quad* qquad = quadIndex->getQuad(quad);
						if (print) {
							std::cout << "Got the real quad " << qquad << std::endl;
						}
						if (qquad!=NULL){
							if (print) {
								std::cout << "Setting upper to false" << std::endl;
							}
							quadIndex->getQuad(quad)->upper	= false;
							if (print) {
								std::cout << "Setted upper to false" << std::endl;
							}
						} else {
							if (print) {
								std::cout << "Quad seems to be NULL" << std::endl;
							}
						}
					} else {
						if (print){
							std::cout << "Reached bottom with inners" << std::endl;
						}
					}
				}
				// If there are no bottomneighbours, start a new row;
				if (quadIndex->getQuad(quad)==NULL){
				if (print)
					std::cout << "BottomQuad is NULL" << std::endl;
					j=outerHeight;
				}
			}
				if (print)
			std::cout << "New line " << &topQuad << std::endl;
			topQuad = topQuad.getRight();
			quad = topQuad;
			// If there are no rightneighbours, put a marker (NULL) in the list to 
			// show we ended prematurly;
			if (quadIndex->getQuad(quad)==NULL){
				if (print)
				std::cout << "RightQuad is NULL" << std::endl;
				i=outerWidth;
				if (outerWidth*outerHeight-tinnerWidth*tinnerHeight>clipSize) {
					std::cout << "WARNING : Buffer not fully used " << outerWidth*outerHeight-innerWidth*innerHeight << " > " << clipSize << std::endl;
					quadList[clipSize]=quadIndex->getQuad(quad);
				}
			}
			
		}
	}
	if (clipSize > outerWidth*outerHeight-innerWidth*innerHeight) {
		std::cout << "ERROR : Buffer over used " << outerWidth*outerHeight-innerWidth*innerHeight << " < " << clipSize << std::endl;
	}
	
//	std::cout << "Calculated " << clipSize << std::endl;
	changed = false;
}

void ClipmapRegion::clearBuffer(){
	if (quadList!=NULL){
		delete quadList;
	}
}

int ClipmapRegion::getNbOfQuads(){
//	std::cout << "quadIndex " << quadIndex << " getDepth() " << quadIndex->getDepth() << std::endl;
	if (changed) {
		calculateQuads();
	}
	return clipSize;
}

void ClipmapRegion::makeZero(){
	clipSize = 0;
	changed = false;
}

void ClipmapRegion::setQuadIndex(QuadIndex* quadIndex){
	this->quadIndex = quadIndex;
}

void ClipmapRegion::clearBuffers(){
	if (vertices!=NULL){
		delete vertices;
	}
	if (indices!=NULL){
		delete indices;
	}
}

int ClipmapRegion::getVertexCount(int outerWidth, int outHeight, int tinnerWidth, int tinnerHeight){
	int vertexCount = (outerWidth+1)*(outerHeight+1);
	int noVertices=0;
	if ((tinnerHeight!=0)&&(tinnerWidth!=0)){
		// If the hole is on an vertical edge
		if ((innerUpperLeftQuad.x==upperLeftQuad.x)||(innerLowerRightQuad.x==lowerRightQuad.x)){
			// If the hole spans from one side to the other
			if ((innerUpperLeftQuad.x==upperLeftQuad.x)&&(innerLowerRightQuad.x==lowerRightQuad.x)){
				if ((innerUpperLeftQuad.y==upperLeftQuad.y)||(innerLowerRightQuad.y==lowerRightQuad.y)){
					// The hole is the complete plane
					if ((innerUpperLeftQuad.y==upperLeftQuad.y)&&(innerLowerRightQuad.y==lowerRightQuad.y)){
						vertexCount = 0;
					} else {
						// One horizontal edge of the plane is cut away
						vertexCount -= (tinnerHeight)*(tinnerWidth+1);
					}
				} else {
					// A strip is cut out of the plane
					vertexCount -= (tinnerHeight-1)*(tinnerWidth+1);
				}
			} else {
				// No complete horizontal spanning
				// Maybe vertical?
				if ((innerUpperLeftQuad.y==upperLeftQuad.y)||(innerLowerRightQuad.y==lowerRightQuad.y)){
					// The plane is cut verticaly in two
					if ((innerUpperLeftQuad.y==upperLeftQuad.y)&&(innerLowerRightQuad.y==lowerRightQuad.y)){
						vertexCount -= (tinnerHeight+1)*(tinnerWidth);
					} else {
						// Corner is cut off
						vertexCount -= (tinnerHeight)*(tinnerWidth);
					}
				} else {
					// The plae is cut un a 90 degree turned U
					vertexCount -= (tinnerHeight-1)*(tinnerWidth);
				}
			}
		} else {
			// If the hole is not on an vertical edge
			// Maybe on a vertical?
			if ((innerUpperLeftQuad.y==upperLeftQuad.y)||(innerLowerRightQuad.y==lowerRightQuad.y)){
				if ((innerUpperLeftQuad.y==upperLeftQuad.y)&&(innerLowerRightQuad.y==lowerRightQuad.y)){
					// A vertical strip is cut out of the plane
					vertexCount = (tinnerHeight+1)*(tinnerWidth-1);
				} else {
					// The plae is cut in a U (or upsidedown U)
					vertexCount -= (tinnerHeight)*(tinnerWidth-1);
				}
			} else {
				// A hole is cut in the plane
				vertexCount -= (tinnerHeight-1)*(tinnerWidth-1);
			}
		}
	}
	return noVertices;
}

void ClipmapRegion::triangulate(){

	// Clear the necessary buffers
	clearBuffers();

	int interior=0;
	int tinnerHeight;
	int tinnerWidth;

	if ((ulex!=0)||(ulez!=0)||(lrex!=0)||(lrex!=0)){
//		std::cout << "innerSquare is outside outerSquare" << std::endl;
		tinnerHeight = 0;
		tinnerWidth = 0;
	} else {
		tinnerHeight = innerHeight;
		tinnerWidth = innerWidth;
	}

	quadCount = outerWidth*outerHeight-tinnerWidth*tinnerHeught;
	vertexCount = getVertexCount(outerWidth, outerHeight, tinnerWidth, tinnerHeight);

	int vertexSize = 3;
#if USE_NORMALS
	vertexSize+=3;
#endif

	vertices = new Real[vertexCount*vertexSize];

#if USE_16BIT_INDICES
	indices = new unsigned short[quadCount*4];
#else
	indices = new unsigned int[quadCount*4];
#endif
	
	Real* vertex = vertices;
	int i,j;
	if (upperLeftQuad.x<innerUpperLeftQuad.x){
		Vextor3ix quadPos = upperLeftQuad;
		for(i=0;i<outerHeight;i++){
			Quad* quad = quadIndex->getQuad(quadPos);
			for (j=0;j<3;j++){
				*(vertex++) = quad->vertices[j];
			}
			for (j=0;j<3;j++){
				*(vertex++) = quad->normals[j];
			}
			quadPos.getBottom();
		}
		for (j=0;j<3;j++){
			*(vertex++) = quad->vertices[6+j];
		}
		for (j=0;j<3;j++){
			*(vertex++) = quad->normals[6+j];
		}
	} else {
		for(i=0;i<outerHeight;i++){
			Vextor3ix quadPos = upperLeftQuad;
			Quad* quad = quadIndex->getQuad(quadPos);
			if (
			for (j=0;j<3;j++){
				*(vertex++) = quad->vertices[j];
			}
			for (j=0;j<3;j++){
				*(vertex++) = quad->normals[j];
			}
		}
		for (j=0;j<3;j++){
			*(vertex++) = quad->vertices[6+j];
		}
		for (j=0;j<3;j++){
			*(vertex++) = quad->normals[6+j];
		}		
	}
	Vector3ix topQuad = upperLeftQuad;
	Vector3ix quad = upperLeftQuad;
	clipSize = 0;
//	std::cout << "Connecting and counting, width " << outerWidth << ", height " << outerHeight << std::endl;
	bool newline = true;
	int intstart = -1;
//	std::cout << "level " << (quad.z) << std::endl;
//	std::cout << "getDepth() " << quadIndex->getDepth() << std::endl;
	if (outerWidth*outerHeight-tinnerWidth*tinnerHeight <= 0){
	} else if (outerWidth*outerHeight-tinnerWidth*tinnerHeight==1) {
		quadList[clipSize]=quadIndex->getQuad(quad);
	} else {
		for (int i=0;i<outerWidth;i++){
//			std::cout << "getDepth() " << quadIndex->getDepth() << std::endl;
			newline = true;		
			// first quad in this row, so it is an upper
			if (quadIndex->getQuad(quad)!=NULL){
				quadIndex->getQuad(quad)->upper	= true;
			}
			for (int j=0;j<outerHeight;j++){
				if (print){
					std::cout << "Quad " << i << "," << j << std::endl;
				}
				// Check if we find the first Quad we have to be careful of.
				// That quad means we are at the edge of the interior boundary
				// If we passed that quad we have to count the number of interior
				// rows we passed
				if ((tinnerHeight*tinnerWidth>0)&&(quad == innerUpperLeftQuad)||((interior>0)&&(newline)&&(j==intstart))){
					if (print)
					std::cout << "Skipping inners" << std::endl;
					if (quad == innerUpperLeftQuad){
						if (print){
							std::cout << "Because upperInner found" << std::endl;
						}
						interior = tinnerWidth;
						intstart = j;
						newline = false;
					}
				if (print)
					std::cout << "Getting bottoms" << std::endl;
					for (int k=0;k<tinnerHeight;k++){
				if (print)
						std::cout << "Old quad " << quadIndex->getQuad(quad) << std::endl;
						quad = quad.getBottom();
				if (print)
						std::cout << "New quad " << quadIndex->getQuad(quad) << std::endl;
						if (quadIndex->getQuad(quad) == NULL){
							k = tinnerHeight;
						}
					}
				if (print)
					std::cout << "Got bottom" << quadIndex->getQuad(quad) << std::endl;
					j+=innerHeight;
					interior--;
					// If there are rows skipped, this one is an upper quad
					if (quadIndex->getQuad(quad)!=NULL){
				if (print)
						std::cout << "Setting it upper " << quadIndex->getQuad(quad) << std::endl;
						quadIndex->getQuad(quad)->upper	= true;
				if (print)
						std::cout << "It upper setted" << std::endl;
					}
				}
				if (quadIndex->getQuad(quad)==NULL){
				if (print)
					std::cout << "OldBottomQuad is NULL" << std::endl;
					j=outerHeight;				
				} else {
					if (j<outerHeight){
						if (print) {
							std::cout << "adding quad " << quadIndex->getQuad(quad) << " to " << quadList << " at " << clipSize << std::endl;
						}
						quadList[clipSize]=quadIndex->getQuad(quad);
//						std::cout << "indices " << quad->indices << std::endl;
						clipSize++;
						if (print) {
							std::cout << "Getting new quad, old : " << &quad << quad.x << "," << quad.y << std::endl;
						}
						quad = quad.getBottom();
						if (print) {
							std::cout << "Got it " << &quad << " : " << quad.x << "," << quad.y << " quadIndex " << quadIndex << std::endl;
						}						
						Quad* qquad = quadIndex->getQuad(quad);
						if (print) {
							std::cout << "Got the real quad " << qquad << std::endl;
						}
						if (qquad!=NULL){
							if (print) {
								std::cout << "Setting upper to false" << std::endl;
							}
							quadIndex->getQuad(quad)->upper	= false;
							if (print) {
								std::cout << "Setted upper to false" << std::endl;
							}
						} else {
							if (print) {
								std::cout << "Quad seems to be NULL" << std::endl;
							}
						}
					} else {
						if (print){
							std::cout << "Reached bottom with inners" << std::endl;
						}
					}
				}
				// If there are no bottomneighbours, start a new row;
				if (quadIndex->getQuad(quad)==NULL){
				if (print)
					std::cout << "BottomQuad is NULL" << std::endl;
					j=outerHeight;
				}
			}
				if (print)
			std::cout << "New line " << &topQuad << std::endl;
			topQuad = topQuad.getRight();
			quad = topQuad;
			// If there are no rightneighbours, put a marker (NULL) in the list to 
			// show we ended prematurly;
			if (quadIndex->getQuad(quad)==NULL){
				if (print)
				std::cout << "RightQuad is NULL" << std::endl;
				i=outerWidth;
				if (outerWidth*outerHeight-tinnerWidth*tinnerHeight>clipSize) {
					std::cout << "WARNING : Buffer not fully used " << outerWidth*outerHeight-innerWidth*innerHeight << " > " << clipSize << std::endl;
					quadList[clipSize]=quadIndex->getQuad(quad);
				}
			}
			
		}
	}
	if (clipSize > outerWidth*outerHeight-innerWidth*innerHeight) {
		std::cout << "ERROR : Buffer over used " << outerWidth*outerHeight-innerWidth*innerHeight << " < " << clipSize << std::endl;
	}
	
//	std::cout << "Calculated " << clipSize << std::endl;
	changed = false;
}





void nop(){
		Quad** quadList = getQuads();
		for (j=0;j<clipRegions[i]->getNbOfQuads();j++){
			Real* vertexList = quadList[j]->vertices;
#if USE_NORMALS
			Real* normalList = quadList[j]->normals;
#endif
#if USE_16BIT_INDICES
			unsigned short* indexList = quadList[j]->indices;
#else
			unsigned int* indexList = quadList[j]->indices;
#endif
//			std::cout << "Quads lvl 3 indices " << clipRegions[3]->getQuads()[0]->indices << std::endl;
//			std::cout << "Regions " << clipRegions[3] << "==" << clipRegions[i] << std::endl;
//			std::cout << "Quads " << clipRegions[3]->getQuads()[0] << "==" << quadList[j] << std::endl;
//			std::cout << "IndexList " << clipRegions[3]->getQuads()[0]->indices << "==" << indexList << std::endl;
			for (k=0;k<6;k++){
//				std::cout << "indexList " << indexList << ", vertex " << vertex << ", vertices " << vertices << ", vertexsize " << vertexsize << ", indexList* " << quadList[i]->indices << std::endl;
//				std::cout << "Getting index " << *(indexList)+((vertex-vertices)/vertexsize) << std::endl;
				if (quadList[j]->upper){
					*(index++) = *(indexList++)+((vertex-vertices)/vertexsize);
				} else {
					*(index++) = *(indexList++)+((vertex-vertices)/vertexsize-2);
				}
			}

//			std::cout << "Getting vertices" << std::endl;
			for (k=0;k<4;k++){
//				std::cout << "Getting vertex "; 
				for (l=0;l<3;l++){
					if (quadList[j]->upper){
						*(vertex++) = *(vertexList++);
					} else {
						if (k<2){
							vertexList++;
						} else {
							*(vertex++) = *(vertexList++);
						}
					}
				}
				vCount++;
				if (!(quadList[j]->upper)){
					if (k<2){
						numVertices--;
					}
				}
//				std::cout << "numVertices " << numVertices << ", vCount " << vCount << std::endl;
//				std::cout << std::endl;
#if USE_NORMALS
//				std::cout << "adding normals" << *(vertexList);
//				*(vertex++) = (Real)0;
//				*(vertex++) = (Real)1;
//				*(vertex++) = (Real)0;
				for (l=0;l<3;l++){					
					if (quadList[j]->upper){
						*(vertex++) = *(normalList++);
					} else {
						if (k<2){
							normalList++;
						} else {
							*(vertex++) = *(normalList++);
						}
					}
				}
#endif
			}
		}
}

}
