1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 #include "manualContourModel.h"
28 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 manualContourModel::manualContourModel()
34 _cntSplineX = vtkKochanekSpline::New( );
35 _cntSplineY = vtkKochanekSpline::New( );
36 _cntSplineZ = vtkKochanekSpline::New( );
38 this->SetCloseContour(true);
40 _cntSplineX->SetDefaultTension( 0 );
41 _cntSplineX->SetDefaultBias( 0 );
42 _cntSplineX->SetDefaultContinuity( 0 );
44 _cntSplineY->SetDefaultTension( 0 );
45 _cntSplineY->SetDefaultBias( 0 );
46 _cntSplineY->SetDefaultContinuity( 0 );
48 _cntSplineZ->SetDefaultTension( 0 );
49 _cntSplineZ->SetDefaultBias( 0 );
50 _cntSplineZ->SetDefaultContinuity( 0 );
52 //JSTG 25-02-08 -------------------------------------------------------------------------------------------------
54 // this parameter is reset in the VIRTUAL manualContourBaseControler::Configure
55 _sizePointsContour = 100; //JSTG 25-02-08 The change in the inisialization of these variable is critical.
58 //---------------------------------------------------------------------------------------------------------------
60 //CMRU 17-08-09 -------------------------------------------------------------------------------------------------
63 //---------------------------------------------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
68 manualContourModel::~manualContourModel()
70 int i,size=_lstPoints.size();
71 for (i=0;i<size; i++){
76 _cntSplineX->RemoveAllPoints();
77 _cntSplineY->RemoveAllPoints();
78 _cntSplineZ->RemoveAllPoints();
80 _cntSplineX->Delete();
81 _cntSplineY->Delete();
82 _cntSplineZ->Delete();
84 // ----------------------------------------------------------------------------
85 int manualContourModel::AddPoint(double x,double y,double z)
87 manualPoint *mp = new manualPoint();
92 return _lstPoints.size()-1;
94 // ----------------------------------------------------------------------------
95 int manualContourModel::InsertPoint(double x,double y,double z)
97 double dd,ddmin=9999999;
102 int i,ii,iii,size=_lstPoints.size();
103 double j,MaxDivisions=20,porcentage;
106 if (_closeContour==false)
113 for ( i=0 ; i<size ; i++ )
117 x1=_lstPoints[ii]->GetX();
118 y1=_lstPoints[ii]->GetY();
119 z1=_lstPoints[ii]->GetZ();
120 x2=_lstPoints[iii]->GetX();
121 y2=_lstPoints[iii]->GetY();
122 z2=_lstPoints[iii]->GetZ();
123 for (j=0; j<=MaxDivisions; j++)
125 porcentage=(j/MaxDivisions);
126 xx=(x2-x1)*porcentage+x1;
127 yy=(y2-y1)*porcentage+y1;
128 zz=(z2-z1)*porcentage+z1;
129 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
139 if (_closeContour==false)
141 if ( (ibak==1) && (jbak==0) )
145 if ( ( ibak==size ) && ( jbak==MaxDivisions ) )
152 //JSTG - 25-04-08 ----------------------------------------------------------
153 //manualPoint *mp = new manualPoint();
154 //mp->SetPoint(x,y,z);
155 //std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + ibak;
156 //_lstPoints.insert(itNum,mp);
157 InsertPoint_id(ibak,x,y,z);
158 //----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
163 void manualContourModel::InsertPoint_id(int id, double x, double y, double z)
165 manualPoint *mp = new manualPoint();
167 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + id;
168 _lstPoints.insert(itNum,mp);
170 // ----------------------------------------------------------------------------
172 void manualContourModel::DeletePoint(int i)
174 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + i;
175 _lstPoints.erase(itNum);
177 // ----------------------------------------------------------------------------
178 void manualContourModel::DeleteAllPoints()
180 int i,size=_lstPoints.size();
181 for (i=0;i<size;i++){
182 _lstPoints.erase( _lstPoints.begin() );
184 this->UpdateSpline();
186 // ----------------------------------------------------------------------------
188 void manualContourModel::MovePoint(int i,double dx,double dy,double dz)
190 manualPoint *mp=_lstPoints[i];
191 double x=mp->GetX()+dx;
192 double y=mp->GetY()+dy;
193 double z=mp->GetZ()+dz;
196 // ----------------------------------------------------------------------------
197 void manualContourModel::MoveLstPoints(double dx,double dy,double dz)
201 // ----------------------------------------------------------------------------
202 void manualContourModel::MoveAllPoints(double dx,double dy,double dz)
204 int i,size=_lstPoints.size();
205 for (i=0;i<size;i++){
206 MovePoint(i,dx,dy,dz);
211 // ----------------------------------------------------------------------------
217 int manualContourModel::GetIdPoint(double x, double y, double z, int i_range,int type)
219 double range = i_range+1;
221 double xx,yy,zz,dd,ddmin=9999999;
223 int i,size=_lstPoints.size();
224 for (i=0;i<size;i++){
225 manualPoint *mp=_lstPoints[i];
232 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) && (fabs(zz-z)<range)) {
233 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
242 if ((fabs(yy-y)<range) && (fabs(zz-z)<range)) {
243 dd=sqrt( (yy-y)*(yy-y) + (zz-z)*(zz-z) );
252 if ((fabs(xx-x)<range) && (fabs(zz-z)<range)) {
253 dd=sqrt( (xx-x)*(xx-x) + (zz-z)*(zz-z) );
262 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) ) {
263 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) );
273 // ----------------------------------------------------------------------------
274 manualPoint* manualContourModel::GetManualPoint(int id)
276 return _lstPoints[id];
278 // ----------------------------------------------------------------------------
279 int manualContourModel::GetSizeLstPoints()
281 return _lstPoints.size();
283 //----------------------------------------------------------------------------
284 int manualContourModel::GetNumberOfPointsSpline()
286 return _sizePointsContour;
288 //----------------------------------------------------------------------------
289 void manualContourModel::SetNumberOfPointsSpline(int size)
291 _sizePointsContour = size;
294 // ----------------------------------------------------------------------------
296 void manualContourModel::SetCloseContour(bool closeContour)
298 _closeContour = closeContour;
299 if (_closeContour==true)
301 _cntSplineX->ClosedOn();
302 _cntSplineY->ClosedOn();
303 _cntSplineZ->ClosedOn();
305 _cntSplineX->ClosedOff();
306 _cntSplineY->ClosedOff();
307 _cntSplineZ->ClosedOff();
311 // ----------------------------------------------------------------------------
312 bool manualContourModel::IfCloseContour()
314 return _closeContour;
317 // ----------------------------------------------------------------------------
319 void manualContourModel::UpdateSpline() // virtual
322 np = _lstPoints.size();
324 _cntSplineX->RemoveAllPoints();
325 _cntSplineY->RemoveAllPoints();
326 _cntSplineZ->RemoveAllPoints();
327 for( i = 0; i < np; i++ ) {
328 mp = GetManualPoint(i);
329 _cntSplineX->AddPoint( i, mp->GetX() );
330 _cntSplineY->AddPoint( i, mp->GetY() );
331 _cntSplineZ->AddPoint( i, mp->GetZ() );
334 //JSTG 25-02-08 ---------------------------------------------------------------------------------------------
335 if (this->_closeContour==true)
337 _delta_JSTG = (double) (np) / double (_sizePointsContour - 1); //Without the -1 the curve is not close
339 // _delta_JSTG = (double) (np-1) / double (_sizePointsContour ); //Without the -1 the curve is not close
340 _delta_JSTG = (double) (np) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
342 //-----------------------------------------------------------------------------------------------------------
345 //---------------------------------------------------------------------------------
347 /*void manualContourModel::GetSplineiPoint(int i, double &x, double &y, double &z)
349 double delta=(double)(_lstPoints.size()) / (double)(_sizePointsContour);
350 double t = delta*(double)i;
351 GetSplinePoint(t, x, y, z);
354 //-----------------------------------------------------------------------------
356 //JSTG 25-02-08 ---------------------------------------------------------------
357 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z) // virtal
359 GetSpline_t_Point(i*_delta_JSTG,x,y,z);
362 // ----------------------------------------------------------------------------
364 //JSTG 25-02-08 ---------------------------------------------------------------
365 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
367 if (_lstPoints.size()==0)
373 if (_lstPoints.size()==1)
376 mp = GetManualPoint(0);
381 if (_lstPoints.size()>=2)
383 *x = _cntSplineX->Evaluate(t);
384 *y = _cntSplineY->Evaluate(t);
385 *z = _cntSplineZ->Evaluate(t);
389 // ----------------------------------------------------------------------------
392 /*void manualContourModel::GetSplinePoint(double t, double &x, double &y, double &z)
394 if (_lstPoints.size()==0)
400 if (_lstPoints.size()==1)
403 mp = GetManualPoint(0);
408 if (_lstPoints.size()>=2)
410 x = _cntSplineX->Evaluate(t);
411 y = _cntSplineY->Evaluate(t);
412 z = _cntSplineZ->Evaluate(t);
415 // ----------------------------------------------------------------------------
416 double manualContourModel::GetPathSize()
422 // JSTG 25-02-08 -----------------------------
426 //--------------------------------------------
428 if (_lstPoints.size()==2)
430 x1=_lstPoints[0]->GetX();
431 y1=_lstPoints[0]->GetY();
432 z1=_lstPoints[0]->GetZ();
433 x2=_lstPoints[1]->GetX();
434 y2=_lstPoints[1]->GetY();
435 z2=_lstPoints[1]->GetZ();
436 result = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
438 if (_lstPoints.size()>2)
441 // JSTG 25-02-08 ------------------------------------------
442 //np = _lstPoints.size( );
444 //delta=( double ) ( np ) / ( double ) ( nps );
446 //GetSplinePoint(0,x1,y1,z1);
447 GetSpline_i_Point(0,&x1,&y1,&z1);
449 //for( i = 1; i < nps; i++ )
450 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
452 //t = delta * (double)i;
453 //GetSplinePoint(t,x2,y2,z2);
454 GetSpline_i_Point(i,&x2,&y2,&z2);
455 //---------------------------------------------------------
456 result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
465 // ----------------------------------------------------------------------------
466 double manualContourModel::GetPathArea()
469 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
472 //JSTG 25-02-08 ---------------------------------------------
474 //-----------------------------------------------------------
480 // This uses Green's theorem:
481 // A = 1/2 * sum( xiyi+1 - xi+1yi); pO == pN
482 // A < 0 -> A = |A| (a negative value could raise because points are
483 // given in clockwise order).
485 //JSTG 25-02-08 -------------------------------------------------
486 //int np = _lstPoints.size( );
488 int nps = GetNumberOfPointsSpline();
489 //double delta=( double ) ( np ) / ( double ) ( nps );
491 for( i = 0, area = 0.0; i < nps; i++ )
494 //ti = delta * (double)i;
495 //tj = delta * (double)j;
496 //GetSplinePoint(ti,x1,y1,z1);
497 //GetSplinePoint(tj,x2,y2,z2);
498 GetSpline_i_Point(i,&x1,&y1,&z1);
499 GetSpline_i_Point(j,&x2,&y2,&z2);
500 //----------------------------------------------------------------
513 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
515 j = ( i + 1 ) % _lstPoints.size();
518 (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
519 ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
520 if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
542 // ----------------------------------------------------------------------------
543 // p[x,y,z] : data in
544 // rp[x,y,z] : data out result point
545 // rn[x,y,z] : data out result normal
547 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
549 double distMin=999999999;
550 double dist,dx,dy,dz;
555 //JSTG 25-02-08 -------------------
559 //---------------------------------
561 np = _lstPoints.size( );
564 // JSTG 25-02-08 ------------------------------------------
566 nps = GetNumberOfPointsSpline();
567 //delta = ( double ) ( np ) / ( double ) ( nps );
569 //GetSplinePoint(0,x1,y1,z1);
570 GetSpline_i_Point(0,&x1,&y1,&z1);
571 for( i = 0; i < nps; i++ )
573 //t = delta * (double)i;
574 //GetSplinePoint(t,x1,y1,z1);
575 GetSpline_i_Point(i,&x1,&y1,&z1);
576 //----------------------------------------------------------
580 dist = sqrt( dx*dx + dy*dy + dz*dz );
598 // JSTG 25-02-08 ------------------------------------------
602 //t = delta * (double)1.0;
603 //GetSplinePoint(t,x1,y1,z1);
604 GetSpline_i_Point(i,&x1,&y1,&z1);
605 //----------------------------------------------------------
622 // ----------------------------------------------------------------------------
623 manualContourModel * manualContourModel :: Clone() // virtual
625 manualContourModel * clone = new manualContourModel();
626 CopyAttributesTo(clone);
630 // ----------------------------------------------------------------------------
631 void manualContourModel::Open(FILE *ff) // virtual
635 int numberOfControlPoints;
638 fscanf(ff,"%s",tmp); // NumberOfControlPoints
639 fscanf(ff,"%s",tmp); // ##
640 numberOfControlPoints = atoi(tmp);
641 for (i=0;i<numberOfControlPoints;i++)
643 fscanf(ff,"%s",tmp); // X
645 fscanf(ff,"%s",tmp); // Y
647 fscanf(ff,"%s",tmp); // Z
653 // ----------------------------------------------------------------------------
654 int manualContourModel::GetTypeModel() //virtual
666 // ----------------------------------------------------------------------------
667 void manualContourModel::Save(FILE *ff) // virtual
669 int i,size=_lstPoints.size();
670 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
671 fprintf(ff,"NumberOfControlPoints %d\n",size);
674 manualPoint *mp=_lstPoints[i];
675 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
678 //CMRU 03-09-09-----------------------------------------------------------------------------------------------
679 void manualContourModel::SaveData(FILE *ff)
681 std::string etiqueta = GetLabel();
683 fprintf(ff,"Label: NO_LABEL\n");
685 fprintf(ff,"Label: %s\n",etiqueta.c_str());
686 fprintf(ff,"Real_Size: %f\n",GetRealSize());
689 void manualContourModel::OpenData(FILE *ff)
693 fscanf(ff,"%s",tmp); // Label:
694 fscanf(ff,"%s",tmp); // value
695 std::cout<<tmp<<std::endl;
696 //if(strcmp(tmp != "NO_LABEL") // JPR
697 if(strcmp(tmp,"NO_LABEL") != 0)
700 fscanf(ff,"%s",tmp); // Real_size
701 fscanf(ff,"%s",tmp);// #
703 //tmp.ToDouble(&tmp);
704 SetRealSize(atof(tmp));
707 //------------------------------------------------------------------------------------------------------------
709 // ---------------------------------------------------------------------------
711 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
714 //XXXX::CopyAttributesTo(cloneObject);
716 cloneObject->SetCloseContour( this->IfCloseContour() );
717 int i, size = GetSizeLstPoints();
718 for( i=0; i<size; i++ )
720 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
722 cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
723 cloneObject->SetCloseContour( _closeContour );
724 cloneObject->UpdateSpline();
728 // ---------------------------------------------------------------------------
729 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )//private
731 _lstPoints.push_back( theManualPoint );
734 std::vector<manualBaseModel*> manualContourModel::ExploseModel( )
736 std::vector<manualBaseModel*> lstTmp;
737 lstTmp.push_back(this);
742 // ----------------------------------------------------------------------------
743 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
747 int i, size = GetSizeLstPoints();
749 for( i=0; i<size; i++ )
751 mp = GetManualPoint( i );
753 mp->SetPointX( mp->GetX()*Ax + Bx );
754 mp->SetPointY( mp->GetY()*Ay + By );
759 //CMRU 17-08-09----------------------------------------------------------------------------
760 void manualContourModel::SetLabel(std::string newLabel)
765 void manualContourModel::SetRealSize(double newRealSize)
767 _realSize = newRealSize;
770 double manualContourModel::GetRealSize()
775 std::string manualContourModel::GetLabel()
779 //----------------------------------------------------------------------------