]> Creatis software - bbtk.git/blob - packages/std/src/bbstdMathOperationVector.cxx
#3474 bug profile_creatools in Linux/Win32
[bbtk.git] / packages / std / src / bbstdMathOperationVector.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbstdMathOperationVector.h"
5 #include "bbstdPackage.h"
6
7 #include <math.h>
8 #include <time.h>
9
10 namespace bbstd
11 {
12
13 BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,MathOperationVector)
14 BBTK_BLACK_BOX_IMPLEMENTATION(MathOperationVector,bbtk::AtomicBlackBox);
15
16 double MathOperationVector::g(std::vector<double>*v,unsigned int i,double defValue )
17 {
18         double result=defValue;
19         if (i < (*v).size()) 
20         {
21                 result= (*v)[i];
22         }
23         return result;
24 }
25
26 void MathOperationVector::SumVector(std::vector<double> *v,std::vector<double> *vr)
27 {
28         unsigned int i;
29         double tmpResult=0;
30         unsigned int sizeVec=v->size(); 
31         if ( sizeVec>0) 
32         { 
33                 for (i=0;i<sizeVec;i++) 
34                 { 
35                         tmpResult = (*v)[i] + tmpResult; 
36                 } // for i 
37                 vr->push_back( tmpResult ); 
38         } // if sizeVec
39 }
40
41
42 void MathOperationVector::MinVector(std::vector<double> *v,std::vector<double> *vr)
43 {
44         unsigned int i;
45         double tmpResult;               
46         unsigned int sizeVec=v->size(); 
47         if ( sizeVec>0) 
48         { 
49                 tmpResult=(*v)[0];
50                 for (i=1;i<sizeVec;i++) 
51                 { 
52                         tmpResult = std::min ( (*v)[i] , tmpResult ) ; 
53                 } // for i 
54                 vr->push_back( tmpResult ); 
55         } // if sizeVec
56 }
57
58
59 void MathOperationVector::MaxVector(std::vector<double> *v,std::vector<double> *vr)
60 {
61         unsigned int i;
62         double tmpResult;               
63         unsigned int sizeVec=v->size(); 
64         if ( sizeVec>0) 
65         { 
66                 tmpResult=(*v)[0];
67                 for (i=1;i<sizeVec;i++) 
68                 { 
69                         tmpResult = std::max ( (*v)[i] , tmpResult ) ; 
70                 } // for i 
71                 vr->push_back( tmpResult ); 
72         } // if sizeVec
73 }
74
75 //===== 
76 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
77 //===== 
78 void MathOperationVector::Process()
79 {
80 // THE MAIN PROCESSING METHOD BODY
81 //   Here we simply set the input 'In' value to the output 'Out'
82 //   And print out the output value
83 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
84 //    void bbSet{Input|Output}NAME(const TYPE&)
85 //    const TYPE& bbGet{Input|Output}NAME() const 
86 //    Where :
87 //    * NAME is the name of the input/output
88 //      (the one provided in the attribute 'name' of the tag 'input')
89 //    * TYPE is the C++ type of the input/output
90 //      (the one provided in the attribute 'type' of the tag 'input')
91
92         bool error;
93         unsigned int i;
94         unsigned int sizeVec = bbGetInputIn0().size();
95         double tmpResult;
96         std::vector<double> resultVec;
97         std::vector<double> _v0=bbGetInputIn0();
98         std::vector<double> _v1=bbGetInputIn1();
99         std::vector<double> _v2=bbGetInputIn2();
100         std::vector<double> _v3=bbGetInputIn3();
101         std::vector<double> _v4=bbGetInputIn4();
102         std::vector<double> _v5=bbGetInputIn5();
103         std::vector<double> _v6=bbGetInputIn6();
104         std::vector<double> _v7=bbGetInputIn7();
105         std::vector<double> _v8=bbGetInputIn8();
106         std::vector<double> _v9=bbGetInputIn9();
107
108         std::vector<double> *v0 = &_v0;
109         std::vector<double> *v1 = &_v1;
110         std::vector<double> *v2 = &_v2;
111         std::vector<double> *v3 = &_v3;
112         std::vector<double> *v4 = &_v4;
113         std::vector<double> *v5 = &_v5;
114         std::vector<double> *v6 = &_v6;
115         std::vector<double> *v7 = &_v7;
116         std::vector<double> *v8 = &_v8;
117         std::vector<double> *v9 = &_v9;
118
119         double minTmp;
120         double maxTmp;
121
122          if (firsttime==true)
123          {
124                 firsttime=false;
125          /* initialize random seed: */ 
126         srand (time(NULL));
127                 if (bbGetInputType()==7) 
128                 {
129                         for (i=0;i<sizeVec;i++)
130                         {
131                                 acum.push_back(bbGetInputIn0()[i]);
132                         } // for
133                 } // if 7
134          } // if firsttime
135
136
137     if (bbGetInputType()==0) 
138         {
139                 for (i=0;i<sizeVec;i++)
140                 {
141                         tmpResult = g(v0,i,0) + g(v1,i,0) + g(v2,i,0) + g(v3,i,0) +
142                                                 g(v4,i,0) + g(v5,i,0) + g(v6,i,0) + 
143                                                 g(v7,i,0) + g(v8,i,0) + g(v9,i,0) ;
144                         resultVec.push_back( tmpResult );
145                 } // for i
146                 bbSetOutputOut( resultVec );
147         } // if Type 0
148
149     if (bbGetInputType()==-99) 
150         {
151                 SumVector(v0,&resultVec);
152                 SumVector(v1,&resultVec);
153                 SumVector(v2,&resultVec);
154                 SumVector(v3,&resultVec);
155                 SumVector(v4,&resultVec);
156                 SumVector(v5,&resultVec);
157                 SumVector(v6,&resultVec);
158                 SumVector(v7,&resultVec);
159                 SumVector(v8,&resultVec);
160                 SumVector(v9,&resultVec);
161
162                 bbSetOutputOut( resultVec );
163         } // if Type -99
164
165
166
167     if (bbGetInputType()==1)
168         {
169                 for (i=0;i<sizeVec;i++)
170                 {
171                         tmpResult = g(v0,i,0) - g(v1,i,0) - g(v2,i,0) - g(v3,i,0) -
172                                                 g(v4,i,0) - g(v5,i,0) - g(v6,i,0) -
173                                                 g(v7,i,0) - g(v8,i,0) - g(v9,i,0) ;
174                         resultVec.push_back( tmpResult );
175                 } // for i
176                 bbSetOutputOut( resultVec );
177         } // if Type 1  
178
179
180     if (bbGetInputType()==2) 
181         {
182                 for (i=0;i<sizeVec;i++)
183                 {
184                         tmpResult = g(v0,i,0) * g(v1,i,1) * g(v2,i,1) * g(v3,i,1) *
185                                                 g(v4,i,1) * g(v5,i,1) * g(v6,i,1) *
186                                                 g(v7,i,1) * g(v8,i,1) * g(v9,i,1) ;
187                         resultVec.push_back( tmpResult );
188                 } // for i
189                 bbSetOutputOut( resultVec );
190         } // if Type 2
191
192
193
194     if (bbGetInputType()==3)
195     {
196                 error = false;
197                 for (i=0;i<sizeVec;i++)
198                 {
199             if (g(v1,i,1)!=0) tmpResult = g(v0,i,0) / g(v1,i,1); else error=true;
200             if (g(v2,i,1)!=0) tmpResult = tmpResult / g(v2,i,1); else error=true;
201             if (g(v3,i,1)!=0) tmpResult = tmpResult / g(v3,i,1); else error=true;
202             if (g(v4,i,1)!=0) tmpResult = tmpResult / g(v4,i,1); else error=true;
203             if (g(v5,i,1)!=0) tmpResult = tmpResult / g(v5,i,1); else error=true;
204             if (g(v6,i,1)!=0) tmpResult = tmpResult / g(v6,i,1); else error=true;
205             if (g(v7,i,1)!=0) tmpResult = tmpResult / g(v7,i,1); else error=true;
206             if (g(v8,i,1)!=0) tmpResult = tmpResult / g(v8,i,1); else error=true;
207             if (g(v9,i,1)!=0) tmpResult = tmpResult / g(v9,i,1); else error=true;
208                         if (error==true)  tmpResult = 99999;
209                         resultVec.push_back( tmpResult );
210                 } // for i
211                 bbSetOutputOut( resultVec );
212     } // if Type 3
213
214
215     if (bbGetInputType()==4)
216         { 
217                 for (i=0;i<sizeVec;i++)
218                 {
219                 if (bbGetInputIn0()[i]>=0) tmpResult = sqrt( bbGetInputIn0()[i] ) ;
220                else  tmpResult = 0 ;
221                         resultVec.push_back( tmpResult );
222                 } // for i
223                 bbSetOutputOut( resultVec );
224         } // if Type 4
225
226
227     if (bbGetInputType()==5)
228          {
229                 for (i=0;i<sizeVec;i++)
230                 {
231                 if (bbGetInputIn0()[i]>0)  tmpResult = log( bbGetInputIn0()[i] ) ;
232                else  tmpResult = -99999 ; 
233                         resultVec.push_back( tmpResult );
234                 } // for i
235                 bbSetOutputOut( resultVec );
236          } // if Type 5
237
238
239
240     if (bbGetInputType()==6)
241         {
242                 for (i=0;i<sizeVec;i++)
243                 {
244                         resultVec.push_back(  exp( bbGetInputIn0()[i] )  );
245                 } // for i
246                 bbSetOutputOut( resultVec );
247         } // Type 6
248
249
250     if (bbGetInputType()==7)
251         {
252                 bbSetOutputOut( acum );
253                 for (i=0;i<sizeVec;i++)
254                 {
255                         acum[i]++;
256                 if (acum[i]>g(v1,i,0)) acum[i]=g(v0,i,0);
257                 } // for
258         } // if Type 7
259
260     if (bbGetInputType()==8 )
261         {
262                 for (i=0;i<sizeVec;i++)
263                 {
264                         resultVec.push_back(  rand() % (int)( g(v1,i,0)-g(v0,i,0) ) + g(v0,i,0) );
265                 } // for i
266                 bbSetOutputOut( resultVec );
267         } // Type 8
268
269     if (bbGetInputType()==9)
270         {
271                 for (i=0;i<sizeVec;i++)
272                 {
273                         resultVec.push_back(  sin(  bbGetInputIn0()[i] )  );
274                 } // for i
275                 bbSetOutputOut( resultVec );
276         } // Type 9
277
278     if (bbGetInputType()==10)
279         {
280                 for (i=0;i<sizeVec;i++)
281                 {
282                         resultVec.push_back(  cos(  bbGetInputIn0()[i] )  );
283                 } // for i
284                 bbSetOutputOut( resultVec );
285         } // Type 10
286
287     if (bbGetInputType()==11)
288         {
289                 for (i=0;i<sizeVec;i++)
290                 {
291                         resultVec.push_back(  tan(  bbGetInputIn0()[i] )  );
292                 } // for i
293                 bbSetOutputOut( resultVec );
294         } // Type 11
295
296     if (bbGetInputType()==12)
297         {
298                 for (i=0;i<sizeVec;i++)
299                 {
300                         resultVec.push_back(  asin(  bbGetInputIn0()[i] )  );
301                 } // for i
302                 bbSetOutputOut( resultVec );
303         } // Type 12
304
305     if (bbGetInputType()==13)
306         {
307                 for (i=0;i<sizeVec;i++)
308                 {
309                         resultVec.push_back(  acos(  bbGetInputIn0()[i] )  );
310                 } // for i
311                 bbSetOutputOut( resultVec );
312         } // Type 13
313
314     if (bbGetInputType()==14)
315         {
316                 for (i=0;i<sizeVec;i++)
317                 {
318                         resultVec.push_back(  atan(  bbGetInputIn0()[i] )  );
319                 } // for i
320                 bbSetOutputOut( resultVec );
321         } // Type 14
322
323     if (bbGetInputType()==15)
324         {
325                 for (i=0;i<sizeVec;i++)
326                 {
327                         resultVec.push_back(  atan2(  g(v0,i,0) , g(v1,i,0) )  );
328                 } // for i
329                 bbSetOutputOut( resultVec );
330         } // Type 15
331
332
333     if (bbGetInputType()==16)
334         {
335                 for (i=0;i<sizeVec;i++)
336                 {
337                         minTmp = std::min(  g(v0,i,999999) , g(v1,i,999999) );
338                         minTmp = std::min(  g(v2,i,999999) , minTmp );
339                         minTmp = std::min(  g(v3,i,999999) , minTmp );
340                         minTmp = std::min(  g(v4,i,999999) , minTmp );
341                         minTmp = std::min(  g(v5,i,999999) , minTmp );
342                         minTmp = std::min(  g(v6,i,999999) , minTmp );
343                         minTmp = std::min(  g(v7,i,999999) , minTmp );
344                         minTmp = std::min(  g(v8,i,999999) , minTmp );
345                         minTmp = std::min(  g(v9,i,999999) , minTmp );
346                         resultVec.push_back( minTmp );
347                 } // for i
348                 bbSetOutputOut( resultVec );
349         } // Type 16
350
351     if (bbGetInputType()==-16)
352         {
353                 MinVector(v0,&resultVec);
354                 MinVector(v1,&resultVec);
355                 MinVector(v2,&resultVec);
356                 MinVector(v3,&resultVec);
357                 MinVector(v4,&resultVec);
358                 MinVector(v5,&resultVec);
359                 MinVector(v6,&resultVec);
360                 MinVector(v7,&resultVec);
361                 MinVector(v8,&resultVec);
362                 MinVector(v9,&resultVec);
363
364                 bbSetOutputOut( resultVec );
365         } // Type -16
366
367     if (bbGetInputType()==17)
368         {
369                 for (i=0;i<sizeVec;i++)
370                 {
371                         maxTmp = std::max(  g(v0,i,-999999) , g(v1,i,-999999) );
372                         maxTmp = std::max(  g(v2,i,-999999) , maxTmp );
373                         maxTmp = std::max(  g(v3,i,-999999) , maxTmp );
374                         maxTmp = std::max(  g(v4,i,-999999) , maxTmp );
375                         maxTmp = std::max(  g(v5,i,-999999) , maxTmp );
376                         maxTmp = std::max(  g(v6,i,-999999) , maxTmp );
377                         maxTmp = std::max(  g(v7,i,-999999) , maxTmp );
378                         maxTmp = std::max(  g(v8,i,-999999) , maxTmp );
379                         maxTmp = std::max(  g(v9,i,-999999) , maxTmp );
380                         resultVec.push_back(  maxTmp  );
381                 } // for i
382                 bbSetOutputOut( resultVec );
383         } // Type 17
384
385
386     if (bbGetInputType()==-17)
387         {
388                 MaxVector(v0,&resultVec);
389                 MaxVector(v1,&resultVec);
390                 MaxVector(v2,&resultVec);
391                 MaxVector(v3,&resultVec);
392                 MaxVector(v4,&resultVec);
393                 MaxVector(v5,&resultVec);
394                 MaxVector(v6,&resultVec);
395                 MaxVector(v7,&resultVec);
396                 MaxVector(v8,&resultVec);
397                 MaxVector(v9,&resultVec);
398                 bbSetOutputOut( resultVec );
399         } // Type -17
400
401
402     if (bbGetInputType()==18)
403         {
404                 for (i=0;i<sizeVec;i++)
405                 {
406                         resultVec.push_back(  fabs( bbGetInputIn0()[i] )  );
407                 } // for i
408                 bbSetOutputOut( resultVec );
409         } // Type 18
410
411     if (bbGetInputType()==19)
412         {
413                 for (i=0;i<sizeVec;i++)
414                 {
415                         resultVec.push_back(   (int)bbGetInputIn0()[i] % (int)bbGetInputIn1()[i]   );
416                 } // for i
417                 bbSetOutputOut( resultVec );
418         } // Type 19
419
420     if (bbGetInputType()==20)
421         {
422                 for (i=0;i<sizeVec;i++)
423                 {
424                         if (bbGetInputIn0()[i] < bbGetInputIn1()[i])
425                         {
426                                 resultVec.push_back( 1 );
427                         } else {
428                                 resultVec.push_back( 0 );
429                         }
430                 } // for i
431                 bbSetOutputOut( resultVec );
432         } // Type 20
433
434     if (bbGetInputType()==21)
435         {
436                 for (i=0;i<sizeVec;i++)
437                 {
438                         if (bbGetInputIn0()[i] > bbGetInputIn1()[i])
439                         {
440                                 resultVec.push_back( 1 );
441                         } else {
442                                 resultVec.push_back( 0 );
443                         }
444                 } // for i
445                 bbSetOutputOut( resultVec );
446         } // Type 21
447
448
449     if (bbGetInputType()==22) 
450         {
451                 double resultX=0;
452                 double resultY=0;
453                 double resultZ=0;
454                 double resultMassTotal=0;
455                 for (i=0;i<sizeVec;i++)
456                 {
457                         resultX                 = resultX + g(v0,i,0)*g(v3,i,1);
458                         resultY                 = resultY + g(v1,i,0)*g(v3,i,1);
459                         resultZ                 = resultZ + g(v2,i,0)*g(v3,i,1);
460                         resultMassTotal = resultMassTotal + g(v3,i,1);
461                 } // for i
462                 if (resultMassTotal!=0) 
463                 {
464                         resultVec.push_back( resultX / resultMassTotal );
465                         resultVec.push_back( resultY / resultMassTotal );
466                         resultVec.push_back( resultZ / resultMassTotal );
467                 } // if resultMassTotal
468                 bbSetOutputOut( resultVec );
469         } // if Type 22
470
471
472     if (bbGetInputType()==23)
473         {
474                 for (i=0;i<sizeVec;i++)
475                 {
476                         resultVec.push_back(  round(  bbGetInputIn0()[i] )  );
477                 } // for i
478                 bbSetOutputOut( resultVec );
479         } // Type 23
480
481     if (bbGetInputType()==24)
482         {
483                 for (i=0;i<sizeVec;i++)
484                 {
485                         resultVec.push_back(  rint(  bbGetInputIn0()[i] )  );
486                 } // for i
487                 bbSetOutputOut( resultVec );
488         } // Type 24
489
490     if (bbGetInputType()==25)
491         {
492                 for (i=0;i<sizeVec;i++)
493                 {
494                         resultVec.push_back(  floor(  bbGetInputIn0()[i] )  );
495                 } // for i
496                 bbSetOutputOut( resultVec );
497         } // Type 25
498
499     if (bbGetInputType()==26)
500         {
501                 for (i=0;i<sizeVec;i++)
502                 {
503                         resultVec.push_back(  ceil(  bbGetInputIn0()[i] )  );
504                 } // for i
505                 bbSetOutputOut( resultVec );
506         } // Type 26
507
508     if (bbGetInputType()==27)
509         {
510                 for (i=0;i<sizeVec;i++)
511                 {
512                         resultVec.push_back(  trunc(  bbGetInputIn0()[i] )  );
513                 } // for i
514                 bbSetOutputOut( resultVec );
515         } // Type 27
516
517 }
518
519 //===== 
520 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
521 //===== 
522 void MathOperationVector::bbUserSetDefaultValues()
523 {
524
525 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
526 //    Here we initialize the input 'In' to 0
527
528    bbSetInputType(0);
529    firsttime=true;
530   
531
532 }
533 //===== 
534 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
535 //===== 
536 void MathOperationVector::bbUserInitializeProcessing()
537 {
538
539 //  THE INITIALIZATION METHOD BODY :
540 //    Here does nothing 
541 //    but this is where you should allocate the internal/output pointers 
542 //    if any 
543
544   
545 }
546 //===== 
547 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
548 //===== 
549 void MathOperationVector::bbUserFinalizeProcessing()
550 {
551
552 //  THE FINALIZATION METHOD BODY :
553 //    Here does nothing 
554 //    but this is where you should desallocate the internal/output pointers 
555 //    if any
556   
557 }
558 }
559 // EO namespace bbstd
560
561