cube.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_CUBE_H
00022 #define VTKWRAP_CUBE_H
00023 
00024 #include <string>
00025 
00026 #include "vtkCubeSource.h"
00027 #include "vtkPolyDataMapper.h"
00028 #include "vtkActor.h"
00029 #include "vtkProperty.h"
00030 
00031 #include "colors.h"
00032 
00033 class Cube
00034 {
00035     friend std::ostream & operator<< (std::ostream & os, Cube const & hh);
00036 public:
00037     Cube(double Xc, double Yc, double Zc, double Xlen, double Ylen, double Zlen, char const * Color=NULL, double Opacity=1.0)
00038     {
00039         // Color
00040         std::string * color;
00041         if (Color==NULL) color = new std::string ("red");
00042         else             color = new std::string (Color);
00043 
00044         // Create object
00045         _cube        = vtkCubeSource     ::New();
00046         _cube_mapper = vtkPolyDataMapper ::New();
00047         _cube_actor  = vtkActor          ::New();
00048         _cube        -> SetCenter                 (Xc,Yc,Zc);
00049         _cube        -> SetXLength                (Xlen);
00050         _cube        -> SetYLength                (Ylen);
00051         _cube        -> SetZLength                (Zlen);
00052         _cube_mapper -> SetInputConnection        (_cube->GetOutputPort());
00053         _cube_actor  -> SetMapper                 (_cube_mapper);
00054         _cube_actor  -> GetProperty()->SetColor   (CLR[color->c_str()].C);  
00055         _cube_actor  -> GetProperty()->SetOpacity (Opacity);
00056 
00057         // Borders
00058         _wire_mapper = vtkPolyDataMapper ::New();
00059         _wire_actor  = vtkActor          ::New();
00060         _wire_mapper -> SetInput            (_cube->GetOutput());
00061         _wire_mapper -> ScalarVisibilityOff ();
00062         _wire_actor  -> SetMapper           (_wire_mapper);
00063         _wire_actor  -> GetProperty         ()->SetRepresentationToWireframe();
00064 
00065         _cube_mapper->SetResolveCoincidentTopologyPolygonOffsetParameters(0,1);
00066         _cube_mapper->SetResolveCoincidentTopologyToPolygonOffset();
00067         _wire_mapper->SetResolveCoincidentTopologyPolygonOffsetParameters(1,1);
00068         _wire_mapper->SetResolveCoincidentTopologyToPolygonOffset();
00069 
00070         // Same color for inside and outside edges
00071         _wire_mapper->ScalarVisibilityOff();
00072         _wire_actor->GetProperty()->SetAmbient(1.0);
00073         _wire_actor->GetProperty()->SetDiffuse(0.0);
00074         _wire_actor->GetProperty()->SetSpecular(0.0);
00075 
00076         // Clean up
00077         delete color;
00078     }
00079     ~Cube()
00080     {
00081         _cube        -> Delete();
00082         _cube_mapper -> Delete();
00083         _cube_actor  -> Delete();
00084         _wire_mapper -> Delete();
00085         _wire_actor  -> Delete();
00086     }
00087     vtkActor           * GetActor()      { return _cube_actor;            }
00088     vtkActor           * GetWireActor()  { return _wire_actor;            }
00089     vtkAlgorithmOutput * GetOutputPort() { return _cube->GetOutputPort(); }
00090     vtkCubeSource      * GetObject()     { return _cube;                  }
00091     Cube & WireWidth (int          Width) { _wire_actor->GetProperty()->SetLineWidth(Width);    return (*this); }
00092     Cube & WireColor (char const * Color) { _wire_actor->GetProperty()->SetColor(CLR[Color].C); return (*this); }
00093 private:
00094     vtkCubeSource     * _cube;
00095     vtkPolyDataMapper * _cube_mapper;
00096     vtkActor          * _cube_actor;
00097     vtkPolyDataMapper * _wire_mapper;
00098     vtkActor          * _wire_actor;
00099 
00100 }; // class Cube
00101 
00102 std::ostream & operator<< (std::ostream & os, Cube const & hh)
00103 {
00104     hh._cube->Print(os);
00105     return os;
00106 }
00107 
00108 #endif // VTKWRAP_CUBE_H
00109 
00110 // vim:fdm=marker

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