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