MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/vtk/cube.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_CUBE_H
00020 #define MECHSYS_CUBE_H
00021 
00022 // VTK
00023 #include <vtkCubeSource.h>
00024 #include <vtkPolyDataMapper.h>
00025 #include <vtkActor.h>
00026 #include <vtkProperty.h>
00027 
00028 // MechSys
00029 #include <mechsys/vtk/win.h>
00030 #include <mechsys/util/colors.h>
00031 
00032 namespace VTK
00033 {
00034 
00035 class Cube
00036 {
00037 public:
00038     // Constructor & Destructor
00039      Cube () { _create(); }
00040     ~Cube ();
00041 
00042     // Alternative constructor
00043     Cube (Vec3_t const & Cen, double Lx=1.0, double Ly=1.0, double Lz=1.0);
00044 
00045     // Set methods
00046     Cube & SetCenter    (Vec3_t const & X);
00047     Cube & SetLengths   (double Lx=1.0, double Ly=1.0, double Lz=1.0);
00048     Cube & SetColor     (char const * Name="yellow", double Opacity=1.0);
00049     Cube & SetWireColor (char const * Name="black");
00050     Cube & SetWireWidth (int Width=1.0);
00051 
00052     // Methods
00053     void AddTo (VTK::Win & win) { win.AddActor(_cube_actor);  win.AddActor(_wire_actor); }
00054 
00055 private:
00056     vtkCubeSource     * _cube;
00057     vtkPolyDataMapper * _cube_mapper;
00058     vtkActor          * _cube_actor;
00059     vtkPolyDataMapper * _wire_mapper;
00060     vtkActor          * _wire_actor;
00061     void _create ();
00062 };
00063 
00064 
00066 
00067 
00068 inline Cube::Cube (Vec3_t const & Cen, double Lx, double Ly, double Lz)
00069 {
00070     _create    ();
00071     SetCenter  (Cen);
00072     SetLengths (Lx, Ly, Lz);
00073 }
00074 
00075 inline Cube::~Cube ()
00076 {
00077     _cube        -> Delete();
00078     _cube_mapper -> Delete();
00079     _cube_actor  -> Delete();
00080     _wire_mapper -> Delete();
00081     _wire_actor  -> Delete();
00082 }
00083 
00084 inline Cube & Cube::SetCenter (Vec3_t const & X)
00085 {
00086     _cube -> SetCenter (X(0), X(1), X(2));
00087     return (*this);
00088 }
00089 
00090 inline Cube & Cube::SetLengths (double Lx, double Ly, double Lz)
00091 {
00092     _cube -> SetXLength (Lx);
00093     _cube -> SetYLength (Ly);
00094     _cube -> SetZLength (Lz);
00095     return (*this);
00096 }
00097 
00098 inline Cube & Cube::SetColor (char const * Name, double Opacity)
00099 {
00100     Vec3_t c(Colors::Get(Name));
00101     _cube_actor->GetProperty()->SetColor   (c.data());
00102     _cube_actor->GetProperty()->SetOpacity (Opacity);
00103     return (*this);
00104 }
00105 
00106 inline Cube & Cube::SetWireColor (char const * Name) 
00107 {
00108     Vec3_t c(Colors::Get(Name));
00109     _wire_actor->GetProperty()->SetColor (c.data());
00110     return (*this); 
00111 }
00112 
00113 inline Cube & Cube::SetWireWidth (int Width)
00114 {
00115     _wire_actor->GetProperty()->SetLineWidth(Width);
00116     return (*this); 
00117 }
00118 
00119 inline void Cube::_create ()
00120 {
00121     // create object
00122     _cube        = vtkCubeSource       ::New();
00123     _cube_mapper = vtkPolyDataMapper   ::New();
00124     _cube_actor  = vtkActor            ::New();
00125     _cube_mapper -> SetInputConnection (_cube->GetOutputPort());
00126     _cube_actor  -> SetMapper          (_cube_mapper);
00127 
00128     // borders
00129     _wire_mapper = vtkPolyDataMapper    ::New();
00130     _wire_actor  = vtkActor             ::New();
00131     _wire_mapper -> SetInput            (_cube->GetOutput());
00132     _wire_mapper -> ScalarVisibilityOff ();
00133     _wire_actor  -> SetMapper           (_wire_mapper);
00134     _wire_actor  -> GetProperty         ()->SetRepresentationToWireframe();
00135 
00136     // set mapper
00137     _cube_mapper -> SetResolveCoincidentTopologyPolygonOffsetParameters (0,1);
00138     _cube_mapper -> SetResolveCoincidentTopologyToPolygonOffset         ();
00139     _wire_mapper -> SetResolveCoincidentTopologyPolygonOffsetParameters (1,1);
00140     _wire_mapper -> SetResolveCoincidentTopologyToPolygonOffset         ();
00141 
00142     // same color for inside and outside edges
00143     _wire_mapper -> ScalarVisibilityOff          ();
00144     _wire_actor  -> GetProperty() -> SetAmbient  (1.0);
00145     _wire_actor  -> GetProperty() -> SetDiffuse  (0.0);
00146     _wire_actor  -> GetProperty() -> SetSpecular (0.0);
00147 
00148     // set colors and wire width
00149     SetColor     ();
00150     SetWireColor ();
00151     SetWireWidth ();
00152 }
00153 
00154 }; // namespace VTK
00155 
00156 #endif // MECHSYS_CUBE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines