Index: height_map_merge/src/lib/graphics/importer/height_map.cc
===================================================================
--- height_map_merge/src/lib/graphics/importer/height_map.cc	(revision 6492)
+++ height_map_merge/src/lib/graphics/importer/height_map.cc	(revision 6494)
@@ -10,5 +10,5 @@
 
    ### File Specific:
-   main-programmer: 
+   main-programmer: bottac@ee.ethz.ch
 */
 
@@ -17,5 +17,8 @@
 #include "texture.h"
 #include "vector.h"
-
+#include "material.h"
+#include "p_node.h"
+#include "state.h"
+#include "resource_manager.h"
 #include "debug.h"
 
@@ -27,110 +30,513 @@
 #endif
 
-HeightMap::HeightMap() : StaticModel()
-{
-	
-}
-
-HeightMap::HeightMap(const char* height_map_name = NULL) : StaticModel()
+Tile::Tile(int i1, int j1, int i2, int j2, HeightMap* hm )
+{
+
+PRINTF(0)("Tile Constructor\n");
+highResModel = new VertexArrayModel();
+lowResModel  = new VertexArrayModel();
+
+this->load(i1,j1,i2,j2,hm,highResModel,4);
+this->load(i1,j1,i2,j2,hm,lowResModel, 32);
+}
+
+Tile::Tile()
+{
+PRINTF(0)("ooops *********************************************************************************************************************************************************************************************************** \n");
+}
+
+Tile::~Tile()
+{
+ delete highResModel;
+ delete lowResModel;
+}
+
+void Tile::draw()
+{
+ lowResModel->draw();
+}
+
+void Tile::drawHighRes()
+{
+ highResModel->draw();
+}
+
+void Tile::drawLowRes()
+{
+ lowResModel->draw();
+}
+
+/*
+*/
+void Tile::load(int i1, int j1, int i2, int j2, HeightMap* hm, VertexArrayModel* model, int Res)
+{
+
+        #define heightMap hm->heightMap
+        #define colours   hm->colours
+        #define scaleX hm->scaleX
+        #define scaleY hm->scaleY
+        #define scaleZ hm->scaleZ
+        #define shiftX hm->shiftX
+        #define shiftY hm->shiftY
+        #define shiftZ hm->shiftZ
+        #define normalVectorField hm->normalVectorField
+
+        int sampleRate = Res;
+
+        unsigned char height = 0;
+        int offset = 0;
+
+        float r = 0.0;
+        float g = 0.0;
+        float b = 0.0;
+
+
+PRINTF(0)("loadin * \n");
+
+ if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
+
+        SDL_LockSurface(heightMap);
+
+        for(int i = i1 ; i <= i2  ; i +=sampleRate)
+        {
+                int w = 0;
+
+
+
+                 if(hm->hasColourMap)
+                 {
+                 r = (float)colours[3*w+2 + 3*i*(heightMap->w )];
+                 g = (float)colours[3*w+1 + 3*i*(heightMap->w)];
+                 b = (float)colours[3*w+0 + 3*i*(heightMap->w)];
+                 }
+
+                w = j1;
+                model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
+                model->addNormal(normalVectorField[i][w].y,normalVectorField[i][w].z,normalVectorField[i][w].x);
+                model->addTexCoor((((w -sampleRate)/sampleRate)%4)/4.0,(((i-sampleRate)/sampleRate)%4)/4.0);
+                model->addColor(r/255.0,g/255.0,b/255.0);
+
+            for(int j = j1  ; j <= j2    ;  j += sampleRate)
+            {
+
+
+                 // To be fixed
+                 if(hm->hasColourMap)
+                 {
+                 r = (float)colours[3*j+2 + 3*i*(heightMap->w )];
+                 g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
+                 b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
+                 }
+                height =  hm->heights[j+sampleRate + i*(heightMap->w )];
+                model->addVertex(scaleX*(heightMap->h -i) + shiftX ,((double)(height)*scaleY) + shiftY ,scaleZ*(j) + shiftZ); // Top Right
+                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
+                model->addTexCoor(((j/sampleRate)%texRate)/texRatef,((i/sampleRate)%texRate)/texRatef);
+
+                model->addColor(r/255.0,g/255.0,b/255.0);
+
+
+                w = j;
+            }
+
+
+                model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
+                model->addNormal(normalVectorField[i][w].y,normalVectorField[i][w].z,normalVectorField[i][w].x);
+                model->addTexCoor((((w+sampleRate)/sampleRate)%4)/4.0,(((i+ sampleRate)/sampleRate)%4)/4.0);
+                model->addColor(r/255.0,g/255.0,b/255.0);
+
+        }
+
+
+
+
+
+        SDL_UnlockSurface(heightMap);
+        int cnt = 0;
+        for(int i = i1   ; i < i2  ; i +=sampleRate)
+        {
+
+            for(int j = j1-sampleRate  ; j < j2  + 2*sampleRate  ;  j += sampleRate)
+            {
+
+                model->addIndice(cnt);
+                model->addIndice(cnt  + (j2 -j1 + 3* sampleRate  )/ sampleRate );
+                cnt++;
+
+            }
+
+
+
+                 model->newStripe();
+
+
+        }
+         cnt += (j2 -j1 + 3* sampleRate)/ sampleRate;
+
+                for(int j = j1 ; j <= j2    ;  j += sampleRate)
+            {
+                int i = i1;
+
+                 // To be fixed
+                 if(hm->hasColourMap)
+                 {
+                 r = (float)colours[3*j+2 + 3*i*(heightMap->w )];
+                 g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
+                 b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
+                 }
+
+                model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
+                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
+                model->addTexCoor(((j/sampleRate)%texRate)/texRatef,(((i - sampleRate)/sampleRate)%texRate)/texRatef);
+                model->addColor(r/255.0,g/255.0,b/255.0);
+
+            }
+
+                for(int j = j1  ; j <= j2    ;  j += sampleRate)
+            {
+                int i = i1;
+                height =  hm->heights[j+sampleRate + i*(heightMap->w )];
+                model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right
+                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
+                model->addTexCoor(((j/sampleRate)%texRate)/texRatef,(((i)/sampleRate)%texRate)/texRatef);
+                model->addColor(r/255.0,g/255.0,b/255.0);
+
+            }
+
+
+
+            for(int j = j1  ; j <= j2    ;  j += sampleRate)
+            {
+                int i = i2;
+
+                 // To be fixed
+                 if(hm->hasColourMap)
+                 {
+                 r = (float)colours[3*j+2 + 3*i*(heightMap->w )];
+                 g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
+                 b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
+                 }
+
+                model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right
+                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
+                model->addTexCoor(((j/sampleRate)%texRate)/texRatef,(((i- sampleRate)/sampleRate)%texRate)/texRatef);
+                model->addColor(r/255.0,g/255.0,b/255.0);
+
+            }
+
+
+            for(int j = j1 ; j <= j2    ;  j += sampleRate)
+            {
+                int i = i2;
+                height =  hm->heights[j+sampleRate + i*(heightMap->w )];
+                model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right
+                model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x);
+                model->addTexCoor(((j/sampleRate)%texRate)/texRatef,((i/sampleRate)%texRate)/texRatef);
+                model->addColor(r/255.0,g/255.0,b/255.0);
+
+            }
+
+
+
+
+         // link Boarder Stripe
+            for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
+            {
+
+                model->addIndice(cnt);
+                model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
+                cnt++;
+
+            }
+
+                cnt++;
+
+                 model->newStripe();
+
+
+
+
+
+         cnt += (j2-j1)/ sampleRate;
+
+        // link 2nd BoarderStripe
+            for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
+            {
+
+                model->addIndice(cnt);
+                model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
+                cnt++;
+
+            }
+
+
+
+         model->finalize();
+
+
+
+
+        #undef heightMap
+        #undef colours
+        #undef scaleX
+        #undef scaleY
+        #undef scaleZ
+        #undef shiftX
+        #undef shiftY
+        #undef shiftZ
+        #undef normalVectorField
+
+}
+
+HeightMap::HeightMap()
+{
+
+}
+
+HeightMap::HeightMap(const char* height_map_name = NULL) : VertexArrayModel()
 {
    this->setClassID(CL_HEIGHT_MAP, "HeightMap");
    heightMap =  IMG_Load(height_map_name);
    if(heightMap!=NULL) {
-	
-		 PRINTF(0)("loading Image %s\n", height_map_name);
-		 PRINTF(0)("width : %i\n", heightMap->w);
-		 PRINTF(0)("hight : %i\n", heightMap->h);
-		 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
-		 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
-		 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
-		 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
-		 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
-		 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);		
-		}
-		 
+
+                 PRINTF(0)("loading Image %s\n", height_map_name);
+                 PRINTF(0)("width : %i\n", heightMap->w);
+                 PRINTF(0)("height : %i\n", heightMap->h);
+                 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
+                 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
+                 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
+                 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
+                 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
+                 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
+                }
+
      else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
