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 -------------------------------------------------------------------------------------------------
54 //---------------------------------------------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
58 manualContourModel::~manualContourModel()
60 int i,size=_lstPoints.size();
61 for (i=0;i<size; i++){
65 _cntSplineX->RemoveAllPoints();
66 _cntSplineY->RemoveAllPoints();
67 _cntSplineZ->RemoveAllPoints();
68 _cntSplineX->Delete();
69 _cntSplineY->Delete();
70 _cntSplineZ->Delete();
73 // ----------------------------------------------------------------------------
74 int manualContourModel::AddPoint(double x,double y,double z)
76 manualPoint *mp = new manualPoint();
80 return _lstPoints.size()-1;
82 // ----------------------------------------------------------------------------
83 int manualContourModel::InsertPoint(double x,double y,double z)
85 double dd,ddmin=9999999;
90 int i,ii,iii,size=_lstPoints.size();
91 double j,MaxDivisions=20,porcentage;
94 if (_closeContour==false)
101 for ( i=0 ; i<size ; i++ )
105 x1=_lstPoints[ii]->GetX();
106 y1=_lstPoints[ii]->GetY();
107 z1=_lstPoints[ii]->GetZ();
108 x2=_lstPoints[iii]->GetX();
109 y2=_lstPoints[iii]->GetY();
110 z2=_lstPoints[iii]->GetZ();
111 for (j=0; j<=MaxDivisions; j++)
113 porcentage=(j/MaxDivisions);
114 xx=(x2-x1)*porcentage+x1;
115 yy=(y2-y1)*porcentage+y1;
116 zz=(z2-z1)*porcentage+z1;
117 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
127 if (_closeContour==false)
129 if ( (ibak==1) && (jbak==0) )
133 if ( ( ibak==size ) && ( jbak==MaxDivisions ) )
140 //JSTG - 25-04-08 ----------------------------------------------------------
141 //manualPoint *mp = new manualPoint();
142 //mp->SetPoint(x,y,z);
143 //std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + ibak;
144 //_lstPoints.insert(itNum,mp);
145 InsertPoint_id(ibak,x,y,z);
146 //----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
151 void manualContourModel::InsertPoint_id(int id, double x, double y, double z)
153 manualPoint *mp = new manualPoint();
155 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + id;
156 _lstPoints.insert(itNum,mp);
158 // ----------------------------------------------------------------------------
160 void manualContourModel::DeletePoint(int i)
162 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + i;
163 _lstPoints.erase(itNum);
165 // ----------------------------------------------------------------------------
166 void manualContourModel::DeleteAllPoints()
168 int i,size=_lstPoints.size();
169 for (i=0;i<size;i++){
170 _lstPoints.erase( _lstPoints.begin() );
172 this->UpdateSpline();
174 // ----------------------------------------------------------------------------
176 void manualContourModel::MovePoint(int i,double dx,double dy,double dz)
178 printf("EED manualContourModel::MovePoint %p \n", this);
179 manualPoint *mp=_lstPoints[i];
180 double x=mp->GetX()+dx;
181 double y=mp->GetY()+dy;
182 double z=mp->GetZ()+dz;
185 // ----------------------------------------------------------------------------
186 void manualContourModel::MoveLstPoints(double dx,double dy,double dz)
190 // ----------------------------------------------------------------------------
191 void manualContourModel::MoveAllPoints(double dx,double dy,double dz)
193 int i,size=_lstPoints.size();
194 for (i=0;i<size;i++){
195 MovePoint(i,dx,dy,dz);
200 // ----------------------------------------------------------------------------
206 int manualContourModel::GetIdPoint(double x, double y, double z, int i_range,int type)
208 double range = i_range+1;
210 double xx,yy,zz,dd,ddmin=9999999;
212 int i,size=_lstPoints.size();
213 for (i=0;i<size;i++){
214 manualPoint *mp=_lstPoints[i];
221 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) && (fabs(zz-z)<range)) {
222 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
231 if ((fabs(yy-y)<range) && (fabs(zz-z)<range)) {
232 dd=sqrt( (yy-y)*(yy-y) + (zz-z)*(zz-z) );
241 if ((fabs(xx-x)<range) && (fabs(zz-z)<range)) {
242 dd=sqrt( (xx-x)*(xx-x) + (zz-z)*(zz-z) );
251 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) ) {
252 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) );
262 // ----------------------------------------------------------------------------
263 manualPoint* manualContourModel::GetManualPoint(int id)
265 return _lstPoints[id];
267 // ----------------------------------------------------------------------------
268 int manualContourModel::GetSizeLstPoints()
270 return _lstPoints.size();
272 //----------------------------------------------------------------------------
273 int manualContourModel::GetNumberOfPointsSpline()
275 return _sizePointsContour;
277 //----------------------------------------------------------------------------
278 void manualContourModel::SetNumberOfPointsSpline(int size)
280 _sizePointsContour = size;
283 // ----------------------------------------------------------------------------
285 void manualContourModel::SetCloseContour(bool closeContour)
287 _closeContour = closeContour;
288 if (_closeContour==true)
290 _cntSplineX->ClosedOn();
291 _cntSplineY->ClosedOn();
292 _cntSplineZ->ClosedOn();
294 _cntSplineX->ClosedOff();
295 _cntSplineY->ClosedOff();
296 _cntSplineZ->ClosedOff();
300 // ----------------------------------------------------------------------------
301 bool manualContourModel::IfCloseContour()
303 return _closeContour;
306 // ----------------------------------------------------------------------------
308 void manualContourModel::UpdateSpline() // virtual
311 np = _lstPoints.size();
313 _cntSplineX->RemoveAllPoints();
314 _cntSplineY->RemoveAllPoints();
315 _cntSplineZ->RemoveAllPoints();
316 for( i = 0; i < np; i++ ) {
317 mp = GetManualPoint(i);
318 _cntSplineX->AddPoint( i, mp->GetX() );
319 _cntSplineY->AddPoint( i, mp->GetY() );
320 _cntSplineZ->AddPoint( i, mp->GetZ() );
323 //JSTG 25-02-08 ---------------------------------------------------------------------------------------------
324 if (this->_closeContour==true)
326 _delta_JSTG = (double) (np) / double (_sizePointsContour - 1); //Without the -1 the curve is not close
328 // _delta_JSTG = (double) (np-1) / double (_sizePointsContour ); //Without the -1 the curve is not close
329 //EED 9/7/2016 _delta_JSTG = (double) (np) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
330 _delta_JSTG = (double) (np-1) / double (_sizePointsContour-1 ); //Without the -1 the curve is not close
332 //-----------------------------------------------------------------------------------------------------------
335 //---------------------------------------------------------------------------------
337 /*void manualContourModel::GetSplineiPoint(int i, double &x, double &y, double &z)
339 double delta=(double)(_lstPoints.size()) / (double)(_sizePointsContour);
340 double t = delta*(double)i;
341 GetSplinePoint(t, x, y, z);
344 //-----------------------------------------------------------------------------
346 //JSTG 25-02-08 ---------------------------------------------------------------
347 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z) // virtal
349 GetSpline_t_Point(i*_delta_JSTG,x,y,z);
352 // ----------------------------------------------------------------------------
354 //JSTG 25-02-08 ---------------------------------------------------------------
355 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
357 if (_lstPoints.size()==0)
363 if (_lstPoints.size()==1)
366 mp = GetManualPoint(0);
371 if (_lstPoints.size()>=2)
373 *x = _cntSplineX->Evaluate(t);
374 *y = _cntSplineY->Evaluate(t);
375 *z = _cntSplineZ->Evaluate(t);
379 // ----------------------------------------------------------------------------
380 /*void manualContourModel::GetSplinePoint(double t, double &x, double &y, double &z)
382 if (_lstPoints.size()==0)
388 if (_lstPoints.size()==1)
391 mp = GetManualPoint(0);
396 if (_lstPoints.size()>=2)
398 x = _cntSplineX->Evaluate(t);
399 y = _cntSplineY->Evaluate(t);
400 z = _cntSplineZ->Evaluate(t);
404 // ----------------------------------------------------------------------------
405 double manualContourModel::GetPathSize(double *spc)
410 // JSTG 25-02-08 -----------------------------
414 //--------------------------------------------
415 if (_lstPoints.size()==2)
417 x1 = spc[0] * _lstPoints[0]->GetX();
418 y1 = spc[1] * _lstPoints[0]->GetY();
419 z1 = spc[2] * _lstPoints[0]->GetZ();
420 x2 = spc[0] * _lstPoints[1]->GetX();
421 y2 = spc[1] * _lstPoints[1]->GetY();
422 z2 = spc[2] * _lstPoints[1]->GetZ();
423 result = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
425 if (_lstPoints.size()>2)
427 // JSTG 25-02-08 ------------------------------------------
428 //np = _lstPoints.size( );
430 //delta=( double ) ( np ) / ( double ) ( nps );
432 //GetSplinePoint(0,x1,y1,z1);
433 GetSpline_i_Point(0,&x1,&y1,&z1);
437 //for( i = 1; i < nps; i++ )
438 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
440 //t = delta * (double)i;
441 //GetSplinePoint(t,x2,y2,z2);
442 GetSpline_i_Point(i,&x2,&y2,&z2);
446 //---------------------------------------------------------
447 result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
456 // ----------------------------------------------------------------------------
457 double manualContourModel::GetPathArea(double *spc)
460 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
463 //JSTG 25-02-08 ---------------------------------------------
465 //-----------------------------------------------------------
470 // This uses Green's theorem:
471 // A = 1/2 * sum( xiyi+1 - xi+1yi); pO == pN
472 // A < 0 -> A = |A| (a negative value could raise because points are
473 // given in clockwise order).
475 //JSTG 25-02-08 -------------------------------------------------
476 //int np = _lstPoints.size( );
478 int nps = GetNumberOfPointsSpline();
479 //double delta=( double ) ( np ) / ( double ) ( nps );
481 for( i = 0, area = 0.0; i < nps; i++ )
484 //ti = delta * (double)i;
485 //tj = delta * (double)j;
486 //GetSplinePoint(ti,x1,y1,z1);
487 //GetSplinePoint(tj,x2,y2,z2);
488 GetSpline_i_Point(i,&x1,&y1,&z1);
489 GetSpline_i_Point(j,&x2,&y2,&z2);
490 //----------------------------------------------------------------
497 area += (x1 * y2 ) - ( x2 * y1 );
507 for( i = 0, area = 0.0; i < _lstPoints.size(); i++ )
509 j = ( i + 1 ) % _lstPoints.size();
512 (_lstPoints[i]->GetX() * _lstPoints[j]->GetY() ) -
513 ( _lstPoints[j]->GetX() * _lstPoints[i]->GetY() );
514 if (_lstPoints[0]->GetZ()!=_lstPoints[i]->GetZ())
536 // ----------------------------------------------------------------------------
537 // p[x,y,z] : data in
538 // rp[x,y,z] : data out result point
539 // rn[x,y,z] : data out result normal
540 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
542 double distMin=999999999;
543 double dist,dx,dy,dz;
548 //JSTG 25-02-08 -------------------
552 //---------------------------------
554 np = _lstPoints.size( );
557 // JSTG 25-02-08 ------------------------------------------
559 nps = GetNumberOfPointsSpline();
560 //delta = ( double ) ( np ) / ( double ) ( nps );
562 //GetSplinePoint(0,x1,y1,z1);
563 GetSpline_i_Point(0,&x1,&y1,&z1);
564 for( i = 0; i < nps; i++ )
566 //t = delta * (double)i;
567 //GetSplinePoint(t,x1,y1,z1);
568 GetSpline_i_Point(i,&x1,&y1,&z1);
569 //----------------------------------------------------------
573 dist = sqrt( dx*dx + dy*dy + dz*dz );
590 // JSTG 25-02-08 ------------------------------------------
594 //t = delta * (double)1.0;
595 //GetSplinePoint(t,x1,y1,z1);
596 GetSpline_i_Point(i,&x1,&y1,&z1);
597 //----------------------------------------------------------
612 // ----------------------------------------------------------------------------
613 manualContourModel * manualContourModel::Clone() // virtual
615 manualContourModel * clone = new manualContourModel();
616 CopyAttributesTo(clone);
620 // ----------------------------------------------------------------------------
621 void manualContourModel::Open(FILE *ff) // virtual
625 int numberOfControlPoints;
628 fscanf(ff,"%s",tmp); // NumberOfControlPoints
629 fscanf(ff,"%s",tmp); // ##
630 numberOfControlPoints = atoi(tmp);
631 for (i=0;i<numberOfControlPoints;i++)
633 fscanf(ff,"%s",tmp); // X
635 fscanf(ff,"%s",tmp); // Y
637 fscanf(ff,"%s",tmp); // Z
643 // ----------------------------------------------------------------------------
644 int manualContourModel::GetTypeModel() //virtual
656 // ----------------------------------------------------------------------------
657 void manualContourModel::Save(FILE *ff) // virtual
659 int i,size=_lstPoints.size();
660 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
661 fprintf(ff,"NumberOfControlPoints %d\n",size);
664 manualPoint *mp=_lstPoints[i];
665 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
668 //CMRU 03-09-09-----------------------------------------------------------------------------------------------
669 void manualContourModel::SaveData(FILE *ff)
671 std::string etiqueta = GetLabel();
674 fprintf(ff,"Label: NO_LABEL\n");
676 fprintf(ff,"Label: %s\n",etiqueta.c_str());
678 fprintf(ff,"Real_Size: %f\n",GetRealSize());
681 void manualContourModel::OpenData(FILE *ff)
685 fscanf(ff,"%s",tmp); // Label:
686 fscanf(ff,"%s",tmp); // value
687 std::cout<<tmp<<std::endl;
688 //if(strcmp(tmp != "NO_LABEL") // JPR
689 if(strcmp(tmp,"NO_LABEL") != 0)
693 fscanf(ff,"%s",tmp); // Real_size
694 fscanf(ff,"%s",tmp);// #
696 //tmp.ToDouble(&tmp);
697 SetRealSize(atof(tmp));
700 //------------------------------------------------------------------------------------------------------------
702 // ---------------------------------------------------------------------------
704 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
707 //XXXX::CopyAttributesTo(cloneObject);
709 cloneObject->SetCloseContour( this->IfCloseContour() );
710 int i, size = GetSizeLstPoints();
711 for( i=0; i<size; i++ )
713 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
715 cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
716 cloneObject->SetCloseContour( _closeContour );
717 cloneObject->UpdateSpline();
721 // ---------------------------------------------------------------------------
722 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )//private
724 _lstPoints.push_back( theManualPoint );
727 std::vector<manualBaseModel*> manualContourModel::ExploseModel( )
729 std::vector<manualBaseModel*> lstTmp;
730 lstTmp.push_back(this);
735 // ----------------------------------------------------------------------------
736 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
740 int i, size = GetSizeLstPoints();
742 for( i=0; i<size; i++ )
744 mp = GetManualPoint( i );
746 mp->SetPointX( mp->GetX()*Ax + Bx );
747 mp->SetPointY( mp->GetY()*Ay + By );
752 //CMRU 17-08-09----------------------------------------------------------------------------
753 void manualContourModel::SetRealSize(double newRealSize)
755 _realSize = newRealSize;
758 double manualContourModel::GetRealSize()
763 //----------------------------------------------------------------------------