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 "mitkTestingMacros.h"
00019
00020 #include "mitkPlanarAngle.h"
00021 #include "mitkPlanarCircle.h"
00022 #include "mitkPlanarCross.h"
00023 #include "mitkPlanarFourPointAngle.h"
00024 #include "mitkPlanarLine.h"
00025 #include "mitkPlanarPolygon.h"
00026 #include "mitkPlanarRectangle.h"
00027
00028 #include "mitkPlanarFigureWriter.h"
00029 #include "mitkPlanarFigureReader.h"
00030
00031 #include "mitkPlaneGeometry.h"
00032
00033 #include <itksys/SystemTools.hxx>
00034
00035
00037 class PlanarFigureIOTestClass
00038 {
00039 public:
00040
00041 typedef std::list< mitk::PlanarFigure::Pointer > PlanarFigureList;
00042
00043 static PlanarFigureList CreatePlanarFigures()
00044 {
00045 PlanarFigureList planarFigures;
00046
00047
00048 mitk::PlaneGeometry::Pointer planeGeometry = mitk::PlaneGeometry::New();
00049 planeGeometry->InitializeStandardPlane( 100.0, 100.0 );
00050
00051
00052 mitk::Point2D p0; p0[0] = 20.0; p0[1] = 20.0;
00053 mitk::Point2D p1; p1[0] = 80.0; p1[1] = 80.0;
00054 mitk::Point2D p2; p2[0] = 90.0; p2[1] = 10.0;
00055 mitk::Point2D p3; p3[0] = 10.0; p3[1] = 90.0;
00056
00057
00058 mitk::PlanarAngle::Pointer planarAngle = mitk::PlanarAngle::New();
00059 planarAngle->SetGeometry2D( planeGeometry );
00060 planarAngle->PlaceFigure( p0 );
00061 planarAngle->SetCurrentControlPoint( p1 );
00062 planarAngle->AddControlPoint( p2 );
00063 planarFigures.push_back( planarAngle.GetPointer() );
00064
00065
00066 mitk::PlanarCircle::Pointer planarCircle = mitk::PlanarCircle::New();
00067 planarCircle->SetGeometry2D( planeGeometry );
00068 planarCircle->PlaceFigure( p0 );
00069 planarCircle->SetCurrentControlPoint( p1 );
00070 planarFigures.push_back( planarCircle.GetPointer() );
00071
00072
00073 mitk::PlanarCross::Pointer planarCross = mitk::PlanarCross::New();
00074 planarCross->SetSingleLineMode( false );
00075 planarCross->SetGeometry2D( planeGeometry );
00076 planarCross->PlaceFigure( p0 );
00077 planarCross->SetCurrentControlPoint( p1 );
00078 planarCross->AddControlPoint( p2 );
00079 planarCross->AddControlPoint( p3 );
00080 planarFigures.push_back( planarCross.GetPointer() );
00081
00082
00083 mitk::PlanarFourPointAngle::Pointer planarFourPointAngle = mitk::PlanarFourPointAngle::New();
00084 planarFourPointAngle->SetGeometry2D( planeGeometry );
00085 planarFourPointAngle->PlaceFigure( p0 );
00086 planarFourPointAngle->SetCurrentControlPoint( p1 );
00087 planarFourPointAngle->AddControlPoint( p2 );
00088 planarFourPointAngle->AddControlPoint( p3 );
00089 planarFigures.push_back( planarFourPointAngle.GetPointer() );
00090
00091
00092 mitk::PlanarLine::Pointer planarLine = mitk::PlanarLine::New();
00093 planarLine->SetGeometry2D( planeGeometry );
00094 planarLine->PlaceFigure( p0 );
00095 planarLine->SetCurrentControlPoint( p1 );
00096 planarFigures.push_back( planarLine.GetPointer() );
00097
00098
00099 mitk::PlanarPolygon::Pointer planarPolygon = mitk::PlanarPolygon::New();
00100 planarPolygon->SetClosed( false );
00101 planarPolygon->SetGeometry2D( planeGeometry );
00102 planarPolygon->PlaceFigure( p0 );
00103 planarPolygon->SetCurrentControlPoint( p1 );
00104 planarPolygon->AddControlPoint( p2 );
00105 planarPolygon->AddControlPoint( p3 );
00106 planarFigures.push_back( planarPolygon.GetPointer() );
00107
00108
00109 mitk::PlanarRectangle::Pointer planarRectangle = mitk::PlanarRectangle::New();
00110 planarRectangle->SetGeometry2D( planeGeometry );
00111 planarRectangle->PlaceFigure( p0 );
00112 planarRectangle->SetCurrentControlPoint( p1 );
00113 planarFigures.push_back( planarRectangle.GetPointer() );
00114
00115 return planarFigures;
00116 }
00117
00118
00119 static void VerifyPlanarFigures( PlanarFigureList &planarFigures1, PlanarFigureList &planarFigures2 )
00120 {
00121 PlanarFigureList::iterator it1, it2;
00122
00123 for ( it1 = planarFigures1.begin(); it1 != planarFigures1.end(); ++it1 )
00124 {
00125 bool planarFigureFound = false;
00126 for ( it2 = planarFigures2.begin(); it2 != planarFigures2.end(); ++it2 )
00127 {
00128
00129 if ( ComparePlanarFigures( *it1, *it2 ) )
00130 {
00131 planarFigureFound = true;
00132 }
00133 }
00134
00135
00136 MITK_TEST_CONDITION_REQUIRED(
00137 planarFigureFound,
00138 "Testing if " << (*it1)->GetNameOfClass() << " has a counterpart" );
00139 }
00140 }
00141
00142 static bool ComparePlanarFigures( mitk::PlanarFigure* figure1, mitk::PlanarFigure* figure2 )
00143 {
00144
00145 if ( strcmp( figure1->GetNameOfClass(), figure2->GetNameOfClass() ) != 0 )
00146 {
00147 return false;
00148 }
00149
00150 const char* figureName = figure1->GetNameOfClass();
00151
00152
00153 MITK_TEST_CONDITION_REQUIRED(
00154 figure1->GetNumberOfControlPoints() == figure2->GetNumberOfControlPoints(),
00155 figureName << ": Testing number of control points" );
00156
00157
00158 for ( unsigned int i = 0; i < figure1->GetNumberOfControlPoints(); ++i )
00159 {
00160 mitk::Point2D& point1 = figure1->GetControlPoint( i );
00161 mitk::Point2D& point2 = figure2->GetControlPoint( i );
00162
00163 MITK_TEST_CONDITION_REQUIRED(
00164 point1.EuclideanDistanceTo( point2 ) < mitk::eps,
00165 figureName << ": Testing equality of control point " << i );
00166 }
00167
00168
00169
00170 typedef mitk::PropertyList::PropertyMap PropertyMap;
00171 const PropertyMap* properties1 = figure1->GetPropertyList()->GetMap();
00172 const PropertyMap* properties2 = figure2->GetPropertyList()->GetMap();
00173
00174 MITK_TEST_CONDITION_REQUIRED(
00175 properties1->size() == properties2->size(),
00176 figureName << ": Testing number of properties" );
00177
00178 MITK_INFO << "List 1:";
00179 for (PropertyMap::const_iterator i1 = properties1->begin(); i1 != properties1->end(); ++i1)
00180 {
00181 std::cout << i1->first << std::endl;
00182 }
00183
00184 MITK_INFO << "List 2:";
00185 for (PropertyMap::const_iterator i2 = properties2->begin(); i2 != properties2->end(); ++i2)
00186 {
00187 std::cout << i2->first << std::endl;
00188 }
00189
00190 MITK_INFO << "-------";
00191
00192
00193 MITK_TEST_CONDITION_REQUIRED(
00194 std::equal( properties1->begin(), properties1->end(), properties2->begin(), PropertyMapEntryCompare() ),
00195 figureName << ": Testing equality of properties");
00196
00197
00198 const mitk::PlaneGeometry* planeGeometry1 = dynamic_cast<const mitk::PlaneGeometry*>(figure1->GetGeometry2D());
00199 const mitk::PlaneGeometry* planeGeometry2 = dynamic_cast<const mitk::PlaneGeometry*>(figure2->GetGeometry2D());
00200
00201
00202 bool parametersEqual = true;
00203 typedef mitk::AffineGeometryFrame3D::TransformType TransformType;
00204 const TransformType* affineGeometry1 = planeGeometry1->GetIndexToWorldTransform();
00205 const TransformType::ParametersType& parameters1 = affineGeometry1->GetParameters();
00206 const TransformType::ParametersType& parameters2 = planeGeometry2->GetIndexToWorldTransform()->GetParameters();
00207 for ( unsigned int i = 0; i < affineGeometry1->GetNumberOfParameters(); ++i )
00208 {
00209 if ( fabs(parameters1.GetElement( i ) - parameters2.GetElement( i )) > mitk::eps )
00210 {
00211 parametersEqual = false;
00212 }
00213 }
00214
00215 MITK_TEST_CONDITION_REQUIRED(
00216 parametersEqual,
00217 figureName << ": Testing if Geometry transform parameters are equal");
00218
00219
00220
00221 bool boundsEqual = true;
00222 typedef mitk::Geometry3D::BoundsArrayType BoundsArrayType;
00223 const BoundsArrayType& bounds1 = planeGeometry1->GetBounds();
00224 const BoundsArrayType& bounds2 = planeGeometry2->GetBounds();
00225 for ( unsigned int i = 0; i < 6; ++i )
00226 {
00227 if ( fabs(bounds1.GetElement( i ) - bounds2.GetElement( i )) > mitk::eps )
00228 {
00229 boundsEqual = false;
00230 };
00231 }
00232
00233 MITK_TEST_CONDITION_REQUIRED(
00234 boundsEqual,
00235 figureName << ": Testing if Geometry bounds are equal");
00236
00237
00238
00239 mitk::Vector3D spacing1 = planeGeometry1->GetSpacing();
00240 mitk::Vector3D spacing2 = planeGeometry2->GetSpacing();
00241
00242 MITK_TEST_CONDITION_REQUIRED(
00243 (spacing1 - spacing2).GetNorm() < mitk::eps,
00244 figureName << ": Testing if Geometry spacing is equal");
00245
00246
00247 mitk::Point3D origin1 = planeGeometry1->GetOrigin();
00248 mitk::Point3D origin2 = planeGeometry2->GetOrigin();
00249
00250 MITK_TEST_CONDITION_REQUIRED(
00251 origin1.EuclideanDistanceTo( origin2 ) < mitk::eps,
00252 figureName << ": Testing if Geometry origin are equal");
00253
00254
00255 return true;
00256 }
00257
00258
00259 static void SerializePlanarFigures( PlanarFigureList &planarFigures, std::string& fileName )
00260 {
00261
00262 std::cout << "File name: " << fileName << std::endl;
00263
00264 mitk::PlanarFigureWriter::Pointer writer = mitk::PlanarFigureWriter::New();
00265 writer->SetFileName( fileName.c_str() );
00266
00267 unsigned int i;
00268 PlanarFigureList::iterator it;
00269 for ( it = planarFigures.begin(), i = 0;
00270 it != planarFigures.end();
00271 ++it, ++i )
00272 {
00273 writer->SetInput( i, *it );
00274 }
00275
00276 writer->Update();
00277
00278 MITK_TEST_CONDITION_REQUIRED(
00279 writer->GetSuccess(),
00280 "Testing if writing was successful");
00281 }
00282
00283
00284 static PlanarFigureList DeserializePlanarFigures( std::string& fileName)
00285 {
00286
00287 mitk::PlanarFigureReader::Pointer reader = mitk::PlanarFigureReader::New();
00288 reader->SetFileName( fileName.c_str() );
00289 reader->Update();
00290
00291 MITK_TEST_CONDITION_REQUIRED(
00292 reader->GetSuccess(),
00293 "Testing if reading was successful");
00294
00295
00296
00297 PlanarFigureList planarFigures;
00298 for ( unsigned int i = 0; i < reader->GetNumberOfOutputs(); ++i )
00299 {
00300 mitk::PlanarFigure* figure = reader->GetOutput( i );
00301 planarFigures.push_back( figure );
00302 }
00303
00304 return planarFigures;
00305 }
00306
00307 private:
00308 class PropertyMapEntryCompare
00309 {
00310 public:
00311 bool operator()(
00312 const mitk::PropertyList::PropertyMap::value_type &entry1,
00313 const mitk::PropertyList::PropertyMap::value_type &entry2 )
00314 {
00315 MITK_INFO << "Comparing " << entry1.first << "(" << entry1.second.first->GetValueAsString() << ") and " << entry2.first << "(" << entry2.second.first->GetValueAsString() << ")";
00316
00317 return *(entry1.second.first) == *(entry2.second.first);
00318 }
00319 };
00320
00321 };
00322
00323
00333 int mitkPlanarFigureIOTest(int , char* [])
00334 {
00335 MITK_TEST_BEGIN("PlanarFigureIO")
00336
00337
00338 PlanarFigureIOTestClass::PlanarFigureList originalPlanarFigures =
00339 PlanarFigureIOTestClass::CreatePlanarFigures();
00340
00341
00342
00343
00344
00345 static unsigned long count = 0;
00346 unsigned long n = count++;
00347 std::ostringstream name;
00348 for (int i = 0; i < 6; ++i)
00349 {
00350 name << char('a' + (n % 26));
00351 n /= 26;
00352 }
00353 std::string myname;
00354 myname.append(name.str());
00355
00356 std::string fileName = itksys::SystemTools::GetCurrentWorkingDirectory() + myname + ".pf";
00357
00358 PlanarFigureIOTestClass::SerializePlanarFigures( originalPlanarFigures, fileName );
00359
00360
00361
00362 PlanarFigureIOTestClass::PlanarFigureList retrievedPlanarFigures =
00363 PlanarFigureIOTestClass::DeserializePlanarFigures( fileName );
00364
00365
00366
00367 PlanarFigureIOTestClass::VerifyPlanarFigures( originalPlanarFigures, retrievedPlanarFigures );
00368
00369
00370 MITK_TEST_END()
00371 }