]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/DataStructures/Simple3DCurve.cxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / DataStructures / Simple3DCurve.cxx
1 #include <cpExtensions/DataStructures/Simple3DCurve.h>
2
3 // -------------------------------------------------------------------------
4 template< class _TScalar >
5 void cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
6 Modified( ) const
7 {
8   this->Superclass::Modified( );
9   this->m_FramesUpdated = false;
10 }
11
12 // -------------------------------------------------------------------------
13 template< class _TScalar >
14 void cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
15 Clear( )
16 {
17   this->m_Points.clear( );
18   this->m_Frames.clear( );
19   this->Modified( );
20 }
21
22 // -------------------------------------------------------------------------
23 template< class _TScalar >
24 unsigned long cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
25 GetNumberOfPoints( ) const
26 {
27   return( this->m_Points.size( ) );
28 }
29
30 // -------------------------------------------------------------------------
31 template< class _TScalar >
32 const typename cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
33 TMatrix& cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
34 GetFrame( unsigned int id ) const
35 {
36   if( !( this->m_FramesUpdated ) )
37   {
38     unsigned long N = this->m_Points.size( );
39
40     std::vector< TVector > tg( N );
41     tg[ 0 ] = this->m_Points[ 1 ] - this->m_Points[ 0 ];
42     tg[ N - 1 ] = this->m_Points[ N - 1 ] - this->m_Points[ N - 2 ];
43     for( unsigned int i = 1; i < N - 1; ++i )
44       tg[ i ] = this->m_Points[ i + 1 ] - this->m_Points[ i - 1 ];
45
46     std::vector< TVector > no( N );
47     std::vector< TVector > bn( N );
48     TVector prev_tg( _TScalar( 0 ) ), prev_no( _TScalar( 0 ) );
49     prev_tg[ 0 ] = _TScalar( 1 );
50     prev_no[ 1 ] = _TScalar( 1 );
51     for( unsigned int i = 0; i < N; ++i )
52     {
53       auto ntg = tg[ i ].GetNorm( );
54       if( double( ntg ) > double( 0 ) )
55         tg[ i ] /= ntg;
56
57       _TScalar ct = prev_tg * tg[ i ];
58       TVector a = itk::CrossProduct( prev_tg, tg[ i ] );
59       _TScalar st = a.GetNorm( );
60       if( st > _TScalar( 0 ) )
61         a /= st;
62
63       no[ i ] =
64         ( prev_no * ct ) +
65         ( itk::CrossProduct( a, prev_no ) * st ) +
66         ( a * ( ( a * prev_no ) * ( _TScalar( 1 ) - ct ) ) );
67       auto nno = no[ i ].GetNorm( );
68       if( double( nno ) > double( 0 ) )
69         no[ i ] /= nno;
70
71       bn[ i ] = itk::CrossProduct( tg[ i ], no[ i ] );
72
73       prev_tg = tg[ i ];
74       prev_no = no[ i ];
75
76     } // rof
77
78     this->m_Frames.clear( );
79     for( unsigned int i = 0; i < N; ++i )
80     {
81       TMatrix m;
82       for( unsigned int d = 0; d < 3; ++d )
83       {
84         m[ d ][ 0 ] = tg[ i ][ d ];
85         m[ d ][ 1 ] = no[ i ][ d ];
86         m[ d ][ 2 ] = bn[ i ][ d ];
87
88       } // rof
89       this->m_Frames.push_back( m );
90
91     } // rof
92
93     this->m_FramesUpdated = true;
94
95   } // fi
96   return( this->m_Frames[ id ] );
97 }
98
99 // -------------------------------------------------------------------------
100 template< class _TScalar >
101 const typename cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
102 TPoint& cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
103 GetPoint( unsigned int id ) const
104 {
105   return( this->m_Points[ id ] );
106 }
107
108 // -------------------------------------------------------------------------
109 template< class _TScalar >
110 typename cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
111 TVector cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
112 GetVector( unsigned int id ) const
113 {
114   return( this->m_Points[ id ].GetVectorFromOrigin( ) );
115 }
116
117 // -------------------------------------------------------------------------
118 template< class _TScalar >
119 cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
120 Simple3DCurve( )
121   : Superclass( ),
122     m_FramesUpdated( false )
123 {
124 }
125
126 // -------------------------------------------------------------------------
127 template< class _TScalar >
128 cpExtensions::DataStructures::Simple3DCurve< _TScalar >::
129 ~Simple3DCurve( )
130 {
131 }
132
133 // -------------------------------------------------------------------------
134 template class cpExtensions::DataStructures::Simple3DCurve< float >;
135 template class cpExtensions::DataStructures::Simple3DCurve< double >;
136
137 // eof - $RCSfile$