]> Creatis software - creaRigidRegistration.git/commitdiff
Added template methods
authortrillos <trillos>
Wed, 18 Nov 2009 09:54:40 +0000 (09:54 +0000)
committertrillos <trillos>
Wed, 18 Nov 2009 09:54:40 +0000 (09:54 +0000)
lib/CheckBoard.cpp
lib/CheckBoard.h
lib/Substraction.cxx
lib/Substraction.h
lib/Surface.cxx
lib/Surface.h
lib/Transformer.cxx
lib/Transformer.h
lib/Transparency.cxx
lib/Transparency.h

index afa33d2c9e98bea1551be40099aa496eac14a3dc..b7668359720ac21f6e905fe60005d2e4f36c610f 100644 (file)
@@ -14,6 +14,8 @@ CheckBoard::CheckBoard()
        newImage = NULL;
        processed=false;
        newImage = NULL;
+       resample = NULL;
+       result = NULL;
 }
 
 /*
@@ -25,6 +27,8 @@ CheckBoard::~CheckBoard()
        //if (image1 != NULL ) { image1->Delete(); }
        //if (image2 != NULL ) { image2->Delete(); }
        if (newImage != NULL ) { newImage->Delete(); }
+       if (resample != NULL ) { resample->Delete(); }
+       if (result != NULL ) { result->Delete(); }
 }
 
 //------------------------------------------------------------
@@ -63,8 +67,8 @@ void CheckBoard::calculateImage()
                
                double factorX = 1;
                double factorY = 1;
-               vtkImageResample *resample = vtkImageResample::New();
-               vtkImageData *result;
+               resample = vtkImageResample::New();
+               result = vtkImageData::New();
                if(numPixelsImg1<numPixelsImg2)
                {
                        resample->SetInput(image1);
@@ -149,55 +153,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                char* dataImagePointer1 = NULL;
                char* dataImagePointer2 = NULL;
                char* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (char*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (char*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (char*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
-
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (char*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (char*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (char*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (char) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (char) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
 
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_SIGNED_CHAR)
        {
@@ -205,54 +162,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                signed char* dataImagePointer1 = NULL;
                signed char* dataImagePointer2 = NULL;
                signed char* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (signed char*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (signed char*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (signed char*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int     divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (signed char*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (signed char*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (signed char*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (signed char) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (signed char) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_UNSIGNED_CHAR)
        {
@@ -260,54 +171,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                unsigned char* dataImagePointer1 = NULL;
                unsigned char* dataImagePointer2 = NULL;
                unsigned char* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (unsigned char*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (unsigned char*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (unsigned char*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (unsigned char*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (unsigned char*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (unsigned char*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (unsigned char) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (unsigned char) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_SHORT)
        {
@@ -315,54 +180,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                short* dataImagePointer1 = NULL;
                short* dataImagePointer2 = NULL;
                short* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (short*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (short*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (short*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (short*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (short*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (short*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (short) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (short) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_UNSIGNED_SHORT)
        {
@@ -370,54 +189,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                unsigned short* dataImagePointer1 = NULL;
                unsigned short* dataImagePointer2 = NULL;
                unsigned short* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (unsigned short*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (unsigned short*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (unsigned short*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (unsigned short*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (unsigned short*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (unsigned short*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (unsigned short) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (unsigned short) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
 
        else if(imageType == VTK_INT)
@@ -426,54 +199,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                int* dataImagePointer1 = NULL;
                int* dataImagePointer2 = NULL;
                int* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (int*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (int*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (int*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (int*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (int*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (int*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (int) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (int) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_UNSIGNED_INT)
        {
@@ -481,54 +208,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                unsigned int* dataImagePointer1 = NULL;
                unsigned int* dataImagePointer2 = NULL;
                unsigned int* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (unsigned int*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (unsigned int*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (unsigned int*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (unsigned int*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (unsigned int*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (unsigned int*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (unsigned int) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (unsigned int) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_LONG)
        {
@@ -536,54 +217,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                long* dataImagePointer1 = NULL;
                long* dataImagePointer2 = NULL;
                long* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (long*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (long*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (long*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (long*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (long*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (long*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (long) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (long) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_UNSIGNED_LONG)
        {
@@ -591,53 +226,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                unsigned long* dataImagePointer1 = NULL;
                unsigned long* dataImagePointer2 = NULL;
                unsigned long* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (unsigned long*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (unsigned long*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (unsigned long*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (unsigned long*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (unsigned long*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (unsigned long*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (unsigned long) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (unsigned long) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_FLOAT)
        {
@@ -645,54 +235,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                float* dataImagePointer1 = NULL;
                float* dataImagePointer2 = NULL;
                float* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (float*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (float*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (float*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (float*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (float*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (float*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (float) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (float) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }
-                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
        else if(imageType == VTK_DOUBLE)
        {
@@ -700,53 +244,8 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
                double* dataImagePointer1 = NULL;
                double* dataImagePointer2 = NULL;
                double* dataImageResultPointer = NULL;
-       
-               dataImagePointer1 = (double*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (double*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (double*) newImage->GetScalarPointer(0,0,0);
-       
-               if(squaresX == 0)
-               {
-                       squaresX = 1;
-               }
-               if(squaresY == 0)
-               {
-                       squaresY = 1;
-               }    
 
-               int divX = sizeX/squaresX;
-               int divY = sizeY/squaresY;
-               int i, j, counterX, counterY;
-               for(i = 0; i < sizeX; i++)
-               {
-                       counterY = 0; 
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (double*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (double*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (double*)newImage->GetScalarPointer(i,j,0);
-
-                               if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (double) *dataImagePointer1;
-                               }
-                               else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
-                               {
-                                       *dataImageResultPointer = (double) *dataImagePointer2;
-                               }
-
-                               if(counterX >= 2*divX)
-                               {
-                                       counterX = 0;
-                               }
-                               else if(counterY >= 2*divY)
-                               {
-                                       counterY = 0;
-                               }                       
-                               counterY++;
-                       }
-                       counterX++;
-               }
+               createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
        }
 
                
@@ -754,6 +253,57 @@ void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX,
    newImage->Update();
 //   newImage->Modified();
 }
+/**Template used for the different image types
+*/
+template <class T>
+void CheckBoard::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
+{
+       dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
+       dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
+       dataImageResultPointer = (T*) newImage->GetScalarPointer(0,0,0);
+       
+       if(squaresX == 0)
+       {
+               squaresX = 1;
+       }
+       if(squaresY == 0)
+       {
+               squaresY = 1;
+       }    
+
+       int divX = sizeX/squaresX;
+       int divY = sizeY/squaresY;
+       int i, j, counterX, counterY;
+       for(i = 0; i < sizeX; i++)
+       {
+               counterY = 0; 
+               for(j = 0; j < sizeY; j++)
+               {
+                       dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
+                       dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
+                       dataImageResultPointer = (T*)newImage->GetScalarPointer(i,j,0);
+                       if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
+                       {
+                               *dataImageResultPointer = (T) *dataImagePointer1;
+                       }
+                       else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
+                       {
+                               *dataImageResultPointer = (T) *dataImagePointer2;
+                       }
+
+                       if(counterX >= 2*divX)
+                       {
+                               counterX = 0;
+                       }
+                       else if(counterY >= 2*divY)
+                       {
+                               counterY = 0;
+                       }                       
+                       counterY++;
+               }
+               counterX++;
+       }
+}
 
 /*
 * Get new image
index cc1ef870ac4638ce0fcee260dc0fbd7adceef460..d7774780957922a36f6e4bb9dc1e0ca5ca864b97 100644 (file)
@@ -33,6 +33,7 @@ class CheckBoard
                void setCols(int cols);
 
                void setRows(int rows);
+               template <class T> void createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY);
 
        // --- Atributes --- //
        private: 
@@ -48,6 +49,12 @@ class CheckBoard
                //Resulting image
                vtkImageData *newImage;
 
+               //Image Resampler
+               vtkImageResample *resample;
+
+               //Rsulting image from the resampler
+               vtkImageData *result;
+
                //Total number of divisions for the X axis (colums)
                int squaresX;
 
index ee25e7039499e38a54ce08ff897c2532b04e3d9c..c0be6d87ba896e338b4b9df3c75828c111694dd9 100644 (file)
@@ -116,74 +116,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                char* dataImagePointer1=NULL;
                char* dataImagePointer2=NULL;
                char* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(char*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(char*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(char*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(char*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(char*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(char*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(char) lowerColor[0];
-                                               dataImageResultPointer[1] =(char) lowerColor[1];
-                                               dataImageResultPointer[2] =(char) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(char) upperColor[0];
-                                               dataImageResultPointer[1] =(char) upperColor[1];
-                                               dataImageResultPointer[2] =(char) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(char) mediumColor[0];
-                                               dataImageResultPointer[1] =(char) mediumColor[1];
-                                               dataImageResultPointer[2] =(char) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_UNSIGNED_CHAR)
        {
@@ -194,74 +128,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                unsigned char* dataImagePointer1=NULL;
                unsigned char* dataImagePointer2=NULL;
                unsigned char* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(unsigned char*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(unsigned char*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(unsigned char*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(unsigned char*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(unsigned char*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(unsigned char*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(unsigned char) lowerColor[0];
-                                               dataImageResultPointer[1] =(unsigned char) lowerColor[1];
-                                               dataImageResultPointer[2] =(unsigned char) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(unsigned char) upperColor[0];
-                                               dataImageResultPointer[1] =(unsigned char) upperColor[1];
-                                               dataImageResultPointer[2] =(unsigned char) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(unsigned char) mediumColor[0];
-                                               dataImageResultPointer[1] =(unsigned char) mediumColor[1];
-                                               dataImageResultPointer[2] =(unsigned char) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }               
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_SIGNED_CHAR)
        {
@@ -272,74 +140,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                signed char* dataImagePointer1=NULL;
                signed char* dataImagePointer2=NULL;
                signed char* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(signed char*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(signed char*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(signed char*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(signed char*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(signed char*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(signed char*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(signed char) lowerColor[0];
-                                               dataImageResultPointer[1] =(signed char) lowerColor[1];
-                                               dataImageResultPointer[2] =(signed char) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(signed char) upperColor[0];
-                                               dataImageResultPointer[1] =(signed char) upperColor[1];
-                                               dataImageResultPointer[2] =(signed char) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(signed char) mediumColor[0];
-                                               dataImageResultPointer[1] =(signed char) mediumColor[1];
-                                               dataImageResultPointer[2] =(signed char) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_SHORT)
        {
@@ -350,74 +152,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                short* dataImagePointer1=NULL;
                short* dataImagePointer2=NULL;
                short* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(short*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(short*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(short*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(short*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(short*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(short*)imageResult->GetScalarPointer(i,j,k);
 
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(short) lowerColor[0];
-                                               dataImageResultPointer[1] =(short) lowerColor[1];
-                                               dataImageResultPointer[2] =(short) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(short) upperColor[0];
-                                               dataImageResultPointer[1] =(short) upperColor[1];
-                                               dataImageResultPointer[2] =(short) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(short) mediumColor[0];
-                                               dataImageResultPointer[1] =(short) mediumColor[1];
-                                               dataImageResultPointer[2] =(short) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);          
        }
        else if(imageType == VTK_UNSIGNED_SHORT)
        {
@@ -428,74 +164,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                unsigned short* dataImagePointer1=NULL;
                unsigned short* dataImagePointer2=NULL;
                unsigned short* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(unsigned short*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(unsigned short*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(unsigned short*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(unsigned short*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(unsigned short*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(unsigned short*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(unsigned short) lowerColor[0];
-                                               dataImageResultPointer[1] =(unsigned short) lowerColor[1];
-                                               dataImageResultPointer[2] =(unsigned short) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(unsigned short) upperColor[0];
-                                               dataImageResultPointer[1] =(unsigned short) upperColor[1];
-                                               dataImageResultPointer[2] =(unsigned short) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(unsigned short) mediumColor[0];
-                                               dataImageResultPointer[1] =(unsigned short) mediumColor[1];
-                                               dataImageResultPointer[2] =(unsigned short) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }               
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_INT)
        {
@@ -506,74 +176,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                int* dataImagePointer1=NULL;
                int* dataImagePointer2=NULL;
                int* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(int*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(int*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(int*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(int*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(int*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(int*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(int) lowerColor[0];
-                                               dataImageResultPointer[1] =(int) lowerColor[1];
-                                               dataImageResultPointer[2] =(int) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(int) upperColor[0];
-                                               dataImageResultPointer[1] =(int) upperColor[1];
-                                               dataImageResultPointer[2] =(int) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(int) mediumColor[0];
-                                               dataImageResultPointer[1] =(int) mediumColor[1];
-                                               dataImageResultPointer[2] =(int) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_UNSIGNED_INT)
        {
@@ -584,74 +188,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                unsigned int* dataImagePointer1=NULL;
                unsigned int* dataImagePointer2=NULL;
                unsigned int* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(unsigned int*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(unsigned int*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(unsigned int*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(unsigned int*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(unsigned int*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(unsigned int*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(unsigned int) lowerColor[0];
-                                               dataImageResultPointer[1] =(unsigned int) lowerColor[1];
-                                               dataImageResultPointer[2] =(unsigned int) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(unsigned int) upperColor[0];
-                                               dataImageResultPointer[1] =(unsigned int) upperColor[1];
-                                               dataImageResultPointer[2] =(unsigned int) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(unsigned int) mediumColor[0];
-                                               dataImageResultPointer[1] =(unsigned int) mediumColor[1];
-                                               dataImageResultPointer[2] =(unsigned int) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }               
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_LONG)
        {
@@ -662,74 +200,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                long* dataImagePointer1=NULL;
                long* dataImagePointer2=NULL;
                long* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(long*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(long*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(long*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
 
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(long*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(long*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(long*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(long) lowerColor[0];
-                                               dataImageResultPointer[1] =(long) lowerColor[1];
-                                               dataImageResultPointer[2] =(long) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(long) upperColor[0];
-                                               dataImageResultPointer[1] =(long) upperColor[1];
-                                               dataImageResultPointer[2] =(long) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(long) mediumColor[0];
-                                               dataImageResultPointer[1] =(long) mediumColor[1];
-                                               dataImageResultPointer[2] =(long) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_UNSIGNED_LONG)
        {
@@ -740,74 +212,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                unsigned long* dataImagePointer1=NULL;
                unsigned long* dataImagePointer2=NULL;
                unsigned long* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(unsigned long*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(unsigned long*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(unsigned long*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(unsigned long*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(unsigned long*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(unsigned long*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(unsigned long) lowerColor[0];
-                                               dataImageResultPointer[1] =(unsigned long) lowerColor[1];
-                                               dataImageResultPointer[2] =(unsigned long) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(unsigned long) upperColor[0];
-                                               dataImageResultPointer[1] =(unsigned long) upperColor[1];
-                                               dataImageResultPointer[2] =(unsigned long) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(unsigned long) mediumColor[0];
-                                               dataImageResultPointer[1] =(unsigned long) mediumColor[1];
-                                               dataImageResultPointer[2] =(unsigned long) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }               
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_FLOAT)
        {
@@ -818,74 +224,8 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                float* dataImagePointer1=NULL;
                float* dataImagePointer2=NULL;
                float* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(float*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(float*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(float*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
-
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
-               {
-                       for(j=0;j<sy;j++)
-                       {
-                               for(k=0;k<sz;k++)
-                               {
-                                       
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(float*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(float*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(float*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(float) lowerColor[0];
-                                               dataImageResultPointer[1] =(float) lowerColor[1];
-                                               dataImageResultPointer[2] =(float) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(float) upperColor[0];
-                                               dataImageResultPointer[1] =(float) upperColor[1];
-                                               dataImageResultPointer[2] =(float) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(float) mediumColor[0];
-                                               dataImageResultPointer[1] =(float) mediumColor[1];
-                                               dataImageResultPointer[2] =(float) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
-                               }
-                       }
-               }
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
        }
        else if(imageType == VTK_DOUBLE)
        {
@@ -896,76 +236,83 @@ void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
                double* dataImagePointer1=NULL;
                double* dataImagePointer2=NULL;
                double* dataImageResultPointer=NULL;
-               // we start where the  image starts
-               dataImagePointer1=(double*)imageData1->GetScalarPointer(0,0,0);
-               dataImagePointer2=(double*)imageData2->GetScalarPointer(0,0,0);
-               dataImageResultPointer=(double*)imageResult->GetScalarPointer(0,0,0);
-       
-               /*
-               Image Size
-               */
-               int ext[6];
-               imageData1->GetExtent(ext);
-               int sx,sy,sz;
-               sx=ext[1]-ext[0]+1;
-               sy=ext[3]-ext[2]+1;
-               sz=ext[5]-ext[4]+1;
-
-               sizeImage=sx*sy*sz;
+               
+               substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
+       }
+}
 
