![]() |
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_ARROW3D_H 00020 #define MECHSYS_ARROW3D_H 00021 00022 // Std Lib 00023 #include <cmath> 00024 00025 // VTK 00026 #include <vtkConeSource.h> 00027 #include <vtkCylinderSource.h> 00028 #include <vtkAppendPolyData.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 /* +-------------------------------+ 00041 * | length | 00042 * +-----------------------+-------+ 00043 * | bod_len |tip_len| 00044 * | | | 00045 * `. ----+ 00046 * | ``. | 00047 * + +-----------------------| ``. | 00048 * bod_rad | | + | + > | tip_rad 00049 * + +-----------|-----------| |_-' | 00050 * | | | _-| | 00051 * | | '' | --+ 00052 * | | | 00053 * +-----------+---------------+-------> y axis 00054 * | | | 00055 * y0 y_bod_cen y_tip_cen 00056 */ 00057 00058 namespace VTK 00059 { 00060 00061 class Arrow 00062 { 00063 public: 00064 // Constructor & Destructor 00065 Arrow (); 00066 ~Arrow (); 00067 00068 // Alternative constructor 00069 Arrow (Vec3_t const & X0, Vec3_t const & V, double ConPct=0.1, double ConRad=0.03, double CylRad=0.015, int Res=20); 00070 00071 // Set methods 00072 Arrow & SetGeometry (double ConPct=0.1, double ConRad=0.03, double CylRad=0.015); 00073 Arrow & SetResolution (int Resolution=20); 00074 Arrow & SetColor (char const * Name="yellow", double Opacity=1.0); 00075 Arrow & SetVector (Vec3_t const & X0, Vec3_t const & V); 00076 Arrow & SetPoints (Vec3_t const & X0, Vec3_t const & X1); 00077 00078 // Methods 00079 void AddTo (VTK::Win & win) { win.AddActor(_arrow_actor); } 00080 00081 private: 00082 double _con_pct; 00083 double _tot_len; 00084 vtkConeSource * _cone; 00085 vtkCylinderSource * _cylin; 00086 vtkTransformFilter * _transform; 00087 vtkAppendPolyData * _arrow; 00088 vtkPolyDataMapper * _arrow_mapper; 00089 vtkActor * _arrow_actor; 00090 void _create (); 00091 void _update_length (); 00092 }; 00093 00094 00096 00097 00098 inline Arrow::Arrow () 00099 : _tot_len (1.0) 00100 { 00101 _create (); 00102 SetGeometry (); 00103 SetResolution (); 00104 SetColor (); 00105 } 00106 00107 inline Arrow::Arrow (Vec3_t const & X0, Vec3_t const & V, double ConPct, double ConRad, double CylRad, int Res) 00108 : _tot_len (norm(V)) 00109 { 00110 _create (); 00111 SetGeometry (ConPct, ConRad, CylRad); 00112 SetResolution (Res); 00113 SetColor (); 00114 SetVector (X0, V); 00115 } 00116 00117 inline Arrow::~Arrow () 00118 { 00119 _cone -> Delete(); 00120 _cylin -> Delete(); 00121 _transform -> Delete(); 00122 _arrow -> Delete(); 00123 _arrow_mapper -> Delete(); 00124 _arrow_actor -> Delete(); 00125 } 00126 00127 inline Arrow & Arrow::SetGeometry (double ConPct, double ConRad, double CylRad) 00128 { 00129 _con_pct = ConPct; 00130 _update_length (); 00131 _cone -> SetRadius (ConRad); 00132 _cylin -> SetRadius (CylRad); 00133 return (*this); 00134 } 00135 00136 inline Arrow & Arrow::SetResolution (int Res) 00137 { 00138 _cone -> SetResolution (Res); 00139 _cylin -> SetResolution (Res); 00140 return (*this); 00141 } 00142 00143 inline Arrow & Arrow::SetColor (char const * Name, double Opacity) 00144 { 00145 Vec3_t c(Colors::Get(Name)); 00146 _arrow_actor->GetProperty()->SetColor (c.data()); 00147 _arrow_actor->GetProperty()->SetOpacity (Opacity); 00148 return (*this); 00149 } 00150 00151 inline Arrow & Arrow::SetVector (Vec3_t const & X0, Vec3_t const & V) 00152 { 00153 // update length 00154 _tot_len = Norm(V); 00155 _update_length (); 00156 00157 // translate 00158 Vec3_t cen(X0+0.5*V); // center of arrow 00159 vtkTransform * affine = vtkTransform ::New(); 00160 affine->Translate (cen(0), cen(1), cen(2)); 00161 00162 // rotate 00163 Vec3_t vy(0.0, 1.0, 0.0); // direction of cylinder source 00164 double angle = (180.0/Util::PI)*acos(dot(vy,V)/norm(V)); // angle of rotation 00165 if (angle>0.0) 00166 { 00167 Vec3_t axis = cross(vy, V); // axis of rotation 00168 if (norm(axis)>0.0) // not parallel 00169 { 00170 affine->RotateWXYZ (angle, axis.data()); 00171 } 00172 else // parallel and oposite (alpha=180) 00173 { 00174 affine->RotateWXYZ (angle, 0.0, 0.0, 1.0); // use z-direction for mirroring 00175 } 00176 } 00177 00178 // tranform 00179 _transform->SetTransform (affine); 00180 00181 // clean up 00182 affine->Delete (); 00183 return (*this); 00184 } 00185 00186 inline Arrow & Arrow::SetPoints(Vec3_t const & X0, Vec3_t const & X1) 00187 { 00188 SetVector (X0, X1-X0); 00189 return (*this); 00190 } 00191 00192 inline void Arrow::_create () 00193 { 00194 _cone = vtkConeSource ::New(); 00195 _cylin = vtkCylinderSource ::New(); 00196 _arrow = vtkAppendPolyData ::New(); 00197 _transform = vtkTransformFilter ::New(); 00198 _arrow_mapper = vtkPolyDataMapper ::New(); 00199 _arrow_actor = vtkActor ::New(); 00200 _cone -> SetDirection (0.0, 1.0, 0.0); 00201 _arrow -> AddInput (_cone->GetOutput()); 00202 _arrow -> AddInput (_cylin->GetOutput()); 00203 _transform -> SetInput (_arrow->GetOutput()); 00204 _arrow_mapper -> SetInput (_transform->GetPolyDataOutput()); 00205 _arrow_actor -> SetMapper (_arrow_mapper); 00206 } 00207 00208 inline void Arrow::_update_length () 00209 { 00210 double con_len = _con_pct*_tot_len; // cone length/height 00211 double cyl_len = _tot_len-con_len; // cylinder length/height 00212 double con_cen = (_tot_len-con_len)/2.0; // cone center 00213 double cyl_cen = -con_len/2.0; // cylinder center 00214 00215 _cone -> SetCenter (0.0, con_cen, 0.0); 00216 _cone -> SetHeight (con_len); 00217 _cylin -> SetCenter (0.0, cyl_cen, 0.0); 00218 _cylin -> SetHeight (cyl_len); 00219 } 00220 00221 }; // namespace VTK 00222 00223 #endif // MECHSYS_ARROW3D_H