]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.hxx
4b4e3424f699f8b167aa9b0fe6a7d394affb33f0
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.hxx
1 #ifndef __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
2 #define __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
3
4 // -------------------------------------------------------------------------
5 template< class I >
6 void cpPlugins::Interface::Parameters::
7 ConfigureAsIndex( const TString& name, const TUint& dim, const I& v )
8 {
9   std::stringstream str;
10   str << v[ 0 ];
11   for( unsigned int d = 1; d < dim; ++d )
12     str << ";" << v[ d ];
13   std::string s = str.str( );
14   this->m_Parameters[ name ] =
15     TParameter( Self::Index, TValues( s, s ) );
16   this->Modified( );
17 }
18
19 // -------------------------------------------------------------------------
20 template< class P >
21 void cpPlugins::Interface::Parameters::
22 ConfigureAsPoint( const TString& name, const TUint& dim, const P& v )
23 {
24   std::stringstream str;
25   str << v[ 0 ];
26   for( unsigned int d = 1; d < dim; ++d )
27     str << ";" << v[ d ];
28   std::string s = str.str( );
29   this->m_Parameters[ name ] =
30     TParameter( Self::Point, TValues( s, s ) );
31   this->Modified( );
32 }
33
34 // -------------------------------------------------------------------------
35 template< class I >
36 I cpPlugins::Interface::Parameters::
37 GetIndex( const TString& name, const TUint& dim ) const
38 {
39   I v;
40   TParameters::const_iterator i = this->m_Parameters.find( name );
41   if( i != this->m_Parameters.end( ) )
42   {
43     if( i->second.first == Self::Index )
44     {
45       std::istringstream str( i->second.second.second );
46       std::string token;
47       unsigned int d = 0;
48       while( std::getline( str, token, ';' ) && d < dim )
49       {
50         v[ d ] = std::atoi( token.c_str( ) );
51         d++;
52
53       } // elihw
54       return( v );
55
56     } // fi
57
58   } // fi
59
60   // If parameter not found
61   for( unsigned int d = 0; d < dim; ++d )
62     v[ d ] = 0;
63   return( v );
64 }
65
66 // -------------------------------------------------------------------------
67 template< class P >
68 P cpPlugins::Interface::Parameters::
69 GetPoint( const TString& name, const TUint& dim ) const
70 {
71   P v;
72   TParameters::const_iterator i = this->m_Parameters.find( name );
73   if( i != this->m_Parameters.end( ) )
74   {
75     if( i->second.first == Self::Point )
76     {
77       std::istringstream str( i->second.second.second );
78       std::string token;
79       unsigned int d = 0;
80       while( std::getline( str, token, ';' ) && d < dim )
81       {
82         v[ d ] = std::atof( token.c_str( ) );
83         d++;
84
85       } // elihw
86       return( v );
87
88     } // fi
89
90   } // fi
91
92   // If parameter not found
93   for( unsigned int d = 0; d < dim; ++d )
94     v[ d ] = float( 0 );
95   return( v );
96 }
97
98 // -------------------------------------------------------------------------
99 template< class I >
100 void cpPlugins::Interface::Parameters::
101 GetIndexList(
102   std::vector< I >& lst, const TString& name, const TUint& dim
103   ) const
104 {
105   lst.clear( );
106
107   TParameters::const_iterator i = this->m_Parameters.find( name );
108   if( i == this->m_Parameters.end( ) )
109     return;
110   if( i->second.first == Self::IndexList )
111     return;
112
113   std::istringstream str( i->second.second.second );
114   std::string token;
115   unsigned int d = 0;
116   while( std::getline( str, token, '#' ) )
117   {
118     std::istringstream str2( token );
119     std::string token2;
120     unsigned int d = 0;
121     I v;
122     while( std::getline( str2, token2, ';' ) && d < dim )
123     {
124       v[ d ] = std::atoi( token.c_str( ) );
125       d++;
126
127     } // elihw
128     lst.push_back( v );
129
130   } // elihw
131 }
132
133 // -------------------------------------------------------------------------
134 template< class P >
135 void cpPlugins::Interface::Parameters::
136 GetPointList(
137   std::vector< P >& lst, const TString& name, const TUint& dim
138   ) const
139 {
140   lst.clear( );
141
142   TParameters::const_iterator i = this->m_Parameters.find( name );
143   if( i == this->m_Parameters.end( ) )
144     return;
145   if( i->second.first == Self::PointList )
146     return;
147
148   std::istringstream str( i->second.second.second );
149   std::string token;
150   unsigned int d = 0;
151   while( std::getline( str, token, '#' ) )
152   {
153     std::istringstream str2( token );
154     std::string token2;
155     unsigned int d = 0;
156     P v;
157     while( std::getline( str2, token2, ';' ) && d < dim )
158     {
159       v[ d ] = std::atof( token.c_str( ) );
160       d++;
161
162     } // elihw
163     lst.push_back( v );
164
165   } // elihw
166 }
167
168 // -------------------------------------------------------------------------
169 template< class I >
170 void cpPlugins::Interface::Parameters::
171 SetIndex( const TString& name, const TUint& dim, const I& v )
172 {
173   TParameters::iterator i = this->m_Parameters.find( name );
174   if( i == this->m_Parameters.end( ) )
175     return;
176   if( i->second.first != Self::Index )
177     return;
178
179   std::stringstream str;
180   str << v[ 0 ];
181   for( unsigned int d = 1; d < dim; ++d )
182     str << ";" << v[ d ];
183   i->second.second.second = str.str( );
184   this->Modified( );
185 }
186
187 // -------------------------------------------------------------------------
188 template< class P >
189 void cpPlugins::Interface::Parameters::
190 SetPoint( const TString& name, const TUint& dim, const P& v )
191 {
192   TParameters::iterator i = this->m_Parameters.find( name );
193   if( i == this->m_Parameters.end( ) )
194     return;
195   if( i->second.first != Self::Point )
196     return;
197
198   std::stringstream str;
199   str << v[ 0 ];
200   for( unsigned int d = 1; d < dim; ++d )
201     str << ";" << v[ d ];
202   i->second.second.second = str.str( );
203   this->Modified( );
204 }
205
206 // -------------------------------------------------------------------------
207 template< class I >
208 void cpPlugins::Interface::Parameters::
209 AddToIndexList( const TString& name, const TUint& dim, const I& v )
210 {
211   TParameters::iterator i = this->m_Parameters.find( name );
212   if( i == this->m_Parameters.end( ) )
213     return;
214   if( i->second.first != Self::IndexList )
215     return;
216
217   std::stringstream str;
218   if( i->second.second.second == "" )
219     str << v[ 0 ];
220   else
221     str << "#" << v[ 0 ];
222   for( unsigned int d = 1; d < dim; ++d )
223     str << ";" << v[ d ];
224   i->second.second.second += str.str( );
225   this->Modified( );
226 }
227
228 // -------------------------------------------------------------------------
229 template< class P >
230 void cpPlugins::Interface::Parameters::
231 AddToPointList( const TString& name, const TUint& dim, const P& v )
232 {
233   TParameters::iterator i = this->m_Parameters.find( name );
234   if( i == this->m_Parameters.end( ) )
235     return;
236   if( i->second.first != Self::PointList )
237     return;
238
239   std::stringstream str;
240   if( i->second.second.second == "" )
241     str << v[ 0 ];
242   else
243     str << "#" << v[ 0 ];
244   for( unsigned int d = 1; d < dim; ++d )
245     str << ";" << v[ d ];
246   i->second.second.second += str.str( );
247   this->Modified( );
248 }
249
250 #endif // __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
251
252 // eof - $RCSfile$