Medical Imaging Interaction Toolkit  2018.4.99-b20efe7f
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkCorrectorTool2D.h"
14 #include "mitkCorrectorAlgorithm.h"
15 
17 #include "mitkBaseRenderer.h"
18 #include "mitkImageReadAccessor.h"
19 #include "mitkLabelSetImage.h"
20 #include "mitkRenderingManager.h"
21 #include "mitkToolManager.h"
22 
23 #include "mitkCorrectorTool2D.xpm"
24 #include "mitkLabelSetImage.h"
25 
26 // us
27 #include <usGetModuleContext.h>
28 #include <usModule.h>
29 #include <usModuleContext.h>
30 #include <usModuleResource.h>
31 
32 namespace mitk
33 {
35 }
36 
38  : FeedbackContourTool("PressMoveRelease"), m_PaintingPixelValue(paintingPixelValue)
39 {
40  GetFeedbackContour()->SetClosed(false); // don't close the contour to a polygon
41 }
42 
44 {
45 }
46 
48 {
49  CONNECT_FUNCTION("PrimaryButtonPressed", OnMousePressed);
52 }
53 
54 const char **mitk::CorrectorTool2D::GetXPM() const
55 {
56  return mitkCorrectorTool2D_xpm;
57 }
58 
60 {
62  us::ModuleResource resource = module->GetResource("Correction_48x48.png");
63  return resource;
64 }
65 
67 {
69  us::ModuleResource resource = module->GetResource("Correction_Cursor_32x32.png");
70  return resource;
71 }
72 
73 const char *mitk::CorrectorTool2D::GetName() const
74 {
75  return "Correction";
76 }
77 
79 {
80  Superclass::Activated();
81 }
82 
84 {
85  Superclass::Deactivated();
86 }
87 
89 {
90  auto *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
91  if (!positionEvent)
92  return;
93 
94  m_LastEventSender = positionEvent->GetSender();
96 
97  int timestep = positionEvent->GetSender()->GetTimeStep();
99  contour->Initialize();
100  contour->Expand(timestep + 1);
101  contour->SetClosed(false, timestep);
102  mitk::Point3D point = positionEvent->GetPositionInWorld();
103  contour->AddVertex(point, timestep);
104 
106 }
107 
109 {
110  auto *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
111  if (!positionEvent)
112  return;
113 
114  int timestep = positionEvent->GetSender()->GetTimeStep();
116  mitk::Point3D point = positionEvent->GetPositionInWorld();
117  contour->AddVertex(point, timestep);
118 
119  assert(positionEvent->GetSender()->GetRenderWindow());
120  mitk::RenderingManager::GetInstance()->RequestUpdate(positionEvent->GetSender()->GetRenderWindow());
121 }
122 
124 {
125  // 1. Hide the feedback contour, find out which slice the user clicked, find out which slice of the toolmanager's
126  // working image corresponds to that
128 
129  auto *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
130  // const PositionEvent* positionEvent = dynamic_cast<const PositionEvent*>(stateEvent->GetEvent());
131  if (!positionEvent)
132  return;
133 
134  assert(positionEvent->GetSender()->GetRenderWindow());
135  mitk::RenderingManager::GetInstance()->RequestUpdate(positionEvent->GetSender()->GetRenderWindow());
136 
137  DataNode *workingNode(m_ToolManager->GetWorkingData(0));
138  if (!workingNode)
139  return;
140 
141  auto *image = dynamic_cast<Image *>(workingNode->GetData());
142  const PlaneGeometry *planeGeometry((positionEvent->GetSender()->GetCurrentWorldPlaneGeometry()));
143  if (!image || !planeGeometry)
144  return;
145 
146  const auto *abstractTransformGeometry(
147  dynamic_cast<const AbstractTransformGeometry *>(positionEvent->GetSender()->GetCurrentWorldPlaneGeometry()));
148  if (!image || abstractTransformGeometry)
149  return;
150 
151  // 2. Slice is known, now we try to get it as a 2D image and project the contour into index coordinates of this slice
153 
154  if (m_WorkingSlice.IsNull())
155  {
156  MITK_ERROR << "Unable to extract slice." << std::endl;
157  return;
158  }
159 
160  int timestep = positionEvent->GetSender()->GetTimeStep();
161  mitk::ContourModel::Pointer singleTimestepContour = mitk::ContourModel::New();
162 
163  auto it = FeedbackContourTool::GetFeedbackContour()->Begin(timestep);
164  auto end = FeedbackContourTool::GetFeedbackContour()->End(timestep);
165 
166  while (it != end)
167  {
168  singleTimestepContour->AddVertex((*it)->Coordinates);
169  it++;
170  }
171 
173  algorithm->SetInput(m_WorkingSlice);
174  algorithm->SetContour(singleTimestepContour);
175 
176  mitk::LabelSetImage::Pointer labelSetImage = dynamic_cast<LabelSetImage *>(workingNode->GetData());
177  int workingColorId(1);
178  if (labelSetImage.IsNotNull())
179  {
180  workingColorId = labelSetImage->GetActiveLabel()->GetValue();
181  algorithm->SetFillColor(workingColorId);
182  }
183  try
184  {
185  algorithm->UpdateLargestPossibleRegion();
186  }
187  catch (std::exception &e)
188  {
189  MITK_ERROR << "Caught exception '" << e.what() << "'" << std::endl;
190  }
191 
192  mitk::Image::Pointer resultSlice = mitk::Image::New();
193  resultSlice->Initialize(algorithm->GetOutput());
194 
195  if (labelSetImage.IsNotNull())
196  {
198  SegTool2D::WritePreviewOnWorkingImage(erg1, algorithm->GetOutput(), image, workingColorId, 0);
199  SegTool2D::WriteBackSegmentationResult(positionEvent, erg1);
200  }
201  else
202  {
203  mitk::ImageReadAccessor imAccess(algorithm->GetOutput());
204  resultSlice->SetVolume(imAccess.GetData());
205  this->WriteBackSegmentationResult(positionEvent, resultSlice);
206  }
207 }
unsigned int m_LastEventSlice
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
Super class for all position events.
virtual void SetClosed(bool isClosed, int timestep=0)
Set closed property to given boolean.
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 unsigned int GetSlice() const
void Initialize() override
Initialize all data objects.
us::ModuleResource GetIconResource() const override
Returns the tool button icon of the tool wrapped by a usModuleResource.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
#define MITKSEGMENTATION_EXPORT
virtual void OnMouseMoved(StateMachineAction *, InteractionEvent *)
DataCollection - Class to facilitate loading/accessing structured data.
static Pointer New()
BaseRenderer * m_LastEventSender
VertexIterator Begin(int timestep=0) const
Returns a const VertexIterator at the start element of the contour.
BaseRenderer * GetSender() const
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 *)
const char * GetName() const override
Returns the name of this tool. Make it short!
MITK_TOOL_MACRO(MITKSEGMENTATION_EXPORT, LiveWireTool2D, "LiveWire tool")
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 unsigned int GetTimeStep() const
void Expand(unsigned int timeSteps) override
Expand the timebounds of the TimeGeometry to given number of timesteps.
Module * GetModule() const
void Activated() override
Called when the tool gets activated.
Image class for storing images.
Definition: mitkImage.h:72
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
void WriteBackSegmentationResult(const InteractionPositionEvent *, Image *)
CorrectorTool2D(int paintingPixelValue=1)
Corrector tool for 2D binary segmentations.
mitk::Image::Pointer image
static Pointer New()
void RequestUpdate(vtkRenderWindow *renderWindow)
Base class for tools that use a contour for feedback.
VertexIterator End(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
void WritePreviewOnWorkingImage(Image *targetSlice, Image *sourceSlice, Image *workingImage, int paintingPixelValue, int timestep)
LabelSetImage class for handling labels and layers in a segmentation session.
ToolManager * m_ToolManager
Definition: mitkTool.h:233
static Pointer New()
Describes a two-dimensional, rectangular plane.
#define CONNECT_FUNCTION(a, f)
virtual void OnMousePressed(StateMachineAction *, InteractionEvent *)
const char ** GetXPM() const override
Returns an icon in the XPM format.
Image::Pointer m_WorkingSlice
ImageReadAccessor class to get locked read access for a particular image part.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57
DataVectorType GetWorkingData()
Get the list of working data.
void ConnectActionsAndFunctions() override
void Deactivated() override
Called when the tool gets deactivated.