Medical Imaging Interaction Toolkit  2016.11.0
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,
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 "mitkContourTool.h"
18 
22 #include "mitkToolManager.h"
23 
24 #include "mitkBaseRenderer.h"
25 #include "mitkRenderingManager.h"
26 //#include "mitkProperties.h"
27 //#include "mitkPlanarCircle.h"
28 #include "mitkLabelSetImage.h"
29 
30 #include "mitkInteractionEvent.h"
31 #include "mitkStateMachineAction.h"
32 
33 mitk::ContourTool::ContourTool(int paintingPixelValue)
34  : FeedbackContourTool("PressMoveReleaseWithCTRLInversion"),
35  m_PaintingPixelValue(paintingPixelValue),
36  m_CurrentLabelID(1)
37 {
38 }
39 
41 {
42 }
43 
45 {
46  CONNECT_FUNCTION("PrimaryButtonPressed", OnMousePressed);
47  CONNECT_FUNCTION("Move", OnMouseMoved);
48  CONNECT_FUNCTION("Release", OnMouseReleased);
49  CONNECT_FUNCTION("InvertLogic", OnInvertLogic);
50 }
51 
53 {
54  Superclass::Activated();
55 }
56 
58 {
59  Superclass::Deactivated();
60 }
61 
66 {
67  mitk::InteractionPositionEvent *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
68  if (!positionEvent)
69  return;
70 
71  m_LastEventSender = positionEvent->GetSender();
72  m_LastEventSlice = m_LastEventSender->GetSlice();
73 
74  int timestep = positionEvent->GetSender()->GetTimeStep();
75 
77  // Clear feedback contour
78  contour->Initialize();
79  // expand time bounds because our contour was initialized
80  contour->Expand(timestep + 1);
81  // draw as a closed contour
82  contour->SetClosed(true, timestep);
83  // add first mouse position
84  mitk::Point3D point = positionEvent->GetPositionInWorld();
85  contour->AddVertex(point, timestep);
86 
88  assert(positionEvent->GetSender()->GetRenderWindow());
90 }
91 
96 {
97  mitk::InteractionPositionEvent *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
98  if (!positionEvent)
99  return;
100 
101  int timestep = positionEvent->GetSender()->GetTimeStep();
102 
104  mitk::Point3D point = positionEvent->GetPositionInWorld();
105  contour->AddVertex(point, timestep);
106 
107  assert(positionEvent->GetSender()->GetRenderWindow());
109 }
110 
115 {
116  // 1. Hide the feedback contour, find out which slice the user clicked, find out which slice of the toolmanager's
117  // working image corresponds to that
119 
120  mitk::InteractionPositionEvent *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent);
121  // const PositionEvent* positionEvent = dynamic_cast<const PositionEvent*>(stateEvent->GetEvent());
122  if (!positionEvent)
123  return;
124 
125  assert(positionEvent->GetSender()->GetRenderWindow());
127 
128  DataNode *workingNode(m_ToolManager->GetWorkingData(0));
129  if (!workingNode)
130  return;
131 
132  Image *image = dynamic_cast<Image *>(workingNode->GetData());
133  const PlaneGeometry *planeGeometry((positionEvent->GetSender()->GetCurrentWorldPlaneGeometry()));
134  if (!image || !planeGeometry)
135  return;
136 
137  // Check if it is a multilabel-image
138  // If yes, get the new drawing color from it.
139  // Otherwise nothing happens.
140  mitk::LabelSetImage *labelSetImage = dynamic_cast<LabelSetImage *>(image);
141  if (labelSetImage)
142  {
143  mitk::Label *label = labelSetImage->GetActiveLabel(labelSetImage->GetActiveLayer());
144  m_CurrentLabelID = label->GetValue();
145  }
146  else
147  {
148  m_CurrentLabelID = 1;
149  }
150 
151  const AbstractTransformGeometry *abstractTransformGeometry(
152  dynamic_cast<const AbstractTransformGeometry *>(positionEvent->GetSender()->GetCurrentWorldPlaneGeometry()));
153  if (!image || abstractTransformGeometry)
154  return;
155 
156  // 2. Slice is known, now we try to get it as a 2D image and project the contour into index coordinates of this slice
157  Image::Pointer slice = SegTool2D::GetAffectedImageSliceAs2DImage(positionEvent, image);
158 
159  if (slice.IsNull())
160  {
161  MITK_ERROR << "Unable to extract slice." << std::endl;
162  return;
163  }
164 
167  slice, feedbackContour, true, false); // true: actually no idea why this is neccessary, but it works :-(
168 
169  if (projectedContour.IsNull())
170  return;
171 
172  int timestep = positionEvent->GetSender()->GetTimeStep();
173 
174  // m_PaintingPixelValue only decides whether to paint or erase
176  projectedContour, timestep, slice, image, (m_PaintingPixelValue * m_CurrentLabelID));
177 
178  // this->WriteBackSegmentationResult(positionEvent, slice);
179  SegTool2D::WriteBackSegmentationResult(positionEvent, slice);
180 
181  // 4. Make sure the result is drawn again --> is visible then.
182  assert(positionEvent->GetSender()->GetRenderWindow());
183 }
184 
189 {
190  // Inversion only for 0 and 1 as painting values
191  if (m_PaintingPixelValue == 1)
192  {
193  m_PaintingPixelValue = 0;
195  }
196  else if (m_PaintingPixelValue == 0)
197  {
198  m_PaintingPixelValue = 1;
200  }
201 }
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
virtual void Activated() override
Called when the tool gets activated.
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.
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:24
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:35
virtual const PlaneGeometry * GetCurrentWorldPlaneGeometry()
Get the current 2D-worldgeometry (m_CurrentWorldPlaneGeometry) used for 2D-rendering.
virtual void OnInvertLogic(StateMachineAction *, InteractionEvent *interactionEvent)
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 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:76
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::Label * GetActiveLabel(unsigned int layer=0)
Returns the active label of a specific layer.
Describes a geometry defined by an vtkAbstractTransform and a plane.
virtual unsigned int GetTimeStep() const
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.
void ConnectActionsAndFunctions() override
Describes a two-dimensional, rectangular plane.
#define CONNECT_FUNCTION(a, f)
unsigned int GetActiveLayer() const
Gets the ID of the currently active layer.
PixelType GetValue() const
Definition: mitkLabel.cpp:171
virtual void OnMousePressed(StateMachineAction *, InteractionEvent *interactionEvent)
vtkRenderWindow * GetRenderWindow() const
Access the RenderWindow into which this renderer renders.
virtual void Deactivated() override
Called when the tool gets deactivated.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66