]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.hxx
Machete filter visualization finished.
[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 V >
36 void cpPlugins::Interface::Parameters::
37 ConfigureAsVector( const TString& name, const TUint& dim, const V& v )
38 {
39   std::stringstream str;
40   str << v[ 0 ];
41   for( unsigned int d = 1; d < dim; ++d )
42     str << ";" << v[ d ];
43   std::string s = str.str( );
44   this->m_Parameters[ name ] =
45     TParameter( Self::Vector, TValues( s, s ) );
46   this->Modified( );
47 }
48
49 // -------------------------------------------------------------------------
50 template< class I >
51 I cpPlugins::Interface::Parameters::
52 GetIndex( const TString& name, const TUint& dim ) const
53 {
54   I v;
55   TParameters::const_iterator i = this->m_Parameters.find( name );
56   if( i != this->m_Parameters.end( ) )
57   {
58     if( i->second.first == Self::Index )
59     {
60       std::istringstream str( i->second.second.second );
61       std::string token;
62       unsigned int d = 0;
63       while( std::getline( str, token, ';' ) && d < dim )
64       {
65         v[ d ] = std::atoi( token.c_str( ) );
66         d++;
67
68       } // elihw
69       return( v );
70
71     } // fi
72
73   } // fi
74
75   // If parameter not found
76   for( unsigned int d = 0; d < dim; ++d )
77     v[ d ] = 0;
78   return( v );
79 }
80
81 // -------------------------------------------------------------------------
82 template< class P >
83 P cpPlugins::Interface::Parameters::
84 GetPoint( const TString& name, const TUint& dim ) const
85 {
86   P v;
87   TParameters::const_iterator i = this->m_Parameters.find( name );
88   if( i != this->m_Parameters.end( ) )
89   {
90     if( i->second.first == Self::Point )
91     {
92       std::istringstream str( i->second.second.second );
93       std::string token;
94       unsigned int d = 0;
95       while( std::getline( str, token, ';' ) && d < dim )
96       {
97         std::istringstream tok_str( token );
98         tok_str >> v[ d ];
99         d++;
100
101       } // elihw
102       return( v );
103
104     } // fi
105
106   } // fi
107
108   // If parameter not found
109   for( unsigned int d = 0; d < dim; ++d )
110     v[ d ] = float( 0 );
111   return( v );
112 }
113
114 // -------------------------------------------------------------------------
115 template< class V >
116 V cpPlugins::Interface::Parameters::
117 GetVector( const TString& name, const TUint& dim ) const
118 {
119   V v;
120   TParameters::const_iterator i = this->m_Parameters.find( name );
121   if( i != this->m_Parameters.end( ) )
122   {
123     if( i->second.first == Self::Vector )
124     {
125       std::istringstream str( i->second.second.second );
126       std::string token;
127       unsigned int d = 0;
128       while( std::getline( str, token, ';' ) && d < dim )
129       {
130         std::istringstream tok_str( token );
131         tok_str >> v[ d ];
132         d++;
133
134       } // elihw
135       return( v );
136
137     } // fi
138
139   } // fi
140
141   // If parameter not found
142   for( unsigned int d = 0; d < dim; ++d )
143     v[ d ] = float( 0 );
144   return( v );
145 }
146
147 // -------------------------------------------------------------------------
148 template< class I >
149 void cpPlugins::Interface::Parameters::
150 GetIndexList(
151   std::vector< I >& lst, const TString& name, const TUint& dim
152   ) const
153 {
154   lst.clear( );
155
156   TParameters::const_iterator i = this->m_Parameters.find( name );
157   if( i == this->m_Parameters.end( ) )
158     return;
159   if( i->second.first == Self::IndexList )
160     return;
161
162   std::istringstream str( i->second.second.second );
163   std::string token;
164   unsigned int d = 0;
165   while( std::getline( str, token, '#' ) )
166   {
167     std::istringstream str2( token );
168     std::string token2;
169     unsigned int d = 0;
170     I v;
171     while( std::getline( str2, token2, ';' ) && d < dim )
172     {
173       v[ d ] = std::atoi( token.c_str( ) );
174       d++;
175
176     } // elihw
177     lst.push_back( v );
178
179   } // elihw
180 }
181
182 // -------------------------------------------------------------------------
183 template< class P >
184 void cpPlugins::Interface::Parameters::
185 GetPointList(
186   std::vector< P >& lst, const TString& name, const TUint& dim
187   ) const
188 {
189   lst.clear( );
190
191   TParameters::const_iterator i = this->m_Parameters.find( name );
192   if( i == this->m_Parameters.end( ) )
193     return;
194   if( i->second.first == Self::PointList )
195     return;
196
197   std::istringstream str( i->second.second.second );
198   std::string token;
199   unsigned int d = 0;
200   while( std::getline( str, token, '#' ) )
201   {
202     std::istringstream str2( token );
203     std::string token2;
204     unsigned int d = 0;
205     P v;
206     while( std::getline( str2, token2, ';' ) && d < dim )
207     {
208       std::istringstream tok_str( token );
209       tok_str >> v[ d ];
210       d++;
211
212     } // elihw
213     lst.push_back( v );
214
215   } // elihw
216 }
217
218 // -------------------------------------------------------------------------
219 template< class V >
220 void cpPlugins::Interface::Parameters::
221 GetVectorList(
222   std::vector< V >& lst, const TString& name, const TUint& dim
223   ) const
224 {
225   lst.clear( );
226
227   TParameters::const_iterator i = this->m_Parameters.find( name );
228   if( i == this->m_Parameters.end( ) )
229     return;
230   if( i->second.first == Self::VectorList )
231     return;
232
233   std::istringstream str( i->second.second.second );
234   std::string token;
235   unsigned int d = 0;
236   while( std::getline( str, token, '#' ) )
237   {
238     std::istringstream str2( token );
239     std::string token2;
240     unsigned int d = 0;
241     V v;
242     while( std::getline( str2, token2, ';' ) && d < dim )
243     {
244       std::istringstream tok_str( token );
245       tok_str >> v[ d ];
246       d++;
247
248     } // elihw
249     lst.push_back( v );
250
251   } // elihw
252 }
253
254 // -------------------------------------------------------------------------
255 template< class I >
256 void cpPlugins::Interface::Parameters::
257 SetIndex( const TString& name, const TUint& dim, const I& v )
258 {
259   TParameters::iterator i = this->m_Parameters.find( name );
260   if( i == this->m_Parameters.end( ) )
261     return;
262   if( i->second.first != Self::Index )
263     return;
264
265   std::stringstream str;
266   str << v[ 0 ];
267   for( unsigned int d = 1; d < dim; ++d )
268     str << ";" << v[ d ];
269   i->second.second.second = str.str( );
270   this->Modified( );
271 }
272
273 // -------------------------------------------------------------------------
274 template< class P >
275 void cpPlugins::Interface::Parameters::
276 SetPoint( const TString& name, const TUint& dim, const P& v )
277 {
278   TParameters::iterator i = this->m_Parameters.find( name );
279   if( i == this->m_Parameters.end( ) )
280     return;
281   if( i->second.first != Self::Point )
282     return;
283
284   std::stringstream str;
285   str << v[ 0 ];
286   for( unsigned int d = 1; d < dim; ++d )
287     str << ";" << v[ d ];
288   i->second.second.second = str.str( );
289   this->Modified( );
290 }
291
292 // -------------------------------------------------------------------------
293 template< class V >
294 void cpPlugins::Interface::Parameters::
295 SetVector( const TString& name, const TUint& dim, const V& v )
296 {
297   TParameters::iterator i = this->m_Parameters.find( name );
298   if( i == this->m_Parameters.end( ) )
299     return;
300   if( i->second.first != Self::Vector )
301     return;
302
303   std::stringstream str;
304   str << v[ 0 ];
305   for( unsigned int d = 1; d < dim; ++d )
306     str << ";" << v[ d ];
307   i->second.second.second = str.str( );
308   this->Modified( );
309 }
310
311 // -------------------------------------------------------------------------
312 template< class I >
313 void cpPlugins::Interface::Parameters::
314 AddToIndexList( const TString& name, const TUint& dim, const I& v )
315 {
316   TParameters::iterator i = this->m_Parameters.find( name );
317   if( i == this->m_Parameters.end( ) )
318     return;
319   if( i->second.first != Self::IndexList )
320     return;
321
322   std::stringstream str;
323   if( i->second.second.second == "" )
324     str << v[ 0 ];
325   else
326     str << "#" << v[ 0 ];
327   for( unsigned int d = 1; d < dim; ++d )
328     str << ";" << v[ d ];
329   i->second.second.second += str.str( );
330   this->Modified( );
331 }
332
333 // -------------------------------------------------------------------------
334 template< class P >
335 void cpPlugins::Interface::Parameters::
336 AddToPointList( const TString& name, const TUint& dim, const P& v )
337 {
338   TParameters::iterator i = this->m_Parameters.find( name );
339   if( i == this->m_Parameters.end( ) )
340     return;
341   if( i->second.first != Self::PointList )
342     return;
343
344   std::stringstream str;
345   if( i->second.second.second == "" )
346     str << v[ 0 ];
347   else
348     str << "#" << v[ 0 ];
349   for( unsigned int d = 1; d < dim; ++d )
350     str << ";" << v[ d ];
351   i->second.second.second += str.str( );
352   this->Modified( );
353 }
354
355 // -------------------------------------------------------------------------
356 template< class V >
357 void cpPlugins::Interface::Parameters::
358 AddToVectorList( const TString& name, const TUint& dim, const V& v )
359 {
360   TParameters::iterator i = this->m_Parameters.find( name );
361   if( i == this->m_Parameters.end( ) )
362     return;
363   if( i->second.first != Self::VectorList )
364     return;
365
366   std::stringstream str;
367   if( i->second.second.second == "" )
368     str << v[ 0 ];
369   else
370     str << "#" << v[ 0 ];
371   for( unsigned int d = 1; d < dim; ++d )
372     str << ";" << v[ d ];
373   i->second.second.second += str.str( );
374   this->Modified( );
375 }
376
377 #endif // __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
378
379 // eof - $RCSfile$