Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkShowSegmentationAsSurface.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 
20 #include <mitkCoreObjectFactory.h>
21 
22 #include <vtkPolyDataNormals.h>
23 
24 namespace mitk
25 {
26  ShowSegmentationAsSurface::ShowSegmentationAsSurface() : m_UIDGeneratorSurfaces("Surface_"), m_AddToTree(false) {}
29  {
30  Superclass::Initialize(other);
31 
32  bool syncVisibility(false);
33 
34  if (other)
35  {
36  other->GetParameter("Sync visibility", syncVisibility);
37  }
38 
39  SetParameter("Sync visibility", syncVisibility);
40  SetParameter("Median kernel size", 3u);
41  SetParameter("Apply median", true);
42  SetParameter("Smooth", true);
43  SetParameter("Gaussian SD", 1.5f);
44  SetParameter("Decimate mesh", true);
45  SetParameter("Decimation rate", 0.8f);
46  SetParameter("Wireframe", false);
47  }
48 
50  {
51  try
52  {
53  Image::Pointer image;
54  GetPointerParameter("Input", image);
55 
56  return image.IsNotNull() && GetGroupNode();
57  }
58  catch (std::invalid_argument &)
59  {
60  return false;
61  }
62  }
63 
65  {
66  Image::Pointer image;
67  GetPointerParameter("Input", image);
68 
69  bool smooth(true);
70  GetParameter("Smooth", smooth);
71 
72  bool applyMedian(true);
73  GetParameter("Apply median", applyMedian);
74 
75  bool decimateMesh(true);
76  GetParameter("Decimate mesh", decimateMesh);
77 
78  unsigned int medianKernelSize(3);
79  GetParameter("Median kernel size", medianKernelSize);
80 
81  float gaussianSD(1.5);
82  GetParameter("Gaussian SD", gaussianSD);
83 
84  float reductionRate(0.8);
85  GetParameter("Decimation rate", reductionRate);
86 
87  MITK_INFO << "Creating polygon model with smoothing " << smooth << " gaussianSD " << gaussianSD << " median "
88  << applyMedian << " median kernel " << medianKernelSize << " mesh reduction " << decimateMesh
89  << " reductionRate " << reductionRate;
90 
92  surfaceFilter->SetInput(image);
93  surfaceFilter->SetThreshold(0.5); // expects binary image with zeros and ones
94 
95  surfaceFilter->SetUseGaussianImageSmooth(smooth); // apply gaussian to thresholded image ?
96  surfaceFilter->SetSmooth(smooth);
97  if (smooth)
98  {
99  surfaceFilter->InterpolationOn();
100  surfaceFilter->SetGaussianStandardDeviation(gaussianSD);
101  }
102 
103  surfaceFilter->SetMedianFilter3D(applyMedian); // apply median to segmentation before marching cubes ?
104  if (applyMedian)
105  {
106  surfaceFilter->SetMedianKernelSize(
107  medianKernelSize, medianKernelSize, medianKernelSize); // apply median to segmentation before marching cubes
108  }
109 
110  // fix to avoid vtk warnings see bug #5390
111  if (image->GetDimension() > 3)
112  decimateMesh = false;
113 
114  if (decimateMesh)
115  {
116  surfaceFilter->SetDecimate(ImageToSurfaceFilter::QuadricDecimation);
117  surfaceFilter->SetTargetReduction(reductionRate);
118  }
119  else
120  {
121  surfaceFilter->SetDecimate(ImageToSurfaceFilter::NoDecimation);
122  }
123 
124  surfaceFilter->UpdateLargestPossibleRegion();
125 
126  // calculate normals for nicer display
127  m_Surface = surfaceFilter->GetOutput();
128 
129  vtkPolyData *polyData = m_Surface->GetVtkPolyData();
130 
131  if (!polyData)
132  throw std::logic_error("Could not create polygon model");
133 
134  polyData->SetVerts(0);
135  polyData->SetLines(0);
136 
137  if (smooth || applyMedian || decimateMesh)
138  {
139  vtkPolyDataNormals *normalsGen = vtkPolyDataNormals::New();
140 
141  normalsGen->AutoOrientNormalsOn();
142  normalsGen->FlipNormalsOff();
143  normalsGen->SetInputData(polyData);
144  normalsGen->Update();
145 
146  m_Surface->SetVtkPolyData(normalsGen->GetOutput());
147 
148  normalsGen->Delete();
149  }
150  else
151  {
152  m_Surface->SetVtkPolyData(polyData);
153  }
154 
155  return true;
156  }
157 
159  {
160  m_Node = DataNode::New();
161 
162  bool wireframe(false);
163  GetParameter("Wireframe", wireframe);
164  if (wireframe)
165  {
167  dynamic_cast<VtkRepresentationProperty *>(m_Node->GetProperty("material.representation"));
168  if (np)
170  }
171 
172  m_Node->SetProperty("opacity", FloatProperty::New(0.3));
173  m_Node->SetProperty("line width", IntProperty::New(1));
174  m_Node->SetProperty("scalar visibility", BoolProperty::New(false));
175 
176  std::string groupNodesName("surface");
177 
178  DataNode *groupNode = GetGroupNode();
179  if (groupNode)
180  {
181  groupNode->GetName(groupNodesName);
182  // if parameter smooth is set add extension to node name
183  bool smooth(true);
184  GetParameter("Smooth", smooth);
185  if (smooth)
186  groupNodesName.append("_smoothed");
187  }
188  m_Node->SetProperty("name", StringProperty::New(groupNodesName));
189 
190  // synchronize this object's color with the parent's color
191  // surfaceNode->SetProperty( "color", parentNode->GetProperty( "color" ) );
192  // surfaceNode->SetProperty( "visible", parentNode->GetProperty( "visible" ) );
193 
194  m_Node->SetData(m_Surface);
195 
196  BaseProperty *colorProp = groupNode->GetProperty("color");
197  if (colorProp)
198  m_Node->ReplaceProperty("color", colorProp->Clone());
199  else
200  m_Node->SetProperty("color", ColorProperty::New(1.0, 1.0, 0.0));
201 
202  bool showResult(true);
203  GetParameter("Show result", showResult);
204 
205  bool syncVisibility(false);
206  GetParameter("Sync visibility", syncVisibility);
207 
208  Image::Pointer image;
209  GetPointerParameter("Input", image);
210 
211  BaseProperty *organTypeProp = image->GetProperty("organ type");
212  if (organTypeProp)
213  m_Surface->SetProperty("organ type", organTypeProp);
214 
215  BaseProperty *visibleProp = groupNode->GetProperty("visible");
216  if (visibleProp && syncVisibility)
217  m_Node->ReplaceProperty("visible", visibleProp->Clone());
218  else
219  m_Node->SetProperty("visible", BoolProperty::New(showResult));
220 
221  InsertBelowGroupNode(m_Node);
222 
223  Superclass::ThreadedUpdateSuccessful();
224  }
225 
226 } // namespace
#define MITK_INFO
Definition: mitkLogMacros.h:22
void InsertBelowGroupNode(mitk::DataNode *node)
static Pointer New()
bool GetName(std::string &nodeName, const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="name") const
Convenience access method for accessing the name of an object (instance of StringProperty with proper...
Definition: mitkDataNode.h:366
DataCollection - Class to facilitate loading/accessing structured data.
void SetParameter(const char *parameter, const T &value)
For any kind of normal types.
mitk::BaseProperty * GetProperty(const char *propertyKey, const mitk::BaseRenderer *renderer=nullptr) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList of the rendere...
static Pointer New()
Abstract base class for properties.
static Pointer New()
static Pointer New()
virtual void Initialize(const NonBlockingAlgorithm *other=NULL) override
Pointer Clone() const
static Pointer New()
static Pointer New()
void GetPointerParameter(const char *parameter, itk::SmartPointer< T > &value) const
void GetParameter(const char *parameter, T &value) const
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.