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 //EED 9/7/2016 _delta_JSTG = (double) (np) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
341 _delta_JSTG = (double) (np-1) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
343 //-----------------------------------------------------------------------------------------------------------
346 //---------------------------------------------------------------------------------
348 /*void manualContourModel::GetSplineiPoint(int i, double &x, double &y, double &z)
350 double delta=(double)(_lstPoints.size()) / (double)(_sizePointsContour);
351 double t = delta*(double)i;
352 GetSplinePoint(t, x, y, z);
355 //-----------------------------------------------------------------------------
357 //JSTG 25-02-08 ---------------------------------------------------------------
358 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z) // virtal
360 GetSpline_t_Point(i*_delta_JSTG,x,y,z);
363 // ----------------------------------------------------------------------------
365 //JSTG 25-02-08 ---------------------------------------------------------------
366 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
368 if (_lstPoints.size()==0)
374 if (_lstPoints.size()==1)
377 mp = GetManualPoint(0);
382 if (_lstPoints.size()>=2)
384 *x = _cntSplineX->Evaluate(t);
385 *y = _cntSplineY->Evaluate(t);
386 *z = _cntSplineZ->Evaluate(t);
390 // ----------------------------------------------------------------------------
393 /*void manualContourModel::GetSplinePoint(double t, double &x, double &y, double &z)
395 if (_lstPoints.size()==0)
401 if (_lstPoints.size()==1)
404 mp = GetManualPoint(0);
409 if (_lstPoints.size()>=2)
411 x = _cntSplineX->Evaluate(t);
412 y = _cntSplineY->Evaluate(t);
413 z = _cntSplineZ->Evaluate(t);
416 // ----------------------------------------------------------------------------
417 double manualContourModel::GetPathSize()
423 // JSTG 25-02-08 -----------------------------
427 //--------------------------------------------
429 if (_lstPoints.size()==2)
431 x1=_lstPoints[0]->GetX();
432 y1=_lstPoints[0]->GetY();
433 z1=_lstPoints[0]->GetZ();
434 x2=_lstPoints[1]->GetX();
435 y2=_lstPoints[1]->GetY();
436 z2=_lstPoints[1]->GetZ();
437 result = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
439 if (_lstPoints.size()>2)
442 // JSTG 25-02-08 ------------------------------------------
443 //np = _lstPoints.size( );
445 //delta=( double ) ( np ) / ( double ) ( nps );
447 //GetSplinePoint(0,x1,y1,z1);
448 GetSpline_i_Point(0,&x1,&y1,&z1);
450 //for( i = 1; i < nps; i++ )
451 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
453 //t = delta * (double)i;
454 //GetSplinePoint(t,x2,y2,z2);
455 GetSpline_i_Point(i,&x2,&y2,&z2);
456 //---------------------------------------------------------
457 result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
466 // ----------------------------------------------------------------------------
467 double manualContourModel::GetPathArea()
470 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
473 //JSTG 25-02-08 ---------------------------------------------
475 //-----------------------------------------------------------
481 // This uses Green's theorem:
482 // A = 1/2 * sum( xiyi+1 - xi+1yi); pO == pN
483 // A < 0 -> A = |A| (a negative value could raise because points are
484 // given in clockwise order).
486 //JSTG 25-02-08 -------------------------------------------------
487 //int np = _lstPoints.size( );
489 int nps = GetNumberOfPointsSpline();
490 //double delta=( double ) ( np ) / ( double ) ( nps );
492 for( i = 0, area = 0.0; i < nps; i++ )
495 //ti = delta * (double)i;
496 //tj = delta * (double)j;
497 //GetSplinePoint(ti,x1,y1,z1);
498 //GetSplinePoint(tj,x2,y2,z2);
499 GetSpline_i_Point(i,&x1,&y1,&z1);
500 GetSpline_i_Point(j,&x2,&y2,&z2);
501 //----------------------------------------------------------------
514 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
516 j = ( i + 1 ) % _lstPoints.size();
519 (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
520 ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
521 if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
543 // ----------------------------------------------------------------------------
544 // p[x,y,z] : data in
545 // rp[x,y,z] : data out result point
546 // rn[x,y,z] : data out result normal
548 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
550 double distMin=999999999;
551 double dist,dx,dy,dz;
556 //JSTG 25-02-08 -------------------
560 //---------------------------------
562 np = _lstPoints.size( );
565 // JSTG 25-02-08 ------------------------------------------
567 nps = GetNumberOfPointsSpline();
568 //delta = ( double ) ( np ) / ( double ) ( nps );
570 //GetSplinePoint(0,x1,y1,z1);
571 GetSpline_i_Point(0,&x1,&y1,&z1);
572 for( i = 0; i < nps; i++ )
574 //t = delta * (double)i;
575 //GetSplinePoint(t,x1,y1,z1);
576 GetSpline_i_Point(i,&x1,&y1,&z1);
577 //----------------------------------------------------------
581 dist = sqrt( dx*dx + dy*dy + dz*dz );
599 // JSTG 25-02-08 ------------------------------------------
603 //t = delta * (double)1.0;
604 //GetSplinePoint(t,x1,y1,z1);
605 GetSpline_i_Point(i,&x1,&y1,&z1);
606 //----------------------------------------------------------
623 // ----------------------------------------------------------------------------
624 manualContourModel * manualContourModel :: Clone() // virtual
626 manualContourModel * clone = new manualContourModel();
627 CopyAttributesTo(clone);
631 // ----------------------------------------------------------------------------
632 void manualContourModel::Open(FILE *ff) // virtual
636 int numberOfControlPoints;
639 fscanf(ff,"%s",tmp); // NumberOfControlPoints
640 fscanf(ff,"%s",tmp); // ##
641 numberOfControlPoints = atoi(tmp);
642 for (i=0;i<numberOfControlPoints;i++)
644 fscanf(ff,"%s",tmp); // X
646 fscanf(ff,"%s",tmp); // Y
648 fscanf(ff,"%s",tmp); // Z
654 // ----------------------------------------------------------------------------
655 int manualContourModel::GetTypeModel() //virtual
667 // ----------------------------------------------------------------------------
668 void manualContourModel::Save(FILE *ff) // virtual
670 int i,size=_lstPoints.size();
671 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
672 fprintf(ff,"NumberOfControlPoints %d\n",size);
675 manualPoint *mp=_lstPoints[i];
676 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
679 //CMRU 03-09-09-----------------------------------------------------------------------------------------------
680 void manualContourModel::SaveData(FILE *ff)
682 std::string etiqueta = GetLabel();
684 fprintf(ff,"Label: NO_LABEL\n");
686 fprintf(ff,"Label: %s\n",etiqueta.c_str());
687 fprintf(ff,"Real_Size: %f\n",GetRealSize());
690 void manualContourModel::OpenData(FILE *ff)
694 fscanf(ff,"%s",tmp); // Label:
695 fscanf(ff,"%s",tmp); // value
696 std::cout<<tmp<<std::endl;
697 //if(strcmp(tmp != "NO_LABEL") // JPR
698 if(strcmp(tmp,"NO_LABEL") != 0)
701 fscanf(ff,"%s",tmp); // Real_size
702 fscanf(ff,"%s",tmp);// #
704 //tmp.ToDouble(&tmp);
705 SetRealSize(atof(tmp));
708 //------------------------------------------------------------------------------------------------------------
710 // ---------------------------------------------------------------------------
712 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
715 //XXXX::CopyAttributesTo(cloneObject);
717 cloneObject->SetCloseContour( this->IfCloseContour() );
718 int i, size = GetSizeLstPoints();
719 for( i=0; i<size; i++ )
721 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
723 cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
724 cloneObject->SetCloseContour( _closeContour );
725 cloneObject->UpdateSpline();
729 // ---------------------------------------------------------------------------
730 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )//private
732 _lstPoints.push_back( theManualPoint );
735 std::vector<manualBaseModel*> manualContourModel::ExploseModel( )
737 std::vector<manualBaseModel*> lstTmp;
738 lstTmp.push_back(this);
743 // ----------------------------------------------------------------------------
744 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
748 int i, size = GetSizeLstPoints();
750 for( i=0; i<size; i++ )
752 mp = GetManualPoint( i );
754 mp->SetPointX( mp->GetX()*Ax + Bx );
755 mp->SetPointY( mp->GetY()*Ay + By );
760 //CMRU 17-08-09----------------------------------------------------------------------------
761 void manualContourModel::SetLabel(std::string newLabel)
766 void manualContourModel::SetRealSize(double newRealSize)
768 _realSize = newRealSize;
771 double manualContourModel::GetRealSize()
776 std::string manualContourModel::GetLabel()
780 //----------------------------------------------------------------------------