MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/vtk/cylinder.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_CYLINDER_H
00020 #define MECHSYS_CYLINDER_H
00021 
00024 // Std Lib
00025 #include <cmath>
00026 
00027 // VTK
00028 #include <vtkCylinderSource.h>
00029 #include <vtkTransform.h>
00030 #include <vtkTransformFilter.h>
00031 #include <vtkPolyDataMapper.h>
00032 #include <vtkActor.h>
00033 #include <vtkProperty.h>
00034 
00035 // MechSys
00036 #include <mechsys/vtk/win.h>
00037 #include <mechsys/util/colors.h>
00038 #include <mechsys/linalg/matvec.h>
00039 
00040 namespace VTK
00041 {
00042 
00043 class Cylinder
00044 {
00045 public:
00046     // Constructor & Destructor
00047      Cylinder ();
00048     ~Cylinder ();
00049 
00050     // Alternative constructor
00051     Cylinder (Vec3_t const & X0, Vec3_t const & X1, double Radius, bool Capping=true, int Resolution=20);
00052 
00053     // Set methods
00054     Cylinder & SetRadius     (double Radius);
00055     Cylinder & SetPoints     (Vec3_t const & X0, Vec3_t const & X1);
00056     Cylinder & SetResolution (int Res=20);
00057     Cylinder & SetColor      (char const * Name="olive", double Opacity=1.0);
00058     Cylinder & SetWire       () { _cylin_actor->GetProperty()->SetRepresentationToWireframe();  return (*this); }
00059 
00060     // Methods
00061     void AddTo (VTK::Win & win) { win.AddActor(_cylin_actor); }
00062 
00063 private:
00064     vtkCylinderSource  * _cylin;
00065     vtkTransformFilter * _transform;
00066     vtkPolyDataMapper  * _cylin_mapper;
00067     vtkActor           * _cylin_actor;
00068     void _create (bool Cap=true);
00069 };
00070 
00071 
00073 
00074 
00075 inline Cylinder::Cylinder ()
00076 {
00077     _create       ();
00078     SetResolution ();
00079     SetColor      ();
00080 }
00081 
00082 inline Cylinder::Cylinder (Vec3_t const & X0, Vec3_t const & X1, double R, bool Cap, int Res)
00083 {
00084     _create       (Cap);
00085     SetRadius     (R);
00086     SetPoints     (X0, X1);
00087     SetResolution (Res);
00088     SetColor      ();
00089 }
00090 
00091 inline Cylinder::~Cylinder ()
00092 {
00093     _cylin        -> Delete();
00094     _transform    -> Delete();
00095     _cylin_mapper -> Delete();
00096     _cylin_actor  -> Delete();
00097 }
00098 
00099 inline Cylinder & Cylinder::SetRadius (double R)
00100 {
00101     _cylin->SetRadius (R);
00102     return (*this);
00103 }
00104 
00105 inline Cylinder & Cylinder::SetPoints (Vec3_t const & X0, Vec3_t const & X1)
00106 {
00107     // update length
00108     Vec3_t V(X1-X0);
00109     _cylin->SetHeight (Norm(V));
00110 
00111     // translate
00112     Vec3_t cen(X0+0.5*V); // center of cylin
00113     vtkTransform * affine = vtkTransform ::New();
00114     affine->Translate (cen(0), cen(1), cen(2));
00115 
00116     // rotate
00117     Vec3_t vy(0.0, 1.0, 0.0); // direction of cylinder source
00118     double angle = (180.0/Util::PI)*acos(dot(vy,V)/norm(V)); // angle of rotation
00119     if (angle>0.0)
00120     {
00121         Vec3_t axis = cross(vy, V); // axis of rotation
00122         if (norm(axis)>0.0)         // not parallel
00123         {
00124             affine->RotateWXYZ (angle, axis.data());
00125         }
00126         else // parallel and oposite (alpha=180)
00127         {
00128             affine->RotateWXYZ (angle, 0.0, 0.0, 1.0); // use z-direction for mirroring
00129         }
00130     }
00131 
00132     // tranform
00133     _transform->SetTransform (affine);
00134 
00135     // clean up
00136     affine->Delete ();
00137     return (*this);
00138 }
00139 
00140 inline Cylinder & Cylinder::SetResolution (int Res)
00141 {
00142     _cylin->SetResolution (Res);
00143     return (*this);
00144 }
00145 
00146 inline Cylinder & Cylinder::SetColor (char const * Name, double Opacity)
00147 {
00148     Vec3_t c(Colors::Get(Name));
00149     _cylin_actor->GetProperty()->SetColor   (c.data());
00150     _cylin_actor->GetProperty()->SetOpacity (Opacity);
00151     return (*this);
00152 }
00153 
00154 inline void Cylinder::_create (bool Cap)
00155 {
00156     _cylin        = vtkCylinderSource  ::New();
00157     _transform    = vtkTransformFilter ::New();
00158     _cylin_mapper = vtkPolyDataMapper  ::New();
00159     _cylin_actor  = vtkActor           ::New();
00160     _cylin        -> SetCapping        (Cap);
00161     _transform    -> SetInput          (_cylin->GetOutput());
00162     _cylin_mapper -> SetInput          (_transform->GetPolyDataOutput());
00163     _cylin_actor  -> SetMapper         (_cylin_mapper);
00164 }
00165 
00166 }; // namespace VTK
00167 
00168 #endif // MECHSYS_CYLINDER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines