diff --git a/Modules/ImageStatistics/Testing/mitkPointSetDifferenceStatisticsCalculatorTest.cpp b/Modules/ImageStatistics/Testing/mitkPointSetDifferenceStatisticsCalculatorTest.cpp index 9d41139c1b..4912ca06d3 100644 --- a/Modules/ImageStatistics/Testing/mitkPointSetDifferenceStatisticsCalculatorTest.cpp +++ b/Modules/ImageStatistics/Testing/mitkPointSetDifferenceStatisticsCalculatorTest.cpp @@ -1,113 +1,151 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ -#include "mitkStandardFileLocations.h" #include "mitkTestingMacros.h" #include "mitkPointSetDifferenceStatisticsCalculator.h" -//#include - /** * \brief Test class for mitkPointSetDifferenceStatisticsCalculator */ -class mitkPointSetDifferenceStatisticsCalculatorTestClass + +//Members used for testing purposes +mitk::PointSetDifferenceStatisticsCalculator::Pointer m_myPointSetDifferenceStatisticsCalculator; +mitk::PointSet::Pointer m_myTestPointSet1; +mitk::PointSet::Pointer m_myTestPointSet2; + +//This method should be called before every new sub-test call in order to freshly intialize all relevant classes +void Setup() { + // let's create an object of our class + m_myPointSetDifferenceStatisticsCalculator = mitk::PointSetDifferenceStatisticsCalculator::New(); + //and some empty test data + m_myTestPointSet1 = mitk::PointSet::New(); + m_myTestPointSet2 = mitk::PointSet::New(); +} -public: - - static void TestInstantiation() - { - // let's create an object of our class - mitk::PointSetDifferenceStatisticsCalculator::Pointer myPointSetDifferenceStatisticsCalculator = mitk::PointSetDifferenceStatisticsCalculator::New(); - MITK_TEST_CONDITION_REQUIRED(myPointSetDifferenceStatisticsCalculator.IsNotNull(),"Testing instantiation with constructor 1."); - - mitk::PointSet::Pointer myTestPointSet1 = mitk::PointSet::New(); - mitk::PointSet::Pointer myTestPointSet2 = mitk::PointSet::New(); - mitk::PointSetDifferenceStatisticsCalculator::Pointer myPointSetDifferenceStatisticsCalculator2 = mitk::PointSetDifferenceStatisticsCalculator::New(myTestPointSet1,myTestPointSet2); - MITK_TEST_CONDITION_REQUIRED(myPointSetDifferenceStatisticsCalculator2.IsNotNull(),"Testing instantiation with constructor 2."); - } - -static void TestSimpleCase() - { - - MITK_TEST_OUTPUT(<< "Starting simple test case..."); - - mitk::Point3D test; - mitk::PointSet::Pointer testPointSet1 = mitk::PointSet::New(); - - mitk::FillVector3D(test,0,0,0); - testPointSet1->InsertPoint(0,test); - - mitk::FillVector3D(test,1,1,1); - testPointSet1->InsertPoint(1,test); - - mitk::PointSet::Pointer testPointSet2 = mitk::PointSet::New(); - - mitk::FillVector3D(test,0.5,0.5,0.5); - testPointSet2->InsertPoint(0,test); - - mitk::FillVector3D(test,2,2,2); - testPointSet2->InsertPoint(1,test); - double squaredDistance1 = 0.75; - double squaredDistance2 = 3; - double mean = (sqrt(squaredDistance1)+sqrt(squaredDistance2))/2; - double variance = ((sqrt(squaredDistance1)-mean)*(sqrt(squaredDistance1)-mean)+(sqrt(squaredDistance2)-mean)*(sqrt(squaredDistance2)-mean))/2; - double sd = sqrt(variance); - double ms = 3.75/2; - double rms = sqrt(ms); - double min = sqrt(squaredDistance1); - double max = sqrt(squaredDistance2); - double median = (min + max)/2; - - mitk::PointSetDifferenceStatisticsCalculator::Pointer myPointSetDifferenceStatisticsCalculator = mitk::PointSetDifferenceStatisticsCalculator::New(testPointSet1,testPointSet2); - - MITK_TEST_CONDITION_REQUIRED((myPointSetDifferenceStatisticsCalculator->GetNumberOfPoints()==testPointSet1->GetSize()),".. Testing GetNumberOfPoints"); - MITK_TEST_CONDITION_REQUIRED(mitk::Equal(myPointSetDifferenceStatisticsCalculator->GetMean(),mean),".. Testing GetMean"); - MITK_TEST_CONDITION_REQUIRED(mitk::Equal(myPointSetDifferenceStatisticsCalculator->GetSD(),sd),".. Testing GetSD"); - MITK_TEST_CONDITION_REQUIRED(mitk::Equal(myPointSetDifferenceStatisticsCalculator->GetVariance(),variance),".. Testing GetVariance"); - MITK_TEST_CONDITION_REQUIRED(mitk::Equal(myPointSetDifferenceStatisticsCalculator->GetRMS(),rms),".. Testing GetRMS"); - MITK_TEST_CONDITION_REQUIRED(mitk::Equal(myPointSetDifferenceStatisticsCalculator->GetMin(),min),".. Testing GetMin"); - MITK_TEST_CONDITION_REQUIRED(mitk::Equal(myPointSetDifferenceStatisticsCalculator->GetMax(),max),".. Testing GetMax"); - MITK_TEST_CONDITION_REQUIRED(mitk::Equal(myPointSetDifferenceStatisticsCalculator->GetMedian(),median),".. Testing GetMedian"); - - testPointSet2->InsertPoint(2,test); - myPointSetDifferenceStatisticsCalculator->SetPointSets(testPointSet1,testPointSet2); - MITK_TEST_OUTPUT(<<"Test for exception when using differently sized point sets"); - MITK_TEST_FOR_EXCEPTION(itk::ExceptionObject,myPointSetDifferenceStatisticsCalculator->GetMean()); - mitk::PointSetDifferenceStatisticsCalculator::Pointer myPointSetDifferenceStatisticsCalculator2 = mitk::PointSetDifferenceStatisticsCalculator::New(); - MITK_TEST_OUTPUT(<<"Test for exception when using point sets with size 0"); - MITK_TEST_FOR_EXCEPTION(itk::ExceptionObject,myPointSetDifferenceStatisticsCalculator2->GetMean()); +void PointSetDifferenceStatisticsCalculator_DefaultConstructor_ResultIsNotNull() +{ + Setup(); + // let's create an object of our class + MITK_TEST_CONDITION_REQUIRED(m_myPointSetDifferenceStatisticsCalculator.IsNotNull(),"Testing instantiation with default constructor."); } -}; -int mitkPointSetDifferenceStatisticsCalculatorTest(int, char* []) +void PointSetDifferenceStatisticsCalculator_NonDefaultConstructor_ResultIsNotNull() { - // always start with this! - MITK_TEST_BEGIN("mitkPointSetDifferenceStatisticsCalculatorTest") + Setup(); + m_myPointSetDifferenceStatisticsCalculator = mitk::PointSetDifferenceStatisticsCalculator::New(m_myTestPointSet1,m_myTestPointSet2); + MITK_TEST_CONDITION_REQUIRED(m_myPointSetDifferenceStatisticsCalculator.IsNotNull(),"Testing instantiation with non default constructor."); +} - // let's create an object of our class - mitk::PointSetDifferenceStatisticsCalculator::Pointer myPointSetDifferenceStatisticsCalculator = mitk::PointSetDifferenceStatisticsCalculator::New(); - MITK_TEST_CONDITION_REQUIRED(myPointSetDifferenceStatisticsCalculator.IsNotNull(),"Testing instantiation with constructor 1."); +void PointSetDifferenceStatisticsCalculator_TwoSimplePointSetsOfSizeTwo_ResultsInGroundTruthValues() +{ - mitk::PointSet::Pointer myTestPointSet1 = mitk::PointSet::New(); - mitk::PointSet::Pointer myTestPointSet2 = mitk::PointSet::New(); - mitk::PointSetDifferenceStatisticsCalculator::Pointer myPointSetDifferenceStatisticsCalculator2 = mitk::PointSetDifferenceStatisticsCalculator::New(myTestPointSet1,myTestPointSet2); - MITK_TEST_CONDITION_REQUIRED(myPointSetDifferenceStatisticsCalculator2.IsNotNull(),"Testing instantiation with constructor 2."); + MITK_TEST_OUTPUT(<< "Starting simple test case..."); + + mitk::Point3D tmpPoint; + + //fill the point sets with simple test data + mitk::FillVector3D(tmpPoint,0,0,0); + m_myTestPointSet1->InsertPoint(0,tmpPoint); + mitk::FillVector3D(tmpPoint,1,1,1); + m_myTestPointSet1->InsertPoint(1,tmpPoint); + + mitk::FillVector3D(tmpPoint,0.5,0.5,0.5); + m_myTestPointSet2->InsertPoint(0,tmpPoint); + mitk::FillVector3D(tmpPoint,2,2,2); + m_myTestPointSet2->InsertPoint(1,tmpPoint); + + //Ground truth values (No logic in tests! Do not change values! :)) + double mean = 1.29904; // from (sqrt(0.75)+sqrt(3))/2; + double variance = 0.1875; // from ((sqrt(0.75)-mean)*(sqrt(0.75)-mean)+(sqrt(3)-mean)*(sqrt(3)-mean))/2; + double sd = 0.433013; //from sqrt(variance); + double rms = 1.36931; //from sqrt(3.75/2); + double min = 0.866025; //from sqrt(0.75); + double max = 1.73205; //from sqrt(3); + double median = 1.29904; //from (min + max)/2; + +m_myPointSetDifferenceStatisticsCalculator->SetPointSets( m_myTestPointSet1, m_myTestPointSet2); + + MITK_TEST_CONDITION_REQUIRED((m_myPointSetDifferenceStatisticsCalculator->GetNumberOfPoints()==m_myTestPointSet1->GetSize()),".. Testing GetNumberOfPoints"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetMean(),mean),".. Testing GetMean"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetSD(),sd),".. Testing GetSD"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetVariance(),variance),".. Testing GetVariance"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetRMS(),rms),".. Testing GetRMS"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetMin(),min),".. Testing GetMin"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetMax(),max),".. Testing GetMax"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetMedian(),median),".. Testing GetMedian"); +} - mitkPointSetDifferenceStatisticsCalculatorTestClass::TestInstantiation(); - mitkPointSetDifferenceStatisticsCalculatorTestClass::TestSimpleCase(); +void PointSetDifferenceStatisticsCalculator_PointSetsOfSameSizeWithDifferentPointIDs_ResultsInGroundTruth() +{ + Setup(); + + mitk::Point3D tmpPoint; + + //Fill the point sets with simple test data, but different point IDs + mitk::FillVector3D(tmpPoint,1,1,1); + m_myTestPointSet1->InsertPoint(2,tmpPoint); + m_myTestPointSet2->InsertPoint(0,tmpPoint); + mitk::FillVector3D(tmpPoint,3.5,4.9,2.1); //same point in both pointsets + m_myTestPointSet1->InsertPoint(17,tmpPoint); + m_myTestPointSet2->InsertPoint(522,tmpPoint); + + m_myPointSetDifferenceStatisticsCalculator->SetPointSets(m_myTestPointSet1, m_myTestPointSet2); + + //Compare results to ground truth which is 0.0, because the sets are identical + MITK_TEST_CONDITION_REQUIRED((m_myPointSetDifferenceStatisticsCalculator->GetNumberOfPoints()==m_myTestPointSet1->GetSize()),".. Testing GetNumberOfPoints"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetMean(),0.0),".. Testing GetMean"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetSD(),0.0),".. Testing GetSD"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetVariance(),0.0),".. Testing GetVariance"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetRMS(),0.0),".. Testing GetRMS"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetMin(),0.0),".. Testing GetMin"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetMax(),0.0),".. Testing GetMax"); + MITK_TEST_CONDITION_REQUIRED(mitk::Equal(m_myPointSetDifferenceStatisticsCalculator->GetMedian(),0.0),".. Testing GetMedian"); +} + +void PointSetDifferenceStatisticsCalculator_TwoPointSetsOfDifferentSize_ThrowsException() +{ + Setup(); + //One set with 2 points and one set with 1 point + mitk::Point3D tmpPoint; + tmpPoint.Fill(0); + m_myTestPointSet1->InsertPoint(0, tmpPoint); + m_myTestPointSet1->InsertPoint(1, tmpPoint); + + m_myTestPointSet2->InsertPoint(0, tmpPoint); + + m_myPointSetDifferenceStatisticsCalculator->SetPointSets(m_myTestPointSet1, m_myTestPointSet2); + MITK_TEST_FOR_EXCEPTION(itk::ExceptionObject,m_myPointSetDifferenceStatisticsCalculator->GetMean()); +} + +void PointSetDifferenceStatisticsCalculator_PointSetWithSizeZero_ThrowsException() +{ + Setup(); + m_myPointSetDifferenceStatisticsCalculator->SetPointSets(m_myTestPointSet1, m_myTestPointSet2); + MITK_TEST_FOR_EXCEPTION(itk::ExceptionObject,m_myPointSetDifferenceStatisticsCalculator->GetMean()); +} + +int mitkPointSetDifferenceStatisticsCalculatorTest(int, char* []) +{ + MITK_TEST_BEGIN("mitkPointSetDifferenceStatisticsCalculatorTest") + PointSetDifferenceStatisticsCalculator_DefaultConstructor_ResultIsNotNull(); + PointSetDifferenceStatisticsCalculator_NonDefaultConstructor_ResultIsNotNull(); + PointSetDifferenceStatisticsCalculator_TwoSimplePointSetsOfSizeTwo_ResultsInGroundTruthValues(); + PointSetDifferenceStatisticsCalculator_PointSetWithSizeZero_ThrowsException(); + PointSetDifferenceStatisticsCalculator_TwoPointSetsOfDifferentSize_ThrowsException(); + PointSetDifferenceStatisticsCalculator_PointSetsOfSameSizeWithDifferentPointIDs_ResultsInGroundTruth(); MITK_TEST_END() } diff --git a/Modules/ImageStatistics/mitkPointSetDifferenceStatisticsCalculator.cpp b/Modules/ImageStatistics/mitkPointSetDifferenceStatisticsCalculator.cpp index 2bdf1986a8..8d2173d8df 100644 --- a/Modules/ImageStatistics/mitkPointSetDifferenceStatisticsCalculator.cpp +++ b/Modules/ImageStatistics/mitkPointSetDifferenceStatisticsCalculator.cpp @@ -1,208 +1,214 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkPointSetDifferenceStatisticsCalculator.h" mitk::PointSetDifferenceStatisticsCalculator::PointSetDifferenceStatisticsCalculator() : m_StatisticsCalculated(false) { m_PointSet1 = mitk::PointSet::New(); m_PointSet2 = mitk::PointSet::New(); m_Statistics.Reset(); } mitk::PointSetDifferenceStatisticsCalculator::PointSetDifferenceStatisticsCalculator(mitk::PointSet::Pointer pSet1, mitk::PointSet::Pointer pSet2) { m_PointSet1 = pSet1; m_PointSet2 = pSet2; m_StatisticsCalculated = false; m_Statistics.Reset(); } mitk::PointSetDifferenceStatisticsCalculator::~PointSetDifferenceStatisticsCalculator() { } void mitk::PointSetDifferenceStatisticsCalculator::SetPointSets(mitk::PointSet::Pointer pSet1, mitk::PointSet::Pointer pSet2) { if (pSet1.IsNotNull()) { m_PointSet1 = pSet1; } if (pSet2.IsNotNull()) { m_PointSet2 = pSet2; } m_StatisticsCalculated = false; m_Statistics.Reset(); } std::vector mitk::PointSetDifferenceStatisticsCalculator::GetDifferences() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_DifferencesVector; } std::vector mitk::PointSetDifferenceStatisticsCalculator::GetSquaredDifferences() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_SquaredDifferencesVector; } double mitk::PointSetDifferenceStatisticsCalculator::GetMean() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_Statistics.Mean; } double mitk::PointSetDifferenceStatisticsCalculator::GetSD() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_Statistics.Sigma; } double mitk::PointSetDifferenceStatisticsCalculator::GetVariance() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_Statistics.Variance; } double mitk::PointSetDifferenceStatisticsCalculator::GetRMS() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_Statistics.RMS; } double mitk::PointSetDifferenceStatisticsCalculator::GetMedian() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_Statistics.Median; } double mitk::PointSetDifferenceStatisticsCalculator::GetMax() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_Statistics.Max; } double mitk::PointSetDifferenceStatisticsCalculator::GetMin() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_Statistics.Min; } double mitk::PointSetDifferenceStatisticsCalculator::GetNumberOfPoints() { if (!m_StatisticsCalculated) { this->ComputeStatistics(); } return m_Statistics.N; } void mitk::PointSetDifferenceStatisticsCalculator::ComputeStatistics() { if ((m_PointSet1.IsNull())||(m_PointSet2.IsNull())) { itkExceptionMacro("Point sets specified are not valid. Please specify correct Point sets"); } else if (m_PointSet1->GetSize()!=m_PointSet2->GetSize()) { itkExceptionMacro("PointSets are not equal. Please make sure that your PointSets have the same size and hold corresponding points."); } else if (m_PointSet1->GetSize()==0) { itkExceptionMacro("There are no points in the PointSets. Please make sure that the PointSets contain points"); } else { double mean = 0.0; double sd = 0.0; double rms= 0.0; std::vector differencesVector; mitk::Point3D point1; mitk::Point3D point2; int numberOfPoints = m_PointSet1->GetSize(); - for (int i=0; iEnd(); + for( mitk::PointSet::PointsIterator pointSetIterator = m_PointSet1->Begin(), pointSetIterator2 = m_PointSet2->Begin(); + pointSetIterator != end; ++pointSetIterator, ++pointSetIterator2) //iterate simultaneously over both sets { - point1 = m_PointSet1->GetPoint(i); - point2 = m_PointSet2->GetPoint(i); + point1 = pointSetIterator.Value(); + point2 = pointSetIterator2.Value(); + double squaredDistance = point1.SquaredEuclideanDistanceTo(point2); mean+=sqrt(squaredDistance); rms+=squaredDistance; this->m_SquaredDifferencesVector.push_back(squaredDistance); differencesVector.push_back(sqrt(squaredDistance)); } + m_DifferencesVector = differencesVector; mean = mean/numberOfPoints; rms = sqrt(rms/numberOfPoints); for (std::vector::size_type i=0; i