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
00019 #include "mitkPointSetReader.h"
00020 #include <iostream>
00021 #include <fstream>
00022 #include <locale>
00023
00024 mitk::PointSetReader::PointSetReader()
00025 {
00026 m_Success = false;
00027 }
00028
00029
00030 mitk::PointSetReader::~PointSetReader()
00031 {}
00032
00033
00034 void mitk::PointSetReader::GenerateData()
00035 {
00036 std::locale::global(std::locale("C"));
00037
00038 m_Success = false;
00039 if ( m_FileName == "" )
00040 {
00041 itkWarningMacro( << "Sorry, filename has not been set!" );
00042 return ;
00043 }
00044 if ( ! this->CanReadFile( m_FileName.c_str() ) )
00045 {
00046 itkWarningMacro( << "Sorry, can't read file " << m_FileName << "!" );
00047 return ;
00048 }
00049
00050 try{
00051 TiXmlDocument doc(m_FileName.c_str());
00052 bool loadOkay = doc.LoadFile();
00053 if (loadOkay)
00054 {
00055 TiXmlHandle docHandle( &doc );
00056 unsigned int pointSetCounter(0);
00057 for( TiXmlElement* currentPointSetElement = docHandle.FirstChildElement("point_set_file").FirstChildElement("point_set").ToElement();
00058 currentPointSetElement != NULL; currentPointSetElement = currentPointSetElement->NextSiblingElement())
00059 {
00060 mitk::PointSet::Pointer newPointSet = mitk::PointSet::New();
00061 if(currentPointSetElement->FirstChildElement("time_series") != NULL)
00062 {
00063 for( TiXmlElement* currentTimeSeries = currentPointSetElement->FirstChildElement("time_series")->ToElement();
00064 currentTimeSeries != NULL; currentTimeSeries = currentTimeSeries->NextSiblingElement())
00065 {
00066 unsigned int currentTimeStep(0);
00067 TiXmlElement* currentTimeSeriesID = currentTimeSeries->FirstChildElement("time_series_id");
00068
00069 currentTimeStep = atoi(currentTimeSeriesID->GetText());
00070
00071 newPointSet = this->ReadPoint(newPointSet, currentTimeSeries, currentTimeStep);
00072 }
00073 } else {
00074 newPointSet = this->ReadPoint(newPointSet, currentPointSetElement, 0);
00075 }
00076 this->SetNthOutput( pointSetCounter, newPointSet );
00077 pointSetCounter++;
00078 }
00079 }
00080 else
00081 {
00082 MITK_WARN << "XML parser error!";
00083 }
00084 }catch(...)
00085 {
00086 MITK_ERROR << "Cannot read point set.";
00087 m_Success = false;
00088 }
00089 m_Success = true;
00090 }
00091
00092 mitk::PointSet::Pointer mitk::PointSetReader::ReadPoint(mitk::PointSet::Pointer newPointSet,
00093 TiXmlElement* currentTimeSeries, unsigned int currentTimeStep)
00094 {
00095 if(currentTimeSeries->FirstChildElement("point") != NULL)
00096 {
00097 for( TiXmlElement* currentPoint = currentTimeSeries->FirstChildElement("point")->ToElement();
00098 currentPoint != NULL; currentPoint = currentPoint->NextSiblingElement())
00099 {
00100 unsigned int id(0);
00101 mitk::PointSpecificationType spec((mitk::PointSpecificationType) 0);
00102 double x(0.0);
00103 double y(0.0);
00104 double z(0.0);
00105
00106 id = atoi(currentPoint->FirstChildElement("id")->GetText());
00107 if(currentPoint->FirstChildElement("specification") != NULL)
00108 {
00109 spec = (mitk::PointSpecificationType) atoi(currentPoint->FirstChildElement("specification")->GetText());
00110 }
00111 x = atof(currentPoint->FirstChildElement("x")->GetText());
00112 y = atof(currentPoint->FirstChildElement("y")->GetText());
00113 z = atof(currentPoint->FirstChildElement("z")->GetText());
00114
00115 mitk::Point3D point;
00116 mitk::FillVector3D(point, x, y, z);
00117 newPointSet->SetPoint(id, point, spec, currentTimeStep);
00118 }
00119 }
00120 return newPointSet;
00121 }
00122
00123 void mitk::PointSetReader::GenerateOutputInformation()
00124 {
00125 }
00126
00127 int mitk::PointSetReader::CanReadFile ( const char *name )
00128 {
00129 std::ifstream in( name );
00130 bool isGood = in.good();
00131 in.close();
00132 return isGood;
00133 }
00134
00135 bool mitk::PointSetReader::CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
00136 {
00137
00138 if( filename == "" )
00139 {
00140
00141 return false;
00142 }
00143
00144
00145 if( filePattern != "" && filePrefix != "" )
00146 return false;
00147
00148 bool extensionFound = false;
00149 std::string::size_type MPSPos = filename.rfind(".mps");
00150 if ((MPSPos != std::string::npos)
00151 && (MPSPos == filename.length() - 4))
00152 {
00153 extensionFound = true;
00154 }
00155
00156 MPSPos = filename.rfind(".MPS");
00157 if ((MPSPos != std::string::npos)
00158 && (MPSPos == filename.length() - 4))
00159 {
00160 extensionFound = true;
00161 }
00162
00163 if( !extensionFound )
00164 {
00165
00166 return false;
00167 }
00168
00169 return true;
00170 }
00171
00172 void mitk::PointSetReader::ResizeOutputs( const unsigned int& num )
00173 {
00174 unsigned int prevNum = this->GetNumberOfOutputs();
00175 this->SetNumberOfOutputs( num );
00176 for ( unsigned int i = prevNum; i < num; ++i )
00177 {
00178 this->SetNthOutput( i, this->MakeOutput( i ).GetPointer() );
00179 }
00180 }
00181
00182
00183 bool mitk::PointSetReader::GetSuccess() const
00184 {
00185 return m_Success;
00186 }