00001
00002
00003
00004 #include "geomrep/TubeRep.h"
00005
00006 #include "geometry/Tube.h"
00007
00008 TubeRep::TubeRep(const Shape& shape)
00009 : m_tube( dynamic_cast<const Tube& >(shape) )
00010 {};
00011
00012
00013 inline static void createPolyLine(gui::DisplayRep* v, Vector a[], unsigned n)
00014 {
00015 v->move_to(a[0]);
00016 for(unsigned i=1;i<n;i++)
00017 v->line_to(a[i]);
00018 }
00019
00020 static inline void createPolyLine(gui::DisplayRep& v, Vector a[], unsigned n)
00021 {
00022 v.move_to(a[0]);
00023 for(unsigned i=1;i<n;i++)
00024 v.line_to(a[i]);
00025 }
00026
00027 void
00028 TubeRep::update()
00029 {
00030
00031
00032
00033
00034
00035
00036 const int ns = 97, rib=ns/8 ;
00037 Vector vi0[ns];
00038 Vector vo0[ns];
00039 Vector vi1[ns];
00040 Vector vo1[ns];
00041 Vector tv;
00042
00043 int nonzero_inner_radius = m_tube.innerRadius()>0;
00044
00045 double hl = 0.5 * m_tube.length();
00046 double phi = 0.0;
00047 double dphi = 2.0 * M_PI / ( ns - 1 );
00048 Vector a = m_tube.axis(),
00049 c = m_tube.center(),
00050 c0 = c - hl * a,
00051 c1 = c + hl * a;
00052 Vector c0hat( 1.0, 0.0, 0.0 );
00053 float ax = a.x();
00054 float ay = a.y();
00055 float d = sqrt( ax * ax + ay * ay );
00056 if ( d > 0.0 )
00057 c0hat = Vector( -ay/d, ax/d, 0.0 );
00058 Vector c1hat = a.cross( c0hat );
00059 int i;
00060 for ( i = 0; i < ns; i++ ) {
00061 phi = i * dphi;
00062 tv = cos( phi ) * c0hat + sin( phi ) * c1hat;
00063 vi0[i] = c0 + m_tube.innerRadius() * tv;
00064 vi1[i] = c1 + m_tube.innerRadius() * tv;
00065 vo0[i] = c0 + m_tube.outerRadius() * tv;
00066 vo1[i] = c1 + m_tube.outerRadius() * tv;
00067 }
00068
00069 if ( nonzero_inner_radius ) {
00070 createPolyLine( this, vi0, ns );
00071 createPolyLine( this, vi1, ns );
00072 }
00073
00074 createPolyLine( this, vo0, ns );
00075 createPolyLine( this, vo1, ns );
00076
00077 for ( i = 0; i < ns; i += rib ) {
00078 Vector pl[] = { vo0[i], vo1[i], vi1[i], vi0[i], vo0[i] };
00079 createPolyLine(this, pl, 5 );
00080 }
00081 }
00082