ctkVTKRenderView.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QTimer>
00023 #include <QVBoxLayout>
00024 
00025 // CTK includes
00026 #include "ctkVTKRenderView.h"
00027 #include "ctkVTKRenderView_p.h"
00028 
00029 // VTK includes
00030 #include <vtkRendererCollection.h>
00031 #include <vtkRenderWindowInteractor.h>
00032 #include <vtkTextProperty.h>
00033 
00034 // --------------------------------------------------------------------------
00035 // ctkVTKRenderViewPrivate methods
00036 
00037 // --------------------------------------------------------------------------
00038 ctkVTKRenderViewPrivate::ctkVTKRenderViewPrivate()
00039 {
00040   this->Renderer = vtkSmartPointer<vtkRenderer>::New();
00041   this->RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
00042   this->Axes = vtkSmartPointer<vtkAxesActor>::New();
00043   this->Orientation = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
00044   this->CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
00045   this->RenderPending = false;
00046   this->RenderEnabled = false;
00047 }
00048 
00049 // --------------------------------------------------------------------------
00050 void ctkVTKRenderViewPrivate::setupCornerAnnotation()
00051 {
00052   if (!this->Renderer->HasViewProp(this->CornerAnnotation))
00053     {
00054     this->Renderer->AddViewProp(this->CornerAnnotation);
00055     this->CornerAnnotation->SetMaximumLineHeight(0.07);
00056     vtkTextProperty *tprop = this->CornerAnnotation->GetTextProperty();
00057     tprop->ShadowOn();
00058     }
00059   this->CornerAnnotation->ClearAllTexts();
00060 }
00061 
00062 //---------------------------------------------------------------------------
00063 void ctkVTKRenderViewPrivate::setupRendering()
00064 {
00065   Q_ASSERT(this->RenderWindow);
00066   this->RenderWindow->SetAlphaBitPlanes(1);
00067   this->RenderWindow->SetMultiSamples(0);
00068   this->RenderWindow->StereoCapableWindowOn();
00069   
00070   this->RenderWindow->GetRenderers()->RemoveAllItems();
00071   
00072   // Add renderer
00073   this->RenderWindow->AddRenderer(this->Renderer);
00074   
00075   // Setup the corner annotation
00076   this->setupCornerAnnotation();
00077 
00078   this->VTKWidget->SetRenderWindow(this->RenderWindow);
00079 }
00080 
00081 //---------------------------------------------------------------------------
00082 void ctkVTKRenderViewPrivate::setupDefaultInteractor()
00083 {
00084   CTK_P(ctkVTKRenderView);
00085   p->setInteractor(this->RenderWindow->GetInteractor());
00086 }
00087 
00088 //---------------------------------------------------------------------------
00089 // ctkVTKRenderView methods
00090 
00091 // --------------------------------------------------------------------------
00092 ctkVTKRenderView::ctkVTKRenderView(QWidget* _parent) : Superclass(_parent)
00093 {
00094   CTK_INIT_PRIVATE(ctkVTKRenderView);
00095   CTK_D(ctkVTKRenderView);
00096   
00097   d->VTKWidget = new QVTKWidget(this);
00098   this->setLayout(new QVBoxLayout);
00099   this->layout()->setMargin(0);
00100   this->layout()->setSpacing(0);
00101   this->layout()->addWidget(d->VTKWidget);
00102 
00103   d->setupRendering();
00104   d->setupDefaultInteractor();
00105 }
00106 
00107 // --------------------------------------------------------------------------
00108 ctkVTKRenderView::~ctkVTKRenderView()
00109 {
00110 }
00111 
00112 //----------------------------------------------------------------------------
00113 CTK_GET_CXX(ctkVTKRenderView, vtkRenderWindowInteractor*, interactor, CurrentInteractor);
00114 
00115 //----------------------------------------------------------------------------
00116 void ctkVTKRenderView::scheduleRender()
00117 {
00118   CTK_D(ctkVTKRenderView);
00119   if (!d->RenderEnabled)
00120     {
00121     return;
00122     }
00123   if (!d->RenderPending)
00124     {
00125     d->RenderPending = true;
00126     QTimer::singleShot(0, this, SLOT(forceRender()));
00127     }
00128 }
00129 
00130 //----------------------------------------------------------------------------
00131 void ctkVTKRenderView::forceRender()
00132 {
00133   CTK_D(ctkVTKRenderView);
00134   if (!d->RenderEnabled)
00135     {
00136     return;
00137     }
00138   d->RenderWindow->Render();
00139   d->RenderPending = false;
00140 }
00141 
00142 //----------------------------------------------------------------------------
00143 void ctkVTKRenderView::setInteractor(vtkRenderWindowInteractor* newInteractor)
00144 {
00145   Q_ASSERT(newInteractor);
00146   CTK_D(ctkVTKRenderView);
00147   d->RenderWindow->SetInteractor(newInteractor);
00148   d->Orientation->SetOrientationMarker(d->Axes);
00149   d->Orientation->SetInteractor(newInteractor);
00150   d->Orientation->SetEnabled(1);
00151   d->Orientation->InteractiveOff();
00152   d->CurrentInteractor = newInteractor; 
00153 }
00154 
00155 //----------------------------------------------------------------------------
00156 void ctkVTKRenderView::setCornerAnnotationText(const QString& text)
00157 {
00158   CTK_D(ctkVTKRenderView);
00159   d->CornerAnnotation->ClearAllTexts();
00160   d->CornerAnnotation->SetText(2, text.toLatin1());
00161 }
00162 
00163 // --------------------------------------------------------------------------
00164 void ctkVTKRenderView::setBackgroundColor(double r, double g, double b)
00165 {
00166   CTK_D(ctkVTKRenderView);
00167   double background_color[3] = {r, g, b};
00168   d->Renderer->SetBackground(background_color);
00169 }
00170 
00171 //----------------------------------------------------------------------------
00172 vtkCamera* ctkVTKRenderView::activeCamera()
00173 {
00174   CTK_D(ctkVTKRenderView);
00175   if (d->Renderer->IsActiveCameraCreated())
00176     {
00177     return 0;
00178     }
00179   else
00180     {
00181     return d->Renderer->GetActiveCamera();
00182     }
00183 }
00184 
00185 //----------------------------------------------------------------------------
00186 void ctkVTKRenderView::resetCamera()
00187 {
00188   CTK_D(ctkVTKRenderView);
00189   d->Renderer->ResetCamera();
00190 }
00191 
00192 //----------------------------------------------------------------------------
00193 CTK_GET_CXX(ctkVTKRenderView, vtkRenderer*, renderer, Renderer);
00194 
00195 //----------------------------------------------------------------------------
00196 CTK_SET_CXX(ctkVTKRenderView, bool, setRenderEnabled, RenderEnabled);
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines