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