]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/manualContour/manualContourModel.cpp
*** empty log message ***
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / manualContour / manualContourModel.cpp
1 #include "manualContourModel.h"
2
3 // ----------------------------------------------------------------------------
4 // ----------------------------------------------------------------------------
5 // ----------------------------------------------------------------------------
6
7 manualContourModel::manualContourModel()
8 {
9     _cntSplineX = vtkKochanekSpline::New( );
10     _cntSplineY = vtkKochanekSpline::New( );
11     _cntSplineZ = vtkKochanekSpline::New( );
12
13         this->SetCloseContour(true);
14
15     _cntSplineX->SetDefaultTension( 0 );
16         _cntSplineX->SetDefaultBias( 0 );
17         _cntSplineX->SetDefaultContinuity( 0 );
18
19     _cntSplineY->SetDefaultTension( 0 );
20         _cntSplineY->SetDefaultBias( 0 );
21         _cntSplineY->SetDefaultContinuity( 0 );
22
23     _cntSplineZ->SetDefaultTension( 0 );
24         _cntSplineZ->SetDefaultBias( 0 );
25         _cntSplineZ->SetDefaultContinuity( 0 );
26
27 //JSTG 25-02-08 -------------------------------------------------------------------------------------------------
28
29         // this parameter is reset in the  VIRTUAL manualContourBaseControler::Configure
30         _sizePointsContour      = 100;                  //JSTG 25-02-08 The change in the inisialization of these variable is critical.
31
32         _delta_JSTG                     = 0.0;
33 //---------------------------------------------------------------------------------------------------------------
34 }
35
36 // ----------------------------------------------------------------------------
37 manualContourModel::~manualContourModel()
38 {
39         int i,size=_lstPoints.size();
40         for (i=0;i<size; i++){
41                 delete _lstPoints[i];
42         }
43         _lstPoints.clear();
44
45         _cntSplineX->RemoveAllPoints();
46         _cntSplineY->RemoveAllPoints();
47         _cntSplineZ->RemoveAllPoints();
48         
49         _cntSplineX->Delete();
50         _cntSplineY->Delete();
51         _cntSplineZ->Delete();
52 }
53 // ----------------------------------------------------------------------------
54 int manualContourModel::AddPoint(double x,double y,double z)
55 {
56    manualPoint *mp = new manualPoint();
57    mp->SetPoint(x,y,z);
58    AddManualPoint(mp);
59    //UpdateSpline();
60
61    return _lstPoints.size()-1;
62 }
63 // ----------------------------------------------------------------------------
64 int manualContourModel::InsertPoint(double x,double y,double z)
65 {
66         double dd,ddmin=9999999;
67         int    ibak=0;
68         double xx,x1,x2;
69         double yy,y1,y2;
70         double zz,z1,z2;
71         int i,ii,iii,size=_lstPoints.size();
72         double j,MaxDivisions=20,porcentage;
73         int sizeB=size;
74
75         if (_closeContour==false)
76         {
77                 size=size-1;
78         }
79
80         double jbak;
81
82         for ( i=0 ; i<size ; i++ )
83         {
84                 ii=i % sizeB ;
85                 iii=(i+1) % sizeB;
86                 x1=_lstPoints[ii]->GetX();
87                 y1=_lstPoints[ii]->GetY();
88                 z1=_lstPoints[ii]->GetZ();
89                 x2=_lstPoints[iii]->GetX();
90                 y2=_lstPoints[iii]->GetY();
91                 z2=_lstPoints[iii]->GetZ();
92                 for (j=0; j<=MaxDivisions; j++)
93                 {
94                         porcentage=(j/MaxDivisions);
95                         xx=(x2-x1)*porcentage+x1;
96                         yy=(y2-y1)*porcentage+y1;
97                         zz=(z2-z1)*porcentage+z1;
98                         dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
99                         if ( dd<ddmin )
100                         {
101                                 ddmin=dd;
102                                 ibak=iii;
103                                 jbak=j;
104                         }
105                 }
106         }
107
108         if (_closeContour==false)
109         {
110                 if ( (ibak==1) && (jbak==0) )
111                 {
112                         ibak=0;
113                 }
114                 if ( ( ibak==size ) && ( jbak==MaxDivisions ) )
115                 {
116                         ibak=sizeB;
117                 }
118         }
119
120
121 //JSTG - 25-04-08 ----------------------------------------------------------
122         //manualPoint *mp = new manualPoint();
123         //mp->SetPoint(x,y,z);
124         //std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + ibak;
125         //_lstPoints.insert(itNum,mp);
126         InsertPoint_id(ibak,x,y,z);
127 //----------------------------------------------------------------------------
128
129         return ibak;
130 }
131 // ----------------------------------------------------------------------------
132 void manualContourModel::InsertPoint_id(int id, double x, double y, double z)
133 {
134         manualPoint *mp = new manualPoint();
135         mp->SetPoint(x,y,z);
136         std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + id;
137         _lstPoints.insert(itNum,mp);
138 }
139 // ----------------------------------------------------------------------------
140
141 void manualContourModel::DeletePoint(int i)
142 {
143         std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + i;
144    _lstPoints.erase(itNum);
145 }
146 // ----------------------------------------------------------------------------
147 void manualContourModel::DeleteAllPoints()
148 {
149         int i,size=_lstPoints.size();
150         for (i=0;i<size;i++){
151            _lstPoints.erase( _lstPoints.begin() );
152         }
153         this->UpdateSpline();
154 }
155 // ----------------------------------------------------------------------------
156
157 void manualContourModel::MovePoint(int i,double dx,double dy,double dz)
158 {
159         manualPoint *mp=_lstPoints[i];
160         double x=mp->GetX()+dx;
161         double y=mp->GetY()+dy;
162         double z=mp->GetZ()+dz;
163         mp->SetPoint(x,y,z);
164 }
165 // ----------------------------------------------------------------------------
166 void manualContourModel::MoveLstPoints(double dx,double dy,double dz)
167 {
168         // ToDo
169 }
170 // ----------------------------------------------------------------------------
171 void manualContourModel::MoveAllPoints(double dx,double dy,double dz)
172 {
173         int i,size=_lstPoints.size();
174         for (i=0;i<size;i++){
175                 MovePoint(i,dx,dy,dz);
176         }
177 }
178
179
180 // ----------------------------------------------------------------------------
181
182 // type=-1  x,y,z
183 // type=0       x,y
184 // type=1       y,z
185 // type=2       x,z
186 int      manualContourModel::GetIdPoint(double x, double y, double z, int i_range,int type)
187 {
188         double range = i_range+1;
189
190         double xx,yy,zz,dd,ddmin=9999999;
191         int ibak=-1;
192         int i,size=_lstPoints.size();
193         for (i=0;i<size;i++){
194                 manualPoint *mp=_lstPoints[i];
195                 xx=mp->GetX();
196                 yy=mp->GetY();
197                 zz=mp->GetZ();
198
199                 if (type==-1)
200                 {
201                         if ((fabs(xx-x)<range) && (fabs(yy-y)<range) && (fabs(zz-z)<range)) {
202                            dd=sqrt(   (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
203                            if (dd<ddmin){
204                                    ddmin=dd;
205                                    ibak=i;
206                            }
207                         }
208                 }
209                 if (type==0)
210                 {
211                         if ((fabs(yy-y)<range) && (fabs(zz-z)<range)) {
212                            dd=sqrt(   (yy-y)*(yy-y) + (zz-z)*(zz-z) );
213                            if (dd<ddmin){
214                                    ddmin=dd;
215                                    ibak=i;
216                            }
217                         }
218                 }
219                 if (type==1)
220                 {
221                         if ((fabs(xx-x)<range) && (fabs(zz-z)<range)) {
222                            dd=sqrt(   (xx-x)*(xx-x)  + (zz-z)*(zz-z) );
223                            if (dd<ddmin){
224                                    ddmin=dd;
225                                    ibak=i;
226                            }
227                         }
228                 }
229                 if (type==2)
230                 {
231                         if ((fabs(xx-x)<range) && (fabs(yy-y)<range) ) {
232                            dd=sqrt(   (xx-x)*(xx-x) + (yy-y)*(yy-y)  );
233                            if (dd<ddmin){
234                                    ddmin=dd;
235                                    ibak=i;
236                            }
237                         }
238                 }
239         }
240         return ibak;
241 }
242 // ----------------------------------------------------------------------------
243 manualPoint* manualContourModel::GetManualPoint(int id)
244 {
245         return _lstPoints[id];
246 }
247 // ----------------------------------------------------------------------------
248 int manualContourModel::GetSizeLstPoints()
249 {
250         return _lstPoints.size();
251 }
252 //----------------------------------------------------------------------------
253 int manualContourModel::GetNumberOfPointsSpline()
254 {
255         return _sizePointsContour;
256 }
257 //----------------------------------------------------------------------------
258 void manualContourModel::SetNumberOfPointsSpline(int size)
259 {
260         _sizePointsContour = size;
261 }
262
263
264 // ----------------------------------------------------------------------------
265
266 void manualContourModel::SetCloseContour(bool closeContour)
267 {
268         _closeContour = closeContour;
269         if (_closeContour==true)
270         {
271                 _cntSplineX->ClosedOn();
272                 _cntSplineY->ClosedOn();
273                 _cntSplineZ->ClosedOn();
274         } else {
275                 _cntSplineX->ClosedOff();
276                 _cntSplineY->ClosedOff();
277                 _cntSplineZ->ClosedOff();
278         }
279 }
280
281 // ----------------------------------------------------------------------------
282 bool manualContourModel::IfCloseContour()
283 {
284         return _closeContour;
285 }
286
287 // ----------------------------------------------------------------------------
288
289 void manualContourModel::UpdateSpline() // virtual
290 {
291         int i, np;
292     np  = _lstPoints.size();
293         manualPoint     *mp;
294         _cntSplineX->RemoveAllPoints();
295         _cntSplineY->RemoveAllPoints();
296         _cntSplineZ->RemoveAllPoints();
297     for( i = 0; i < np; i++ ) {
298                 mp = GetManualPoint(i);
299         _cntSplineX->AddPoint( i, mp->GetX() );
300         _cntSplineY->AddPoint( i, mp->GetY() );
301         _cntSplineZ->AddPoint( i, mp->GetZ() );
302     } //  rof
303
304 //JSTG 25-02-08 ---------------------------------------------------------------------------------------------
305         if (this->_closeContour==true)
306         {
307                 _delta_JSTG = (double) (np) / double (_sizePointsContour - 1);  //Without the -1 the curve is not close
308         } else {
309 //              _delta_JSTG = (double) (np-1) / double (_sizePointsContour );  //Without the -1 the curve is not close
310                 _delta_JSTG = (double) (np) / double (_sizePointsContour-1 );  //Without the -1 the curve is not close
311         }
312 //-----------------------------------------------------------------------------------------------------------
313 }
314
315 //---------------------------------------------------------------------------------
316
317 /*void manualContourModel::GetSplineiPoint(int i, double &x, double &y, double &z)
318 {
319         double delta=(double)(_lstPoints.size()) / (double)(_sizePointsContour);
320         double t = delta*(double)i;
321         GetSplinePoint(t, x, y, z);
322 }*/
323
324 //-----------------------------------------------------------------------------
325
326 //JSTG 25-02-08 ---------------------------------------------------------------
327 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z) // virtal
328 {
329         GetSpline_t_Point(i*_delta_JSTG,x,y,z);
330 }
331
332 // ----------------------------------------------------------------------------
333
334 //JSTG 25-02-08 ---------------------------------------------------------------
335 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
336 {
337                 if (_lstPoints.size()==0)
338         {
339                 *x      = 0;
340                 *y      = 0;
341                 *z      = 0;
342         }
343         if (_lstPoints.size()==1)
344         {
345                 manualPoint     *mp;
346                 mp      = GetManualPoint(0);
347                 *x      = mp->GetX();
348                 *y      = mp->GetY();
349                 *z      = mp->GetZ();
350         }
351         if (_lstPoints.size()>=2)
352         {
353                 *x      = _cntSplineX->Evaluate(t);
354                 *y      = _cntSplineY->Evaluate(t);
355                 *z      = _cntSplineZ->Evaluate(t);
356         }
357 }
358
359 // ----------------------------------------------------------------------------
360
361
362 /*void manualContourModel::GetSplinePoint(double t, double &x, double &y, double &z)
363 {
364         if (_lstPoints.size()==0)
365         {
366                 x       = 0;
367                 y       = 0;
368                 z       = 0;
369         }
370         if (_lstPoints.size()==1)
371         {
372                 manualPoint     *mp;
373                 mp      = GetManualPoint(0);
374                 x       = mp->GetX();
375                 y       = mp->GetY();
376                 z       = mp->GetZ();
377         }
378         if (_lstPoints.size()>=2)
379         {
380                 x       = _cntSplineX->Evaluate(t);
381                 y       = _cntSplineY->Evaluate(t);
382                 z       = _cntSplineZ->Evaluate(t);
383         }
384 }*/
385 // ----------------------------------------------------------------------------
386 double manualContourModel::GetPathSize()
387 {
388         double result = 0;
389         double x1,y1,z1;
390         double x2,y2,z2;
391
392 // JSTG 25-02-08 -----------------------------
393         //double t,delta;
394         //int i,np,nps;
395         int i;
396 //--------------------------------------------
397
398         if (_lstPoints.size()==2)
399         {
400                 x1=_lstPoints[0]->GetX();
401                 y1=_lstPoints[0]->GetY();
402                 z1=_lstPoints[0]->GetZ();
403                 x2=_lstPoints[1]->GetX();
404                 y2=_lstPoints[1]->GetY();
405                 z2=_lstPoints[1]->GetZ();
406                 result = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
407         }
408         if (_lstPoints.size()>2)
409         {
410
411 // JSTG 25-02-08 ------------------------------------------
412                 //np  = _lstPoints.size( );
413                 //nps = 200;
414                 //delta=( double ) ( np  ) / ( double ) ( nps  );
415                 UpdateSpline();
416                 //GetSplinePoint(0,x1,y1,z1);
417                 GetSpline_i_Point(0,&x1,&y1,&z1);
418
419                 //for( i = 1; i < nps; i++ )
420                 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
421                 {
422                         //t = delta * (double)i;
423                         //GetSplinePoint(t,x2,y2,z2);
424                         GetSpline_i_Point(i,&x2,&y2,&z2);
425 //---------------------------------------------------------
426                         result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
427                         x1=x2;
428                         y1=y2;
429                         z1=z2;
430                 }// for
431         }
432
433         return result;
434 }
435 // ----------------------------------------------------------------------------
436 double manualContourModel::GetPathArea()
437 {
438         double result = 555;
439         if ((_lstPoints.size()>=3) && IfCloseContour()==true )
440         {
441                 double area;
442 //JSTG 25-02-08 ---------------------------------------------
443                 //double ti,tj;
444 //-----------------------------------------------------------
445                 double x1,y1,z1;
446                 double x2,y2,z2;
447                 bool okArea=true;
448                 int i, j;
449
450                 // This uses Green's theorem:
451                 // A = 1/2 * sum( xiyi+1 - xi+1yi); pO == pN
452                 // A < 0 -> A = |A| (a negative value could raise because points are
453                 // given in clockwise order).
454
455 //JSTG 25-02-08 -------------------------------------------------
456                 //int np  = _lstPoints.size( );
457                 //int nps = 200;
458                 int nps = GetNumberOfPointsSpline();
459                 //double delta=( double ) ( np  ) / ( double ) ( nps  );
460                 UpdateSpline();
461                 for( i = 0, area = 0.0; i < nps; i++ )
462                 {
463                         j = ( i + 1 ) % nps;
464                         //ti = delta * (double)i;
465                         //tj = delta * (double)j;
466                         //GetSplinePoint(ti,x1,y1,z1);
467                         //GetSplinePoint(tj,x2,y2,z2);
468                         GetSpline_i_Point(i,&x1,&y1,&z1);
469                         GetSpline_i_Point(j,&x2,&y2,&z2);
470 //----------------------------------------------------------------
471                         area +=
472                                         (x1 * y2 ) -
473                                         ( x2 * y1 );
474                         if (z1!=z2)
475                         {
476                                 okArea=false;
477                         }
478                 }// for
479                 area /= 2.0;
480                 area = fabs( area );
481
482 /*
483                 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
484                 {
485                         j = ( i + 1 ) % _lstPoints.size();
486                         //  Area
487                         area +=
488                                         (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
489                                         ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
490                         if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
491                         {
492                                 okArea=false;
493                         }
494                 } // rof
495                 area /= 2.0;
496                 area = fabs( area );
497 */
498
499                 if (okArea==true)
500                 {
501                         result = area;
502                 } else {
503                         result = -1;
504                 }
505
506         } else {
507                 result = 0;
508         }
509         return result;
510 }
511
512 // ----------------------------------------------------------------------------
513 // p[x,y,z]   :  data in
514 // rp[x,y,z]  :  data out  result point
515 // rn[x,y,z]  :  data out   result normal
516
517 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp,  double *rn)
518 {
519         double  distMin=999999999;
520         double  dist,dx,dy,dz;
521         double  x1,y1,z1;
522         double  x2,y2,z2;
523         int             i,np,nps;
524
525 //JSTG 25-02-08 -------------------
526         //double  tback;
527         int iback;
528         //double        t,delta;
529 //---------------------------------
530
531         np      = _lstPoints.size( );
532         if (np>=2)
533         {
534 // JSTG 25-02-08 ------------------------------------------
535                 //nps           = 200;
536                 nps = GetNumberOfPointsSpline();
537                 //delta = ( double ) ( np  ) / ( double ) ( nps  );
538                 UpdateSpline();
539                 //GetSplinePoint(0,x1,y1,z1);
540                 GetSpline_i_Point(0,&x1,&y1,&z1);
541                 for( i = 0; i < nps; i++ )
542                 {
543                         //t = delta * (double)i;
544                         //GetSplinePoint(t,x1,y1,z1);
545                         GetSpline_i_Point(i,&x1,&y1,&z1);
546 //----------------------------------------------------------
547                         dx= x1-p[0];
548                         dy= y1-p[1];
549                         dz= z1-p[2];
550                         dist = sqrt( dx*dx + dy*dy + dz*dz );
551                         if (dist<distMin)
552                         {
553                                 distMin  = dist;
554 //JSTG                  tback = t;
555                                 iback = i;
556                                 rp[0] = x1;
557                                 rp[1] = y1;
558                                 rp[2] = z1;
559                                 rn[0] = x2-x1;
560                                 rn[1] = y2-y1;
561                                 rn[2] = z2-z1;
562                         }
563                         x2=x1;
564                         y2=y1;
565                         z2=z1;
566                 }// for
567
568 // JSTG 25-02-08 ------------------------------------------
569                 //if (tback==0)
570                 if (iback==0)
571                 {
572                         //t = delta * (double)1.0;
573                         //GetSplinePoint(t,x1,y1,z1);
574                         GetSpline_i_Point(i,&x1,&y1,&z1);
575 //----------------------------------------------------------
576                         rn[0]=rp[0]-x1;
577                         rn[1]=rp[1]-y1;
578                         rn[2]=rp[2]-z1;
579                 }
580         }
581         else
582         {
583                 rp[0] = 0;
584                 rp[1] = 0;
585                 rp[2] = 0;
586                 rn[0] = -1;
587                 rn[1] = 0;
588                 rn[2] = 0;
589         }
590 }
591
592 // ----------------------------------------------------------------------------
593 manualContourModel * manualContourModel :: Clone() // virtual
594 {
595         manualContourModel * clone = new manualContourModel();
596         CopyAttributesTo(clone);
597         return clone;
598 }
599
600 // ----------------------------------------------------------------------------
601 void manualContourModel::Open(FILE *ff) // virtual
602 {
603         char tmp[255];
604         int i;
605         int numberOfControlPoints;
606         double x,y,z;
607
608         fscanf(ff,"%s",tmp); // NumberOfControlPoints
609         fscanf(ff,"%s",tmp); // ##
610         numberOfControlPoints = atoi(tmp);
611         for (i=0;i<numberOfControlPoints;i++)
612         {
613                 fscanf(ff,"%s",tmp); // X
614                 x = atof(tmp);
615                 fscanf(ff,"%s",tmp); // Y
616                 y = atof(tmp);
617                 fscanf(ff,"%s",tmp); // Z
618                 z = atof(tmp);
619                 AddPoint(x,y,z);
620         }
621 }
622
623 // ----------------------------------------------------------------------------
624 int manualContourModel::GetTypeModel() //virtual
625 {
626         // 0 spline
627         // 1 spline
628         // 2 rectangle
629         // 3 circle
630         // 4 BullEye
631         // 5 BullEyeSector
632         // 6 Line
633         return 1;
634 }
635
636 // ----------------------------------------------------------------------------
637 void manualContourModel::Save(FILE *ff) // virtual
638 {
639         int i,size=_lstPoints.size();
640         fprintf(ff,"TypeModel %d\n", GetTypeModel() );
641         fprintf(ff,"NumberOfControlPoints %d\n",size);
642         for (i=0;i<size;i++)
643         {
644                 manualPoint *mp=_lstPoints[i];
645                 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
646         }
647 }
648
649
650 // ---------------------------------------------------------------------------
651
652 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
653 {
654         // Fathers object
655         //XXXX::CopyAttributesTo(cloneObject);
656
657         cloneObject->SetCloseContour( this->IfCloseContour() );
658         int i, size = GetSizeLstPoints();
659         for( i=0; i<size; i++ )
660         {
661                 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
662         }
663         cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
664         cloneObject->SetCloseContour( _closeContour );
665         cloneObject->UpdateSpline();
666 }
667
668
669 // ---------------------------------------------------------------------------
670 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )//private
671 {
672         _lstPoints.push_back( theManualPoint );
673 }
674
675 std::vector<manualContourModel*> manualContourModel::ExploseModel(  )
676 {
677         std::vector<manualContourModel*> lstTmp;
678         lstTmp.push_back(this);
679         return lstTmp;
680 }
681
682
683 // ----------------------------------------------------------------------------
684 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
685 {
686         ManualPoint * mp;
687         int i, size = GetSizeLstPoints();
688
689         for( i=0; i<size; i++ )
690         {
691                 mp = GetManualPoint( i );
692
693                 mp->SetPointX( mp->GetX()*Ax + Bx );
694                 mp->SetPointY( mp->GetY()*Ay + By );
695         }
696 }