-}
-
-
+
+
+  generateNormalVectorField();
+
+  shiftX = 0;
+  shiftY = 0;
+  shiftZ = 0;
+
+}
+
+HeightMap::HeightMap(const char* height_map_name = NULL, const char* colour_map_name = NULL) : VertexArrayModel()
+{
+this->setClassID(CL_HEIGHT_MAP, "HeightMap");
+
+   heightMap =  IMG_Load(height_map_name);
+   if(heightMap!=NULL) {
+
+                 PRINTF(0)("loading Image %s\n", height_map_name);
+                 PRINTF(0)("width : %i\n", heightMap->w);
+                 PRINTF(0)("height : %i\n", heightMap->h);
+                 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
+                 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
+                 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
+                 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
+                 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
+                 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
+                }
+
+     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
+
+
+    generateNormalVectorField();
+
+  colourMap=NULL;
+  if(colour_map_name != NULL)
+  {
+  colourMap = IMG_Load(colour_map_name);
+  }
+  if(colourMap != NULL)
+                {
+                 PRINTF(0)("loading Image %s\n", colour_map_name);
+                 PRINTF(0)("width : %i\n", colourMap->w);
+                 PRINTF(0)("height : %i\n", colourMap->h);
+                 PRINTF(0)("%i Byte(s) per Pixel \n", colourMap->format->BytesPerPixel);
+                 PRINTF(0)("Rshift : %i\n", colourMap->format->Rshift);
+                 PRINTF(0)("Bshift: %i\n", colourMap->format->Bshift);
+                 PRINTF(0)("Gshift: %i\n", colourMap->format->Gshift);
+                 PRINTF(0)("Rmask: %i\n", colourMap->format->Rmask);
+                 PRINTF(0)("Gmask: %i\n", colourMap->format->Gmask);
+                 }
+                 else       PRINTF(0)("oops! couldn't load colorMap for some reason.\n");
+
+
+
+  if(colourMap != NULL)
+  {
+  colours = (unsigned char *) colourMap->pixels;
+  hasColourMap = true;
+  }
+  else hasColourMap = false;
+
+
+   heights  = (unsigned char*) heightMap->pixels;
+
+
+}
 HeightMap::~HeightMap()
 {
-	delete heightMap;
+        delete heightMap;
 }
 
 void HeightMap::load()
 {
- unsigned char height = 0; 
- int offset = 0;
-
- char *  bmp = (char*) heightMap->pixels;
-
-  if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
-	{
-	SDL_LockSurface(heightMap);
-	for(int i = 0 ; i < heightMap->h  ; i +=2)
-	{
-	    for(int j = 0; j < heightMap->w   ;  j += 2)
-	    {
-	    	// get local hight from heightMap
-		// This seems to work only on 8-Bit Grayscale-Bitmaps
-		height = bmp[j + i*(heightMap->w )];
-		
-			
-		/*height = heightMap->format->palette->colors[offset].r +
-			   heightMap->format->palette->colors[offset].g +
-			   heightMap->format->palette->colors[offset].b ; */
-					
-				
-		this->addVertex( 20*(heightMap->h - i) , (( (double)height)/0.3)-1200  ,20*j);
-	     }	
-	}
-	SDL_UnlockSurface(heightMap);
-
-
-
-	int c = (heightMap->w)/2 ; // One line
-	for(int i = 0; i < (heightMap->w)/2  -2  ; i ++)
-	{
-	    for(int j = 0; j < (heightMap->h)/2 - 2; j++)
-	    {
-		 
-		/* Two Triangles or ...*/ 
-		
-		//this->addFace (3, VERTEX_ONLY,j + (i+1)*c,j+1+i*c , j + i*c );
-		//this->addFace (3, VERTEX_ONLY,j + (i+1)*c,j + (i+1)*c +1 ,j+i*c +1 );				
-				 
-		/* ... one square*/
-
-		this->addFace (4 ,VERTEX_ONLY,j+i*c,j+(i+1)*c  ,j + (i+1)*c +1, j +i*c+1 );
-	    }
-				
-			
-				
-	}
-		
-
-	}//if
-  else
-   	{ 
-
-  //make a cube
-  this->setName("HardCore");
-  this->addVertex (-0.5, -0.5, 0.5);
-  this->addVertex (0.5, -0.5, 0.5);
-  this->addVertex (-0.5, 0.5, 0.5);
-  this->addVertex (0.5, 0.5, 0.5);
-  this->addVertex (-0.5, 0.5, -0.5);
-  this->addVertex (0.5, 0.5, -0.5);
-  this->addVertex (-0.5, -0.5, -0.5);
-  this->addVertex (0.5, -0.5, -0.5);
-	
-
-
-
-  this->addFace (3, VERTEX_ONLY, 4, 3, 2);
-
-	}
-      this->finalize();
-
-
-}
+
+//Create a Dynamicly sized 2D-Array for Tiles
+tiles =  new Tile** [heightMap->h/tileSize];
+for(int i=0;i <    heightMap->h/tileSize ; i++)
+tiles [i]= new (Tile* [heightMap->w /tileSize ]);
+
+//SetUp Arrays
+for(int i = 0; i < (heightMap->h - tileSize )/ tileSize; i ++)
+{
+        for(int j = 0; j < (heightMap->w - tileSize )/ tileSize; j ++)
+        {
+
+         tiles[i][j] =    new Tile( i*tileSize ,  j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ;
+        }
+}
+
+}
+
+
+void HeightMap::draw()
+{
+const PNode* camera = State::getCamera();
+Vector v = camera->getAbsCoor();
+
+int i_min = 1;
+int i_max = (heightMap->h - tileSize)/ tileSize;
+int j_min = 1;
+int j_max= (heightMap->w - tileSize) / tileSize;
+
+// calc i_min , i_max j_min and j_maxt
+
+int centerX =  (int)((heightMap->h - ((v.x - shiftX)/ (float)scaleX ))/ tileSize);
+int  centerY = (int)((v.z - shiftZ)/ (float)scaleZ )/ tileSize;
+//PRINTF(0)("i_max %i \n" , i_max);
+//PRINTF(0)("CENTERY %i \n ",centerY );
+i_min = max(centerX  -1, i_min);
+i_max = min(centerX +2 , i_max );
+
+j_min = max(centerY -1,1);
+j_max = min(centerY +2, j_max);
+
+
+for(int i = 1; i <  i_min        ; i ++)
+{
+        for(int j = 1; j < j_max ; j++)
+        {
+
+         tiles[i][j]->drawLowRes();
+        }
+}
+for(int i = i_min; i <  i_max    ; i ++)
+{
+        for(int j = j_min; j < j_max ; j++)
+        {
+
+         tiles[i][j]->drawHighRes();
+        }
+}
+
+for(int i = i_max; i <  (heightMap->h -tileSize)/tileSize        ; i ++)
+{
+        for(int j = 1; j < j_max ; j++)
+        {
+
+         tiles[i][j]->drawLowRes();
+        }
+}
+
+
+for(int i = 1; i < (heightMap->h -tileSize)/tileSize; i++ )
+{
+        for(int j = 1; j < j_min; j++)
+        {
+         tiles[i][j]->drawLowRes();
+        }
+}
+for(int i = 1; i < (heightMap->h -tileSize)/tileSize; i++)
+{
+        for(int j = j_max; j < (heightMap->w -tileSize)/tileSize; j++ )
+        {
+        tiles[i][j]->drawLowRes();
+        }
+}
+
+}
+void HeightMap::generateNormalVectorField()
+{
+int delta = 1;
+heights  = (unsigned char*) heightMap->pixels;
+
+//Create a Dynamicly sized 2D-Array to store our normals
+normalVectorField =  new Vector* [heightMap->h];
+for(int i=0;i<heightMap->h;i++)
+normalVectorField [i]= new (Vector [heightMap->w]);
+
+
+
+// !!! Does not yet calculate the normals of some border points!!!!!
+
+if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
+        {
+        SDL_LockSurface(heightMap);
+        for(int i = 0 ; i < heightMap->h - 1  ; i ++)
+        {
+            for(int j = 0; j < heightMap->w  - 1  ;  j ++)
+            {
+
+
+                delta = (int)heights[j + (i+1)*(heightMap->w )] -  (int) heights[j + i*(heightMap->w )];
+                Vector a =  Vector(-scaleX,(float)delta*scaleY  ,0.0f);
+
+                delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )];
+                Vector b =  Vector(0.0f,(float) delta*scaleY ,scaleZ);
+
+
+                 normalVectorField[i][j] = b.cross(a);
+                 normalVectorField[i][j].normalize();
+
+             }
+        }
+        SDL_UnlockSurface(heightMap);
+
+}
+
+}
+
+
+
+
+
+void HeightMap::scale(Vector v)
+{
+ scaleX = v.x;
+ scaleY = v.y;
+ scaleZ = v.z;
+}
+
+void HeightMap::shift(Vector v)
+{
+ shiftX = v.x;
+ shiftY = v.y;
+ shiftZ = v.z;
+}
+
+
+float HeightMap::getHeight(float x, float y)
+{
+ int xInt = (int)x / scaleX;  x -= (float)xInt*scaleX; xInt = heightMap->h - xInt;
+ int yInt = (int)y / scaleZ;    y -= (float)yInt*scaleZ;
+ if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->w  ) return 0.0f;
+ float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
+ float a = normalVectorField[(xInt)][yInt].x;
+ float b = normalVectorField [(xInt)][yInt].z;
+ float c = normalVectorField [(xInt)][yInt].y;
+  height -= ( (a/c)*(x) + (b/c)*(y))*scaleY;
+ return height;
+}
Index: height_map_merge/src/lib/graphics/importer/height_map.h
===================================================================
--- height_map_merge/src/lib/graphics/importer/height_map.h	(revision 6492)
+++ height_map_merge/src/lib/graphics/importer/height_map.h	(revision 6494)
@@ -14,20 +14,95 @@
 
 
-#include "static_model.h"
+#include "vertex_array_model.h"
+
+
+#define texRate      4
+#define texRatef     4.0f
+#define tileSize     64
 
 class SDL_Surface;
 class Vector;
