arrow.h

00001 /***********************************************************************************
00002  * VTKwrap - Simple VTK wrappers                                                   *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>     *
00004  *                                                                                 *
00005  * This file is part of VTKwrap.                                                   *
00006  *                                                                                 *
00007  * VTKwrap is free software; you can redistribute it and/or modify it under        *
00008  * the  terms of the GNU General Public License as published by the Free Software  *
00009  * Foundation; either version 2 of the License, or (at your option) any later      *
00010  * version.                                                                        *
00011  *                                                                                 *
00012  * VTKwrap is distributed in the hope that it will be useful, but WITHOUT ANY      *
00013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
00014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.        *
00015  *                                                                                 *
00016  * You should have received a copy of the GNU General Public License along with    *
00017  * VTKwrap; if not, write to the Free Software Foundation, Inc., 51 Franklin       *
00018  * Street, Fifth Floor, Boston, MA 02110-1301, USA                                 *
00019  ***********************************************************************************/
00020 
00021 #ifndef VTKWRAP_ARROW3D_H
00022 #define VTKWRAP_ARROW3D_H
00023 
00024 #include "vtkConeSource.h"
00025 #include "vtkCylinderSource.h"
00026 #include "vtkAppendPolyData.h"
00027 #include "vtkTransform.h"
00028 #include "vtkTransformFilter.h"
00029 #include "vtkPoints.h"
00030 #include "vtkFloatArray.h"
00031 #include "vtkPolyData.h"
00032 #include "vtkPointData.h"
00033 #include "vtkArrowSource.h"
00034 #include "vtkGlyph3D.h"
00035 #include "vtkPolyDataMapper.h"
00036 #include "vtkProperty.h"
00037 #include "vtkLODActor.h"
00038 
00039 #include "tensors/tensor1.h"
00040 
00041 #include "colors.h"
00042 
00043 class Arrow
00044 {
00045     friend std::ostream & operator<< (std::ostream & os, Arrow const & ar);
00046 public:
00047     // Auxiliar class
00048     class Geometry
00049     { // {{{
00050         /*                +-------------------------------+ 
00051          *                |            length             |
00052          *                +-----------------------+-------+
00053          *                |        bod_len        |tip_len|
00054          *                |                       |       |
00055          *                                        `.      ----+  
00056          *                                        | ``.       |
00057          *             +  +-----------------------|    ``.    |
00058          *     bod_rad |  |           +           |   +   >   | tip_rad   
00059          *             +  +-----------|-----------|   |_-'    |
00060          *                |           |           | _-|       |
00061          *                |           |           ''  |     --+  
00062          *                |           |               |
00063          *                +-----------+---------------+-------> y axis
00064          *                |           |               |    
00065          *                y0      y_bod_cen      y_tip_cen
00066          */
00067     public:
00068         Geometry(double BodyRadius=0.015, double TipRadius=0.03, double TipMultiplier=4.0, int Resolution=22)
00069             : _bod_rad (BodyRadius)       , 
00070               _tip_rad (TipRadius)        , 
00071               _tip_mul (TipMultiplier)    , 
00072               _resolut (Resolution)       , 
00073               _tip_len (_tip_mul*_tip_rad)
00074         {}
00075         Geometry(Geometry const & G)
00076         {
00077             _bod_rad = G._bod_rad;
00078             _tip_rad = G._tip_rad;
00079             _tip_mul = G._tip_mul;
00080             _resolut = G._resolut;
00081             _tip_len = G._tip_len;
00082         }
00083         double BodRad() const { return _bod_rad; }
00084         double TipRad() const { return _tip_rad; }
00085         double TipMul() const { return _tip_mul; }
00086         int    Resolu() const { return _resolut; }
00087         double TipLen() const { return _tip_len; }
00088 
00089     private:
00090         double _bod_rad; // body (cylinder) radius
00091         double _tip_rad; // tip (cone) radius
00092         double _tip_mul; // tip_len = tip_mul * tip_rad
00093         int    _resolut; // number of facets
00094         double _tip_len; // tip_len = tip_mul * tip_rad
00095     }; // class Geometry }}}
00096 
00097     // Constructor
00098     Arrow(double vx, double vy, double vz,  char const * Color=NULL, double x0=0.0, double y0=0.0, double z0=0.0, Geometry const * Geom=NULL)
00099     { _construct(vx,vy,vz, x0,y0,z0, Color, Geom); }
00100 
00101     Arrow(TensorsLib::Tensor1<double,3> const & V, char const * Color=NULL, double x0=0.0, double y0=0.0, double z0=0.0, Geometry const * Geom=NULL)
00102     { _construct(V[0],V[1],V[2], x0,y0,z0, Color, Geom); }
00103 
00104     // Destructor
00105     ~Arrow()
00106     {
00107         _vector        -> Delete();
00108         _vector_mapper -> Delete();
00109         _vector_actor  -> Delete();
00110     }
00111 
00112     // Methods
00113     vtkActor * GetActor() { return _vector_actor; }
00114 
00115 private:
00116     // Data
00117     vtkGlyph3D        * _vector;
00118     vtkPolyDataMapper * _vector_mapper;
00119     vtkLODActor       * _vector_actor;
00120     void _construct(double vx, double vy, double vz, double x0, double y0, double z0, char const * Color, Geometry const * Geom);
00121 }; // class Arrow
00122 
00123 std::ostream & operator<< (std::ostream & os, Arrow const & ar)
00124 {
00125     ar._vector->Print(os);
00126     return os;
00127 }
00128 
00129 
00131 
00132 inline void Arrow::_construct(double vx, double vy, double vz, double x0, double y0, double z0, char const * Color, Geometry const * Geom) // {{{
00133 {
00134     // Color
00135     std::string * color;
00136     if (Color==NULL) color = new std::string ("red");
00137     else             color = new std::string (Color);
00138 
00139     // SetGeometry
00140     Geometry * geom;
00141     if (Geom==NULL) geom = new Geometry;
00142     else            geom = new Geometry (*Geom);
00143 
00144     // Dimensions
00145     double length  = sqrt(vx*vx + vy*vy + vz*vz);
00146     double bod_len = length-geom->TipLen();
00147     double bod_cen = bod_len/2.0;
00148     double tip_cen = bod_len+geom->TipLen()/2.0;
00149 
00150     // Create the body (cylinder) and tip (cone) of the arrow
00151     vtkConeSource     * cone  = vtkConeSource     ::New();
00152     vtkCylinderSource * cylin = vtkCylinderSource ::New();
00153     vtkAppendPolyData * arrow = vtkAppendPolyData ::New();
00154     cylin -> SetCenter     (0.0, bod_cen, 0.0);
00155     cylin -> SetHeight     (bod_len);
00156     cylin -> SetRadius     (geom->BodRad());
00157     cylin -> SetResolution (geom->Resolu());
00158     cone  -> SetCenter     (0.0, tip_cen, 0.0);
00159     cone  -> SetDirection  (0,1,0); // because cylinder direction is along y axis
00160     cone  -> SetHeight     (geom->TipLen());
00161     cone  -> SetRadius     (geom->TipRad());
00162     cone  -> SetResolution (geom->Resolu());
00163     arrow -> AddInput      (cone->GetOutput());
00164     arrow -> AddInput      (cylin->GetOutput());
00165 
00166     // Rotate arrow around z axis because glyph3D uses arrows on x axis
00167     vtkTransform       * rotate        = vtkTransform       ::New();
00168     vtkTransformFilter * arrow_along_x = vtkTransformFilter ::New();
00169     rotate        -> RotateZ      (-90.0);
00170     arrow_along_x -> SetTransform (rotate);
00171     arrow_along_x -> SetInput     (arrow->GetOutput());
00172 
00173     // Setup data for the glyph
00174     vtkPoints     * points   = vtkPoints     ::New();
00175     vtkFloatArray * vectors  = vtkFloatArray ::New();
00176     vtkPolyData   * polydata = vtkPolyData   ::New();
00177     points   -> SetNumberOfPoints     (1);
00178     points   -> InsertPoint           (0, x0,y0,z0);
00179     vectors  -> SetNumberOfComponents (3);
00180     vectors  -> SetNumberOfTuples     (1);
00181     vectors  -> InsertTuple3          (0, vx,vy,vz);
00182     polydata -> SetPoints             (points);
00183     polydata -> GetPointData() -> SetVectors(vectors);
00184 
00185     // Create the glyph
00186     _vector =  vtkGlyph3D                   ::New();
00187     _vector -> SetInput                     (polydata);
00188     _vector -> SetSource                    (arrow_along_x->GetPolyDataOutput());
00189     _vector -> SetVectorModeToUseVector     ();
00190     _vector -> SetScaleModeToDataScalingOff ();
00191 
00192     // Create mapper and actor
00193     _vector_mapper = vtkPolyDataMapper   ::New();
00194     _vector_actor  = vtkLODActor         ::New();
00195     _vector_mapper -> SetInputConnection (_vector->GetOutputPort());
00196     _vector_actor  -> SetMapper          (_vector_mapper);
00197     _vector_actor  -> GetProperty        ()->SetColor(CLR[color->c_str()].C);   
00198     _vector_actor  -> VisibilityOn       ();
00199     
00200     // Clean up
00201     cone          -> Delete();
00202     cylin         -> Delete();
00203     arrow         -> Delete();
00204     rotate        -> Delete();
00205     arrow_along_x -> Delete();
00206     points        -> Delete();
00207     vectors       -> Delete();
00208     polydata      -> Delete();
00209 
00210     // Clean up
00211     delete color;
00212     delete geom;
00213     
00214 } // }}}
00215 
00216 #endif // VTKWRAP_ARROW3D_H
00217 
00218 // vim:fdm=marker

Generated on Wed Jan 24 15:56:27 2007 for MechSys by  doxygen 1.4.7