-               //-----------------
-               //A3    
-               //-----------------
-               //walking in the image
-               int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
-               double sum1=0,sum2=0;
-               for(i=0;i<sx;i++)
+template <class T> 
+void Substraction::substractByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *imageData1, vtkImageData *imageData2)
+{
+       // we start where the  image starts
+       dataImagePointer1=(T*)imageData1->GetScalarPointer(0,0,0);
+       dataImagePointer2=(T*)imageData2->GetScalarPointer(0,0,0);
+       dataImageResultPointer=(T*)imageResult->GetScalarPointer(0,0,0);
+       /*
+       Image Size
+       */
+       int ext[6];
+       imageData1->GetExtent(ext);
+       int sx,sy,sz;
+       sx=ext[1]-ext[0]+1;
+       sy=ext[3]-ext[2]+1;
+       sz=ext[5]-ext[4]+1;
+
+       sizeImage=sx*sy*sz;
+       //-----------------
+       //A3    
+       //-----------------
+       //walking in the image
+       int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
+       double sum1=0,sum2=0;
+       for(i=0;i<sx;i++)
+       {
+               for(j=0;j<sy;j++)
                {
-                       for(j=0;j<sy;j++)
+                       for(k=0;k<sz;k++)
                        {
-                               for(k=0;k<sz;k++)
-                               {
                                        
-                                       // this is for getting just the grey level in that position
-                                       //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-                               
-                                       // we get the pointer to the position (i,j,k)y that way we can get the 
-                                       //grey level and we can change it
-                                       dataImagePointer1=(double*)imageData1->GetScalarPointer(i,j,k);
-                                       dataImagePointer2=(double*)imageData2->GetScalarPointer(i,j,k);
-                                       dataImageResultPointer=(double*)imageResult->GetScalarPointer(i,j,k);
-
-                                       sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
-                                       sum1=sum1/3;
-                                       sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
-                                       sum2=sum2/3;                            
-                                       if((sum1 - sum2) < lZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(double) lowerColor[0];
-                                               dataImageResultPointer[1] =(double) lowerColor[1];
-                                               dataImageResultPointer[2] =(double) lowerColor[2];
-                                               nL++;
-                                       }
-                                       else if((sum1 - sum2) > uZeroLevel)
-                                       {
-                                               dataImageResultPointer[0] =(double) upperColor[0];
-                                               dataImageResultPointer[1] =(double) upperColor[1];
-                                               dataImageResultPointer[2] =(double) upperColor[2];
-                                               nU++;
-                                       }
-                                       else
-                                       {
-                                               dataImageResultPointer[0] =(double) mediumColor[0];
-                                               dataImageResultPointer[1] =(double) mediumColor[1];
-                                               dataImageResultPointer[2] =(double) mediumColor[2];
-                                               nZ++;
-                                       }                               
-                                       counter++;
+                               // this is for getting just the grey level in that position
+                               //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
+               
+                               // we get the pointer to the position (i,j,k)y that way we can get the 
+                               //grey level and we can change it
+
+                               dataImagePointer1=(T*)imageData1->GetScalarPointer(i,j,k);
+                               dataImagePointer2=(T*)imageData2->GetScalarPointer(i,j,k);
+                               dataImageResultPointer=(T*)imageResult->GetScalarPointer(i,j,k);
+
+                               sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
+                               sum1=sum1/3;
+                               sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
+                               sum2=sum2/3;                            
+                               if((sum1 - sum2) < lZeroLevel)
+                               {
+                                       dataImageResultPointer[0] =(T) lowerColor[0];
+                                       dataImageResultPointer[1] =(T) lowerColor[1];
+                                       dataImageResultPointer[2] =(T) lowerColor[2];
+                                       nL++;
                                }
+                               else if((sum1 - sum2) > uZeroLevel)
+                               {
+                                       dataImageResultPointer[0] =(T) upperColor[0];
+                                       dataImageResultPointer[1] =(T) upperColor[1];
+                                       dataImageResultPointer[2] =(T) upperColor[2];
+                                       nU++;
+                               }
+                               else
+                               {
+                                       dataImageResultPointer[0] =(T) mediumColor[0];
+                                       dataImageResultPointer[1] =(T) mediumColor[1];
+                                       dataImageResultPointer[2] =(T) mediumColor[2];
+                                       nZ++;
+                               }                               
+                               counter++;
                        }
                }
        }
 }
+
 /*
 Returns the filtered image
 */
index 85a3991528688ca9fd308eb21b52e7a53c1a284b..747caf44f6cb32c94fcafed82dd4abecf1112dee 100644 (file)
@@ -42,6 +42,11 @@ class Substraction
                constructing image substract
                */
                void substract(vtkImageData* imageData1, vtkImageData* imageData2);
+
+               /*
+               Template method of substract for different image types
+               */
+               template <class T> void substractByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *imageData1, vtkImageData *imageData2);
                
 
 
