]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuManualContourModel_Box.cxx
11806ac6db134bcf21dd54025fcf189ec650086d
[creaMaracasVisu.git] / bbtk / src / bbcreaMaracasVisuManualContourModel_Box.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 "bbcreaMaracasVisuManualContourModel_Box.h"
5 #include "bbcreaMaracasVisuPackage.h"
6
7 #include <creaContoursFactory.h>
8
9
10 namespace bbcreaMaracasVisu
11 {
12
13 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ManualContourModel_Box)
14 BBTK_BLACK_BOX_IMPLEMENTATION(ManualContourModel_Box,bbtk::AtomicBlackBox);
15 //===== 
16 // 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)
17 //===== 
18
19
20 void ManualContourModel_Box::ProcessBySegment(  
21                         int Type, 
22                         int &iGeneral, int sizeSegment,
23                         std::vector<double> *lstInX,std::vector<double> *lstInY, std::vector<double> *lstInZ,
24                         std::vector<double> *lstOutX,std::vector<double> *lstOutY, std::vector<double> *lstOutZ,
25                         std::vector<int>        *lstIndexsOut, bool open )
26 {
27         creaContoursFactory f;
28         manualContourModel      *m;
29         int i,size=iGeneral+sizeSegment;
30         double x,y,z;
31         m = (manualContourModel*)f.getContourModel( bbGetInputType() );
32         m->SetNumberOfPointsSpline( bbGetInputNbPoints() );
33         m->SetCloseContour( open );
34         for (i=iGeneral;i<size;i++)
35         {
36                 m->AddPoint( (*lstInX)[i] , (*lstInY)[i] , (*lstInZ)[i] );
37         } // for
38         m->UpdateSpline();
39         int sizeContour = bbGetInputNbPoints();
40         for (i=0;i<sizeContour;i++)
41         {
42                 m->GetSpline_i_Point(i,&x,&y,&z);
43                 lstOutX->push_back(x);
44                 lstOutY->push_back(y);
45                 lstOutZ->push_back(z);
46         } // for
47         iGeneral=iGeneral+sizeSegment;
48         lstIndexsOut->push_back( sizeContour );
49         delete m;
50 }
51
52
53 void ManualContourModel_Box::RedistributionPoints(      std::vector<double> *lstOutX,
54                                                                                                         std::vector<double> *lstOutY, 
55                                                                                                         std::vector<double> *lstOutZ,
56                                                                                                         std::vector<int> *lstIndexsOut )
57 {
58         std::vector<double> lstRstX;
59         std::vector<double> lstRstY;
60         std::vector<double> lstRstZ;
61         int iLstIndexOut,sizeLstIndexOut = lstIndexsOut->size();
62         int ii, iGeneral=0;
63         int size;
64         for (iLstIndexOut=0;  iLstIndexOut<sizeLstIndexOut; iLstIndexOut++)
65         {
66                 lstRstX.clear();
67                 lstRstY.clear();
68                 lstRstZ.clear();
69                 size=(*lstIndexsOut)[iLstIndexOut];
70                 if (size>2)
71                 {
72                         double dist=0,dist2,distSeg,delta;
73                         double dd,dx,dy,dz;
74                         int i,k;
75                         for ( i=iGeneral+1 ; i<iGeneral+size ; i++ )
76                         {
77                                 dx      = (*lstOutX)[i]-(*lstOutX)[i-1];
78                                 dy      = (*lstOutY)[i]-(*lstOutY)[i-1];
79                                 dz      = (*lstOutZ)[i]-(*lstOutZ)[i-1];
80                                 dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
81                         } //for
82                         delta = dist/(size-1);
83                         lstRstX.push_back( (*lstOutX)[iGeneral] );
84                         lstRstY.push_back( (*lstOutY)[iGeneral] );
85                         lstRstZ.push_back( (*lstOutZ)[iGeneral] );
86                         for (i=iGeneral+1; i<iGeneral+size;i++)
87                         {
88                                 dist2 = 0;
89                                 for (k=iGeneral+1; k<iGeneral+size;k++)
90                                 {
91                                         dx = (*lstOutX)[k]-(*lstOutX)[k-1];
92                                         dy = (*lstOutY)[k]-(*lstOutY)[k-1];
93                                         dz = (*lstOutZ)[k]-(*lstOutZ)[k-1];
94                                         distSeg = sqrt( dx*dx + dy*dy + dz*dz );
95                                         ii = i-iGeneral;
96                                         if ( dist2+distSeg >= ii*delta )
97                                         {
98                                                 dd=(ii*delta-dist2)/distSeg;
99                                                 if (distSeg==0)
100                                                 {
101                                                         dd = 0;
102                                                 } // if distSeg == 0
103                                                 lstRstX.push_back( (*lstOutX)[k-1] + dd*dx );
104                                                 lstRstY.push_back( (*lstOutY)[k-1] + dd*dy );
105                                                 lstRstZ.push_back( (*lstOutZ)[k-1] + dd*dz );
106                                                 k=iGeneral+size;
107                                         } else {
108                                                 if ( k==iGeneral+size-1 )
109                                                 {
110                                                         dd = 1;
111                                                         lstRstX.push_back( (*lstOutX)[k-1] + dd*dx );
112                                                         lstRstY.push_back( (*lstOutY)[k-1] + dd*dy );
113                                                         lstRstZ.push_back( (*lstOutZ)[k-1] + dd*dz );
114                                                 }
115                                         }// if dist2 
116                                         dist2 = dist2+distSeg;
117                                 } // for k
118                         } //for i   
119                         if (lstRstX.size()!=size) 
120                         {
121                                 printf("EED Warnning!   ManualContourModel_Box::RedistributionPoints  >> This list is not coherent  iLstIndexOut=%d  lstRstX.size()=%d  size=%d\n",iLstIndexOut, lstRstX.size(), size);
122                         }
123                         for (i=iGeneral; i<iGeneral+size;i++)
124                         {
125                                 ii=i-iGeneral;
126                                 (*lstOutX)[i] = lstRstX[ii];
127                                 (*lstOutY)[i] = lstRstY[ii];
128                                 (*lstOutZ)[i] = lstRstZ[ii];
129                         } // for i
130                 } // if size>2
131                 iGeneral=iGeneral+size;
132         }// for iLstIndexOut
133 }
134
135
136 //-------------------------------------------------------------------------------------------------------------
137
138
139 void ManualContourModel_Box::ExtractContour(    std::vector<double> *lstX,
140                                                                                                 std::vector<double> *lstY, 
141                                                                                                 std::vector<double> *lstZ,
142                                                                                                 std::vector<int>        *lstIndexs,
143                                                                                                 int contour,
144                                                                                                 std::vector<double> *lstOutX,
145                                                                                                 std::vector<double> *lstOutY, 
146                                                                                                 std::vector<double> *lstOutZ )
147 {
148         int     i;
149         int     iLstIndex;
150         int     iContour;
151         int     sizeLstIndexslstIndexs;
152         int     iGeneral;
153         int     iGeneralPlusSize;
154         if ( (lstX!=NULL) &&  (lstY!=NULL) && (lstZ!=NULL) && (lstIndexs!=NULL) && (lstOutX!=NULL) && (lstOutY!=NULL) && (lstOutZ!=NULL) )
155         {
156                 sizeLstIndexslstIndexs  = (*lstIndexs).size();
157                 if  ( sizeLstIndexslstIndexs!=0 ) 
158                 {
159                         (*lstOutX).clear();
160                         (*lstOutY).clear();
161                         (*lstOutZ).clear();
162                         iGeneral                                = 0;
163                         iGeneralPlusSize                = (*lstIndexs)[0];
164                         for ( iContour=1 ; iContour<=contour ; iContour++ )
165                         {
166                                 iGeneral                        = iGeneral+(*lstIndexs)[iContour-1];
167                                 iGeneralPlusSize        = iGeneral+(*lstIndexs)[iContour];
168                         } // for iContour
169                         for ( i=iGeneral ; i<iGeneralPlusSize ; i++ )
170                         {
171                                 (*lstOutX).push_back( (*lstX)[i] );
172                                 (*lstOutY).push_back( (*lstY)[i] );
173                                 (*lstOutZ).push_back( (*lstZ)[i] );                             
174                         } //for
175                 } // size
176         } // NULL
177 }
178
179 void ManualContourModel_Box::PutPointsInContour(std::vector<double> *lstTmpX,
180                                                                                                 std::vector<double> *lstTmpY, 
181                                                                                                 std::vector<double> *lstTmpZ,
182                                                                                                 int contour,
183                                                                                                 std::vector<double> *lstOutX,
184                                                                                                 std::vector<double> *lstOutY, 
185                                                                                                 std::vector<double> *lstOutZ,
186                                                                                                 std::vector<int>        *lstOutIndexs )
187 {
188         int     i;
189         int     iLstIndex;
190         int     iContour;
191         int     sizeLstIndexslstIndexs;
192         int     iGeneral;
193         int     iGeneralPlusSize;
194         int     iSize;
195         int     SizeContour;
196         if ( (lstTmpX!=NULL) &&  (lstTmpY!=NULL) && (lstTmpZ!=NULL)  && (lstOutX!=NULL) && (lstOutY!=NULL) && (lstOutZ!=NULL) && (lstOutIndexs!=NULL) )
197         {
198                 sizeLstIndexslstIndexs  = (*lstOutIndexs).size();
199                 if  ( sizeLstIndexslstIndexs!=0 ) 
200                 {
201                         iGeneral                                = 0;
202                         SizeContour                             = (*lstOutIndexs)[0];
203                         iGeneralPlusSize                = iGeneral + SizeContour;
204                         for ( iContour=1 ; iContour<=contour ; iContour++ )
205                         {
206                                 iGeneral                        = iGeneral + (*lstOutIndexs)[iContour-1];
207                                 SizeContour             = (*lstOutIndexs)[iContour];
208                                 iGeneralPlusSize        = iGeneral + SizeContour;
209                         } // for iContour
210                         
211                         if(SizeContour==(*lstTmpX).size() )
212                         {
213                                 int iSize=0;
214                                 for ( i=iGeneral ; i<iGeneralPlusSize ; i++ )
215                                 {
216                                         (*lstOutX)[i] = (*lstTmpX)[iSize] ;
217                                         (*lstOutY)[i] = (*lstTmpY)[iSize] ;
218                                         (*lstOutZ)[i] = (*lstTmpZ)[iSize] ;
219                                         iSize++;                                        
220                                 } //for                 
221                         } else {
222                                 printf("EED Warnning!! ManualContourModel_Box::PutPointsInContour  the lstTmp vector is not of the correct size. SizeContour=%d  lstTmp.size=%d\n"
223                                                                                                                                                                                                                 ,SizeContour, (*lstTmpX).size() );
224                                 for ( i=iGeneral ; i<iGeneralPlusSize ; i++ )
225                                 {
226                                         (*lstOutX)[i] = -9999 ;
227                                         (*lstOutY)[i] = -9999 ;
228                                         (*lstOutZ)[i] = -9999 ;
229                                 } //for                 
230                         }
231                 } // size
232         } // NULL
233 }
234
235 double ManualContourModel_Box::SizeContour(     std::vector<double> *lstX,
236                                                                                         std::vector<double> *lstY, 
237                                                                                         std::vector<double> *lstZ)
238 {
239         int     i;
240         double  dx;
241         double  dy;
242         double  dz;
243         double  dist            = 0;
244         int     iSize           = (*lstX).size()  - 1;
245         for ( i=0 ; i<iSize ; i++ )
246         {
247                 dx      = (*lstX)[i+1]-(*lstX)[i];
248                 dy      = (*lstY)[i+1]-(*lstY)[i];
249                 dz      = (*lstZ)[i+1]-(*lstZ)[i];
250                 dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
251         } //for i
252         return dist;
253 }
254
255
256 void ManualContourModel_Box::Redistribution_SIN(        double alpha,
257                                                                                                         double beta,
258                                                                                                         std::vector<double> *lstC1X,
259                                                                                                         std::vector<double> *lstC1Y, 
260                                                                                                         std::vector<double> *lstC1Z,
261                                                                                                         double sizeOfContour,
262                                                                                                         std::vector<double> *lstC2X,
263                                                                                                         std::vector<double> *lstC2Y, 
264                                                                                                         std::vector<double> *lstC2Z )
265 {
266         std::vector<double> lstRstX;
267         std::vector<double> lstRstY;
268         std::vector<double> lstRstZ;
269         int     ii,iGeneral;
270         int     size,iGeneralPlusSize;
271         int     i,k;
272         int     firstK;
273         double  iiByDelta;
274         double  dist2,distSeg;
275 //      double  delta;
276         double  dd,dx,dy,dz;
277         double  t,tt, PI;
278         double  TwoPI;
279         double  dist;
280         dist            = sizeOfContour;
281         PI                      = 3.14159265;
282         TwoPI           = 2*PI;
283         iGeneral        = 0;
284 //      for (iLstIndexOut=0;  iLstIndexOut<sizeLstIndexOut; iLstIndexOut++)  // For each contour
285 //      {
286 //              lstRstX.clear();
287 //              lstRstY.clear();
288 //              lstRstZ.clear();
289 //              size                            = (*lstIndexsOut)[iLstIndexOut];
290                 size = (*lstC1X).size();
291                 iGeneralPlusSize        = iGeneral+size;
292                 if (size>2)
293                 {
294 //                      dist=0;
295 //                      for ( i=iGeneral ; i<iGeneralPlusSize-1 ; i++ )
296 //                      {
297 //                              dx      = (*lstOutX)[i]-(*lstOutX)[i+1];
298 //                              dy      = (*lstOutY)[i]-(*lstOutY)[i+1];
299 //                              dz      = (*lstOutZ)[i]-(*lstOutZ)[i+1];
300 //                              dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
301 //                      } //for
302
303
304 //                      delta = dist/(size-1);
305                         firstK  = 0;                    
306                         for (i=iGeneral; i<iGeneralPlusSize;i++)                                //  For each point of one contour
307                         {
308                                 ii = i-iGeneral;
309                                 dist2   = 0;
310                                 for (k=iGeneral; k<iGeneralPlusSize;k++)                        // Search inside
311                                 {
312 //if ( 385==(*lstOutY)[k]  )
313 //{ 
314         t               =       ((double)ii) / ((double)(size-1));
315 //      Beta    = 0.20; //  [0..1]
316 //      Alpha   = 0.0;  // [0..1]
317 //      Beta    = bbGetInputParam()[0];
318 //      Alpha   = bbGetInputParam()[1];
319         tt              = t + 0.70710678182*sin(t*TwoPI)*beta + alpha;
320         if (tt>1) { tt=tt-1; }
321         if (tt<0) { tt=tt+1; }
322         iiByDelta = tt * dist;
323 //} else {
324 //  iiByDelta = ii*delta;
325 //}
326
327 //                                      dx = (*lstOutX)[k+1]-(*lstOutX)[k];
328 //                                      dy = (*lstOutY)[k+1]-(*lstOutY)[k];
329 //                                      dz = (*lstOutZ)[k+1]-(*lstOutZ)[k];
330                                         dx = (*lstC1X)[k+1]-(*lstC1X)[k];
331                                         dy = (*lstC1Y)[k+1]-(*lstC1Y)[k];
332                                         dz = (*lstC1Z)[k+1]-(*lstC1Z)[k];
333                                         distSeg = sqrt( dx*dx + dy*dy + dz*dz );
334                                         if ( dist2+distSeg >= iiByDelta )
335                                         {
336                                                 if (distSeg==0)
337                                                 {
338                                                         dd = 0;
339                                                 } else {
340                                                         dd=(iiByDelta-dist2)/distSeg;
341                                                 }// if distSeg == 0
342                                                 lstRstX.push_back( (*lstC1X)[k] + dd*dx );
343                                                 lstRstY.push_back( (*lstC1Y)[k] + dd*dy );
344                                                 lstRstZ.push_back( (*lstC1Z)[k] + dd*dz );
345                                                 if (ii==0) { firstK=k; }
346                                                 k = iGeneralPlusSize-1;
347                                         } else {
348                                                 if ( k==iGeneral+size-2 )
349                                                 {
350                                                         dd = 1;
351                                                         lstRstX.push_back( (*lstC1X)[k] + dd*dx );
352                                                         lstRstY.push_back( (*lstC1Y)[k] + dd*dy );
353                                                         lstRstZ.push_back( (*lstC1Z)[k] + dd*dz );
354                                                 }
355                                         }// if dist2 
356                                         dist2 = dist2 + distSeg;
357                                 } // for k
358                         } //for i                       
359                         if (lstRstX.size()!=size) 
360                         {
361                                 printf("EED Warnning!   ManualContourModel_Box::Redistribution_SIN  >> This list is not coherent lstRstX.size()=%d  size=%d\n", lstRstX.size(), size);
362                         }
363                         (*lstC2X).clear();
364                         (*lstC2Y).clear();
365                         (*lstC2Z).clear();
366                         for (i=iGeneral; i<iGeneralPlusSize;i++)
367                         {
368                                 (*lstC2X).push_back(-1);        
369                                 (*lstC2Y).push_back(-1);        
370                                 (*lstC2Z).push_back(-1);        
371                         } // for i
372                         int iii;
373                         for (i=iGeneral; i<iGeneralPlusSize;i++)
374                         {
375                                 ii                              = i-iGeneral;
376                                 iii                             = iGeneral+ ( (i-iGeneral) + firstK) % size ;
377 //                              (*lstOutX)[iii] = lstRstX[ii];
378 //                              (*lstOutY)[iii] = lstRstY[ii];
379 //                              (*lstOutZ)[iii] = lstRstZ[ii];
380                                 (*lstC2X)[iii]  = lstRstX[ii];
381                                 (*lstC2Y)[iii]  = lstRstY[ii];
382                                 (*lstC2Z)[iii]  = lstRstZ[ii];
383                         } // for i
384                 } else {
385                         for (i=0; i<size ; i++)
386                         {
387                                 (*lstC2X)[i] = (*lstC1X)[i];
388                                 (*lstC2Y)[i] = (*lstC1Y)[i];
389                                 (*lstC2Z)[i] = (*lstC1Z)[i];
390                         } // for i
391                 }// if size>2
392 //              iGeneral=iGeneral+size;
393 //      }// for iLstIndexOut
394 }
395
396 void ManualContourModel_Box::CopyContour2InContour1(
397                                                                                                         std::vector<double> *lstInX,
398                                                                                                         std::vector<double> *lstInY, 
399                                                                                                         std::vector<double> *lstInZ,
400                                                                                                         std::vector<double> *lstOutX,
401                                                                                                         std::vector<double> *lstOutY, 
402                                                                                                         std::vector<double> *lstOutZ )
403 {
404         int i,sizeLstInX=(*lstInX).size();
405         (*lstOutX).clear();     
406         (*lstOutY).clear();     
407         (*lstOutZ).clear();     
408         for ( i=0 ; i<sizeLstInX ; i++ )
409         {
410                 (*lstOutX).push_back( (*lstInX)[i] );
411                 (*lstOutY).push_back( (*lstInY)[i] );
412                 (*lstOutZ).push_back( (*lstInZ)[i] );
413         } // for i
414 }
415
416 double ManualContourModel_Box::IntegralDistanceTwoContours(std::vector<double> *lstTmpAX,
417                                                                                                                         std::vector<double> *lstTmpAY, 
418                                                                                                                         std::vector<double> *lstTmpAZ,
419                                                                                                                         std::vector<double> *lstTmpBX,
420                                                                                                                         std::vector<double> *lstTmpBY, 
421                                                                                                                         std::vector<double> *lstTmpBZ )
422 {
423         int     i;
424         double  dx;
425         double  dy;
426         double  dz;
427         double  dist            = 0;
428         int     iSize           = (*lstTmpAX).size();
429         for ( i=0 ; i<iSize ; i++ )
430         {
431                 dx              = (*lstTmpAX)[i]-(*lstTmpBX)[i];
432                 dy              = (*lstTmpAY)[i]-(*lstTmpBY)[i];
433                 dz              = (*lstTmpAZ)[i]-(*lstTmpBZ)[i];
434                 dist    = dist + sqrt( dx*dx + dy*dy + dz*dz );
435         } //for i
436         return dist;
437 }
438
439
440 void ManualContourModel_Box::RedistributionPointsAllContours_SIN(       std::vector<double> *lstOutX,
441                                                                                                                                         std::vector<double> *lstOutY, 
442                                                                                                                                         std::vector<double> *lstOutZ,
443                                                                                                                                         std::vector<int>        *lstIndexsOut )
444 {
445 printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN start\n");
446         std::vector<double> lstTmp1X;
447         std::vector<double> lstTmp1Y;
448         std::vector<double> lstTmp1Z;
449         std::vector<double> lstTmp2X;
450         std::vector<double> lstTmp2Y;
451         std::vector<double> lstTmp2Z;
452         std::vector<double> lstTmp2aX;
453         std::vector<double> lstTmp2aY;
454         std::vector<double> lstTmp2aZ;
455         std::vector<double> lstTmp2bX;
456         std::vector<double> lstTmp2bY;
457         std::vector<double> lstTmp2bZ;
458         std::vector<double> lstTmp2cX;
459         std::vector<double> lstTmp2cY;
460         std::vector<double> lstTmp2cZ;
461         std::vector<double> lstTmp3X;
462         std::vector<double> lstTmp3Y;
463         std::vector<double> lstTmp3Z;
464         std::vector<int>        lstContourExeption;
465         int     iContour;
466         double  nbContours              = (*lstIndexsOut).size();
467         double  alpha,iAlpha;
468         double  beta,iBeta;
469         double  sizeContour1;
470         double  sizeContour2;
471         double  sizeContour3;
472         double  distAcum;
473         double minDistAcum;
474         
475         
476         // ------------ Wave 1 -----------------
477         
478         ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut,0,&lstTmp1X,&lstTmp1Y,&lstTmp1Z);
479         sizeContour1    = SizeContour( &lstTmp1X, &lstTmp1Y, &lstTmp1Z );
480         
481 // Increment    
482         for ( iContour=0; iContour<nbContours-1 ; iContour++ )
483         {
484 printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN iContour=%d\n",iContour);
485                 beta    = 0;
486                 alpha   = 0;
487 // find Alpha
488                 ExtractContour( lstOutX,lstOutY,lstOutZ,lstIndexsOut, iContour+1 ,&lstTmp2X,&lstTmp2Y,&lstTmp2Z );              
489                 sizeContour2    = SizeContour( &lstTmp2X, &lstTmp2Y, &lstTmp2Z );
490
491                 if (iContour+2<nbContours)
492                 {
493                         ExtractContour( lstOutX,lstOutY,lstOutZ,lstIndexsOut, iContour+2 ,&lstTmp3X,&lstTmp3Y,&lstTmp3Z );              
494                         sizeContour3    = SizeContour( &lstTmp3X, &lstTmp3Y, &lstTmp3Z );
495                 } else {
496                         sizeContour3=-1;
497                 }
498                 
499                 if ( (sizeContour1>sizeContour2) && (sizeContour2>sizeContour3) )
500                 {
501                         beta    = 0.05;
502                         alpha   = 0;
503                         minDistAcum = 999999999;
504                         for (iAlpha=0 ; iAlpha<1; iAlpha=iAlpha+0.1 ) 
505                         {
506                                 Redistribution_SIN( iAlpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
507                                 distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
508                                 if (distAcum<minDistAcum) 
509                                 {
510                                         alpha           = iAlpha;
511                                         minDistAcum     = distAcum;
512                                 } // if integerDist
513                         } // for alpha
514 // find Beta
515                         minDistAcum = 999999999;
516                         for (iBeta=0.0 ; iBeta<0.2; iBeta=iBeta+0.01) 
517                         {
518                                 Redistribution_SIN( alpha,iBeta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
519                                 distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
520                                 if (distAcum<minDistAcum) 
521                                 {
522                                         beta            = iBeta;
523                                         minDistAcum     = distAcum;
524                                 } // if integerDist
525
526                         } // for beta
527                 } //if sizeContour1>sizeContour2>sizeContour3
528                  else  {
529                         if ( (sizeContour1>sizeContour2) && (sizeContour2<sizeContour3) )
530                         {
531                                 lstContourExeption.push_back( iContour+1 );
532                         } // 
533                  }
534                  
535 // Set Alpha y Beta
536
537
538                 Redistribution_SIN( alpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
539
540 // if(lstTmp2Y[0]==385) { alpha=bbGetInputParam()[1]; beta=bbGetInputParam()[2]; printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN ---385-- \n"); }
541 printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN wve 1 alpha=%f  beta=%f  z=%f   sc1=%f   sc2=%f\n",alpha,beta,lstTmp2Y[0], sizeContour1,sizeContour2 );
542
543                 PutPointsInContour(&lstTmp2aX,&lstTmp2aY,&lstTmp2aZ, iContour+1 ,lstOutX,lstOutY,lstOutZ,lstIndexsOut);                         
544                 sizeContour1 = sizeContour2;
545                 CopyContour2InContour1( &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ , &lstTmp1X,&lstTmp1Y,&lstTmp1Z );
546         
547         } // for iContour 
548         
549         
550         
551         
552         
553         // ------------ Wave 2 -----------------
554         
555         ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut,nbContours-1,&lstTmp1X,&lstTmp1Y,&lstTmp1Z);
556         sizeContour1    = SizeContour( &lstTmp1X, &lstTmp1Y, &lstTmp1Z );
557         
558 // Increment    
559         for ( iContour=nbContours-1; iContour>=0 ; iContour-- )
560         {
561 printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN iContour=%d\n",iContour);
562                 beta    = 0;
563                 alpha   = 0;
564 // find Alpha
565                 ExtractContour( lstOutX,lstOutY,lstOutZ,lstIndexsOut, iContour-1 ,&lstTmp2X,&lstTmp2Y,&lstTmp2Z );              
566                 sizeContour2    = SizeContour( &lstTmp2X, &lstTmp2Y, &lstTmp2Z );
567                 
568                 
569                 if (iContour-2<=0)
570                 {
571                         ExtractContour( lstOutX,lstOutY,lstOutZ,lstIndexsOut, iContour-2 ,&lstTmp3X,&lstTmp3Y,&lstTmp3Z );              
572                         sizeContour3    = SizeContour( &lstTmp3X, &lstTmp3Y, &lstTmp3Z );
573                 } else {
574                         sizeContour3=-1;
575                 }
576                 
577                 if ( (sizeContour1>sizeContour2) && (sizeContour2>sizeContour3) ){
578                         beta    = 0.05;
579                         alpha   = 0;
580                         minDistAcum = 999999999;
581                         for (iAlpha=0 ; iAlpha<1; iAlpha=iAlpha+0.1 ) 
582                         {
583                                 Redistribution_SIN( iAlpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
584                                 distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
585                                 if (distAcum<minDistAcum) 
586                                 {
587                                         alpha           = iAlpha;
588                                         minDistAcum     = distAcum;
589                                 } // if integerDist
590                         } // for alpha
591 // find Beta
592                         minDistAcum = 999999999;
593                         for (iBeta=0.0 ; iBeta<0.2; iBeta=iBeta+0.01) 
594                         {
595                                 Redistribution_SIN( alpha,iBeta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
596                                 distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
597                                 if (distAcum<minDistAcum) 
598                                 {
599                                         beta            = iBeta;
600                                         minDistAcum     = distAcum;
601                                 } // if integerDist
602
603                         } // for beta
604                 } //if  sizeContour1>sizeContour2>sizeContour3
605 // Set Alpha y Beta
606
607
608                 Redistribution_SIN( alpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
609
610 // if(lstTmp2Y[0]==385) { alpha=bbGetInputParam()[1]; beta=bbGetInputParam()[2]; printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN ---385-- \n"); }
611 printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN wave 2 alpha=%f  beta=%f  z=%f   sc1=%f   sc2=%f\n",alpha,beta,lstTmp2Y[0], sizeContour1,sizeContour2 );
612
613                 PutPointsInContour(&lstTmp2aX,&lstTmp2aY,&lstTmp2aZ, iContour-1 ,lstOutX,lstOutY,lstOutZ,lstIndexsOut);                         
614                 sizeContour1 = sizeContour2;
615                 CopyContour2InContour1( &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ , &lstTmp1X,&lstTmp1Y,&lstTmp1Z );
616         
617         } // for iContour 
618         
619         
620         
621         // ------------ Wave 3 -----------------
622         
623         printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN  wave3 los que faltan");
624         double iExtra,sizeExtra=lstContourExeption.size();
625         for ( iExtra=0 ; iExtra<sizeExtra ; iExtra++ )
626         {
627                 printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN  wave3 los que faltan %d \n", lstContourExeption[iExtra] );
628          
629                 ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut, lstContourExeption[iExtra]-1       , &lstTmp1X,&lstTmp1Y,&lstTmp1Z);
630                 sizeContour1    = SizeContour( &lstTmp1X, &lstTmp1Y, &lstTmp1Z );
631                 ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut, lstContourExeption[iExtra]         , &lstTmp2X,&lstTmp2Y,&lstTmp2Z);
632                 sizeContour2    = SizeContour( &lstTmp2X, &lstTmp2Y, &lstTmp2Z );
633                 ExtractContour(lstOutX,lstOutY,lstOutZ,lstIndexsOut, lstContourExeption[iExtra]+1       , &lstTmp3X,&lstTmp3Y,&lstTmp3Z);
634                 sizeContour3    = SizeContour( &lstTmp3X, &lstTmp3Y, &lstTmp3Z );
635          
636                 beta    = 0.05;
637                 alpha   = 0;
638                 minDistAcum = 999999999;
639                 for (iAlpha=0 ; iAlpha<1; iAlpha=iAlpha+0.1 ) 
640                 {
641                         Redistribution_SIN( iAlpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
642                         distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
643                         if (distAcum<minDistAcum) 
644                         {
645                                 alpha           = iAlpha;
646                                 minDistAcum     = distAcum;
647                         } // if integerDist
648                 } // for alpha
649 // find Beta
650                 minDistAcum = 999999999;
651                 for (iBeta=0.0 ; iBeta<0.2; iBeta=iBeta+0.01) 
652                 {
653                         Redistribution_SIN( alpha,iBeta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
654                         distAcum = IntegralDistanceTwoContours( &lstTmp1X,&lstTmp1Y,&lstTmp1Z , &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
655                         if (distAcum<minDistAcum) 
656                         {
657                                 beta            = iBeta;
658                                 minDistAcum     = distAcum;
659                         } // if integerDist
660                 } // for beta    
661                 Redistribution_SIN( alpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2aX,&lstTmp2aY,&lstTmp2aZ );
662
663  
664                 beta    = 0.05;
665                 alpha   = 0;
666                 minDistAcum = 999999999;
667                 for (iAlpha=0 ; iAlpha<1; iAlpha=iAlpha+0.1 ) 
668                 {
669                         Redistribution_SIN( iAlpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
670                         distAcum = IntegralDistanceTwoContours( &lstTmp3X,&lstTmp3Y,&lstTmp3Z , &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
671                         if (distAcum<minDistAcum) 
672                         {
673                                 alpha           = iAlpha;
674                                 minDistAcum     = distAcum;
675                         } // if integerDist
676                 } // for alpha
677 // find Beta
678                 minDistAcum = 999999999;
679                 for (iBeta=0.0 ; iBeta<0.2; iBeta=iBeta+0.01) 
680                 {
681                         Redistribution_SIN( alpha,iBeta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
682                         distAcum = IntegralDistanceTwoContours( &lstTmp3X,&lstTmp3Y,&lstTmp3Z , &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
683                         if (distAcum<minDistAcum) 
684                         {
685                                 beta            = iBeta;
686                                 minDistAcum     = distAcum;
687                         } // if integerDist
688                 } // for beta    
689                 Redistribution_SIN( alpha,beta, &lstTmp2X,&lstTmp2Y,&lstTmp2Z ,sizeContour2, &lstTmp2bX,&lstTmp2bY,&lstTmp2bZ );
690                 
691 //              Mixing2DistributionVectors(&lstTmp2aX,&lstTmp2aY,&lstTmp2aZ,&lstTmp2bX,&lstTmp2bY,&lstTmp2bZ, &lstTmp2cX,&lstTmp2cY,&lstTmp2cZ );
692                 PutPointsInContour(&lstTmp2cX,&lstTmp2cY,&lstTmp2cZ,  lstContourExeption[iExtra]  ,lstOutX,lstOutY,lstOutZ,lstIndexsOut);                               
693                 
694         } // for iExtra
695         
696         
697         
698         
699         
700         
701         
702 printf("ManualContourModel_Box::RedistributionPointsAllContours_SIN End\n");
703
704 }
705  
706
707
708 void ManualContourModel_Box::RedistributionPoints_SIN_iContour(int iContour,    std::vector<double> *lstOutX,
709                                                                                                         std::vector<double> *lstOutY, 
710                                                                                                         std::vector<double> *lstOutZ,
711                                                                                                         std::vector<int> *lstIndexsOut,double alpha, double beta )
712 {
713 printf("EED ManualContourModel_Box::RedistributionPoints_SIN_iContour  Start \n");
714         std::vector<double> lstRstX;
715         std::vector<double> lstRstY;
716         std::vector<double> lstRstZ;
717         int iLstIndexOut,sizeLstIndexOut = lstIndexsOut->size();
718         int ii,iGeneral;
719         double iiByDelta;
720         int size,iGeneralPlusSize;
721         double Alpha,Beta,t,tt, PI=3.14159265;
722         double TwoPI=2*PI;
723         double dist=0,dist2,distSeg,delta;
724         double dd,dx,dy,dz;
725         int i,k;
726         int firstK;
727         double tmpX,tmpY,tmpZ;
728         iGeneral=0;
729         for (iLstIndexOut=0;  iLstIndexOut<sizeLstIndexOut; iLstIndexOut++)  // For each contour
730         {
731                 lstRstX.clear();
732                 lstRstY.clear();
733                 lstRstZ.clear();
734                 size                            = (*lstIndexsOut)[iLstIndexOut];
735                 iGeneralPlusSize        = iGeneral+size;
736                 if (size>2)
737                 {
738                         dist=0;
739                         for ( i=iGeneral ; i<iGeneralPlusSize-1 ; i++ )
740                         {
741                                 dx      = (*lstOutX)[i]-(*lstOutX)[i+1];
742                                 dy      = (*lstOutY)[i]-(*lstOutY)[i+1];
743                                 dz      = (*lstOutZ)[i]-(*lstOutZ)[i+1];
744                                 dist= dist+sqrt( dx*dx + dy*dy + dz*dz );
745                         } //for
746                         delta = dist/(size-1);
747
748                         firstK  = 0;                    
749                         for (i=iGeneral; i<iGeneralPlusSize;i++)                                //  For each point of one contour
750                         {
751                                 ii = i-iGeneral;
752                                 dist2   = 0;
753                                 for (k=iGeneral; k<iGeneralPlusSize-1;k++)                      // Search inside
754                                 {
755 if ( 385==(*lstOutY)[k]  )
756
757         t               =       ((double)ii) / ((double)(size-1));
758         Beta    = 0.20; //  [0..1]
759         Alpha   = 0.0;  // [0..1]
760         Beta    = bbGetInputParam()[0];
761         Alpha   = bbGetInputParam()[1];
762         tt              = t + 0.70710678182*sin(t*TwoPI)*Beta + Alpha;
763         if (tt>1) { tt=tt-1; }
764         if (tt<0) { tt=tt+1; }
765         iiByDelta = tt * dist;
766 } else {
767   iiByDelta = ii*delta;
768 }
769                                         dx = (*lstOutX)[k+1]-(*lstOutX)[k];
770                                         dy = (*lstOutY)[k+1]-(*lstOutY)[k];
771                                         dz = (*lstOutZ)[k+1]-(*lstOutZ)[k];
772                                         distSeg = sqrt( dx*dx + dy*dy + dz*dz );
773                                         if ( dist2+distSeg >= iiByDelta )
774                                         {
775                                                 if (distSeg==0)
776                                                 {
777                                                         dd = 0;
778                                                 } else {
779                                                         dd=(iiByDelta-dist2)/distSeg;
780                                                 }// if distSeg == 0
781                                                 lstRstX.push_back( (*lstOutX)[k] + dd*dx );
782                                                 lstRstY.push_back( (*lstOutY)[k] + dd*dy );
783                                                 lstRstZ.push_back( (*lstOutZ)[k] + dd*dz );
784                                                 if (ii==0) { firstK=k; }
785                                                 k = iGeneralPlusSize-1;
786                                         } else {
787                                                 if ( k==iGeneral+size-2 )
788                                                 {
789                                                         dd = 1;
790                                                         lstRstX.push_back( (*lstOutX)[k] + dd*dx );
791                                                         lstRstY.push_back( (*lstOutY)[k] + dd*dy );
792                                                         lstRstZ.push_back( (*lstOutZ)[k] + dd*dz );
793                                                 }
794                                         }// if dist2 
795                                         dist2 = dist2+distSeg;
796                                 } // for k
797                         } //for i   
798                         
799                         
800                         if (lstRstX.size()!=size) 
801                         {
802                                 printf("EED Warnning!   ManualContourModel_Box::RedistributionPoints  >> This list is not coherent  iLstIndexOut=%d  lstRstX.size()=%d  size=%d\n",iLstIndexOut, lstRstX.size(), size);
803                                 tmpX    = lstRstX[iGeneral];
804                                 tmpY    = lstRstY[iGeneral];
805                                 tmpZ    = lstRstZ[iGeneral];
806                                 lstRstX.push_back( tmpX );
807                                 lstRstY.push_back( tmpY );
808                                 lstRstZ.push_back( tmpZ );
809                         }
810                         int iii;
811                         for (i=iGeneral; i<iGeneralPlusSize;i++)
812                         {
813 //                              ii=( (i-iGeneral) + firstK) % size ;
814                                 ii= i-iGeneral ;
815                                 iii=iGeneral+ ( (i-iGeneral) + firstK) % size ;
816                                 (*lstOutX)[iii] = lstRstX[ii];
817                                 (*lstOutY)[iii] = lstRstY[ii];
818                                 (*lstOutZ)[iii] = lstRstZ[ii];
819                         } // for i
820                 } // if size>2
821                 iGeneral=iGeneral+size;
822         }// for iLstIndexOut
823         
824 printf("EED ManualContourModel_Box::RedistributionPoints_SIN_iContour  End \n");
825         
826 }
827
828
829
830 void ManualContourModel_Box::ClockwisePoints(   std::vector<double> *lstInX,
831                                                                                         std::vector<double> *lstInY, 
832                                                                                         std::vector<double> *lstInZ,
833                                                                                         std::vector<int> *lstIndexsIn )
834 {
835         int     iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
836         int     i,iGeneral=0;
837         int     size,size2;
838         double  cx,cy,cz;
839         double  px,py,pz;
840         double  backpx,backpy,backpz;
841         double  ang;
842         char    dir=-1;
843         bool    dirx,diry,dirz;
844         int     flagAng=0;
845         float   backang;
846         double  tmp;
847
848         // For each contour
849         for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn; iLstIndexIn++)
850         {
851                 // Step 1. Find gravity center and direction
852                 size    = (*lstIndexsIn)[iLstIndexIn];
853                 if (size>2)  // for contour with more than 2 points
854                 {
855                         cx              = 0;
856                         cy              = 0;
857                         cz              = 0;
858                         dirx    = true;
859                         diry    = true;
860                         dirz    = true;
861                         for ( i=0 ; i<size ; i++ )
862                         {
863                                 px=(*lstInX)[iGeneral+i];
864                                 py=(*lstInY)[iGeneral+i];
865                                 pz=(*lstInZ)[iGeneral+i];
866                                 cx      = px + cx;
867                                 cy      = py + cy;
868                                 cz      = pz + cz;
869                                 if (i!=0) 
870                                 { 
871                                         if (backpx!=px) { dirx=false; } 
872                                         if (backpy!=py) { diry=false; } 
873                                         if (backpz!=pz) { dirz=false; } 
874                                         backpx=px;
875                                         backpy=py;
876                                         backpz=pz;
877                                 } // if i!=0
878                                 backpx=px; 
879                                 backpy=py; 
880                                 backpz=pz; 
881                         } // for i 
882                         cx=cx/size;
883                         cy=cy/size;
884                         cz=cz/size;
885                         if (dirx==true) { dir=1; }  // YZ 
886                         if (diry==true) { dir=2; }  // XZ 
887                         if (dirz==true) { dir=0; }  // XZ 
888                         // Step 2. Find angle diference find 
889                         flagAng=0;
890                         for ( i=0 ; i<size ; i++ )
891                         {
892                                 px = (*lstInX)[iGeneral+i]-cx;
893                                 py = (*lstInY)[iGeneral+i]-cy;
894                                 pz = (*lstInZ)[iGeneral+i]-cz;
895                                 if (dir==0) { ang=atan2( py , px ); } // XY
896                                 if (dir==1) { ang=atan2( pz , py ); } // YZ
897                                 if (dir==2) { ang=atan2( pz , px ); } // XZ
898                                 if (i>0) 
899                                 { 
900                                         if (backang<ang) 
901                                         {
902                                                 flagAng++;
903                                         } else {
904                                                 flagAng--;
905                                         }// if backang<ang
906                                 } // if i
907                                 backang=ang; 
908                         } // for i 
909
910
911                         // Step 3. Invert order of points
912                         if (flagAng<0)
913                         {
914                                 size2 = size/2;
915                                 for ( i=0 ; i<size2 ; i++ )
916                                 {
917                                         tmp                                                     = (*lstInX)[iGeneral+i];
918                                         (*lstInX)[iGeneral+i]                   = (*lstInX)[iGeneral+size-1-i];
919                                         (*lstInX)[iGeneral+size-1-i]    = tmp;
920                                         tmp                                                     = (*lstInY)[iGeneral+i];
921                                         (*lstInY)[iGeneral+i]                   = (*lstInY)[iGeneral+size-1-i];
922                                         (*lstInY)[iGeneral+size-1-i]    = tmp;
923                                         tmp                                                     = (*lstInZ)[iGeneral+i];
924                                         (*lstInZ)[iGeneral+i]                   = (*lstInZ)[iGeneral+size-1-i];
925                                         (*lstInZ)[iGeneral+size-1-i]    = tmp;
926                                 } // for i
927                         } // flagAng
928                 } // size>2
929                 iGeneral = iGeneral+size;
930         } // for iLstIndexIn
931 }
932
933
934 /*
935 void ManualContourModel_Box::ShiftValues(       std::vector<double> *lstInX,
936                                                                                         std::vector<double> *lstInY, 
937                                                                                         std::vector<double> *lstInZ,
938                                                                                         std::vector<int> *lstIndexsIn )
939 {
940         int iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
941         int ii, iGeneral=0;
942         int size,size2;
943         double dist,distMin;
944         int i,iBack;
945         int ig;
946         double dx,dy,dz;
947         std::vector<double> LstTmpX;
948         std::vector<double> LstTmpY;
949         std::vector<double> LstTmpZ;
950         if (sizeLstIndexIn>=2)
951         {
952         
953                 for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
954                 {
955                         size  = (*lstIndexsIn)[iLstIndexIn];
956                         size2 = (*lstIndexsIn)[iLstIndexIn+1];
957                         //find min distance and  iBack
958                         distMin = 10000000;
959                         iBack   = 0;
960                         
961                         
962                         for (ig=0; ig<size; ig++)
963                         {
964                                 for ( i=0 ; i<size2 ; i++ )
965                                 {
966                                         dx      = (*lstInX)[iGeneral+ig]-(*lstInX)[iGeneral+size+i];
967                                         dy      = (*lstInY)[iGeneral+ig]-(*lstInY)[iGeneral+size+i];
968                                         dz      = (*lstInZ)[iGeneral+ig]-(*lstInZ)[iGeneral+size+i];
969                                         dist= sqrt( dx*dx + dy*dy + dz*dz );
970                                         if ( dist<distMin ) 
971                                         {
972                                                 iBack   = i-ig;
973                                                 distMin = dist;
974                                         }
975                                 } // for i size2
976                         } // for ig size
977
978 if (iBack<0) 
979 {
980         printf("- "); 
981         iBack = iBack + size2; 
982 }
983                         
984 printf("EED ManualContourModel_Box::ShiftValues px=%f py=%f pz=%f  iBack=%d/%d  size=%d  distMin=%f \n", (*lstInX)[iGeneral]  , (*lstInY)[iGeneral] , (*lstInZ)[iGeneral], iBack, size2,size, distMin);
985                         
986                         if (iBack!=0)
987                         {
988                                 LstTmpX.clear();
989                                 LstTmpY.clear();
990                                 LstTmpZ.clear();
991                                 for (i=0 ; i<size2 ; i++) 
992                                 {
993                                         ii= (i+iBack)%size2;
994                                         LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
995                                         LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
996                                         LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
997                                 } // for i                              
998                                 for (i=0 ; i<size2 ; i++) 
999                                 {
1000                                         (*lstInX)[iGeneral+size+i] = LstTmpX[i];
1001                                         (*lstInY)[iGeneral+size+i] = LstTmpY[i];
1002                                         (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
1003                                 } // for i                              
1004                         }
1005                         iGeneral=iGeneral+size;
1006                 } // for iLstIndexIn
1007                 
1008 //------------          
1009                 
1010                 iGeneral=0;
1011
1012                 for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
1013                 {
1014                         size  = (*lstIndexsIn)[iLstIndexIn];
1015                         size2 = (*lstIndexsIn)[iLstIndexIn+1];
1016                         //find min distance and  iBack
1017                         distMin = 10000000;
1018                         iBack   = 0;
1019                         
1020                         
1021 //                      for (ig=0; ig<size; ig++)
1022 //                      {
1023                                 for ( i=0 ; i<size2 ; i++ )
1024                                 {
1025                                         dx      = (*lstInX)[iGeneral]-(*lstInX)[iGeneral+size+i];
1026                                         dy      = (*lstInY)[iGeneral]-(*lstInY)[iGeneral+size+i];
1027                                         dz      = (*lstInZ)[iGeneral]-(*lstInZ)[iGeneral+size+i];
1028                                         dist= sqrt( dx*dx + dy*dy + dz*dz );
1029                                         if ( dist<distMin ) 
1030                                         {
1031                                                 iBack   = i;
1032                                                 distMin = dist;
1033                                         }
1034                                 } // for i size2
1035 //                      } // for ig size
1036
1037                         
1038 printf("EED ManualContourModel_Box::ShiftValues px=%f py=%f pz=%f  iBack=%d/%d  size=%d  distMin=%f \n", (*lstInX)[iGeneral]  , (*lstInY)[iGeneral] , (*lstInZ)[iGeneral], iBack, size2,size, distMin);
1039                         
1040                         if (iBack!=0)
1041                         {
1042                                 LstTmpX.clear();
1043                                 LstTmpY.clear();
1044                                 LstTmpZ.clear();
1045                                 for (i=0 ; i<size2 ; i++) 
1046                                 {
1047                                         ii= (i+iBack)%size2;
1048                                         LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
1049                                         LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
1050                                         LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
1051                                 } // for i                              
1052                                 for (i=0 ; i<size2 ; i++) 
1053                                 {
1054                                         (*lstInX)[iGeneral+size+i] = LstTmpX[i];
1055                                         (*lstInY)[iGeneral+size+i] = LstTmpY[i];
1056                                         (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
1057                                 } // for i                              
1058                         }
1059                         iGeneral=iGeneral+size;
1060                 } // for iLstIndexIn
1061                 
1062         } // if sizeLstIndexIn
1063         
1064 }
1065 */
1066
1067
1068
1069 void ManualContourModel_Box::ShiftValues(       std::vector<double> *lstInX,
1070                                                                                         std::vector<double> *lstInY, 
1071                                                                                         std::vector<double> *lstInZ,
1072                                                                                         std::vector<int> *lstIndexsIn )
1073 {
1074         int iLstIndexIn,sizeLstIndexIn=lstIndexsIn->size();
1075         int ii, iGeneral=0;
1076         int size,size2;
1077         double dist,distMin;
1078         int i,iBack;
1079         int ig;
1080         double dx,dy,dz;
1081         std::vector<double> LstTmpX;
1082         std::vector<double> LstTmpY;
1083         std::vector<double> LstTmpZ;
1084         if (sizeLstIndexIn>=2)
1085         {
1086         
1087           
1088          
1089                 for (iLstIndexIn=0;  iLstIndexIn<sizeLstIndexIn-1; iLstIndexIn++)
1090                 {
1091                         size  = (*lstIndexsIn)[iLstIndexIn];
1092                         size2 = (*lstIndexsIn)[iLstIndexIn+1];
1093                         //find min distance and  iBack
1094                         distMin = 10000000;
1095                         iBack   = 0;
1096                         
1097                     // Comparing distance between two contours  
1098                     // Both contours need the same size 
1099                         for (ig=0; ig<size; ig++)
1100                         {
1101                                 dist=0;
1102                                 for ( i=0 ; i<size2 ; i++ )
1103                                 {
1104                                         dx      = (*lstInX)[iGeneral+i]-(*lstInX)[iGeneral+size+(i+ig)%size];
1105                                         dy      = (*lstInY)[iGeneral+i]-(*lstInY)[iGeneral+size+(i+ig)%size];
1106                                         dz      = (*lstInZ)[iGeneral+i]-(*lstInZ)[iGeneral+size+(i+ig)%size];
1107                                         dist= dist + sqrt( dx*dx + dy*dy + dz*dz );
1108                                 } // for i size2
1109
1110                                 if ( dist<distMin ) 
1111                                 {
1112                                         iBack   = ig+size;
1113                                         distMin = dist;
1114                                 }
1115                                 
1116                         } // for ig size
1117
1118                         
1119 //printf("EED ManualContourModel_Box::ShiftValues px=%f py=%f pz=%f  iBack=%d/%d  size=%d  distMin=%f \n", (*lstInX)[iGeneral]  , (*lstInY)[iGeneral] , (*lstInZ)[iGeneral], iBack, size2,size, distMin);
1120                         
1121                         if (iBack!=0)
1122                         {
1123                                 LstTmpX.clear();
1124                                 LstTmpY.clear();
1125                                 LstTmpZ.clear();
1126                                 for (i=0 ; i<size2 ; i++) 
1127                                 {
1128                                         ii = (i+iBack)%size2;
1129                                         if (ii<(size2-1)) // Skip the last item
1130                                         {
1131                                                 LstTmpX.push_back( (*lstInX)[iGeneral+size+ii] );
1132                                                 LstTmpY.push_back( (*lstInY)[iGeneral+size+ii] );
1133                                                 LstTmpZ.push_back( (*lstInZ)[iGeneral+size+ii] );
1134                                         }
1135                                 } // for i
1136                                 
1137                                 //Repeat the first item at the end to close the contour
1138                                 LstTmpX.push_back( LstTmpX[0] );
1139                                 LstTmpY.push_back( LstTmpY[0] );
1140                                 LstTmpZ.push_back( LstTmpZ[0] );
1141
1142                                 for (i=0 ; i<size2 ; i++) 
1143                                 {
1144                                         (*lstInX)[iGeneral+size+i] = LstTmpX[i];
1145                                         (*lstInY)[iGeneral+size+i] = LstTmpY[i];
1146                                         (*lstInZ)[iGeneral+size+i] = LstTmpZ[i];
1147                                 } // for i                              
1148                         }
1149                         iGeneral=iGeneral+size;
1150                 } // for iLstIndexIn
1151                 
1152                 
1153         } // if sizeLstIndexIn
1154         
1155 }
1156
1157
1158
1159
1160
1161
1162 void ManualContourModel_Box::Process()
1163 {
1164 // THE MAIN PROCESSING METHOD BODY
1165 //   Here we simply set the input 'In' value to the output 'Out'
1166 //   And print out the output value
1167 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
1168 //    void bbSet{Input|Output}NAME(const TYPE&)
1169 //    const TYPE& bbGet{Input|Output}NAME() const 
1170 //    Where :
1171 //    * NAME is the name of the input/output
1172 //      (the one provided in the attribute 'name' of the tag 'input')
1173 //    * TYPE is the C++ type of the input/output
1174 //      (the one provided in the attribute 'type' of the tag 'input')
1175
1176 //    bbSetOutputOut( bbGetInputIn() );
1177 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
1178
1179         // First Step  Spline Interpolation
1180         std::vector<double> lstInX=bbGetInputLstControlPointsX();
1181         std::vector<double> lstInY=bbGetInputLstControlPointsY();
1182         std::vector<double> lstInZ=bbGetInputLstControlPointsZ();
1183         if ( (lstInX.size()!=lstInY.size()) || (lstInY.size()!=lstInZ.size()) ) 
1184         { 
1185                 printf("Warnning !!  .. ManualContourModel_Box: The list X Y Z, no have the same number of elements \n");
1186                 return;
1187         }
1188         std::vector<int>        lstIndexsIn=bbGetInputLstIndexsIn();
1189         std::vector<int>        lstIndexsOut;
1190         std::vector<double> lstOutX;
1191         std::vector<double> lstOutY;
1192         std::vector<double> lstOutZ;
1193         if (bbGetInputLstIndexsIn().size()==0)
1194         {
1195                 lstIndexsIn.push_back( lstInX.size() );
1196         }
1197         
1198 // Step 1.  All contours the same clockwise direction (Control Points)
1199         if (bbGetInputDoubleContour()==1)
1200         {
1201                 ClockwisePoints( &lstInX , &lstInY , &lstInZ , &lstIndexsIn );
1202 //              ShiftValues( &lstInX , &lstInY , &lstInZ , &lstIndexsIn );
1203         } // DoubleContour
1204         int i,size=lstIndexsIn.size();
1205         int iGeneral=0;
1206         
1207 // Step 2.  Spline interpolation of control points      
1208         for (i=0;i<size;i++)
1209         {
1210                 ProcessBySegment(       bbGetInputType() , 
1211                                                         iGeneral,  lstIndexsIn[i] ,
1212                                                         &lstInX ,  &lstInY  , &lstInZ,
1213                                                         &lstOutX , &lstOutY , &lstOutZ,
1214                                                         &lstIndexsOut,bbGetInputOpenClose() );
1215         } // for
1216
1217         if (bbGetInputDoubleContour()==0)
1218         {
1219 // Finish if Simple contours
1220                 //////////////////// Set Out   DoubleContour = 0
1221                 bbSetOutputLstContourPointsX(lstOutX);  
1222                 bbSetOutputLstContourPointsY(lstOutY);  
1223                 bbSetOutputLstContourPointsZ(lstOutZ);
1224                 bbSetOutputLstIndexsOut(lstIndexsOut);  
1225         } else {
1226 // Step 3. Interpolation in the other direction
1227 // Step 3.1 Linear Normalice points around contours     
1228                 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
1229 //EED 01/2021           
1230 // Step 3.2 Shift points to find minimun acumulate distance     
1231                 ShiftValues( &lstOutX, &lstOutY, &lstOutZ, &lstIndexsOut );
1232 // Step 3.3. SIN Normalice points around contours       
1233 //              RedistributionPoints_SIN_iContour(-1, &lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut,-1,-1);
1234
1235 if (bbGetInputParam()[0]==1)
1236 {
1237                 RedistributionPointsAllContours_SIN( &lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
1238 }
1239 // Step 3.4 Transpose the vectors   
1240                 lstInX.clear();
1241                 lstInY.clear();
1242                 lstInZ.clear();
1243                 lstIndexsIn.clear();
1244                 size  = bbGetInputNbPoints();
1245                 int j,size2 = lstIndexsOut.size();
1246                 for (i=0;i<size;i++)
1247                 {
1248                         for (j=0;j<size2;j++)
1249                         {
1250                                 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
1251                                 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
1252                                 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
1253                         } // for j
1254                         lstIndexsIn.push_back( size2 );
1255                 } // for i
1256                 lstOutX.clear();
1257                 lstOutY.clear();
1258                 lstOutZ.clear();
1259                 lstIndexsOut.clear();
1260 // Step 3.5 Interponation 2
1261                 size=lstIndexsIn.size();
1262                 iGeneral=0;
1263                 for (i=0;i<size;i++)
1264                 {
1265                         ProcessBySegment(       bbGetInputType() , 
1266                                                                 iGeneral, lstIndexsIn[i] ,
1267                                                                 &lstInX,&lstInY,&lstInZ,
1268                                                                 &lstOutX,&lstOutY,&lstOutZ,
1269                                                                 &lstIndexsOut,bbGetInputOpenClose2());
1270                 } // for
1271                 RedistributionPoints(&lstOutX,&lstOutY,&lstOutZ,&lstIndexsOut);
1272 // Step 3.6 Transpose the vectors   
1273                 lstInX.clear();
1274                 lstInY.clear();
1275                 lstInZ.clear();
1276                 lstIndexsIn.clear();
1277                 size  = bbGetInputNbPoints();
1278                 size2 = lstIndexsOut.size();
1279                 for (i=0;i<size;i++)
1280                 {
1281                         for (j=0;j<size2;j++)
1282                         {
1283                                 lstInX.push_back( lstOutX[ j*lstIndexsOut[j] + i ] );
1284                                 lstInY.push_back( lstOutY[ j*lstIndexsOut[j] + i ] );
1285                                 lstInZ.push_back( lstOutZ[ j*lstIndexsOut[j] + i ] );
1286                         } // for j
1287                         lstIndexsIn.push_back( size2 );
1288                 } // for i
1289                 lstOutX.clear();
1290                 lstOutY.clear();
1291                 lstOutZ.clear();
1292                 lstIndexsOut.clear();
1293                 //////////////////// Set Out   DoubleContour = 1
1294                 bbSetOutputLstContourPointsX(lstInX);   
1295                 bbSetOutputLstContourPointsY(lstInY);   
1296                 bbSetOutputLstContourPointsZ(lstInZ);
1297                 bbSetOutputLstIndexsOut(lstIndexsIn);   
1298
1299         } // if DoubleContour 
1300
1301 }
1302 //===== 
1303 // 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)
1304 //===== 
1305 void ManualContourModel_Box::bbUserSetDefaultValues()
1306 {
1307
1308 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
1309 //    Here we initialize the input 'In' to 0
1310    bbSetInputType(1);
1311    bbSetInputDoubleContour(0);
1312    bbSetInputOpenClose(false);
1313    bbSetInputOpenClose2(false);
1314    bbSetInputNbPoints(100);
1315   
1316 }
1317 //===== 
1318 // 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)
1319 //===== 
1320 void ManualContourModel_Box::bbUserInitializeProcessing()
1321 {
1322
1323 //  THE INITIALIZATION METHOD BODY :
1324 //    Here does nothing 
1325 //    but this is where you should allocate the internal/output pointers 
1326 //    if any 
1327
1328   
1329 }
1330 //===== 
1331 // 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)
1332 //===== 
1333 void ManualContourModel_Box::bbUserFinalizeProcessing()
1334 {
1335
1336 //  THE FINALIZATION METHOD BODY :
1337 //    Here does nothing 
1338 //    but this is where you should desallocate the internal/output pointers 
1339 //    if any
1340   
1341 }
1342 }
1343 // EO namespace bbcreaMaracasVisu
1344
1345