Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkCorrectorTool2D.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkCorrectorTool2D.h"
18 #include "mitkCorrectorAlgorithm.h"
19 
21 #include "mitkBaseRenderer.h"
22 #include "mitkImageReadAccessor.h"
23 #include "mitkLabelSetImage.h"
24 #include "mitkRenderingManager.h"
25 #include "mitkToolManager.h"
26 
27 #include "mitkCorrectorTool2D.xpm"
28 #include "mitkLabelSetImage.h"
29 
30 // us
31 #include <usGetModuleContext.h>
32 #include <usModule.h>
33 #include <usModuleContext.h>
34 #include <usModuleResource.h>
35 
36 namespace mitk
37 {
39 }
40 
42  : FeedbackContourTool("PressMoveRelease"), m_PaintingPixelValue(paintingPixelValue)
43 {
44  GetFeedbackContour()->SetClosed(false); // don't close the contour to a polygon
45 }
46 
48 {
49 }
50 
52 {
53  CONNECT_FUNCTION("PrimaryButtonPressed", OnMousePressed);
54  CONNECT_FUNCTION("Move", OnMouseMoved);
55  CONNECT_FUNCTION("Release", OnMouseReleased);
56 }
57 
58 const char **mitk::CorrectorTool2D::GetXPM() const
59 {
60  return mitkCorrectorTool2D_xpm;
61 }
62 
64 {
66  us::ModuleResource resource = module->GetResource("Correction_48x48.png");
67  return resource;
68 }
69 
71 {
73  us::ModuleResource resource = module->GetResource("Correction_Cursor_32x32.png");
74  return resource;
75 }
76 
77 const char *mitk::CorrectorTool2D::GetName() const
78 {
79  return "Correction";
80 }
81 
83 {
84  Superclass::Activated();
85 }
86 
88 {
89  Superclass::Deactivated();
90 }
91 
93 {
94  mitk::InteractionPositionEvent *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
95  if (!positionEvent)
96  return;
97 
98  m_LastEventSender = positionEvent->GetSender();
99  m_LastEventSlice = m_LastEventSender->GetSlice();
100 
101  int timestep = positionEvent->GetSender()->GetTimeStep();
103  contour->Initialize();
104  contour->Expand(timestep + 1);
105  contour->SetClosed(false, timestep);
106  mitk::Point3D point = positionEvent->GetPositionInWorld();
107  contour->AddVertex(point, timestep);
108 
110 }
111 
113 {
114  mitk::InteractionPositionEvent *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
115  if (!positionEvent)
116  return;
117 
118  int timestep = positionEvent->GetSender()->GetTimeStep();
120  mitk::Point3D point = positionEvent->GetPositionInWorld();
121  contour->AddVertex(point, timestep);
122 
123  assert(positionEvent->GetSender()->GetRenderWindow());
125 }
126 
128 {
129  // 1. Hide the feedback contour, find out which slice the user clicked, find out which slice of the toolmanager's
130  // working image corresponds to that
132 
133  mitk::InteractionPositionEvent *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
134  // const PositionEvent* positionEvent = dynamic_cast<const PositionEvent*>(stateEvent->GetEvent());
135  if (!positionEvent)
136  return;
137 
138  assert(positionEvent->GetSender()->GetRenderWindow());
140 
141  DataNode *workingNode(m_ToolManager->GetWorkingData(0));
142  if (!workingNode)
143  return;
144 
145  Image *image = dynamic_cast<Image *>(workingNode->GetData());
146  const PlaneGeometry *planeGeometry((positionEvent->GetSender()->GetCurrentWorldPlaneGeometry()));
147  if (!image || !planeGeometry)
148  return;
149 
150  const AbstractTransformGeometry *abstractTransformGeometry(
151  dynamic_cast<const AbstractTransformGeometry *>(positionEvent->GetSender()->GetCurrentWorldPlaneGeometry()));
152  if (!image || abstractTransformGeometry)
153  return;
154 
155  // 2. Slice is known, now we try to get it as a 2D image and project the contour into index coordinates of this slice
156  m_WorkingSlice = FeedbackContourTool::GetAffectedImageSliceAs2DImage(positionEvent, image);
157 
158  if (m_WorkingSlice.IsNull())
159  {
160  MITK_ERROR << "Unable to extract slice." << std::endl;
161  return;
162  }
163 
164  int timestep = positionEvent->GetSender()->GetTimeStep();
165  mitk::ContourModel::Pointer singleTimestepContour = mitk::ContourModel::New();
166 
169 
170  while (it != end)
171  {
172  singleTimestepContour->AddVertex((*it)->Coordinates);
173  it++;
174  }
175 
177  algorithm->SetInput(m_WorkingSlice);
178  algorithm->SetContour(singleTimestepContour);
179 
180  mitk::LabelSetImage::Pointer labelSetImage = dynamic_cast<LabelSetImage *>(workingNode->GetData());
181  int workingColorId(1);
182  if (labelSetImage.IsNotNull())
183  {
184  workingColorId = labelSetImage->GetActiveLabel()->GetValue();
185  algorithm->SetFillColor(workingColorId);
186  }
187  try
188  {
189  algorithm->UpdateLargestPossibleRegion();
190  }
191  catch (std::exception &e)
192  {
193  MITK_ERROR << "Caught exception '" << e.what() << "'" << std::endl;
194  }
195 
196  mitk::Image::Pointer resultSlice = mitk::Image::New();
197  resultSlice->Initialize(algorithm->GetOutput());
198 
199  if (labelSetImage.IsNotNull())
200  {
202  SegTool2D::WritePreviewOnWorkingImage(erg1, algorithm->GetOutput(), image, workingColorId, 0);
203  SegTool2D::WriteBackSegmentationResult(positionEvent, erg1);
204  }
205  else
206  {
207  mitk::ImageReadAccessor imAccess(algorithm->GetOutput());
208  resultSlice->SetVolume(imAccess.GetData());
209  this->WriteBackSegmentationResult(positionEvent, resultSlice);
210  }
211 }
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
Super class for all position events.
virtual unsigned int GetSlice() const
virtual void SetClosed(bool isClosed, int timestep=0)
Set closed property to given boolean.
BaseRenderer * GetSender() const
Image::Pointer GetAffectedImageSliceAs2DImage(const InteractionPositionEvent *positionEvent, const Image *image, unsigned int component=0)
Extract the slice of an image that the user just scribbles on. The given component denotes the vector...
virtual void Initialize() override
Initialize all data objects.
VertexIterator End(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
us::ModuleResource GetIconResource() const override
Returns the tool button icon of the tool wrapped by a usModuleResource.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
mitk::ContourElement::VertexIterator VertexIterator
#define MITKSEGMENTATION_EXPORT
virtual void OnMouseMoved(StateMachineAction *, InteractionEvent *)
DataCollection - Class to facilitate loading/accessing structured data.
#define MITK_TOOL_MACRO(EXPORT_SPEC, CLASS_NAME, DESCRIPTION)
virtual const PlaneGeometry * GetCurrentWorldPlaneGeometry()
Get the current 2D-worldgeometry (m_CurrentWorldPlaneGeometry) used for 2D-rendering.
static Pointer New()
void AddVertex(mitk::Point3D &vertex, int timestep=0)
Add a vertex to the contour at given timestep. The vertex is added at the end of contour.
virtual void OnMouseReleased(StateMachineAction *, InteractionEvent *)
virtual const char * GetName() const override
Returns the name of this tool. Make it short!
virtual us::ModuleResource GetCursorIconResource() const override
Returns the path of a cursor icon.
static RenderingManager * GetInstance()
Represents an action, that is executed after a certain event (in statemachine-mechanism) TODO: implem...
virtual void Expand(unsigned int timeSteps) override
Expand the timebounds of the TimeGeometry to given number of timesteps.
Module * GetModule() const
virtual void Activated() override
Called when the tool gets activated.
Image class for storing images.
Definition: mitkImage.h:76
void WriteBackSegmentationResult(const InteractionPositionEvent *, Image *)
CorrectorTool2D(int paintingPixelValue=1)
Describes a geometry defined by an vtkAbstractTransform and a plane.
Corrector tool for 2D binary segmentations.
static Pointer New()
virtual unsigned int GetTimeStep() const
void RequestUpdate(vtkRenderWindow *renderWindow)
Base class for tools that use a contour for feedback.
void WritePreviewOnWorkingImage(Image *targetSlice, Image *sourceSlice, Image *workingImage, int paintingPixelValue, int timestep)
LabelSetImage class for handling labels and layers in a segmentation session.
VertexIterator Begin(int timestep=0) const
Returns a const VertexIterator at the start element of the contour.
static Pointer New()
Describes a two-dimensional, rectangular plane.
#define CONNECT_FUNCTION(a, f)
virtual void OnMousePressed(StateMachineAction *, InteractionEvent *)
virtual const char ** GetXPM() const override
Returns an icon in the XPM format.
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
ImageReadAccessor class to get locked read access for a particular image part.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
vtkRenderWindow * GetRenderWindow() const
Access the RenderWindow into which this renderer renders.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
void ConnectActionsAndFunctions() override
virtual void Deactivated() override
Called when the tool gets deactivated.