index ac7467d562f2faddfafbe623be3dfa245005edc7..08993a91925fc4959d37d82edd5c8549602f4154 100644 (file)
@@ -1,14 +1,12 @@
 #include "vtkImageData.h"
 #include "vtkImageCast.h"
 #include "vtkActor.h"
-#include "vtkPoints.h"
 #include "vtkPolyDataMapper.h"
 #include "vtkPolyData.h"
 #include "vtkProperty.h"
 #include "vtkFloatArray.h"
 #include "vtkType.h"
 #include "vtkDataSetMapper.h"
-#include "vtkCellArray.h"
 
 #include "Surface.h"
 
@@ -76,243 +74,149 @@ void Surface::surface(vtkImageData* imageData)
        /*
                images pointers
        */
-       vtkPoints* surfacePoints = vtkPoints::New();
-       vtkCellArray* surfaceCells = vtkCellArray::New();
-       int counter=0;
-
+       surfacePoints = vtkPoints::New();
+       surfaceCells = vtkCellArray::New();
+       
        if(imageType == VTK_CHAR)
        {
                // pointers to get into the image
-       char* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(char*)imageData->GetScalarPointer(0,0,0);     
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
+               char* dataImagePointer=NULL;
+               
+               char max = VTK_CHAR_MAX;
 
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
+               surfaceByType(dataImagePointer, imageData, max);
+       }               
+       else if(imageType == VTK_SIGNED_CHAR)
        {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(char*)imageData->GetScalarPointer(i,j,k);                             
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_CHAR_MAX);                             
+               // pointers to get into the image
+               signed char* dataImagePointer=NULL;
 
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
+               signed char max = VTK_SIGNED_CHAR_MAX;
 
