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
00019 #include "mitkSplineVtkMapper3D.h"
00020 #include <vtkProp.h>
00021 #include <vtkPropAssembly.h>
00022 #include <vtkCardinalSpline.h>
00023 #include <vtkPoints.h>
00024 #include <vtkPolyData.h>
00025 #include <vtkCellArray.h>
00026 #include <vtkPolyDataMapper.h>
00027 #include <vtkActor.h>
00028 #include <vtkProperty.h>
00029 #include <vtkTubeFilter.h>
00030 #include <vtkPropCollection.h>
00031 #include <mitkProperties.h>
00032 #include <mitkPointSet.h>
00033
00034
00035 mitk::SplineVtkMapper3D::SplineVtkMapper3D()
00036 : m_SplinesAvailable (false), m_SplinesAddedToAssembly(false)
00037 {
00038 m_SplinesActor = vtkActor::New();
00039 m_SplineAssembly = vtkPropAssembly::New();
00040 m_SplineResolution = 500;
00041 }
00042
00043
00044 mitk::SplineVtkMapper3D::~SplineVtkMapper3D()
00045 {
00046 m_SplinesActor->Delete();
00047 m_SplineAssembly->Delete();
00048 }
00049
00050 vtkProp*
00051 mitk::SplineVtkMapper3D::GetVtkProp(mitk::BaseRenderer * )
00052 {
00053 return m_SplineAssembly;
00054 }
00055
00056 void mitk::SplineVtkMapper3D::UpdateVtkTransform(mitk::BaseRenderer * )
00057 {
00058 vtkLinearTransform * vtktransform =
00059 this->GetDataNode()->GetVtkTransform(this->GetTimestep());
00060
00061 m_SplinesActor->SetUserTransform(vtktransform);
00062 }
00063
00064 void
00065 mitk::SplineVtkMapper3D::GenerateData()
00066 {
00067 Superclass::GenerateData();
00068
00069
00070
00071 if ( m_SplineUpdateTime < m_LastUpdateTime || m_SplineUpdateTime == 0)
00072 {
00073 this->UpdateSpline();
00074 this->ApplyProperties();
00075 }
00076
00077 if ( m_SplinesAvailable )
00078 {
00079 if ( ! m_SplinesAddedToAssembly )
00080 {
00081 m_SplineAssembly->AddPart( m_SplinesActor );
00082 m_SplinesAddedToAssembly = true;
00083 }
00084 }
00085 else
00086 {
00087 if ( m_SplinesAddedToAssembly )
00088 {
00089 m_SplineAssembly->RemovePart( m_SplinesActor );
00090 m_SplinesAddedToAssembly = false;
00091 }
00092 }
00093 }
00094
00095
00096 void mitk::SplineVtkMapper3D::GenerateData( mitk::BaseRenderer* renderer )
00097 {
00098 if ( IsVisible( renderer ) == false )
00099 {
00100 m_SplinesActor->VisibilityOff();
00101 m_SplineAssembly->VisibilityOff();
00102 }
00103 else
00104 {
00105 m_SplinesActor->VisibilityOn();
00106 m_SplineAssembly->VisibilityOn();
00107
00108
00109 if(m_SplineAssembly->GetParts()->IsItemPresent(m_PointsAssembly))
00110 m_SplineAssembly->RemovePart(m_PointsAssembly);
00111 }
00112
00113 if ( (m_SplineUpdateTime < this->m_DataNode->GetPropertyList()->GetMTime() ) ||
00114 (m_SplineUpdateTime < this->m_DataNode->GetPropertyList(renderer)->GetMTime() ) )
00115 this->ApplyProperties();
00116 }
00117
00118
00119 void mitk::SplineVtkMapper3D::ApplyProperties()
00120 {
00121
00122 vtkFloatingPointType rgba[ 4 ] = {1.0f, 1.0f, 1.0f, 1.0f};
00123
00124
00125 float temprgba[4];
00126 this->GetDataNode()->GetColor( &temprgba[0], NULL );
00127
00128 rgba[0] = temprgba[0]; rgba[1] = temprgba[1]; rgba[2] = temprgba[2]; rgba[3] = temprgba[3];
00129
00130 m_SplinesActor->GetProperty()->SetColor( rgba );
00131
00132 float lineWidth;
00133 if (dynamic_cast<mitk::FloatProperty *>(this->GetDataNode()->GetProperty("line width")) == NULL)
00134 lineWidth = 1.0;
00135 else
00136 lineWidth = dynamic_cast<mitk::FloatProperty *>(this->GetDataNode()->GetProperty("line width"))->GetValue();
00137 m_SplinesActor->GetProperty()->SetLineWidth(lineWidth);
00138
00139 m_SplineUpdateTime.Modified();
00140 }
00141
00142
00143 bool mitk::SplineVtkMapper3D::SplinesAreAvailable()
00144 {
00145 return m_SplinesAvailable;
00146 }
00147
00148
00149 vtkPolyData* mitk::SplineVtkMapper3D::GetSplinesPolyData()
00150 {
00151 if ( m_SplinesAvailable )
00152 return ( dynamic_cast<vtkPolyDataMapper*>( m_SplinesActor->GetMapper() ) )->GetInput();
00153 else
00154 return NULL;
00155 }
00156
00157 vtkActor* mitk::SplineVtkMapper3D::GetSplinesActor()
00158 {
00159 if ( m_SplinesAvailable )
00160 return m_SplinesActor;
00161 else
00162 return vtkActor::New();
00163 }
00164
00165 unsigned long mitk::SplineVtkMapper3D::GetLastUpdateTime() const
00166 {
00167 return m_LastUpdateTime.GetMTime();
00168 }
00169
00170 void mitk::SplineVtkMapper3D::UpdateSpline()
00171 {
00172 mitk::PointSet::Pointer input = const_cast<mitk::PointSet*>( this->GetInput( ) );
00173
00174
00175
00176 unsigned int numberOfOutputPoints = m_SplineResolution;
00177 unsigned int numberOfInputPoints = input->GetSize();
00178
00179
00180 if ( numberOfInputPoints >= 2 )
00181 {
00182 m_SplinesAvailable = true;
00183 vtkCardinalSpline* splineX = vtkCardinalSpline::New();
00184 vtkCardinalSpline* splineY = vtkCardinalSpline::New();
00185 vtkCardinalSpline* splineZ = vtkCardinalSpline::New();
00186 unsigned int index = 0;
00187 mitk::PointSet::DataType::PointsContainer::Pointer pointsContainer = input->GetPointSet()->GetPoints();
00188 for ( mitk::PointSet::DataType::PointsContainer::Iterator it = pointsContainer->Begin(); it != pointsContainer->End(); ++it, ++index )
00189
00190 {
00191 mitk::PointSet::PointType point = it->Value();
00192 splineX->AddPoint( index, point[ 0 ] );
00193 splineY->AddPoint( index, point[ 1 ] );
00194 splineZ->AddPoint( index, point[ 2 ] );
00195 }
00196 vtkPoints* points = vtkPoints::New();
00197 vtkPolyData* profileData = vtkPolyData::New();
00198
00199
00200
00201
00202 double t = 0.0f;
00203 for ( unsigned int i = 0; i < numberOfOutputPoints; ++i )
00204 {
00205 t = ( ( ( ( double ) numberOfInputPoints ) - 1.0f ) / ( ( ( double ) numberOfOutputPoints ) - 1.0f ) ) * ( ( double ) i );
00206 points->InsertPoint( i, splineX->Evaluate( t ), splineY->Evaluate( t ), splineZ->Evaluate( t ) ) ;
00207 }
00208
00209
00210 vtkCellArray* lines = vtkCellArray::New();
00211 lines->InsertNextCell( numberOfOutputPoints );
00212 for ( unsigned int i = 0; i < numberOfOutputPoints; ++i )
00213 lines->InsertCellPoint( i );
00214
00215 profileData->SetPoints( points );
00216 profileData->SetLines( lines );
00217
00218
00219
00220
00221
00222
00223
00224 vtkPolyDataMapper* profileMapper = vtkPolyDataMapper::New();
00225 profileMapper->SetInput( profileData );
00226
00227 m_SplinesActor->SetMapper( profileMapper );
00228 }
00229 else
00230 {
00231 m_SplinesAvailable = false;
00232 }
00233 m_SplineUpdateTime.Modified();
00234 }
00235