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;
270 // ----------------------------------------------------------------------------
272 void manualContourModel::SetCloseContour(bool closeContour)
274 _closeContour = closeContour;
275 if (_closeContour==true)
277 _cntSplineX->ClosedOn();
278 _cntSplineY->ClosedOn();
279 _cntSplineZ->ClosedOn();
281 _cntSplineX->ClosedOff();
282 _cntSplineY->ClosedOff();
283 _cntSplineZ->ClosedOff();
287 // ----------------------------------------------------------------------------
288 bool manualContourModel::IfCloseContour()
290 return _closeContour;
293 // ----------------------------------------------------------------------------
295 void manualContourModel::UpdateSpline() // virtual
298 np = _lstPoints.size();
300 _cntSplineX->RemoveAllPoints();
301 _cntSplineY->RemoveAllPoints();
302 _cntSplineZ->RemoveAllPoints();
303 for( i = 0; i < np; i++ ) {
304 mp = GetManualPoint(i);
305 _cntSplineX->AddPoint( i, mp->GetX() );
306 _cntSplineY->AddPoint( i, mp->GetY() );
307 _cntSplineZ->AddPoint( i, mp->GetZ() );
310 //JSTG 25-02-08 ---------------------------------------------------------------------------------------------
311 if (this->_closeContour==true)
313 _delta_JSTG = (double) (np) / double (_sizePointsContour - 1); //Without the -1 the curve is not close
315 // _delta_JSTG = (double) (np-1) / double (_sizePointsContour ); //Without the -1 the curve is not close
316 _delta_JSTG = (double) (np) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
318 //-----------------------------------------------------------------------------------------------------------
321 //---------------------------------------------------------------------------------
323 /*void manualContourModel::GetSplineiPoint(int i, double &x, double &y, double &z)
325 double delta=(double)(_lstPoints.size()) / (double)(_sizePointsContour);
326 double t = delta*(double)i;
327 GetSplinePoint(t, x, y, z);
330 //-----------------------------------------------------------------------------
332 //JSTG 25-02-08 ---------------------------------------------------------------
333 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z) // virtal
335 GetSpline_t_Point(i*_delta_JSTG,x,y,z);
338 // ----------------------------------------------------------------------------
340 //JSTG 25-02-08 ---------------------------------------------------------------
341 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
343 if (_lstPoints.size()==0)
349 if (_lstPoints.size()==1)
352 mp = GetManualPoint(0);
357 if (_lstPoints.size()>=2)
359 *x = _cntSplineX->Evaluate(t);
360 *y = _cntSplineY->Evaluate(t);
361 *z = _cntSplineZ->Evaluate(t);
365 // ----------------------------------------------------------------------------
368 /*void manualContourModel::GetSplinePoint(double t, double &x, double &y, double &z)
370 if (_lstPoints.size()==0)
376 if (_lstPoints.size()==1)
379 mp = GetManualPoint(0);
384 if (_lstPoints.size()>=2)
386 x = _cntSplineX->Evaluate(t);
387 y = _cntSplineY->Evaluate(t);
388 z = _cntSplineZ->Evaluate(t);
391 // ----------------------------------------------------------------------------
392 double manualContourModel::GetPathSize()
398 // JSTG 25-02-08 -----------------------------
402 //--------------------------------------------
404 if (_lstPoints.size()==2)
406 x1=_lstPoints[0]->GetX();
407 y1=_lstPoints[0]->GetY();
408 z1=_lstPoints[0]->GetZ();
409 x2=_lstPoints[1]->GetX();
410 y2=_lstPoints[1]->GetY();
411 z2=_lstPoints[1]->GetZ();
412 result = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
414 if (_lstPoints.size()>2)
417 // JSTG 25-02-08 ------------------------------------------
418 //np = _lstPoints.size( );
420 //delta=( double ) ( np ) / ( double ) ( nps );
422 //GetSplinePoint(0,x1,y1,z1);
423 GetSpline_i_Point(0,&x1,&y1,&z1);
425 //for( i = 1; i < nps; i++ )
426 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
428 //t = delta * (double)i;
429 //GetSplinePoint(t,x2,y2,z2);
430 GetSpline_i_Point(i,&x2,&y2,&z2);
431 //---------------------------------------------------------
432 result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
441 // ----------------------------------------------------------------------------
442 double manualContourModel::GetPathArea()
445 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
448 //JSTG 25-02-08 ---------------------------------------------
450 //-----------------------------------------------------------
456 // This uses Green's theorem:
457 // A = 1/2 * sum( xiyi+1 - xi+1yi); pO == pN
458 // A < 0 -> A = |A| (a negative value could raise because points are
459 // given in clockwise order).
461 //JSTG 25-02-08 -------------------------------------------------
462 //int np = _lstPoints.size( );
464 int nps = GetNumberOfPointsSpline();
465 //double delta=( double ) ( np ) / ( double ) ( nps );
467 for( i = 0, area = 0.0; i < nps; i++ )
470 //ti = delta * (double)i;
471 //tj = delta * (double)j;
472 //GetSplinePoint(ti,x1,y1,z1);
473 //GetSplinePoint(tj,x2,y2,z2);
474 GetSpline_i_Point(i,&x1,&y1,&z1);
475 GetSpline_i_Point(j,&x2,&y2,&z2);
476 //----------------------------------------------------------------
489 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
491 j = ( i + 1 ) % _lstPoints.size();
494 (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
495 ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
496 if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
518 // ----------------------------------------------------------------------------
519 // p[x,y,z] : data in
520 // rp[x,y,z] : data out result point
521 // rn[x,y,z] : data out result normal
523 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
525 double distMin=999999999;
526 double dist,dx,dy,dz;
531 //JSTG 25-02-08 -------------------
535 //---------------------------------
537 np = _lstPoints.size( );
540 // JSTG 25-02-08 ------------------------------------------
542 nps = GetNumberOfPointsSpline();
543 //delta = ( double ) ( np ) / ( double ) ( nps );
545 //GetSplinePoint(0,x1,y1,z1);
546 GetSpline_i_Point(0,&x1,&y1,&z1);
547 for( i = 0; i < nps; i++ )
549 //t = delta * (double)i;
550 //GetSplinePoint(t,x1,y1,z1);
551 GetSpline_i_Point(i,&x1,&y1,&z1);
552 //----------------------------------------------------------
556 dist = sqrt( dx*dx + dy*dy + dz*dz );
574 // JSTG 25-02-08 ------------------------------------------
578 //t = delta * (double)1.0;
579 //GetSplinePoint(t,x1,y1,z1);
580 GetSpline_i_Point(i,&x1,&y1,&z1);
581 //----------------------------------------------------------
598 // ----------------------------------------------------------------------------
599 manualContourModel * manualContourModel :: Clone() // virtual
601 manualContourModel * clone = new manualContourModel();
602 CopyAttributesTo(clone);
606 // ----------------------------------------------------------------------------
607 void manualContourModel::Open(FILE *ff) // virtual
611 int numberOfControlPoints;
614 fscanf(ff,"%s",tmp); // NumberOfControlPoints
615 fscanf(ff,"%s",tmp); // ##
616 numberOfControlPoints = atoi(tmp);
617 for (i=0;i<numberOfControlPoints;i++)
619 fscanf(ff,"%s",tmp); // X
621 fscanf(ff,"%s",tmp); // Y
623 fscanf(ff,"%s",tmp); // Z
629 // ----------------------------------------------------------------------------
630 int manualContourModel::GetTypeModel() //virtual
642 // ----------------------------------------------------------------------------
643 void manualContourModel::Save(FILE *ff) // virtual
645 int i,size=_lstPoints.size();
646 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
647 fprintf(ff,"NumberOfControlPoints %d\n",size);
650 manualPoint *mp=_lstPoints[i];
651 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
654 //CMRU 03-09-09-----------------------------------------------------------------------------------------------
655 void manualContourModel::SaveData(FILE *ff)
657 std::string etiqueta = GetLabel();
659 fprintf(ff,"Label: NO_LABEL\n");
661 fprintf(ff,"Label: %s\n",etiqueta.c_str());
662 fprintf(ff,"Real_Size: %f\n",GetRealSize());
665 void manualContourModel::OpenData(FILE *ff)
669 fscanf(ff,"%s",tmp); // Label:
670 fscanf(ff,"%s",tmp); // value
671 std::cout<<tmp<<std::endl;
672 if(tmp != "NO_LABEL")
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 //----------------------------------------------------------------------------