![]() |
MechSys
1.0
Computing library for simulations in continuum and discrete mechanics
|
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_AXES_H 00020 #define MECHSYS_AXES_H 00021 00022 // Std Lib 00023 #include <cmath> 00024 00025 // VTK 00026 #include <vtkPoints.h> 00027 #include <vtkLine.h> 00028 #include <vtkUnstructuredGrid.h> 00029 #include <vtkDataSetMapper.h> 00030 #include <vtkActor.h> 00031 #include <vtkProperty.h> 00032 #include <vtkTextActor3D.h> 00033 #include <vtkTextProperty.h> 00034 00035 // MechSys 00036 #include <mechsys/vtk/win.h> 00037 #include <mechsys/util/colors.h> 00038 00039 namespace VTK 00040 { 00041 00042 class Axes 00043 { 00044 public: 00045 // Constructor & Destructor 00046 Axes (); 00047 ~Axes (); 00048 00049 // Alternative constructor 00050 Axes (double Scale, bool DrawHydroLine=false, bool Reverse=false, bool Full=false, bool Labels=true); 00051 00052 // Set Methods 00053 void SetLabels (char const * X= "x", char const * Y= "y", char const * Z= "z", char const * Color="blue", int SizePt=22, bool Shadow=true); 00054 void SetNegLabels (char const * X="-x", char const * Y="-y", char const * Z="-z", char const * Color="red", int SizePt=22, bool Shadow=true); 00055 void SetWireWidth (int Width=1.0) { _axes_actor->GetProperty()->SetLineWidth(Width); } 00056 00057 // Get Methods 00058 vtkTextActor3D & XLabel() { return (*_x_label_actor); } 00059 vtkTextActor3D & YLabel() { return (*_y_label_actor); } 00060 vtkTextActor3D & ZLabel() { return (*_z_label_actor); } 00061 00062 // Methods 00063 void AddTo (VTK::Win & win); 00064 00065 private: 00066 vtkUnstructuredGrid * _axes; 00067 vtkDataSetMapper * _axes_mapper; 00068 vtkActor * _axes_actor; 00069 vtkTextActor3D * _x_label_actor; 00070 vtkTextActor3D * _y_label_actor; 00071 vtkTextActor3D * _z_label_actor; 00072 vtkTextProperty * _text_prop; 00073 vtkTextActor3D * _negx_label_actor; 00074 vtkTextActor3D * _negy_label_actor; 00075 vtkTextActor3D * _negz_label_actor; 00076 vtkTextProperty * _neg_text_prop; 00077 void _create (double Scale=1.0, bool DrawHydroLine=false, bool Reverse=false, bool Full=false, bool Labels=true); 00078 }; 00079 00080 00082 00083 00084 inline Axes::Axes () 00085 : _x_label_actor(NULL), _y_label_actor(NULL), _z_label_actor(NULL), _text_prop(NULL), 00086 _negx_label_actor(NULL), _negy_label_actor(NULL), _negz_label_actor(NULL), _neg_text_prop(NULL) 00087 { 00088 _create (); 00089 } 00090 00091 inline Axes::Axes (double Scale, bool DrawHydroLine, bool Reverse, bool Full, bool Labels) 00092 : _x_label_actor(NULL), _y_label_actor(NULL), _z_label_actor(NULL), _text_prop(NULL), 00093 _negx_label_actor(NULL), _negy_label_actor(NULL), _negz_label_actor(NULL), _neg_text_prop(NULL) 00094 { 00095 _create (Scale, DrawHydroLine, Reverse, Full, Labels); 00096 } 00097 00098 inline Axes::~Axes () 00099 { 00100 _axes -> Delete(); 00101 _axes_mapper -> Delete(); 00102 _axes_actor -> Delete(); 00103 if ( _x_label_actor!=NULL) _x_label_actor -> Delete(); 00104 if ( _y_label_actor!=NULL) _y_label_actor -> Delete(); 00105 if ( _z_label_actor!=NULL) _z_label_actor -> Delete(); 00106 if (_negx_label_actor!=NULL) _negx_label_actor -> Delete(); 00107 if (_negy_label_actor!=NULL) _negy_label_actor -> Delete(); 00108 if (_negz_label_actor!=NULL) _negz_label_actor -> Delete(); 00109 if ( _text_prop !=NULL) _text_prop -> Delete(); 00110 if (_neg_text_prop !=NULL) _neg_text_prop -> Delete(); 00111 } 00112 00113 inline void Axes::AddTo (VTK::Win & win) 00114 { 00115 win.AddActor(_axes_actor); 00116 if ( _x_label_actor!=NULL) win.AddActor(reinterpret_cast<vtkActor*>( _x_label_actor)); 00117 if ( _y_label_actor!=NULL) win.AddActor(reinterpret_cast<vtkActor*>( _y_label_actor)); 00118 if ( _z_label_actor!=NULL) win.AddActor(reinterpret_cast<vtkActor*>( _z_label_actor)); 00119 if (_negx_label_actor!=NULL) win.AddActor(reinterpret_cast<vtkActor*>(_negx_label_actor)); 00120 if (_negy_label_actor!=NULL) win.AddActor(reinterpret_cast<vtkActor*>(_negy_label_actor)); 00121 if (_negz_label_actor!=NULL) win.AddActor(reinterpret_cast<vtkActor*>(_negz_label_actor)); 00122 } 00123 00124 inline void Axes::SetLabels (char const * X, char const * Y, char const * Z, char const * Color, int SizePt, bool Shadow) 00125 { 00126 if (_x_label_actor==NULL) return; 00127 if (_y_label_actor==NULL) return; 00128 if (_z_label_actor==NULL) return; 00129 if ( _text_prop==NULL) return; 00130 Vec3_t c = Colors::Get(Color); 00131 _x_label_actor -> SetInput (X); 00132 _y_label_actor -> SetInput (Y); 00133 _z_label_actor -> SetInput (Z); 00134 _text_prop -> SetFontSize (SizePt); 00135 _text_prop -> SetColor (c.data()); 00136 if (Shadow) _text_prop -> ShadowOn (); 00137 else _text_prop -> ShadowOff(); 00138 } 00139 00140 inline void Axes::SetNegLabels (char const * X, char const * Y, char const * Z, char const * Color, int SizePt, bool Shadow) 00141 { 00142 if (_negx_label_actor==NULL) return; 00143 if (_negy_label_actor==NULL) return; 00144 if (_negz_label_actor==NULL) return; 00145 if (_neg_text_prop ==NULL) return; 00146 Vec3_t c = Colors::Get(Color); 00147 _negx_label_actor -> SetInput (X); 00148 _negy_label_actor -> SetInput (Y); 00149 _negz_label_actor -> SetInput (Z); 00150 _neg_text_prop -> SetFontSize (SizePt); 00151 _neg_text_prop -> SetColor (c.data()); 00152 if (Shadow) _neg_text_prop -> ShadowOn (); 00153 else _neg_text_prop -> ShadowOff(); 00154 } 00155 00156 inline void Axes::_create (double Scale, bool DrawHydroLine, bool Reverse, bool Full, bool Labels) 00157 { 00158 // points 00159 double cte = ((Reverse && (!Full)) ? -Scale : Scale); 00160 vtkPoints * points = vtkPoints::New(); points->SetNumberOfPoints(6); 00161 if (Full) 00162 { 00163 points->InsertPoint(0, -cte, 0.0, 0.0); points->InsertPoint(1, cte, 0.0, 0.0); 00164 points->InsertPoint(2, 0.0,-cte, 0.0); points->InsertPoint(3, 0.0, cte, 0.0); 00165 points->InsertPoint(4, 0.0, 0.0,-cte); points->InsertPoint(5, 0.0, 0.0, cte); if (DrawHydroLine) { 00166 points->InsertPoint(6, -cte,-cte,-cte); points->InsertPoint(7, cte, cte, cte); } 00167 } 00168 else 00169 { 00170 points->InsertPoint(0, 0,0,0); points->InsertPoint(1, cte, 0.0, 0.0); 00171 points->InsertPoint(2, 0,0,0); points->InsertPoint(3, 0.0, cte, 0.0); 00172 points->InsertPoint(4, 0,0,0); points->InsertPoint(5, 0.0, 0.0, cte); if (DrawHydroLine) { 00173 points->InsertPoint(6, 0,0,0); points->InsertPoint(7, cte, cte, cte); } 00174 } 00175 00176 // lines 00177 vtkLine * line_X = vtkLine::New(); // X axis 00178 vtkLine * line_Y = vtkLine::New(); // Y axis 00179 vtkLine * line_Z = vtkLine::New(); // Z axis 00180 vtkLine * line_H = vtkLine::New(); // hydro axis 00181 line_X->GetPointIds()->SetNumberOfIds(2); line_X->GetPointIds()->SetId(0,0); line_X->GetPointIds()->SetId(1,1); 00182 line_Y->GetPointIds()->SetNumberOfIds(2); line_Y->GetPointIds()->SetId(0,2); line_Y->GetPointIds()->SetId(1,3); 00183 line_Z->GetPointIds()->SetNumberOfIds(2); line_Z->GetPointIds()->SetId(0,4); line_Z->GetPointIds()->SetId(1,5); if (DrawHydroLine) { 00184 line_H->GetPointIds()->SetNumberOfIds(2); line_H->GetPointIds()->SetId(0,6); line_H->GetPointIds()->SetId(1,7); } 00185 00186 // grid 00187 _axes = vtkUnstructuredGrid::New(); 00188 _axes->Allocate(3,3); 00189 _axes->InsertNextCell(line_X->GetCellType(),line_X->GetPointIds()); 00190 _axes->InsertNextCell(line_Y->GetCellType(),line_Y->GetPointIds()); 00191 _axes->InsertNextCell(line_Z->GetCellType(),line_Z->GetPointIds()); if (DrawHydroLine) { 00192 _axes->InsertNextCell(line_H->GetCellType(),line_H->GetPointIds()); } 00193 _axes->SetPoints(points); 00194 00195 // mapper and actor 00196 _axes_mapper = vtkDataSetMapper ::New(); 00197 _axes_actor = vtkActor ::New(); 00198 _axes_mapper -> SetInput (_axes); 00199 _axes_actor -> SetMapper (_axes_mapper); 00200 _axes_actor -> GetProperty () -> SetColor (0.0,0.0,0.0); 00201 _axes_actor -> GetProperty () -> SetDiffuseColor(0.0,0.0,0.0); 00202 00203 // clean up 00204 points -> Delete(); 00205 line_X -> Delete(); 00206 line_Y -> Delete(); 00207 line_Z -> Delete(); 00208 line_H -> Delete(); 00209 00210 // text 00211 if (!Labels) return; 00212 _text_prop = vtkTextProperty ::New(); 00213 _x_label_actor = vtkTextActor3D ::New(); 00214 _y_label_actor = vtkTextActor3D ::New(); 00215 _z_label_actor = vtkTextActor3D ::New(); 00216 _x_label_actor->SetTextProperty (_text_prop); 00217 _y_label_actor->SetTextProperty (_text_prop); 00218 _z_label_actor->SetTextProperty (_text_prop); 00219 _x_label_actor->SetPosition (1.05*cte,0,0); 00220 _y_label_actor->SetPosition (0,cte,0); 00221 _z_label_actor->SetPosition (0,0,cte); 00222 _x_label_actor->SetScale (0.003*Scale); 00223 _y_label_actor->SetScale (0.003*Scale); 00224 _z_label_actor->SetScale (0.003*Scale); 00225 if (Reverse) 00226 { 00227 _x_label_actor->SetOrientation (-90,0,-180); 00228 _y_label_actor->SetOrientation (-90,-90,0); 00229 _z_label_actor->SetOrientation (-90,-90,45); 00230 if (Full) SetLabels ( "x", "y", "z"); 00231 else SetLabels ("-x", "-y", "-z"); 00232 } 00233 else 00234 { 00235 _x_label_actor->SetOrientation (90,0,180); 00236 _y_label_actor->SetOrientation (90,90,0); 00237 _z_label_actor->SetOrientation (90,90,45); 00238 SetLabels (); 00239 } 00240 if (Full) 00241 { 00242 _neg_text_prop = vtkTextProperty ::New(); 00243 _negx_label_actor = vtkTextActor3D ::New(); 00244 _negy_label_actor = vtkTextActor3D ::New(); 00245 _negz_label_actor = vtkTextActor3D ::New(); 00246 _negx_label_actor->SetTextProperty (_neg_text_prop); 00247 _negy_label_actor->SetTextProperty (_neg_text_prop); 00248 _negz_label_actor->SetTextProperty (_neg_text_prop); 00249 _negx_label_actor->SetPosition (-cte,0,0); 00250 _negy_label_actor->SetPosition (0,-cte,0); 00251 _negz_label_actor->SetPosition (0,0,-cte); 00252 _negx_label_actor->SetScale (0.003*Scale); 00253 _negy_label_actor->SetScale (0.003*Scale); 00254 _negz_label_actor->SetScale (0.003*Scale); 00255 if (Reverse) 00256 { 00257 _negx_label_actor->SetOrientation (-90,0,-180); 00258 _negy_label_actor->SetOrientation (-90,-90,0); 00259 _negz_label_actor->SetOrientation (-90,-90,45); 00260 } 00261 else 00262 { 00263 _negx_label_actor->SetOrientation (90,0,180); 00264 _negy_label_actor->SetOrientation (90,90,0); 00265 _negz_label_actor->SetOrientation (90,90,45); 00266 } 00267 SetNegLabels (); 00268 } 00269 } 00270 00271 }; // namespace VTK 00272 00273 #endif // MECHSYS_AXES_H