-                               counter ++;
-                       }
-               }
+               surfaceByType(dataImagePointer, imageData, max);        
        }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
+       else if(imageType == VTK_UNSIGNED_CHAR)
        {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
+               // pointers to get into the image
+               unsigned char* dataImagePointer=NULL;
 
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
+               unsigned char max = VTK_UNSIGNED_CHAR_MAX;
 
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
-       }               
+               surfaceByType(dataImagePointer, imageData, max);        
        }
-       else if(imageType == VTK_SIGNED_CHAR)
-       {
-                       // pointers to get into the image
-       signed char* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(signed char*)imageData->GetScalarPointer(0,0,0);      
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
-
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
+       else if(imageType == VTK_SHORT)
        {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(signed char*)imageData->GetScalarPointer(i,j,k);                              
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_SIGNED_CHAR_MAX);                              
+               // pointers to get into the image
+               short* dataImagePointer=NULL;
 
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
+               short max = VTK_SHORT_MAX;
 
-                               counter ++;
-                       }
-               }
+               surfaceByType(dataImagePointer, imageData, max);        
        }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
+       else if(imageType == VTK_UNSIGNED_SHORT)
        {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
+               // pointers to get into the image
+               unsigned short* dataImagePointer=NULL;
 
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
+               unsigned short max = VTK_UNSIGNED_SHORT_MAX;
 
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
+               surfaceByType(dataImagePointer, imageData, max);        
        }
