Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkShowSegmentationAsSurface.h"
00019 #include "mitkManualSegmentationToSurfaceFilter.h"
00020 #include "mitkDataNodeFactory.h"
00021 #include "mitkVtkRepresentationProperty.h"
00022 #include <mitkCoreObjectFactory.h>
00023
00024 #include <vtkPolyDataNormals.h>
00025
00026 namespace mitk
00027 {
00028
00029 ShowSegmentationAsSurface::ShowSegmentationAsSurface()
00030 :m_UIDGeneratorSurfaces("Surface_"),
00031 m_AddToTree(false)
00032 {
00033 }
00034
00035
00036 ShowSegmentationAsSurface::~ShowSegmentationAsSurface()
00037 {
00038 }
00039
00040 void ShowSegmentationAsSurface::Initialize(const NonBlockingAlgorithm* other)
00041 {
00042 Superclass::Initialize(other);
00043
00044 bool syncVisibility(false);
00045
00046 if (other)
00047 {
00048 other->GetParameter("Sync visibility", syncVisibility);
00049 }
00050
00051 SetParameter("Sync visibility", syncVisibility );
00052 SetParameter("Median kernel size", 3u);
00053 SetParameter("Apply median", true );
00054 SetParameter("Smooth", true );
00055 SetParameter("Gaussian SD", 1.5f );
00056 SetParameter("Decimate mesh", true );
00057 SetParameter("Decimation rate", 0.8f );
00058 SetParameter("Wireframe", false );
00059 }
00060
00061
00062 bool ShowSegmentationAsSurface::ReadyToRun()
00063 {
00064 try
00065 {
00066 Image::Pointer image;
00067 GetPointerParameter("Input", image);
00068
00069 return image.IsNotNull() && GetGroupNode();
00070 }
00071 catch (std::invalid_argument&)
00072 {
00073 return false;
00074 }
00075 }
00076
00077
00078 bool ShowSegmentationAsSurface::ThreadedUpdateFunction()
00079 {
00080 Image::Pointer image;
00081 GetPointerParameter("Input", image);
00082
00083 bool smooth(true);
00084 GetParameter("Smooth", smooth);
00085
00086 bool applyMedian(true);
00087 GetParameter("Apply median", applyMedian);
00088
00089 bool decimateMesh(true);
00090 GetParameter("Decimate mesh", decimateMesh);
00091
00092 unsigned int medianKernelSize(3);
00093 GetParameter("Median kernel size", medianKernelSize);
00094
00095 float gaussianSD(1.5);
00096 GetParameter("Gaussian SD", gaussianSD );
00097
00098 float reductionRate(0.8);
00099 GetParameter("Decimation rate", reductionRate );
00100
00101 MITK_INFO << "Creating polygon model with smoothing " << smooth << " gaussianSD " << gaussianSD
00102 << " median " << applyMedian << " median kernel " << medianKernelSize
00103 << " mesh reduction " << decimateMesh << " reductionRate " << reductionRate;
00104
00105 ManualSegmentationToSurfaceFilter::Pointer surfaceFilter = ManualSegmentationToSurfaceFilter::New();
00106 surfaceFilter->SetInput( image );
00107 surfaceFilter->SetThreshold( 1 );
00108
00109 surfaceFilter->SetUseGaussianImageSmooth(smooth);
00110 if (smooth)
00111 {
00112 surfaceFilter->InterpolationOn();
00113 surfaceFilter->SetGaussianStandardDeviation( gaussianSD );
00114
00115
00116 }
00117
00118 surfaceFilter->SetMedianFilter3D(applyMedian);
00119 if (applyMedian)
00120 {
00121 surfaceFilter->SetMedianKernelSize(medianKernelSize, medianKernelSize, medianKernelSize);
00122 }
00123
00124 if (decimateMesh)
00125 {
00126 surfaceFilter->SetDecimate( ImageToSurfaceFilter::DecimatePro );
00127 surfaceFilter->SetTargetReduction( reductionRate );
00128 }
00129 else
00130 {
00131 surfaceFilter->SetDecimate( ImageToSurfaceFilter::NoDecimation );
00132 }
00133
00134 surfaceFilter->UpdateLargestPossibleRegion();
00135
00136
00137 m_Surface = surfaceFilter->GetOutput();
00138
00139 vtkPolyData* polyData = m_Surface->GetVtkPolyData();
00140
00141 if (!polyData) throw std::logic_error("Could not create polygon model");
00142
00143 polyData->SetVerts(0);
00144 polyData->SetLines(0);
00145
00146 if ( smooth || applyMedian || decimateMesh)
00147 {
00148 vtkPolyDataNormals* normalsGen = vtkPolyDataNormals::New();
00149
00150 normalsGen->SetInput( polyData );
00151 normalsGen->Update();
00152
00153 m_Surface->SetVtkPolyData( normalsGen->GetOutput() );
00154
00155 normalsGen->Delete();
00156 }
00157 else
00158 {
00159 m_Surface->SetVtkPolyData( polyData );
00160 }
00161
00162 return true;
00163 }
00164
00165 void ShowSegmentationAsSurface::ThreadedUpdateSuccessful()
00166 {
00167 m_Node = LookForPointerTargetBelowGroupNode("Surface representation");
00168
00169 m_AddToTree = m_Node.IsNull();
00170
00171 if (m_AddToTree)
00172 {
00173 m_Node = DataNode::New();
00174
00175 bool wireframe(false);
00176 GetParameter("Wireframe", wireframe );
00177 if (wireframe)
00178 {
00179 VtkRepresentationProperty *np = dynamic_cast<VtkRepresentationProperty*>(m_Node->GetProperty("material.representation"));
00180 if (np)
00181 np->SetRepresentationToWireframe();
00182 }
00183
00184 m_Node->SetProperty("opacity", FloatProperty::New(0.3) );
00185 m_Node->SetProperty("line width", IntProperty::New(1) );
00186 m_Node->SetProperty("scalar visibility", BoolProperty::New(false) );
00187
00188 std::string uid = m_UIDGeneratorSurfaces.GetUID();
00189 m_Node->SetProperty( "FILENAME", StringProperty::New( uid + ".vtk" ) );
00190 std::string groupNodesName ("surface");
00191
00192 DataNode* groupNode = GetGroupNode();
00193 if (groupNode)
00194 {
00195 groupNode->GetName( groupNodesName );
00196 }
00197 m_Node->SetProperty( "name", StringProperty::New(groupNodesName) );
00198
00199
00200
00201
00202 }
00203
00204 m_Node->SetData( m_Surface );
00205
00206 if (m_AddToTree)
00207 {
00208
00209 DataNode* groupNode = GetGroupNode();
00210 if (groupNode)
00211 {
00212 groupNode->SetProperty( "Surface representation", SmartPointerProperty::New(m_Node) );
00213 BaseProperty* colorProp = groupNode->GetProperty("color");
00214 if (colorProp)
00215 m_Node->ReplaceProperty("color", colorProp);
00216 else
00217 m_Node->SetProperty("color", ColorProperty::New(1.0, 1.0, 0.0));
00218
00219 bool showResult(true);
00220 GetParameter("Show result", showResult );
00221
00222 bool syncVisibility(false);
00223 GetParameter("Sync visibility", syncVisibility );
00224
00225 Image::Pointer image;
00226 GetPointerParameter("Input", image);
00227
00228 BaseProperty* organTypeProp = image->GetProperty("organ type");
00229 if (organTypeProp)
00230 m_Surface->SetProperty("organ type", organTypeProp);
00231
00232 BaseProperty* visibleProp = groupNode->GetProperty("visible");
00233 if (visibleProp && syncVisibility)
00234 m_Node->ReplaceProperty("visible", visibleProp);
00235 else
00236 m_Node->SetProperty("visible", BoolProperty::New(showResult));
00237 }
00238
00239 InsertBelowGroupNode(m_Node);
00240 }
00241
00242 Superclass::ThreadedUpdateSuccessful();
00243 }
00244
00245 }
00246