00001 #include "manualContourModel.h"
00002
00003
00004
00005
00006
00007 manualContourModel::manualContourModel()
00008 {
00009 _cntSplineX = vtkKochanekSpline::New( );
00010 _cntSplineY = vtkKochanekSpline::New( );
00011 _cntSplineZ = vtkKochanekSpline::New( );
00012
00013 this->SetCloseContour(true);
00014
00015 _cntSplineX->SetDefaultTension( 0 );
00016 _cntSplineX->SetDefaultBias( 0 );
00017 _cntSplineX->SetDefaultContinuity( 0 );
00018
00019 _cntSplineY->SetDefaultTension( 0 );
00020 _cntSplineY->SetDefaultBias( 0 );
00021 _cntSplineY->SetDefaultContinuity( 0 );
00022
00023 _cntSplineZ->SetDefaultTension( 0 );
00024 _cntSplineZ->SetDefaultBias( 0 );
00025 _cntSplineZ->SetDefaultContinuity( 0 );
00026
00027
00028
00029
00030 _sizePointsContour = 100;
00031
00032 _delta_JSTG = 0.0;
00033
00034
00035
00036 _realSize = 0.0;
00037 _label = "";
00038
00039
00040 }
00041
00042
00043 manualContourModel::~manualContourModel()
00044 {
00045 int i,size=_lstPoints.size();
00046 for (i=0;i<size; i++){
00047 delete _lstPoints[i];
00048 }
00049 _lstPoints.clear();
00050
00051 _cntSplineX->RemoveAllPoints();
00052 _cntSplineY->RemoveAllPoints();
00053 _cntSplineZ->RemoveAllPoints();
00054
00055 _cntSplineX->Delete();
00056 _cntSplineY->Delete();
00057 _cntSplineZ->Delete();
00058 }
00059
00060 int manualContourModel::AddPoint(double x,double y,double z)
00061 {
00062 manualPoint *mp = new manualPoint();
00063 mp->SetPoint(x,y,z);
00064 AddManualPoint(mp);
00065
00066
00067 return _lstPoints.size()-1;
00068 }
00069
00070 int manualContourModel::InsertPoint(double x,double y,double z)
00071 {
00072 double dd,ddmin=9999999;
00073 int ibak=0;
00074 double xx,x1,x2;
00075 double yy,y1,y2;
00076 double zz,z1,z2;
00077 int i,ii,iii,size=_lstPoints.size();
00078 double j,MaxDivisions=20,porcentage;
00079 int sizeB=size;
00080
00081 if (_closeContour==false)
00082 {
00083 size=size-1;
00084 }
00085
00086 double jbak;
00087
00088 for ( i=0 ; i<size ; i++ )
00089 {
00090 ii=i % sizeB ;
00091 iii=(i+1) % sizeB;
00092 x1=_lstPoints[ii]->GetX();
00093 y1=_lstPoints[ii]->GetY();
00094 z1=_lstPoints[ii]->GetZ();
00095 x2=_lstPoints[iii]->GetX();
00096 y2=_lstPoints[iii]->GetY();
00097 z2=_lstPoints[iii]->GetZ();
00098 for (j=0; j<=MaxDivisions; j++)
00099 {
00100 porcentage=(j/MaxDivisions);
00101 xx=(x2-x1)*porcentage+x1;
00102 yy=(y2-y1)*porcentage+y1;
00103 zz=(z2-z1)*porcentage+z1;
00104 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
00105 if ( dd<ddmin )
00106 {
00107 ddmin=dd;
00108 ibak=iii;
00109 jbak=j;
00110 }
00111 }
00112 }
00113
00114 if (_closeContour==false)
00115 {
00116 if ( (ibak==1) && (jbak==0) )
00117 {
00118 ibak=0;
00119 }
00120 if ( ( ibak==size ) && ( jbak==MaxDivisions ) )
00121 {
00122 ibak=sizeB;
00123 }
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 InsertPoint_id(ibak,x,y,z);
00133
00134
00135 return ibak;
00136 }
00137
00138 void manualContourModel::InsertPoint_id(int id, double x, double y, double z)
00139 {
00140 manualPoint *mp = new manualPoint();
00141 mp->SetPoint(x,y,z);
00142 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + id;
00143 _lstPoints.insert(itNum,mp);
00144 }
00145
00146
00147 void manualContourModel::DeletePoint(int i)
00148 {
00149 std::vector<manualPoint*>::iterator itNum = _lstPoints.begin() + i;
00150 _lstPoints.erase(itNum);
00151 }
00152
00153 void manualContourModel::DeleteAllPoints()
00154 {
00155 int i,size=_lstPoints.size();
00156 for (i=0;i<size;i++){
00157 _lstPoints.erase( _lstPoints.begin() );
00158 }
00159 this->UpdateSpline();
00160 }
00161
00162
00163 void manualContourModel::MovePoint(int i,double dx,double dy,double dz)
00164 {
00165 manualPoint *mp=_lstPoints[i];
00166 double x=mp->GetX()+dx;
00167 double y=mp->GetY()+dy;
00168 double z=mp->GetZ()+dz;
00169 mp->SetPoint(x,y,z);
00170 }
00171
00172 void manualContourModel::MoveLstPoints(double dx,double dy,double dz)
00173 {
00174
00175 }
00176
00177 void manualContourModel::MoveAllPoints(double dx,double dy,double dz)
00178 {
00179 int i,size=_lstPoints.size();
00180 for (i=0;i<size;i++){
00181 MovePoint(i,dx,dy,dz);
00182 }
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192 int manualContourModel::GetIdPoint(double x, double y, double z, int i_range,int type)
00193 {
00194 double range = i_range+1;
00195
00196 double xx,yy,zz,dd,ddmin=9999999;
00197 int ibak=-1;
00198 int i,size=_lstPoints.size();
00199 for (i=0;i<size;i++){
00200 manualPoint *mp=_lstPoints[i];
00201 xx=mp->GetX();
00202 yy=mp->GetY();
00203 zz=mp->GetZ();
00204
00205 if (type==-1)
00206 {
00207 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) && (fabs(zz-z)<range)) {
00208 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) + (zz-z)*(zz-z) );
00209 if (dd<ddmin){
00210 ddmin=dd;
00211 ibak=i;
00212 }
00213 }
00214 }
00215 if (type==0)
00216 {
00217 if ((fabs(yy-y)<range) && (fabs(zz-z)<range)) {
00218 dd=sqrt( (yy-y)*(yy-y) + (zz-z)*(zz-z) );
00219 if (dd<ddmin){
00220 ddmin=dd;
00221 ibak=i;
00222 }
00223 }
00224 }
00225 if (type==1)
00226 {
00227 if ((fabs(xx-x)<range) && (fabs(zz-z)<range)) {
00228 dd=sqrt( (xx-x)*(xx-x) + (zz-z)*(zz-z) );
00229 if (dd<ddmin){
00230 ddmin=dd;
00231 ibak=i;
00232 }
00233 }
00234 }
00235 if (type==2)
00236 {
00237 if ((fabs(xx-x)<range) && (fabs(yy-y)<range) ) {
00238 dd=sqrt( (xx-x)*(xx-x) + (yy-y)*(yy-y) );
00239 if (dd<ddmin){
00240 ddmin=dd;
00241 ibak=i;
00242 }
00243 }
00244 }
00245 }
00246 return ibak;
00247 }
00248
00249 manualPoint* manualContourModel::GetManualPoint(int id)
00250 {
00251 return _lstPoints[id];
00252 }
00253
00254 int manualContourModel::GetSizeLstPoints()
00255 {
00256 return _lstPoints.size();
00257 }
00258
00259 int manualContourModel::GetNumberOfPointsSpline()
00260 {
00261 return _sizePointsContour;
00262 }
00263
00264 void manualContourModel::SetNumberOfPointsSpline(int size)
00265 {
00266 _sizePointsContour = size;
00267 }
00268
00269
00270
00271 void manualContourModel::SetCloseContour(bool closeContour)
00272 {
00273 _closeContour = closeContour;
00274 if (_closeContour==true)
00275 {
00276 _cntSplineX->ClosedOn();
00277 _cntSplineY->ClosedOn();
00278 _cntSplineZ->ClosedOn();
00279 } else {
00280 _cntSplineX->ClosedOff();
00281 _cntSplineY->ClosedOff();
00282 _cntSplineZ->ClosedOff();
00283 }
00284 }
00285
00286
00287 bool manualContourModel::IfCloseContour()
00288 {
00289 return _closeContour;
00290 }
00291
00292
00293
00294 void manualContourModel::UpdateSpline()
00295 {
00296 int i, np;
00297 np = _lstPoints.size();
00298 manualPoint *mp;
00299 _cntSplineX->RemoveAllPoints();
00300 _cntSplineY->RemoveAllPoints();
00301 _cntSplineZ->RemoveAllPoints();
00302 for( i = 0; i < np; i++ ) {
00303 mp = GetManualPoint(i);
00304 _cntSplineX->AddPoint( i, mp->GetX() );
00305 _cntSplineY->AddPoint( i, mp->GetY() );
00306 _cntSplineZ->AddPoint( i, mp->GetZ() );
00307 }
00308
00309
00310 if (this->_closeContour==true)
00311 {
00312 _delta_JSTG = (double) (np) / double (_sizePointsContour - 1);
00313 } else {
00314
00315 _delta_JSTG = (double) (np) / double (_sizePointsContour-1 );
00316 }
00317
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 void manualContourModel::GetSpline_i_Point(int i, double *x, double *y, double *z)
00333 {
00334 GetSpline_t_Point(i*_delta_JSTG,x,y,z);
00335 }
00336
00337
00338
00339
00340 void manualContourModel::GetSpline_t_Point(double t, double *x, double *y, double *z)
00341 {
00342 if (_lstPoints.size()==0)
00343 {
00344 *x = 0;
00345 *y = 0;
00346 *z = 0;
00347 }
00348 if (_lstPoints.size()==1)
00349 {
00350 manualPoint *mp;
00351 mp = GetManualPoint(0);
00352 *x = mp->GetX();
00353 *y = mp->GetY();
00354 *z = mp->GetZ();
00355 }
00356 if (_lstPoints.size()>=2)
00357 {
00358 *x = _cntSplineX->Evaluate(t);
00359 *y = _cntSplineY->Evaluate(t);
00360 *z = _cntSplineZ->Evaluate(t);
00361 }
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 double manualContourModel::GetPathSize()
00392 {
00393 double result = 0;
00394 double x1,y1,z1;
00395 double x2,y2,z2;
00396
00397
00398
00399
00400 int i;
00401
00402
00403 if (_lstPoints.size()==2)
00404 {
00405 x1=_lstPoints[0]->GetX();
00406 y1=_lstPoints[0]->GetY();
00407 z1=_lstPoints[0]->GetZ();
00408 x2=_lstPoints[1]->GetX();
00409 y2=_lstPoints[1]->GetY();
00410 z2=_lstPoints[1]->GetZ();
00411 result = sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
00412 }
00413 if (_lstPoints.size()>2)
00414 {
00415
00416
00417
00418
00419
00420 UpdateSpline();
00421
00422 GetSpline_i_Point(0,&x1,&y1,&z1);
00423
00424
00425 for( i = 1; i < GetNumberOfPointsSpline(); i++ )
00426 {
00427
00428
00429 GetSpline_i_Point(i,&x2,&y2,&z2);
00430
00431 result=result + sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
00432 x1=x2;
00433 y1=y2;
00434 z1=z2;
00435 }
00436 }
00437
00438 return result;
00439 }
00440
00441 double manualContourModel::GetPathArea()
00442 {
00443 double result = 555;
00444 if ((_lstPoints.size()>=3) && IfCloseContour()==true )
00445 {
00446 double area;
00447
00448
00449
00450 double x1,y1,z1;
00451 double x2,y2,z2;
00452 bool okArea=true;
00453 int i, j;
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463 int nps = GetNumberOfPointsSpline();
00464
00465 UpdateSpline();
00466 for( i = 0, area = 0.0; i < nps; i++ )
00467 {
00468 j = ( i + 1 ) % nps;
00469
00470
00471
00472
00473 GetSpline_i_Point(i,&x1,&y1,&z1);
00474 GetSpline_i_Point(j,&x2,&y2,&z2);
00475
00476 area +=
00477 (x1 * y2 ) -
00478 ( x2 * y1 );
00479 if (z1!=z2)
00480 {
00481 okArea=false;
00482 }
00483 }
00484 area /= 2.0;
00485 area = fabs( area );
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 if (okArea==true)
00505 {
00506 result = area;
00507 } else {
00508 result = -1;
00509 }
00510
00511 } else {
00512 result = 0;
00513 }
00514 return result;
00515 }
00516
00517
00518
00519
00520
00521
00522 void manualContourModel::GetNearestPointAndNormal(double *p, double *rp, double *rn)
00523 {
00524 double distMin=999999999;
00525 double dist,dx,dy,dz;
00526 double x1,y1,z1;
00527 double x2,y2,z2;
00528 int i,np,nps;
00529
00530
00531
00532 int iback;
00533
00534
00535
00536 np = _lstPoints.size( );
00537 if (np>=2)
00538 {
00539
00540
00541 nps = GetNumberOfPointsSpline();
00542
00543 UpdateSpline();
00544
00545 GetSpline_i_Point(0,&x1,&y1,&z1);
00546 for( i = 0; i < nps; i++ )
00547 {
00548
00549
00550 GetSpline_i_Point(i,&x1,&y1,&z1);
00551
00552 dx= x1-p[0];
00553 dy= y1-p[1];
00554 dz= z1-p[2];
00555 dist = sqrt( dx*dx + dy*dy + dz*dz );
00556 if (dist<distMin)
00557 {
00558 distMin = dist;
00559
00560 iback = i;
00561 rp[0] = x1;
00562 rp[1] = y1;
00563 rp[2] = z1;
00564 rn[0] = x2-x1;
00565 rn[1] = y2-y1;
00566 rn[2] = z2-z1;
00567 }
00568 x2=x1;
00569 y2=y1;
00570 z2=z1;
00571 }
00572
00573
00574
00575 if (iback==0)
00576 {
00577
00578
00579 GetSpline_i_Point(i,&x1,&y1,&z1);
00580
00581 rn[0]=rp[0]-x1;
00582 rn[1]=rp[1]-y1;
00583 rn[2]=rp[2]-z1;
00584 }
00585 }
00586 else
00587 {
00588 rp[0] = 0;
00589 rp[1] = 0;
00590 rp[2] = 0;
00591 rn[0] = -1;
00592 rn[1] = 0;
00593 rn[2] = 0;
00594 }
00595 }
00596
00597
00598 manualContourModel * manualContourModel :: Clone()
00599 {
00600 manualContourModel * clone = new manualContourModel();
00601 CopyAttributesTo(clone);
00602 return clone;
00603 }
00604
00605
00606 void manualContourModel::Open(FILE *ff)
00607 {
00608 char tmp[255];
00609 int i;
00610 int numberOfControlPoints;
00611 double x,y,z;
00612
00613 fscanf(ff,"%s",tmp);
00614 fscanf(ff,"%s",tmp);
00615 numberOfControlPoints = atoi(tmp);
00616 for (i=0;i<numberOfControlPoints;i++)
00617 {
00618 fscanf(ff,"%s",tmp);
00619 x = atof(tmp);
00620 fscanf(ff,"%s",tmp);
00621 y = atof(tmp);
00622 fscanf(ff,"%s",tmp);
00623 z = atof(tmp);
00624 AddPoint(x,y,z);
00625 }
00626 }
00627
00628
00629 int manualContourModel::GetTypeModel()
00630 {
00631
00632
00633
00634
00635
00636
00637
00638 return 1;
00639 }
00640
00641
00642 void manualContourModel::Save(FILE *ff)
00643 {
00644 int i,size=_lstPoints.size();
00645 fprintf(ff,"TypeModel %d\n", GetTypeModel() );
00646 fprintf(ff,"NumberOfControlPoints %d\n",size);
00647 for (i=0;i<size;i++)
00648 {
00649 manualPoint *mp=_lstPoints[i];
00650 fprintf(ff,"%f %f %f\n", mp->GetX(), mp->GetY(), mp->GetZ() );
00651 }
00652 }
00653
00654 void manualContourModel::SaveData(FILE *ff)
00655 {
00656 std::string etiqueta = GetLabel();
00657 if(etiqueta.empty())
00658 fprintf(ff,"Label: NO_LABEL\n");
00659 else
00660 fprintf(ff,"Label: %s\n",etiqueta.c_str());
00661 fprintf(ff,"Real_Size: %f\n",GetRealSize());
00662 }
00663
00664 void manualContourModel::OpenData(FILE *ff)
00665 {
00666 char tmp[255];
00667
00668 fscanf(ff,"%s",tmp);
00669 fscanf(ff,"%s",tmp);
00670 std::cout<<tmp<<std::endl;
00671
00672 if(strcmp(tmp,"NO_LABEL") != 0)
00673 SetLabel(tmp);
00674
00675 fscanf(ff,"%s",tmp);
00676 fscanf(ff,"%s",tmp);
00677
00678
00679 SetRealSize(atof(tmp));
00680 }
00681
00682
00683
00684
00685
00686 void manualContourModel::CopyAttributesTo( manualContourModel * cloneObject)
00687 {
00688
00689
00690
00691 cloneObject->SetCloseContour( this->IfCloseContour() );
00692 int i, size = GetSizeLstPoints();
00693 for( i=0; i<size; i++ )
00694 {
00695 cloneObject->AddManualPoint( GetManualPoint( i )->Clone() );
00696 }
00697 cloneObject->SetNumberOfPointsSpline( GetNumberOfPointsSpline () );
00698 cloneObject->SetCloseContour( _closeContour );
00699 cloneObject->UpdateSpline();
00700 }
00701
00702
00703
00704 void manualContourModel::AddManualPoint( manualPoint* theManualPoint )
00705 {
00706 _lstPoints.push_back( theManualPoint );
00707 }
00708
00709 std::vector<manualBaseModel*> manualContourModel::ExploseModel( )
00710 {
00711 std::vector<manualBaseModel*> lstTmp;
00712 lstTmp.push_back(this);
00713 return lstTmp;
00714 }
00715
00716
00717
00718 void manualContourModel::Transform_Ax_Plus_B (double Ax, double Bx, double Ay, double By)
00719 {
00720 manualPoint * mp;
00721
00722 int i, size = GetSizeLstPoints();
00723
00724 for( i=0; i<size; i++ )
00725 {
00726 mp = GetManualPoint( i );
00727
00728 mp->SetPointX( mp->GetX()*Ax + Bx );
00729 mp->SetPointY( mp->GetY()*Ay + By );
00730 }
00731 }
00732
00733
00734
00735 void manualContourModel::SetLabel(std::string newLabel)
00736 {
00737 _label = newLabel;
00738 }
00739
00740 void manualContourModel::SetRealSize(double newRealSize)
00741 {
00742 _realSize = newRealSize;
00743 }
00744
00745 double manualContourModel::GetRealSize()
00746 {
00747 return _realSize;
00748 }
00749
00750 std::string manualContourModel::GetLabel()
00751 {
00752 return _label;
00753 }
00754