MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/vtk/win.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raúl D. D. Farfan             *
00004  *                                                                      *
00005  * This program is free software: you can redistribute it and/or modify *
00006  * it under the terms of the GNU General Public License as published by *
00007  * the Free Software Foundation, either version 3 of the License, or    *
00008  * any later version.                                                   *
00009  *                                                                      *
00010  * This program is distributed in the hope that it will be useful,      *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00013  * GNU General Public License for more details.                         *
00014  *                                                                      *
00015  * You should have received a copy of the GNU General Public License    *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00017  ************************************************************************/
00018 
00019 #ifndef MECHSYS_VTKWIN_H
00020 #define MECHSYS_VTKWIN_H
00021 
00022 // VTK
00023 #include <vtkCamera.h>
00024 #include <vtkRenderer.h>
00025 #include <vtkRenderWindow.h>
00026 #include <vtkRenderWindowInteractor.h>
00027 #include <vtkInteractorStyleSwitch.h>
00028 #include <vtkActor.h>
00029 #include <vtkWindowToImageFilter.h>
00030 #include <vtkPNGWriter.h>
00031 #include <vtkRenderLargeImage.h>
00032 
00033 // MechSys
00034 #include <mechsys/util/string.h>
00035 #include <mechsys/util/colors.h>
00036 
00037 namespace VTK
00038 {
00039 
00040 class Win
00041 {
00042 public:
00043     // Constructors
00044     Win (int Width=600, int Height=600, double BgRed=1.0, double BgGreen=1.0, double BgBlue=1.0);
00045     Win (vtkRenderWindowInteractor * vtkRWI, int Width=600, int Height=600, double BgRed=1.0, double BgGreen=1.0, double BgBlue=1.0);
00046 
00047     // Destructor
00048     ~Win ();
00049 
00050     // Methods
00051     void AddActor (vtkActor * TheActor, bool RstCam=true);
00052     void DelActor (vtkActor * TheActor) { _renderer -> RemoveActor(TheActor); }
00053     void AddLight (vtkLight * Light)    { _renderer -> AddLight(Light);       }
00054     void Render   ()                    { _ren_win  -> Render();              }
00055     void Show     ();
00056     void WritePNG (char const * Filename, bool Large=false, int Magnification=1);
00057     void Camera   (double xUp, double yUp, double zUp, double xFoc, double yFoc, double zFoc, double xPos, double yPos, double zPos);
00058     void Parallel (bool ParallelProjection=true) { _camera->SetParallelProjection(ParallelProjection); }
00059 
00060     // (low level) Access methods
00061     vtkRenderer * GetRen    () { return _renderer; }
00062     vtkCamera   * GetCamera () { return _camera;   }
00063 
00064     // Set methods
00065     Win & SetViewDefault (bool RevCam=false);
00066     Win & SetViewPIplane (bool RevCam=false);
00067     Win & SetBgColor     (char const * Name="white");
00068     Win & SetBgColor     (double BgRed, double BgGreen, double BgBlue);
00069 
00070 private:
00071     vtkCamera                 * _camera;
00072     vtkRenderer               * _renderer;
00073     vtkRenderWindow           * _ren_win;
00074     vtkRenderWindowInteractor * _interactor;
00075     vtkInteractorStyleSwitch  * _int_switch;
00076 };
00077 
00078 
00080 
00081 
00082 inline Win::Win (int Width, int Height, double BgRed, double BgGreen, double BgBlue)
00083 {
00084     _camera = vtkCamera::New();
00085     SetViewDefault();
00086 
00087     _renderer = vtkRenderer::New();
00088     _ren_win  = vtkRenderWindow::New();
00089     _ren_win  -> AddRenderer   (_renderer);
00090     _ren_win  -> SetSize       (Width, Height);
00091     _renderer -> SetBackground (BgRed, BgGreen, BgBlue);
00092 
00093     _interactor = vtkRenderWindowInteractor::New();
00094     _int_switch = vtkInteractorStyleSwitch::New();
00095     _interactor -> SetRenderWindow    (_ren_win);
00096     _interactor -> SetInteractorStyle (_int_switch);
00097     _int_switch -> SetCurrentStyleToTrackballCamera();
00098 }
00099 
00100 inline Win::Win (vtkRenderWindowInteractor * vtkRWI, int Width, int Height, double BgRed, double BgGreen, double BgBlue)
00101 {
00102     _camera = vtkCamera::New();
00103     SetViewDefault();
00104 
00105     _renderer = vtkRenderer::New();
00106     _ren_win  = vtkRenderWindow::New();
00107     _ren_win  -> AddRenderer   (_renderer);
00108     _ren_win  -> SetSize       (Width, Height);
00109     _renderer -> SetBackground (BgRed, BgGreen, BgBlue);
00110 
00111     _interactor = vtkRWI;
00112     _int_switch = vtkInteractorStyleSwitch::New();
00113     _interactor -> SetRenderWindow    (_ren_win);
00114     _interactor -> SetInteractorStyle (_int_switch);
00115     _int_switch -> SetCurrentStyleToTrackballCamera();
00116 }
00117 
00118 inline Win::~Win ()
00119 {
00120     _camera     -> Delete();
00121     _renderer   -> Delete();
00122     _ren_win    -> Delete();
00123     _interactor -> Delete();
00124     _int_switch -> Delete();
00125 }
00126 
00127 inline void Win::AddActor (vtkActor * TheActor, bool RstCam)
00128 {
00129     _renderer -> AddActor (TheActor);
00130     if (RstCam)
00131     {
00132         _renderer -> SetActiveCamera (_camera);
00133         _renderer -> ResetCamera     ();
00134     }
00135 }
00136 
00137 inline void Win::Show ()
00138 {
00139     Render ();
00140     _interactor->Start ();
00141 }
00142 
00143 inline void Win::WritePNG (char const * Filekey, bool Large, int Magnification)
00144 {
00145     // png writer
00146     String fname(Filekey);   fname += ".png";
00147     vtkPNGWriter * writer = vtkPNGWriter::New();
00148     writer -> SetFileName (fname.CStr());
00149 
00150     // re-render window
00151     Render();
00152 
00153     // write
00154     if (Large)
00155     {
00156         vtkRenderLargeImage * large_img = vtkRenderLargeImage::New();
00157         large_img -> SetInput           (_renderer);
00158         large_img -> Update             ();
00159         large_img -> SetMagnification   (Magnification);
00160         writer    -> SetInputConnection (large_img->GetOutputPort());
00161         writer    -> Write              ();
00162         large_img -> Delete             ();
00163     }
00164     else
00165     {
00166         vtkWindowToImageFilter * win_to_img = vtkWindowToImageFilter::New();
00167         win_to_img -> SetInput (_ren_win);
00168         win_to_img -> Update   ();
00169         writer     -> SetInput (win_to_img->GetOutput());
00170         writer     -> Write    ();
00171         win_to_img -> Delete   ();
00172     }
00173 
00174     // clean up
00175     writer -> Delete();
00176 
00177     // Notification
00178     printf("File <%s%s.png%s> written\n", TERM_CLR_BLUE_H, Filekey, TERM_RST);
00179 }
00180 
00181 inline void Win::Camera (double xUp, double yUp, double zUp, double xFoc, double yFoc, double zFoc, double xPos, double yPos, double zPos)
00182 {
00183     _camera->SetViewUp     (xUp, yUp, zUp);
00184     _camera->SetFocalPoint (xFoc,yFoc,zFoc);
00185     _camera->SetPosition   (xPos,yPos,zPos);
00186     _renderer->ResetCamera ();
00187 }
00188 
00189 inline Win & Win::SetViewDefault (bool RevCam)
00190 {
00191     double c = (RevCam ? -1 : 1);
00192     _camera->SetViewUp     (0,0,c);
00193     _camera->SetPosition   (2*c,c,c);
00194     _camera->SetFocalPoint (0,0,0);
00195     return (*this);
00196 }
00197 
00198 inline Win & Win::SetViewPIplane (bool RevCam)
00199 {
00200     double c = (RevCam ? -1 : 1);
00201     _camera->SetViewUp     (0,0,c);
00202     _camera->SetPosition   (c,c,c);
00203     _camera->SetFocalPoint (0,0,0);
00204     return (*this);
00205 }
00206 
00207 inline Win & Win::SetBgColor (char const * Name)
00208 {
00209     Vec3_t bgclr = Colors::Get(Name);
00210     _renderer -> SetBackground (bgclr(0), bgclr(1), bgclr(2));
00211     return (*this);
00212 }
00213 
00214 inline Win & Win::SetBgColor (double BgRed, double BgGreen, double BgBlue)
00215 {
00216     _renderer -> SetBackground (BgRed, BgGreen, BgBlue);
00217     return (*this);
00218 }
00219 
00220 }; // namespace VTK
00221 
00222 #endif // MECHSYS_VTKWIN_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines