1 #include "manualContourModel.h"
3 // ----------------------------------------------------------------------------
4 // ----------------------------------------------------------------------------
5 // ----------------------------------------------------------------------------
7 manualContourModel::manualContourModel()
9 _cntSplineX = vtkKochanekSpline::New( );
10 _cntSplineY = vtkKochanekSpline::New( );
11 _cntSplineZ = vtkKochanekSpline::New( );
13 this->SetCloseContour(true);
15 _cntSplineX->SetDefaultTension( 0 );
16 _cntSplineX->SetDefaultBias( 0 );
17 _cntSplineX->SetDefaultContinuity( 0 );
19 _cntSplineY->SetDefaultTension( 0 );
20 _cntSplineY->SetDefaultBias( 0 );
21 _cntSplineY->SetDefaultContinuity( 0 );
23 _cntSplineZ->SetDefaultTension( 0 );
24 _cntSplineZ->SetDefaultBias( 0 );
25 _cntSplineZ->SetDefaultContinuity( 0 );
27 //JSTG 25-02-08 -------------------------------------------------------------------------------------------------
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.
33 //---------------------------------------------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
37 manualContourModel::~manualContourModel()
39 int i,size=_lstPoints.size();
40 for (i=0;i<size; i++){
45 _cntSplineX->RemoveAllPoints();
46 _cntSplineY->RemoveAllPoints();
47 _cntSplineZ->RemoveAllPoints();
49 _cntSplineX->Delete();
50 _cntSplineY->Delete();
51 _cntSplineZ->Delete();
53 // ----------------------------------------------------------------------------
54 int manualContourModel::AddPoint(double x,double y,double z)
56 manualPoint *mp = new manualPoint();
61 return _lstPoints.size()-1;
63 // ----------------------------------------------------------------------------
64 int manualContourModel::InsertPoint(double x,double y,double z)
66 double dd,ddmin=9999999;
71 int i,ii,iii,size=_lstPoints.size();
72 double j,MaxDivisions=20,porcentage;
75 if (_closeContour==false)
82 for ( i=0 ; i<size ; i++ )
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++)
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) );
108 if (_closeContour==false)
110 if ( (ibak==1) && (jbak==0) )
114 if ( ( ibak==size ) && ( jbak==MaxDivisions ) )
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 //----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
132 void manualContourModel::InsertPoint_id(int id, double x, double y, double z)
134 manualPoint *mp = new manualPoint();
136 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + id;
137 _lstPoints.insert(itNum,mp);
139 // ----------------------------------------------------------------------------
141 void manualContourModel::DeletePoint(int i)
143 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + i;
144 _lstPoints.erase(itNum);
146 // ----------------------------------------------------------------------------
147 void manualContourModel::DeleteAllPoints()
149 int i,size=_lstPoints.size();
150 for (i=0;i<size;i++){
151 _lstPoints.erase( _lstPoints.begin() );
153 this->UpdateSpline();
155 // ----------------------------------------------------------------------------
157 void manualContourModel::MovePoint(int i,double dx,double dy,double dz)
159 manualPoint *mp=_lstPoints[i];
160 double x=mp->GetX()+dx;
161 double y=mp->GetY()+dy;
162 double z=mp->GetZ()+dz;
165 // ----------------------------------------------------------------------------
166 void manualContourModel::MoveLstPoints(double dx,double dy,double dz)
170 // ----------------------------------------------------------------------------
171 void manualContourModel::MoveAllPoints(double dx,double dy,double dz)
173 int i,size=_lstPoints.size();
174 for (i=0;i<size;i++){
175 MovePoint(i,dx,dy,dz);
180 // ----------------------------------------------------------------------------
186 int manualContourModel::GetIdPoint(double x, double y, double z, int i_range,int type)
188 double range = i_range+1;
190 double xx,yy,zz,dd,ddmin=9999999;
192 int i,size=_lstPoints.size();
193 for (i=0;i<size;i++){
194 manualPoint *mp=_lstPoints[i];
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) );
211 if ((fabs(yy-y)<range) && (fabs(zz-z)<range)) {
212 dd=sqrt( (yy-y)*(yy-y) + (zz-z)*(zz-z) );
221 if ((fabs(xx-x)<range) && (fabs(zz-z)<range)) {
222 dd=sqrt( (xx-x)*(xx-x) + (zz-z)*(zz-z) );
231 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) ) {
232 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) );
242 // ----------------------------------------------------------------------------
243 manualPoint* manualContourModel::GetManualPoint(int id)
245 return _lstPoints[id];
247 // ----------------------------------------------------------------------------
248 int manualContourModel::GetSizeLstPoints()
250 return _lstPoints.size();
252 //----------------------------------------------------------------------------
253 int manualContourModel::GetNumberOfPointsSpline()
255 return _sizePointsContour;
257 //----------------------------------------------------------------------------
258 void manualContourModel::SetNumberOfPointsSpline(int size)
260 _sizePointsContour = size;
264 // ----------------------------------------------------------------------------
266 void manualContourModel::SetCloseContour(bool closeContour)
268 _closeContour = closeContour;
269 if (_closeContour==true)
271 _cntSplineX->ClosedOn();
272 _cntSplineY->ClosedOn();
273 _cntSplineZ->ClosedOn();
275 _cntSplineX->ClosedOff();
276 _cntSplineY->ClosedOff();
277 _cntSplineZ->ClosedOff();
281 // ----------------------------------------------------------------------------
282 bool manualContourModel::IfCloseContour()
284 return _closeContour;
287 // ----------------------------------------------------------------------------
289 void manualContourModel::UpdateSpline() // virtual
292 np = _lstPoints.size();
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() );
304 //JSTG 25-02-08 ---------------------------------------------------------------------------------------------
305 if (this->_closeContour==true)
307 _delta_JSTG = (double) (np) / double (_sizePointsContour - 1); //Without the -1 the curve is not close
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
312 //-----------------------------------------------------------------------------------------------------------
315 //---------------------------------------------------------------------------------
317 /*void manualContourModel::GetSplineiPoint(int i, double &x, double &y, double &z)
319 double delta=(double)(_lstPoints.size()) / (double)(_sizePointsContour);
320 double t = delta*(double)i;
321 GetSplinePoint(t, x, y, z);
324 //-----------------------------------------------------------------------------
326 //JSTG 25-02-08 ---------------------------------------------------------------
327 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z) // virtal
329 GetSpline_t_Point(i*_delta_JSTG,x,y,z);
332 // ----------------------------------------------------------------------------
334 //JSTG 25-02-08 ---------------------------------------------------------------
335 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
337 if (_lstPoints.size()==0)
343 if (_lstPoints.size()==1)
346 mp = GetManualPoint(0);
351 if (_lstPoints.size()>=2)
353 *x = _cntSplineX->Evaluate(t);
354 *y = _cntSplineY->Evaluate(t);
355 *z = _cntSplineZ->Evaluate(t);
359 // ----------------------------------------------------------------------------
362 /*void manualContourModel::GetSplinePoint(double t, double &x, double &y, double &z)
364 if (_lstPoints.size()==0)
370 if (_lstPoints.size()==1)
373 mp = GetManualPoint(0);
378 if (_lstPoints.size()>=2)
380 x = _cntSplineX->Evaluate(t);
381 y = _cntSplineY->Evaluate(t);
382 z = _cntSplineZ->Evaluate(t);
385 // ----------------------------------------------------------------------------
386 double manualContourModel::GetPathSize()
392 // JSTG 25-02-08 -----------------------------
396 //--------------------------------------------
398 if (_lstPoints.size()==2)
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) );
408 if (_lstPoints.size()>2)
411 // JSTG 25-02-08 ------------------------------------------
412 //np = _lstPoints.size( );
414 //delta=( double ) ( np ) / ( double ) ( nps );
416 //GetSplinePoint(0,x1,y1,z1);
417 GetSpline_i_Point(0,&x1,&y1,&z1);
419 //for( i = 1; i < nps; i++ )
420 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
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) );
435 // ----------------------------------------------------------------------------
436 double manualContourModel::GetPathArea()
439 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
442 //JSTG 25-02-08 ---------------------------------------------
444 //-----------------------------------------------------------
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).
455 //JSTG 25-02-08 -------------------------------------------------
456 //int np = _lstPoints.size( );
458 int nps = GetNumberOfPointsSpline();
459 //double delta=( double ) ( np ) / ( double ) ( nps );
461 for( i = 0, area = 0.0; i < nps; i++ )
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 //----------------------------------------------------------------
483 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
485 j = ( i + 1 ) % _lstPoints.size();
488 (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
489 ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
490 if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
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
517 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
519 double distMin=999999999;
520 double dist,dx,dy,dz;
525 //JSTG 25-02-08 -------------------
529 //---------------------------------
531 np = _lstPoints.size( );
534 // JSTG 25-02-08 ------------------------------------------
536 nps = GetNumberOfPointsSpline();
537 //delta = ( double ) ( np ) / ( double ) ( nps );
539 //GetSplinePoint(0,x1,y1,z1);
540 GetSpline_i_Point(0,&x1,&y1,&z1);
541 for( i = 0; i < nps; i++ )
543 //t = delta * (double)i;
544 //GetSplinePoint(t,x1,y1,z1);
545 GetSpline_i_Point(i,&x1,&y1,&z1);
546 //----------------------------------------------------------
550 dist = sqrt( dx*dx + dy*dy + dz*dz );
568 // JSTG 25-02-08 ------------------------------------------
572 //t = delta * (double)1.0;
573 //GetSplinePoint(t,x1,y1,z1);
574 GetSpline_i_Point(i,&x1,&y1,&z1);
575 //----------------------------------------------------------
592 // ----------------------------------------------------------------------------
593 manualContourModel * manualContourModel :: Clone() // virtual
595 manualContourModel * clone = new manualContourModel();
596 CopyAttributesTo(clone);
600 // ----------------------------------------------------------------------------
601 void manualContourModel::Open(FILE *ff) // virtual
605 int numberOfControlPoints;
608 fscanf(ff,"%s",tmp); // NumberOfControlPoints
609 fscanf(ff,"%s",tmp); // ##
610 numberOfControlPoints = atoi(tmp);
611 for (i=0;i<numberOfControlPoints;i++)
613 fscanf(ff,"%s",tmp); // X
615 fscanf(ff,"%s",tmp); // Y
617 fscanf(ff,"%s",tmp); // Z
623 // ----------------------------------------------------------------------------
624 int manualContourModel::GetTypeModel() //virtual
636 // ----------------------------------------------------------------------------
637 void manualContourModel::Save(FILE *ff) // virtual
639 int i,size=_lstPoints.size();
640 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
641 fprintf(ff,"NumberOfControlPoints %d\n",size);
644 manualPoint *mp=_lstPoints[i];
645 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
650 // ---------------------------------------------------------------------------
652 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
655 //XXXX::CopyAttributesTo(cloneObject);
657 cloneObject->SetCloseContour( this->IfCloseContour() );
658 int i, size = GetSizeLstPoints();
659 for( i=0; i<size; i++ )
661 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
663 cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
664 cloneObject->SetCloseContour( _closeContour );
665 cloneObject->UpdateSpline();
669 // ---------------------------------------------------------------------------
670 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )//private
672 _lstPoints.push_back( theManualPoint );
675 std::vector<manualContourModel*> manualContourModel::ExploseModel( )
677 std::vector<manualContourModel*> lstTmp;
678 lstTmp.push_back(this);
683 // ----------------------------------------------------------------------------
684 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
688 int i, size = GetSizeLstPoints();
690 for( i=0; i<size; i++ )
692 mp = GetManualPoint( i );
694 mp->SetPointX( mp->GetX()*Ax + Bx );
695 mp->SetPointY( mp->GetY()*Ay + By );