#include "vtkIsosurfaceManager.h" #include #include "vtkRenderer.h" #include "vtkRendererCollection.h" vtkCxxRevisionMacro(vtkIsosurfaceManager, "$Revision: 1.15 $"); vtkStandardNewMacro(vtkIsosurfaceManager); vtkIsosurfaceManager::vtkIsosurfaceManager() { this->Input = 0; this->RenderWindowInteractor = 0; this->LUT = vtkLookupTable::New(); this->DefaultColor = new double[4]; this->DefaultColor[0] = 1; this->DefaultColor[1] = 0; this->DefaultColor[2] = 0; this->DefaultColor[3] = 1; this->Visibility = 1; this->Opacity = 0.5; this->Decimation = 1; this->SetLUTValues(); } vtkIsosurfaceManager::~vtkIsosurfaceManager() { this->ResetData(); delete [] this->DefaultColor; if (this->LUT) { this->LUT->Delete(); } if( this->Input ) { this->Input->Delete(); } if( this->RenderWindowInteractor ) { this->RenderWindowInteractor->Delete(); } } void vtkIsosurfaceManager::GenerateData() { if( !this->RenderWindowInteractor ) { vtkErrorMacro (<<"Error: The interactor must be set prior to calling GenerateData()!"); return; } if( !this->Input ) { vtkErrorMacro (<<"Error: Input is not set!"); return; } this->ResetData(); // Get the isovalues of the Input image unsigned char* Scalars= (unsigned char*)this->Input->GetScalarPointer(); for (int i=0;iInput->GetNumberOfPoints();i++) { int val = (int)(*Scalars); if (val>0) { bool IsInserted = false; for (unsigned int j=0; jIsoValues.size(); j++) { if ( this->IsoValues[j]==val ) { IsInserted=true; } } if (!IsInserted) { this->IsoValues.push_back (val); } } ++Scalars; } this->RenderWindowInteractor->GetRenderWindow()->GetRenderers()->InitTraversal(); vtkRenderer* renderer = this->RenderWindowInteractor->GetRenderWindow()->GetRenderers()->GetNextItem(); // Add the new actors to the renderwindow for (unsigned int i=0; iIsoValues.size(); i++) { double* color = new double[4]; if ( this->IsoValues[i]LUT->GetNumberOfTableValues() ) { this->LUT->GetTableValue ( this->IsoValues[i], color ); } else { color[0] = this->DefaultColor[0]; color[1] = this->DefaultColor[1]; color[2] = this->DefaultColor[2]; color[3] = this->DefaultColor[3]; } vtkImageToIsosurface* ImageToIsosurfaceFilter = vtkImageToIsosurface::New(); ImageToIsosurfaceFilter->SetParameters ( this->IsoValues[i], color ); ImageToIsosurfaceFilter->SetInput ( this->Input ); ImageToIsosurfaceFilter->Update(); if( !this->Decimation ) { ImageToIsosurfaceFilter->SetDecimationOff(); } if( renderer ) { renderer->AddActor ( ImageToIsosurfaceFilter->GetActor() ); } this->Isosurfaces.push_back ( ImageToIsosurfaceFilter ); delete [] color; } // Update the actors with the settings this->UpdateActors(); } void vtkIsosurfaceManager::ResetData() { if (!this->RenderWindowInteractor) { return; } // Remove the old actors from the renderwindow if any if( this->RenderWindowInteractor ) { this->RenderWindowInteractor->GetRenderWindow()->GetRenderers()->InitTraversal(); vtkRenderer* renderer = this->RenderWindowInteractor->GetRenderWindow()->GetRenderers()->GetNextItem(); if ( renderer ) { for (unsigned int i=0; iIsosurfaces.size(); i++) { renderer->RemoveActor ( this->Isosurfaces[i]->GetActor() ); } } } for (unsigned int i=0; iIsosurfaces.size(); i++) { this->Isosurfaces[i]->Delete(); } this->Isosurfaces.clear(); } void vtkIsosurfaceManager::UpdateActors () { for (unsigned int i=0; iIsosurfaces.size(); i++) { this->Isosurfaces[i]->GetActor()->SetVisibility ( this->Visibility ); //this->Actors[i]->GetProperty()->SetOpacity (this->Opacity); } } void vtkIsosurfaceManager::SetLUTValues() { this->LUT->SetNumberOfTableValues (256); this->LUT->SetTableValue (0, 0.0, 0.0, 0.0, 0.0); // erase color this->LUT->SetTableValue (1, 1.0, 0.0, 0.0, 0.5); this->LUT->SetTableValue (2, 0.0, 1.0, 0.0, 0.5); this->LUT->SetTableValue (3, 0.0, 0.0, 1.0, 0.5); this->LUT->SetTableValue (4, 1.0, 1.0, 0.0, 0.5); this->LUT->SetTableValue (5, 0.0, 1.0, 1.0, 0.5); this->LUT->SetTableValue (6, 1.0, 0.0, 1.0, 0.5); // Fill the rest of the labels with color ramps, code taken from SNAP for (int i = 7; i < 256; i++) { if (i < 85) { this->LUT->SetTableValue (i, ((84.0-i)/85.0 * 200.0 + 50.0)/255.0, (i/85.0 * 200.0 + 50.0)/255.0, 0, 0.5); } else if (i < 170) { this->LUT->SetTableValue (i, 0, ((169.0-i)/85.0 * 200.0 + 50)/255.0, ((i-85)/85.0 * 200.0 + 50)/255.0, 0.5); } else { this->LUT->SetTableValue (i, ((i-170)/85.0 * 200.0 + 50)/255.0, 0.0, ((255.0-i)/85.0 * 200.0 + 50)/255.0, 0.5); } } } void vtkIsosurfaceManager::SetVisibility (int IsVisible) { this->Visibility = IsVisible; for (unsigned int i=0; iIsosurfaces.size(); i++) { this->Isosurfaces[i]->GetActor()->SetVisibility ( this->Visibility ); } } void vtkIsosurfaceManager::SetVisibility (int i, int IsVisible) { int ind=-1; for( unsigned int j=0; jIsoValues.size(); j++) { if( this->IsoValues[j]==i ) { ind=(int)j; break; } } if( ind==-1 ) { return; } else { this->Isosurfaces[ind]->GetActor()->SetVisibility (IsVisible); } } int vtkIsosurfaceManager::GetVisibility (int i) const { int ind=-1; for( unsigned int j=0; jIsoValues.size(); j++) { if( this->IsoValues[j]==i ) { ind=(int)j; break; } } if( ind==-1 ) { return 1; } else { return this->Isosurfaces[ind]->GetActor()->GetVisibility(); } } void vtkIsosurfaceManager::SetOpacity (double val) { this->Opacity = val; for (unsigned int i=0; iIsosurfaces.size(); i++) { this->Isosurfaces[i]->GetActor()->GetProperty()->SetOpacity (this->Opacity); } } void vtkIsosurfaceManager::SetOpacity (int i, double val) { int ind=-1; for( unsigned int j=0; jIsoValues.size(); j++) { if( this->IsoValues[j]==i ) { ind=(int)j; break; } } if( ind!=-1 ) { this->Isosurfaces[ind]->GetActor()->GetProperty()->SetOpacity (val); } } double vtkIsosurfaceManager::GetOpacity(int i) const { int ind=-1; for( unsigned int j=0; jIsoValues.size(); j++) { if( this->IsoValues[j]==i ) { ind=(int)j; break; } } if(ind!=-1) { return this->Isosurfaces[ind]->GetActor()->GetProperty()->GetOpacity(); } else { return -1.0; } } void vtkIsosurfaceManager::SetDecimationOn () { this->Decimation = 1; for( unsigned int i=0; iIsosurfaces.size(); i++) { this->Isosurfaces[i]->SetDecimationOn(); } } void vtkIsosurfaceManager::SetDecimationOff () { this->Decimation = 0; for( unsigned int i=0; iIsosurfaces.size(); i++) { this->Isosurfaces[i]->SetDecimationOff(); } }