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