33 #include <itksys/SystemTools.hxx>
37 return original->Clone();
41 class PlanarFigureIOTestClass
44 typedef std::list<mitk::PlanarFigure::Pointer> PlanarFigureList;
45 typedef std::vector<mitk::PlanarFigureWriter::Pointer> PlanarFigureToMemoryWriterList;
47 static PlanarFigureList CreatePlanarFigures()
49 PlanarFigureList planarFigures;
53 planeGeometry->InitializeStandardPlane(100.0, 100.0);
71 planarAngle->SetPlaneGeometry(planeGeometry);
72 planarAngle->PlaceFigure(p0);
73 planarAngle->SetCurrentControlPoint(p1);
74 planarAngle->AddControlPoint(p2);
75 planarAngle->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
76 planarFigures.push_back(planarAngle.GetPointer());
80 planarCircle->SetPlaneGeometry(planeGeometry);
81 planarCircle->PlaceFigure(p0);
82 planarCircle->SetCurrentControlPoint(p1);
83 planarCircle->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
84 planarFigures.push_back(planarCircle.GetPointer());
88 planarCross->SetSingleLineMode(
false);
89 planarCross->SetPlaneGeometry(planeGeometry);
90 planarCross->PlaceFigure(p0);
91 planarCross->SetCurrentControlPoint(p1);
92 planarCross->AddControlPoint(p2);
93 planarCross->AddControlPoint(p3);
94 planarCross->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
95 planarFigures.push_back(planarCross.GetPointer());
99 planarFourPointAngle->SetPlaneGeometry(planeGeometry);
100 planarFourPointAngle->PlaceFigure(p0);
101 planarFourPointAngle->SetCurrentControlPoint(p1);
102 planarFourPointAngle->AddControlPoint(p2);
103 planarFourPointAngle->AddControlPoint(p3);
104 planarFourPointAngle->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
105 planarFigures.push_back(planarFourPointAngle.GetPointer());
109 planarLine->SetPlaneGeometry(planeGeometry);
110 planarLine->PlaceFigure(p0);
111 planarLine->SetCurrentControlPoint(p1);
112 planarLine->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
113 planarFigures.push_back(planarLine.GetPointer());
117 planarPolygon->SetClosed(
false);
118 planarPolygon->SetPlaneGeometry(planeGeometry);
119 planarPolygon->PlaceFigure(p0);
120 planarPolygon->SetCurrentControlPoint(p1);
121 planarPolygon->AddControlPoint(p2);
122 planarPolygon->AddControlPoint(p3);
123 planarPolygon->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
124 planarFigures.push_back(planarPolygon.GetPointer());
128 planarSubdivisionPolygon->SetClosed(
false);
129 planarSubdivisionPolygon->SetPlaneGeometry(planeGeometry);
130 planarSubdivisionPolygon->PlaceFigure(p0);
131 planarSubdivisionPolygon->SetCurrentControlPoint(p1);
132 planarSubdivisionPolygon->AddControlPoint(p2);
133 planarSubdivisionPolygon->AddControlPoint(p3);
134 planarSubdivisionPolygon->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
135 planarFigures.push_back(planarSubdivisionPolygon.GetPointer());
139 planarRectangle->SetPlaneGeometry(planeGeometry);
140 planarRectangle->PlaceFigure(p0);
141 planarRectangle->SetCurrentControlPoint(p1);
142 planarRectangle->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
143 planarFigures.push_back(planarRectangle.GetPointer());
158 spacing[0] = 0.0123456;
159 spacing[1] = 0.0123456;
160 spacing[2] = 1.123456;
161 preciseGeometry->InitializeStandardPlane(right, down, &spacing);
165 p0precise[0] = p0[0] * spacing[0];
166 p0precise[1] = p0[1] * spacing[1];
168 p1precise[0] = p1[0] * spacing[0];
169 p1precise[1] = p1[1] * spacing[1];
171 p2precise[0] = p2[0] * spacing[0];
172 p2precise[1] = p2[1] * spacing[1];
174 p3precise[0] = p3[0] * spacing[0];
175 p3precise[1] = p3[1] * spacing[1];
180 nochncross->SetSingleLineMode(
false);
181 nochncross->SetPlaneGeometry(preciseGeometry);
182 nochncross->PlaceFigure(p0precise);
183 nochncross->SetCurrentControlPoint(p1precise);
184 nochncross->AddControlPoint(p2precise);
185 nochncross->AddControlPoint(p3precise);
186 nochncross->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
187 planarFigures.push_back(nochncross.GetPointer());
191 planarAnglePrecise->SetPlaneGeometry(preciseGeometry);
192 planarAnglePrecise->PlaceFigure(p0precise);
193 planarAnglePrecise->SetCurrentControlPoint(p1precise);
194 planarAnglePrecise->AddControlPoint(p2precise);
195 planarAnglePrecise->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
196 planarFigures.push_back(planarAnglePrecise.GetPointer());
200 planarCirclePrecise->SetPlaneGeometry(preciseGeometry);
201 planarCirclePrecise->PlaceFigure(p0precise);
202 planarCirclePrecise->SetCurrentControlPoint(p1precise);
203 planarCirclePrecise->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
204 planarFigures.push_back(planarCirclePrecise.GetPointer());
208 planarFourPointAnglePrecise->SetPlaneGeometry(preciseGeometry);
209 planarFourPointAnglePrecise->PlaceFigure(p0precise);
210 planarFourPointAnglePrecise->SetCurrentControlPoint(p1precise);
211 planarFourPointAnglePrecise->AddControlPoint(p2precise);
212 planarFourPointAnglePrecise->AddControlPoint(p3precise);
213 planarFourPointAnglePrecise->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
214 planarFigures.push_back(planarFourPointAnglePrecise.GetPointer());
218 planarLinePrecise->SetPlaneGeometry(preciseGeometry);
219 planarLinePrecise->PlaceFigure(p0precise);
220 planarLinePrecise->SetCurrentControlPoint(p1precise);
221 planarLinePrecise->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
222 planarFigures.push_back(planarLinePrecise.GetPointer());
226 planarPolygonPrecise->SetClosed(
false);
227 planarPolygonPrecise->SetPlaneGeometry(preciseGeometry);
228 planarPolygonPrecise->PlaceFigure(p0precise);
229 planarPolygonPrecise->SetCurrentControlPoint(p1precise);
230 planarPolygonPrecise->AddControlPoint(p2precise);
231 planarPolygonPrecise->AddControlPoint(p3precise);
232 planarPolygonPrecise->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
233 planarFigures.push_back(planarPolygonPrecise.GetPointer());
237 planarSubdivisionPolygonPrecise->SetClosed(
false);
238 planarSubdivisionPolygonPrecise->SetPlaneGeometry(preciseGeometry);
239 planarSubdivisionPolygonPrecise->PlaceFigure(p0precise);
240 planarSubdivisionPolygonPrecise->SetCurrentControlPoint(p1precise);
241 planarSubdivisionPolygonPrecise->AddControlPoint(p2precise);
242 planarSubdivisionPolygonPrecise->AddControlPoint(p3precise);
243 planarSubdivisionPolygonPrecise->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
244 planarFigures.push_back(planarSubdivisionPolygonPrecise.GetPointer());
248 planarRectanglePrecise->SetPlaneGeometry(preciseGeometry);
249 planarRectanglePrecise->PlaceFigure(p0precise);
250 planarRectanglePrecise->SetCurrentControlPoint(p1precise);
251 planarRectanglePrecise->GetPropertyList()->SetBoolProperty(
"initiallyplaced",
true);
252 planarFigures.push_back(planarRectanglePrecise.GetPointer());
254 return planarFigures;
257 static PlanarFigureList CreateDeepCopiedPlanarFigures(PlanarFigureList original)
259 PlanarFigureList copiedPlanarFigures;
261 PlanarFigureList::iterator it1;
263 for (it1 = original.begin(); it1 != original.end(); ++it1)
267 copiedPlanarFigures.push_back(copiedFigure);
269 return copiedPlanarFigures;
272 static PlanarFigureList CreateClonedPlanarFigures(PlanarFigureList original)
274 PlanarFigureList clonedPlanarFigures;
275 clonedPlanarFigures.resize(original.size());
276 std::transform(original.begin(), original.end(), clonedPlanarFigures.begin(),
Clone);
277 return clonedPlanarFigures;
280 static void VerifyPlanarFigures(PlanarFigureList &planarFigures1, PlanarFigureList &planarFigures2)
282 PlanarFigureList::iterator it1, it2;
285 for (it1 = planarFigures1.begin(); it1 != planarFigures1.end(); ++it1)
287 bool planarFigureFound =
false;
289 for (it2 = planarFigures2.begin(); it2 != planarFigures2.end(); ++it2)
292 if (ComparePlanarFigures(*it1, *it2))
294 planarFigureFound =
true;
301 "Testing if " << (*it1)->GetNameOfClass() <<
" has a counterpart " << i);
309 if (strcmp(figure1->GetNameOfClass(), figure2->GetNameOfClass()) != 0)
314 if (strcmp(figure1->GetNameOfClass(),
"PlanarCross") == 0)
316 std::cout <<
"Planar Cross Found" << std::endl;
331 if (point1.EuclideanDistanceTo(point2) >=
mitk::eps)
342 if (properties1->size() != properties2->size())
348 for (
auto i1 = properties1->begin(); i1 != properties1->end(); ++i1)
350 std::cout << i1->first << std::endl;
354 for (
auto i2 = properties2->begin(); i2 != properties2->end(); ++i2)
356 std::cout << i2->first << std::endl;
362 if (!std::equal(properties1->begin(), properties1->end(), properties2->begin(), PropertyMapEntryCompare()))
374 const TransformType::ParametersType ¶meters1 = affineGeometry1->GetParameters();
376 for (
unsigned int i = 0; i < affineGeometry1->GetNumberOfParameters(); ++i)
378 if (fabs(parameters1.GetElement(i) - parameters2.GetElement(i)) >=
mitk::eps)
386 const BoundsArrayType &bounds1 = planeGeometry1->
GetBounds();
387 const BoundsArrayType &bounds2 = planeGeometry2->
GetBounds();
388 for (
unsigned int i = 0; i < 6; ++i)
390 if (fabs(bounds1.GetElement(i) - bounds2.GetElement(i)) >=
mitk::eps)
399 if ((spacing1 - spacing2).GetNorm() >=
mitk::eps)
407 if (origin1.EuclideanDistanceTo(origin2) >=
mitk::eps)
414 static void SerializePlanarFigures(PlanarFigureList &planarFigures, std::string &fileName)
417 std::cout <<
"File name: " << fileName << std::endl;
420 writer->SetFileName(fileName.c_str());
423 PlanarFigureList::iterator it;
424 for (it = planarFigures.begin(), i = 0; it != planarFigures.end(); ++it, ++i)
426 writer->SetInput(i, *it);
434 static PlanarFigureList DeserializePlanarFigures(std::string &fileName)
438 reader->SetFileName(fileName.c_str());
444 PlanarFigureList planarFigures;
445 for (
unsigned int i = 0; i < reader->GetNumberOfOutputs(); ++i)
448 planarFigures.push_back(figure);
451 return planarFigures;
454 static PlanarFigureToMemoryWriterList SerializePlanarFiguresToMemoryBuffers(PlanarFigureList &planarFigures)
456 PlanarFigureToMemoryWriterList pfMemoryWriters;
458 PlanarFigureList::iterator it;
461 for (it = planarFigures.begin(), i = 0; it != planarFigures.end(); ++it, ++i)
464 writer->SetWriteToMemory(
true);
465 writer->SetInput(*it);
468 pfMemoryWriters.push_back(writer);
470 if (!writer->GetSuccess())
476 return pfMemoryWriters;
479 static PlanarFigureList DeserializePlanarFiguresFromMemoryBuffers(PlanarFigureToMemoryWriterList pfMemoryWriters)
482 PlanarFigureList planarFigures;
484 for (
unsigned int i = 0; i < pfMemoryWriters.size(); ++i)
488 reader->SetReadFromMemory(
true);
489 reader->SetMemoryBuffer(pfMemoryWriters[i]->GetMemoryPointer(), pfMemoryWriters[i]->GetMemorySize());
492 planarFigures.push_back(figure);
494 if (!reader->GetSuccess())
500 return planarFigures;
504 class PropertyMapEntryCompare
507 bool operator()(
const mitk::PropertyList::PropertyMap::value_type &entry1,
508 const mitk::PropertyList::PropertyMap::value_type &entry2)
510 MITK_INFO <<
"Comparing " << entry1.first <<
"(" << entry1.second->GetValueAsString() <<
") and " << entry2.first
511 <<
"(" << entry2.second->GetValueAsString() <<
")";
513 return *(entry1.second) == *(entry2.second);
533 PlanarFigureIOTestClass::PlanarFigureList originalPlanarFigures = PlanarFigureIOTestClass::CreatePlanarFigures();
536 PlanarFigureIOTestClass::PlanarFigureList copiedPlanarFigures =
537 PlanarFigureIOTestClass::CreateDeepCopiedPlanarFigures(originalPlanarFigures);
539 PlanarFigureIOTestClass::VerifyPlanarFigures(originalPlanarFigures, copiedPlanarFigures);
542 PlanarFigureIOTestClass::PlanarFigureList clonedPlanarFigures =
543 PlanarFigureIOTestClass::CreateClonedPlanarFigures(originalPlanarFigures);
545 PlanarFigureIOTestClass::VerifyPlanarFigures(originalPlanarFigures, clonedPlanarFigures);
549 static unsigned long count = 0;
550 unsigned long n = count++;
551 std::ostringstream name;
552 for (
int i = 0; i < 6; ++i)
554 name << char(
'a' + (n % 26));
558 myname.append(name.str());
560 std::string fileName = itksys::SystemTools::GetCurrentWorkingDirectory() + myname +
".pf";
562 PlanarFigureIOTestClass::SerializePlanarFigures(originalPlanarFigures, fileName);
565 PlanarFigureIOTestClass::PlanarFigureToMemoryWriterList writersWithMemoryBuffers =
566 PlanarFigureIOTestClass::SerializePlanarFiguresToMemoryBuffers(originalPlanarFigures);
569 PlanarFigureIOTestClass::PlanarFigureList retrievedPlanarFigures =
570 PlanarFigureIOTestClass::DeserializePlanarFigures(fileName);
573 PlanarFigureIOTestClass::PlanarFigureList retrievedPlanarFiguresFromMemory =
574 PlanarFigureIOTestClass::DeserializePlanarFiguresFromMemoryBuffers(writersWithMemoryBuffers);
576 auto it = writersWithMemoryBuffers.begin();
577 while (it != writersWithMemoryBuffers.end())
579 (*it)->ReleaseMemory();
584 PlanarFigureIOTestClass::VerifyPlanarFigures(originalPlanarFigures, retrievedPlanarFigures);
587 PlanarFigureIOTestClass::VerifyPlanarFigures(originalPlanarFigures, retrievedPlanarFiguresFromMemory);
590 originalPlanarFigures.empty();
593 PlanarFigureIOTestClass::VerifyPlanarFigures(copiedPlanarFigures, retrievedPlanarFigures);
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
BoundingBoxType::BoundsArrayType BoundsArrayType
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
GeometryTransformHolder::TransformType TransformType
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
const BoundsArrayType GetBounds() const
std::map< std::string, BaseProperty::Pointer > PropertyMap
mitk::PropertyList::Pointer GetPropertyList() const
Get the data's property list.
MITKCORE_EXPORT const ScalarType eps
Describes a two-dimensional, rectangular plane.
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
BoundingBoxType::BoundsArrayType BoundsArrayType