20 #include "vtkPolyDataWriter.h" 43 double dot = vtkMath::Dot(n, vec);
53 double dot2 = vtkMath::Dot(n, n2);
54 bool contoursParallel =
mitk::Equal(fabs(lengthLHS * lengthRHS), fabs(dot2), 0.001);
56 if (
mitk::Equal(dot, 0.0, 0.001) && contoursParallel)
69 contour->GetVtkPolyData()->GetPoints()->GetPoint(0, p);
70 vtkPolygon::ComputeNormal(contour->GetVtkPolyData()->GetPoints(), n);
77 : m_SelectedSegmentation(nullptr), m_CurrentTimeStep(0)
79 m_DistanceImageSpacing = 0.0;
85 m_ReduceFilter->SetUseProgressBar(
false);
87 m_NormalsFilter->SetUseProgressBar(
true);
88 m_NormalsFilter->SetProgressStepSize(1);
89 m_InterpolateSurfaceFilter->SetUseProgressBar(
true);
90 m_InterpolateSurfaceFilter->SetProgressStepSize(7);
94 m_PolyData = vtkSmartPointer<vtkPolyData>::New();
95 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
96 m_PolyData->SetPoints(points);
98 m_InterpolationResult =
nullptr;
99 m_CurrentNumberOfReducedContours = 0;
105 auto dataIter = m_SegmentationObserverTags.begin();
106 for (; dataIter != m_SegmentationObserverTags.end(); ++dataIter)
108 (*dataIter).first->RemoveObserver((*dataIter).second);
110 m_SegmentationObserverTags.clear();
115 static mitk::SurfaceInterpolationController::Pointer m_Instance;
117 if (m_Instance.IsNull())
126 if (newContour->GetVtkPolyData()->GetNumberOfPoints() > 0)
129 this->AddToInterpolationPipeline(contourInfo);
137 for (
unsigned int i = 0; i < newContours.size(); ++i)
139 if (newContours.at(i)->GetVtkPolyData()->GetNumberOfPoints() > 0)
142 this->AddToInterpolationPipeline(contourInfo);
150 if (!m_SelectedSegmentation)
156 unsigned int numTimeSteps = m_SelectedSegmentation->
GetTimeSteps();
157 if (m_CurrentTimeStep >= numTimeSteps)
159 MITK_ERROR <<
"Invalid time step requested for interpolation pipeline.";
167 for (
unsigned int i = 0; i < currentContourList.size(); i++)
178 if (pos == -1 && newContour->
GetVtkPolyData()->GetNumberOfPoints() > 0)
180 m_ReduceFilter->SetInput(m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].size(),
182 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].push_back(contourInfo);
184 else if (pos != -1 && newContour->
GetVtkPolyData()->GetNumberOfPoints() > 0)
186 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].at(pos) = contourInfo;
187 m_ReduceFilter->SetInput(pos, newContour);
197 if (!m_SelectedSegmentation)
202 unsigned int numTimeSteps = m_SelectedSegmentation->
GetTimeSteps();
203 if (m_CurrentTimeStep >= numTimeSteps)
208 auto it = m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].begin();
209 while (it != m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].end())
214 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].erase(it);
225 if (!m_SelectedSegmentation)
230 unsigned int numTimeSteps = m_SelectedSegmentation->
GetTimeSteps();
231 if (m_CurrentTimeStep >= numTimeSteps)
237 for (
unsigned int i = 0; i < contourList.size(); ++i)
248 if (!m_SelectedSegmentation)
253 unsigned int numTimeSteps = m_SelectedSegmentation->
GetTimeSteps();
254 if (m_CurrentTimeStep >= numTimeSteps)
259 return m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].size();
264 m_ReduceFilter->Update();
266 m_CurrentNumberOfReducedContours = m_ReduceFilter->GetNumberOfOutputs();
267 if (m_CurrentNumberOfReducedContours == 1)
269 vtkPolyData *tmp = m_ReduceFilter->GetOutput(0)->GetVtkPolyData();
272 m_CurrentNumberOfReducedContours = 0;
277 timeSelector->SetInput(m_SelectedSegmentation);
278 timeSelector->SetTimeNr(m_CurrentTimeStep);
279 timeSelector->SetChannelNr(0);
280 timeSelector->Update();
283 m_NormalsFilter->SetSegmentationBinaryImage(refSegImage);
284 for (
unsigned int i = 0; i < m_CurrentNumberOfReducedContours; i++)
287 reducedContour->DisconnectPipeline();
288 m_NormalsFilter->SetInput(i, reducedContour);
289 m_InterpolateSurfaceFilter->SetInput(i, m_NormalsFilter->GetOutput(i));
292 if (m_CurrentNumberOfReducedContours < 2)
295 m_InterpolationResult =
nullptr;
304 imageToSurfaceFilter->SetInput(m_InterpolateSurfaceFilter->GetOutput());
305 imageToSurfaceFilter->SetThreshold(0);
306 imageToSurfaceFilter->SetSmooth(
true);
307 imageToSurfaceFilter->SetSmoothIteration(20);
308 imageToSurfaceFilter->Update();
311 interpolationResult->SetVtkPolyData(imageToSurfaceFilter->GetOutput()->GetVtkPolyData(), m_CurrentTimeStep);
312 m_InterpolationResult = interpolationResult;
314 m_DistanceImageSpacing = m_InterpolateSurfaceFilter->GetDistanceImageSpacing();
316 vtkSmartPointer<vtkAppendPolyData> polyDataAppender = vtkSmartPointer<vtkAppendPolyData>::New();
317 for (
unsigned int i = 0; i < m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].size(); i++)
319 polyDataAppender->AddInputData(
320 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].at(i).contour->GetVtkPolyData());
322 polyDataAppender->Update();
323 m_Contours->SetVtkPolyData(polyDataAppender->GetOutput());
328 m_InterpolationResult->DisconnectPipeline();
333 return m_InterpolationResult;
348 m_ReduceFilter->SetMinSpacing(minSpacing);
353 m_ReduceFilter->SetMaxSpacing(maxSpacing);
354 m_NormalsFilter->SetMaxSpacing(maxSpacing);
359 m_InterpolateSurfaceFilter->SetDistanceImageVolume(distImgVolume);
364 return m_SelectedSegmentation;
369 return m_InterpolateSurfaceFilter->GetOutput();
374 double numberOfPointsAfterReduction = m_ReduceFilter->GetNumberOfPointsAfterReduction() * 3;
375 double sizeOfPoints = pow(numberOfPointsAfterReduction, 2) *
sizeof(double);
377 double percentage = sizeOfPoints / totalMem;
383 return m_ListOfInterpolationSessions.size();
386 template <
typename TPixel,
unsigned int VImageDimension>
388 itk::ImageBase<3>::Pointer &result)
390 result->Graft(input);
400 if (currentSegmentationImage.GetPointer() == m_SelectedSegmentation)
403 if (currentSegmentationImage.IsNull())
405 m_SelectedSegmentation =
nullptr;
409 m_SelectedSegmentation = currentSegmentationImage.GetPointer();
411 auto it = m_ListOfInterpolationSessions.find(currentSegmentationImage.GetPointer());
414 if (it == m_ListOfInterpolationSessions.end())
417 m_ListOfInterpolationSessions.insert(
418 std::pair<mitk::Image *, ContourPositionInformationVec2D>(m_SelectedSegmentation, newList));
419 m_InterpolationResult =
nullptr;
420 m_CurrentNumberOfReducedContours = 0;
422 itk::MemberCommand<SurfaceInterpolationController>::Pointer command =
423 itk::MemberCommand<SurfaceInterpolationController>::New();
424 command->SetCallbackFunction(
this, &SurfaceInterpolationController::OnSegmentationDeleted);
425 m_SegmentationObserverTags.insert(std::pair<mitk::Image *, unsigned long>(
426 m_SelectedSegmentation, m_SelectedSegmentation->AddObserver(itk::DeleteEvent(), command)));
435 if (oldSession.IsNull() || newSession.IsNull())
438 if (oldSession.GetPointer() == newSession.GetPointer())
444 auto it = m_ListOfInterpolationSessions.find(oldSession.GetPointer());
446 if (it == m_ListOfInterpolationSessions.end())
450 m_ListOfInterpolationSessions.insert(
451 std::pair<mitk::Image *, ContourPositionInformationVec2D>(newSession.GetPointer(), oldList));
452 itk::MemberCommand<SurfaceInterpolationController>::Pointer command =
453 itk::MemberCommand<SurfaceInterpolationController>::New();
454 command->SetCallbackFunction(
this, &SurfaceInterpolationController::OnSegmentationDeleted);
455 m_SegmentationObserverTags.insert(
456 std::pair<mitk::Image *, unsigned long>(newSession, newSession->AddObserver(itk::DeleteEvent(), command)));
458 if (m_SelectedSegmentation == oldSession)
459 m_SelectedSegmentation = newSession;
462 timeSelector->SetInput(m_SelectedSegmentation);
463 timeSelector->SetTimeNr(m_CurrentTimeStep);
464 timeSelector->SetChannelNr(0);
465 timeSelector->Update();
467 m_NormalsFilter->SetSegmentationBinaryImage(refSegImage);
480 if (segmentationImage)
482 if (m_SelectedSegmentation == segmentationImage)
484 m_NormalsFilter->SetSegmentationBinaryImage(
nullptr);
485 m_SelectedSegmentation =
nullptr;
487 m_ListOfInterpolationSessions.erase(segmentationImage);
489 auto pos = m_SegmentationObserverTags.find(segmentationImage);
490 if (pos != m_SegmentationObserverTags.end())
492 segmentationImage->RemoveObserver((*pos).second);
493 m_SegmentationObserverTags.erase(pos);
501 auto dataIter = m_SegmentationObserverTags.begin();
502 while (dataIter != m_SegmentationObserverTags.end())
505 image->RemoveObserver((*dataIter).second);
509 m_SegmentationObserverTags.clear();
510 m_SelectedSegmentation =
nullptr;
511 m_ListOfInterpolationSessions.clear();
521 vtkSmartPointer<vtkCellArray> existingPolys;
522 vtkSmartPointer<vtkPoints> existingPoints;
523 existingPolys = contours->GetVtkPolyData()->GetPolys();
524 existingPoints = contours->GetVtkPolyData()->GetPoints();
525 existingPolys->InitTraversal();
527 vtkSmartPointer<vtkIdList> ids = vtkSmartPointer<vtkIdList>::New();
529 typedef std::pair<mitk::Vector3D, mitk::Point3D> PointNormalPair;
530 std::vector<ContourPositionInformation> list;
531 std::vector<vtkSmartPointer<vtkPoints>> pointsList;
533 for (existingPolys->InitTraversal(); existingPolys->GetNextCell(ids);)
536 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
537 existingPoints->GetPoints(ids, points);
539 pointsList.push_back(points);
543 vtkPolygon::ComputeNormal(points, n);
547 existingPoints->GetPoint(ids->GetId(0), p);
553 list.push_back(p_info);
558 auto outer = list.begin();
559 std::vector<std::vector<vtkSmartPointer<vtkPoints>>> relatedPoints;
560 while (outer != list.end())
564 std::vector<vtkSmartPointer<vtkPoints>> rel;
565 auto pointsIter = pointsList.begin();
566 rel.push_back((*pointsIter));
567 pointsIter = pointsList.erase(pointsIter);
569 while (inner != list.end())
573 inner = list.erase(inner);
574 rel.push_back((*pointsIter));
575 pointsIter = pointsList.erase(pointsIter);
583 relatedPoints.push_back(rel);
588 std::vector<mitk::Surface::Pointer> finalSurfaces;
589 for (
unsigned int i = 0; i < relatedPoints.size(); ++i)
591 vtkSmartPointer<vtkPolyData> contourSurface = vtkSmartPointer<vtkPolyData>::New();
592 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
593 vtkSmartPointer<vtkCellArray> polygons = vtkSmartPointer<vtkCellArray>::New();
594 unsigned int pointId(0);
595 for (
unsigned int j = 0; j < relatedPoints.at(i).size(); ++j)
597 unsigned int numPoints = relatedPoints.at(i).at(j)->GetNumberOfPoints();
598 vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
599 polygon->GetPointIds()->SetNumberOfIds(numPoints);
600 polygon->GetPoints()->SetNumberOfPoints(numPoints);
601 vtkSmartPointer<vtkPoints> currentPoints = relatedPoints.at(i).at(j);
602 for (
unsigned k = 0;
k < numPoints; ++
k)
604 points->InsertPoint(pointId, currentPoints->GetPoint(
k));
605 polygon->GetPointIds()->SetId(
k, pointId);
608 polygons->InsertNextCell(polygon);
610 contourSurface->SetPoints(points);
611 contourSurface->SetPolys(polygons);
612 contourSurface->BuildLinks();
614 surface->SetVtkPolyData(contourSurface);
615 finalSurfaces.push_back(surface);
622 void mitk::SurfaceInterpolationController::OnSegmentationDeleted(
const itk::Object *caller,
623 const itk::EventObject & )
625 auto *tempImage =
dynamic_cast<mitk::Image *
>(
const_cast<itk::Object *
>(caller));
628 if (m_SelectedSegmentation == tempImage)
630 m_NormalsFilter->SetSegmentationBinaryImage(
nullptr);
631 m_SelectedSegmentation =
nullptr;
633 m_SegmentationObserverTags.erase(tempImage);
634 m_ListOfInterpolationSessions.erase(tempImage);
641 m_ReduceFilter->Reset();
642 m_NormalsFilter->Reset();
643 m_InterpolateSurfaceFilter->Reset();
645 itk::ImageBase<3>::Pointer itkImage = itk::ImageBase<3>::New();
647 if (m_SelectedSegmentation)
650 timeSelector->SetInput(m_SelectedSegmentation);
651 timeSelector->SetTimeNr(m_CurrentTimeStep);
652 timeSelector->SetChannelNr(0);
653 timeSelector->Update();
656 m_InterpolateSurfaceFilter->SetReferenceImage(itkImage.GetPointer());
658 unsigned int numTimeSteps = m_SelectedSegmentation->
GetTimeSteps();
659 unsigned int size = m_ListOfInterpolationSessions[m_SelectedSegmentation].size();
660 if (size != numTimeSteps)
662 m_ListOfInterpolationSessions[m_SelectedSegmentation].resize(numTimeSteps);
665 if (m_CurrentTimeStep < numTimeSteps)
667 unsigned int numContours = m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep].size();
668 for (
unsigned int c = 0; c < numContours; ++c)
670 m_ReduceFilter->SetInput(c,
671 m_ListOfInterpolationSessions[m_SelectedSegmentation][m_CurrentTimeStep][c].contour);
674 m_ReduceFilter->Update();
676 m_CurrentNumberOfReducedContours = m_ReduceFilter->GetNumberOfOutputs();
677 if (m_CurrentNumberOfReducedContours == 1)
679 vtkPolyData *tmp = m_ReduceFilter->GetOutput(0)->GetVtkPolyData();
682 m_CurrentNumberOfReducedContours = 0;
686 for (
unsigned int i = 0; i < m_CurrentNumberOfReducedContours; i++)
688 m_NormalsFilter->SetInput(i, m_ReduceFilter->GetOutput(i));
689 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()
mitk::SurfaceInterpolationController::ContourPositionInformation CreateContourPositionInformation(mitk::Surface::Pointer contour)
virtual vtkPolyData * GetVtkPolyData(unsigned int t=0) const
Surface * GetContoursAsSurface()
const mitk::Surface * GetContour(ContourPositionInformation contourInfo)
Returns the contour for a given plane for the current selected segmenation.
std::vector< ContourPositionInformationList > ContourPositionInformationVec2D
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
bool ContoursCoplanar(mitk::SurfaceInterpolationController::ContourPositionInformation leftHandSide, mitk::SurfaceInterpolationController::ContourPositionInformation rightHandSide)
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)
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...
mitk::Image::Pointer image
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
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...
~SurfaceInterpolationController() override
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)