![]() |
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_SPHERES_H 00020 #define MECHSYS_SPHERES_H 00021 00022 // VTK 00023 #include <vtkPoints.h> 00024 #include <vtkPointData.h> 00025 #include <vtkDoubleArray.h> 00026 #include <vtkSphereSource.h> 00027 #include <vtkGlyph3D.h> 00028 #include <vtkPolyDataMapper.h> 00029 #include <vtkLODActor.h> 00030 #include <vtkProperty.h> 00031 #include <vtkLookupTable.h> 00032 #include <vtkTextActor3D.h> 00033 00034 // MechSys 00035 #include <mechsys/vtk/win.h> 00036 #include <mechsys/util/maps.h> 00037 #include <mechsys/util/colors.h> 00038 #include <mechsys/util/string.h> 00039 #include <mechsys/linalg/matvec.h> 00040 00041 namespace VTK 00042 { 00043 00044 class Spheres 00045 { 00046 public: 00047 // Constructor & Destructor 00048 Spheres () : Ids(NULL) { _create(); SetResolution (); } 00049 ~Spheres (); 00050 00051 // Alternative constructor 00052 Spheres (Array<Vec3_t> const & X, Array<double> const & R, int ThetaRes=20, int PhiRes=20); 00053 Spheres (Table const & XYZR, int ThetaRes=20, int PhiRes=20); 00054 Spheres (Array<double> const & X, Array<double> const & Y, Array<double> const & Z, Array<double> const & R, int ThetaRes=20, int PhiRes=20); 00055 00056 // Set methods 00057 Spheres & SetSpheres (Array<Vec3_t> const & X, Array<double> const * R=NULL); 00058 Spheres & SetSpheres (Table const & XYZR); 00059 Spheres & SetSpheres (Array<double> const & X, Array<double> const & Y, Array<double> const & Z, Array<double> const * R=NULL); 00060 Spheres & SetCenter (int Id, Vec3_t const & X); 00061 Spheres & SetRadius (int Id, double R); 00062 Spheres & SetResolution (int ThetaRes=20, int PhiRes=20); 00063 Spheres & SetColor (char const * Name="red", double Opacity=0.8); 00064 00065 // Alternative set methods 00066 void SetNumSpheres (int Num) { _points->SetNumberOfPoints(Num); _scalars->SetNumberOfTuples(Num); } 00067 void InsertSphere (int Id, double x, double y, double z, double r) { _points->InsertPoint(Id,x,y,z); _scalars->InsertTuple1(Id,2.0*r); } 00068 void InsertSphere (int Id, Vec3_t const & X, double r) { _points->InsertPoint(Id,X(0),X(1),X(2)); _scalars->InsertTuple1(Id,2.0*r); } 00069 void SetSphere (int Id, Vec3_t const & X, double r) { _points->SetPoint(Id,X(0),X(1),X(2)); _scalars->SetTuple1(Id,2.0*r); } 00070 void Modified () { _spheres->Modified(); } 00071 00072 // Methods 00073 void ShowIds (double OriX=90, double OriY=90, double OriZ=45, double Scale=0.003, int SizePt=14, bool Shadow=true, char const * Color="black"); 00074 void AddTo (VTK::Win & win, bool RstCam=true); 00075 void DelFrom (VTK::Win & win); 00076 00077 int const * Ids; // to be set externally: size = num points/spheres 00078 00079 private: 00080 vtkPoints * _points; 00081 vtkDoubleArray * _scalars; 00082 vtkSphereSource * _sphere; 00083 vtkGlyph3D * _spheres; 00084 vtkPolyDataMapper * _spheres_mapper; 00085 vtkLODActor * _spheres_actor; 00086 vtkLookupTable * _ltable; 00087 Array<vtkTextActor3D*> _text; 00088 void _create (); 00089 }; 00090 00091 00093 00094 00095 inline Spheres::Spheres (Array<Vec3_t> const & X, Array<double> const & R, int ThetaRes, int PhiRes) 00096 : Ids(NULL) 00097 { 00098 _create (); 00099 SetSpheres (X, &R); 00100 SetResolution (ThetaRes, PhiRes); 00101 } 00102 00103 inline Spheres::Spheres (Table const & XYZR, int ThetaRes, int PhiRes) 00104 : Ids(NULL) 00105 { 00106 _create (); 00107 SetSpheres (XYZR); 00108 SetResolution (ThetaRes, PhiRes); 00109 } 00110 00111 inline Spheres::Spheres (Array<double> const & X, Array<double> const & Y, Array<double> const & Z, Array<double> const & R, int ThetaRes, int PhiRes) 00112 : Ids(NULL) 00113 { 00114 _create (); 00115 SetSpheres (X, Y, Z, &R); 00116 SetResolution (ThetaRes, PhiRes); 00117 } 00118 00119 inline Spheres::~Spheres () 00120 { 00121 _points -> Delete(); 00122 _scalars -> Delete(); 00123 _sphere -> Delete(); 00124 _spheres -> Delete(); 00125 _spheres_mapper -> Delete(); 00126 _spheres_actor -> Delete(); 00127 _ltable -> Delete(); 00128 for (size_t i=0; i<_text.Size(); ++i) _text[i] -> Delete(); 00129 } 00130 00131 inline Spheres & Spheres::SetSpheres (Array<Vec3_t> const & X, Array<double> const * R) 00132 { 00133 _points -> SetNumberOfPoints (X.Size()); 00134 _scalars -> SetNumberOfTuples (X.Size()); 00135 for (size_t i=0; i<X.Size(); ++i) 00136 { 00137 _points -> InsertPoint (i, X[i](0), X[i](1), X[i](2)); 00138 if (R==NULL) _scalars -> InsertTuple1 (i, 1.0); 00139 else _scalars -> InsertTuple1 (i, 2.0*(*R)[i]); 00140 } 00141 return (*this); 00142 } 00143 00144 inline Spheres & Spheres::SetSpheres (Table const & XYZR) 00145 { 00146 Array<double> const & X = XYZR("X"); 00147 Array<double> const & Y = XYZR("Y"); 00148 Array<double> const & Z = XYZR("Z"); 00149 Array<double> const & R = XYZR("R"); 00150 return SetSpheres (X, Y, Z, &R); 00151 } 00152 00153 inline Spheres & Spheres::SetSpheres (Array<double> const & X, Array<double> const & Y, Array<double> const & Z, Array<double> const * R) 00154 { 00155 if (Y.Size()!=X.Size()) throw new Fatal("Spheres::SetSpheres: X and Y arrays must have the same size"); 00156 if (Z.Size()!=X.Size()) throw new Fatal("Spheres::SetSpheres: X and Z arrays must have the same size"); 00157 _points -> SetNumberOfPoints (X.Size()); 00158 _scalars -> SetNumberOfTuples (X.Size()); 00159 for (size_t i=0; i<X.Size(); ++i) 00160 { 00161 _points -> InsertPoint (i, X[i], Y[i], Z[i]); 00162 if (R==NULL) _scalars -> InsertTuple1 (i, 1.0); 00163 else _scalars -> InsertTuple1 (i, 2.0*(*R)[i]); 00164 } 00165 return (*this); 00166 } 00167 00168 inline Spheres & Spheres::SetCenter (int Id, Vec3_t const & X) 00169 { 00170 _points -> SetPoint (Id, X(0), X(1), X(2)); 00171 return (*this); 00172 } 00173 00174 inline Spheres & Spheres::SetRadius (int Id, double R) 00175 { 00176 _scalars -> SetTuple1 (Id, R); 00177 return (*this); 00178 } 00179 00180 inline Spheres & Spheres::SetResolution (int ThetaRes, int PhiRes) 00181 { 00182 _sphere -> SetThetaResolution (ThetaRes); 00183 _sphere -> SetPhiResolution (PhiRes); 00184 return (*this); 00185 } 00186 00187 inline Spheres & Spheres::SetColor (char const * Name, double Opacity) 00188 { 00189 Vec3_t c = Colors::Get(Name); 00190 _ltable->SetNumberOfColors (2); 00191 _ltable->Build (); 00192 _ltable->SetTableValue (0, c(0), c(1), c(2)); 00193 _ltable->SetTableValue (1, c(0), c(1), c(2)); 00194 _spheres_actor->GetProperty()->SetOpacity (Opacity); 00195 return (*this); 00196 } 00197 00198 inline void Spheres::ShowIds (double OriX, double OriY, double OriZ, double Scale, int SizePt, bool Shadow, char const * Color) 00199 { 00200 Vec3_t c(Colors::Get(Color)); 00201 for (size_t i=0; i<_text.Size(); ++i) _text[i] -> Delete(); 00202 String buf; 00203 _text.Resize (_points->GetNumberOfPoints()); 00204 for (int i=0; i<_points->GetNumberOfPoints(); ++i) 00205 { 00206 if (Ids==NULL) buf.Printf ("%d",i); 00207 else buf.Printf ("%d",Ids[i]); 00208 _text[i] = vtkTextActor3D ::New(); 00209 _text[i] -> SetInput (buf.CStr()); 00210 _text[i] -> SetPosition (_points->GetPoint(i)); 00211 _text[i] -> SetOrientation (OriX, OriY, OriZ); 00212 _text[i] -> SetScale (Scale); 00213 _text[i] -> GetTextProperty()-> SetFontSize (SizePt); 00214 _text[i] -> GetTextProperty()-> SetShadow (Shadow); 00215 _text[i] -> GetTextProperty()-> SetColor (c.data()); 00216 } 00217 } 00218 00219 inline void Spheres::AddTo (VTK::Win & win, bool RstCam) 00220 { 00221 win.AddActor (_spheres_actor, RstCam); 00222 for (size_t i=0; i<_text.Size(); ++i) win.AddActor (reinterpret_cast<vtkActor*>(_text[i]), RstCam); 00223 } 00224 00225 inline void Spheres::DelFrom (VTK::Win & win) 00226 { 00227 win.DelActor (_spheres_actor); 00228 for (size_t i=0; i<_text.Size(); ++i) win.DelActor (reinterpret_cast<vtkActor*>(_text[i])); 00229 } 00230 00231 inline void Spheres::_create () 00232 { 00233 // points and scalars 00234 _points = vtkPoints ::New(); 00235 _scalars = vtkDoubleArray ::New(); 00236 _scalars -> SetNumberOfComponents (1); 00237 00238 // polydata 00239 vtkPolyData * polydata = vtkPolyData ::New(); 00240 polydata -> SetPoints (_points); 00241 polydata -> GetPointData()->SetScalars (_scalars); 00242 00243 // spheres 00244 _sphere = vtkSphereSource ::New(); 00245 _spheres = vtkGlyph3D ::New(); 00246 _spheres_mapper = vtkPolyDataMapper ::New(); 00247 _spheres_actor = vtkLODActor ::New(); 00248 _ltable = vtkLookupTable ::New(); 00249 _spheres -> SetInput (polydata); 00250 _spheres -> SetSource (_sphere->GetOutput()); 00251 _spheres -> SetScaleModeToScaleByScalar (); 00252 _spheres -> SetColorModeToColorByScalar (); 00253 _spheres -> SetScaleFactor (1.0); 00254 _spheres_mapper -> SetInputConnection (_spheres->GetOutputPort()); 00255 _spheres_mapper -> SetLookupTable (_ltable); 00256 _spheres_actor -> SetMapper (_spheres_mapper); 00257 SetColor (); 00258 } 00259 00260 }; // namespace VTK 00261 00262 #endif // MECHSYS_SPHERES_H