axes.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_AXES_H
00022 #define VTKWRAP_AXES_H
00023 
00024 #ifdef HAVE_CONFIG_H
00025   #include "config.h"
00026 #else
00027   #ifndef REAL
00028     #define REAL double
00029   #endif
00030 #endif
00031 
00032 // STL
00033 #include <cmath>
00034 
00035 // VTK
00036 #include "vtkPoints.h"
00037 #include "vtkLine.h"
00038 #include "vtkUnstructuredGrid.h"
00039 #include "vtkDataSetMapper.h"
00040 #include "vtkActor.h"
00041 #include "vtkProperty.h"
00042 #include "vtkTextActor3D.h"
00043 #include "vtkTextProperty.h"
00044 
00045 // MechSys
00046 #include "vtkwrap/vtkwin.h"
00047 #include "vtkwrap/arrow.h"
00048 
00049 class Axes
00050 {
00051     friend std::ostream & operator<< (std::ostream & os, Axes const & sgo);
00052 public:
00053     // Constructor
00054     Axes(REAL Scale=1.0, bool DrawHydroLine=false, char const * Xlab="X", char const * Ylab="Y", char const * Zlab="Z");
00055 
00056     // Alternative Constructor for 3D Axes
00057     Axes(REAL       Xmax           , REAL       Ymax    , REAL       Zmax    ,
00058          char const * Xlab="X"       , char const * Ylab="Y", char const * Zlab="Z",
00059          char const * Color="peacock", char const * LabColor="black", REAL RadiusCoef=0.015, REAL TextCoef=0.0035,
00060          bool DrawHydroLine=false);
00061 
00062     // Destructor
00063     ~Axes();
00064 
00065     // Methods
00066     vtkActor * GetActor()       { return _axes_actor; }
00067     vtkActor * GetXLabelActor() { return reinterpret_cast<vtkActor*>(_x_label_actor); }
00068     vtkActor * GetYLabelActor() { return reinterpret_cast<vtkActor*>(_y_label_actor); }
00069     vtkActor * GetZLabelActor() { return reinterpret_cast<vtkActor*>(_z_label_actor); }
00070     void AddActorsTo  (VTKWin & Win);
00071     void DelActorsFrom(VTKWin & Win);
00072 
00073     // Methods
00074     void SetXLabOrient(REAL XDeg=90, REAL YDeg=0 , REAL ZDeg=180) { if (_x_txt!=NULL) _x_txt->SetOrientation(XDeg,YDeg,ZDeg); }
00075     void SetYLabOrient(REAL XDeg=90, REAL YDeg=90, REAL ZDeg=0  ) { if (_y_txt!=NULL) _y_txt->SetOrientation(XDeg,YDeg,ZDeg); }
00076     void SetZLabOrient(REAL XDeg=90, REAL YDeg=90, REAL ZDeg=0  ) { if (_z_txt!=NULL) _z_txt->SetOrientation(XDeg,YDeg,ZDeg); }
00077 
00078 private:
00079     // Data
00080     vtkUnstructuredGrid * _axes;
00081     vtkDataSetMapper    * _axes_mapper;
00082     vtkActor            * _axes_actor;
00083     vtkTextActor3D      * _x_label_actor;
00084     vtkTextActor3D      * _y_label_actor;
00085     vtkTextActor3D      * _z_label_actor;
00086     vtkTextProperty     * _text_prop;
00087 
00088     // Arrow
00089     Arrow          * _x_ar;
00090     Arrow          * _y_ar;
00091     Arrow          * _z_ar;
00092     Arrow          * _h_ar;  // HydroLine
00093     vtkTextActor3D * _x_txt;
00094     vtkTextActor3D * _y_txt;
00095     vtkTextActor3D * _z_txt;
00096 
00097     // Methods
00098     void _start_text_prop();
00099     void _set_text_property(vtkTextActor3D * TxtActor, REAL Scale, char const * Color);
00100 
00101 }; // class SGridOutline
00102 
00103 
00105 
00106 
00107 inline Axes::Axes(REAL Scale, bool DrawHydroLine, char const * Xlab, char const * Ylab, char const * Zlab) // {{{
00108     : _axes          (NULL),
00109       _axes_mapper   (NULL),
00110       _axes_actor    (NULL),
00111       _x_label_actor (NULL),
00112       _y_label_actor (NULL),
00113       _z_label_actor (NULL),
00114       _text_prop     (NULL),
00115       _x_ar          (NULL),
00116       _y_ar          (NULL),
00117       _z_ar          (NULL),
00118       _h_ar          (NULL),
00119       _x_txt         (NULL),
00120       _y_txt         (NULL),
00121       _z_txt         (NULL)
00122 {
00123     // Points
00124     vtkPoints * points = vtkPoints::New();  points->SetNumberOfPoints(6);
00125     points->InsertPoint(0, 0,0,0);  points->InsertPoint(1, Scale*1.0,       0.0,       0.0);
00126     points->InsertPoint(2, 0,0,0);  points->InsertPoint(3,       0.0, Scale*1.0,       0.0);
00127     points->InsertPoint(4, 0,0,0);  points->InsertPoint(5,       0.0,       0.0, Scale*1.0); if (DrawHydroLine) {
00128     points->InsertPoint(6, 0,0,0);  points->InsertPoint(7, Scale*1.0, Scale*1.0, Scale*1.0); }
00129 
00130     // Lines
00131     vtkLine * line_X = vtkLine::New(); // X axis
00132     vtkLine * line_Y = vtkLine::New(); // Y axis
00133     vtkLine * line_Z = vtkLine::New(); // Z axis
00134     vtkLine * line_H = vtkLine::New(); // hydro axis
00135     line_X->GetPointIds()->SetNumberOfIds(2); line_X->GetPointIds()->SetId(0,0);  line_X->GetPointIds()->SetId(1,1);
00136     line_Y->GetPointIds()->SetNumberOfIds(2); line_Y->GetPointIds()->SetId(0,2);  line_Y->GetPointIds()->SetId(1,3);
00137     line_Z->GetPointIds()->SetNumberOfIds(2); line_Z->GetPointIds()->SetId(0,4);  line_Z->GetPointIds()->SetId(1,5); if (DrawHydroLine) {
00138     line_H->GetPointIds()->SetNumberOfIds(2); line_H->GetPointIds()->SetId(0,6);  line_H->GetPointIds()->SetId(1,7); }
00139 
00140     // Grid
00141     _axes = vtkUnstructuredGrid::New();
00142     _axes->Allocate(3,3);
00143     _axes->InsertNextCell(line_X->GetCellType(),line_X->GetPointIds());
00144     _axes->InsertNextCell(line_Y->GetCellType(),line_Y->GetPointIds());
00145     _axes->InsertNextCell(line_Z->GetCellType(),line_Z->GetPointIds()); if (DrawHydroLine) {
00146     _axes->InsertNextCell(line_H->GetCellType(),line_H->GetPointIds()); }
00147     _axes->SetPoints(points);
00148 
00149     // Mapper and actor
00150     _axes_mapper = vtkDataSetMapper ::New();
00151     _axes_actor  = vtkActor         ::New();
00152     _axes_mapper -> SetInput        (_axes);
00153     _axes_actor  -> SetMapper       (_axes_mapper);
00154     _axes_actor  -> GetProperty     () -> SetColor       (0.0,0.0,0.0); 
00155     _axes_actor  -> GetProperty     () -> SetDiffuseColor(0.0,0.0,0.0); 
00156 
00157     // Clean up
00158     points -> Delete();
00159     line_X -> Delete();
00160     line_Y -> Delete();
00161     line_Z -> Delete();
00162     line_H -> Delete();
00163 
00164     // Text
00165     _start_text_prop();
00166     REAL sc=0.003*Scale;
00167     _x_label_actor=vtkTextActor3D::New(); _x_label_actor->SetInput(Xlab); _x_label_actor->SetTextProperty(_text_prop); _x_label_actor->SetPosition(Scale*1,0,0); _x_label_actor->SetScale(sc);
00168     _y_label_actor=vtkTextActor3D::New(); _y_label_actor->SetInput(Ylab); _y_label_actor->SetTextProperty(_text_prop); _y_label_actor->SetPosition(0,Scale*1,0); _y_label_actor->SetScale(sc);
00169     _z_label_actor=vtkTextActor3D::New(); _z_label_actor->SetInput(Zlab); _z_label_actor->SetTextProperty(_text_prop); _z_label_actor->SetPosition(0,0,Scale*1); _z_label_actor->SetScale(sc);
00170     _x_label_actor->SetOrientation(90,0,180);
00171     _y_label_actor->SetOrientation(90,90,0);
00172     _z_label_actor->SetOrientation(90,90,45);
00173 } // }}}
00174 
00175 inline Axes::Axes(REAL Xmax, REAL Ymax, REAL Zmax, char const * Xlab, char const * Ylab, char const * Zlab, char const * Color, // {{{
00176                   char const * LabColor, REAL RadiusCoef, REAL TextCoef, bool DrawHydroLine)
00177     : _axes          (NULL),
00178       _axes_mapper   (NULL),
00179       _axes_actor    (NULL),
00180       _x_label_actor (NULL),
00181       _y_label_actor (NULL),
00182       _z_label_actor (NULL),
00183       _text_prop     (NULL),
00184       _x_ar          (NULL),
00185       _y_ar          (NULL),
00186       _z_ar          (NULL),
00187       _h_ar          (NULL),
00188       _x_txt         (NULL),
00189       _y_txt         (NULL),
00190       _z_txt         (NULL)
00191 {
00192     // Constans
00193     REAL L = sqrt(Xmax*Xmax + Ymax*Ymax + Zmax*Zmax);
00194     REAL r = RadiusCoef*L;
00195     REAL s = TextCoef*L;
00196     REAL m = (Xmax>Ymax? Xmax : Ymax);
00197          m = (Zmax>m   ? Zmax : m   );
00198 
00199     // Arrow geometry
00200     Arrow::Geometry geom(r/2.0, r);
00201 
00202     // Arrows
00203     _x_ar = new Arrow (Xmax,0,0, Color, 0,0,0, &geom);
00204     _y_ar = new Arrow (0,Ymax,0, Color, 0,0,0, &geom);
00205     _z_ar = new Arrow (0,0,Zmax, Color, 0,0,0, &geom); if (DrawHydroLine)
00206     _h_ar = new Arrow (m,m,m,    Color, 0,0,0, &geom);
00207 
00208     // Labels
00209     _x_txt = vtkTextActor3D::New();
00210     _y_txt = vtkTextActor3D::New();
00211     _z_txt = vtkTextActor3D::New();
00212     _x_txt -> SetPosition(Xmax,0,0);
00213     _y_txt -> SetPosition(0,Ymax,0);
00214     _z_txt -> SetPosition(0,0,Zmax);
00215     _x_txt -> SetInput (Xlab);
00216     _y_txt -> SetInput (Ylab);
00217     _z_txt -> SetInput (Zlab);
00218     SetXLabOrient();
00219     SetYLabOrient();
00220     SetZLabOrient();
00221 
00222     // Text Property
00223     _set_text_property(_x_txt, s, LabColor);
00224     _set_text_property(_y_txt, s, LabColor);
00225     _set_text_property(_z_txt, s, LabColor);
00226 
00227 } // }}}
00228 
00229 inline Axes::~Axes() // {{{
00230 {
00231     if (_axes         !=NULL) _axes          -> Delete();
00232     if (_axes_mapper  !=NULL) _axes_mapper   -> Delete();
00233     if (_axes_actor   !=NULL) _axes_actor    -> Delete();
00234     if (_x_label_actor!=NULL) _x_label_actor -> Delete();
00235     if (_y_label_actor!=NULL) _y_label_actor -> Delete();
00236     if (_z_label_actor!=NULL) _z_label_actor -> Delete();
00237     if (_text_prop    !=NULL) _text_prop     -> Delete();
00238 
00239     if (_x_ar !=NULL) delete _x_ar;
00240     if (_y_ar !=NULL) delete _y_ar;
00241     if (_z_ar !=NULL) delete _z_ar;
00242     if (_h_ar !=NULL) delete _h_ar;
00243     if (_x_txt!=NULL) _x_txt -> Delete();
00244     if (_y_txt!=NULL) _y_txt -> Delete();
00245     if (_z_txt!=NULL) _z_txt -> Delete();
00246 } // }}}
00247 
00248 inline void Axes::AddActorsTo(VTKWin & Win) // {{{
00249 {
00250     if (_axes_actor   !=NULL) Win.AddActor(_axes_actor);
00251     if (_x_label_actor!=NULL) Win.AddActor(reinterpret_cast<vtkActor*>(_x_label_actor));
00252     if (_y_label_actor!=NULL) Win.AddActor(reinterpret_cast<vtkActor*>(_y_label_actor));
00253     if (_z_label_actor!=NULL) Win.AddActor(reinterpret_cast<vtkActor*>(_z_label_actor));
00254 
00255     if (_x_ar !=NULL) Win.AddActor(_x_ar->GetActor());
00256     if (_y_ar !=NULL) Win.AddActor(_y_ar->GetActor());
00257     if (_z_ar !=NULL) Win.AddActor(_z_ar->GetActor());
00258     if (_h_ar !=NULL) Win.AddActor(_h_ar->GetActor());
00259     if (_x_txt!=NULL) Win.AddActor(reinterpret_cast<vtkActor*>(_x_txt));
00260     if (_y_txt!=NULL) Win.AddActor(reinterpret_cast<vtkActor*>(_y_txt));
00261     if (_z_txt!=NULL) Win.AddActor(reinterpret_cast<vtkActor*>(_z_txt));
00262 } // }}}
00263 
00264 inline void Axes::DelActorsFrom(VTKWin & Win) // {{{
00265 {
00266     if (_axes_actor   !=NULL) Win.DelActor(_axes_actor);
00267     if (_x_label_actor!=NULL) Win.DelActor(reinterpret_cast<vtkActor*>(_x_label_actor));
00268     if (_y_label_actor!=NULL) Win.DelActor(reinterpret_cast<vtkActor*>(_y_label_actor));
00269     if (_z_label_actor!=NULL) Win.DelActor(reinterpret_cast<vtkActor*>(_z_label_actor));
00270 
00271     if (_x_ar !=NULL) Win.DelActor(_x_ar->GetActor());
00272     if (_y_ar !=NULL) Win.DelActor(_y_ar->GetActor());
00273     if (_z_ar !=NULL) Win.DelActor(_z_ar->GetActor());
00274     if (_h_ar !=NULL) Win.DelActor(_h_ar->GetActor());
00275     if (_x_txt!=NULL) Win.DelActor(reinterpret_cast<vtkActor*>(_x_txt));
00276     if (_y_txt!=NULL) Win.DelActor(reinterpret_cast<vtkActor*>(_y_txt));
00277     if (_z_txt!=NULL) Win.DelActor(reinterpret_cast<vtkActor*>(_z_txt));
00278 } // }}}
00279 
00280 std::ostream & operator<< (std::ostream & os, Axes const & sgo) // {{{
00281 {
00282     sgo._axes->Print(os);
00283     return os;
00284 } // }}}
00285 
00286 inline void Axes::_start_text_prop() // {{{
00287 {
00288     _text_prop = vtkTextProperty::New();
00289     _text_prop->SetFontSize(48);
00290     _text_prop->ShadowOff();
00291     _text_prop->SetColor(1.0,1.0,1.0);
00292     _text_prop->SetFontFamilyToArial();
00293 } // }}}
00294 
00295 inline void Axes::_set_text_property(vtkTextActor3D * TxtActor, REAL Scale, char const * Color) // {{{
00296 {
00297     TxtActor -> GetTextProperty()->SetFontSize                (24);
00298     TxtActor -> SetScale                                      (Scale);
00299     TxtActor -> GetTextProperty()->SetFontFamilyToArial       ();
00300     TxtActor -> GetTextProperty()->SetJustificationToCentered ();
00301     TxtActor -> GetTextProperty()->BoldOn                     ();
00302     TxtActor -> GetTextProperty()->SetColor                   (CLR[Color].C);
00303 } // }}}
00304 
00305 #endif // VTKWRAP_AXES_H
00306 
00307 // vim:fdm=marker

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