diff --git a/Modules/ImageStatistics/Testing/mitkPointSetDifferenceStatisticsCalculatorTest.cpp b/Modules/ImageStatistics/Testing/mitkPointSetDifferenceStatisticsCalculatorTest.cpp index 2e528baaf2..e331ac17e3 100644 --- a/Modules/ImageStatistics/Testing/mitkPointSetDifferenceStatisticsCalculatorTest.cpp +++ b/Modules/ImageStatistics/Testing/mitkPointSetDifferenceStatisticsCalculatorTest.cpp @@ -1,107 +1,114 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2008-02-25 17:27:17 +0100 (Mo, 25 Feb 2008) $ Version: $Revision: 7837 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #include "mitkStandardFileLocations.h" #include "mitkTestingMacros.h" #include "mitkPointSetDifferenceStatisticsCalculator.h" //#include /** * \brief Test class for mitkPointSetDifferenceStatisticsCalculator */ class mitkPointSetDifferenceStatisticsCalculatorTestClass { 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()); +} }; int mitkPointSetDifferenceStatisticsCalculatorTest(int argc, char* argv[]) { // always start with this! MITK_TEST_BEGIN("mitkPointSetDifferenceStatisticsCalculatorTest") // 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."); mitkPointSetDifferenceStatisticsCalculatorTestClass::TestInstantiation(); mitkPointSetDifferenceStatisticsCalculatorTestClass::TestSimpleCase(); MITK_TEST_END() } diff --git a/Modules/ImageStatistics/mitkPointSetDifferenceStatisticsCalculator.cpp b/Modules/ImageStatistics/mitkPointSetDifferenceStatisticsCalculator.cpp index 38ef679831..cb0cc5a9ea 100644 --- a/Modules/ImageStatistics/mitkPointSetDifferenceStatisticsCalculator.cpp +++ b/Modules/ImageStatistics/mitkPointSetDifferenceStatisticsCalculator.cpp @@ -1,192 +1,211 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-05-12 19:56:03 +0200 (Di, 12 Mai 2009) $ Version: $Revision: 17179 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #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() { - 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; iGetPoint(i); - point2 = m_PointSet2->GetPoint(i); - 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 (int i=0; iGetSize()!=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 { - median = differencesVector.at((numberOfPoints-1)/2+1); + 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; iGetPoint(i); + point2 = m_PointSet2->GetPoint(i); + 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 (int i=0; i