16 #pragma warning (disable : 4996)
19 #include <vtkXMLDataElement.h>
32 const std::string
DATA =
"data";
33 const std::string
ITEM =
"item";
35 const std::string
NAME =
"name";
36 const std::string
ID =
"id";
38 const std::string
LINK =
"link";
41 static std::string
GetName(std::string fileName,std::string suffix)
43 fileName = QFileInfo(QString::fromStdString(fileName)).fileName().toStdString();
44 return fileName.substr(0,fileName.length() -suffix.length()-9);
47 static std::string
GetDate(std::string fileName,std::string suffix)
49 fileName = QFileInfo(QString::fromStdString(fileName)).fileName().toStdString();
50 fileName = fileName.substr(fileName.length() - suffix.length()-8,8);
51 fileName.insert(6,
"-");
52 fileName.insert(4,
"-");
59 m_SubCollection(NULL),
60 m_DataItemCollection(NULL),
61 m_ColIgnore(false), m_ItemIgnore(false)
76 QDir fileName = QFileInfo(xmlFileName.c_str()).absoluteDir();
77 m_BaseDir = fileName.path().toStdString() + QDir::separator().toLatin1();
78 this->SetFileName(xmlFileName.c_str());
80 if (m_Collection.IsNotNull())
81 m_Collection->SetXMLFile(xmlFileName);
87 m_SelectedDataItemIds.insert( m_SelectedDataItemIds.end(), dataElemetIds.begin(), dataElemetIds.end() );
92 m_SelectedSubColIds.insert( m_SelectedSubColIds.end(), subColIds.begin(), subColIds.end() );
97 m_SelectedDataItemNames = itemNames;
102 m_SelectedDataItemIds.clear();
107 m_SelectedSubColIds.clear();
112 m_DataItemCollection = NULL;
113 m_SubCollection = NULL;
121 FileListType fileList = SanitizeFileList(GenerateFileLists(folder, suffixes, allowGaps));
123 if (fileList.size() <= 0)
127 collection->SetName(
GetName(fileList.at(0).at(0),suffixes.at(0)));
129 for (
unsigned int k=0; k < fileList.at(0).size(); ++k)
132 for (
unsigned int i=0; i< suffixes.size(); ++i)
134 std::string fileName = fileList.at(i).at(k);
135 if (fileName.find(
".fib") >= fileName.length())
138 subCollection->AddData(image.GetPointer(),seriesNames.at(i), fileList.at(i).at(k));
142 const std::string s1=
"", s2=
"";
145 if( fiber_infile.empty() )
147 MITK_INFO <<
"Fiber bundle could not be read " << fileName ;
150 subCollection->AddData(fiber_baseData,seriesNames.at(i), fileList.at(i).at(k));
153 std::string sDate =
GetDate(fileList.at(0).at(k),suffixes.at(0));
154 collection->AddData(subCollection.GetPointer(),sDate,
"--");
161 std::string name(elementName);
166 std::string colName = ReadXMLStringAttribut(
NAME, atts);
167 m_Collection->SetName(colName);
172 m_ItemIgnore =
false;
174 std::string subColName = ReadXMLStringAttribut(
NAME, atts);
175 std::string subColId = ReadXMLStringAttribut(
ID, atts);
177 if (m_SelectedSubColIds.size() > 0 && std::find(m_SelectedSubColIds.begin(), m_SelectedSubColIds.end(), subColId) == m_SelectedSubColIds.end() )
185 m_SubCollection->Init(subColName);
187 else if (name ==
DATA)
192 std::string dataId = ReadXMLStringAttribut(
ID, atts);
193 if (m_SelectedDataItemIds.size() > 0 && std::find(m_SelectedDataItemIds.begin(), m_SelectedDataItemIds.end(), dataId) == m_SelectedDataItemIds.end() )
198 m_ItemIgnore =
false;
199 std::string dataName = ReadXMLStringAttribut(
NAME, atts);
202 m_DataItemCollection->Init(dataName);
204 else if (name ==
ITEM)
206 if (m_ColIgnore || m_ItemIgnore)
210 std::string relativeItemLink = ReadXMLStringAttribut(
LINK, atts);
211 std::string itemLink = m_BaseDir + relativeItemLink;
212 std::string itemName = ReadXMLStringAttribut(
NAME, atts);
215 if (m_SelectedDataItemNames.size() != 0 && std::find(m_SelectedDataItemNames.begin(), m_SelectedDataItemNames.end(), itemName) == m_SelectedDataItemNames.end() )
219 if (itemLink.find(
".fib") >= itemLink.length())
222 if (image.IsNotNull())
223 m_DataItemCollection->AddData(image.GetPointer(),itemName,relativeItemLink);
225 MITK_ERROR <<
"File could not be loaded: " << itemLink <<
". Wihtin Sub-Collection " << m_SubCollection->GetName() <<
", within " << m_DataItemCollection->GetName() ;
229 const std::string s1=
"", s2=
"";
232 if( fiber_infile.empty() )
234 MITK_INFO <<
"Fiber bundle could not be read " << itemLink ;
237 m_DataItemCollection->AddData(fiber_baseData,itemName, relativeItemLink);
241 MITK_WARN<<
"Malformed description ? -- unknown tag: " << name;
246 std::string name(elementName);
249 if (m_SubCollection.IsNull())
251 if (m_ColIgnore || m_SubCollection->Size() == 0)
254 m_Collection->AddData(m_SubCollection.GetPointer(),m_SubCollection->GetName());
259 if (m_DataItemCollection.IsNull())
261 if (m_DataItemCollection->Size() == 0)
264 m_SubCollection->AddData(m_DataItemCollection.GetPointer(),m_DataItemCollection->GetName());
269 std::string mitk::CollectionReader::ReadXMLStringAttribut(std::string name,
const char** atts)
273 const char** attsIter = atts;
277 if (name == *attsIter)
286 return std::string();
289 bool mitk::CollectionReader::ReadXMLBooleanAttribut(std::string name,
const char** atts)
291 std::string s = ReadXMLStringAttribut(name, atts);
292 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
300 int mitk::CollectionReader::ReadXMLIntegerAttribut(std::string name,
const char** atts)
302 std::string s = ReadXMLStringAttribut(name, atts);
303 return atoi(s.c_str());
311 QString qFolder = QString::fromStdString(folder);
312 if (!QFileInfo(qFolder).isDir())
318 for (
unsigned int i=0; i< suffixes.size(); ++i)
320 std::vector<std::string> list;
321 fileList.push_back(list);
330 parseDir.setFilter(QDir::Files);
331 parseDir.setPath(qFolder);
332 QStringList filterMorph;
333 filterMorph <<
"*" + QString::fromStdString( suffixes.at(0) );
334 parseDir.setNameFilters( filterMorph );
336 QFileInfoList qFileList = parseDir.entryInfoList();
339 for (
int i = 0; i < qFileList.size(); ++i)
341 std::string baseFileName = qFileList.at(i).absoluteFilePath().toStdString();
342 fileList.at(0).push_back( baseFileName );
345 for (
unsigned int suffNo=1; suffNo < suffixes.size(); ++suffNo)
347 std::string derivedFileName = baseFileName.substr(0,baseFileName.length() -suffixes.at(0).length()) + suffixes.at(suffNo);
350 if (QFileInfo(QString::fromStdString(derivedFileName)).isFile())
351 fileList.at(suffNo).push_back(derivedFileName);
353 fileList.at(suffNo).push_back(
"");
359 int numberOfFiles=-1;
360 for (
unsigned int i=0; i< suffixes.size(); ++i)
363 parseDir.setFilter(QDir::Files);
364 parseDir.setPath(qFolder);
365 QStringList filterMorph;
366 filterMorph <<
"*" + QString::fromStdString( suffixes.at(i) );
367 parseDir.setNameFilters( filterMorph );
369 QFileInfoList qFileList = parseDir.entryInfoList();
370 if (numberOfFiles == -1)
371 numberOfFiles = qFileList.size();
373 if (numberOfFiles != qFileList.size() )
375 MITK_ERROR <<
"Series contain different number of images. Loading aborting.";
380 for (
int fileNo=0; fileNo<qFileList.size(); ++fileNo)
382 fileList.at(i).push_back(qFileList.at(fileNo).absoluteFilePath().toStdString());
391 std::vector<int> indexRemoval;
394 int modalities = list.size();
395 int timeSteps = list.at(0).size();
396 MITK_INFO <<
"Modalities " << modalities;
400 for (
int listIndex = 0 ; listIndex < timeSteps; listIndex++)
402 for (
int modalityIndex = 0 ; modalityIndex < modalities; modalityIndex++)
404 if (list.at(modalityIndex).at(listIndex) ==
"")
406 MITK_INFO <<
"Marked Index " << listIndex <<
" for removal.";
407 indexRemoval.push_back(listIndex);
414 for (
int listIndex = indexRemoval.size()-1 ; listIndex >= 0; --listIndex)
416 for (
int i = 0 ; i < modalities; i++)
418 list.at(i).erase(list.at(i).begin()+indexRemoval.at(listIndex)) ;
421 MITK_INFO <<
"Time Steps after sanitizing: " << list.at(0).size();
DataCollection::Pointer LoadCollection(const std::string &xmlFileName)
Build up a mitk::DataCollection from a XML resource.
void ClearDataElementIds()
void SetDataItemNames(std::vector< std::string > itemNames)
const std::string COLLECTION
Base of all data objects.
static FileListType GenerateFileLists(std::string folder, std::vector< std::string > suffixes, bool allowGaps=false)
GenerateFileLists Returns a collection of lists with valid files names in a folder.
static FileListType SanitizeFileList(FileListType list)
SanitizeFileList Removes all entries that are lacking at least one modality.
const std::string SUBCOLLECTION
std::vector< std::vector< std::string > > FileListType
void EndElement(const char *elementName)
Derived from XMLReader.
void StartElement(const char *elementName, const char **atts)
Derived from XMLReader.
void AddSubColIds(std::vector< std::string > subColIds)
void AddDataElementIds(std::vector< std::string > dataElemetIds)
static std::vector< mitk::BaseData::Pointer > LoadBaseDataFromFile(const std::string path, const std::string filePrefix, const std::string filePattern, bool series)
static std::string GetDate(std::string fileName, std::string suffix)
const std::string FILEPATH
static std::string GetName(std::string fileName, std::string suffix)
static DataCollection::Pointer FolderToCollection(std::string folder, std::vector< std::string > suffixes, std::vector< std::string > seriesNames, bool allowGaps)
Build up a mitk::DataCollection from a folder providing suffixes to group the files.
itk::SmartPointer< Self > Pointer
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.