MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/vtk/arrows.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_ARROWS_H
00020 #define MECHSYS_ARROWS_H
00021 
00022 // VTK
00023 #include <vtkConeSource.h>
00024 #include <vtkCylinderSource.h>
00025 #include <vtkAppendPolyData.h>
00026 #include <vtkGlyph3D.h>
00027 #include <vtkPolyDataMapper.h>
00028 #include <vtkLODActor.h>
00029 #include <vtkProperty.h>
00030 #include <vtkLookupTable.h>
00031 #include <vtkTransform.h>
00032 #include <vtkTransformFilter.h>
00033 
00034 // MechSys
00035 #include <mechsys/vtk/win.h>
00036 #include <mechsys/vtk/sgrid.h>
00037 #include <mechsys/util/maps.h>
00038 #include <mechsys/util/colors.h>
00039 #include <mechsys/util/string.h>
00040 #include <mechsys/linalg/matvec.h>
00041 
00042 namespace VTK
00043 {
00044 
00045 class Arrows
00046 {
00047 public:
00048     // Constructor & Destructor
00049      Arrows () { _create();  SetGeometry();  SetScale(); }
00050     ~Arrows ();
00051 
00052     // Alternative constructor
00053     Arrows (VTK::SGrid & G, double ConPct=0.1, int Resolution=12);
00054 
00055     // Set methods
00056     Arrows & SetArrows   (VTK::SGrid & G);
00057     Arrows & SetGeometry (double ConPct=0.1, int Resolution=12);
00058     Arrows & SetColor    (char const * Name="blue", double Opacity=1.0);
00059     Arrows & SetScale    (double Factor=1.0) { _arrows->SetScaleFactor(Factor);  return (*this); }
00060 
00061     // Methods
00062     void AddTo (VTK::Win & win) { win.AddActor (_arrows_actor); }
00063 
00064 private:
00065     vtkConeSource     * _cone;
00066     vtkCylinderSource * _cylin;
00067     vtkGlyph3D        * _arrows;
00068     vtkPolyDataMapper * _arrows_mapper;
00069     vtkLODActor       * _arrows_actor;
00070     vtkLookupTable    * _ltable;
00071     void _create ();
00072 };
00073 
00074 
00076 
00077 
00078 inline Arrows::Arrows (VTK::SGrid & G, double ConPct, int Resolution)
00079 {
00080     _create     ();
00081     SetArrows   (G);
00082     SetGeometry (ConPct, Resolution);
00083     SetScale    ();
00084 }
00085 
00086 inline Arrows::~Arrows ()
00087 {
00088     _cone          -> Delete();
00089     _cylin         -> Delete();
00090     _arrows        -> Delete();
00091     _arrows_mapper -> Delete();
00092     _arrows_actor  -> Delete();
00093     _ltable        -> Delete();
00094 }
00095 
00096 inline Arrows & Arrows::SetArrows (VTK::SGrid & G)
00097 {
00098     vtkPolyData * polydata = vtkPolyData   ::New();
00099     polydata -> SetPoints                  (G.GetPoints());
00100     polydata -> GetPointData()->SetScalars (G.GetScalars());
00101     polydata -> GetPointData()->SetVectors (G.GetVectors());
00102     _arrows  -> SetInput                   (polydata);
00103     polydata -> Delete                     ();
00104     return (*this);
00105 }
00106 
00107 inline Arrows & Arrows::SetGeometry (double ConPct, int Resolution)
00108 {
00109     double con_rad = 0.03;
00110     double cyl_rad = 0.015;
00111     double tot_len = 1.0;                   // length of arrow (glyph)
00112     double con_len = ConPct*tot_len;        // cone length/height
00113     double cyl_len = tot_len-con_len;       // cylinder length/height
00114     double con_cen = (tot_len-con_len)/2.0; // cone center
00115     double cyl_cen = -con_len/2.0;          // cylinder center
00116 
00117     _cone  -> SetCenter     (0.0, con_cen+tot_len/2.0, 0.0);
00118     _cone  -> SetHeight     (con_len);
00119     _cylin -> SetCenter     (0.0, cyl_cen+tot_len/2.0, 0.0);
00120     _cylin -> SetHeight     (cyl_len);
00121     _cone  -> SetRadius     (con_rad);
00122     _cylin -> SetRadius     (cyl_rad);
00123     _cone  -> SetResolution (Resolution);
00124     _cylin -> SetResolution (Resolution);
00125 
00126     return (*this);
00127 }
00128 
00129 inline Arrows & Arrows::SetColor (char const * Name, double Opacity)
00130 {
00131     Vec3_t c = Colors::Get(Name);
00132     _ltable->SetNumberOfColors (2);
00133     _ltable->Build             ();
00134     _ltable->SetTableValue     (0, c(0), c(1), c(2));
00135     _ltable->SetTableValue     (1, c(0), c(1), c(2));
00136     _arrows_actor->GetProperty()->SetOpacity (Opacity);
00137     return (*this);
00138 }
00139 
00140 inline void Arrows::_create ()
00141 {
00142     // create arrow
00143     _cone                     = vtkConeSource     ::New();
00144     _cylin                    = vtkCylinderSource ::New();
00145     vtkAppendPolyData * arrow = vtkAppendPolyData ::New();
00146     _cone -> SetDirection (0,1,0); // because cylinder direction is along y axis
00147     arrow -> AddInput     (_cone->GetOutput());
00148     arrow -> AddInput     (_cylin->GetOutput());
00149 
00150     // rotate around z axis because glyph3D needs the arrow along the x-axis
00151     vtkTransform       * rotate = vtkTransform       ::New();
00152     vtkTransformFilter * transf = vtkTransformFilter ::New();
00153     rotate -> RotateZ      (-90.0);
00154     transf -> SetTransform (rotate);
00155     transf -> SetInput     (arrow->GetOutput());
00156 
00157     // glyph
00158     _arrows        = vtkGlyph3D                   ::New();
00159     _arrows_mapper = vtkPolyDataMapper            ::New();
00160     _arrows_actor  = vtkLODActor                  ::New();
00161     _ltable        = vtkLookupTable               ::New();
00162     _arrows        -> SetSource                   (transf->GetPolyDataOutput());
00163     _arrows        -> SetScaleModeToScaleByVector ();
00164     _arrows        -> SetColorModeToColorByVector ();
00165     _arrows_mapper -> SetInputConnection          (_arrows->GetOutputPort());
00166     _arrows_mapper -> SetLookupTable              (_ltable);
00167     _arrows_actor  -> SetMapper                   (_arrows_mapper);
00168 
00169     // clean up
00170     arrow  -> Delete ();
00171     rotate -> Delete ();
00172     transf -> Delete ();
00173 }
00174 
00175 }; // namespace VTK
00176 
00177 #endif // MECHSYS_ARROWS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines