diff --git a/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.cpp b/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.cpp index cccc2b4b89..d70f1ebbd9 100644 --- a/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.cpp +++ b/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.cpp @@ -1,107 +1,170 @@ /*=================================================================== 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) +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 clusterVector; + 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++) { - visited[i] = true; //mark P as visited - vtkSmartPointer idList = vtkSmartPointer::New(); - pLocator->FindPointsWithinRadius(m_eps, inpPoints->GetPoint(i), idList); //find neighbours - if(idList->GetNumberOfIds() < m_MinPts) + if(!visited[i]) + { + visited[i] = true; //mark P as visited + vtkSmartPointer idList = vtkSmartPointer::New(); //represent N + pLocator->FindPointsWithinRadius(m_eps, inpPoints->GetPoint(i), idList); //find neighbours + if(idList->GetNumberOfIds() < m_MinPts) + { + isNoise[i] = true;//point is noise - maybe just skip it? + } + else + { + vtkSmartPointer cluster = vtkSmartPointer::New(); + clusterVector.push_back(cluster); //next Cluster + this->ExpandCluster(i,idList,cluster,inpPoints); + } + } + } + + //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++) { - isNoise[i] = true;//point is noise - maybe just skip it? + double point[3]; + points->GetPoint(j,point); + std::cout << "POINT: " << point[0] << " " << point[1] << " " << point[2] << std::endl; } - else + if(points->GetNumberOfPoints() > numberOfClusterPoints) { - vtkSmartPointer cluster = vtkSmartPointer::New(); - clusterVector.push_back(cluster); - this->ExpandCluster(i,idList,cluster,inpPoints); + numberOfClusterPoints = points->GetNumberOfPoints(); + IdOfBiggestCluster = i; } } - m_UnstructGrid = this->GetOutput(); + 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 Point to Cluster + clusterMember[id] = true; //right? - vtkSmartPointer neighbours = vtkSmartPointer::New(); + vtkSmartPointer neighbours = vtkSmartPointer::New(); //same N as top inpPoints->GetPoints(pointIDs,neighbours); for(int i=0; iGetNumberOfIds();i++) { - if(!visited[pointIDs->GetId(i)]) //if P is not visited + if(!visited[pointIDs->GetId(i)]) //if P' is not visited { - visited[pointIDs->GetId(i)] = true; // mark P as visited - vtkSmartPointer idList = vtkSmartPointer::New(); + 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); //find neighbours if(idList->GetNumberOfIds() >= m_MinPts) { for(int j=0; jGetNumberOfIds();j++)//join every point in range to the points { pointIDs->InsertNextId(idList->GetId(j)); } } } - if(!clusterMember[pointIDs->GetId(i)]) //P ist not yet member of any cluster + if(!clusterMember[pointIDs->GetId(i)]) //P' ist not yet member of any cluster { clusterMember[pointIDs->GetId(i)] = true; - cluster->InsertNextPoint(inpPoints->GetPoint(pointIDs->GetId(i))); + cluster->InsertNextPoint(inpPoints->GetPoint(pointIDs->GetId(i))); //add P' to cluster C } } } diff --git a/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.h b/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.h index 942ed72f88..68d1166d7c 100644 --- a/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.h +++ b/Modules/AlgorithmsExt/mitkUnstructuredGridClusteringFilter.h @@ -1,99 +1,110 @@ /*=================================================================== 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) + +// itkGetMacro(Clusters, std::vector) + 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__