vtkwin.h

00001 /***********************************************************************************
00002  * VTKwrap - Simple VTK wrappers                                                   *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>     *
00004  *                                                                                 *
00005  * This file is part of VTKwrap.                                                   *
00006  *                                                                                 *
00007  * VTKwrap is free software; you can redistribute it and/or modify it under        *
00008  * the  terms of the GNU General Public License as published by the Free Software  *
00009  * Foundation; either version 2 of the License, or (at your option) any later      *
00010  * version.                                                                        *
00011  *                                                                                 *
00012  * VTKwrap is distributed in the hope that it will be useful, but WITHOUT ANY      *
00013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
00014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.        *
00015  *                                                                                 *
00016  * You should have received a copy of the GNU General Public License along with    *
00017  * VTKwrap; if not, write to the Free Software Foundation, Inc., 51 Franklin       *
00018  * Street, Fifth Floor, Boston, MA 02110-1301, USA                                 *
00019  ***********************************************************************************/
00020 
00021 #ifndef VTKWRAP_VTKWIN_H
00022 #define VTKWRAP_VTKWIN_H
00023 
00024 #include "vtkCamera.h"
00025 #include "vtkRenderer.h"
00026 #include "vtkRenderWindow.h"
00027 #include "vtkRenderWindowInteractor.h"
00028 #include "vtkInteractorStyleSwitch.h"
00029 #include "vtkActor.h"
00030 #include "vtkActor2D.h"
00031 #include "vtkWindowToImageFilter.h"
00032 #include "vtkPNGWriter.h"
00033 
00034 class VTKWin
00035 {
00036 public:
00037     // Constructor
00038     VTKWin(int Width=300, int Height=300);
00039 
00040     // Alternative constructor
00041     VTKWin(vtkRenderWindowInteractor * vtkRWI, int Width=300, int Height=300);
00042 
00043     // Destructor
00044     ~VTKWin();
00045 
00046     // Methods
00047     void AddActor        (vtkActor   * TheActor) { _renderer -> AddActor      (TheActor); }
00048     void AddActor2D      (vtkActor2D * TheActor) { _renderer -> AddActor2D    (TheActor); }
00049     void DelActor        (vtkActor   * TheActor) { _renderer -> RemoveActor   (TheActor); }
00050     void DelActor2D      (vtkActor2D * TheActor) { _renderer -> RemoveActor2D (TheActor); }
00051     void AddLight        (vtkLight   * Light)    { _renderer -> AddLight      (Light);    }
00052     void Show            ();
00053     void SetViewDefault  ();
00054     void SetViewPIplane  ();
00055     void WritePNG        (char const * Filename);
00056     void ResetCamera     ();
00057 
00058     // Access methods
00059     vtkRenderer               * GetRen    () { return _renderer;   }
00060     vtkRenderWindowInteractor * GetIRen   () { return _interactor; }
00061     vtkCamera                 * GetCamera () { return _camera;     }
00062 
00063 private:
00064     // Data
00065     vtkCamera                 * _camera;
00066     vtkRenderer               * _renderer;
00067     vtkRenderWindow           * _ren_win;
00068     vtkRenderWindowInteractor * _interactor;
00069     vtkInteractorStyleSwitch  * _int_switch;
00070 
00071 }; // class VTKWin
00072 
00073 
00075 
00076 
00077 inline VTKWin::VTKWin(int Width, int Height) // {{{
00078 {
00079     _camera = vtkCamera::New();
00080     SetViewDefault();
00081 
00082     _renderer = vtkRenderer::New();
00083     _ren_win  = vtkRenderWindow::New();
00084     _ren_win->AddRenderer(_renderer);
00085     _ren_win->SetSize(Width,Height);
00086 
00087     _interactor = vtkRenderWindowInteractor::New();
00088     _int_switch = vtkInteractorStyleSwitch::New();
00089     _interactor -> SetRenderWindow(_ren_win);
00090     _interactor -> SetInteractorStyle(_int_switch);
00091     _int_switch -> SetCurrentStyleToTrackballCamera();
00092 
00093     _renderer -> SetActiveCamera(_camera);
00094     _renderer -> SetBackground(1,1,1);
00095 
00096 } // }}}
00097 
00098 inline VTKWin::VTKWin(vtkRenderWindowInteractor * vtkRWI, int Width, int Height) // {{{
00099 {
00100     _camera = vtkCamera::New();
00101     SetViewDefault();
00102 
00103     _renderer = vtkRenderer::New();
00104     _ren_win  = vtkRenderWindow::New();
00105     _ren_win->AddRenderer(_renderer);
00106     _ren_win->SetSize(Width,Height);
00107 
00108     _interactor = vtkRWI;
00109     _int_switch = vtkInteractorStyleSwitch::New();
00110     _interactor -> SetRenderWindow(_ren_win);
00111     _interactor -> SetInteractorStyle(_int_switch);
00112     _int_switch -> SetCurrentStyleToTrackballCamera();
00113 
00114     _renderer -> SetActiveCamera(_camera);
00115     _renderer -> SetBackground(1,1,1);
00116 
00117 } // }}}
00118 
00119 inline VTKWin::~VTKWin() // {{{
00120 {
00121     _camera     -> Delete();
00122     _renderer   -> Delete();
00123     _ren_win    -> Delete();
00124     _interactor -> Delete();
00125     _int_switch -> Delete();
00126 
00127 } // }}}
00128 
00129 inline void VTKWin::Show() // {{{
00130 {
00131     _ren_win    -> Render();
00132     _interactor -> Start();
00133 
00134 } // }}}
00135 
00136 inline void VTKWin::SetViewDefault() // {{{
00137 {
00138     _camera->SetViewUp     (0,0,1);
00139     _camera->SetPosition   (2,1,1);
00140     _camera->SetFocalPoint (0,0,0);
00141 
00142 } // }}}
00143 
00144 inline void VTKWin::SetViewPIplane() // {{{
00145 {
00146     _camera->SetViewUp     (0,0,1);
00147     _camera->SetPosition   (1,1,1);
00148     _camera->SetFocalPoint (0,0,0);
00149 
00150 } // }}}
00151 
00152 inline void VTKWin::WritePNG(char const * Filename) // {{{
00153 {
00154     // Window to image filter
00155     vtkWindowToImageFilter * win_to_img = vtkWindowToImageFilter::New();
00156     win_to_img -> SetInput(_ren_win);
00157     win_to_img -> Update();
00158 
00159     // PNG writer
00160     vtkPNGWriter * writer = vtkPNGWriter::New();
00161     writer -> SetInput(win_to_img->GetOutput());
00162     writer -> SetFileName(Filename);
00163     writer -> Write();
00164 
00165     // Clean up
00166     win_to_img -> Delete();
00167     writer     -> Delete();
00168 
00169 } // }}}
00170 
00171 inline void VTKWin::ResetCamera() // {{{
00172 {
00173     SetViewDefault();
00174     _renderer -> SetActiveCamera(_camera);
00175     _renderer -> ResetCamera();
00176     _renderer -> SetBackground(1,1,1);
00177 
00178 } // }}}
00179 
00180 #endif // VTKWRAP_VTKWIN_H
00181 
00182 // vim:fdm=marker

Generated on Wed Jan 24 15:56:28 2007 for MechSys by  doxygen 1.4.7