00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef VTKWRAP_ARROW3D_H
00022 #define VTKWRAP_ARROW3D_H
00023
00024 #include "vtkConeSource.h"
00025 #include "vtkCylinderSource.h"
00026 #include "vtkAppendPolyData.h"
00027 #include "vtkTransform.h"
00028 #include "vtkTransformFilter.h"
00029 #include "vtkPoints.h"
00030 #include "vtkFloatArray.h"
00031 #include "vtkPolyData.h"
00032 #include "vtkPointData.h"
00033 #include "vtkArrowSource.h"
00034 #include "vtkGlyph3D.h"
00035 #include "vtkPolyDataMapper.h"
00036 #include "vtkProperty.h"
00037 #include "vtkLODActor.h"
00038
00039 #include "tensors/tensor1.h"
00040
00041 #include "colors.h"
00042
00043 class Arrow
00044 {
00045 friend std::ostream & operator<< (std::ostream & os, Arrow const & ar);
00046 public:
00047
00048 class Geometry
00049 {
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 public:
00068 Geometry(double BodyRadius=0.015, double TipRadius=0.03, double TipMultiplier=4.0, int Resolution=22)
00069 : _bod_rad (BodyRadius) ,
00070 _tip_rad (TipRadius) ,
00071 _tip_mul (TipMultiplier) ,
00072 _resolut (Resolution) ,
00073 _tip_len (_tip_mul*_tip_rad)
00074 {}
00075 Geometry(Geometry const & G)
00076 {
00077 _bod_rad = G._bod_rad;
00078 _tip_rad = G._tip_rad;
00079 _tip_mul = G._tip_mul;
00080 _resolut = G._resolut;
00081 _tip_len = G._tip_len;
00082 }
00083 double BodRad() const { return _bod_rad; }
00084 double TipRad() const { return _tip_rad; }
00085 double TipMul() const { return _tip_mul; }
00086 int Resolu() const { return _resolut; }
00087 double TipLen() const { return _tip_len; }
00088
00089 private:
00090 double _bod_rad;
00091 double _tip_rad;
00092 double _tip_mul;
00093 int _resolut;
00094 double _tip_len;
00095 };
00096
00097
00098 Arrow(double vx, double vy, double vz, char const * Color=NULL, double x0=0.0, double y0=0.0, double z0=0.0, Geometry const * Geom=NULL)
00099 { _construct(vx,vy,vz, x0,y0,z0, Color, Geom); }
00100
00101 Arrow(TensorsLib::Tensor1<double,3> const & V, char const * Color=NULL, double x0=0.0, double y0=0.0, double z0=0.0, Geometry const * Geom=NULL)
00102 { _construct(V[0],V[1],V[2], x0,y0,z0, Color, Geom); }
00103
00104
00105 ~Arrow()
00106 {
00107 _vector -> Delete();
00108 _vector_mapper -> Delete();
00109 _vector_actor -> Delete();
00110 }
00111
00112
00113 vtkActor * GetActor() { return _vector_actor; }
00114
00115 private:
00116
00117 vtkGlyph3D * _vector;
00118 vtkPolyDataMapper * _vector_mapper;
00119 vtkLODActor * _vector_actor;
00120 void _construct(double vx, double vy, double vz, double x0, double y0, double z0, char const * Color, Geometry const * Geom);
00121 };
00122
00123 std::ostream & operator<< (std::ostream & os, Arrow const & ar)
00124 {
00125 ar._vector->Print(os);
00126 return os;
00127 }
00128
00129
00131
00132 inline void Arrow::_construct(double vx, double vy, double vz, double x0, double y0, double z0, char const * Color, Geometry const * Geom)
00133 {
00134
00135 std::string * color;
00136 if (Color==NULL) color = new std::string ("red");
00137 else color = new std::string (Color);
00138
00139
00140 Geometry * geom;
00141 if (Geom==NULL) geom = new Geometry;
00142 else geom = new Geometry (*Geom);
00143
00144
00145 double length = sqrt(vx*vx + vy*vy + vz*vz);
00146 double bod_len = length-geom->TipLen();
00147 double bod_cen = bod_len/2.0;
00148 double tip_cen = bod_len+geom->TipLen()/2.0;
00149
00150
00151 vtkConeSource * cone = vtkConeSource ::New();
00152 vtkCylinderSource * cylin = vtkCylinderSource ::New();
00153 vtkAppendPolyData * arrow = vtkAppendPolyData ::New();
00154 cylin -> SetCenter (0.0, bod_cen, 0.0);
00155 cylin -> SetHeight (bod_len);
00156 cylin -> SetRadius (geom->BodRad());
00157 cylin -> SetResolution (geom->Resolu());
00158 cone -> SetCenter (0.0, tip_cen, 0.0);
00159 cone -> SetDirection (0,1,0);
00160 cone -> SetHeight (geom->TipLen());
00161 cone -> SetRadius (geom->TipRad());
00162 cone -> SetResolution (geom->Resolu());
00163 arrow -> AddInput (cone->GetOutput());
00164 arrow -> AddInput (cylin->GetOutput());
00165
00166
00167 vtkTransform * rotate = vtkTransform ::New();
00168 vtkTransformFilter * arrow_along_x = vtkTransformFilter ::New();
00169 rotate -> RotateZ (-90.0);
00170 arrow_along_x -> SetTransform (rotate);
00171 arrow_along_x -> SetInput (arrow->GetOutput());
00172
00173
00174 vtkPoints * points = vtkPoints ::New();
00175 vtkFloatArray * vectors = vtkFloatArray ::New();
00176 vtkPolyData * polydata = vtkPolyData ::New();
00177 points -> SetNumberOfPoints (1);
00178 points -> InsertPoint (0, x0,y0,z0);
00179 vectors -> SetNumberOfComponents (3);
00180 vectors -> SetNumberOfTuples (1);
00181 vectors -> InsertTuple3 (0, vx,vy,vz);
00182 polydata -> SetPoints (points);
00183 polydata -> GetPointData() -> SetVectors(vectors);
00184
00185
00186 _vector = vtkGlyph3D ::New();
00187 _vector -> SetInput (polydata);
00188 _vector -> SetSource (arrow_along_x->GetPolyDataOutput());
00189 _vector -> SetVectorModeToUseVector ();
00190 _vector -> SetScaleModeToDataScalingOff ();
00191
00192
00193 _vector_mapper = vtkPolyDataMapper ::New();
00194 _vector_actor = vtkLODActor ::New();
00195 _vector_mapper -> SetInputConnection (_vector->GetOutputPort());
00196 _vector_actor -> SetMapper (_vector_mapper);
00197 _vector_actor -> GetProperty ()->SetColor(CLR[color->c_str()].C);
00198 _vector_actor -> VisibilityOn ();
00199
00200
00201 cone -> Delete();
00202 cylin -> Delete();
00203 arrow -> Delete();
00204 rotate -> Delete();
00205 arrow_along_x -> Delete();
00206 points -> Delete();
00207 vectors -> Delete();
00208 polydata -> Delete();
00209
00210
00211 delete color;
00212 delete geom;
00213
00214 }
00215
00216 #endif // VTKWRAP_ARROW3D_H
00217
00218