diff --git a/Modules/SurfaceInterpolation/Testing/mitkPointCloudScoringFilterTest.cpp b/Modules/SurfaceInterpolation/Testing/mitkPointCloudScoringFilterTest.cpp index 7282fe2e2d..5243981cad 100644 --- a/Modules/SurfaceInterpolation/Testing/mitkPointCloudScoringFilterTest.cpp +++ b/Modules/SurfaceInterpolation/Testing/mitkPointCloudScoringFilterTest.cpp @@ -1,122 +1,129 @@ /*=================================================================== 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 "mitkTestingMacros.h" #include #include #include #include #include #include #include class mitkPointCloudScoringFilterTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkPointCloudScoringFilterTestSuite); MITK_TEST(testPointCloudScoringFilterInitialization); MITK_TEST(testInputs); MITK_TEST(testOutput); MITK_TEST(testScoring); CPPUNIT_TEST_SUITE_END(); private: mitk::UnstructuredGrid::Pointer m_UnstructuredGrid1; mitk::UnstructuredGrid::Pointer m_UnstructuredGrid2; public: void setUp() { m_UnstructuredGrid1 = mitk::UnstructuredGrid::New(); m_UnstructuredGrid2 = mitk::UnstructuredGrid::New(); vtkSmartPointer vtkGrid1 = vtkSmartPointer::New(); vtkSmartPointer vtkGrid2 = vtkSmartPointer::New(); vtkSmartPointer points1 = vtkSmartPointer::New(); vtkSmartPointer points2 = vtkSmartPointer::New(); for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { for(int k=0; k<3; k++) { mitk::Point3D point; point[0]=i; point[1]=j; point[2]=k; points1->InsertNextPoint(point[0],point[1],point[2]); points2->InsertNextPoint(point[0]+i,point[1]+i,point[2]+i); } } } vtkGrid1->SetPoints(points1); vtkGrid2->SetPoints(points2); m_UnstructuredGrid1->SetVtkUnstructuredGrid(vtkGrid1); m_UnstructuredGrid2->SetVtkUnstructuredGrid(vtkGrid2); } void testPointCloudScoringFilterInitialization() { mitk::PointCloudScoringFilter::Pointer testFilter = mitk::PointCloudScoringFilter::New(); CPPUNIT_ASSERT_MESSAGE("Testing instantiation of test object", testFilter.IsNotNull()); } void testInputs() { mitk::PointCloudScoringFilter::Pointer testFilter = mitk::PointCloudScoringFilter::New(); testFilter->SetInput(0, m_UnstructuredGrid1); testFilter->SetInput(1, m_UnstructuredGrid2); mitk::UnstructuredGrid::Pointer uGrid1 = dynamic_cast(testFilter->GetInputs().at(0).GetPointer()); mitk::UnstructuredGrid::Pointer uGrid2 = dynamic_cast(testFilter->GetInputs().at(1).GetPointer()); CPPUNIT_ASSERT_MESSAGE("Testing the first input", uGrid1 == m_UnstructuredGrid1); CPPUNIT_ASSERT_MESSAGE("Testing the second input", uGrid2 == m_UnstructuredGrid2); } void testOutput() { mitk::PointCloudScoringFilter::Pointer testFilter = mitk::PointCloudScoringFilter::New(); testFilter->SetInput(0, m_UnstructuredGrid1); testFilter->SetInput(1, m_UnstructuredGrid2); testFilter->Update(); - CPPUNIT_ASSERT_MESSAGE("Testing mitkUnstructuredGrid generation!", testFilter->GetOutput() != NULL); + + if(!testFilter->GetOutput()->GetVtkUnstructuredGrid()) + std::cout << "ITS EMPTY1!" << std::endl; + + CPPUNIT_ASSERT_MESSAGE("Testing mitkUnstructuredGrid generation!", testFilter->GetOutput()->GetVtkUnstructuredGrid() != NULL); } void testScoring() { mitk::PointCloudScoringFilter::Pointer testFilter = mitk::PointCloudScoringFilter::New(); - testFilter->SetInput(0, m_UnstructuredGrid2); - testFilter->SetInput(1, m_UnstructuredGrid1); + testFilter->SetInput(0, m_UnstructuredGrid1); + testFilter->SetInput(1, m_UnstructuredGrid2); testFilter->Update(); + if(!testFilter->GetOutput()->GetVtkUnstructuredGrid()) + std::cout << "ITS EMPTY2!" << std::endl; + mitk::UnstructuredGrid::Pointer outpGrid = testFilter->GetOutput(); int numBefore = m_UnstructuredGrid1->GetVtkUnstructuredGrid()->GetNumberOfPoints(); int numAfter = outpGrid->GetVtkUnstructuredGrid()->GetNumberOfPoints(); CPPUNIT_ASSERT_MESSAGE("Testing grid scoring", numBefore > numAfter); } }; MITK_TEST_SUITE_REGISTRATION(mitkPointCloudScoringFilter) diff --git a/Modules/SurfaceInterpolation/mitkPointCloudScoringFilter.cpp b/Modules/SurfaceInterpolation/mitkPointCloudScoringFilter.cpp index ba07f1ff6e..9379d6242a 100644 --- a/Modules/SurfaceInterpolation/mitkPointCloudScoringFilter.cpp +++ b/Modules/SurfaceInterpolation/mitkPointCloudScoringFilter.cpp @@ -1,134 +1,186 @@ /*=================================================================== 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 "mitkPointCloudScoringFilter.h" +#include + #include #include #include #include #include +#include +#include mitk::PointCloudScoringFilter::PointCloudScoringFilter(): m_NumberOfOutpPoints(0) { - m_OutpGrid = mitk::UnstructuredGrid::New(); - - this->SetNthOutput(0, m_OutpGrid); + this->SetNumberOfIndexedOutputs(1); } mitk::PointCloudScoringFilter::~PointCloudScoringFilter(){} void mitk::PointCloudScoringFilter::GenerateData() { if(UnstructuredGridToUnstructuredGridFilter::GetNumberOfInputs()!=2) { MITK_ERROR << "Not enough input arguments for PointCloudScoringFilter" << std::endl; return; } DataObjectPointerArray inputs = UnstructuredGridToUnstructuredGridFilter::GetInputs(); mitk::UnstructuredGrid::Pointer edgeGrid = dynamic_cast(inputs.at(0).GetPointer()); mitk::UnstructuredGrid::Pointer segmGrid = dynamic_cast(inputs.at(1).GetPointer()); if(edgeGrid->IsEmpty() || segmGrid->IsEmpty()) { if(edgeGrid->IsEmpty()) - MITK_ERROR << "Cannot convert edgeGrid into Surfaces" << std::endl; + MITK_ERROR << "edgeGrid is empty" << std::endl; if(segmGrid->IsEmpty()) - MITK_ERROR << "Cannot convert segmGrid into Surfaces" << std::endl; + MITK_ERROR << "segmGrid is empty" << std::endl; } + if(m_FilteredScores.size()>0) + m_FilteredScores.clear(); + vtkSmartPointer edgevtkGrid = edgeGrid->GetVtkUnstructuredGrid(); vtkSmartPointer segmvtkGrid = segmGrid->GetVtkUnstructuredGrid(); // KdTree from here vtkSmartPointer kdPoints = vtkSmartPointer::New(); vtkSmartPointer kdTree = vtkSmartPointer::New(); for(int i=0; iGetNumberOfPoints(); i++) { kdPoints->InsertNextPoint(edgevtkGrid->GetPoint(i)); } kdTree->BuildLocatorFromPoints(kdPoints); // KdTree until here vtkSmartPointer points = vtkSmartPointer::New(); for(int i=0; iGetNumberOfPoints(); i++) { points->InsertNextPoint(segmvtkGrid->GetPoint(i)); } std::vector< ScorePair > score; + std::vector< double > distances; double dist_glob = 0.0; double dist = 0.0; for(int i=0; iGetNumberOfPoints(); i++) { double point[3]; points->GetPoint(i,point); kdTree->FindClosestPoint(point[0],point[1],point[2],dist); dist_glob+=dist; + distances.push_back(dist); score.push_back(std::make_pair(i,dist)); } double avg = dist_glob / points->GetNumberOfPoints(); - for(unsigned int i=0; i avg) + tmpVar = tmpVar + ((distances.at(i) - avg) * (distances.at(i) - avg)); + if(distances.at(i)>highest) + highest = distances.at(i); + } + + //CUBIC MEAN + double cubicAll = 0.0; + for(unsigned i=0; i(score.size()); + double cubic = pow(root2,1.0/3.0); + //CUBIC END + + double metricValue = cubic; + + for(unsigned int i=0; i metricValue) { m_FilteredScores.push_back(std::make_pair(score.at(i).first,score.at(i).second)); } } m_NumberOfOutpPoints = m_FilteredScores.size(); vtkSmartPointer filteredPoints = vtkSmartPointer::New(); + //storing the distances in the uGrid PointData + vtkSmartPointer pointDataDistances = + vtkSmartPointer::New(); + pointDataDistances->SetNumberOfComponents(1); + pointDataDistances->SetNumberOfTuples(m_FilteredScores.size()); + pointDataDistances->SetName("Distances"); + for(unsigned int i=0; iGetPoint(m_FilteredScores.at(i).first); filteredPoints->InsertNextPoint(point[0],point[1],point[2]); + if(score.at(i).second > 0.001) + { + double dist[1] = {score.at(i).second}; + pointDataDistances->InsertTuple(i,dist); + } + else + { + double dist[1] = {0.0}; + pointDataDistances->InsertTuple(i,dist); + } } unsigned int numPoints = filteredPoints->GetNumberOfPoints(); vtkSmartPointer verts = vtkSmartPointer::New(); verts->GetPointIds()->SetNumberOfIds(numPoints); for(unsigned int i=0; iGetPointIds()->SetId(i,i); } vtkSmartPointer uGrid = vtkSmartPointer::New(); uGrid->Allocate(1); uGrid->InsertNextCell(verts->GetCellType(), verts->GetPointIds()); uGrid->SetPoints(filteredPoints); + uGrid->GetPointData()->AddArray(pointDataDistances); + + mitk::UnstructuredGrid::Pointer outputGrid = mitk::UnstructuredGrid::New(); + outputGrid->SetVtkUnstructuredGrid(uGrid); + this->SetNthOutput(0, outputGrid); - m_OutpGrid->SetVtkUnstructuredGrid(uGrid); + score.clear(); + distances.clear(); } void mitk::PointCloudScoringFilter::GenerateOutputInformation() { Superclass::GenerateOutputInformation(); } diff --git a/Modules/SurfaceInterpolation/mitkPointCloudScoringFilter.h b/Modules/SurfaceInterpolation/mitkPointCloudScoringFilter.h index 0bc3ba55cf..54aad00060 100644 --- a/Modules/SurfaceInterpolation/mitkPointCloudScoringFilter.h +++ b/Modules/SurfaceInterpolation/mitkPointCloudScoringFilter.h @@ -1,89 +1,84 @@ /*=================================================================== 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. ===================================================================*/ #ifndef mitkPointCloudScoringFilter_h_Included #define mitkPointCloudScoringFilter_h_Included #include #include namespace mitk { class UnstructuredGrid; /** * @brief Scores an UnstructuredGrid as good as one matches to the other. * * The result UnstructureGrid of the filter are the points where the distance to * the closest point of the other UnstructuredGrid is higher than the average * distance from all points to their closest neighbours of the other * UnstructuredGrid. * The second input is the UnstructuredGrid, which you want to score. All Points * of the output UnstructuredGrid are from the second input. */ class MITKSURFACEINTERPOLATION_EXPORT PointCloudScoringFilter: public UnstructuredGridToUnstructuredGridFilter { public: typedef std::pair ScorePair; mitkClassMacro( PointCloudScoringFilter, UnstructuredGridToUnstructuredGridFilter) itkFactorylessNewMacro(Self) - itkCloneMacro(Self) /** Number of Points of the scored UnstructuredGrid. These points are far away * from their neighbours */ itkGetMacro(NumberOfOutpPoints, int) /** A vector in which the point IDs and their distance to their neighbours * is stored */ itkGetMacro(FilteredScores, std::vector< ScorePair >) protected: /** is called by the Update() method */ virtual void GenerateData(); /** Defines the output */ virtual void GenerateOutputInformation(); /** Constructor */ PointCloudScoringFilter(); /** Destructor */ virtual ~PointCloudScoringFilter(); private: - /** The output UnstructuredGrid containing the scored points, which are far - * aways from their neighbours */ - mitk::UnstructuredGrid::Pointer m_OutpGrid; - /** The Point IDs and their distance to their neighbours */ std::vector< ScorePair > m_FilteredScores; /** The number of points which are far aways from their neighbours */ int m_NumberOfOutpPoints; }; } #endif