+class Material;
+class PNode;
+class Texture;
+class HeightMap;
 
-class HeightMap : public StaticModel
+class Tile 
 {
 public:
+void draw();
+void drawHighRes();
+void drawLowRes();
+int getRes();
+int setHighRes(bool b);
+Tile(int i1, int j1, int i2, int j2, HeightMap* hm ) ;
+Tile();
+~Tile();
+
+
+
+private:
+VertexArrayModel* highResModel;
+VertexArrayModel* lowResModel; 
+int highRes;
+int lowRes;
+void load(int i1, int j1, int i2, int i2, HeightMap* hm, VertexArrayModel* model, int Res);
+};
+
+class HeightMap : public VertexArrayModel
+{
+public:
+void draw();
 void load();
-void load(const char*, int);
+void load(int Mode);
+void load(const char*, int Mode);
+void scale( Vector V); 
+void shift (Vector V);
+float getHeight(float x, float y); 
 HeightMap();
 HeightMap(const char*);
+HeightMap(const char*, const char*);
 ~HeightMap();
+
+friend class Tile;
 
 private:
 SDL_Surface* heightMap;
+SDL_Surface* colourMap;
+unsigned char* heights; 
+unsigned char* colours;
 
+void generateNormalVectorField();
+void drawRect(int xBottomLeft, int yBottomLeft, int xTopRight, int yTopRight );
+void fixBoarder(int xBottomLeft, int yBottomLeft, int xTopRight, int yTopRight);
+Vector  **  normalVectorField ; 
+Tile    ***  tiles;
+Vector     camCoords;
+Material * tmp_mat;
+Material* red_mat;
+Texture* texture;
+const PNode* camera;
+float scaleX ;
+float scaleY ;
+float scaleZ ;
+float shiftX ;
+float shiftY ;
+float shiftZ ;
+int   cmScaleX;
+int   cmScaleY;
+bool  hasColourMap;
+
+inline int abs(int val)
+{
+ if(val < 0) val = -val;
+ return val;
+}
+inline int max(int x, int y)
+{
+ return (x>y)? x:y;
+}
+inline int min(int x, int y)
+{
+return (x<y)? x: y;
+}
 };
