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 void ctkVTKRenderView::scheduleRender()
00114 {
00115   CTK_D(ctkVTKRenderView);
00116   if (!d->RenderEnabled)
00117     {
00118     return;
00119     }
00120   if (!d->RenderPending)
00121     {
00122     d->RenderPending = true;
00123     QTimer::singleShot(0, this, SLOT(forceRender()));
00124     }
00125 }
00126 
00127 //----------------------------------------------------------------------------
00128 void ctkVTKRenderView::forceRender()
00129 {
00130   CTK_D(ctkVTKRenderView);
00131   if (!d->RenderEnabled)
00132     {
00133     return;
00134     }
00135   d->RenderWindow->Render();
00136   d->RenderPending = false;
00137 }
00138 
00139 //----------------------------------------------------------------------------
00140 CTK_GET_CXX(ctkVTKRenderView, vtkRenderWindow*, renderWindow, RenderWindow);
00141 
00142 //----------------------------------------------------------------------------
00143 CTK_GET_CXX(ctkVTKRenderView, vtkRenderWindowInteractor*, interactor, CurrentInteractor);
00144 
00145 //----------------------------------------------------------------------------
00146 void ctkVTKRenderView::setInteractor(vtkRenderWindowInteractor* newInteractor)
00147 {
00148   CTK_D(ctkVTKRenderView);
00149   d->RenderWindow->SetInteractor(newInteractor);
00150   d->Orientation->SetOrientationMarker(d->Axes);
00151   d->Orientation->SetInteractor(newInteractor);
00152   d->Orientation->SetEnabled(1);
00153   d->Orientation->InteractiveOff();
00154   d->CurrentInteractor = newInteractor; 
00155 }
00156 
00157 //----------------------------------------------------------------------------
00158 vtkInteractorObserver* ctkVTKRenderView::interactorStyle()
00159 {
00160   CTK_D(ctkVTKRenderView);
00161   if (d->CurrentInteractor)
00162     {
00163     return d->CurrentInteractor->GetInteractorStyle();
00164     }
00165   else
00166     {
00167     return 0;
00168     }
00169 }
00170 
00171 //----------------------------------------------------------------------------
00172 void ctkVTKRenderView::setCornerAnnotationText(const QString& text)
00173 {
00174   CTK_D(ctkVTKRenderView);
00175   d->CornerAnnotation->ClearAllTexts();
00176   d->CornerAnnotation->SetText(2, text.toLatin1());
00177 }
00178 
00179 // --------------------------------------------------------------------------
00180 void ctkVTKRenderView::setBackgroundColor(double r, double g, double b)
00181 {
00182   CTK_D(ctkVTKRenderView);
00183   double background_color[3] = {r, g, b};
00184   d->Renderer->SetBackground(background_color);
00185 }
00186 
00187 //----------------------------------------------------------------------------
00188 vtkCamera* ctkVTKRenderView::activeCamera()
00189 {
00190   CTK_D(ctkVTKRenderView);
00191   if (d->Renderer->IsActiveCameraCreated())
00192     {
00193     return d->Renderer->GetActiveCamera();
00194     }
00195   else
00196     {
00197     return 0;
00198     }
00199 }
00200 
00201 //----------------------------------------------------------------------------
00202 void ctkVTKRenderView::resetCamera()
00203 {
00204   CTK_D(ctkVTKRenderView);
00205   d->Renderer->ResetCamera();
00206 }
00207 
00208 //----------------------------------------------------------------------------
00209 CTK_GET_CXX(ctkVTKRenderView, vtkRenderer*, renderer, Renderer);
00210 
00211 //----------------------------------------------------------------------------
00212 CTK_SET_CXX(ctkVTKRenderView, bool, setRenderEnabled, RenderEnabled);
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1