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( );
37 this->SetCloseContour(true);
38 _cntSplineX->SetDefaultTension( 0 );
39 _cntSplineX->SetDefaultBias( 0 );
40 _cntSplineX->SetDefaultContinuity( 0 );
41 _cntSplineY->SetDefaultTension( 0 );
42 _cntSplineY->SetDefaultBias( 0 );
43 _cntSplineY->SetDefaultContinuity( 0 );
44 _cntSplineZ->SetDefaultTension( 0 );
45 _cntSplineZ->SetDefaultBias( 0 );
46 _cntSplineZ->SetDefaultContinuity( 0 );
47 //JSTG 25-02-08 -------------------------------------------------------------------------------------------------
48 // this parameter is reset in the VIRTUAL manualContourBaseControler::Configure
49 _sizePointsContour = 100; //JSTG 25-02-08 The change in the inisialization of these variable is critical.
51 //---------------------------------------------------------------------------------------------------------------
52 //CMRU 17-08-09 -------------------------------------------------------------------------------------------------
55 //---------------------------------------------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
59 manualContourModel::~manualContourModel()
61 int i,size=_lstPoints.size();
62 for (i=0;i<size; i++){
66 _cntSplineX->RemoveAllPoints();
67 _cntSplineY->RemoveAllPoints();
68 _cntSplineZ->RemoveAllPoints();
69 _cntSplineX->Delete();
70 _cntSplineY->Delete();
71 _cntSplineZ->Delete();
74 // ----------------------------------------------------------------------------
75 int manualContourModel::AddPoint(double x,double y,double z)
77 manualPoint *mp = new manualPoint();
81 return _lstPoints.size()-1;
83 // ----------------------------------------------------------------------------
84 int manualContourModel::InsertPoint(double x,double y,double z)
86 double dd,ddmin=9999999;
91 int i,ii,iii,size=_lstPoints.size();
92 double j,MaxDivisions=20,porcentage;
95 if (_closeContour==false)
102 for ( i=0 ; i<size ; i++ )
106 x1=_lstPoints[ii]->GetX();
107 y1=_lstPoints[ii]->GetY();
108 z1=_lstPoints[ii]->GetZ();
109 x2=_lstPoints[iii]->GetX();
110 y2=_lstPoints[iii]->GetY();
111 z2=_lstPoints[iii]->GetZ();
112 for (j=0; j<=MaxDivisions; j++)
114 porcentage=(j/MaxDivisions);
115 xx=(x2-x1)*porcentage+x1;
116 yy=(y2-y1)*porcentage+y1;
117 zz=(z2-z1)*porcentage+z1;
118 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
128 if (_closeContour==false)
130 if ( (ibak==1) && (jbak==0) )
134 if ( ( ibak==size ) && ( jbak==MaxDivisions ) )
141 //JSTG - 25-04-08 ----------------------------------------------------------
142 //manualPoint *mp = new manualPoint();
143 //mp->SetPoint(x,y,z);
144 //std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + ibak;
145 //_lstPoints.insert(itNum,mp);
146 InsertPoint_id(ibak,x,y,z);
147 //----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
152 void manualContourModel::InsertPoint_id(int id, double x, double y, double z)
154 manualPoint *mp = new manualPoint();
156 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + id;
157 _lstPoints.insert(itNum,mp);
159 // ----------------------------------------------------------------------------
161 void manualContourModel::DeletePoint(int i)
163 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + i;
164 _lstPoints.erase(itNum);
166 // ----------------------------------------------------------------------------
167 void manualContourModel::DeleteAllPoints()
169 int i,size=_lstPoints.size();
170 for (i=0;i<size;i++){
171 _lstPoints.erase( _lstPoints.begin() );
173 this->UpdateSpline();
175 // ----------------------------------------------------------------------------
177 void manualContourModel::MovePoint(int i,double dx,double dy,double dz)
179 printf("EED manualContourModel::MovePoint %p \n", this);
180 manualPoint *mp=_lstPoints[i];
181 double x=mp->GetX()+dx;
182 double y=mp->GetY()+dy;
183 double z=mp->GetZ()+dz;
186 // ----------------------------------------------------------------------------
187 void manualContourModel::MoveLstPoints(double dx,double dy,double dz)
191 // ----------------------------------------------------------------------------
192 void manualContourModel::MoveAllPoints(double dx,double dy,double dz)
194 int i,size=_lstPoints.size();
195 for (i=0;i<size;i++){
196 MovePoint(i,dx,dy,dz);
201 // ----------------------------------------------------------------------------
207 int manualContourModel::GetIdPoint(double x, double y, double z, int i_range,int type)
209 double range = i_range+1;
211 double xx,yy,zz,dd,ddmin=9999999;
213 int i,size=_lstPoints.size();
214 for (i=0;i<size;i++){
215 manualPoint *mp=_lstPoints[i];
222 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) && (fabs(zz-z)<range)) {
223 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
232 if ((fabs(yy-y)<range) && (fabs(zz-z)<range)) {
233 dd=sqrt( (yy-y)*(yy-y) + (zz-z)*(zz-z) );
242 if ((fabs(xx-x)<range) && (fabs(zz-z)<range)) {
243 dd=sqrt( (xx-x)*(xx-x) + (zz-z)*(zz-z) );
252 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) ) {
253 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) );
263 // ----------------------------------------------------------------------------
264 manualPoint* manualContourModel::GetManualPoint(int id)
266 return _lstPoints[id];
268 // ----------------------------------------------------------------------------
269 int manualContourModel::GetSizeLstPoints()
271 return _lstPoints.size();
273 //----------------------------------------------------------------------------
274 int manualContourModel::GetNumberOfPointsSpline()
276 return _sizePointsContour;
278 //----------------------------------------------------------------------------
279 void manualContourModel::SetNumberOfPointsSpline(int size)
281 _sizePointsContour = size;
284 // ----------------------------------------------------------------------------
286 void manualContourModel::SetCloseContour(bool closeContour)
288 _closeContour = closeContour;
289 if (_closeContour==true)
291 _cntSplineX->ClosedOn();
292 _cntSplineY->ClosedOn();
293 _cntSplineZ->ClosedOn();
295 _cntSplineX->ClosedOff();
296 _cntSplineY->ClosedOff();
297 _cntSplineZ->ClosedOff();
301 // ----------------------------------------------------------------------------
302 bool manualContourModel::IfCloseContour()
304 return _closeContour;
307 // ----------------------------------------------------------------------------
309 void manualContourModel::UpdateSpline() // virtual
312 np = _lstPoints.size();
314 _cntSplineX->RemoveAllPoints();
315 _cntSplineY->RemoveAllPoints();
316 _cntSplineZ->RemoveAllPoints();
317 for( i = 0; i < np; i++ ) {
318 mp = GetManualPoint(i);
319 _cntSplineX->AddPoint( i, mp->GetX() );
320 _cntSplineY->AddPoint( i, mp->GetY() );
321 _cntSplineZ->AddPoint( i, mp->GetZ() );
324 //JSTG 25-02-08 ---------------------------------------------------------------------------------------------
325 if (this->_closeContour==true)
327 _delta_JSTG = (double) (np) / double (_sizePointsContour - 1); //Without the -1 the curve is not close
329 // _delta_JSTG = (double) (np-1) / double (_sizePointsContour ); //Without the -1 the curve is not close
330 //EED 9/7/2016 _delta_JSTG = (double) (np) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
331 _delta_JSTG = (double) (np-1) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
333 //-----------------------------------------------------------------------------------------------------------
336 //---------------------------------------------------------------------------------
338 /*void manualContourModel::GetSplineiPoint(int i, double &x, double &y, double &z)
340 double delta=(double)(_lstPoints.size()) / (double)(_sizePointsContour);
341 double t = delta*(double)i;
342 GetSplinePoint(t, x, y, z);
345 //-----------------------------------------------------------------------------
347 //JSTG 25-02-08 ---------------------------------------------------------------
348 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z) // virtal
350 GetSpline_t_Point(i*_delta_JSTG,x,y,z);
353 // ----------------------------------------------------------------------------
355 //JSTG 25-02-08 ---------------------------------------------------------------
356 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
358 if (_lstPoints.size()==0)
364 if (_lstPoints.size()==1)
367 mp = GetManualPoint(0);
372 if (_lstPoints.size()>=2)
374 *x = _cntSplineX->Evaluate(t);
375 *y = _cntSplineY->Evaluate(t);
376 *z = _cntSplineZ->Evaluate(t);
380 // ----------------------------------------------------------------------------
381 /*void manualContourModel::GetSplinePoint(double t, double &x, double &y, double &z)
383 if (_lstPoints.size()==0)
389 if (_lstPoints.size()==1)
392 mp = GetManualPoint(0);
397 if (_lstPoints.size()>=2)
399 x = _cntSplineX->Evaluate(t);
400 y = _cntSplineY->Evaluate(t);
401 z = _cntSplineZ->Evaluate(t);
405 // ----------------------------------------------------------------------------
406 double manualContourModel::GetPathSize(double *spc)
411 // JSTG 25-02-08 -----------------------------
415 //--------------------------------------------
416 if (_lstPoints.size()==2)
418 x1 = spc[0] * _lstPoints[0]->GetX();
419 y1 = spc[1] * _lstPoints[0]->GetY();
420 z1 = spc[2] * _lstPoints[0]->GetZ();
421 x2 = spc[0] * _lstPoints[1]->GetX();
422 y2 = spc[1] * _lstPoints[1]->GetY();
423 z2 = spc[2] * _lstPoints[1]->GetZ();
424 result = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
426 if (_lstPoints.size()>2)
428 // JSTG 25-02-08 ------------------------------------------
429 //np = _lstPoints.size( );
431 //delta=( double ) ( np ) / ( double ) ( nps );
433 //GetSplinePoint(0,x1,y1,z1);
434 GetSpline_i_Point(0,&x1,&y1,&z1);
438 //for( i = 1; i < nps; i++ )
439 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
441 //t = delta * (double)i;
442 //GetSplinePoint(t,x2,y2,z2);
443 GetSpline_i_Point(i,&x2,&y2,&z2);
447 //---------------------------------------------------------
448 result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
457 // ----------------------------------------------------------------------------
458 double manualContourModel::GetPathArea(double *spc)
461 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
464 //JSTG 25-02-08 ---------------------------------------------
466 //-----------------------------------------------------------
471 // This uses Green's theorem:
472 // A = 1/2 * sum( xiyi+1 - xi+1yi); pO == pN
473 // A < 0 -> A = |A| (a negative value could raise because points are
474 // given in clockwise order).
476 //JSTG 25-02-08 -------------------------------------------------
477 //int np = _lstPoints.size( );
479 int nps = GetNumberOfPointsSpline();
480 //double delta=( double ) ( np ) / ( double ) ( nps );
482 for( i = 0, area = 0.0; i < nps; i++ )
485 //ti = delta * (double)i;
486 //tj = delta * (double)j;
487 //GetSplinePoint(ti,x1,y1,z1);
488 //GetSplinePoint(tj,x2,y2,z2);
489 GetSpline_i_Point(i,&x1,&y1,&z1);
490 GetSpline_i_Point(j,&x2,&y2,&z2);
491 //----------------------------------------------------------------
498 area += (x1 * y2 ) - ( x2 * y1 );
508 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
510 j = ( i + 1 ) % _lstPoints.size();
513 (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
514 ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
515 if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
537 // ----------------------------------------------------------------------------
538 // p[x,y,z] : data in
539 // rp[x,y,z] : data out result point
540 // rn[x,y,z] : data out result normal
541 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
543 double distMin=999999999;
544 double dist,dx,dy,dz;
549 //JSTG 25-02-08 -------------------
553 //---------------------------------
555 np = _lstPoints.size( );
558 // JSTG 25-02-08 ------------------------------------------
560 nps = GetNumberOfPointsSpline();
561 //delta = ( double ) ( np ) / ( double ) ( nps );
563 //GetSplinePoint(0,x1,y1,z1);
564 GetSpline_i_Point(0,&x1,&y1,&z1);
565 for( i = 0; i < nps; i++ )
567 //t = delta * (double)i;
568 //GetSplinePoint(t,x1,y1,z1);
569 GetSpline_i_Point(i,&x1,&y1,&z1);
570 //----------------------------------------------------------
574 dist = sqrt( dx*dx + dy*dy + dz*dz );
591 // JSTG 25-02-08 ------------------------------------------
595 //t = delta * (double)1.0;
596 //GetSplinePoint(t,x1,y1,z1);
597 GetSpline_i_Point(i,&x1,&y1,&z1);
598 //----------------------------------------------------------
613 // ----------------------------------------------------------------------------
614 manualContourModel * manualContourModel::Clone() // virtual
616 manualContourModel * clone = new manualContourModel();
617 CopyAttributesTo(clone);
621 // ----------------------------------------------------------------------------
622 void manualContourModel::Open(FILE *ff) // virtual
626 int numberOfControlPoints;
629 fscanf(ff,"%s",tmp); // NumberOfControlPoints
630 fscanf(ff,"%s",tmp); // ##
631 numberOfControlPoints = atoi(tmp);
632 for (i=0;i<numberOfControlPoints;i++)
634 fscanf(ff,"%s",tmp); // X
636 fscanf(ff,"%s",tmp); // Y
638 fscanf(ff,"%s",tmp); // Z
644 // ----------------------------------------------------------------------------
645 int manualContourModel::GetTypeModel() //virtual
657 // ----------------------------------------------------------------------------
658 void manualContourModel::Save(FILE *ff) // virtual
660 int i,size=_lstPoints.size();
661 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
662 fprintf(ff,"NumberOfControlPoints %d\n",size);
665 manualPoint *mp=_lstPoints[i];
666 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
669 //CMRU 03-09-09-----------------------------------------------------------------------------------------------
670 void manualContourModel::SaveData(FILE *ff)
672 std::string etiqueta = GetLabel();
674 fprintf(ff,"Label: NO_LABEL\n");
676 fprintf(ff,"Label: %s\n",etiqueta.c_str());
677 fprintf(ff,"Real_Size: %f\n",GetRealSize());
680 void manualContourModel::OpenData(FILE *ff)
684 fscanf(ff,"%s",tmp); // Label:
685 fscanf(ff,"%s",tmp); // value
686 std::cout<<tmp<<std::endl;
687 //if(strcmp(tmp != "NO_LABEL") // JPR
688 if(strcmp(tmp,"NO_LABEL") != 0)
691 fscanf(ff,"%s",tmp); // Real_size
692 fscanf(ff,"%s",tmp);// #
694 //tmp.ToDouble(&tmp);
695 SetRealSize(atof(tmp));
698 //------------------------------------------------------------------------------------------------------------
700 // ---------------------------------------------------------------------------
702 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
705 //XXXX::CopyAttributesTo(cloneObject);
707 cloneObject->SetCloseContour( this->IfCloseContour() );
708 int i, size = GetSizeLstPoints();
709 for( i=0; i<size; i++ )
711 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
713 cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
714 cloneObject->SetCloseContour( _closeContour );
715 cloneObject->UpdateSpline();
719 // ---------------------------------------------------------------------------
720 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )//private
722 _lstPoints.push_back( theManualPoint );
725 std::vector<manualBaseModel*> manualContourModel::ExploseModel( )
727 std::vector<manualBaseModel*> lstTmp;
728 lstTmp.push_back(this);
733 // ----------------------------------------------------------------------------
734 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
738 int i, size = GetSizeLstPoints();
740 for( i=0; i<size; i++ )
742 mp = GetManualPoint( i );
744 mp->SetPointX( mp->GetX()*Ax + Bx );
745 mp->SetPointY( mp->GetY()*Ay + By );
750 //CMRU 17-08-09----------------------------------------------------------------------------
751 void manualContourModel::SetLabel(std::string newLabel)
756 void manualContourModel::SetRealSize(double newRealSize)
758 _realSize = newRealSize;
761 double manualContourModel::GetRealSize()
766 std::string manualContourModel::GetLabel()
770 //----------------------------------------------------------------------------