+       else if(imageType == VTK_INT)
+       {
+               // pointers to get into the image
+               int* dataImagePointer=NULL;
+               
+               int max = VTK_INT_MAX;
 
+               surfaceByType(dataImagePointer, imageData, max);
        }
-       else if(imageType == VTK_UNSIGNED_CHAR)
+       else if(imageType == VTK_UNSIGNED_INT)
        {
                // pointers to get into the image
-       unsigned char* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(unsigned char*)imageData->GetScalarPointer(0,0,0);    
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
+               unsigned int* dataImagePointer=NULL;
+               
+               unsigned int max = VTK_UNSIGNED_INT_MAX;
 
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
+               surfaceByType(dataImagePointer, imageData, max);
+       }
+       else if(imageType == VTK_LONG)
        {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(unsigned char*)imageData->GetScalarPointer(i,j,k);                            
+               // pointers to get into the image
+               long* dataImagePointer=NULL;
+               
+               long max = VTK_LONG_MAX;
 
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_UNSIGNED_CHAR_MAX);                            
+               surfaceByType(dataImagePointer, imageData, max);
+       }
+       else if(imageType == VTK_UNSIGNED_LONG)
+       {
+               // pointers to get into the image
+               unsigned long* dataImagePointer=NULL;
 
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
+               unsigned long max = VTK_UNSIGNED_LONG_MAX;
 
-                               counter ++;
-                       }
-               }
+               surfaceByType(dataImagePointer, imageData, max);        
        }
+       else if(imageType == VTK_FLOAT)
+       {               
+               // pointers to get into the image
+               float* dataImagePointer=NULL;
 
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
+               float max = VTK_FLOAT_MAX;
+
+               surfaceByType(dataImagePointer, imageData, max);        
+       }
+       else if(imageType == VTK_DOUBLE)
        {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
+               // pointers to get into the image
+               double* dataImagePointer=NULL;
 
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
+               double max = VTK_DOUBLE_MAX;
 
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
+               surfaceByType(dataImagePointer, imageData, max);
        }
