![]() |
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_DISK_H 00020 #define MECHSYS_DISK_H 00021 00022 // Std Lib 00023 #include <cmath> 00024 00025 // VTK 00026 #include <vtkDiskSource.h> 00027 #include <vtkTransform.h> 00028 #include <vtkTransformFilter.h> 00029 #include <vtkLinearExtrusionFilter.h> 00030 #include <vtkPolyDataMapper.h> 00031 #include <vtkActor.h> 00032 #include <vtkProperty.h> 00033 00034 // MechSys 00035 #include <mechsys/vtk/win.h> 00036 #include <mechsys/util/colors.h> 00037 #include <mechsys/linalg/matvec.h> 00038 00039 namespace VTK 00040 { 00041 00042 class Disk 00043 { 00044 public: 00045 // Constructor & Destructor 00046 Disk (); 00047 ~Disk (); 00048 00049 // Alternative constructor 00050 Disk (Vec3_t const & X0, Vec3_t const & X1, double Rin, double Rout, bool Capping=true, int RRes=20, int CRes=30); 00051 00052 // Set methods 00053 Disk & SetRadiusIn (double Radius); 00054 Disk & SetRadiusOut (double Radius); 00055 Disk & SetPoints (Vec3_t const & X0, Vec3_t const & X1); 00056 Disk & SetResolution (int RRes=20, int CRes=30); 00057 Disk & SetColor (char const * Name="rose_madder", double Opacity=1.0); 00058 Disk & SetWire () { _disk_actor->GetProperty()->SetRepresentationToWireframe(); return (*this); } 00059 00060 // Methods 00061 void AddTo (VTK::Win & win) { win.AddActor(_disk_actor); } 00062 00063 private: 00064 vtkDiskSource * _disk; 00065 vtkTransformFilter * _transform; 00066 vtkLinearExtrusionFilter * _extrusion; 00067 vtkPolyDataMapper * _disk_mapper; 00068 vtkActor * _disk_actor; 00069 void _create (bool Cap=true); 00070 }; 00071 00072 00074 00075 00076 inline Disk::Disk () 00077 { 00078 _create (); 00079 SetResolution (); 00080 SetColor (); 00081 } 00082 00083 inline Disk::Disk (Vec3_t const & X0, Vec3_t const & X1, double Rin, double Rout, bool Cap, int RRes, int CRes) 00084 { 00085 _create (Cap); 00086 SetRadiusIn (Rin); 00087 SetRadiusOut (Rout); 00088 SetPoints (X0, X1); 00089 SetResolution (RRes, CRes); 00090 SetColor (); 00091 } 00092 00093 inline Disk::~Disk () 00094 { 00095 _disk -> Delete(); 00096 _transform -> Delete(); 00097 _disk_mapper -> Delete(); 00098 _disk_actor -> Delete(); 00099 } 00100 00101 inline Disk & Disk::SetRadiusIn (double Rin) 00102 { 00103 _disk->SetInnerRadius (Rin); 00104 return (*this); 00105 } 00106 00107 inline Disk & Disk::SetRadiusOut (double Rout) 00108 { 00109 _disk->SetOuterRadius (Rout); 00110 return (*this); 00111 } 00112 00113 inline Disk & Disk::SetPoints (Vec3_t const & X0, Vec3_t const & X1) 00114 { 00115 // update length 00116 Vec3_t V(X1-X0); 00117 00118 // extrude along z 00119 double len = Norm(V); 00120 _extrusion->SetVector (0.0, 0.0, len); 00121 00122 // translate 00123 Vec3_t cen(X0); // center of disk 00124 vtkTransform * affine = vtkTransform ::New(); 00125 affine->Translate (cen(0), cen(1), cen(2)); 00126 00127 // rotate 00128 Vec3_t vy(0.0, 0.0, 1.0); // direction of extruded source 00129 double angle = (180.0/Util::PI)*acos(dot(vy,V)/norm(V)); // angle of rotation 00130 if (angle>0.0) 00131 { 00132 Vec3_t axis = cross(vy, V); // axis of rotation 00133 if (norm(axis)>0.0) // not parallel 00134 { 00135 affine->RotateWXYZ (angle, axis.data()); 00136 } 00137 else // parallel and oposite (alpha=180) 00138 { 00139 affine->RotateWXYZ (angle, 0.0, 0.0, 1.0); // use z-direction for mirroring 00140 } 00141 } 00142 00143 // tranform 00144 _transform->SetTransform (affine); 00145 00146 // clean up 00147 affine->Delete (); 00148 return (*this); 00149 } 00150 00151 inline Disk & Disk::SetResolution (int RRes, int CRes) 00152 { 00153 _disk->SetRadialResolution (RRes); 00154 _disk->SetCircumferentialResolution (CRes); 00155 return (*this); 00156 } 00157 00158 inline Disk & Disk::SetColor (char const * Name, double Opacity) 00159 { 00160 Vec3_t c(Colors::Get(Name)); 00161 _disk_actor->GetProperty()->SetColor (c.data()); 00162 _disk_actor->GetProperty()->SetOpacity (Opacity); 00163 return (*this); 00164 } 00165 00166 inline void Disk::_create (bool Cap) 00167 { 00168 _disk = vtkDiskSource ::New(); 00169 _extrusion = vtkLinearExtrusionFilter::New(); 00170 _transform = vtkTransformFilter ::New(); 00171 _disk_mapper = vtkPolyDataMapper ::New(); 00172 _disk_actor = vtkActor ::New(); 00173 _extrusion -> SetCapping (Cap); 00174 _extrusion -> SetInput (_disk->GetOutput()); 00175 _transform -> SetInput (_extrusion->GetOutput()); 00176 _disk_mapper -> SetInput (_transform->GetPolyDataOutput()); 00177 _disk_actor -> SetMapper (_disk_mapper); 00178 } 00179 00180 }; // namespace VTK 00181 00182 #endif // MECHSYS_DISK_H