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 //---------------------------------------------------------------------------------------------------------------
35 //CMRU 17-08-09 -------------------------------------------------------------------------------------------------
38 //---------------------------------------------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
43 manualContourModel::~manualContourModel()
45 int i,size=_lstPoints.size();
46 for (i=0;i<size; i++){
51 _cntSplineX->RemoveAllPoints();
52 _cntSplineY->RemoveAllPoints();
53 _cntSplineZ->RemoveAllPoints();
55 _cntSplineX->Delete();
56 _cntSplineY->Delete();
57 _cntSplineZ->Delete();
59 // ----------------------------------------------------------------------------
60 int manualContourModel::AddPoint(double x,double y,double z)
62 manualPoint *mp = new manualPoint();
67 return _lstPoints.size()-1;
69 // ----------------------------------------------------------------------------
70 int manualContourModel::InsertPoint(double x,double y,double z)
72 double dd,ddmin=9999999;
77 int i,ii,iii,size=_lstPoints.size();
78 double j,MaxDivisions=20,porcentage;
81 if (_closeContour==false)
88 for ( i=0 ; i<size ; i++ )
92 x1=_lstPoints[ii]->GetX();
93 y1=_lstPoints[ii]->GetY();
94 z1=_lstPoints[ii]->GetZ();
95 x2=_lstPoints[iii]->GetX();
96 y2=_lstPoints[iii]->GetY();
97 z2=_lstPoints[iii]->GetZ();
98 for (j=0; j<=MaxDivisions; j++)
100 porcentage=(j/MaxDivisions);
101 xx=(x2-x1)*porcentage+x1;
102 yy=(y2-y1)*porcentage+y1;
103 zz=(z2-z1)*porcentage+z1;
104 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
114 if (_closeContour==false)
116 if ( (ibak==1) && (jbak==0) )
120 if ( ( ibak==size ) && ( jbak==MaxDivisions ) )
127 //JSTG - 25-04-08 ----------------------------------------------------------
128 //manualPoint *mp = new manualPoint();
129 //mp->SetPoint(x,y,z);
130 //std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + ibak;
131 //_lstPoints.insert(itNum,mp);
132 InsertPoint_id(ibak,x,y,z);
133 //----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
138 void manualContourModel::InsertPoint_id(int id, double x, double y, double z)
140 manualPoint *mp = new manualPoint();
142 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + id;
143 _lstPoints.insert(itNum,mp);
145 // ----------------------------------------------------------------------------
147 void manualContourModel::DeletePoint(int i)
149 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + i;
150 _lstPoints.erase(itNum);
152 // ----------------------------------------------------------------------------
153 void manualContourModel::DeleteAllPoints()
155 int i,size=_lstPoints.size();
156 for (i=0;i<size;i++){
157 _lstPoints.erase( _lstPoints.begin() );
159 this->UpdateSpline();
161 // ----------------------------------------------------------------------------
163 void manualContourModel::MovePoint(int i,double dx,double dy,double dz)
165 manualPoint *mp=_lstPoints[i];
166 double x=mp->GetX()+dx;
167 double y=mp->GetY()+dy;
168 double z=mp->GetZ()+dz;
171 // ----------------------------------------------------------------------------
172 void manualContourModel::MoveLstPoints(double dx,double dy,double dz)
176 // ----------------------------------------------------------------------------
177 void manualContourModel::MoveAllPoints(double dx,double dy,double dz)
179 int i,size=_lstPoints.size();
180 for (i=0;i<size;i++){
181 MovePoint(i,dx,dy,dz);
186 // ----------------------------------------------------------------------------
192 int manualContourModel::GetIdPoint(double x, double y, double z, int i_range,int type)
194 double range = i_range+1;
196 double xx,yy,zz,dd,ddmin=9999999;
198 int i,size=_lstPoints.size();
199 for (i=0;i<size;i++){
200 manualPoint *mp=_lstPoints[i];
207 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) && (fabs(zz-z)<range)) {
208 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
217 if ((fabs(yy-y)<range) && (fabs(zz-z)<range)) {
218 dd=sqrt( (yy-y)*(yy-y) + (zz-z)*(zz-z) );
227 if ((fabs(xx-x)<range) && (fabs(zz-z)<range)) {
228 dd=sqrt( (xx-x)*(xx-x) + (zz-z)*(zz-z) );
237 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) ) {
238 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) );
248 // ----------------------------------------------------------------------------
249 manualPoint* manualContourModel::GetManualPoint(int id)
251 return _lstPoints[id];
253 // ----------------------------------------------------------------------------
254 int manualContourModel::GetSizeLstPoints()
256 return _lstPoints.size();
258 //----------------------------------------------------------------------------
259 int manualContourModel::GetNumberOfPointsSpline()
261 return _sizePointsContour;
263 //----------------------------------------------------------------------------
264 void manualContourModel::SetNumberOfPointsSpline(int size)
266 _sizePointsContour = size;
269 // ----------------------------------------------------------------------------
271 void manualContourModel::SetCloseContour(bool closeContour)
273 _closeContour = closeContour;
274 if (_closeContour==true)
276 _cntSplineX->ClosedOn();
277 _cntSplineY->ClosedOn();
278 _cntSplineZ->ClosedOn();
280 _cntSplineX->ClosedOff();
281 _cntSplineY->ClosedOff();
282 _cntSplineZ->ClosedOff();
286 // ----------------------------------------------------------------------------
287 bool manualContourModel::IfCloseContour()
289 return _closeContour;
292 // ----------------------------------------------------------------------------
294 void manualContourModel::UpdateSpline() // virtual
297 np = _lstPoints.size();
299 _cntSplineX->RemoveAllPoints();
300 _cntSplineY->RemoveAllPoints();
301 _cntSplineZ->RemoveAllPoints();
302 for( i = 0; i < np; i++ ) {
303 mp = GetManualPoint(i);
304 _cntSplineX->AddPoint( i, mp->GetX() );
305 _cntSplineY->AddPoint( i, mp->GetY() );
306 _cntSplineZ->AddPoint( i, mp->GetZ() );
309 //JSTG 25-02-08 ---------------------------------------------------------------------------------------------
310 if (this->_closeContour==true)
312 _delta_JSTG = (double) (np) / double (_sizePointsContour - 1); //Without the -1 the curve is not close
314 // _delta_JSTG = (double) (np-1) / double (_sizePointsContour ); //Without the -1 the curve is not close
315 _delta_JSTG = (double) (np) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
317 //-----------------------------------------------------------------------------------------------------------
320 //---------------------------------------------------------------------------------
322 /*void manualContourModel::GetSplineiPoint(int i, double &x, double &y, double &z)
324 double delta=(double)(_lstPoints.size()) / (double)(_sizePointsContour);
325 double t = delta*(double)i;
326 GetSplinePoint(t, x, y, z);
329 //-----------------------------------------------------------------------------
331 //JSTG 25-02-08 ---------------------------------------------------------------
332 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z) // virtal
334 GetSpline_t_Point(i*_delta_JSTG,x,y,z);
337 // ----------------------------------------------------------------------------
339 //JSTG 25-02-08 ---------------------------------------------------------------
340 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
342 if (_lstPoints.size()==0)
348 if (_lstPoints.size()==1)
351 mp = GetManualPoint(0);
356 if (_lstPoints.size()>=2)
358 *x = _cntSplineX->Evaluate(t);
359 *y = _cntSplineY->Evaluate(t);
360 *z = _cntSplineZ->Evaluate(t);
364 // ----------------------------------------------------------------------------
367 /*void manualContourModel::GetSplinePoint(double t, double &x, double &y, double &z)
369 if (_lstPoints.size()==0)
375 if (_lstPoints.size()==1)
378 mp = GetManualPoint(0);
383 if (_lstPoints.size()>=2)
385 x = _cntSplineX->Evaluate(t);
386 y = _cntSplineY->Evaluate(t);
387 z = _cntSplineZ->Evaluate(t);
390 // ----------------------------------------------------------------------------
391 double manualContourModel::GetPathSize()
397 // JSTG 25-02-08 -----------------------------
401 //--------------------------------------------
403 if (_lstPoints.size()==2)
405 x1=_lstPoints[0]->GetX();
406 y1=_lstPoints[0]->GetY();
407 z1=_lstPoints[0]->GetZ();
408 x2=_lstPoints[1]->GetX();
409 y2=_lstPoints[1]->GetY();
410 z2=_lstPoints[1]->GetZ();
411 result = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
413 if (_lstPoints.size()>2)
416 // JSTG 25-02-08 ------------------------------------------
417 //np = _lstPoints.size( );
419 //delta=( double ) ( np ) / ( double ) ( nps );
421 //GetSplinePoint(0,x1,y1,z1);
422 GetSpline_i_Point(0,&x1,&y1,&z1);
424 //for( i = 1; i < nps; i++ )
425 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
427 //t = delta * (double)i;
428 //GetSplinePoint(t,x2,y2,z2);
429 GetSpline_i_Point(i,&x2,&y2,&z2);
430 //---------------------------------------------------------
431 result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
440 // ----------------------------------------------------------------------------
441 double manualContourModel::GetPathArea()
444 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
447 //JSTG 25-02-08 ---------------------------------------------
449 //-----------------------------------------------------------
455 // This uses Green's theorem:
456 // A = 1/2 * sum( xiyi+1 - xi+1yi); pO == pN
457 // A < 0 -> A = |A| (a negative value could raise because points are
458 // given in clockwise order).
460 //JSTG 25-02-08 -------------------------------------------------
461 //int np = _lstPoints.size( );
463 int nps = GetNumberOfPointsSpline();
464 //double delta=( double ) ( np ) / ( double ) ( nps );
466 for( i = 0, area = 0.0; i < nps; i++ )
469 //ti = delta * (double)i;
470 //tj = delta * (double)j;
471 //GetSplinePoint(ti,x1,y1,z1);
472 //GetSplinePoint(tj,x2,y2,z2);
473 GetSpline_i_Point(i,&x1,&y1,&z1);
474 GetSpline_i_Point(j,&x2,&y2,&z2);
475 //----------------------------------------------------------------
488 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
490 j = ( i + 1 ) % _lstPoints.size();
493 (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
494 ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
495 if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
517 // ----------------------------------------------------------------------------
518 // p[x,y,z] : data in
519 // rp[x,y,z] : data out result point
520 // rn[x,y,z] : data out result normal
522 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
524 double distMin=999999999;
525 double dist,dx,dy,dz;
530 //JSTG 25-02-08 -------------------
534 //---------------------------------
536 np = _lstPoints.size( );
539 // JSTG 25-02-08 ------------------------------------------
541 nps = GetNumberOfPointsSpline();
542 //delta = ( double ) ( np ) / ( double ) ( nps );
544 //GetSplinePoint(0,x1,y1,z1);
545 GetSpline_i_Point(0,&x1,&y1,&z1);
546 for( i = 0; i < nps; i++ )
548 //t = delta * (double)i;
549 //GetSplinePoint(t,x1,y1,z1);
550 GetSpline_i_Point(i,&x1,&y1,&z1);
551 //----------------------------------------------------------
555 dist = sqrt( dx*dx + dy*dy + dz*dz );
573 // JSTG 25-02-08 ------------------------------------------
577 //t = delta * (double)1.0;
578 //GetSplinePoint(t,x1,y1,z1);
579 GetSpline_i_Point(i,&x1,&y1,&z1);
580 //----------------------------------------------------------
597 // ----------------------------------------------------------------------------
598 manualContourModel * manualContourModel :: Clone() // virtual
600 manualContourModel * clone = new manualContourModel();
601 CopyAttributesTo(clone);
605 // ----------------------------------------------------------------------------
606 void manualContourModel::Open(FILE *ff) // virtual
610 int numberOfControlPoints;
613 fscanf(ff,"%s",tmp); // NumberOfControlPoints
614 fscanf(ff,"%s",tmp); // ##
615 numberOfControlPoints = atoi(tmp);
616 for (i=0;i<numberOfControlPoints;i++)
618 fscanf(ff,"%s",tmp); // X
620 fscanf(ff,"%s",tmp); // Y
622 fscanf(ff,"%s",tmp); // Z
628 // ----------------------------------------------------------------------------
629 int manualContourModel::GetTypeModel() //virtual
641 // ----------------------------------------------------------------------------
642 void manualContourModel::Save(FILE *ff) // virtual
644 int i,size=_lstPoints.size();
645 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
646 fprintf(ff,"NumberOfControlPoints %d\n",size);
649 manualPoint *mp=_lstPoints[i];
650 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
653 //CMRU 03-09-09-----------------------------------------------------------------------------------------------
654 void manualContourModel::SaveData(FILE *ff)
656 std::string etiqueta = GetLabel();
658 fprintf(ff,"Label: NO_LABEL\n");
660 fprintf(ff,"Label: %s\n",etiqueta.c_str());
661 fprintf(ff,"Real_Size: %f\n",GetRealSize());
664 void manualContourModel::OpenData(FILE *ff)
668 fscanf(ff,"%s",tmp); // Label:
669 fscanf(ff,"%s",tmp); // value
670 std::cout<<tmp<<std::endl;
671 //if(strcmp(tmp != "NO_LABEL") // JPR
672 if(strcmp(tmp,"NO_LABEL") != 0)
675 fscanf(ff,"%s",tmp); // Real_size
676 fscanf(ff,"%s",tmp);// #
678 //tmp.ToDouble(&tmp);
679 SetRealSize(atof(tmp));
682 //------------------------------------------------------------------------------------------------------------
684 // ---------------------------------------------------------------------------
686 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
689 //XXXX::CopyAttributesTo(cloneObject);
691 cloneObject->SetCloseContour( this->IfCloseContour() );
692 int i, size = GetSizeLstPoints();
693 for( i=0; i<size; i++ )
695 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
697 cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
698 cloneObject->SetCloseContour( _closeContour );
699 cloneObject->UpdateSpline();
703 // ---------------------------------------------------------------------------
704 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )//private
706 _lstPoints.push_back( theManualPoint );
709 std::vector<manualBaseModel*> manualContourModel::ExploseModel( )
711 std::vector<manualBaseModel*> lstTmp;
712 lstTmp.push_back(this);
717 // ----------------------------------------------------------------------------
718 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
722 int i, size = GetSizeLstPoints();
724 for( i=0; i<size; i++ )
726 mp = GetManualPoint( i );
728 mp->SetPointX( mp->GetX()*Ax + Bx );
729 mp->SetPointY( mp->GetY()*Ay + By );
734 //CMRU 17-08-09----------------------------------------------------------------------------
735 void manualContourModel::SetLabel(std::string newLabel)
740 void manualContourModel::SetRealSize(double newRealSize)
742 _realSize = newRealSize;
745 double manualContourModel::GetRealSize()
750 std::string manualContourModel::GetLabel()
754 //----------------------------------------------------------------------------