00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mitkVtkPropRenderer.h"
00020
00021 #include "picimage.h"
00022
00023
00024 #include "mitkMapper.h"
00025 #include "mitkImageMapperGL2D.h"
00026 #include "mitkVtkMapper2D.h"
00027 #include "mitkVtkMapper3D.h"
00028 #include "mitkGeometry2DDataVtkMapper3D.h"
00029 #include "mitkPointSetGLMapper2D.h"
00030
00031 #include "mitkImageSliceSelector.h"
00032 #include "mitkRenderingManager.h"
00033 #include "mitkGL.h"
00034 #include "mitkGeometry3D.h"
00035 #include "mitkDisplayGeometry.h"
00036 #include "mitkLevelWindow.h"
00037 #include "mitkCameraController.h"
00038 #include "mitkVtkInteractorCameraController.h"
00039 #include "mitkPlaneGeometry.h"
00040 #include "mitkProperties.h"
00041 #include "mitkSurface.h"
00042 #include "mitkNodePredicateDataType.h"
00043
00044
00045 #include <vtkRenderer.h>
00046 #include <vtkRendererCollection.h>
00047 #include <vtkLight.h>
00048 #include <vtkLightKit.h>
00049 #include <vtkRenderWindow.h>
00050 #include <vtkLinearTransform.h>
00051 #include <vtkCamera.h>
00052 #include <vtkWorldPointPicker.h>
00053 #include <vtkPointPicker.h>
00054 #include <vtkCellPicker.h>
00055 #include <vtkTextActor.h>
00056 #include <vtkTextProperty.h>
00057 #include <vtkProp.h>
00058 #include <vtkAssemblyPath.h>
00059 #include <vtkAssemblyNode.h>
00060 #include <vtkMapper.h>
00061
00062
00063
00064
00065 mitk::VtkPropRenderer::VtkPropRenderer( const char* name, vtkRenderWindow * renWin, mitk::RenderingManager* rm )
00066 : BaseRenderer(name,renWin, rm),
00067 m_VtkMapperPresent(false),
00068 m_NewRenderer(true)
00069 {
00070 didCount=false;
00071
00072 m_WorldPointPicker = vtkWorldPointPicker::New();
00073
00074 m_PointPicker = vtkPointPicker::New();
00075 m_PointPicker->SetTolerance( 0.0025 );
00076
00077 m_CellPicker = vtkCellPicker::New();
00078 m_CellPicker->SetTolerance( 0.0025 );
00079
00080 mitk::Geometry2DDataVtkMapper3D::Pointer geometryMapper = mitk::Geometry2DDataVtkMapper3D::New();
00081 m_CurrentWorldGeometry2DMapper = geometryMapper;
00082 m_CurrentWorldGeometry2DNode->SetMapper(2, geometryMapper);
00083
00084 m_LightKit = vtkLightKit::New();
00085 m_LightKit->AddLightsToRenderer(m_VtkRenderer);
00086 m_PickingMode = WorldPointPicking;
00087
00088 m_TextRenderer = vtkRenderer::New();
00089 m_TextRenderer->SetRenderWindow(renWin);
00090 m_TextRenderer->SetInteractive(0);
00091 #if ( VTK_MAJOR_VERSION >= 5 )
00092 m_TextRenderer->SetErase(0);
00093 #endif
00094 }
00095
00099 mitk::VtkPropRenderer::~VtkPropRenderer()
00100 {
00101
00102 {
00103 m_MapperID=0;
00104 checkState();
00105 }
00106
00107 if (m_LightKit != NULL)
00108 m_LightKit->Delete();
00109
00110 if (m_VtkRenderer!=NULL)
00111 {
00112 m_CameraController = NULL;
00113
00114 m_VtkRenderer->Delete();
00115
00116 m_VtkRenderer = NULL;
00117 }
00118 else
00119 m_CameraController = NULL;
00120
00121 if (m_WorldPointPicker != NULL)
00122 m_WorldPointPicker->Delete();
00123 if (m_PointPicker != NULL)
00124 m_PointPicker->Delete();
00125 if (m_CellPicker != NULL)
00126 m_CellPicker->Delete();
00127 if (m_TextRenderer != NULL)
00128 m_TextRenderer->Delete();
00129 }
00130
00131
00132 void mitk::VtkPropRenderer::SetDataStorage( mitk::DataStorage* storage )
00133 {
00134 if ( storage == NULL )
00135 return;
00136
00137 BaseRenderer::SetDataStorage(storage);
00138
00139 static_cast<mitk::Geometry2DDataVtkMapper3D*>(m_CurrentWorldGeometry2DMapper.GetPointer())->SetDataStorageForTexture( m_DataStorage.GetPointer() );
00140
00141
00142 this->SetWorldGeometryToDataStorageBounds();
00143 }
00144
00145
00146 bool mitk::VtkPropRenderer::SetWorldGeometryToDataStorageBounds()
00147 {
00148 if ( m_DataStorage.IsNull() )
00149 return false;
00150
00151
00152 mitk::TimeSlicedGeometry::Pointer geometry = m_DataStorage->ComputeVisibleBoundingGeometry3D( NULL, "includeInBoundingBox" );
00153
00154 if ( geometry.IsNull() )
00155 return false;
00156
00157 this->SetWorldGeometry(geometry);
00158
00159 this->GetDisplayGeometry()->Fit();
00160 this->GetVtkRenderer()->ResetCamera();
00161 this->Modified();
00162 return true;
00163 }
00164
00165
00171 int mitk::VtkPropRenderer::Render(mitk::VtkPropRenderer::RenderType type)
00172 {
00173
00174 if ( this->GetEmptyWorldGeometry())
00175 return 0;
00176
00177 if ( m_DataStorage.IsNull())
00178 return 0;
00179
00180
00181 if (type == VtkPropRenderer::Opaque)
00182 this->PrepareMapperQueue();
00183
00184
00185 bool lastVtkBased = true;
00186 bool sthVtkBased = false;
00187
00188 for(MappersMapType::iterator it = m_MappersMap.begin(); it != m_MappersMap.end(); it++)
00189 {
00190 Mapper * mapper = (*it).second;
00191 if((mapper->IsVtkBased() == true) )
00192 {
00193 sthVtkBased = true;
00194 mitk::VtkMapper3D::Pointer vtkMapper = dynamic_cast<mitk::VtkMapper3D*>(mapper);
00195 if(vtkMapper)
00196 {
00197 vtkMapper->GetVtkProp(this)->SetAllocatedRenderTime(5000,GetVtkRenderer());
00198
00199 }
00200 if(lastVtkBased == false)
00201 {
00202 Disable2DOpenGL();
00203 lastVtkBased = true;
00204 }
00205 }
00206 else
00207 if((mapper->IsVtkBased() == false) && (lastVtkBased == true))
00208 {
00209 Enable2DOpenGL();
00210 lastVtkBased = false;
00211 }
00212
00213 switch(type)
00214 {
00215 case mitk::VtkPropRenderer::Opaque: mapper->MitkRenderOpaqueGeometry(this); break;
00216 case mitk::VtkPropRenderer::Translucent: mapper->MitkRenderTranslucentGeometry(this); break;
00217 case mitk::VtkPropRenderer::Overlay: mapper->MitkRenderOverlay(this); break;
00218
00219 #if ( ( VTK_MAJOR_VERSION >= 5 ) && ( VTK_MINOR_VERSION>=2) )
00220 case mitk::VtkPropRenderer::Volumetric: mapper->MitkRenderVolumetricGeometry(this); break;
00221 #endif
00222 }
00223 }
00224
00225 if (lastVtkBased == false)
00226 Disable2DOpenGL();
00227
00228
00229
00230
00231 if (sthVtkBased == false)
00232 this->GetVtkRenderer()->ResetCamera();
00233
00234
00235 if (type == VtkPropRenderer::Overlay)
00236 {
00237 if (m_TextCollection.size() > 0)
00238 {
00239 for (TextMapType::iterator it = m_TextCollection.begin(); it != m_TextCollection.end() ; it++)
00240 m_TextRenderer->AddViewProp((*it).second);
00241 m_TextRenderer->Render();
00242 }
00243 }
00244 return 1;
00245 }
00246
00252 void mitk::VtkPropRenderer::PrepareMapperQueue()
00253 {
00254
00255 m_NumberOfVisibleLODEnabledMappers = 0;
00256
00257
00258 if ( m_LastUpdateTime < GetMTime() || m_LastUpdateTime < GetDisplayGeometry()->GetMTime() )
00259 Update();
00260 else if (m_MapperID>=2 && m_MapperID < 6)
00261 Update();
00262
00263
00264 m_TextRenderer->RemoveAllViewProps();
00265 m_TextCollection.clear();
00266
00267
00268 m_MappersMap.clear();
00269
00270 int mapperNo = 0;
00271
00272
00273 if( m_DataStorage.IsNull() )
00274 return;
00275
00276 DataStorage::SetOfObjects::ConstPointer allObjects = m_DataStorage->GetAll();
00277
00278 for (DataStorage::SetOfObjects::ConstIterator it = allObjects->Begin(); it != allObjects->End(); ++it)
00279 {
00280 DataNode::Pointer node = it->Value();
00281 if ( node.IsNull() )
00282 continue;
00283 mitk::Mapper::Pointer mapper = node->GetMapper(m_MapperID);
00284 if ( mapper.IsNull() )
00285 continue;
00286
00287
00288 if ( mapper->IsLODEnabled( this ) && mapper->IsVisible( this ) )
00289 {
00290 ++m_NumberOfVisibleLODEnabledMappers;
00291 }
00292
00293 int layer = 1;
00294 node->GetIntProperty("layer", layer, this);
00295 int nr = (layer<<16) + mapperNo;
00296 m_MappersMap.insert( std::pair< int, Mapper * >( nr, mapper ) );
00297 mapperNo++;
00298 }
00299 }
00300
00306 void mitk::VtkPropRenderer::Enable2DOpenGL()
00307 {
00308 GLint iViewport[4];
00309
00310
00311 glGetIntegerv( GL_VIEWPORT, iViewport );
00312
00313
00314
00315 glMatrixMode( GL_PROJECTION );
00316 glPushMatrix();
00317 glLoadIdentity();
00318
00319
00320 glOrtho(
00321 iViewport[0], iViewport[0]+iViewport[2],
00322 iViewport[1], iViewport[1]+iViewport[3],
00323 -1.0, 1.0
00324 );
00325 glMatrixMode( GL_MODELVIEW );
00326 glPushMatrix();
00327 glLoadIdentity();
00328
00329
00330
00331 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT );
00332 glDisable( GL_DEPTH_TEST );
00333 glDisable( GL_LIGHTING );
00334 }
00335
00341 void mitk::VtkPropRenderer::Disable2DOpenGL()
00342 {
00343 glPopAttrib();
00344 glMatrixMode( GL_PROJECTION );
00345 glPopMatrix();
00346 glMatrixMode( GL_MODELVIEW );
00347 glPopMatrix();
00348 }
00349
00350 void mitk::VtkPropRenderer::Update(mitk::DataNode* datatreenode)
00351 {
00352 if(datatreenode!=NULL)
00353 {
00354 mitk::Mapper::Pointer mapper = datatreenode->GetMapper(m_MapperID);
00355 if(mapper.IsNotNull())
00356 {
00357 Mapper2D* mapper2d=dynamic_cast<Mapper2D*>(mapper.GetPointer());
00358 if(mapper2d != NULL)
00359 {
00360 if(GetDisplayGeometry()->IsValid())
00361 {
00362 VtkMapper2D* vtkmapper2d=dynamic_cast<VtkMapper2D*>(mapper.GetPointer());
00363 if(vtkmapper2d != NULL)
00364 {
00365 vtkmapper2d->Update(this);
00366 m_VtkMapperPresent=true;
00367 }
00368 else
00369 mapper2d->Update(this);
00370 }
00371 }
00372 else
00373 {
00374 VtkMapper3D* vtkmapper3d=dynamic_cast<VtkMapper3D*>(mapper.GetPointer());
00375 if(vtkmapper3d != NULL)
00376 {
00377 vtkmapper3d->Update(this);
00378 vtkmapper3d->UpdateVtkTransform(this);
00379 m_VtkMapperPresent=true;
00380 }
00381 }
00382 }
00383 }
00384 }
00385
00386
00387 void mitk::VtkPropRenderer::Update()
00388 {
00389 if( m_DataStorage.IsNull() )
00390 return;
00391
00392 m_VtkMapperPresent = false;
00393 mitk::DataStorage::SetOfObjects::ConstPointer all = m_DataStorage->GetAll();
00394 for (mitk::DataStorage::SetOfObjects::ConstIterator it = all->Begin(); it != all->End(); ++it)
00395 Update(it->Value());
00396
00397 Modified();
00398 m_LastUpdateTime = GetMTime();
00399 }
00400
00401
00407 void mitk::VtkPropRenderer::InitRenderer(vtkRenderWindow* renderWindow)
00408 {
00409 BaseRenderer::InitRenderer(renderWindow);
00410
00411 if(renderWindow == NULL)
00412 {
00413 m_InitNeeded = false;
00414 m_ResizeNeeded = false;
00415 return;
00416 }
00417
00418 m_InitNeeded = true;
00419 m_ResizeNeeded = true;
00420
00421 m_LastUpdateTime = 0;
00422
00423 }
00424
00425
00429 void mitk::VtkPropRenderer::Resize(int w, int h)
00430 {
00431 BaseRenderer::Resize(w, h);
00432
00433 Update();
00434 }
00435
00436
00437 void mitk::VtkPropRenderer::InitSize(int w, int h)
00438 {
00439 m_RenderWindow->SetSize(w,h);
00440 Superclass::InitSize(w, h);
00441 Modified();
00442 Update();
00443 if(m_VtkRenderer!=NULL)
00444 {
00445 int w=vtkObject::GetGlobalWarningDisplay();
00446 vtkObject::GlobalWarningDisplayOff();
00447 m_VtkRenderer->ResetCamera();
00448 vtkObject::SetGlobalWarningDisplay(w);
00449 }
00450 }
00451
00452
00453 void mitk::VtkPropRenderer::SetMapperID(const MapperSlotId mapperId)
00454 {
00455 if(m_MapperID != mapperId)
00456 Superclass::SetMapperID(mapperId);
00457
00458
00459 checkState();
00460 }
00461
00462
00466 void mitk::VtkPropRenderer::MakeCurrent()
00467 {
00468 if(m_RenderWindow!=NULL)
00469 m_RenderWindow->MakeCurrent();
00470 }
00471
00472
00473 void mitk::VtkPropRenderer::PickWorldPoint(const mitk::Point2D& displayPoint, mitk::Point3D& worldPoint) const
00474 {
00475 if(m_VtkMapperPresent)
00476 {
00477
00478 switch ( m_PickingMode )
00479 {
00480 case (WorldPointPicking) :
00481 {
00482 m_WorldPointPicker->Pick(displayPoint[0], displayPoint[1], 0, m_VtkRenderer);
00483 vtk2itk(m_WorldPointPicker->GetPickPosition(), worldPoint);
00484 break;
00485 }
00486 case (PointPicking) :
00487 {
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 m_PointPicker->Pick(displayPoint[0], displayPoint[1], 0, m_VtkRenderer);
00523 vtk2itk(m_PointPicker->GetPickPosition(), worldPoint);
00524 break;
00525 }
00526 }
00527 }
00528 else
00529 {
00530 Superclass::PickWorldPoint(displayPoint, worldPoint);
00531 }
00532 }
00533
00534 mitk::DataNode *
00535 mitk::VtkPropRenderer::PickObject( const Point2D &displayPosition, Point3D &worldPosition ) const
00536 {
00537 if ( m_VtkMapperPresent )
00538 {
00539 m_CellPicker->InitializePickList();
00540
00541
00542
00543 DataStorage::SetOfObjects::ConstPointer allObjects = m_DataStorage->GetAll();
00544 for ( DataStorage::SetOfObjects::ConstIterator it = allObjects->Begin();
00545 it != allObjects->End();
00546 ++it )
00547 {
00548 DataNode *node = it->Value();
00549 if ( node == NULL )
00550 continue;
00551
00552 bool pickable = false;
00553 node->GetBoolProperty( "pickable", pickable );
00554 if ( !pickable )
00555 continue;
00556
00557 VtkMapper3D *mapper = dynamic_cast< VtkMapper3D * >
00558 ( node->GetMapper( m_MapperID ) );
00559 if ( mapper == NULL )
00560 continue;
00561
00562 vtkProp *prop = mapper->GetVtkProp( (mitk::BaseRenderer *)this );
00563 if ( prop == NULL )
00564 continue;
00565
00566 m_CellPicker->AddPickList( prop );
00567 }
00568
00569
00570
00571 m_CellPicker->PickFromListOn();
00572 m_CellPicker->Pick( displayPosition[0], displayPosition[1], 0.0, m_VtkRenderer );
00573 m_CellPicker->PickFromListOff();
00574
00575 vtk2itk( m_CellPicker->GetPickPosition(), worldPosition );
00576 vtkProp *prop = m_CellPicker->GetViewProp();
00577
00578 if ( prop == NULL )
00579 {
00580 return NULL;
00581 }
00582
00583
00584
00585 for ( DataStorage::SetOfObjects::ConstIterator it = allObjects->Begin();
00586 it != allObjects->End();
00587 ++it)
00588 {
00589 DataNode::Pointer node = it->Value();
00590 if ( node.IsNull() )
00591 continue;
00592
00593 mitk::Mapper::Pointer mapper = node->GetMapper( m_MapperID );
00594 if ( mapper.IsNull() )
00595 continue;
00596
00597 if ( mapper->HasVtkProp( prop, const_cast< mitk::VtkPropRenderer * >( this ) ) )
00598 {
00599 return node;
00600 }
00601 }
00602
00603 return NULL;
00604 }
00605 else
00606 {
00607 return Superclass::PickObject( displayPosition, worldPosition );
00608 }
00609 };
00610
00611
00612
00617 int mitk::VtkPropRenderer::WriteSimpleText(std::string text, double posX, double posY, double color1, double color2, double color3)
00618 {
00619 if(text.size() > 0)
00620 {
00621 vtkTextActor* textActor = vtkTextActor::New();
00622
00623 textActor->SetPosition(posX,posY);
00624 textActor->SetInput(text.c_str());
00625 textActor->GetTextProperty()->SetColor(color1, color2, color3);
00626 int text_id = m_TextCollection.size();
00627 m_TextCollection.insert(TextMapType::value_type(text_id,textActor));
00628 return text_id;
00629 }
00630 return -1;
00631 }
00632
00633
00637 vtkTextProperty* mitk::VtkPropRenderer::GetTextLabelProperty(int text_id)
00638 {
00639 return this->m_TextCollection[text_id]->GetTextProperty();
00640 }
00641
00642
00643 void mitk::VtkPropRenderer::InitPathTraversal()
00644 {
00645 if (m_DataStorage.IsNotNull())
00646 {
00647 m_PickingObjects = m_DataStorage->GetAll();
00648 m_PickingObjectsIterator = m_PickingObjects->begin();
00649 }
00650 }
00651
00652
00653 vtkAssemblyPath* mitk::VtkPropRenderer::GetNextPath()
00654 {
00655 if (m_DataStorage.IsNull() )
00656 {
00657 return NULL;
00658 }
00659
00660 if ( m_PickingObjectsIterator == m_PickingObjects->end() )
00661 {
00662 return NULL;
00663 }
00664
00665 vtkAssemblyPath* returnPath = vtkAssemblyPath::New();
00666
00667
00668 bool success = false;
00669
00670 while (!success)
00671 {
00672
00673 const DataNode* node = *m_PickingObjectsIterator;
00674 if (node)
00675 {
00676 Mapper* mapper = node->GetMapper( BaseRenderer::Standard3D );
00677 if (mapper)
00678 {
00679 VtkMapper3D* vtkmapper = dynamic_cast<VtkMapper3D*>( mapper );
00680 if (vtkmapper)
00681 {
00682 vtkProp* prop = vtkmapper->GetVtkProp(this);
00683 if ( prop && prop->GetVisibility() )
00684 {
00685
00686 returnPath->AddNode( prop, prop->GetMatrix() );
00687 success = true;
00688 }
00689 }
00690 }
00691 }
00692
00693 ++m_PickingObjectsIterator;
00694
00695 if ( m_PickingObjectsIterator == m_PickingObjects->end() ) break;
00696 }
00697
00698 if ( success )
00699 {
00700 return returnPath;
00701 }
00702 else
00703 {
00704 return NULL;
00705 }
00706 }
00707
00708
00709 void mitk::VtkPropRenderer::ReleaseGraphicsResources(vtkWindow *renWin)
00710 {
00711 if( m_DataStorage.IsNull() )
00712 return;
00713
00714 DataStorage::SetOfObjects::ConstPointer allObjects = m_DataStorage->GetAll();
00715 for (DataStorage::SetOfObjects::const_iterator iter = allObjects->begin(); iter != allObjects->end(); ++iter)
00716 {
00717 DataNode::Pointer node = *iter;
00718 if ( node.IsNull() )
00719 continue;
00720
00721 Mapper::Pointer mapper = node->GetMapper(m_MapperID);
00722 if(mapper.IsNotNull())
00723 mapper->ReleaseGraphicsResources(renWin);
00724 }
00725 }
00726
00727
00728 const vtkWorldPointPicker *mitk::VtkPropRenderer::GetWorldPointPicker() const
00729 {
00730 return m_WorldPointPicker;
00731 }
00732
00733
00734 const vtkPointPicker *mitk::VtkPropRenderer::GetPointPicker() const
00735 {
00736 return m_PointPicker;
00737 }
00738
00739
00740 const vtkCellPicker *mitk::VtkPropRenderer::GetCellPicker() const
00741 {
00742 return m_CellPicker;
00743 }
00744
00745
00746 #if ( ( VTK_MAJOR_VERSION >= 5 ) && ( VTK_MINOR_VERSION>=2) )
00747 mitk::VtkPropRenderer::MappersMapType mitk::VtkPropRenderer::GetMappersMap() const
00748 {
00749 return m_MappersMap;
00750 }
00751 #endif
00752
00753
00754
00755
00756 static int glWorkAroundGlobalCount = 0;
00757
00758 bool mitk::VtkPropRenderer::useImmediateModeRendering()
00759 {
00760 return glWorkAroundGlobalCount>1;
00761 }
00762
00763 void mitk::VtkPropRenderer::checkState()
00764 {
00765 if (m_MapperID == Standard3D)
00766 {
00767 if (!didCount)
00768 {
00769 didCount = true;
00770 glWorkAroundGlobalCount++;
00771
00772 if (glWorkAroundGlobalCount == 2)
00773 {
00774 MITK_INFO << "Multiple 3D Renderwindows active...: turning Immediate Rendering ON for legacy mappers";
00775
00776 }
00777
00778
00779 }
00780 }
00781 else
00782 {
00783 if(didCount)
00784 {
00785 didCount=false;
00786 glWorkAroundGlobalCount--;
00787 if(glWorkAroundGlobalCount==1)
00788 {
00789 MITK_INFO << "Single 3D Renderwindow active...: turning Immediate Rendering OFF for legacy mappers";
00790
00791 }
00792
00793 }
00794 }
00795 }