+
+       vtkPolyData* surfaceData = vtkPolyData::New();
+       surfaceData->SetPolys(surfaceCells);
+       surfaceData->SetPoints(surfacePoints);
+       surfaceData->Update();
+
+       vtkPolyDataMapper* surfaceMapper = vtkPolyDataMapper::New();
+       surfaceMapper->SetInput(surfaceData);
+       surfaceMapper->Update();
+
+    surfaceResult->SetMapper(surfaceMapper);
+       surfaceResult->GetProperty()->SetOpacity(1.0);
+
+       if (color == "RED")
+       {
+               surfaceResult->GetProperty()->SetColor(1,0,0);
        }
-       else if(imageType == VTK_SHORT)
+       else if (color == "BLUE")
+       {
+               surfaceResult->GetProperty()->SetColor(0,0,1);
+       }
+       else if (color == "GREEN")
+       {
+               surfaceResult->GetProperty()->SetColor(0,1,0);
+       }
+       else
        {
-                       // pointers to get into the image
-       short* dataImagePointer=NULL;
+               surfaceResult->GetProperty()->SetColor(1,1,1);
+       }
+}
+
+/*
+Template for constructing the surface by image type
+*/
+template <class T>
+void Surface::surfaceByType(T* dataImagePointer, vtkImageData* imageData, T max)
+{
+       int counter=0;
        
        // we start where the  image starts
-       dataImagePointer=(short*)imageData->GetScalarPointer(0,0,0);    
+       dataImagePointer=(T*)imageData->GetScalarPointer(0,0,0);        
        
        /*
         Image Size
@@ -338,10 +242,10 @@ void Surface::surface(vtkImageData* imageData)
                        {
                                
                                // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(short*)imageData->GetScalarPointer(i,j,k);                            
+                               dataImagePointer=(T*)imageData->GetScalarPointer(i,j,k);                                
 
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_SHORT_MAX);                            
+                               sum1=(T)(dataImagePointer[0]) + (T)(dataImagePointer[1]) + (T)(dataImagePointer[2]);
+                               sum1=sum1/(3*max);                              
 
                                surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
 
@@ -380,571 +284,8 @@ void Surface::surface(vtkImageData* imageData)
                        }
                }
        }
+}
 
-       }
-       else if(imageType == VTK_UNSIGNED_SHORT)
-       {
-                       // pointers to get into the image
-       unsigned short* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(unsigned short*)imageData->GetScalarPointer(0,0,0);   
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
-
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
-       {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(unsigned short*)imageData->GetScalarPointer(i,j,k);                           
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_UNSIGNED_SHORT_MAX);                           
-
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
-
-                               counter ++;
-                       }
-               }
-       }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
-       {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
-
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
-       }
-
-       }
-       else if(imageType == VTK_INT)
-       {
-                       // pointers to get into the image
-       int* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(int*)imageData->GetScalarPointer(0,0,0);      
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
-
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
-       {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(int*)imageData->GetScalarPointer(i,j,k);                              
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_INT_MAX);                              
-
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
-
-                               counter ++;
-                       }
-               }
-       }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
-       {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
-
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
-       }
-
-       }
-       else if(imageType == VTK_UNSIGNED_INT)
-       {
-                       // pointers to get into the image
-       unsigned int* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(unsigned int*)imageData->GetScalarPointer(0,0,0);     
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
-
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
-       {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(unsigned int*)imageData->GetScalarPointer(i,j,k);                             
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_UNSIGNED_INT_MAX);                             
-
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
-
-                               counter ++;
-                       }
-               }
-       }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
-       {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
-
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
-       }
-
-       }
-       else if(imageType == VTK_LONG)
-       {
-                       // pointers to get into the image
-       long* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(long*)imageData->GetScalarPointer(0,0,0);     
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
-
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
-       {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(long*)imageData->GetScalarPointer(i,j,k);                             
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_LONG_MAX);                             
-
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
-
-                               counter ++;
-                       }
-               }
-       }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
-       {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
-
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
-       }
-
-       }
-       else if(imageType == VTK_UNSIGNED_LONG)
-       {
-                       // pointers to get into the image
-       unsigned long* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(unsigned long*)imageData->GetScalarPointer(0,0,0);    
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
-
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
-       {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(unsigned long*)imageData->GetScalarPointer(i,j,k);                            
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_UNSIGNED_LONG_MAX);                            
-
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
-
-                               counter ++;
-                       }
-               }
-       }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
-       {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
-
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
-       }
-
-       }
-       else if(imageType == VTK_FLOAT)
-       {               
-                       // pointers to get into the image
-       float* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(float*)imageData->GetScalarPointer(0,0,0);    
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
-
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
-       {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(float*)imageData->GetScalarPointer(i,j,k);                            
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_FLOAT_MAX);                            
-
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
-
-                               counter ++;
-                       }
-               }
-       }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
-       {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
-
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
-       }
-
-       }
-       else if(imageType == VTK_DOUBLE)
-       {
-               std::cout << "Got inside Double" << std::endl;
-                       // pointers to get into the image
-       double* dataImagePointer=NULL;
-       
-       // we start where the  image starts
-       dataImagePointer=(double*)imageData->GetScalarPointer(0,0,0);   
-       
-       /*
-        Image Size
-       */
-       int ext[6];
-       imageData->GetExtent(ext);
-       int sx,sy,sz;
-       sx=ext[1]-ext[0]+1;
-       sy=ext[3]-ext[2]+1;
-       sz=ext[5]-ext[4]+1;
-
-       //-----------------
-       //A3
-       //-----------------
-       //walking in the image
-       int i=0,j=0,k=0;
-       double sum1=0,sum2=0,sum3=0,sum4=0;     
-       for(i=0;i<sx;i++)
-       {
-               for(j=0;j<sy;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               
-                               // we get the pointer to the position (i,j,k)y that way we can get the position of the point in the surface
-                               dataImagePointer=(double*)imageData->GetScalarPointer(i,j,k);                           
-
-                               sum1=(int)(dataImagePointer[0]) + (int)(dataImagePointer[1]) + (int)(dataImagePointer[2]);
-                               sum1=sum1/(3*VTK_DOUBLE_MAX);                           
-
-                               surfacePoints->InsertPoint(counter, i, j, sum1*height);                         
-
-                               counter ++;
-                       }
-               }
-       }
-
-       //This cycle creates the cells of the surface
-       int n =0;       
-       for(i=0;i<sx-1;i++)
-       {
-               for(j=0;j<sy-1;j++)
-               {
-                       for(k=0;k<sz;k++)
-                       {
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+1);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-
-                               surfaceCells->InsertNextCell(3);
-                               surfaceCells->InsertCellPoint(n);
-                               surfaceCells->InsertCellPoint(n+sy+1);
-                               surfaceCells->InsertCellPoint(n+sy);
-
-                               if(j<sy-2)
-                               {
-                                       n++;
-                               }
-                               else
-                               {
-                                       n=n+2;
-                               }
-                               
-                       }
-               }
-       }
-
-       }       
-
-       vtkPolyData* surfaceData = vtkPolyData::New();
-       surfaceData->SetPolys(surfaceCells);
-       surfaceData->SetPoints(surfacePoints);
-       surfaceData->Update();
-
-       vtkPolyDataMapper* surfaceMapper = vtkPolyDataMapper::New();
-       surfaceMapper->SetInput(surfaceData);
-       surfaceMapper->Update();
-
-    surfaceResult->SetMapper(surfaceMapper);
-       surfaceResult->GetProperty()->SetOpacity(1.0);
-
-       if (color == "RED")
-       {
-               surfaceResult->GetProperty()->SetColor(1,0,0);
-       }
-       else if (color == "BLUE")
-       {
-               surfaceResult->GetProperty()->SetColor(0,0,1);
-       }
-       else if (color == "GREEN")
-       {
-               surfaceResult->GetProperty()->SetColor(0,1,0);
-       }
-       else
-       {
-               surfaceResult->GetProperty()->SetColor(1,1,1);
-       }
-}
 /*
 Returns the filtered image
 */
