]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/Contour/AutoControlPoints.cxx
creaMaracasVisu Library
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / Contour / AutoControlPoints.cxx
1 #include "AutoControlPoints.h"
2 //------------------------------------------------------------------------------------------------------------------------------------------
3 //CLASS: AutoControlPoints -----------------------------------------------------------------------------------------------------------------
4 //------------------------------------------------------------------------------------------------------------------------------------------
5 //Constructor
6 AutoControlPoints::AutoControlPoints()
7 {
8          _pathsize = 0;
9          _numspline = 100;
10 }
11 //Destructor
12 AutoControlPoints::~AutoControlPoints()
13 {
14 }
15 //------------------------------------------------------------------------------------------------------------------------------------------
16 int AutoControlPoints::GetSizeVector(std::vector<double>*Vector)
17    {
18            if(Vector != NULL)
19            {
20                         return _SizeVectorIn = Vector->size();
21            }
22            else
23            {
24                         return _SizeVectorIn = -1;
25            }
26    }
27 //------------------------------------------------------------------------------------------------------------------------------------------
28 void AutoControlPoints::PointLeft ( std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ,
29                                                                 double* lex, double* ley, double* lez )
30 {
31         double LeftX = 1000;
32         int pos = 0;
33         int size = GetSizeVector(InX);
34         if(size != -1)
35         {
36                 for(int i=0; i< size; i++)
37                 {
38                         if(  (*InX)[i] < LeftX  )
39                         {
40                                 LeftX = (*InX)[i];
41                                 pos = i;
42                         }
43                 }
44                 *lex = (*InX)[pos];
45                 *ley = (*InY)[pos];
46                 *lez = (*InZ)[pos];
47         }
48 }
49 //------------------------------------------------------------------------------------------------------------------------------------------
50 void AutoControlPoints::PointRight( std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ,
51                                                                 double* rix, double* riy, double* riz )
52 {
53           double RightX = 0;
54           int pos = 0;
55           int size = GetSizeVector(InX);
56           if(size != -1)
57           {
58                 for(int i=0; i< size; i++)
59                 {
60                   if(  (*InX)[i] > RightX  )
61                   {
62                           RightX = (*InX)[i];
63                           pos = i;      
64                   }
65                 }
66                 *rix = (*InX)[pos];
67                 *riy = (*InY)[pos];
68                 *riz = (*InZ)[pos];
69           }
70 }
71 //------------------------------------------------------------------------------------------------------------------------------------------
72 void AutoControlPoints::PointHigh ( std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ,
73                                                                 double* hix, double* hiy, double* hiz )
74 {
75         double HighY=-1;
76         int pos = 0;
77         int size = InX->size();
78         if(size > 0){
79                 HighY = (*InY)[0];
80                 for(int i = 1; i < size; i++){
81                         if((*InY)[i] < HighY){
82                                 HighY = (*InY)[i];
83                                 pos = i;
84                         }
85                 }
86                 *hix = (*InX)[pos];
87                 *hiy = (*InY)[pos];
88                 *hiz = (*InZ)[pos];
89         }
90         
91 /*JCP 29-09-08
92         int size = GetSizeVector(InX);
93         if(size != -1)
94         {
95                 for(int i=0; i< _SizeVectorIn; i++)
96                 {
97                         if(  (*InY)[i] < HighY  )
98                         {
99                                 HighY = (*InY)[i];
100                                 pos = i;
101                         }
102                 }
103                 *hix = (*InX)[pos];
104                 *hiy = (*InY)[pos];
105                 *hiz = (*InZ)[pos];
106         }
107 JCP 29-09-08*/
108         
109 }
110
111 //------------------------------------------------------------------------------------------------------------------------------------------
112 void AutoControlPoints::PointLow (      std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ,
113                                                                         double *lox, double *loy, double *loz)
114 {
115         double LowY = 0;
116         int pos = 0;
117         int size = GetSizeVector(InX);
118         if(size != 0)
119         {
120                 for(int i=0; i< _SizeVectorIn; i++)
121                 {
122                         if(  (*InY)[i] > LowY  )
123                         {
124                                 LowY = (*InY)[i];
125                                 pos = i;
126                         }
127                 } 
128                 *lox = (*InX)[pos];
129                 *loy = (*InY)[pos];
130                 *loz = (*InZ)[pos];
131         }
132 }
133 //------------------------------------------------------------------------------------------------------------------------------------------
134 void AutoControlPoints::TwoPoints ( std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ )
135 {
136         double hiX=0,hiY=0,hiZ=0;
137         PointHigh ( InX,InY,InZ,&hiX,&hiY,&hiZ );
138         
139         double leX=0,leY=0,leZ=0;
140         PointLeft ( InX,InY,InZ,&leX,&leY,&leZ );
141         
142         double loX=0,loY=0,loZ=0;
143         PointLow  ( InX,InY,InZ,&loX,&loY,&loZ );
144
145         double riX=0,riY=0,riZ=0;
146         PointRight( InX,InY,InZ,&riX,&riY,&riZ );
147
148         double distHiLo = sqrt( pow(hiX-loX,2) + pow(hiY-loY,2) );
149         double distRiLe = sqrt( pow(riX-leX,2) + pow(riY-leY,2) );
150                 
151         _controlpointsX.clear();
152         _controlpointsY.clear();
153         _controlpointsZ.clear();
154         if(distHiLo >= distRiLe)
155         {
156                 _controlpointsX.push_back(hiX);
157                 _controlpointsY.push_back(hiY);
158                 _controlpointsZ.push_back(hiZ);
159                         
160                 _controlpointsX.push_back(loX);
161                 _controlpointsY.push_back(loY);
162                 _controlpointsZ.push_back(loZ);
163         }
164         else
165         {
166                 _controlpointsX.push_back(riX);
167                 _controlpointsY.push_back(riY);
168                 _controlpointsZ.push_back(riZ);
169
170                 _controlpointsX.push_back(leX);
171                 _controlpointsY.push_back(leY);
172                 _controlpointsZ.push_back(leZ);
173         }
174 }
175 //------------------------------------------------------------------------------------------------------------------------------------------
176 void AutoControlPoints::CircleCenter(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ, double *cx, double *cy, double *r)
177 {
178         double hiX=0,hiY=0,hiZ=0;
179         PointHigh ( InX,InY,InZ,&hiX,&hiY,&hiZ );
180
181         double leX=0,leY=0,leZ=0;
182         PointLeft ( InX,InY,InZ,&leX,&leY,&leZ );
183                 
184         double loX=0,loY=0,loZ=0;
185         PointLow  ( InX,InY,InZ,&loX,&loY,&loZ );
186
187         double riX=0,riY=0,riZ=0;
188         PointRight( InX,InY,InZ,&riX,&riY,&riZ );
189
190         *cx = (riX+leX)/2;
191         *cy = (hiY+loY)/2;
192         *r = sqrt(pow(leX-*cx,2)+pow(hiY-*cy,2)) + 7;
193 }
194 //------------------------------------------------------------------------------------------------------------------------------------------
195 void AutoControlPoints::CirclePoints(double cx, double cy, double r, double grad, double *x, double *y)
196 {
197         double alpha = (2*3.14159265*grad)/360;
198
199         *x = cx + (r*cos(alpha));
200         *y = cy + (r*sin(alpha));
201 }
202 //------------------------------------------------------------------------------------------------------------------------------------------
203  void AutoControlPoints::ChargeSpline( )
204  {
205          int size = _controlpointsX.size();
206          if(size != 0)
207          {
208                 _mContourModel = new manualContourModel();
209                 if( _controlpointsX.size() == 2 )
210                 {
211                         _mContourModel->SetCloseContour(false);
212                 }
213                 if( _controlpointsX.size() > 2 )
214                 {
215                         _mContourModel->SetCloseContour(true);
216                 }
217
218                 _mContourModel->DeleteAllPoints();
219                 _mContourModel->SetNumberOfPointsSpline(_numspline);
220                 for(int i=0; i<size; i++)
221                 {
222                         _mContourModel->AddPoint(_controlpointsX[i],_controlpointsY[i],_controlpointsZ[i]);
223                 }
224                 _mContourModel->UpdateSpline();
225                 int numspline = _mContourModel->GetNumberOfPointsSpline();
226                 double x,y,z;
227                 _chargecontrolpointsX.clear();
228                 _chargecontrolpointsY.clear();
229                 _chargecontrolpointsZ.clear();
230                 for(int j=0; j<numspline; j++)
231                 {
232                         _mContourModel->GetSpline_i_Point(j,&x,&y,&z);
233                         _chargecontrolpointsX.push_back(x);
234                         _chargecontrolpointsY.push_back(y);
235                         _chargecontrolpointsZ.push_back(z);
236                 }
237                 _pathsize = _mContourModel->GetPathSize();
238 //printf("\nPATH SIZE = %f",_pathsize);
239
240                  std::ofstream file1;
241                 file1.open( "4_SplinePoints.txt" );
242
243                 for(int i = 0; i < numspline; i++){
244                         file1<<"X= "<<_chargecontrolpointsX[i] << "\tY= "<<_chargecontrolpointsY[i] << "\tZ= "<<_chargecontrolpointsZ[i]<<std::endl;
245                 }
246                 file1.close();
247          }
248         
249  }
250  //Given the coordinates of two points, it calculates the slope
251 double AutoControlPoints::Slope(double x0, double y0, double x1, double y1)
252 {
253         double m = (y1-y0)/(x1-x0);
254         return m;
255 }
256 //----------------------------------------------------------------------------------------------------------------------------------------
257 //Given the coordinates of two points, it calculates the normal and it's slope 
258 double AutoControlPoints::Normal(double x0, double y0, double* m, double xi)
259 {
260         double y;
261         *m = -(1/(*m));
262         y = ((*m)*(xi - x0)) + y0;
263         return y;
264 }
265 //----------------------------------------------------------------------------------------------------------------------------------------
266 void AutoControlPoints::Intersection(double x01, double y01, double x02, double y02, double mn, double m2, double* x, double* y)
267 {
268         *x = ( y02-y01-(m2*x02)+(mn*x01) )/(mn-m2);
269         *y = m2*(*x-x02)+y02;
270 }
271 //-----------------------------------------------------------------------------------------------------------------------------------------
272 /**
273 **This methods finds the points where each radius of the circle intersect the contour
274 **/
275 void AutoControlPoints::InterCircle(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ)
276 {
277         double cx,cy,r;
278         CircleCenter(InX,InY,InZ,&cx,&cy,&r);
279
280         //GENERATE THE CIRCLE'S POINTS
281         int i;
282         double grad,x,y,n;
283         std::vector<double>tempX;
284         std::vector<double>tempY;
285         tempX.clear();
286         tempY.clear();
287         n = 1;
288         grad = 0;
289         for(i=0; i<360/n; i++)
290         {
291                 CirclePoints(cx,cy,r,grad,&x,&y);
292                 tempX.push_back(x);
293                 tempY.push_back(y);
294                 grad = grad + n;
295         }
296
297         //FIND THE INTERSECTIONS BETWEEN THE CIRCLE AND THE CONTOUR
298         int j,jj;
299         bool interRad = false;
300         double m1,mn=0,m2,xinter,yinter,xmin,ymin,min,dist;
301         _intercircleX.clear();
302         _intercircleY.clear();
303         _intercircleDist.clear();
304         _interbewteencircleX.clear();
305         _interbewteencircleY.clear();
306         _interbewteencircleDist.clear();
307         _interbewteencirclePos.clear();
308
309 //EED 22 Sep 2008
310 //      FILE *fd, *fexp;
311 //      fd = fopen("C:/bbtk_JS/data/tempCircle.txt","w");
312 //      fexp = fopen("C:/bbtk_JS/data/InterCircle.txt","w");
313 //      fprintf(fexp,"\npos     min             xmin    ymin    xcir    ycir");
314
315         //std::ofstream file1;
316     //file1.open( "Temp.txt" ); 
317         
318         for(i=0; i<tempX.size(); i++)
319         {
320 //              fprintf(fd,"\n Para X = %f, Y = %f",tempX[i],tempY[i]);
321                 _circleX.push_back(tempX[i]);
322                 _circleY.push_back(tempY[i]);
323                 m1 = Slope(tempX[i],tempY[i],cx,cy);//slope of the radius
324                 min = 9999;
325                 for(j=0; j<InX->size(); j++)
326                 {
327                         jj = (j+1)%(InX->size());
328                         m2 = Slope((*InX)[j],(*InY)[j],(*InX)[jj],(*InY)[jj]);//Slope of the vector between the adjacent points
329                         Intersection(tempX[i],tempY[i],(*InX)[j],(*InY)[j],m1,m2,&xinter,&yinter);
330
331 //JCP 26-09-2008
332                         //If the point of intersection is between two points of the contour
333                         if( ((xinter>=(*InX)[j]) && (xinter<=(*InX)[jj]))||((xinter<=(*InX)[j]) && (xinter>=(*InX)[jj]) ))
334                         {
335                                 dist = sqrt(pow(tempX[i]-xinter,2) + pow(tempY[i]-yinter,2));
336                                 if(dist<min)
337                                 {
338                                         min = dist;
339                                         xmin = xinter;
340                                         ymin = yinter;
341                                 }
342                         }
343 //JCP 26-09-2008
344 /*JCP 26-09-2008
345                         if((*InX)[j]<=(*InX)[jj])
346                         {
347                                 if( (xinter>=(*InX)[j]) && (xinter<=(*InX)[jj]) )               //Intersection entre le cercle et le contour
348                                 {
349                                         dist = sqrt(pow(tempX[i]-xinter,2) + pow(tempY[i]-yinter,2));
350                                         if(dist<min)
351                                         {
352                                                 min = dist;
353                                                 xmin = xinter;
354                                                 ymin = yinter;
355                                         }
356                                 }
357                         }
358                         if((*InX)[j]>(*InX)[jj])
359                         {
360                                 if( (xinter<=(*InX)[j]) && (xinter>=(*InX)[jj]) )               //Intersection entre le cercle et le contour
361                                 {
362                                         dist = sqrt(pow(tempX[i]-xinter,2) + pow(tempY[i]-yinter,2));
363                                         if(dist<min)
364                                         {
365                                                 min = dist;
366                                                 xmin = xinter;
367                                                 ymin = yinter;
368                                         }
369                                 }
370                         }
371 JCP 26-09-2008*/
372                 }
373
374
375
376 //              fprintf(fd,"\n => x_int = %f, y_int = %f, dist_int = %f",xmin,ymin,min);
377 //              fprintf(fexp,"\n%d      %f      %f      %f      %f      %f",i,min,xmin,ymin,tempX[i],tempY[i]);
378 //JCP 26-09-08 If the distance of the intersection is bigger than the radio we have to invert the segment
379                 if(min>=r)
380                 {
381                         interRad = true;
382                         _interbewteencirclePos.push_back(i);
383                         _interbewteencircleX.push_back(xmin);
384                         _interbewteencircleY.push_back(ymin);
385                         _interbewteencircleDist.push_back( sqrt(pow(cx-xmin,2)+pow(cy-ymin,2)) );
386
387                         //      file1<<i<<std::endl;
388                 }else{
389 //JCP 26-09-08          if(min<r)
390 //JCP 26-09-08          {
391                         _intercircleX.push_back(xmin);
392                         _intercircleY.push_back(ymin);
393                         _intercircleDist.push_back(min);
394                 //      file1<<"\t"<<i<<std::endl;
395                 }
396         }
397         //file1.close();
398 //      fclose(fd);
399 //      fclose(fexp);
400
401         //WHEN THERE IS RADIAL INTERSECTION
402         vectorFunctions *vecf = new vectorFunctions();
403
404 //EED 22 Sep 2008
405 //      FILE *fdata;
406 //      fdata = fopen("C:/bbtk_JS/data/autoCPdata.txt","w");
407         if(interRad == true)
408         {
409                 std::vector<double> tempXX;
410                 std::vector<double> tempYY;
411                 std::vector<double> tempDD;
412                 tempXX.clear();
413                 tempYY.clear();
414                 tempDD.clear();
415 //Copy of the first points in the array until the first intersection is found           
416                 for(i=0; i<_interbewteencirclePos[0]; i++)
417                 {
418                         tempXX.push_back(_intercircleX[i]);
419                         tempYY.push_back(_intercircleY[i]);
420                         tempDD.push_back(_intercircleDist[i]);
421 //                      fprintf(fdata,"\n%f     %f      %f",_intercircleDist[i],_intercircleX[i],_intercircleY[i]);
422                 }
423                 int sizep = _interbewteencirclePos.size();
424 //Copy all the points where there is an intersection with the center but inverted
425 //JCP 26-09-08          for(i=_interbewteencirclePos[sizep-1],j=sizep-1; i>=_interbewteencirclePos[0]; i--,j--)
426                 for(i=sizep-1; i >= 0;i--)
427                 {
428 //JCP 26-09-08                  tempXX.push_back(_interbewteencircleX[j]);
429 //JCP 26-09-08                  tempYY.push_back(_interbewteencircleY[j]);
430 //JCP 26-09-08                  tempDD.push_back(_interbewteencircleDist[j]);
431                         tempXX.push_back(_interbewteencircleX[i]);
432                         tempYY.push_back(_interbewteencircleY[i]);
433                         tempDD.push_back(_interbewteencircleDist[i]);
434 //                      fprintf(fdata,"\n%f     %f      %f",_interbewteencircleDist[j],_interbewteencircleX[j],_interbewteencircleY[j]);
435                 }
436                 for(i=_interbewteencirclePos[0]; i<_intercircleX.size(); i++)
437                 {
438                         tempXX.push_back(_intercircleX[i]);
439                         tempYY.push_back(_intercircleY[i]);
440                         tempDD.push_back(_intercircleDist[i]);
441 //                      fprintf(fdata,"\n%f     %f      %f",_intercircleDist[i],_intercircleX[i],_intercircleY[i]);
442                 }
443                 
444                 _intercircleX.clear();
445                 _intercircleY.clear();
446                 _intercircleDist.clear();
447                 vecf->copyVector(&tempXX,&_intercircleX);
448                 vecf->copyVector(&tempYY,&_intercircleY);
449                 vecf->copyVector(&tempDD,&_intercircleDist);
450         }       
451 //      fclose(fdata);
452
453         //DELETE!!
454
455         std::ofstream file1;
456     file1.open( "1_Intersection.txt" );
457
458         for(int i = 0; i < _intercircleX.size(); i++){
459                 file1<<"X= "<<_intercircleX[i] << "\tY= "<<_intercircleY[i] << "\tDist= "<<_intercircleDist[i]<<std::endl;
460         }
461         file1.close();
462                 
463
464         delete vecf;
465 }
466 //-----------------------------------------------------------------------------------------------------------------------------------------
467 void AutoControlPoints::maxminLocal()
468 {
469         int i;
470         _posmaxlocal.clear();
471         _posminlocal.clear();
472         _posminmaxlocal.clear();
473         _maxlocalX.clear();
474         _maxlocalY.clear();
475         _minlocalX.clear();
476         _minlocalY.clear();
477         _minmaxlocalX.clear();
478         _minmaxlocalY.clear();
479         
480         if(_intercircleDist.size() != 0)
481         {
482 //JCP 26 - 09 - 08 This change was du to the posibility of having a maximum or a minimum value in the limits of 
483 //JCP 26 - 09 - 08 the array
484                 double lastdist, currentdist, nextdist;
485                 for(i=0; i < _intercircleDist.size(); i++)
486                 {
487                         //FOR MAXIMUM LOCAL
488 //JCP 26-09-08
489                         currentdist = _intercircleDist[i];
490                         if(i == 0){
491                                 lastdist = _intercircleDist[_intercircleDist.size()-1];
492                                 nextdist = _intercircleDist[i+1];
493                         }else if (i == _intercircleDist.size()-1){
494                                 lastdist = _intercircleDist[i-1];
495                                 nextdist = _intercircleDist[0];
496                         }else{
497                                 lastdist = _intercircleDist[i-1];                               
498                                 nextdist = _intercircleDist[i+1];
499                         }
500                         
501
502 //JCP 26-09-08                  if( (_intercircleDist[i-1]<_intercircleDist[i]) && (_intercircleDist[i]>_intercircleDist[i+1]))
503                         if(lastdist < currentdist && currentdist > nextdist)
504                         {
505                                 _posmaxlocal.push_back(i);
506                                 _maxlocalX.push_back(_intercircleX[i]);
507                                 _maxlocalY.push_back(_intercircleY[i]);
508                                 _minmaxlocalX.push_back(_intercircleX[i]);
509                                 _minmaxlocalY.push_back(_intercircleY[i]);
510                                 _posminmaxlocal.push_back(i);
511                         }
512                         //FOR MINIMUM LOCAL
513 //JCP 26-09-08                  if( (_intercircleDist[i-1]>_intercircleDist[i]) && (_intercircleDist[i]<_intercircleDist[i+1]))
514                         if(lastdist > currentdist && currentdist < nextdist)
515                         {
516                                 _posminlocal.push_back(i);
517                                 _minlocalX.push_back(_intercircleX[i]);
518                                 _minlocalY.push_back(_intercircleY[i]);
519                                 _minmaxlocalX.push_back(_intercircleX[i]);
520                                 _minmaxlocalY.push_back(_intercircleY[i]);
521                                 _posminmaxlocal.push_back(i);
522                         }
523                 }
524         }
525
526         vectorFunctions *vecf   = new vectorFunctions();
527         std::vector<double> tempZ;
528         tempZ.clear();
529         
530         vecf->copyVector(&_minlocalX,&_controlpointsX); 
531         vecf->copyVector(&_minlocalY,&_controlpointsY);
532         for(i=0; i<_minlocalX.size(); i++)
533         {
534                 tempZ.push_back(_controlpointsZ[0]);
535         }
536         vecf->copyVector(&tempZ,&_controlpointsZ);
537
538 //JCP 26-09-08  
539         std::ofstream file1;
540     file1.open( "2_MaxMin.txt" );
541
542         for(int i = 0; i < _controlpointsX.size(); i++){
543                 file1<<"X= "<<_controlpointsX[i] << "\tY= "<<_controlpointsY[i] << "\tZ= "<<_controlpointsZ[i]<<std::endl;
544         }
545         file1.close();
546 //JCP 26-09-08
547         delete vecf;
548 }
549 //-----------------------------------------------------------------------------------------------------------------------------------------
550 //ELIMINATES THE POINTS WITH A DISTANCE < val
551 void AutoControlPoints::fixBetweenPoints(double val)
552 {
553         int size = _controlpointsX.size();
554         double dist;
555         if(size != 0)
556         {
557                 std::vector<double> tempX;
558                 std::vector<double> tempY;
559                 std::vector<double>     tempZ;
560                 tempX.clear();
561                 tempY.clear();
562                 tempZ.clear();
563
564                 int ii;
565                 vectorFunctions *vecf = new vectorFunctions();
566                 for(int i=0; i<size; i++)
567                 {
568                         ii = (i+1)%size;
569                         dist = sqrt(pow(_controlpointsX[i]-_controlpointsX[ii],2)+pow(_controlpointsY[i]-_controlpointsY[ii],2));
570                         if(dist>val)
571                         {
572                                 tempX.push_back(_controlpointsX[i]);
573                                 tempY.push_back(_controlpointsY[i]);
574                                 tempZ.push_back(_controlpointsZ[i]);
575                         }
576                 }
577                 _controlpointsX.clear();
578                 _controlpointsY.clear();
579                 _controlpointsZ.clear();
580
581                 vecf->copyVector(&tempX,&_controlpointsX);
582                 vecf->copyVector(&tempY,&_controlpointsY);
583                 vecf->copyVector(&tempZ,&_controlpointsZ);
584
585                 std::ofstream file1;
586                 file1.open( "3_PointsFixed.txt" );
587
588                 for(int i = 0; i < _controlpointsX.size(); i++){
589                         file1<<"X= "<<_controlpointsX[i] << "\tY= "<<_controlpointsY[i] << "\tZ= "<<_controlpointsZ[i]<<std::endl;
590                 }
591                 file1.close();
592
593                 delete vecf;
594         }
595 }
596 //-----------------------------------------------------------------------------------------------------------------------------------------
597 //ALL THE INTERSECTIONS
598 void AutoControlPoints::InterBetweenContours(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ)
599 {
600         _intervectorX.clear();
601         _intervectorY.clear();
602         ChargeSpline();
603
604         int i,ii,j,jj;
605         double m1,mn,m2,xinter,yinter;
606         if(_chargecontrolpointsX.size() > 1) //These condition exists because there is a method for find the initial control points 
607         {
608 //EED 22 Sep 2008
609 //              FILE *fd;
610 //              fd = fopen("C:/bbtk_JS/data/interBetweenContours.txt","w");
611                 for(i=0; i<_chargecontrolpointsX.size(); i++)
612                 {
613                         ii = (i+1)%(_chargecontrolpointsX.size());
614
615                         m1 = Slope(_chargecontrolpointsX[i],_chargecontrolpointsY[i],_chargecontrolpointsX[ii],_chargecontrolpointsY[ii]);
616                         mn = m1;
617                         Normal(_chargecontrolpointsX[i],_chargecontrolpointsY[i],&mn,_chargecontrolpointsX[i]+1);
618 //                      fprintf(fd,"\n Para X = %f, Y = %f",_chargecontrolpointsX[i],_chargecontrolpointsY[i]);
619
620                         Vector *vecX = new Vector();
621                         Vector *vecY = new Vector();
622                         vecX->set_var(_chargecontrolpointsX[i]);
623                         vecY->set_var(_chargecontrolpointsY[i]);
624
625                         for(j=0; j<InX->size(); j++)
626                         {
627                                 jj = (j+1)%(InX->size());
628                                 m2 = Slope((*InX)[j],(*InY)[j],(*InX)[jj],(*InY)[jj]);
629                                 Intersection(_chargecontrolpointsX[i],_chargecontrolpointsY[i],(*InX)[j],(*InY)[j],mn,m2,&xinter,&yinter);
630
631                                 if(((*InX)[j] <= xinter && xinter <= (*InX)[jj]) || (xinter<=(*InX)[j] && xinter>=(*InX)[jj])){
632                                         vecX->set_vec(xinter);
633                                         vecY->set_vec(yinter);
634                                 }
635 /*JCP 29-09-08
636                                 if( (*InX)[j]<=(*InX)[jj] )
637                                 {
638                                         if( (xinter>=(*InX)[j]) && (xinter<=(*InX)[jj]) )
639                                         {
640                                                 //If the point is a CP, the intersection is itself.
641                                                 if((xinter==_chargecontrolpointsX[i]) && (yinter==_chargecontrolpointsY[i]))
642                                                 {
643                                                         vecX->set_vec(xinter);
644                                                         vecY->set_vec(yinter);
645 //                                                      fprintf(fd,"\n => x_int = %f, y_int = %f",xinter,yinter);
646                                                 }
647                                                 else
648                                                 {
649                                                         vecX->set_vec(xinter);
650                                                         vecY->set_vec(yinter);
651 //                                                      fprintf(fd,"\n => x_int = %f, y_int = %f",xinter,yinter);
652                                                 }
653                                         }
654                                 }
655                                 if( (*InX)[j]>(*InX)[jj] )
656                                 {
657                                         if( (xinter<=(*InX)[j]) && (xinter>=(*InX)[jj]) )
658                                         {
659                                                 //If the point is a CP, the intersection is itself.
660                                                 if((xinter==_chargecontrolpointsX[i]) && (yinter==_chargecontrolpointsY[i]))
661                                                 {
662                                                         vecX->set_vec(xinter);
663                                                         vecY->set_vec(yinter);
664 //                                                      fprintf(fd,"\n => x_int = %f, y_int = %f",xinter,yinter);
665                                                 }
666                                                 else
667                                                 {
668                                                         vecX->set_vec(xinter);
669                                                         vecY->set_vec(yinter);
670 //                                                      fprintf(fd,"\n => x_int = %f, y_int = %f",xinter,yinter);
671                                                 }
672                                         }
673                                 }
674 JCP 29-09-08*/
675                         }//FOR2
676                         _intervectorX.push_back(*vecX);
677                         _intervectorY.push_back(*vecY);
678                         //DELETE!!
679                         delete vecX;
680                         delete vecY;
681                 }//FOR1
682 //              fclose(fd);
683         }//IF
684 }
685 //-----------------------------------------------------------------------------------------------------------------------------------------
686 void AutoControlPoints::GetInterBetweenContours(std::vector<Vector>*interVX, std::vector<Vector>*interVY)
687 {
688         interVX->clear();
689         interVY->clear();
690         int size = _intervectorX.size();
691         int i;
692         if(size != 0)
693         {
694                 for(i=0; i<size; i++)
695                 {
696                         interVX->push_back(_intervectorX[i]);
697                         interVY->push_back(_intervectorY[i]);
698                 }
699         }
700 }
701 //-----------------------------------------------------------------------------------------------------------------------------------------
702 //ONLY THE LOGICAL INTERSECTIONS
703 void AutoControlPoints::IntersectionPoints()
704 {
705         if(_intervectorX.size() != 0)
706         {               
707                 _interpointsX.clear();
708                 _interpointsY.clear();
709
710 //EED
711 //              FILE *fd;
712 //              fd = fopen("C:/bbtk_JS/data/IntersectionPoints.txt","w");
713                 double dist,min;
714                 int i,j,posj;
715
716                 posj = -1;
717                 min = 9999;
718 /*JCP 30-08-09 
719                 for(j=0; j<_intervectorX[0].getsize_vec(); j++)
720                 {
721                         dist = sqrt( pow( _intervectorX[0].get_vec(j)-_intervectorX[0].get_var(),2 ) + pow( _intervectorY[0].get_vec(j)-_intervectorY[0].get_var(),2 ) );
722                         if( dist < min )
723                         {
724                                 min = dist;
725                                 posj = j;
726                         }
727                 }
728                 if(posj != -1)
729                 {
730                         _interpointsX.push_back(_intervectorX[0].get_vec(posj));
731                         _interpointsY.push_back(_intervectorY[0].get_vec(posj));
732 //                      fprintf(fd,"\n Para X = %f, Y = %f",_intervectorX[0].get_var(),_intervectorY[0].get_var());
733 //                      fprintf(fd,"\n => x_int = %f, y_int = %f",_interpointsX[0],_interpointsY[0]);
734                 }
735                 if(posj == -1)
736                 {
737                         printf("\n\n There is an invalid intersection: Must see AutoControlPoints::IntersectionPoints() method");
738                 }
739 JCP 30-08-09 */
740                 for(i=0; i<_intervectorX.size(); i++){
741                         min = 9999;
742                         posj = -1;
743                         for(j=0; j<_intervectorX[i].getsize_vec(); j++) {
744                                 dist  = sqrt( pow( _intervectorX[i].get_vec(j)-_intervectorX[i].get_var(),2 ) + pow( _intervectorY[i].get_vec(j)-_intervectorX[i].get_var(),2 ) );
745                                 if( dist < min ){
746                                         min = dist;
747                                         posj = j;
748                                 }
749                         }
750                         _interpointsX.push_back(_intervectorX[i].get_vec(posj));
751                         _interpointsY.push_back(_intervectorY[i].get_vec(posj));                
752                 }
753 /*JCP 30-09-08
754                 for(i=1; i<_intervectorX.size(); i++)
755                 {
756                         min = 9999;
757                         posj = -1;
758 //                      fprintf(fd,"\n Para X = %f, Y = %f",_intervectorX[i].get_var(),_intervectorY[i].get_var());
759                         for(j=0; j<_intervectorX[i].getsize_vec(); j++)
760                         {
761                                 //TYPE: LE PLUS PRES VOISIN
762                                 dist  = sqrt( pow( _intervectorX[i].get_vec(j)-_interpointsX[i-1],2 ) + pow( _intervectorY[i].get_vec(j)-_interpointsY[i-1],2 ) );
763                                 //TYPE: LE PLUS PRES DANS LA MÊME DROITE
764                                 //dist = sqrt(pow(_intervectorX[i].get_vec(j)-_intervectorX[i].get_var(),2)+pow(_intervectorY[i].get_vec(j)-_intervectorY[i].get_var(),2));
765                                 
766                                 if( dist < min )
767                                 {
768                                         min = dist;
769                                         posj = j;
770                                 }
771                         }
772                         _interpointsX.push_back(_intervectorX[i].get_vec(posj));
773                         _interpointsY.push_back(_intervectorY[i].get_vec(posj));
774 //                      fprintf(fd,"\n => x_int = %f, y_int = %f",_interpointsX[i],_interpointsY[i]);
775                 }
776 JCP 30-09-08*/
777 //              fclose(fd);
778         }
779 }
780 //-----------------------------------------------------------------------------------------------------------------------------------------
781 void AutoControlPoints::GetIntersectionPoints(std::vector<Vector>*interVX, std::vector<Vector>*interVY)
782 {
783         int size = _interpointsX.size();
784         int i;
785         if(size != 0)
786         {
787                 Vector *vecX = new Vector();
788                 Vector *vecY = new Vector();
789                 interVX->clear();
790                 interVY->clear();
791                 for(i=0; i<size; i++)
792                 {
793                         vecX->set_var(_controlpointsZ[0]);
794                         vecX->set_vec(_interpointsX[i]);
795                         vecY->set_var(_controlpointsZ[0]);
796                         vecY->set_vec(_interpointsY[i]);
797                         interVX->push_back(*vecX);
798                         interVY->push_back(*vecY);
799                 }
800                 delete vecX;
801                 delete vecY;
802         }
803 }
804 //-----------------------------------------------------------------------------------------------------------------------------------------
805 //ERROR BETWEEN THE LOGICAL INTERSECTIONS
806 void AutoControlPoints::ErrorBetweenContours()
807 {
808         _errorpos = -1;
809         if(_interpointsX.size() != 0)
810         {
811                 _errorvector.clear();
812                 int i;
813 //EED 22 Sep 2008
814
815 //              FILE *fd;
816 //              fd = fopen("C:/bbtk_JS/data/interErrorData.txt","w");
817                 for(i=0; i<_interpointsX.size(); i++)
818                 {
819                         _errorvector.push_back( (sqrt( pow( _interpointsX[i]-_intervectorX[i].get_var(),2 ) + pow( _interpointsY[i]-_intervectorY[i].get_var(),2 ) )/_pathsize)*100 );
820 //                      fprintf(fd,"\n%d        %f",i,_errorvector[i]);
821                 }
822 //              fclose(fd);
823                 double max = -1;
824                 for(i=0; i<_errorvector.size(); i++)
825                 {
826                         if(_interpointsX[i] != -1)
827                         {
828                                 if(_errorvector[i]>max)
829                                 {
830                                         max = _errorvector[i];
831                                         _errorpos = i;
832                                 }
833                         }
834                 }
835         }
836 }
837 //-----------------------------------------------------------------------------------------------------------------------------------------
838 void AutoControlPoints::GetErrorBetweenContours( std::vector<double>*vec )
839 {
840         vec->clear();
841         vectorFunctions *vf = new vectorFunctions();
842         vf->copyVector(&_errorvector,vec);
843         delete vf;
844 }
845 //-----------------------------------------------------------------------------------------------------------------------------------------
846 void AutoControlPoints::AddControlPoint(bool activate)
847 {
848         if(_errorpos != -1)
849         {
850                 double xmax = _interpointsX[_errorpos];
851                 double ymax = _interpointsY[_errorpos];
852                 double xx       = _intervectorX[_errorpos].get_var();
853                 double yy       = _intervectorY[_errorpos].get_var();
854 printf("\n XMAX = %f, YMAX = %f, XX = %f, YY = %f",xmax,ymax,xx,yy);
855
856                 int i,ii,j,posA=-1,posB=-1;
857                 bool findA=false, findB=false;
858                 //CASE A
859                 for(i=_errorpos; findA!=true; i++)
860                 {
861                         ii = i%_errorvector.size();
862                         for(j=0; j<_controlpointsX.size(); j++)
863                         {
864                                 if( ((float)_controlpointsX[j]-1.5<=(float)_intervectorX[ii].get_var()) && ((float)_intervectorX[ii].get_var()<=(float)_controlpointsX[j]+1.5) &&
865                                         ((float)_controlpointsY[j]-1.5<=(float)_intervectorY[ii].get_var()) && ((float)_intervectorY[ii].get_var()<=(float)_controlpointsY[j]+1.5) )
866                                 {
867                                         findA = true;
868                                         posA = j;
869                                 }
870                         }
871                 }
872                 //CASE B
873                 for(i=_errorpos; findB!=true; i--)
874                 {
875                         if(_errorpos==-1)
876                         {
877                                 i = _errorvector.size();
878                         }
879                         for(j=0; j<_controlpointsX.size(); j++)
880                         {
881                                 if( ((float)_controlpointsX[j]-1.5<=(float)_intervectorX[i].get_var()) && ((float)_intervectorX[i].get_var()<=(float)_controlpointsX[j]+1.5) &&
882                                         ((float)_controlpointsY[j]-1.5<=(float)_intervectorY[i].get_var()) && ((float)_intervectorY[i].get_var()<=(float)_controlpointsY[j]+1.5) )
883                                 {
884                                         findB = true;
885                                         posB = j;
886                                 }
887                         }
888                 }
889                 if(posA == posB)
890                 {
891                         posB = posA-1;
892                 }
893                 if(posA<posB)
894                 {
895                         posA = posB+1;
896                         if(posB = _controlpointsX.size()-1)
897                         {
898                                 posA = 0;
899                         }
900                 }
901 printf("\n POSA = %d, X = %f, Y = %f",posA,_controlpointsX[posA],_controlpointsY[posA]);
902 printf("\n POSB = %d, X = %f, Y = %f",posB,_controlpointsX[posB],_controlpointsY[posB]);
903                 _posA = posA;
904                 _posB = posB;
905                 int id = -1;
906                 if(((posA!=-1)&&(posB!=-1)))
907                 {
908                         id = posA;
909                 }
910 printf("\n ID = %d",id);
911                 if(id != -1)
912                 {
913                         std::vector<double> tempX;
914                         std::vector<double> tempY;
915                         std::vector<double> tempZ;
916                         for(i=0; i<_controlpointsX.size(); i++)
917                         {
918                                 if(i == id)
919                                 {
920                                         tempX.push_back(xmax);
921                                         tempY.push_back(ymax);
922                                         tempZ.push_back(_controlpointsZ[0]);
923                                 }
924                                 tempX.push_back(_controlpointsX[i]);
925                                 tempY.push_back(_controlpointsY[i]);
926                                 tempZ.push_back(_controlpointsZ[i]);
927                         }
928                 
929                         if(activate == true)
930                         {
931                                 vectorFunctions *vf = new vectorFunctions();
932                                 vf->copyVector(&tempX,&_controlpointsX);
933                                 vf->copyVector(&tempY,&_controlpointsY);
934                                 vf->copyVector(&tempZ,&_controlpointsZ);
935                                 delete vf;
936                         }
937                 
938                 }
939         }//IF-(principal)
940 }
941 //-----------------------------------------------------------------------------------------------------------------------------------------
942 void AutoControlPoints::InterBetweenControl( )
943 {
944         _intervecXX.clear();
945         _intervecYY.clear();
946         
947         int i,ii,j,jj;
948         double m1,mn,m2,xinter,yinter;
949         if(_chargecontrolpointsX.size() > 1) //These condition exists because there is a method for find the initial control points 
950         {
951 //EED 22 Sep 2008
952
953 //              FILE *fd;
954 //              fd = fopen("C:/bbtk_JS/data/InterBetweenControl.txt","w");
955                 for(i=0; i<_chargecontrolpointsX.size()-1; i++)
956                 {
957                         ii = (i+1)%(_chargecontrolpointsX.size());
958                         m1 = Slope(_chargecontrolpointsX[i],_chargecontrolpointsY[i],_chargecontrolpointsX[ii],_chargecontrolpointsY[ii]);
959                         mn = m1;
960                         Normal(_chargecontrolpointsX[i],_chargecontrolpointsY[i],&mn,_chargecontrolpointsX[i]+1);
961 //                      fprintf(fd,"\n Para X = %f, Y = %f",_chargecontrolpointsX[i],_chargecontrolpointsY[i]);
962
963                         Vector *vecX = new Vector();
964                         Vector *vecY = new Vector();
965                         vecX->set_var(_chargecontrolpointsX[i]);
966                         vecY->set_var(_chargecontrolpointsY[i]);
967
968                         for(j=0; j<_chargecontrolpointsX.size(); j++)
969                         {
970                                 jj = (j+1)%(_chargecontrolpointsX.size());
971                                 m2 = Slope(_chargecontrolpointsX[j],_chargecontrolpointsY[j],_chargecontrolpointsX[jj],_chargecontrolpointsY[jj]);
972                                 Intersection(_chargecontrolpointsX[i],_chargecontrolpointsY[i],_chargecontrolpointsX[j],_chargecontrolpointsY[j],mn,m2,&xinter,&yinter);
973                                 if( _chargecontrolpointsX[j]<=_chargecontrolpointsX[jj] )
974                                 {
975                                         if( (xinter>=_chargecontrolpointsX[j]) && (xinter<=_chargecontrolpointsX[jj]) )
976                                         {
977                                                 if(((float)xinter==(float)_chargecontrolpointsX[i]) && ((float)yinter==(float)_chargecontrolpointsY[i]))
978                                                 {
979                                                 }
980                                                 else
981                                                 {
982 //                                                      fprintf(fd,"\n => x_int = %f, y_int = %f",xinter,yinter);
983                                                         vecX->set_vec(xinter);
984                                                         vecY->set_vec(yinter);
985                                                 }
986                                         }
987                                 }
988                                 if( _chargecontrolpointsX[j]>_chargecontrolpointsX[jj] )
989                                 {
990                                         if( (xinter<=_chargecontrolpointsX[j]) && (xinter>=_chargecontrolpointsX[jj]) )
991                                         {
992                                                 if(((float)xinter==(float)_chargecontrolpointsX[i]) && ((float)yinter==(float)_chargecontrolpointsY[i]))
993                                                 {
994                                                 }
995                                                 else
996                                                 {
997 //                                                      fprintf(fd,"\n => x_int = %f, y_int = %f",xinter,yinter);
998                                                         vecX->set_vec(xinter);
999                                                         vecY->set_vec(yinter);
1000                                                 }
1001                                         }
1002                                 }
1003                         }//FOR2
1004                         _intervecXX.push_back(*vecX);
1005                         _intervecYY.push_back(*vecY);
1006                         
1007                         //DELETE!!
1008                         delete vecX;
1009                         delete vecY;
1010
1011                 }//FOR1
1012 //              fclose(fd);
1013         }//IF
1014 }
1015 //-----------------------------------------------------------------------------------------------------------------------------------------
1016 void AutoControlPoints::fixBetweenControl()
1017 {
1018         _interitselfX.clear();
1019         _interitselfY.clear();
1020         int i,j;
1021         float vecx,vecy,varx,vary;
1022 //      FILE *fd;
1023 //      fd = fopen("C:/bbtk_JS/data/InterBetweenControlFix.txt","w");
1024         for(i=0; i<_intervecXX.size(); i++)
1025         {
1026                 Vector *vx = new Vector();
1027                 Vector *vy = new Vector();
1028                 vx->set_var(_intervecXX[i].get_var());
1029                 vy->set_var(_intervecYY[i].get_var());
1030 //              fprintf(fd,"\n Para X = %f, Y = %f",_intervecXX[i].get_var(),_intervecYY[i].get_var());
1031                 for(j=0; j<_intervecXX[i].getsize_vec(); j++)
1032                 {
1033                         vecx = _intervecXX[i].get_vec(j);
1034                         varx = _intervecXX[i].get_var();
1035                         vecy = _intervecYY[i].get_vec(j);
1036                         vary = _intervecYY[i].get_var();
1037                         if(  (vecx == varx) && (vecy == vary) )
1038                         {
1039                         }
1040                         else
1041                         {
1042                                 vx->set_vec((double)vecx);
1043                                 vy->set_vec((double)vecy);
1044 //                              fprintf(fd,"\n => x_int = %f, y_int = %f",vecx,vecy);
1045                         }
1046                 }
1047                 _interitselfX.push_back(*vx);
1048                 _interitselfY.push_back(*vy);
1049                 delete vx;
1050                 delete vy;
1051         }
1052 //      fclose(fd);
1053 }
1054 //-----------------------------------------------------------------------------------------------------------------------------------------
1055 void AutoControlPoints::PossibleIntersections( std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ )
1056 {
1057         InterBetweenContours(InX,InY,InZ);      //_intervectorX
1058         //InterBetweenControl();                                //_intervecXX
1059         //fixBetweenControl();                          //_interitselfX
1060         
1061         std::vector<Vector> tempX;
1062         std::vector<Vector> tempY;
1063         tempX.clear();
1064         tempY.clear();
1065         int i,j,k;
1066         double dist1,dist2;
1067 //EED 22 Sep 2008
1068 //      FILE *fd;
1069 /*
1070         bool ready;
1071         fd = fopen("C:/bbtk_JS/data/InterPossibleIntersections.txt","w");
1072
1073
1074         for(i=0; i<_intervectorX.size(); i++)
1075         {
1076                 fprintf(fd,"\n Para X = %f, Y = %f",_intervectorX[i].get_var(),_intervectorY[i].get_var());
1077                 Vector *vx = new Vector();
1078                 Vector *vy = new Vector();
1079                 vx->set_var(_intervectorX[i].get_var());
1080                 vy->set_var(_intervectorY[i].get_var());
1081                 ready = false;
1082                 for(j=0; j<_intervectorX[i].getsize_vec() ; j++)
1083                 {
1084                         dist1 = sqrt( pow(_intervectorX[i].get_var()-_intervectorX[i].get_vec(j),2) + pow(_intervectorY[i].get_var()-_intervectorY[i].get_vec(j),2) );
1085                         for(k=0; (k<_interitselfX[i].getsize_vec()) && (ready!=true); k++)
1086                         {
1087                                 dist2 = sqrt( pow(_interitselfX[i].get_var()-_interitselfX[i].get_vec(k),2) + pow(_interitselfY[i].get_var()-_interitselfY[i].get_vec(k),2) );
1088                                 if(dist2>dist1)
1089                                 {
1090                                         fprintf(fd,"\n => x_int = %f, y_int = %f",_intervectorX[i].get_vec(j),_intervectorY[i].get_vec(j));
1091                                         vx->set_vec(_intervectorX[i].get_vec(j));
1092                                         vy->set_vec(_intervectorY[i].get_vec(j));
1093                                         ready == true;
1094                                 }
1095                         }
1096                 }
1097                 tempX.push_back(*vx);
1098                 tempY.push_back(*vy);
1099                 delete vx;
1100                 delete vy;
1101         }
1102         fclose(fd);
1103         
1104         _intervectorX.clear();
1105         _intervectorY.clear();
1106         Vector *vv = new Vector();
1107         vv->copyVector(&tempX,&_intervectorX);
1108         vv->copyVector(&tempY,&_intervectorY);
1109         
1110         //vv->printVector(&_intervectorX);
1111 */
1112         std::vector<double> arrX; 
1113         std::vector<double> arrY;
1114         std::vector<double>::iterator itx;
1115         std::vector<double>::iterator ity;
1116         std::vector<double>::iterator itxx;
1117         std::vector<double>::iterator ityy;
1118         double distA, distB;
1119         if(_intervectorX.size() != 0)
1120         {
1121 //              fd = fopen("C:/bbtk_JS/data/InterPossibleIntersections.txt","w");
1122                 for(i=0; i<_intervectorX.size(); i++)
1123                 {
1124 //                      fprintf(fd,"\n Para X = %f, Y = %f",_intervectorX[i].get_var(),_intervectorY[i].get_var());
1125                         if(_intervectorX[i].getsize_vec() > 1)
1126                         {
1127                                 arrX.clear();
1128                                 arrY.clear();
1129                                 for(j=0; j<_intervectorX[i].getsize_vec(); j++)
1130                                 {
1131                                         arrX.push_back(_intervectorX[i].get_vec(j));
1132                                         arrY.push_back(_intervectorY[i].get_vec(j));
1133                                 }
1134 //printf("\n arrX Size = %d",arrX.size());
1135                                 itx = arrX.begin();
1136                                 ity = arrY.begin();
1137                                 itxx = arrX.begin()+1;
1138                                 ityy = arrY.begin()+1;
1139                                 for(j=0; j<arrX.size()-1; j++)
1140                                 {
1141                                         // I
1142                                         if( (*itx > _intervectorX[i].get_var())  && (*ity < _intervectorY[i].get_var()) && 
1143                                                 (*itxx > _intervectorX[i].get_var()) && (*ityy < _intervectorY[i].get_var()) )
1144                                         {
1145                                                 distA = sqrt( pow(*itx-_intervectorX[i].get_var(),2) + pow(*ity-_intervectorY[i].get_var(),2) );
1146                                                 distB = sqrt( pow(*itxx-_intervectorX[i].get_var(),2) + pow(*ityy-_intervectorY[i].get_var(),2) );
1147                                                 if(distA<distB)
1148                                                 {
1149                                                         arrX.erase(itxx);
1150                                                         arrY.erase(ityy);
1151                                                 }
1152                                                         if(distA>distB)
1153                                                 {
1154                                                         arrX.erase(itx);
1155                                                         arrY.erase(ity);
1156                                                         itxx++;
1157                                                         ityy++;
1158                                                 }       
1159                                         }
1160                                         // II
1161                                         else if( (*itx < _intervectorX[i].get_var())  && (*ity < _intervectorY[i].get_var()) && 
1162                                                 (*itxx < _intervectorX[i].get_var()) && (*ityy < _intervectorY[i].get_var()) )
1163                                         {
1164                                                 distA = sqrt( pow(*itx-_intervectorX[i].get_var(),2) + pow(*ity-_intervectorY[i].get_var(),2) );
1165                                                 distB = sqrt( pow(*itxx-_intervectorX[i].get_var(),2) + pow(*ityy-_intervectorY[i].get_var(),2) );
1166                                                 if(distA<distB)
1167                                                 {
1168                                                         arrX.erase(itxx);
1169                                                         arrY.erase(ityy);
1170                                                 }
1171                                                 if(distA>distB)
1172                                                 {
1173                                                         arrX.erase(itx);
1174                                                         arrY.erase(ity);
1175                                                         itxx++;
1176                                                         ityy++;
1177                                                 }       
1178                                         }
1179                                         // III
1180                                         else if( (*itx < _intervectorX[i].get_var())  && (*ity > _intervectorY[i].get_var()) && 
1181                                                 (*itxx < _intervectorX[i].get_var()) && (*ityy > _intervectorY[i].get_var()) )
1182                                         {
1183                                                 distA = sqrt( pow(*itx-_intervectorX[i].get_var(),2) + pow(*ity-_intervectorY[i].get_var(),2) );
1184                                                 distB = sqrt( pow(*itxx-_intervectorX[i].get_var(),2) + pow(*ityy-_intervectorY[i].get_var(),2) );
1185                                                 if(distA<distB)
1186                                                 {
1187                                                         arrX.erase(itxx);
1188                                                         arrY.erase(ityy);
1189                                                 }
1190                                                 if(distA>distB)
1191                                                 {
1192                                                         arrX.erase(itx);
1193                                                         arrY.erase(ity);
1194                                                         itxx++;
1195                                                         ityy++;
1196                                                 }       
1197                                         }
1198                                         // IV
1199                                         else if( ((double)*itx > _intervectorX[i].get_var())  && (*ity > _intervectorY[i].get_var()) && 
1200                                                 ((double)*itxx > _intervectorX[i].get_var()) && (*ityy > _intervectorY[i].get_var()) )
1201                                         {
1202                                                 distA = sqrt( pow(*itx-_intervectorX[i].get_var(),2) + pow(*ity-_intervectorY[i].get_var(),2) );
1203                                                 distB = sqrt( pow(*itxx-_intervectorX[i].get_var(),2) + pow(*ityy-_intervectorY[i].get_var(),2) );
1204                                                 if(distA<distB)
1205                                                 {
1206                                                         arrX.erase(itxx);
1207                                                         arrY.erase(ityy);
1208                                                 }
1209                                                 if(distA>distB)
1210                                                 {
1211                                                         arrX.erase(itx);
1212                                                         arrY.erase(ity);
1213                                                         itxx++;
1214                                                         ityy++;
1215                                                 }       
1216                                         }
1217                                         else
1218                                         {
1219                                                 itx++;
1220                                                 ity++;
1221                                                 itxx++;
1222                                                 ityy++;
1223                                         }
1224                                 }
1225                                 _intervectorX[i].resetVec();
1226                                 _intervectorY[i].resetVec();
1227 //printf("\n _intervector(%d) Size = %d",i,_intervectorX[i].getsize_vec());
1228                                 for(k=0; k<arrX.size(); k++)
1229                                 {
1230 //printf("\n arr(%d) X = %f, Y = %f",k,arrX[k],arrY[k]);
1231 //                                      fprintf(fd,"\n => x_int = %f, y_int = %f",arrX[k],arrY[k]);
1232                                         _intervectorX[i].set_vec(arrX[k]);
1233                                         _intervectorY[i].set_vec(arrY[k]);
1234                                 }
1235                         }
1236                 }
1237         }
1238
1239         //DELETE!!
1240 //      delete vv;
1241
1242 }
1243 //-----------------------------------------------------------------------------------------------------------------------------------------
1244 void AutoControlPoints::ControlInContour(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ)
1245 {
1246         int i,j;
1247         _contIncontpos.clear(); 
1248         bool find = false;
1249         double range = 2;
1250         for(i=0; i<_controlpointsX.size(); i++)
1251         {
1252                 find = false;
1253                 for(j=0; (j<InX->size()) && (find!=true); j++)
1254                 {
1255                         if( ((*InX)[j]-range<=_controlpointsX[i]) && (_controlpointsX[i]<=(*InX)[j]+range) && ((*InY)[j]-range<=_controlpointsY[i]) && (_controlpointsY[i]<=(*InY)[j]+range) )
1256                         {
1257                                 _contIncontpos.push_back(j);
1258                                 find = true;
1259                         }
1260                 }
1261         }
1262 }
1263 //-----------------------------------------------------------------------------------------------------------------------------------------
1264 void AutoControlPoints::NearMaxError2Control()
1265 {
1266         if(_interpointsX.size() != 0)
1267         {
1268                 AddControlPoint(false);
1269
1270                 double distA = sqrt( pow(_interpointsX[_errorpos]-_controlpointsX[_posA],2) + pow(_interpointsY[_errorpos]-_controlpointsY[_posA],2) );
1271                 double distB = sqrt( pow(_interpointsX[_errorpos]-_controlpointsX[_posB],2) + pow(_interpointsY[_errorpos]-_controlpointsY[_posB],2) );
1272                 double nearp = -1;
1273                 if(distA<distB)
1274                 {
1275                         nearp = distA;
1276                         _posn = _posA;
1277
1278                 }
1279                 else
1280                 {
1281                         nearp = distB;
1282                         _posn = _posB;
1283                 }
1284         }
1285 }
1286 //-----------------------------------------------------------------------------------------------------------------------------------------
1287 void AutoControlPoints::MoveControlPointInContour(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ)
1288 {
1289 int i;
1290 /*
1291 //PRINTF---------------------------------------------------------------
1292 printf("\n CONTROL POINTS BEFORE MOVEMENT");
1293 for(i=0; i<_controlpointsX.size(); i++)
1294 {
1295         printf("\n X = %f, Y = %f",_controlpointsX[i],_controlpointsY[i]);
1296 }
1297 //---------------------------------------------------------------------
1298 */
1299         vectorFunctions *vf = new vectorFunctions();
1300         fixBetweenPoints(5.0);
1301         PossibleIntersections(InX,InY,InZ);
1302         IntersectionPoints();
1303         ErrorBetweenContours();
1304         double promIn = vf->promVector(&_errorvector,false);
1305
1306         std::vector<double> tempX;
1307         std::vector<double> tempY;
1308         std::vector<double> tempZ;
1309         tempX.clear();
1310         tempY.clear();
1311         tempZ.clear();
1312         
1313         vf->copyVector(&_controlpointsX,&tempX);
1314         vf->copyVector(&_controlpointsY,&tempY);
1315         vf->copyVector(&_controlpointsZ,&tempZ);
1316         _controlpointsX.clear();
1317         _controlpointsY.clear();
1318         _controlpointsZ.clear();
1319
1320         for(i=0; i<tempX.size(); i++)
1321         {
1322                 if(i==_posn)
1323                 {
1324                         _controlpointsX.push_back( (*InX)[_contIncontpos[_posn]] );
1325                         _controlpointsY.push_back( (*InY)[_contIncontpos[_posn]] );
1326                         _controlpointsZ.push_back( (*InZ)[_contIncontpos[_posn]] );
1327                 }
1328                 else
1329                 {
1330                         _controlpointsX.push_back( tempX[i] );
1331                         _controlpointsY.push_back( tempY[i] );
1332                         _controlpointsZ.push_back( tempZ[i] );
1333                 }
1334         }
1335
1336         fixBetweenPoints(5.0);
1337         PossibleIntersections(InX,InY,InZ);
1338         IntersectionPoints();
1339         ErrorBetweenContours();
1340         double promactualIn = vf->promVector(&_errorvector,false);
1341         double prom;
1342         double promactual;
1343         
1344         int posact;
1345         int pos1;
1346         double prom1final;
1347         int pos2;
1348         double prom2final;
1349
1350         //DIRECTION 1
1351         int direction = 1;
1352         posact = _posn;
1353         prom = -1;
1354         promactual = promactualIn;
1355         _controlpointsX.clear();
1356         _controlpointsY.clear();
1357         _controlpointsZ.clear();
1358         for(i=0; promactual > prom; i++)
1359         {
1360                 prom = promactual;
1361                 for(i=0; i<tempX.size(); i++)
1362                 {
1363                         if(i==_posn)
1364                         {
1365                                 _controlpointsX.push_back( (*InX)[_contIncontpos[posact]] );
1366                                 _controlpointsY.push_back( (*InY)[_contIncontpos[posact]] );
1367                                 _controlpointsZ.push_back( (*InZ)[_contIncontpos[posact]] );
1368                         }
1369                         else
1370                         {
1371                                 _controlpointsX.push_back( tempX[i] );
1372                                 _controlpointsY.push_back( tempY[i] );
1373                                 _controlpointsZ.push_back( tempZ[i] );
1374                         }
1375                 }
1376                 if(direction == 1)
1377                 {
1378                         posact = posact+1;
1379                 }
1380                 if(direction == -1)
1381                 {
1382                         posact = posact-1;
1383                 }
1384                 fixBetweenPoints(5.0);
1385                 PossibleIntersections(InX,InY,InZ);
1386                 IntersectionPoints();
1387                 ErrorBetweenContours();
1388                 promactual = vf->promVector(&_errorvector,false);
1389 //printf("\n The point in the position %d, has moved %d times",_posn,i);
1390         }
1391         pos2 = posact;
1392         prom2final = promactual;
1393
1394         delete vf;
1395 }
1396 //--------------------------------------------------------------------------------------------------------------------------------
1397 double AutoControlPoints::MoveAndAverage(int dir, std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ)
1398 {
1399         /*
1400 //PRINTF---------------------------------------------------------------
1401         int i;
1402         printf("\n CONTROL POINTS BEFORE MOVEMENT");
1403         for(i=0; i<_controlpointsX.size(); i++)
1404         {
1405                 printf("\n X = %f, Y = %f",_controlpointsX[i],_controlpointsY[i]);
1406         }
1407         for(i=0; i<_contIncontpos.size(); i++)
1408         {
1409                 printf("\n contIncont pos = %d",_contIncontpos[i]);
1410         }
1411 */
1412 //IMPLEMENTATION-------------------------------------------------------
1413         if( (_contIncontpos.size() != 0) && (_controlpointsX.size() != 0) )
1414         {
1415                 vectorFunctions *vf = new vectorFunctions();
1416                 std::vector<double>::iterator itx;
1417                 std::vector<double>::iterator ity;
1418                 std::vector<double> tempX;
1419                 std::vector<double> tempY;
1420                 tempX.clear();
1421                 tempY.clear();
1422                 vf->copyVector(&_controlpointsX,&tempX);
1423                 vf->copyVector(&_controlpointsY,&tempY);
1424                 int i,j,pos = 0;
1425                 double prom1=0,promactual1=1;
1426                 double prom2=0,promactual2=1;
1427                 int h = 0;
1428                 int hh = 1;
1429                 if(_contIncontpos[h]>_contIncontpos[hh])
1430                 {
1431                         itx = _controlpointsX.begin();
1432                         ity = _controlpointsY.begin();
1433                         for(i=_contIncontpos[h],j=_contIncontpos[h]; (i>_contIncontpos[hh]) && (promactual1>prom1); i--)
1434                         {
1435                                 if(j == InX->size())
1436                                 {
1437                                         j = 0;
1438                                 }
1439                                 prom1 = promactual1;
1440                                 *itx = (*InX)[j];
1441                                 *ity = (*InY)[j];
1442                                 printf("\n itx = %f, ity = %f", *itx,*ity);
1443                                 fixBetweenPoints(5.0);
1444                                 PossibleIntersections(InX,InY,InZ);
1445                                 IntersectionPoints();
1446                                 ErrorBetweenContours();
1447                                 promactual1 = vf->promVector(&_errorvector,false);
1448                                 j++;
1449                         }
1450                 }
1451                 if(_contIncontpos[h]<_contIncontpos[hh])
1452                 {
1453                         itx = _controlpointsX.begin();
1454                         ity = _controlpointsY.begin();
1455                         for(i=_contIncontpos[h],j=_contIncontpos[h]; (i<_contIncontpos[hh]) && (promactual1>prom1); i++)
1456                         {
1457                                 if(j == -1)
1458                                 {
1459                                         j = InX->size()-1;
1460                                 }
1461                                 prom1 = promactual1;
1462                                 *itx = (*InX)[j];
1463                                 *ity = (*InY)[j];
1464                                 printf("\n itx = %f, ity = %f", *itx,*ity);
1465                                 fixBetweenPoints(5.0);
1466                                 PossibleIntersections(InX,InY,InZ);
1467                                 IntersectionPoints();
1468                                 ErrorBetweenContours();
1469                                 promactual1 = vf->promVector(&_errorvector,false);
1470                                 j--;
1471                         }
1472                 }
1473                 delete vf;
1474         }
1475         return 99999;
1476 }
1477 //--------------------------------------------------------------------------------------------------------------------------------
1478 void AutoControlPoints::MoveControlPoints(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ)
1479 {
1480         ControlInContour(InX,InY,InZ);
1481         NearMaxError2Control();
1482         MoveAndAverage(1,InX,InY,InZ);
1483 }
1484 //-----------------------------------------------------------------------------------------------------------------------------------------
1485 void AutoControlPoints::GetNewPoints( std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ )
1486 {
1487         vectorFunctions *vf = new vectorFunctions();
1488         double prom,maxerror;
1489         
1490         InterCircle(InX,InY,InZ);
1491         maxminLocal();
1492         fixBetweenPoints(5.0);
1493         PossibleIntersections(InX,InY,InZ);
1494         IntersectionPoints();
1495         ErrorBetweenContours();
1496         prom = vf->promVector(&_errorvector,false);
1497         vf->maxVector(&_errorvector,&maxerror);
1498 printf("\n");
1499         if( maxerror>3.0 )
1500         {
1501                 _controlpointsX.clear();
1502                 _controlpointsY.clear();
1503                 _controlpointsZ.clear();
1504                 vf->copyVector(&_minmaxlocalX,&_controlpointsX);
1505                 vf->copyVector(&_minmaxlocalY,&_controlpointsY);
1506                 for(int i=0; i<_minmaxlocalX.size(); i++)
1507                 {
1508                         _controlpointsZ.push_back( (*InZ)[0] );
1509                 }
1510                 fixBetweenPoints(5.0);
1511                 PossibleIntersections(InX,InY,InZ);
1512                 IntersectionPoints();
1513                 ErrorBetweenContours();
1514                 prom = vf->promVector(&_errorvector,false);
1515                 vf->maxVector(&_errorvector,&maxerror);
1516 printf("\n");
1517         }
1518
1519         std::vector<double> cpX;
1520         std::vector<double> cpY;
1521         std::vector<double> cpZ;
1522         cpX.clear();
1523         cpY.clear();
1524         cpZ.clear();
1525         vf->copyVector(&_controlpointsX,&cpX);
1526         vf->copyVector(&_controlpointsY,&cpY);
1527         vf->copyVector(&_controlpointsZ,&cpZ);
1528         int i;
1529
1530         for(i=0; (i<10)&&(maxerror>0.5)&&(prom>0.15); i++ )
1531         {
1532                 AddControlPoint(true);
1533                 fixBetweenPoints(5.0);
1534                 PossibleIntersections(InX,InY,InZ);
1535                 IntersectionPoints();
1536                 ErrorBetweenContours();
1537                 prom = vf->promVector(&_errorvector,false);
1538                 vf->maxVector(&_errorvector,&maxerror);
1539                 printf("\n %d ",i);
1540         }
1541
1542         if(i == 10)
1543         {
1544                 _controlpointsX.clear();
1545                 _controlpointsY.clear();
1546                 _controlpointsZ.clear();
1547                 int inicontrolpoints = cpX.size();
1548                 double inipercentage = (inicontrolpoints*100)/InX->size();
1549                 int h=0;
1550                 if(inicontrolpoints<10)
1551                 {
1552                         int points = (int)((inipercentage*3*InX->size())/100);
1553                         for (int i=0; i<InX->size(); i++, h++)
1554                         {
1555                                 if( h == points )
1556                                 {
1557                                         _controlpointsX.push_back( (*InX)[i] );
1558                                         _controlpointsY.push_back( (*InY)[i] );
1559                                         _controlpointsZ.push_back( (*InZ)[i] );
1560                                         h = 0;
1561                                 }
1562                         }
1563                 }
1564                 if(inicontrolpoints>=10)
1565                 {
1566                         int points = (int)((inipercentage*2*InX->size())/100);
1567                         for (int i=0; i<InX->size(); i++, h++)
1568                         {
1569                                 if( h == points )
1570                                 {
1571                                         _controlpointsX.push_back( (*InX)[i] );
1572                                         _controlpointsY.push_back( (*InY)[i] );
1573                                         _controlpointsZ.push_back( (*InZ)[i] );
1574                                         h = 0;
1575                                 }
1576                         }
1577                 }
1578         }
1579 /*
1580         fixBetweenPoints(5.0);
1581         PossibleIntersections(InX,InY,InZ);
1582         IntersectionPoints();
1583         ErrorBetweenContours();
1584         prom = vf->promVector(&_errorvector,false);
1585         vf->maxVector(&_errorvector,&maxerror);
1586
1587
1588 printf("\n Error Average  = %f",prom);
1589 printf("\n Error Max  = %f",maxerror);
1590         AddControlPoint(false);
1591 */
1592 /*
1593         //if( (prom>1) || (maxerror>2))
1594         if( prom>0.2 )
1595         {
1596 printf("\n Error Average is grater than 1 !!");
1597                 MoveControlPoints(InX,InY,InZ);
1598         }
1599 */
1600         delete vf;
1601 }
1602 //------------------------------------------------------------------------------------------------------------------------------------------
1603 void AutoControlPoints::GetInitialNewPoints(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ )
1604 {
1605         vectorFunctions *vf = new vectorFunctions();
1606         double prom,maxerror;
1607         
1608         InterCircle(InX,InY,InZ);
1609         maxminLocal();
1610         fixBetweenPoints(5.0);
1611         PossibleIntersections(InX,InY,InZ);
1612         IntersectionPoints();
1613         ErrorBetweenContours();
1614         prom = vf->promVector(&_errorvector,false);
1615         vf->maxVector(&_errorvector,&maxerror);
1616         
1617         if( maxerror>3.0 )
1618         {
1619                 _controlpointsX.clear();
1620                 _controlpointsY.clear();
1621                 _controlpointsZ.clear();
1622                 vf->copyVector(&_minmaxlocalX,&_controlpointsX);
1623                 vf->copyVector(&_minmaxlocalY,&_controlpointsY);
1624                 for(int i=0; i<_minmaxlocalX.size(); i++)
1625                 {
1626                         _controlpointsZ.push_back( (*InZ)[0] );
1627                 }
1628                 fixBetweenPoints(5.0);
1629                 PossibleIntersections(InX,InY,InZ);
1630                 IntersectionPoints();
1631                 ErrorBetweenContours();
1632                 prom = vf->promVector(&_errorvector,false);
1633                 vf->maxVector(&_errorvector,&maxerror);
1634         }
1635
1636         std::vector<double> cpX;
1637         std::vector<double> cpY;
1638         std::vector<double> cpZ;
1639         cpX.clear();
1640         cpY.clear();
1641         cpZ.clear();
1642         vf->copyVector(&_controlpointsX,&cpX);
1643         vf->copyVector(&_controlpointsY,&cpY);
1644         vf->copyVector(&_controlpointsZ,&cpZ);
1645
1646         double promini = prom; 
1647
1648         int i;
1649         for(i=0; (i<10)&&(maxerror>0.5)&&(prom>0.15); i++ )
1650         {
1651                 AddControlPoint(true);
1652                 fixBetweenPoints(5.0);
1653                 PossibleIntersections(InX,InY,InZ);
1654                 IntersectionPoints();
1655                 ErrorBetweenContours();
1656                 prom = vf->promVector(&_errorvector,false);
1657                 vf->maxVector(&_errorvector,&maxerror);
1658         }
1659
1660         if( i==10 || prom > promini)
1661         {
1662                 _controlpointsX.clear();
1663                 _controlpointsY.clear();
1664                 _controlpointsZ.clear();
1665                 vf->copyVector(&cpX,&_controlpointsX);
1666                 vf->copyVector(&cpY,&_controlpointsY);
1667                 vf->copyVector(&cpZ,&_controlpointsZ);
1668         }
1669         delete vf;
1670 }
1671 //------------------------------------------------------------------------------------------------------------------------------------------
1672 void AutoControlPoints::CalculeControlPoints(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ)
1673 {
1674
1675         _controlpointsX.clear();
1676         _controlpointsY.clear();
1677         _controlpointsZ.clear();
1678         _controlpointsZ.push_back((*InZ)[0]);
1679         GetNewPoints( InX,InY,InZ );
1680 }
1681 //-----------------------------------------------------------------------------------------------------------------------------------------
1682 void AutoControlPoints::CalculeInitialControlPoints(std::vector<double>*InX, std::vector<double>*InY, std::vector<double>*InZ)
1683 {
1684
1685         _controlpointsX.clear();
1686         _controlpointsY.clear();
1687         _controlpointsZ.clear();
1688         _controlpointsZ.push_back((*InZ)[0]);
1689         GetInitialNewPoints( InX,InY,InZ );
1690 }
1691 //-----------------------------------------------------------------------------------------------------------------------------------------
1692 void AutoControlPoints::GetControlPoints(std::vector<double>*OutX, std::vector<double>*OutY, std::vector<double>*OutZ)
1693 {
1694         vectorFunctions *vf = new vectorFunctions();
1695         OutX->clear();
1696         OutY->clear();
1697         OutZ->clear();
1698         vf->copyVector(&_controlpointsX,OutX);
1699         vf->copyVector(&_controlpointsY,OutY);
1700         vf->copyVector(&_controlpointsZ,OutZ);
1701         delete vf;
1702 }
1703 //-----------------------------------------------------------------------------------------------------------------------------------------
1704 void AutoControlPoints::GetInitialControlPoints(std::vector<double>*OutX, std::vector<double>*OutY, std::vector<double>*OutZ)
1705 {
1706         vectorFunctions *vf = new vectorFunctions();
1707         OutX->clear();
1708         OutY->clear();
1709         OutZ->clear();
1710         vf->copyVector(&_controlpointsX,OutX);
1711         vf->copyVector(&_controlpointsY,OutY);
1712         vf->copyVector(&_controlpointsZ,OutZ);
1713         delete vf;
1714 }
1715
1716 //-----------------------------------------------------------------------------------------------------------------------------------------
1717 void AutoControlPoints::SetNumSplineInterpolation(int num)
1718 {
1719         _numspline = num;
1720 }
1721 //-----------------------------------------------------------------------------------------------------------------------------------------
1722 //-----------------------------------------------------------------------------------------------------------------------------------------
1723 //-----------------------------------------------------------------------------------------------------------------------------------------