MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/vtk/isosurf.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_ISOSURF_H
00020 #define MECHSYS_ISOSURF_H
00021 
00022 // VTK
00023 #include <vtkMarchingContourFilter.h>
00024 #include <vtkHedgeHog.h>
00025 #include <vtkDataSetMapper.h>
00026 #include <vtkPolyDataMapper.h>
00027 #include <vtkLookupTable.h>
00028 #include <vtkProperty.h>
00029 
00030 // MechSys
00031 #include <mechsys/vtk/sgrid.h>
00032 #include <mechsys/vtk/win.h>
00033 #include <mechsys/util/array.h>
00034 #include <mechsys/linalg/matvec.h>
00035 
00036 namespace VTK
00037 {
00038 
00039 class IsoSurf
00040 {
00041 public:
00042     // Constructor & Destructor
00043     IsoSurf (VTK::SGrid & G);
00044 
00045     // Destructor
00046     ~IsoSurf ();
00047 
00048     // Set methods
00049     void SetColors   (Array<String> const & Names, double Opacity=1.0);
00050     void SetColor    (char const * Name="blue",    double Opacity=1.0) { SetColors(Array<String>(Name,true), Opacity); }
00051     void SetValue    (double F=0.0)                                    { _isosurf->SetValue       (0,F);               }
00052     void GenValues   (int NSurfs, double fMin, double fMax)            { _isosurf->GenerateValues (NSurfs,fMin,fMax);  }
00053     void SetVecScale (double Factor=1.0)                               { _hedgehog->SetScaleFactor(Factor);            }
00054     void SetWire     ()                                                { _isosurf_actor->GetProperty()->SetRepresentationToWireframe(); }
00055 
00056     // Extra methods
00057     void SetMaterial (double Ambient, double Diffuse, double Specular, double SpecularPower);
00058 
00059     // Methods
00060     void AddTo (VTK::Win & win);
00061 
00062     // Data
00063     bool ShowIsoSurf;
00064     bool ShowVectors;
00065 
00066 private:
00067     vtkMarchingContourFilter * _isosurf;
00068     vtkPolyDataMapper        * _isosurf_mapper;
00069     vtkActor                 * _isosurf_actor;
00070     vtkLookupTable           * _isosurf_lt;
00071     vtkHedgeHog              * _hedgehog;
00072     vtkPolyDataMapper        * _hedgehog_mapper;
00073     vtkActor                 * _hedgehog_actor;
00074 };
00075 
00076 
00078 
00079 
00080 inline IsoSurf::IsoSurf (VTK::SGrid & G)
00081     : ShowIsoSurf (true),
00082       ShowVectors (true)
00083 {
00084     // isosurf
00085     _isosurf        = vtkMarchingContourFilter ::New();
00086     _isosurf_mapper = vtkPolyDataMapper        ::New();
00087     _isosurf_actor  = vtkActor                 ::New();
00088     _isosurf_lt     = vtkLookupTable           ::New();
00089     _isosurf        -> SetInput                (G.GetGrid());
00090     _isosurf        -> ComputeNormalsOff       ();
00091     _isosurf        -> ComputeGradientsOff     ();
00092     _isosurf_mapper -> SetInputConnection      (_isosurf->GetOutputPort());
00093     _isosurf_mapper -> SetLookupTable          (_isosurf_lt);
00094     _isosurf_actor  -> SetMapper               (_isosurf_mapper);
00095     SetColor ();
00096     SetValue ();
00097 
00098     // hedgehog
00099     _hedgehog        = vtkHedgeHog         ::New();
00100     _hedgehog_mapper = vtkPolyDataMapper   ::New();
00101     _hedgehog_actor  = vtkActor            ::New();
00102     _hedgehog        -> SetInput           (G.GetGrid());
00103     _hedgehog_mapper -> SetInputConnection (_hedgehog->GetOutputPort());
00104     _hedgehog_actor  -> SetMapper          (_hedgehog_mapper);
00105     SetVecScale ();
00106 }
00107 
00108 inline IsoSurf::~IsoSurf ()
00109 {
00110     _isosurf         -> Delete();
00111     _isosurf_mapper  -> Delete();
00112     _isosurf_actor   -> Delete();
00113     _isosurf_lt      -> Delete();
00114     _hedgehog        -> Delete();
00115     _hedgehog_mapper -> Delete();
00116     _hedgehog_actor  -> Delete();
00117 }
00118 
00119 inline void IsoSurf::SetColors (Array<String> const & Colors, double Opacity)
00120 {
00121     _isosurf_lt->SetNumberOfColors (Colors.Size());
00122     _isosurf_lt->Build             ();
00123     for (size_t i=0; i<Colors.Size(); ++i)
00124     {
00125         Vec3_t c = Colors::Get(Colors[i]);
00126         _isosurf_lt->SetTableValue (i, c(0), c(1), c(2));
00127     }
00128     _isosurf_actor->GetProperty()->SetOpacity(Opacity);
00129 }
00130 
00131 inline void IsoSurf::AddTo (VTK::Win & win)
00132 {
00133     if (ShowIsoSurf) win.AddActor (_isosurf_actor);
00134     if (ShowVectors) win.AddActor (_hedgehog_actor);
00135 }
00136 
00137 inline void IsoSurf::SetMaterial (double Ambient, double Diffuse, double Specular, double SpecularPower)
00138 {
00139     _isosurf_actor->GetProperty()->SetAmbient       (Ambient);
00140     _isosurf_actor->GetProperty()->SetDiffuse       (Diffuse);
00141     _isosurf_actor->GetProperty()->SetSpecular      (Specular);
00142     _isosurf_actor->GetProperty()->SetSpecularPower (SpecularPower);
00143 }
00144 
00145 }; // namespace VTK
00146 
00147 #endif // MECHSYS_ISOSURF_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines