00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <mitkConfig.h>
00019 #include <mitkDataNodeFactory.h>
00020 #include <mitkBaseDataIOFactory.h>
00021 #include <mitkCoreObjectFactory.h>
00022 #include <mitkITKImageImport.h>
00023
00024
00025 #include <stdlib.h>
00026 #include <locale.h>
00027
00028
00029 #include <vector>
00030 #include <map>
00031 #include <istream>
00032 #include <cstdlib>
00033 #include <locale>
00034
00035
00036
00037 #include <vtkSTLReader.h>
00038 #include <vtkOBJReader.h>
00039 #include <vtkPolyData.h>
00040 #include <vtkPolyDataNormals.h>
00041
00042 #include <vtkDataReader.h>
00043 #include <vtkPolyDataReader.h>
00044 #include <vtkStructuredPointsReader.h>
00045
00046 #include <vtkStructuredPoints.h>
00047 #include <vtkLookupTable.h>
00048 #include <vtkPointData.h>
00049 #if ((VTK_MAJOR_VERSION > 4) || ((VTK_MAJOR_VERSION==4) && (VTK_MINOR_VERSION>=4) ))
00050 #include <vtkXMLImageDataReader.h>
00051 #endif
00052
00053
00054 #include <itksys/SystemTools.hxx>
00055 #include <itksys/Directory.hxx>
00056 #include <itkImage.h>
00057 #include <itkImageSeriesReader.h>
00058 #include <itkImageFileReader.h>
00059 #include <itkImageIOFactory.h>
00060 #include <itkImageIORegion.h>
00061 #include <itkImageSeriesReader.h>
00062 #include <itkDICOMImageIO2.h>
00063 #include <itkDICOMSeriesFileNames.h>
00064 #include <itkGDCMImageIO.h>
00065 #include <itkGDCMSeriesFileNames.h>
00066 #include <itkNumericSeriesFileNames.h>
00067 #include <itkCommand.h>
00068
00069
00070 #include "mitkSurface.h"
00071 #include "mitkPointSet.h"
00072 #include "mitkStringProperty.h"
00073 #include "mitkProperties.h"
00074
00075 #include "mitkLevelWindowProperty.h"
00076 #include "mitkVtkRepresentationProperty.h"
00077 #include "mitkVtkInterpolationProperty.h"
00078 #include "mitkVtkScalarModeProperty.h"
00079 #include "mitkImage.h"
00080 #include "mitkLookupTableProperty.h"
00081 #include "mitkLookupTable.h"
00082 #include "mitkImageChannelSelector.h"
00083 #include "mitkImageSliceSelector.h"
00084 #include "mitkCoreObjectFactory.h"
00085 #include "mitkTransferFunctionProperty.h"
00086 #include "mitkVtkResliceInterpolationProperty.h"
00087 #include "mitkProgressBar.h"
00088 #include <mitkDicomSeriesReader.h>
00089
00090 bool mitk::DataNodeFactory::m_TextureInterpolationActive = false;
00091
00092 mitk::DataNodeFactory::DataNodeFactory()
00093 : m_UseSeriesDetails(true)
00094 {
00095 m_Serie = false;
00096 m_OldProgress = 0;
00097 this->Modified();
00098
00099 mitk::CoreObjectFactory::GetInstance();
00100 }
00101
00102 mitk::DataNodeFactory::~DataNodeFactory()
00103 {}
00104
00105 void mitk::DataNodeFactory::SetImageSerie(bool serie)
00106 {
00107 m_Serie = serie;
00108 }
00109
00110 void mitk::DataNodeFactory::GenerateData()
00111 {
00112
00113
00114
00115 std::ifstream exists(m_FileName.c_str());
00116 if (!exists)
00117 {
00118 std::string testfilename = m_FileName + ".gz";
00119
00120 std::ifstream exists(testfilename.c_str());
00121 if (exists.good())
00122 {
00123 m_FileName += ".gz";
00124 }
00125 else
00126 {
00127 testfilename = m_FileName + ".GZ";
00128 std::ifstream exists(testfilename.c_str());
00129 if (exists.good())
00130 {
00131 m_FileName += ".GZ";
00132 }
00133 else
00134 {
00135 std::string message("File does not exist, or cannot be read. Filename = ");
00136 message += m_FileName;
00137 MITK_ERROR << message;
00138 itkExceptionMacro( << message );
00139 }
00140 }
00141 }
00142
00143
00144
00145
00146
00147
00148 if (DicomSeriesReader::IsDicom(this->m_FileName) )
00149 {
00150 this->ReadFileSeriesTypeDCM();
00151 }
00152 else
00153 {
00154 bool usedNewDTNF = false;
00155
00156
00157 std::vector<mitk::BaseData::Pointer> baseDataVector = mitk::BaseDataIO::LoadBaseDataFromFile( m_FileName, m_FilePrefix, m_FilePattern, m_Serie );
00158
00159 if( !baseDataVector.empty() )
00160 this->ResizeOutputs((unsigned int)baseDataVector.size());
00161
00162 for(int i=0; i<(int)baseDataVector.size(); i++)
00163 {
00164 mitk::BaseData::Pointer baseData = baseDataVector.at(i);
00165
00166 if( baseData.IsNotNull() )
00167 {
00168 usedNewDTNF = true;
00169 mitk::DataNode::Pointer node = mitk::DataNode::New();
00170 node->SetData(baseData);
00171 this->SetDefaultCommonProperties( node );
00172
00173 this->SetOutput(i, node);
00174 }
00175 }
00176 if(!usedNewDTNF && !( m_FileName != "" ) && !(m_Serie == false))
00177 ReadFileSeriesTypeITKImageSeriesReader();
00178 }
00179 }
00180
00181 void mitk::DataNodeFactory::ResizeOutputs( const unsigned int& num )
00182 {
00183 unsigned int prevNum = this->GetNumberOfOutputs();
00184 this->SetNumberOfOutputs( num );
00185 for ( unsigned int i = prevNum; i < num; ++i )
00186 {
00187 this->SetNthOutput( i, this->MakeOutput( i ).GetPointer() );
00188 }
00189 }
00190
00191 bool mitk::DataNodeFactory::FileNameEndsWith( const std::string& name )
00192 {
00193 if (m_FileName.size() < name.size()) return false;
00194
00195 return m_FileName.substr(m_FileName.size() - name.size()) == name;
00196 }
00197
00198 bool mitk::DataNodeFactory::FilePatternEndsWith( const std::string& name )
00199 {
00200 return m_FilePattern.find( name ) != std::string::npos;
00201 }
00202
00203 std::string mitk::DataNodeFactory::GetBaseFileName()
00204 {
00205 return itksys::SystemTools::GetFilenameName( m_FileName );
00206 }
00207
00208 std::string mitk::DataNodeFactory::GetBaseFilePrefix()
00209 {
00210 return itksys::SystemTools::GetFilenameName( m_FilePrefix );
00211 }
00212
00213 std::string mitk::DataNodeFactory::GetDirectory()
00214 {
00215 if ( m_FileName != "" )
00216 return itksys::SystemTools::GetFilenamePath( m_FileName );
00217 else if ( m_FilePrefix != "" )
00218 return itksys::SystemTools::GetFilenamePath( m_FilePrefix );
00219 else
00220 return std::string( "" );
00221 }
00222
00223 void mitk::DataNodeFactory::ReadFileSeriesTypeDCM()
00224 {
00225 MITK_INFO << "loading image series with prefix " << m_FilePrefix << " and pattern " << m_FilePattern << " as DICOM..." << std::endl;
00226 const char* previousCLocale = setlocale(LC_NUMERIC, NULL);
00227 setlocale(LC_NUMERIC, "C");
00228 std::locale previousCppLocale( std::cin.getloc() );
00229 std::locale l( "C" );
00230 std::cin.imbue(l);
00231
00232 DicomSeriesReader::UidFileNamesMap names_map = DicomSeriesReader::GetSeries(this->GetDirectory(), this->m_UseSeriesDetails, this->m_SeriesRestrictions);
00233 const unsigned int size = names_map.size();
00234
00235 this->ResizeOutputs(size);
00236 ProgressBar::GetInstance()->AddStepsToDo(size);
00237 ProgressBar::GetInstance()->Progress();
00238
00239 unsigned int i = 0u;
00240 const DicomSeriesReader::UidFileNamesMap::const_iterator n_end = names_map.end();
00241
00242 for (DicomSeriesReader::UidFileNamesMap::const_iterator n_it = names_map.begin(); n_it != n_end; ++n_it)
00243 {
00244 const std::string &uid = n_it->first;
00245 DataNode::Pointer node = this->GetOutput(i);
00246
00247 MITK_INFO << "Reading series #" << i << ": " << uid << std::endl;
00248
00249 if (DicomSeriesReader::LoadDicomSeries(n_it->second, *node))
00250 {
00251 ++i;
00252 node->SetName(uid);
00253 }
00254 else
00255 {
00256 MITK_ERROR << "skipping series #" << i << " due to exception" << std::endl;
00257 }
00258
00259 ProgressBar::GetInstance()->Progress();
00260 }
00261
00262 setlocale(LC_NUMERIC, previousCLocale);
00263 std::cin.imbue(previousCppLocale);
00264 }
00265
00266
00267
00268 void mitk::DataNodeFactory::ReadFileSeriesTypeITKImageSeriesReader()
00269 {
00270 typedef itk::Image<int, 3> ImageType;
00271 typedef itk::ImageSeriesReader< ImageType > ReaderType;
00272 typedef itk::NumericSeriesFileNames NameGenerator;
00273
00274 if ( ! this->GenerateFileList() )
00275 {
00276 itkWarningMacro( "Sorry, file list could not be generated!" );
00277 return ;
00278 }
00279 if ( m_MatchedFileNames.size() == 0 )
00280 {
00281 itkWarningMacro( "Sorry, no files matched the given filename ("<< m_FileName <<")!" );
00282 return ;
00283 }
00284
00285
00286
00287
00288 ReaderType::Pointer reader = ReaderType::New();
00289 reader->SetFileNames( m_MatchedFileNames );
00290 try
00291 {
00292 reader->Update();
00293 ResizeOutputs( reader->GetNumberOfOutputs() );
00294 for ( unsigned int i = 0; i < reader->GetNumberOfOutputs(); ++i )
00295 {
00296
00297 mitk::Image::Pointer image = mitk::Image::New();
00298 image->InitializeByItk( reader->GetOutput( i ) );
00299 image->SetVolume( reader->GetOutput( i )->GetBufferPointer() );
00300
00301
00302 mitk::DataNode::Pointer node = this->GetOutput( i );
00303 node->SetData( image );
00304
00305 mitk::StringProperty::Pointer nameProp = mitk::StringProperty::New( m_FileName );
00306 node->SetProperty( "name", nameProp );
00307 }
00308 }
00309 catch ( const std::exception & e )
00310 {
00311 itkWarningMacro( << e.what() );
00312 return ;
00313 }
00314 }
00315
00316
00317
00318 mitk::ColorProperty::Pointer mitk::DataNodeFactory::DefaultColorForOrgan( const std::string& organ )
00319 {
00320 static bool initialized = false;
00321 static std::map< std::string, std::string > s_ColorMap;
00322
00323 if (!initialized)
00324 {
00325
00326
00327 s_ColorMap.insert( std::make_pair( "ankle", "0xe38686") );
00328 s_ColorMap.insert( std::make_pair( "appendix", "0xe38686") );
00329 s_ColorMap.insert( std::make_pair( "blood vessels", "0xff3131") );
00330 s_ColorMap.insert( std::make_pair( "bronchial tree", "0x3168ff") );
00331 s_ColorMap.insert( std::make_pair( "bone", "0xd5d5d5") );
00332 s_ColorMap.insert( std::make_pair( "brain", "0xff9cca") );
00333 s_ColorMap.insert( std::make_pair( "coccyx", "0xe38686") );
00334 s_ColorMap.insert( std::make_pair( "colon", "0xe38686") );
00335 s_ColorMap.insert( std::make_pair( "cyst", "0xe38686") );
00336 s_ColorMap.insert( std::make_pair( "elbow", "0xe38686") );
00337 s_ColorMap.insert( std::make_pair( "eye", "0xe38686") );
00338 s_ColorMap.insert( std::make_pair( "fallopian tube", "0xe38686") );
00339 s_ColorMap.insert( std::make_pair( "fat", "0xff2bee") );
00340 s_ColorMap.insert( std::make_pair( "hand", "0xe38686") );
00341 s_ColorMap.insert( std::make_pair( "gall bladder", "0x567f18") );
00342 s_ColorMap.insert( std::make_pair( "heart", "0xeb1d32") );
00343 s_ColorMap.insert( std::make_pair( "hip", "0xe38686") );
00344 s_ColorMap.insert( std::make_pair( "kidney", "0xd33f00") );
00345 s_ColorMap.insert( std::make_pair( "knee", "0xe38686") );
00346 s_ColorMap.insert( std::make_pair( "larynx", "0xe38686") );
00347 s_ColorMap.insert( std::make_pair( "liver", "0xffcc3d") );
00348 s_ColorMap.insert( std::make_pair( "lung", "0x6bdcff") );
00349 s_ColorMap.insert( std::make_pair( "lymph node", "0xff0000") );
00350 s_ColorMap.insert( std::make_pair( "muscle", "0xff456a") );
00351 s_ColorMap.insert( std::make_pair( "nerve", "0xffea4f") );
00352 s_ColorMap.insert( std::make_pair( "nose", "0xe38686") );
00353 s_ColorMap.insert( std::make_pair( "oesophagus", "0xe38686") );
00354 s_ColorMap.insert( std::make_pair( "ovaries", "0xe38686") );
00355 s_ColorMap.insert( std::make_pair( "pancreas", "0xf9ab3d") );
00356 s_ColorMap.insert( std::make_pair( "pelvis", "0xe38686") );
00357 s_ColorMap.insert( std::make_pair( "penis", "0xe38686") );
00358 s_ColorMap.insert( std::make_pair( "pharynx", "0xe38686") );
00359 s_ColorMap.insert( std::make_pair( "prostate", "0xe38686") );
00360 s_ColorMap.insert( std::make_pair( "rectum", "0xe38686") );
00361 s_ColorMap.insert( std::make_pair( "sacrum", "0xe38686") );
00362 s_ColorMap.insert( std::make_pair( "seminal vesicle", "0xe38686") );
00363 s_ColorMap.insert( std::make_pair( "shoulder", "0xe38686") );
00364 s_ColorMap.insert( std::make_pair( "spinal cord", "0xf5f93d") );
00365 s_ColorMap.insert( std::make_pair( "spleen", "0xf96c3d") );
00366 s_ColorMap.insert( std::make_pair( "stomach", "0xf96c3d") );
00367 s_ColorMap.insert( std::make_pair( "teeth", "0xfffcd8") );
00368 s_ColorMap.insert( std::make_pair( "testicles", "0xe38686") );
00369 s_ColorMap.insert( std::make_pair( "thyroid", "0xfff694") );
00370 s_ColorMap.insert( std::make_pair( "tongue", "0xe38686") );
00371 s_ColorMap.insert( std::make_pair( "tumor", "0x937011") );
00372 s_ColorMap.insert( std::make_pair( "urethra", "0xf8ff32") );
00373 s_ColorMap.insert( std::make_pair( "urinary bladder", "0xf8ff32") );
00374 s_ColorMap.insert( std::make_pair( "uterus", "0xe38686") );
00375 s_ColorMap.insert( std::make_pair( "vagina", "0xe38686") );
00376 s_ColorMap.insert( std::make_pair( "vertebra", "0xe38686") );
00377 s_ColorMap.insert( std::make_pair( "wrist", "0xe38686") );
00378 initialized = true;
00379 }
00380
00381 std::string lowercaseOrgan(organ);
00382 for(unsigned int i = 0; i < organ.length(); i++)
00383 {
00384 lowercaseOrgan[i] = tolower(lowercaseOrgan[i]);
00385 }
00386
00387 std::map< std::string, std::string >::iterator iter = s_ColorMap.find( lowercaseOrgan );
00388 if ( iter != s_ColorMap.end() )
00389 {
00390 std::string hexColor = iter->second;
00391 std::string hexRed = std::string("0x") + hexColor.substr( 2, 2 );
00392 std::string hexGreen = std::string("0x") + hexColor.substr( 4, 2 );
00393 std::string hexBlue = std::string("0x") + hexColor.substr( 6, 2 );
00394
00395 long int red = strtol( hexRed.c_str(), NULL, 16 );
00396 long int green = strtol( hexGreen.c_str(), NULL, 16 );
00397 long int blue = strtol( hexBlue.c_str(), NULL, 16 );
00398
00399 return ColorProperty::New( (float)red/ 255.0, (float)green/ 255.0, (float)blue/ 255.0 );
00400 }
00401 else
00402 {
00403
00404 return ColorProperty::New( 0.0, 1.0, 0.0 );
00405 }
00406
00407 }
00408
00409 void mitk::DataNodeFactory::SetDefaultCommonProperties(mitk::DataNode::Pointer &node)
00410 {
00411
00412 mitk::StringProperty::Pointer pathProp = mitk::StringProperty::New( itksys::SystemTools::GetFilenamePath( m_FileName ) );
00413 node->SetProperty( StringProperty::PATH, pathProp );
00414
00415
00416
00417 mitk::StringProperty::Pointer nameProp = dynamic_cast<mitk::StringProperty*>(node->GetProperty("name"));
00418 if(nameProp.IsNull() || (strcmp(nameProp->GetValue(),"No Name!")==0))
00419 {
00420
00421 mitk::StringProperty::Pointer baseDataNameProp = dynamic_cast<mitk::StringProperty*>(node->GetData()->GetProperty("name").GetPointer() );
00422 if(baseDataNameProp.IsNull() || (strcmp(baseDataNameProp->GetValue(),"No Name!")==0))
00423 {
00424
00425 if (FileNameEndsWith( ".gz" ))
00426 m_FileName = m_FileName.substr( 0, m_FileName.length()-3 );
00427
00428 nameProp = mitk::StringProperty::New( itksys::SystemTools::GetFilenameWithoutLastExtension( m_FileName ) );
00429
00430 node->SetProperty( "name", nameProp );
00431 }
00432 else
00433 {
00434
00435 nameProp = mitk::StringProperty::New( baseDataNameProp->GetValue() );
00436 node->SetProperty( "name", nameProp );
00437 }
00438 }
00439
00440
00441 if(!node->GetProperty("visible"))
00442 node->SetVisibility(true);
00443 }
00444