index ed7a826fac713f064158bfa06be05709e42ab4ab..20e0f8bd80082b41b6272f8aa1417598701983c3 100644 (file)
@@ -1,6 +1,8 @@
 #include "vtkImageData.h"
 #include "vtkProp3D.h"
 #include "vtkRenderer.h"
+#include "vtkPoints.h"
+#include "vtkCellArray.h"
 
 #include <string>
 
@@ -34,9 +36,14 @@ class Surface
                vtkActor* getSurface();
                
                /*
-               constructing image substract
+               constructing image surface
                */
                void surface(vtkImageData* imageData);
+
+               /*
+               Template for constructing the surface by image type
+               */
+               template <class T> void surfaceByType(T* dataImagePointer, vtkImageData* imageData, T max);
                
 
 
@@ -63,5 +70,15 @@ class Surface
                The type of image being viewed
                */
                int imageType;
+
+               /*
+               The points of the surface
+               */
+               vtkPoints* surfacePoints;
+
+               /*
+               Cells to be constructed from the surface
+               */
+               vtkCellArray* surfaceCells;
 };
 
index 8c34e09933102e2fc91fe74560ab54d84326fd10..e6e6c8b5ce8091c40d034173498d26ec8165a762 100644 (file)
@@ -15,10 +15,9 @@ Transformer::Transformer()
        //If the transform already exists, we delete it before we create a new transform 
        //and set the matrix with the identity matrix
        _transform= vtkTransform::New();
-       vtkMatrix4x4 *_matrix = vtkMatrix4x4::New();
+       _matrix = vtkMatrix4x4::New();
        _matrix->Identity();
