diff --git a/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.cpp b/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.cpp index 978f10cb07..818a53a7d5 100644 --- a/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.cpp +++ b/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.cpp @@ -1,214 +1,220 @@ /*=================================================================== 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 #include #include #include #include #include #include mitk::UnstructuredGridClusteringFilter::UnstructuredGridClusteringFilter() : m_eps(0.0), m_MinPts(0), m_Meshing(true) { this->m_UnstructGrid = mitk::UnstructuredGrid::New(); } mitk::UnstructuredGridClusteringFilter::~UnstructuredGridClusteringFilter(){} std::map visited; std::map isNoise; std::map clusterMember; vtkSmartPointer pLocator; void mitk::UnstructuredGridClusteringFilter::GenerateOutputInformation() { m_UnstructGrid = this->GetOutput(); } void mitk::UnstructuredGridClusteringFilter::GenerateData() { mitk::UnstructuredGrid::Pointer inputGrid = const_cast(this->GetInput()); if(inputGrid.IsNull()) return; vtkSmartPointer vtkInpGrid = inputGrid->GetVtkUnstructuredGrid(); vtkSmartPointer inpPoints = vtkInpGrid->GetPoints(); pLocator =vtkSmartPointer::New(); std::vector< vtkSmartPointer > clusterVector; pLocator->SetDataSet(vtkInpGrid); pLocator->AutomaticOn(); pLocator->SetNumberOfPointsPerBucket(2); pLocator->BuildLocator(); //fill the visited map with false for checking for(int i=0; iGetNumberOfPoints();i++) { visited[i] = false; isNoise[i] = false; clusterMember[i] = false; } for(int i=0; iGetNumberOfPoints();i++) { if(!visited[i]) { visited[i] = true; //mark P as visited vtkSmartPointer idList = vtkSmartPointer::New(); //represent N pLocator->FindPointsWithinRadius(m_eps, inpPoints->GetPoint(i), idList); //N = D.regionQuery(P, eps) if(idList->GetNumberOfIds() < m_MinPts) //if sizeof(N) < MinPts { isNoise[i] = true; //mark P as NOISE } else { vtkSmartPointer cluster = vtkSmartPointer::New(); //represent a cluster clusterVector.push_back(cluster); //C = next cluster this->ExpandCluster(i,idList,cluster,inpPoints); //expandCluster(P, N, C, eps, MinPts) mod. the parameter list } } } //OUTPUT LOGIC m_Clusters = clusterVector; int numberOfClusterPoints = 0; int IdOfBiggestCluster = 0; for(unsigned int i=0; i points = m_Clusters.at(i); std::cout << "CLUSTER " << i << ": " << points->GetNumberOfPoints() << std::endl; for(int j=0; jGetNumberOfPoints();j++) { double point[3]; points->GetPoint(j,point); std::cout << "POINT: " << point[0] << " " << point[1] << " " << point[2] << std::endl; } if(points->GetNumberOfPoints() > numberOfClusterPoints) { numberOfClusterPoints = points->GetNumberOfPoints(); IdOfBiggestCluster = i; } } vtkSmartPointer biggestCluster = vtkSmartPointer::New(); vtkSmartPointer points = vtkSmartPointer::New(); points = m_Clusters.at(IdOfBiggestCluster); vtkSmartPointer verts = vtkSmartPointer::New(); verts->GetPointIds()->SetNumberOfIds(m_Clusters.at(IdOfBiggestCluster)->GetNumberOfPoints()); for(int i=0; iGetNumberOfPoints(); i++) { verts->GetPointIds()->SetId(i,i); } biggestCluster->Allocate(1); biggestCluster->InsertNextCell(verts->GetCellType(), verts->GetPointIds()); biggestCluster->SetPoints(m_Clusters.at(IdOfBiggestCluster)); if(m_Meshing) { vtkSmartPointer mesher = vtkSmartPointer::New(); mesher->SetInputData(biggestCluster); mesher->SetAlpha(0.9); mesher->Update(); vtkSmartPointer output = mesher->GetOutput(); m_UnstructGrid->SetVtkUnstructuredGrid(output); } else { m_UnstructGrid->SetVtkUnstructuredGrid(biggestCluster); } } void mitk::UnstructuredGridClusteringFilter::ExpandCluster(int id, vtkIdList *pointIDs, vtkPoints* cluster, vtkPoints* inpPoints) { cluster->InsertNextPoint(inpPoints->GetPoint(id)); //add P to cluster C clusterMember[id] = true; //right? vtkSmartPointer neighbours = vtkSmartPointer::New(); //same N as in other function inpPoints->GetPoints(pointIDs,neighbours); for(int i=0; iGetNumberOfIds();i++) //for each point P' in N { if(!visited[pointIDs->GetId(i)]) //if P' is not visited { visited[pointIDs->GetId(i)] = true; //mark P' as visited vtkSmartPointer idList = vtkSmartPointer::New(); //represent N' pLocator->FindPointsWithinRadius(m_eps, inpPoints->GetPoint(pointIDs->GetId(i)), idList); //N' = D.regionQuery(P', eps) if(idList->GetNumberOfIds() >= m_MinPts) //if sizeof(N') >= MinPts { for(int j=0; jGetNumberOfIds();j++) //N = N joined with N' { pointIDs->InsertNextId(idList->GetId(j)); } } } if(!clusterMember[pointIDs->GetId(i)]) //if P' is not yet member of any cluster { clusterMember[pointIDs->GetId(i)] = true; cluster->InsertNextPoint(inpPoints->GetPoint(pointIDs->GetId(i))); //add P' to cluster C } } } std::vector mitk::UnstructuredGridClusteringFilter::GetAllClusters() { std::vector< mitk::UnstructuredGrid::Pointer > mitkUGridVector; - for(int i=0; i cluster = vtkSmartPointer::New(); vtkSmartPointer points = m_Clusters.at(i); vtkSmartPointer verts = vtkSmartPointer::New(); verts->GetPointIds()->SetNumberOfIds(points->GetNumberOfPoints()); for(int i=0; iGetNumberOfPoints(); i++) { verts->GetPointIds()->SetId(i,i); } cluster->Allocate(1); cluster->InsertNextCell(verts->GetCellType(), verts->GetPointIds()); cluster->SetPoints(points); mitk::UnstructuredGrid::Pointer mitkGrid = mitk::UnstructuredGrid::New(); if(m_Meshing) { vtkSmartPointer mesher = vtkSmartPointer::New(); mesher->SetInputData(cluster); mesher->SetAlpha(0.9); mesher->Update(); vtkSmartPointer output = mesher->GetOutput(); mitkGrid->SetVtkUnstructuredGrid(output); } else { mitkGrid->SetVtkUnstructuredGrid(cluster); } mitkUGridVector.push_back(mitkGrid); - return mitkUGridVector; } + + return mitkUGridVector; +} + +int mitk::UnstructuredGridClusteringFilter::GetNumberOfFoundClusters() +{ + return m_Clusters.size(); } diff --git a/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.h b/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.h index 50908b19f3..606c18ca99 100644 --- a/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.h +++ b/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.h @@ -1,110 +1,112 @@ /*=================================================================== 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 _MITKUNSTRUCTUREDGRIDCLUSTERINGFILTER_h__ #define _MITKUNSTRUCTUREDGRIDCLUSTERINGFILTER_h__ #include #include #include #include #include #include #include namespace mitk { /** * @brief The UnstructuredGridClusteringFilter class * * DBSCAN algorithm: * * DBSCAN(D, eps, MinPts) * C = 0 * for each unvisited point P in dataset D * mark P as visited * N = D.regionQuery(P, eps) * if sizeof(N) < MinPts * mark P as NOISE * else * C = next cluster * expandCluster(P, N, C, eps, MinPts) * * expandCluster(P, N, C, eps, MinPts) * add P to cluster C * for each point P' in N * if P' is not visited * mark P' as visited * N' = D.regionQuery(P', eps) * if sizeof(N') >= MinPts * N = N joined with N' * if P' is not yet member of any cluster * add P' to cluster C */ class MitkAlgorithmsExt_EXPORT UnstructuredGridClusteringFilter : public UnstructuredGridToUnstructuredGridFilter { public: mitkClassMacro(UnstructuredGridClusteringFilter, UnstructuredGridToUnstructuredGridFilter) itkFactorylessNewMacro(Self) itkCloneMacro(Self) itkSetMacro(eps, double) itkGetMacro(eps, double) itkSetMacro(MinPts, int) itkGetMacro(MinPts, int) itkSetMacro(Meshing, bool) virtual std::vector< mitk::UnstructuredGrid::Pointer> GetAllClusters(); + virtual int GetNumberOfFoundClusters(); + virtual void GenerateOutputInformation(); virtual void GenerateData(); protected: UnstructuredGridClusteringFilter(); virtual ~UnstructuredGridClusteringFilter(); private: void ExpandCluster(int id, vtkIdList* pointIDs, vtkPoints* cluster, vtkPoints *inpPoints); mitk::UnstructuredGrid::Pointer m_UnstructGrid; std::vector< vtkSmartPointer > m_Clusters; double m_eps; int m_MinPts; bool m_Meshing; }; } // namespace mitk #endif //_MITKUNSTRUCTUREDGRIDCLUSTERINGFILTER_h__