Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkLabeledImageToSurfaceFilter.h"
00019 #include <itksys/SystemTools.hxx>
00020 #include "mitkDataNodeFactory.h"
00021 #include "mitkReferenceCountWatcher.h"
00022
00023 #include <cmath>
00024
00025 bool equals(const mitk::ScalarType& val1, const mitk::ScalarType& val2, mitk::ScalarType epsilon = mitk::eps )
00026 {
00027 return ( std::fabs(val1 - val2) <= epsilon );
00028 }
00029
00030 int mitkLabeledImageToSurfaceFilterTest(int argc, char* argv[])
00031 {
00032 if(argc<2)
00033 {
00034 std::cout<<"no path to testing specified [FAILED]"<<std::endl;
00035 return EXIT_FAILURE;
00036 }
00037
00038 std::string fileIn = argv[1];
00039 std::cout<<"Eingabe Datei: "<<fileIn<<std::endl;
00040 mitk::Image::Pointer image = NULL;
00041 mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New();
00042 try
00043 {
00044 std::cout << "Loading file: ";
00045 factory->SetFileName( fileIn.c_str() );
00046 factory->Update();
00047
00048 if(factory->GetNumberOfOutputs()<1)
00049 {
00050 std::cout<<"file could not be loaded [FAILED]"<<std::endl;
00051 return EXIT_FAILURE;
00052 }
00053 mitk::DataNode::Pointer node = factory->GetOutput( 0 );
00054 image = dynamic_cast<mitk::Image*>(node->GetData());
00055 if(image.IsNull())
00056 {
00057 std::cout<<"file not an image - test will not be applied [PASSED]"<<std::endl;
00058 std::cout<<"[TEST DONE]"<<std::endl;
00059 return EXIT_SUCCESS;
00060 }
00061 else if(image->GetPixelType() != mitk::PixelType(typeid(char))
00062 || image->GetPixelType() != mitk::PixelType(typeid(unsigned char)) )
00063 {
00064 std::cout<<"file not a char or unsigned char image - test will not be applied [PASSED]"<<std::endl;
00065 std::cout<<"[TEST DONE]"<<std::endl;
00066 return EXIT_SUCCESS;
00067 }
00068 }
00069 catch ( itk::ExceptionObject & ex )
00070 {
00071 std::cout << "Exception: " << ex << "[FAILED]" << std::endl;
00072 return EXIT_FAILURE;
00073 }
00074
00075
00076 std::cout << "Testing instantiation: " ;
00077 mitk::LabeledImageToSurfaceFilter::Pointer filter = mitk::LabeledImageToSurfaceFilter::New();
00078 if (filter.IsNull())
00079 {
00080 std::cout<<"[FAILED]"<<std::endl;
00081 return EXIT_FAILURE;
00082 }
00083 else {
00084 std::cout<<"[PASSED]"<<std::endl;
00085 }
00086
00087 std::cout << "Create surface with default settings: ";
00088 filter->SetInput(image);
00089 filter->Update();
00090
00091 if ( filter->GetNumberOfOutputs() != 1 )
00092 {
00093 std::cout<<"Wrong number of outputs, [FAILED]"<<std::endl;
00094 return EXIT_FAILURE;
00095 }
00096 else if ( filter->GetOutput() == NULL )
00097 {
00098 std::cout<<"Output is NULL, [FAILED]"<<std::endl;
00099 return EXIT_FAILURE;
00100 }
00101 else if ( filter->GetOutput()->GetVtkPolyData() == NULL )
00102 {
00103 std::cout<<"PolyData of surface is NULL, [FAILED]"<<std::endl;
00104 return EXIT_FAILURE;
00105 }
00106 else
00107 {
00108 std::cout<<"[PASSED]"<<std::endl;
00109 }
00110
00111 std::cout << "Testing index to label conversion: ";
00112 if ( filter->GetLabelForNthOutput( 0 ) != 257 )
00113 {
00114 std::cout<<"[FAILED]"<<std::endl;
00115 return EXIT_FAILURE;
00116 }
00117 else
00118 {
00119 std::cout<<"[PASSED]"<<std::endl;
00120 }
00121
00122 std::cout << "Testing volume for label calculation: ";
00123 if ( ! equals ( filter->GetVolumeForLabel( 257 ) , 14.328 ) )
00124 {
00125 std::cout<<filter->GetVolumeForLabel( 257 )<<"[FAILED]"<<std::endl;
00126 return EXIT_FAILURE;
00127 }
00128 else
00129 {
00130 std::cout<<"[PASSED]"<<std::endl;
00131 }
00132
00133 std::cout << "Testing volume for index calculation: ";
00134 if ( ! equals ( filter->GetVolumeForNthOutput( 0 ) , 14.328 ) )
00135 {
00136 std::cout<<"[FAILED]"<<std::endl;
00137 return EXIT_FAILURE;
00138 }
00139 else
00140 {
00141 std::cout<<"[PASSED]"<<std::endl;
00142 }
00143
00144
00145 std::cout << "Create surface using optimised settings: ";
00146 filter->GenerateAllLabelsOn();
00147 filter->SetGaussianStandardDeviation( 1.5 );
00148 filter->SetSmooth(true);
00149 filter->SetDecimate( mitk::ImageToSurfaceFilter::DecimatePro );
00150 filter->SetTargetReduction( 0.8 );
00151 if( filter->GetNumberOfOutputs() != 1 )
00152 {
00153 std::cout<<"[FAILED]"<<std::endl;
00154 return EXIT_FAILURE;
00155 }
00156 else
00157 {
00158 std::cout<<"[PASSED]"<<std::endl;
00159 }
00160
00161 std::cout << "Create surface for label 257: ";
00162 filter->GenerateAllLabelsOff();
00163 filter->SetLabel(257);
00164 filter->Update();
00165 if ( filter->GetNumberOfOutputs() != 1 )
00166 {
00167 std::cout<<"Wrong number of outputs, [FAILED]"<<std::endl;
00168 return EXIT_FAILURE;
00169 }
00170 else if ( filter->GetOutput() == NULL )
00171 {
00172 std::cout<<"Output is NULL, [FAILED]"<<std::endl;
00173 return EXIT_FAILURE;
00174 }
00175 else if ( filter->GetOutput()->GetVtkPolyData() == NULL )
00176 {
00177 std::cout<<"PolyData of surface is NULL, [FAILED]"<<std::endl;
00178 return EXIT_FAILURE;
00179 }
00180 else
00181 {
00182 std::cout<<"[PASSED]"<<std::endl;
00183 }
00184
00185 std::cout << "Testing volume for label calculation: ";
00186 if ( ! equals ( filter->GetVolumeForLabel( 257 ) , 14.328 ) )
00187 {
00188 std::cout<<"[FAILED]"<<std::endl;
00189 return EXIT_FAILURE;
00190 }
00191 else
00192 {
00193 std::cout<<"[PASSED]"<<std::endl;
00194 }
00195
00196 std::cout << "Testing volume for index calculation: ";
00197 if ( ! equals ( filter->GetVolumeForNthOutput( 0 ) , 14.328 ) )
00198 {
00199 std::cout<<"[FAILED]"<<std::endl;
00200 return EXIT_FAILURE;
00201 }
00202 else
00203 {
00204 std::cout<<"[PASSED]"<<std::endl;
00205 }
00206
00207 std::cout << "Create surface for multiple labels: ";
00208 filter->GenerateAllLabelsOn();
00209 filter->SetBackgroundLabel(32000);
00210 filter->Update();
00211 if ( filter->GetNumberOfOutputs() != 2 )
00212 {
00213 std::cout<<"Wrong number of outputs, [FAILED]"<<std::endl;
00214 return EXIT_FAILURE;
00215 }
00216 else if ( filter->GetOutput(0) == NULL )
00217 {
00218 std::cout<<"Output 0 is NULL, [FAILED]"<<std::endl;
00219 return EXIT_FAILURE;
00220 }
00221 else if ( filter->GetOutput(0)->GetVtkPolyData() == NULL )
00222 {
00223 std::cout<<"PolyData of output 0 is NULL, [FAILED]"<<std::endl;
00224 return EXIT_FAILURE;
00225 }
00226 else if ( filter->GetOutput(1) == NULL )
00227 {
00228 std::cout<<"Output 1 is NULL, [FAILED]"<<std::endl;
00229 return EXIT_FAILURE;
00230 }
00231 else if ( filter->GetOutput(1)->GetVtkPolyData() == NULL )
00232 {
00233 std::cout<<"PolyData of output 1 is NULL, [FAILED]"<<std::endl;
00234 return EXIT_FAILURE;
00235 }
00236 else
00237 {
00238 std::cout<<"[PASSED]"<<std::endl;
00239 }
00240
00241 std::cout << "Testing volume for label calculation: ";
00242 if ( ! equals ( filter->GetVolumeForLabel( 257 ) , 14.328 ) )
00243 {
00244 std::cout<<"[FAILED]"<<std::endl;
00245 return EXIT_FAILURE;
00246 }
00247 else if ( ! equals ( filter->GetVolumeForLabel( 0 ), 12.672 ) )
00248 {
00249 std::cout<<"[FAILED]"<<std::endl;
00250 return EXIT_FAILURE;
00251 }
00252 else
00253 {
00254 std::cout<<"[PASSED]"<<std::endl;
00255 }
00256
00257 std::cout << "Testing volume for index calculation: ";
00258 if ( ! equals( filter->GetVolumeForNthOutput( 1 ), 14.328 ) )
00259 {
00260 std::cout<<"[FAILED]"<<std::endl;
00261 return EXIT_FAILURE;
00262 }
00263 else if ( ! equals ( filter->GetVolumeForNthOutput( 0 ), 12.672 ) )
00264 {
00265 std::cout<<"[FAILED]"<<std::endl;
00266 return EXIT_FAILURE;
00267 }
00268 else
00269 {
00270 std::cout<<"[PASSED]"<<std::endl;
00271 }
00272
00273 mitk::ReferenceCountWatcher::Pointer outputSurface1Watcher = new mitk::ReferenceCountWatcher(filter->GetOutput(1), "outputSurface1");
00274 mitk::ReferenceCountWatcher::Pointer filterWatcher = new mitk::ReferenceCountWatcher(filter, "filter");
00275
00276 std::cout << "Create surface for background (label 0): " << std::flush;
00277 filter->GenerateAllLabelsOff();
00278 filter->SetLabel(0);
00279 filter->SetBackgroundLabel(257);
00280
00281
00282 filter->Update();
00283
00284
00285 if ( filter->GetNumberOfOutputs() != 1 )
00286 {
00287 std::cout<<"Wrong number of outputs, [FAILED]"<<std::endl;
00288 return EXIT_FAILURE;
00289 }
00290 else if ( filter->GetOutput() == NULL )
00291 {
00292 std::cout<<"Output is NULL, [FAILED]"<<std::endl;
00293 return EXIT_FAILURE;
00294 }
00295 else if ( filter->GetOutput()->GetVtkPolyData() == NULL )
00296 {
00297 std::cout<<"PolyData of surface is NULL, [FAILED]"<<std::endl;
00298 return EXIT_FAILURE;
00299 }
00300 else
00301 {
00302 std::cout<<"[PASSED]"<<std::endl;
00303 }
00304 std::cout << "Testing reference count correctness of old output 1: " << std::flush;
00305 if ( outputSurface1Watcher->GetReferenceCount() != 0 )
00306 {
00307 std::cout<<"outputSurface1Watcher->GetReferenceCount()=="
00308 << outputSurface1Watcher->GetReferenceCount()
00309 << "!=0, [FAILED]" << std::endl;
00310 return EXIT_FAILURE;
00311 }
00312 std::cout<<"[PASSED]"<<std::endl;
00313 std::cout << "Testing reference count correctness of filter: " << std::flush;
00314 if ( filterWatcher->GetReferenceCount() != 2 )
00315 {
00316 std::cout<<"filterWatcher->GetReferenceCount()=="
00317 << outputSurface1Watcher->GetReferenceCount()
00318 << "!=2, [FAILED]" << std::endl;
00319 return EXIT_FAILURE;
00320 }
00321 std::cout<<"[PASSED]"<<std::endl;
00322
00323 std::cout << "Testing index to label conversion: ";
00324 if ( filter->GetLabelForNthOutput( 0 ) != 0 )
00325 {
00326 std::cout<<"[FAILED]"<<std::endl;
00327 return EXIT_FAILURE;
00328 }
00329 else
00330 {
00331 std::cout<<"[PASSED]"<<std::endl;
00332 }
00333
00334 std::cout << "Testing volume for label calculation: ";
00335 if ( ! equals ( filter->GetVolumeForLabel( filter->GetLabel() ) , 12.672 ) )
00336 {
00337 std::cout<<"[FAILED]"<<std::endl;
00338 return EXIT_FAILURE;
00339 }
00340 else
00341 {
00342 std::cout<<"[PASSED]"<<std::endl;
00343 }
00344
00345 std::cout << "Testing volume for index calculation: ";
00346 if ( ! equals( filter->GetVolumeForNthOutput( 0 ), 12.672 ) )
00347 {
00348 std::cout<<"[FAILED]"<<std::endl;
00349 return EXIT_FAILURE;
00350 }
00351 else
00352 {
00353 std::cout<<"[PASSED]"<<std::endl;
00354 }
00355
00356 std::cout << "Create surface for invalid label: ";
00357 filter->GenerateAllLabelsOff();
00358 filter->SetLabel(1);
00359 filter->Update();
00360 if ( filter->GetNumberOfOutputs() != 1 )
00361 {
00362 std::cout<<"Number of outputs != 1, [FAILED]"<<std::endl;
00363 return EXIT_FAILURE;
00364 }
00365 else if ( filter->GetOutput()->GetVtkPolyData()->GetNumberOfPoints() != 0 )
00366 {
00367 std::cout<<"PolyData is not empty ("<<filter->GetOutput()->GetVtkPolyData()->GetNumberOfPoints()<<"), [FAILED]"<<std::endl;
00368 return EXIT_FAILURE;
00369 }
00370 else
00371 {
00372 std::cout<<"[PASSED]"<<std::endl;
00373 }
00374
00375
00376
00377 std::cout<<"[TEST DONE]"<<std::endl;
00378 return EXIT_SUCCESS;
00379
00380 }