-       _transform->SetMatrix(_matrix);
-       
+       _transform->SetMatrix(_matrix); 
 }
 
 /*
@@ -28,6 +27,7 @@ Transformer::~Transformer()
 {
        //We delete the existing transform
        if (_transform != NULL ) { _transform->Delete(); }
+       if (_matrix != NULL ) { _matrix->Delete(); }
 }
 
 /*
@@ -96,8 +96,8 @@ void Transformer::Run()
        _transform->Identity();
        _transform->Translate(_centerPoint[0], _centerPoint[1], _centerPoint[2]);
 
-       _transform->Scale(_scaleX, _scaleY,_scaleZ);
        _transform->RotateWXYZ(_angle, 0, 0, 1);
-       
+       _transform->Scale(_scaleX, _scaleY,_scaleZ);
+               
        _transform->Update();
 }
index 327eae60633513f9e75a2bec0fabcaefd58e279c..e7a40e044c2e1de4e18542867e411546111e4997 100644 (file)
@@ -27,6 +27,9 @@ private:
        double _scaleZ;
        //vtkImageData *_image;
        vtkTransform *_transform;
+
+       //
+       vtkMatrix4x4 *_matrix;
 };
 
 #endif
index eee092bbea2445c202824f154f929ae1d8d44386..1459e2da459d5d6dfb3cd9c4877af42e0bb81108 100644 (file)
@@ -138,20 +138,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                char* dataImagePointer2 = NULL;
                char* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (char*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (char*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (char*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (char*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (char*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (char*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (char)*dataImagePointer1*_percent + (char)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_SIGNED_CHAR)
        {
@@ -160,20 +147,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                signed char* dataImagePointer2 = NULL;
                signed char* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (signed char*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (signed char*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (signed char*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (signed char*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (signed char*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (signed char*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (signed char)*dataImagePointer1*_percent + (signed char)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_UNSIGNED_CHAR)
        {
@@ -182,20 +156,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                unsigned char* dataImagePointer2 = NULL;
                unsigned char* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (unsigned char*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (unsigned char*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (unsigned char*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (unsigned char*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (unsigned char*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (unsigned char*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (unsigned char)*dataImagePointer1*_percent + (unsigned char)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_SHORT)
        {
@@ -204,20 +165,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                short* dataImagePointer2 = NULL;
                short* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (short*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (short*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (short*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (short*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (short*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (short*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (short)*dataImagePointer1*_percent + (short)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_UNSIGNED_SHORT)
        {
@@ -226,20 +174,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                unsigned short* dataImagePointer2 = NULL;
                unsigned short* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (unsigned short*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (unsigned short*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (unsigned short*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (unsigned short*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (unsigned short*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (unsigned short*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (unsigned short)*dataImagePointer1*_percent + (unsigned short)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_INT)
        {
@@ -248,20 +183,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                int* dataImagePointer2 = NULL;
                int* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (int*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (int*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (int*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (int*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (int*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (int*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (int)*dataImagePointer1*_percent + (int)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_UNSIGNED_INT)
        {
@@ -270,20 +192,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                unsigned int* dataImagePointer2 = NULL;
                unsigned int* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (unsigned int*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (unsigned int*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (unsigned int*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (unsigned int*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (unsigned int*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (unsigned int*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (unsigned int)*dataImagePointer1*_percent + (unsigned int)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_LONG)
        {
@@ -292,20 +201,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                long* dataImagePointer2 = NULL;
                long* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (long*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (long*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (long*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (long*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (long*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (long*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (long)*dataImagePointer1*_percent + (long)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_UNSIGNED_LONG)
        {
@@ -314,20 +210,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                unsigned long* dataImagePointer2 = NULL;
                unsigned long* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (unsigned long*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (unsigned long*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (unsigned long*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (unsigned long*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (unsigned long*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (unsigned long*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (unsigned long)*dataImagePointer1*_percent + (unsigned long)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);              
        }
        else if(_type == VTK_FLOAT)
        {
@@ -336,20 +219,7 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                float* dataImagePointer2 = NULL;
                float* dataImageResultPointer = NULL;
        
-               dataImagePointer1 = (float*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (float*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (float*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
-               {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (float*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (float*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (float*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (float)*dataImagePointer1*_percent + (float)*dataImagePointer2*(1-_percent);
-                       }
-               }
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
        }
        else if(_type == VTK_DOUBLE)
        {
@@ -357,24 +227,32 @@ void Transparency::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX
                double* dataImagePointer1 = NULL;
                double* dataImagePointer2 = NULL;
                double* dataImageResultPointer = NULL;
+
+               createImageByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, img1, img2, sizeX, sizeY);
+       }
+
+       _newImage->Update();
+}
+
+template <class T>
+void Transparency::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
+{
+       int i, j;
        
-               dataImagePointer1 = (double*) img1->GetScalarPointer(0,0,0);
-               dataImagePointer2 = (double*) img2->GetScalarPointer(0,0,0);
-               dataImageResultPointer = (double*) _newImage->GetScalarPointer(0,0,0);
-               for(i = 0; i < sizeX; i++)
+       dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
+       dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
+       dataImageResultPointer = (T*) _newImage->GetScalarPointer(0,0,0);
+       for(i = 0; i < sizeX; i++)
+       {
+               for(j = 0; j < sizeY; j++)
                {
-                       for(j = 0; j < sizeY; j++)
-                       {
-                               dataImagePointer1 = (double*)img1->GetScalarPointer(i,j,0);
-                               dataImagePointer2 = (double*)img2->GetScalarPointer(i,j,0);
-                               dataImageResultPointer = (double*)_newImage->GetScalarPointer(i,j,0);
-                               
-                               *dataImageResultPointer = (double)*dataImagePointer1*_percent + (double)*dataImagePointer2*(1-_percent);                                
-                       }
+                       dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
+                       dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
+                       dataImageResultPointer = (T*)_newImage->GetScalarPointer(i,j,0);
+                       
+                       *dataImageResultPointer = (T)*dataImagePointer1*_percent + (T)*dataImagePointer2*(1-_percent);                          
                }
        }
-
-       _newImage->Update();
 }
 
 /*
index f75c4dfbf6e11baabe4cf55e5fccc6f32537f652..c28214fd14d95e4e356c79a378eaefb9d9344ef1 100644 (file)
@@ -26,9 +26,12 @@ class Transparency
                //Initialize newImage points
                void initialize(int dimensions[], double spacing[]);
 
-               //Constructs new transparent image
+               //Constructs new transparent image
                void createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY);
 
+               //Template for constructing the image by image type
+               template <class T> void createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY);
+
        // --- Atributes --- //
        private: