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(double *spc)
423 // JSTG 25-02-08 -----------------------------
427 //--------------------------------------------
429 if (_lstPoints.size()==2)
431 x1 = spc[0] * _lstPoints[0]->GetX();
432 y1 = spc[1] * _lstPoints[0]->GetY();
433 z1 = spc[2] * _lstPoints[0]->GetZ();
434 x2 = spc[0] * _lstPoints[1]->GetX();
435 y2 = spc[1] * _lstPoints[1]->GetY();
436 z2 = spc[2] * _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);
453 //for( i = 1; i < nps; i++ )
454 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
456 //t = delta * (double)i;
457 //GetSplinePoint(t,x2,y2,z2);
458 GetSpline_i_Point(i,&x2,&y2,&z2);
462 //---------------------------------------------------------
463 result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
474 // ----------------------------------------------------------------------------
475 double manualContourModel::GetPathArea(double *spc)
478 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
481 //JSTG 25-02-08 ---------------------------------------------
483 //-----------------------------------------------------------
489 // This uses Green's theorem:
490 // A = 1/2 * sum( xiyi+1 - xi+1yi); pO == pN
491 // A < 0 -> A = |A| (a negative value could raise because points are
492 // given in clockwise order).
494 //JSTG 25-02-08 -------------------------------------------------
495 //int np = _lstPoints.size( );
497 int nps = GetNumberOfPointsSpline();
498 //double delta=( double ) ( np ) / ( double ) ( nps );
500 for( i = 0, area = 0.0; i < nps; i++ )
503 //ti = delta * (double)i;
504 //tj = delta * (double)j;
505 //GetSplinePoint(ti,x1,y1,z1);
506 //GetSplinePoint(tj,x2,y2,z2);
507 GetSpline_i_Point(i,&x1,&y1,&z1);
508 GetSpline_i_Point(j,&x2,&y2,&z2);
509 //----------------------------------------------------------------
528 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
530 j = ( i + 1 ) % _lstPoints.size();
533 (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
534 ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
535 if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
557 // ----------------------------------------------------------------------------
558 // p[x,y,z] : data in
559 // rp[x,y,z] : data out result point
560 // rn[x,y,z] : data out result normal
562 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
564 double distMin=999999999;
565 double dist,dx,dy,dz;
570 //JSTG 25-02-08 -------------------
574 //---------------------------------
576 np = _lstPoints.size( );
579 // JSTG 25-02-08 ------------------------------------------
581 nps = GetNumberOfPointsSpline();
582 //delta = ( double ) ( np ) / ( double ) ( nps );
584 //GetSplinePoint(0,x1,y1,z1);
585 GetSpline_i_Point(0,&x1,&y1,&z1);
586 for( i = 0; i < nps; i++ )
588 //t = delta * (double)i;
589 //GetSplinePoint(t,x1,y1,z1);
590 GetSpline_i_Point(i,&x1,&y1,&z1);
591 //----------------------------------------------------------
595 dist = sqrt( dx*dx + dy*dy + dz*dz );
613 // JSTG 25-02-08 ------------------------------------------
617 //t = delta * (double)1.0;
618 //GetSplinePoint(t,x1,y1,z1);
619 GetSpline_i_Point(i,&x1,&y1,&z1);
620 //----------------------------------------------------------
635 // ----------------------------------------------------------------------------
636 manualContourModel * manualContourModel :: Clone() // virtual
638 manualContourModel * clone = new manualContourModel();
639 CopyAttributesTo(clone);
643 // ----------------------------------------------------------------------------
644 void manualContourModel::Open(FILE *ff) // virtual
648 int numberOfControlPoints;
651 fscanf(ff,"%s",tmp); // NumberOfControlPoints
652 fscanf(ff,"%s",tmp); // ##
653 numberOfControlPoints = atoi(tmp);
654 for (i=0;i<numberOfControlPoints;i++)
656 fscanf(ff,"%s",tmp); // X
658 fscanf(ff,"%s",tmp); // Y
660 fscanf(ff,"%s",tmp); // Z
666 // ----------------------------------------------------------------------------
667 int manualContourModel::GetTypeModel() //virtual
679 // ----------------------------------------------------------------------------
680 void manualContourModel::Save(FILE *ff) // virtual
682 int i,size=_lstPoints.size();
683 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
684 fprintf(ff,"NumberOfControlPoints %d\n",size);
687 manualPoint *mp=_lstPoints[i];
688 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
691 //CMRU 03-09-09-----------------------------------------------------------------------------------------------
692 void manualContourModel::SaveData(FILE *ff)
694 std::string etiqueta = GetLabel();
696 fprintf(ff,"Label: NO_LABEL\n");
698 fprintf(ff,"Label: %s\n",etiqueta.c_str());
699 fprintf(ff,"Real_Size: %f\n",GetRealSize());
702 void manualContourModel::OpenData(FILE *ff)
706 fscanf(ff,"%s",tmp); // Label:
707 fscanf(ff,"%s",tmp); // value
708 std::cout<<tmp<<std::endl;
709 //if(strcmp(tmp != "NO_LABEL") // JPR
710 if(strcmp(tmp,"NO_LABEL") != 0)
713 fscanf(ff,"%s",tmp); // Real_size
714 fscanf(ff,"%s",tmp);// #
716 //tmp.ToDouble(&tmp);
717 SetRealSize(atof(tmp));
720 //------------------------------------------------------------------------------------------------------------
722 // ---------------------------------------------------------------------------
724 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
727 //XXXX::CopyAttributesTo(cloneObject);
729 cloneObject->SetCloseContour( this->IfCloseContour() );
730 int i, size = GetSizeLstPoints();
731 for( i=0; i<size; i++ )
733 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
735 cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
736 cloneObject->SetCloseContour( _closeContour );
737 cloneObject->UpdateSpline();
741 // ---------------------------------------------------------------------------
742 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )//private
744 _lstPoints.push_back( theManualPoint );
747 std::vector<manualBaseModel*> manualContourModel::ExploseModel( )
749 std::vector<manualBaseModel*> lstTmp;
750 lstTmp.push_back(this);
755 // ----------------------------------------------------------------------------
756 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
760 int i, size = GetSizeLstPoints();
762 for( i=0; i<size; i++ )
764 mp = GetManualPoint( i );
766 mp->SetPointX( mp->GetX()*Ax + Bx );
767 mp->SetPointY( mp->GetY()*Ay + By );
772 //CMRU 17-08-09----------------------------------------------------------------------------
773 void manualContourModel::SetLabel(std::string newLabel)
778 void manualContourModel::SetRealSize(double newRealSize)
780 _realSize = newRealSize;
783 double manualContourModel::GetRealSize()
788 std::string manualContourModel::GetLabel()
792 //----------------------------------------------------------------------------