Go to the documentation of this file.00001 #ifndef OPENTISSUE_UTILITY_GL_GL_DRAW_VECTOR_H
00002 #define OPENTISSUE_UTILITY_GL_GL_DRAW_VECTOR_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/utility/gl/gl.h>
00013 #include <OpenTissue/core/math/math_vector3.h>
00014 #include <cmath>
00015
00016 namespace OpenTissue
00017 {
00018
00019 namespace gl
00020 {
00021
00028 template <typename vector3_type>
00029 inline void DrawVector( vector3_type const & p, vector3_type const & v , double scale = 1.0, bool draw_pos = true )
00030 {
00031 using std::acos;
00032 using std::sqrt;
00033 using std::min;
00034
00035 typedef typename vector3_type::value_type value_type;
00036
00037 value_type total_length = sqrt( v * v );
00038 value_type arrow_cone_height = min( 0.2, total_length * 0.2 );
00039 value_type shaft_height = total_length - arrow_cone_height;
00040
00041 value_type shaft_radius = min( 0.05, total_length * 0.05 ) * scale;
00042 value_type arrow_cone_radius = 2 * shaft_radius;
00043 value_type origin_radius = 1.2 * arrow_cone_radius;
00044
00045 GLUquadric * qobj = gluNewQuadric();
00046 GLint slices;
00047 GLint stacks;
00048
00049 if (draw_pos)
00050 {
00051 glPushMatrix();
00052 glTranslatef( p(0), p(1), p(2) );
00053 slices = 8;
00054 stacks = 8;
00055 gluSphere( qobj, origin_radius, slices, stacks );
00056 glPopMatrix();
00057 }
00058
00059 GLfloat angle = 0;
00060 GLfloat x = 1;
00061 GLfloat y = 0;
00062 GLfloat z = 0;
00063
00064
00065 if ( ( v(0) == 0 ) && ( v(1) == 0 ) )
00066 {
00067 if ( v(2) > 0 )
00068 {
00069 angle = 0;
00070 }
00071 else
00072 {
00073 angle = 180;
00074 }
00075 }
00076 else
00077 {
00078 vector3_type k(0,0,1);
00079 vector3_type v_unit;
00080 v_unit = unit( v );
00081 vector3_type axis = unit( ( v % k ) );
00082 angle = acos( v_unit * k );
00083 angle = -180.0 * angle / 3.1415;
00084 x = axis(0);
00085 y = axis(1);
00086 z = axis(2);
00087 }
00088
00089 glPushMatrix();
00090 glTranslatef( p(0), p(1), p(2) );
00091 glRotatef( angle, x, y, z );
00092 slices = 12;
00093 stacks = 1;
00094 gluCylinder( qobj, shaft_radius, shaft_radius, shaft_height, slices, stacks );
00095 glPopMatrix();
00096
00097 glPushMatrix();
00098 glTranslatef( p(0), p(1), p(2) );
00099 glRotatef( angle, x, y, z );
00100 glTranslatef( 0, 0, shaft_height );
00101 gluCylinder( qobj, arrow_cone_radius, 0, arrow_cone_height, slices, stacks );
00102 glPopMatrix();
00103
00104 gluDeleteQuadric( qobj );
00105 }
00106
00112 template <typename vector3_type>
00113 inline void DrawVector( vector3_type const & v )
00114 {
00115 DrawVector( vector3_type( 0, 0, 0 ), v );
00116 }
00117
00118 }
00119
00120 }
00121
00122
00123 #endif