Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkContourTool.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 "mitkContourTool.h"
14 
18 #include "mitkToolManager.h"
19 
20 #include "mitkBaseRenderer.h"
21 #include "mitkRenderingManager.h"
22 //#include "mitkProperties.h"
23 //#include "mitkPlanarCircle.h"
24 #include "mitkLabelSetImage.h"
25 
26 #include "mitkInteractionEvent.h"
27 #include "mitkStateMachineAction.h"
28 
29 mitk::ContourTool::ContourTool(int paintingPixelValue)
30  : FeedbackContourTool("PressMoveReleaseWithCTRLInversion"),
31  m_PaintingPixelValue(paintingPixelValue),
32  m_CurrentLabelID(1)
33 {
34 }
35 
37 {
38 }
39 
41 {
42  CONNECT_FUNCTION("PrimaryButtonPressed", OnMousePressed);
45  CONNECT_FUNCTION("InvertLogic", OnInvertLogic);
46 }
47 
49 {
50  Superclass::Activated();
51 }
52 
54 {
55  Superclass::Deactivated();
56 }
57 
62 {
63  auto *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
64  if (!positionEvent)
65  return;
66 
67  m_LastEventSender = positionEvent->GetSender();
69 
70  int timestep = positionEvent->GetSender()->GetTimeStep();
71 
73  // Clear feedback contour
74  contour->Initialize();
75  // expand time bounds because our contour was initialized
76  contour->Expand(timestep + 1);
77  // draw as a closed contour
78  contour->SetClosed(true, timestep);
79  // add first mouse position
80  mitk::Point3D point = positionEvent->GetPositionInWorld();
81  contour->AddVertex(point, timestep);
82 
84  assert(positionEvent->GetSender()->GetRenderWindow());
85  mitk::RenderingManager::GetInstance()->RequestUpdate(positionEvent->GetSender()->GetRenderWindow());
86 }
87 
92 {
93  auto *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
94  if (!positionEvent)
95  return;
96 
97  int timestep = positionEvent->GetSender()->GetTimeStep();
98 
100  mitk::Point3D point = positionEvent->GetPositionInWorld();
101  contour->AddVertex(point, timestep);
102 
103  assert(positionEvent->GetSender()->GetRenderWindow());
104  mitk::RenderingManager::GetInstance()->RequestUpdate(positionEvent->GetSender()->GetRenderWindow());
105 }
106 
111 {
112  // 1. Hide the feedback contour, find out which slice the user clicked, find out which slice of the toolmanager's
113  // working image corresponds to that
115 
116  auto *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
117  // const PositionEvent* positionEvent = dynamic_cast<const PositionEvent*>(stateEvent->GetEvent());
118  if (!positionEvent)
119  return;
120 
121  assert(positionEvent->GetSender()->GetRenderWindow());
122  mitk::RenderingManager::GetInstance()->RequestUpdate(positionEvent->GetSender()->GetRenderWindow());
123 
124  DataNode *workingNode(m_ToolManager->GetWorkingData(0));
125  if (!workingNode)
126  return;
127 
128  auto *image = dynamic_cast<Image *>(workingNode->GetData());
129  const PlaneGeometry *planeGeometry((positionEvent->GetSender()->GetCurrentWorldPlaneGeometry()));
130  if (!image || !planeGeometry)
131  return;
132 
133  // Check if it is a multilabel-image
134  // If yes, get the new drawing color from it.
135  // Otherwise nothing happens.
136  auto *labelSetImage = dynamic_cast<LabelSetImage *>(image);
137  if (labelSetImage)
138  {
139  mitk::Label *label = labelSetImage->GetActiveLabel(labelSetImage->GetActiveLayer());
140  m_CurrentLabelID = label->GetValue();
141  }
142  else
143  {
144  m_CurrentLabelID = 1;
145  }
146 
147  const auto *abstractTransformGeometry(
148  dynamic_cast<const AbstractTransformGeometry *>(positionEvent->GetSender()->GetCurrentWorldPlaneGeometry()));
149  if (!image || abstractTransformGeometry)
150  return;
151 
152  // 2. Slice is known, now we try to get it as a 2D image and project the contour into index coordinates of this slice
154 
155  if (slice.IsNull())
156  {
157  MITK_ERROR << "Unable to extract slice." << std::endl;
158  return;
159  }
160 
163  slice, feedbackContour, true, false); // true: actually no idea why this is neccessary, but it works :-(
164 
165  if (projectedContour.IsNull())
166  return;
167 
168  int timestep = positionEvent->GetSender()->GetTimeStep();
169 
170  // m_PaintingPixelValue only decides whether to paint or erase
172  projectedContour, timestep, slice, image, (m_PaintingPixelValue * m_CurrentLabelID));
173 
174  // this->WriteBackSegmentationResult(positionEvent, slice);
175  SegTool2D::WriteBackSegmentationResult(positionEvent, slice);
176 
177  // 4. Make sure the result is drawn again --> is visible then.
178  assert(positionEvent->GetSender()->GetRenderWindow());
179 }
180 
185 {
186  // Inversion only for 0 and 1 as painting values
187  if (m_PaintingPixelValue == 1)
188  {
191  }
192  else if (m_PaintingPixelValue == 0)
193  {
196  }
197 }
unsigned int m_LastEventSlice
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
void Activated() override
Called when the tool gets activated.
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.
virtual void OnMouseMoved(StateMachineAction *, InteractionEvent *interactionEvent)
static void FillContourInSlice(ContourModel *projectedContour, Image *sliceImage, mitk::Image::Pointer workingImage, int paintingPixelValue=1)
Fill a contour in a 2D slice with a specified pixel value at time step 0.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
ContourTool(int paintingPixelValue=1)
void SetFeedbackContourColor(float r, float g, float b)
Provide values from 0.0 (black) to 1.0 (full color)
virtual void OnMouseReleased(StateMachineAction *, InteractionEvent *interactionEvent)
A data structure describing a label.
Definition: mitkLabel.h:31
virtual void OnInvertLogic(StateMachineAction *, InteractionEvent *interactionEvent)
BaseRenderer * m_LastEventSender
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.
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.
Image class for storing images.
Definition: mitkImage.h:72
void WriteBackSegmentationResult(const InteractionPositionEvent *, Image *)
ContourModel::Pointer ProjectContourTo2DSlice(Image *slice, ContourModel *contourIn3D, bool correctionForIpSegmentation=false, bool constrainToInside=true)
Projects a contour onto an image point by point. Converts from world to index coordinates.
mitk::Image::Pointer image
PixelType GetValue() const
Definition: mitkLabel.cpp:169
void RequestUpdate(vtkRenderWindow *renderWindow)
Base class for tools that use a contour for feedback.
LabelSetImage class for handling labels and layers in a segmentation session.
ToolManager * m_ToolManager
Definition: mitkTool.h:230
void ConnectActionsAndFunctions() override
Describes a two-dimensional, rectangular plane.
#define CONNECT_FUNCTION(a, f)
virtual void OnMousePressed(StateMachineAction *, InteractionEvent *interactionEvent)
void Deactivated() override
Called when the tool gets deactivated.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:64
DataVectorType GetWorkingData()