24 #include "vtkPolyDataWriter.h"
47 double dot = vtkMath::Dot(n, vec);
57 double dot2 = vtkMath::Dot(n, n2);
58 bool contoursParallel =
mitk::Equal(fabs(lengthLHS * lengthRHS), fabs(dot2), 0.001);
60 if (
mitk::Equal(dot, 0.0, 0.001) && contoursParallel)
73 contour->GetVtkPolyData()->GetPoints()->GetPoint(0, p);
74 vtkPolygon::ComputeNormal(contour->GetVtkPolyData()->GetPoints(), n);
81 : m_SelectedSegmentation(nullptr), m_CurrentTimeStep(0)
83 m_DistanceImageSpacing = 0.0;
89 m_ReduceFilter->SetUseProgressBar(
false);
91 m_NormalsFilter->SetUseProgressBar(
true);
92 m_NormalsFilter->SetProgressStepSize(1);
93 m_InterpolateSurfaceFilter->SetUseProgressBar(
true);
94 m_InterpolateSurfaceFilter->SetProgressStepSize(7);
100 m_PolyData->SetPoints(points);
102 m_InterpolationResult =
nullptr;
103 m_CurrentNumberOfReducedContours = 0;
109 auto dataIter = m_SegmentationObserverTags.begin();
110 for (; dataIter != m_SegmentationObserverTags.end(); ++dataIter)
112 (*dataIter).first->RemoveObserver((*dataIter).second);
114 m_SegmentationObserverTags.clear();
121 if (m_Instance.IsNull())
130 if (newContour->GetVtkPolyData()->GetNumberOfPoints() > 0)
133 this->AddToInterpolationPipeline(contourInfo);
141 for (
unsigned int i = 0; i < newContours.size(); ++i)
143 if (newContours.at(i)->GetVtkPolyData()->GetNumberOfPoints() > 0)
146 this->AddToInterpolationPipeline(contourInfo);
152 void mitk::SurfaceInterpolationController::AddToInterpolationPipeline(ContourPositionInformation contourInfo)
154 if (!m_SelectedSegmentation)
160 unsigned int numTimeSteps = m_SelectedSegmentation->GetTimeSteps();
161 if (m_CurrentTimeStep >= numTimeSteps)
163 MITK_ERROR <<
"Invalid time step requested for interpolation pipeline.";
167 ContourPositionInformationVec2D currentContours = m_ListOfInterpolationSessions[m_SelectedSegmentation];
168 ContourPositionInformationList currentContourList = currentContours[m_CurrentTimeStep];
171 for (
unsigned int i = 0; i < currentContourList.size(); i++)
173 ContourPositionInformation contourFromList = currentContourList.at(i);
182 if (pos == -1 && newContour->
GetVtkPolyData()->GetNumberOfPoints() > 0)
184 m_ReduceFilter->SetInput(m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].size(),
186 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].push_back(contourInfo);
188 else if (pos != -1 && newContour->
GetVtkPolyData()->GetNumberOfPoints() > 0)
190 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].at(pos) = contourInfo;
191 m_ReduceFilter->SetInput(pos, newContour);
195 this->RemoveContour(contourInfo);
201 if (!m_SelectedSegmentation)
206 unsigned int numTimeSteps = m_SelectedSegmentation->GetTimeSteps();
207 if (m_CurrentTimeStep >= numTimeSteps)
212 auto it = m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].begin();
213 while (it != m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].end())
218 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].erase(it);
219 this->ReinitializeInterpolation();
229 if (!m_SelectedSegmentation)
234 unsigned int numTimeSteps = m_SelectedSegmentation->
GetTimeSteps();
235 if (m_CurrentTimeStep >= numTimeSteps)
241 for (
unsigned int i = 0; i < contourList.size(); ++i)
252 if (!m_SelectedSegmentation)
257 unsigned int numTimeSteps = m_SelectedSegmentation->GetTimeSteps();
258 if (m_CurrentTimeStep >= numTimeSteps)
263 return m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].size();
268 m_ReduceFilter->Update();
270 m_CurrentNumberOfReducedContours = m_ReduceFilter->GetNumberOfOutputs();
271 if (m_CurrentNumberOfReducedContours == 1)
273 vtkPolyData *tmp = m_ReduceFilter->GetOutput(0)->GetVtkPolyData();
276 m_CurrentNumberOfReducedContours = 0;
281 timeSelector->SetInput(m_SelectedSegmentation);
282 timeSelector->SetTimeNr(m_CurrentTimeStep);
283 timeSelector->SetChannelNr(0);
284 timeSelector->Update();
287 m_NormalsFilter->SetSegmentationBinaryImage(refSegImage);
288 for (
unsigned int i = 0; i < m_CurrentNumberOfReducedContours; i++)
291 reducedContour->DisconnectPipeline();
292 m_NormalsFilter->SetInput(i, reducedContour);
293 m_InterpolateSurfaceFilter->SetInput(i, m_NormalsFilter->GetOutput(i));
296 if (m_CurrentNumberOfReducedContours < 2)
299 m_InterpolationResult =
nullptr;
308 imageToSurfaceFilter->SetInput(m_InterpolateSurfaceFilter->GetOutput());
309 imageToSurfaceFilter->SetThreshold(0);
310 imageToSurfaceFilter->SetSmooth(
true);
311 imageToSurfaceFilter->SetSmoothIteration(20);
312 imageToSurfaceFilter->Update();
315 interpolationResult->SetVtkPolyData(imageToSurfaceFilter->GetOutput()->GetVtkPolyData(), m_CurrentTimeStep);
316 m_InterpolationResult = interpolationResult;
318 m_DistanceImageSpacing = m_InterpolateSurfaceFilter->GetDistanceImageSpacing();
321 for (
unsigned int i = 0; i < m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].size(); i++)
323 polyDataAppender->AddInputData(
324 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].at(i).contour->GetVtkPolyData());
326 polyDataAppender->Update();
327 m_Contours->SetVtkPolyData(polyDataAppender->GetOutput());
332 m_InterpolationResult->DisconnectPipeline();
337 return m_InterpolationResult;
352 m_ReduceFilter->SetMinSpacing(minSpacing);
357 m_ReduceFilter->SetMaxSpacing(maxSpacing);
358 m_NormalsFilter->SetMaxSpacing(maxSpacing);
363 m_InterpolateSurfaceFilter->SetDistanceImageVolume(distImgVolume);
368 return m_SelectedSegmentation;
373 return m_InterpolateSurfaceFilter->GetOutput();
378 double numberOfPointsAfterReduction = m_ReduceFilter->GetNumberOfPointsAfterReduction() * 3;
379 double sizeOfPoints = pow(numberOfPointsAfterReduction, 2) *
sizeof(double);
381 double percentage = sizeOfPoints / totalMem;
387 return m_ListOfInterpolationSessions.size();
390 template <
typename TPixel,
unsigned int VImageDimension>
394 result->Graft(input);
399 this->SetCurrentInterpolationSession(segmentation);
404 if (currentSegmentationImage.GetPointer() == m_SelectedSegmentation)
407 if (currentSegmentationImage.IsNull())
409 m_SelectedSegmentation =
nullptr;
413 m_SelectedSegmentation = currentSegmentationImage.GetPointer();
415 auto it = m_ListOfInterpolationSessions.find(currentSegmentationImage.GetPointer());
418 if (it == m_ListOfInterpolationSessions.end())
421 m_ListOfInterpolationSessions.insert(
422 std::pair<mitk::Image *, ContourPositionInformationVec2D>(m_SelectedSegmentation, newList));
423 m_InterpolationResult =
nullptr;
424 m_CurrentNumberOfReducedContours = 0;
428 command->SetCallbackFunction(
this, &SurfaceInterpolationController::OnSegmentationDeleted);
429 m_SegmentationObserverTags.insert(std::pair<mitk::Image *, unsigned long>(
430 m_SelectedSegmentation, m_SelectedSegmentation->AddObserver(itk::DeleteEvent(), command)));
433 this->ReinitializeInterpolation();
439 if (oldSession.IsNull() || newSession.IsNull())
442 if (oldSession.GetPointer() == newSession.GetPointer())
448 auto it = m_ListOfInterpolationSessions.find(oldSession.GetPointer());
450 if (it == m_ListOfInterpolationSessions.end())
454 m_ListOfInterpolationSessions.insert(
455 std::pair<mitk::Image *, ContourPositionInformationVec2D>(newSession.GetPointer(), oldList));
458 command->SetCallbackFunction(
this, &SurfaceInterpolationController::OnSegmentationDeleted);
459 m_SegmentationObserverTags.insert(
460 std::pair<mitk::Image *, unsigned long>(newSession, newSession->AddObserver(itk::DeleteEvent(), command)));
462 if (m_SelectedSegmentation == oldSession)
463 m_SelectedSegmentation = newSession;
466 timeSelector->SetInput(m_SelectedSegmentation);
467 timeSelector->SetTimeNr(m_CurrentTimeStep);
468 timeSelector->SetChannelNr(0);
469 timeSelector->Update();
471 m_NormalsFilter->SetSegmentationBinaryImage(refSegImage);
473 this->RemoveInterpolationSession(oldSession);
479 this->RemoveInterpolationSession(segmentation);
484 if (segmentationImage)
486 if (m_SelectedSegmentation == segmentationImage)
488 m_NormalsFilter->SetSegmentationBinaryImage(
nullptr);
489 m_SelectedSegmentation =
nullptr;
491 m_ListOfInterpolationSessions.erase(segmentationImage);
493 auto pos = m_SegmentationObserverTags.find(segmentationImage);
494 if (pos != m_SegmentationObserverTags.end())
496 segmentationImage->RemoveObserver((*pos).second);
497 m_SegmentationObserverTags.erase(pos);
505 auto dataIter = m_SegmentationObserverTags.begin();
506 while (dataIter != m_SegmentationObserverTags.end())
509 image->RemoveObserver((*dataIter).second);
513 m_SegmentationObserverTags.clear();
514 m_SelectedSegmentation =
nullptr;
515 m_ListOfInterpolationSessions.clear();
525 vtkSmartPointer<vtkCellArray> existingPolys;
526 vtkSmartPointer<vtkPoints> existingPoints;
527 existingPolys = contours->GetVtkPolyData()->GetPolys();
528 existingPoints = contours->GetVtkPolyData()->GetPoints();
529 existingPolys->InitTraversal();
533 typedef std::pair<mitk::Vector3D, mitk::Point3D> PointNormalPair;
534 std::vector<ContourPositionInformation> list;
535 std::vector<vtkSmartPointer<vtkPoints>> pointsList;
537 for (existingPolys->InitTraversal(); existingPolys->GetNextCell(ids);)
541 existingPoints->GetPoints(ids, points);
543 pointsList.push_back(points);
547 vtkPolygon::ComputeNormal(points, n);
551 existingPoints->GetPoint(ids->GetId(0), p);
557 list.push_back(p_info);
562 auto outer = list.begin();
563 std::vector<std::vector<vtkSmartPointer<vtkPoints>>> relatedPoints;
564 while (outer != list.end())
568 std::vector<vtkSmartPointer<vtkPoints>> rel;
569 auto pointsIter = pointsList.begin();
570 rel.push_back((*pointsIter));
571 pointsIter = pointsList.erase(pointsIter);
573 while (inner != list.end())
577 inner = list.erase(inner);
578 rel.push_back((*pointsIter));
579 pointsIter = pointsList.erase(pointsIter);
587 relatedPoints.push_back(rel);
592 std::vector<mitk::Surface::Pointer> finalSurfaces;
593 for (
unsigned int i = 0; i < relatedPoints.size(); ++i)
598 unsigned int pointId(0);
599 for (
unsigned int j = 0; j < relatedPoints.at(i).size(); ++j)
601 unsigned int numPoints = relatedPoints.at(i).at(j)->GetNumberOfPoints();
603 polygon->GetPointIds()->SetNumberOfIds(numPoints);
604 polygon->GetPoints()->SetNumberOfPoints(numPoints);
605 vtkSmartPointer<vtkPoints> currentPoints = relatedPoints.at(i).at(j);
606 for (
unsigned k = 0; k < numPoints; ++k)
608 points->InsertPoint(pointId, currentPoints->GetPoint(k));
609 polygon->GetPointIds()->SetId(k, pointId);
612 polygons->InsertNextCell(polygon);
614 contourSurface->SetPoints(points);
615 contourSurface->SetPolys(polygons);
616 contourSurface->BuildLinks();
618 surface->SetVtkPolyData(contourSurface);
619 finalSurfaces.push_back(surface);
623 this->AddNewContours(finalSurfaces);
626 void mitk::SurfaceInterpolationController::OnSegmentationDeleted(
const itk::Object *caller,
627 const itk::EventObject & )
632 if (m_SelectedSegmentation == tempImage)
634 m_NormalsFilter->SetSegmentationBinaryImage(
nullptr);
635 m_SelectedSegmentation =
nullptr;
637 m_SegmentationObserverTags.erase(tempImage);
638 m_ListOfInterpolationSessions.erase(tempImage);
645 m_ReduceFilter->Reset();
646 m_NormalsFilter->Reset();
647 m_InterpolateSurfaceFilter->Reset();
651 if (m_SelectedSegmentation)
654 timeSelector->SetInput(m_SelectedSegmentation);
655 timeSelector->SetTimeNr(m_CurrentTimeStep);
656 timeSelector->SetChannelNr(0);
657 timeSelector->Update();
660 m_InterpolateSurfaceFilter->SetReferenceImage(itkImage.GetPointer());
662 unsigned int numTimeSteps = m_SelectedSegmentation->GetTimeSteps();
663 unsigned int size = m_ListOfInterpolationSessions[m_SelectedSegmentation].size();
664 if (size != numTimeSteps)
666 m_ListOfInterpolationSessions[m_SelectedSegmentation].resize(numTimeSteps);
669 if (m_CurrentTimeStep < numTimeSteps)
671 unsigned int numContours = m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].size();
672 for (
unsigned int c = 0; c < numContours; ++c)
674 m_ReduceFilter->SetInput(c,
675 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep][c].contour);
678 m_ReduceFilter->Update();
680 m_CurrentNumberOfReducedContours = m_ReduceFilter->GetNumberOfOutputs();
681 if (m_CurrentNumberOfReducedContours == 1)
683 vtkPolyData *tmp = m_ReduceFilter->GetOutput(0)->GetVtkPolyData();
686 m_CurrentNumberOfReducedContours = 0;
690 for (
unsigned int i = 0; i < m_CurrentNumberOfReducedContours; i++)
692 m_NormalsFilter->SetInput(i, m_ReduceFilter->GetOutput(i));
693 m_InterpolateSurfaceFilter->SetInput(i, m_NormalsFilter->GetOutput(i));
static vcl_size_t GetTotalSizeOfPhysicalRam()
void Progress(unsigned int steps=1)
Sets the current amount of progress to current progress + steps.
bool ReplaceInterpolationSession(mitk::Image::Pointer oldSession, mitk::Image::Pointer newSession)
void ReinitializeInterpolation(mitk::Surface::Pointer contours)
Reinitializes the interpolation using the provided contour data.
Class for storing surfaces (vtkPolyData).
double EstimatePortionOfNeededMemory()
itk::SmartPointer< Self > Pointer
mitk::SurfaceInterpolationController::ContourPositionInformation CreateContourPositionInformation(mitk::Surface::Pointer contour)
Surface * GetContoursAsSurface()
const mitk::Surface * GetContour(ContourPositionInformation contourInfo)
Returns the contour for a given plane for the current selected segmenation.
std::vector< ContourPositionInformationList > ContourPositionInformationVec2D
virtual vtkPolyData * GetVtkPolyData(unsigned int t=0) const
unsigned int GetNumberOfContours()
Returns the number of available contours for the current selected segmentation.
void RemoveSegmentationFromContourList(mitk::Image *segmentation)
SurfaceInterpolationController()
void SetMaxSpacing(double maxSpacing)
unsigned int GetNumberOfInterpolationSessions()
std::vector< ContourPositionInformation > ContourPositionInformationList
unsigned int GetTimeSteps() const
Get the number of time steps from the TimeGeometry As the base data has not a data vector given by it...
bool ContoursCoplanar(mitk::SurfaceInterpolationController::ContourPositionInformation leftHandSide, mitk::SurfaceInterpolationController::ContourPositionInformation rightHandSide)
~SurfaceInterpolationController()
static ProgressBar * GetInstance()
static method to get the GUI dependent ProgressBar-instance so the methods for steps to do and progre...
void RemoveAllInterpolationSessions()
Removes all sessions.
void SetCurrentInterpolationSession(mitk::Image::Pointer currentSegmentationImage)
void SetDataStorage(DataStorage::Pointer ds)
mitk::DataStorage::Pointer m_DataStorage
void GetImageBase(itk::Image< TPixel, VImageDimension > *input, itk::ImageBase< 3 >::Pointer &result)
Image class for storing images.
mitk::Image::Pointer GetCurrentSegmentation()
Get the current selected segmentation for which the interpolation is performed.
void AddNewContours(std::vector< Surface::Pointer > newContours)
Adds new extracted contours to the list. If one or more contours at a given position already exist th...
bool RemoveContour(ContourPositionInformation contourInfo)
Removes the contour for a given plane for the current selected segmenation.
void AddStepsToDo(unsigned int steps)
Adds steps to totalSteps.
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
#define AccessFixedDimensionByItk_1(mitkImage, itkImageTypeFunction, dimension, arg1)
static SurfaceInterpolationController * GetInstance()
MITKCORE_EXPORT const ScalarType eps
void SetMinSpacing(double minSpacing)
mitk::Surface::Pointer GetInterpolationResult()
void AddNewContour(Surface::Pointer newContour)
Adds a new extracted contour to the list.
void SetDistanceImageVolume(unsigned int distImageVolume)
void RemoveInterpolationSession(mitk::Image::Pointer segmentationImage)
Remove interpolation session.
void SetCurrentSegmentationInterpolationList(mitk::Image::Pointer segmentation)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.