00001 #include< math/math2.h>
00002 #include < assert.h>
00003
00004
00005 #include "PL_Sequential.h"
00006 #include "sp3dstct.h"
00007 #include "spfiles.h"
00008 #include "gdefs.h"
00009 #include "sp3dgl.h"
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include "opengl/glos.h"
00013 #include <gl/gl.h>
00014 #include "smoothers\SmootherBase.h"
00015
00016 #pragma warning( disable : 4786 )
00017
00018 const int UNDONE = -1 ;
00019
00020
00021
00022
00023
00024 PL_Sequential::~PL_Sequential()
00025 {
00026
00027 free( orig_initial_config ) ;
00028 free( orig_final_config ) ;
00029
00030 int i;
00031 for( i = 0; i < arm_degree; i++ )
00032 {
00033 free( lnkcps[ i ].d );
00034 lnkcps->d = NULL;
00035 }
00036 free( lnkcps );
00037 free2D( ( void** )globalpath );
00038 free( tqcs.tqlink );
00039 free( bkwdpath.d );
00040 free( frwdpath.d );
00041 free( combpath.d );
00042 free( mainpath.d );
00043 free( wall );
00044
00045 }
00046
00047
00048
00049
00050
00051
00052
00053 bool PL_Sequential::DrawExplicit () const
00054 {
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 if( this->tqcs.tqlink->bitmap == NULL )
00085 {
00086
00087
00088 const int xRes = 128;
00089 const int yRes = 128;
00090 char pixels[ yRes ][ xRes ];
00091 int i;
00092 int j;
00093 for( i = 0; i < xRes; i++ )
00094 {
00095 for( j = 0; j < yRes; j++ )
00096 {
00097 pixels[ j ][ i ] = i;
00098 }
00099 }
00100
00101
00102 ::glDrawPixels
00103 (
00104 128,
00105 128,
00106 GL_LUMINANCE,
00107 GL_BYTE,
00108 &pixels
00109 );
00110 }
00111 else
00112 {
00113
00114
00115 const int xRes = tqcs.tqlink->dimt;
00116 const int yRes = tqcs.tqlink->dimq;
00117
00118 ::glDrawPixels
00119 (
00120 xRes,
00121 yRes,
00122 GL_LUMINANCE,
00123 GL_BYTE,
00124 tqcs.tqlink->bitmap
00125 );
00126 }
00127 return true;
00128 }
00129
00130
00131 bool PL_Sequential::Plan ()
00132 {
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 int i,j;
00155 int blen;
00156 int BT_flag, Block_flag, Narrow_flag, on_BT_max;
00157 int current_link, path_link, path_final=SP_PLANNING, on_BT, linkno;
00158 int back_to_link;
00159 int BT_Number[7][7], BT_Number_Record[7];
00160 TQConfig block[100], block_last[1];
00161
00162
00163 for (linkno=0; linkno<arm_degree; linkno++) {
00164 original_joint_limit[linkno].status = 1;
00165 original_joint_limit[linkno].start = RADIAN(collisionDetector->JointMin(linkno));
00166 original_joint_limit[linkno].end = RADIAN(collisionDetector->JointMax(linkno));
00167 }
00168
00169
00170 for( int loop = 0; loop < arm_degree; loop++ )
00171 {
00172 orig_initial_config[ loop ] = RADIAN(GetStartConfig()[ loop ]) ;
00173 }
00174
00175
00176 for( loop = 0; loop < arm_degree; loop++ )
00177 {
00178 orig_final_config[ loop ] = RADIAN(GetGoalConfig()[ loop ]) ;
00179 }
00180
00181
00182 convert_angle_to_index();
00183
00184
00185 allocate_space_for_link_cs(0);
00186
00187
00188 collisionDetector->DeactivateAllFrames() ;
00189
00190
00191 collisionDetector->ActivateFrames(0,1);
00192 path_link = plan_path_for_first_link();
00193 if (path_link != SP_TQ_OK)
00194 {
00195 return false;
00196 }
00197
00198
00199 current_link = 1;
00200 path_final= SP_PLANNING;
00201 on_BT = FALSE;
00202 BT_level_test = 1;
00203 BT_flag = 0;
00204 Block_flag = 0;
00205 Narrow_flag = 0;
00206 on_BT_max = 0;
00207
00208 block_last[0].t = 0;
00209 block_last[0].q = 0;
00210
00211
00212 for (i=0; i<=max_level; i++) {
00213 BT_Number_Record[i] = 0;
00214 for (j=0; j<=arm_degree; j++) {
00215 BT_Number[j][i] = 0;
00216 }
00217 }
00218
00219 current_link++;
00220
00221
00222
00223 while ((current_link <= arm_degree) && (path_link == SP_TQ_OK)) {
00224 blen = 0;
00225 path_link = plan_path_for_nth_link(current_link,
00226 &(tqcs.tqlink[current_link-1]),
00227 block, block_last, &blen,
00228 &BT_flag, &Block_flag, &Narrow_flag);
00229
00230 if (path_link == SP_IF_NOT_OK){
00231 return false ;
00232 }
00233 if (path_link == SP_STOPPING) {
00234 return false ;
00235 }
00236 if (path_link == SP_TQ_OK) {
00237 on_BT = FALSE;
00238 BT_level_test = 1;
00239 current_link++;
00240 }
00241 else
00242 {
00243 on_BT = TRUE;
00244 BT_flag = 0;
00245
00246 if(Narrow_flag == 1) {
00247 blen = 1;
00248 }
00249
00250 if(BT_Number[current_link][BT_level_test]>=max_bt_no){
00251 BT_Number_Record[BT_level_test] += max_bt_no;
00252 BT_Number[current_link][BT_level_test] = 0;
00253 BT_level_test++;
00254
00255 if(BT_level_test > max_level){
00256 path_final = SP_FAILED;
00257 return false ;
00258 }
00259 BT_flag = 1;
00260 blen = 1;
00261 }
00262 }
00263
00264
00265 while ((on_BT == TRUE) && (BT_level_test >= 1) && (BT_level_test <= max_level )) {
00266 BT_Number[current_link][BT_level_test]++;
00267 while(BT_Number[current_link][BT_level_test]>max_bt_no){
00268 BT_Number_Record[BT_level_test] += max_bt_no;
00269 BT_Number[current_link][BT_level_test] = 0;
00270 BT_level_test++;
00271
00272 if(BT_level_test>max_level){
00273 path_final = SP_FAILED;
00274 return false ;
00275 break;
00276 }
00277 BT_Number[current_link][BT_level_test]++;
00278 BT_flag = 1;
00279 blen = 1;
00280 }
00281
00282 back_to_link = current_link - BT_level_test;
00283
00284
00285 if (back_to_link < 2) {
00286 return false;
00287 }
00288
00289
00290 path_link = plan_path_for_nth_link(back_to_link,
00291 &(tqcs.tqlink[back_to_link-1]),
00292 block, block_last, &blen,
00293 &BT_flag, &Block_flag, &Narrow_flag);
00294
00295 if (path_link == SP_IF_NOT_OK) {
00296 return false ;
00297 }
00298 if (path_link == SP_STOPPING) {
00299 return false ;
00300 }
00301 if (path_link == SP_TQ_OK) {
00302 BT_level_test--;
00303 }
00304 else if (path_link == SP_TQ_NOT_OK) {
00305
00306 BT_level_test++;
00307 if(Narrow_flag == 1)
00308 blen = 1;
00309 }
00310 else {
00311 return false ;
00312 }
00313 continue;
00314 }
00315
00316 BT_level_test = 1;
00317 continue;
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 if ( path_final != SP_FAILED )
00342 {
00343 globaldim = lnkcps[ arm_degree - 1 ].dim + 2;
00344 globalpath = ( double** ) malloc2D( globaldim, arm_degree, sizeof( double ) );
00345
00346 retrieve_global_path(globalpath, globaldim, orig_initial_config, orig_final_config);
00347
00348 path.Clear() ;
00349 Configuration config ;
00350 config.SetNumDOF( arm_degree ) ;
00351 for( i = 0; i < globaldim; i++ )
00352 {
00353 for( int j = 0; j < arm_degree; j++ )
00354 {
00355 config[ j ] = (globalpath[i][j]*180.0/PI);
00356 }
00357 path.AppendPoint( config ) ;
00358 }
00359
00360
00361
00362
00363 if( this->m_Smoother != NULL )
00364 {
00365 this->m_Smoother->SetPath( &this->path );
00366 this->m_Smoother->SetCollisionDetector( this->collisionDetector );
00367 m_Smoother->Smooth();
00368 this->path = m_Smoother->GetPath();
00369 m_Smoother->SetPath( NULL );
00370 }
00371 }
00372 else {
00373 return (false);
00374 }
00375
00376 return true ;
00377
00378
00379 }
00380
00381 void PL_Sequential::initialize_parameters ()
00382 {
00383
00384
00385
00386 sp_debug_level = 1;
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 dimt[0] = DIMT;
00397 dimq[0] = DIMQ;
00398
00399
00400 max_level = MAX_BACKTRACKING_LEVEL;
00401
00402
00403 max_bt_no = MAX_BACKTRACKING_NUMBER;
00404
00405 COEFD_Q = dimq[0]/(2*PI);
00406 COEFQ_D = (2*PI)/dimq[0];
00407
00408 for (int i=1; i<= arm_degree; i++)
00409 {
00410 dimq[i] = dimq[0];
00411 }
00412
00413 }
00414
00415 void PL_Sequential::SetCollisionDetector (CD_BasicStyle* collisionDetector)
00416 {
00417
00418 PL_Boolean_Output::SetCollisionDetector( collisionDetector ) ;
00419
00420 arm_degree = collisionDetector->DOF() ;
00421
00422
00423 for( int i = 0; i <= arm_degree; i++ )
00424 {
00425 for( int j = 0; j <= arm_degree; j++ )
00426 {
00427 collisionDetector->DeactivateFrames( i, j ) ;
00428 }
00429 }
00430
00431 tqcs.nlinks = arm_degree;
00432
00433 initialize_parameters();
00434
00435 orig_initial_config = (double *) malloc(arm_degree*sizeof(double));
00436 assert( orig_initial_config != NULL );
00437 orig_final_config = (double *) malloc(arm_degree*sizeof(double));
00438 assert( orig_final_config != NULL );
00439 tqcs.tqlink = (TQLinkCSpace *) calloc(arm_degree, sizeof(TQLinkCSpace));
00440 assert( tqcs.tqlink != NULL );
00441 lnkcps = (LinkPath *) calloc(arm_degree, sizeof(LinkPath));
00442 assert( lnkcps != NULL );
00443 wall = (unsigned char *) calloc(dimq[0], sizeof(unsigned char) ) ;
00444 assert( wall != NULL );
00445
00446 for (i=0; i<dimq[0];i++) {
00447 wall[i] = OBSTACLE;
00448 }
00449
00450 for (i=0; i<arm_degree; i++) {
00451 lnkcps[i].d = NULL;
00452 }
00453
00454
00455 allocate_space_for_working_tq_path();
00456 globalpath = NULL;
00457
00458
00459 }
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 int PL_Sequential::insert (int xx, int yy, IntPoint **FRONT, IntPoint *PFRONT, int nbfront,
00477 short int **V)
00478 {
00479 int icur,ipere;
00480 IntPoint *temp;
00481
00482 nbfront++;
00483 FRONT[nbfront] = PFRONT++;
00484 FRONT[nbfront]->x = (xx);
00485 FRONT[nbfront]->y = (yy);
00486 icur = nbfront;
00487 while(icur > 1){
00488 ipere = icur/2;
00489 if(V[FRONT[icur]->x][FRONT[icur]->y] <= V[FRONT[ipere]->x][FRONT[ipere]->y])
00490 break;
00491 temp = FRONT[icur];
00492 FRONT[icur] = FRONT[ipere];
00493 FRONT[ipere] = temp;
00494 icur = ipere;
00495 }
00496 return (nbfront);
00497 }
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510 int PL_Sequential::deletemin (int xx, int yy, IntPoint **FRONT, int nbfront, short int **V)
00511 {
00512 int icur, inew, ifilsd, ifilsg;
00513 IntPoint *temp;
00514
00515 xx = FRONT[1]->x;
00516 yy = FRONT[1]->y;
00517 FRONT[1] = FRONT[nbfront];
00518 nbfront--;
00519 icur = 1;
00520
00521 while(icur <= nbfront / 2){
00522 ifilsg = 2 * icur;
00523 ifilsd = 2 * icur + 1;
00524 if(V[FRONT[ifilsg]->x][FRONT[ifilsg]->y] > V[FRONT[ifilsd]->x][FRONT[ifilsd]->y] || nbfront == ifilsg)
00525 inew = ifilsg;
00526 else
00527 inew = ifilsd;
00528
00529 if(V[FRONT[icur]->x][FRONT[icur]->y] < V[FRONT[inew]->x][FRONT[inew]->y]){
00530 temp = FRONT[icur];
00531 FRONT[icur] = FRONT[inew];
00532 FRONT[inew] = temp;
00533 icur = inew;
00534 }
00535 else break;
00536 }
00537 return (nbfront);
00538 }
00539
00540
00541
00542
00543
00544
00545
00546
00547 int PL_Sequential::plan_path_for_first_link( void )
00548 {
00549 int col_rc, i, j, iminable, imaxable, fminable, fmaxable, connect_case;
00550 int blocked;
00551 unsigned char **map;
00552 TQConfig start, end;
00553 double ft;
00554 TQConfig *para;
00555
00556
00557 tqcs.tqlink[0].t_bound = FALSE;
00558 if (!(joint_limit[0].status))
00559 tqcs.tqlink[0].q_bound = FALSE;
00560 else
00561 tqcs.tqlink[0].q_bound = TRUE;
00562
00563
00564 map = tqcs.tqlink[0].bitmap;
00565
00566
00567 para = (TQConfig *) malloc (sizeof(TQConfig));
00568 para[0].t = 0;
00569 para[0].q = 0;
00570
00571
00572 col_rc = compute_a_column_for_link_cspace(tqcs.tqlink, 1, 0, para);
00573 if (col_rc == SP_STOPPING) {
00574 return(false);
00575 }
00576
00577 free(para);
00578
00579
00580 blocked = false;
00581 for (j=0; j<tqcs.tqlink[0].dimq; j++) {
00582 if ( map[0][j] == OBSTACLE) {
00583 blocked = true;
00584 break;
00585 }
00586 }
00587
00588
00589
00590 for (j=1; j<tqcs.tqlink[0].dimt; j++) {
00591 for (i=0; i<tqcs.tqlink[0].dimq; i++) {
00592 map[j][i] = map[0][i];
00593 }
00594 }
00595
00596
00597
00598 iminable = initial_config[0];
00599 while ((iminable>=0) || blocked)
00600 {
00601 if (map[0][(iminable+dimq[0])%dimq[0]]==FREESPACE)
00602 {
00603 iminable--;
00604 }
00605 else
00606 {
00607 break;
00608 }
00609 }
00610 iminable++;
00611
00612
00613 imaxable = initial_config[0];
00614 while ((imaxable<tqcs.tqlink[0].dimq) || blocked)
00615 {
00616 if (map[0][imaxable%dimq[0]]==FREESPACE)
00617 {
00618 imaxable++;
00619 }
00620 else
00621 {
00622 break;
00623 }
00624 }
00625 imaxable--;
00626
00627
00628 fminable = final_config[0];
00629 while ((fminable>=0) || blocked)
00630 {
00631 if (map[0][(fminable+dimq[0])%dimq[0]]==FREESPACE)
00632 {
00633 fminable--;
00634 }
00635 else
00636 {
00637 break;
00638 }
00639 }
00640 fminable++;
00641
00642
00643 fmaxable = final_config[0];
00644 while ((fmaxable<tqcs.tqlink[0].dimq) || blocked)
00645 {
00646 if (map[0][fmaxable%dimq[0]]==FREESPACE)
00647 {
00648 fmaxable++;
00649 }
00650 else
00651 {
00652 break;
00653 }
00654 }
00655 fmaxable--;
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668 if (((initial_config[0]>fminable)&&(initial_config[0]<fmaxable))
00669 || ((final_config[0]>iminable)&&(final_config[0]<imaxable))
00670 || (initial_config[0]==final_config[0]))
00671 connect_case = 0;
00672
00673 else {
00674 if (tqcs.tqlink[0].q_bound)
00675 return (false);
00676 if ( ((iminable<=0) && (fmaxable>=(tqcs.tqlink[0].dimq-1)) )
00677 || ((fminable <=0) && (imaxable>=(tqcs.tqlink[0].dimq-1) ) ) )
00678 connect_case = 1;
00679 else
00680 return (false);
00681 }
00682
00683
00684 start.t = 0;
00685 end.t = dimt[0]-1;
00686
00687 allocate_space_for_Link_tq_path(0, dimt[0]);
00688
00689
00690 if (connect_case == 0) {
00691 if (initial_config[0] <= final_config[0]) {
00692 start.q = iminable; end.q = fmaxable;
00693 }
00694 else {
00695 start.q = imaxable; end.q = fminable;
00696 }
00697 lnkcps[0].dim = dimt[0];
00698 }
00699 else if (connect_case == 1) {
00700 if (initial_config[0] <= final_config[0]) {
00701 start.q = imaxable;
00702 end.q = fminable-dimq[0];
00703 }
00704 else {
00705 start.q = iminable;
00706 end.q = fmaxable+dimq[0];
00707 }
00708 }
00709 else {
00710 return (false);
00711 }
00712
00713
00714 if ((start.q==((end.q+1)%dimq[0])) || ((end.q%dimq[0])==((start.q+1)%dimq[0])))
00715 tqcs.tqlink[0].q_bound = FALSE;
00716 else
00717 tqcs.tqlink[0].q_bound = TRUE;
00718
00719
00720 if ( !(tqcs.tqlink[0].t_bound) && !(tqcs.tqlink[0].q_bound) )
00721 tqcs.tqlink[0].p_bound = FALSE;
00722 if ( tqcs.tqlink[0].p_bound ) {
00723 lnkcps[0].d[0].t = start.t;
00724 lnkcps[0].d[0].q = ROUND(start.q);
00725 lnkcps[0].d[dimq[0]-1].t = end.t;
00726 lnkcps[0].d[dimq[0]-1].q = ROUND(end.q);
00727 start.t += 1; end.t -= 1;
00728 }
00729
00730
00731
00732 for (i = start.t, ft = start.t; i <= end.t; i++, ft++) {
00733 lnkcps[0].d[i].t = i;
00734 lnkcps[0].d[i].q = ROUND(ft*(end.q-start.q)/(double)dimt[0]+start.q);
00735 lnkcps[0].d[i].q = (lnkcps[0].d[i].q +dimq[0]) %dimq[0];
00736
00737 if (lnkcps[0].d[i].q % dimq[0] == initial_config[0]) {
00738 initial_tq[0].t = i;
00739 initial_tq[0].q = lnkcps[0].d[i].q;
00740 }
00741 if (lnkcps[0].d[i].q % dimq[0] == final_config[0]) {
00742 final_tq[0].t = i;
00743 final_tq[0].q = lnkcps[0].d[i].q;
00744 }
00745 }
00746
00747
00748 if (sp_debug_level >0) {
00749 for (i=0; i< dimt[0]; i++) {
00750 lnkcps[0].d[i].q = lnkcps[0].d[i].q % dimq[0];
00751 }
00752 }
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771 dimt[1] = lnkcps[0].dim;
00772
00773 free_space_for_link_cs(0);
00774
00775 return(SP_TQ_OK);
00776
00777 }
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 int PL_Sequential::plan_path_for_nth_link( int lnkno,
00794 TQLinkCSpace *ptqlink,
00795 TQConfig *pblock,
00796 TQConfig *pblock_last,
00797 int *bl,
00798 int *BT_Flag,
00799 int *block_flag,
00800 int *narrow_flag)
00801 {
00802 int tq_rc, i, j ;
00803 int lnkno1, path_found;
00804 int ipos, fpos;
00805 IntPoint pinitial, pfinal;
00806 TQConfig tqi, tqf;
00807 int blockwidth = 0;
00808
00809
00810 for (i = 0; i < lnkno; i++)
00811 {
00812 collisionDetector->ActivateFrames(i, lnkno);
00813 }
00814
00815 lnkno1 = lnkno - 1 ;
00816
00817
00818 tqi.t = pinitial.x = initial_tq[lnkno-2].t;
00819 tqi.q = pinitial.y = initial_config[lnkno1];
00820
00821
00822 tqf.t = pfinal.x = final_tq[lnkno-2].t;
00823 tqf.q = pfinal.y = final_config[lnkno1];
00824
00825
00826 allocate_space_for_link_cs(lnkno1);
00827
00828
00829 tq_rc = compute_cspace_obstacle_map(lnkno);
00830 if (tq_rc == SP_STOPPING) {
00831 free_space_for_link_cs(lnkno1);
00832 return(tq_rc);
00833 }
00834
00835 if (*bl)
00836 {
00837 blockwidth = *bl;
00838 const int RESERVED_SIZE = 2;
00839
00840 for (i = 0; i < blockwidth; i++)
00841 {
00842 for (int k = pblock[i].t - blockwidth; k < pblock[i].t + blockwidth; k++)
00843 {
00844 for (j = pblock[i].q - blockwidth; j < pblock[i].q + blockwidth; j++)
00845 {
00846 if ((ABS(tqi.t - k) > RESERVED_SIZE)
00847 && (ABS(tqi.q - j) > RESERVED_SIZE)
00848 && (ABS(tqf.t - k) > RESERVED_SIZE)
00849 && (ABS(tqf.q - j) > RESERVED_SIZE))
00850 {
00851 ptqlink->bitmap[(k+dimt[lnkno1]) % dimt[lnkno1]]
00852 [(j+dimq[lnkno1]) % dimq[lnkno1]]
00853 = OBSTACLE;
00854 }
00855 }
00856 }
00857 }
00858
00859
00860 if (ptqlink->bitmap[pinitial.x][pinitial.y] == OBSTACLE) {
00861 pblock[0].t = lnkcps[lnkno1-1].d[pinitial.x].t;
00862 pblock[0].q = lnkcps[lnkno1-1].d[pinitial.x].q;
00863 *bl = 1;
00864 free_space_for_link_cs(lnkno1);
00865 return(SP_TQ_NOT_OK);
00866 }
00867
00868
00869 if (ptqlink->bitmap[pfinal.x][pfinal.y] == OBSTACLE) {
00870 pblock[0].t = lnkcps[lnkno1-1].d[pfinal.x].t;
00871 pblock[0].q = lnkcps[lnkno1-1].d[pfinal.x].q;
00872 *bl = 1;
00873 free_space_for_link_cs(lnkno1);
00874 return(SP_TQ_NOT_OK);
00875 }
00876 }
00877 else
00878 {
00879
00880
00881 if (ptqlink->bitmap[pinitial.x][pinitial.y] == OBSTACLE) {
00882 free_space_for_link_cs(lnkno1);
00883 return(SP_IF_NOT_OK);
00884 }
00885
00886
00887 if (ptqlink->bitmap[pfinal.x][pfinal.y] == OBSTACLE) {
00888 free_space_for_link_cs(lnkno1);
00889 return(SP_IF_NOT_OK);
00890 }
00891 }
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936 if (joint_limit[lnkno1].status)
00937 ptqlink->q_bound = TRUE;
00938 else
00939 ptqlink->q_bound = FALSE;
00940
00941 if ((tqcs.tqlink[lnkno-2].p_bound) || (tqcs.tqlink[lnkno-2].q_bound))
00942 ptqlink->t_bound = TRUE;
00943 else
00944 ptqlink->t_bound = FALSE;
00945
00946 if (ptqlink->t_bound || ptqlink->q_bound)
00947 ptqlink->p_bound = TRUE;
00948
00949
00950 if (ptqlink->t_bound) {
00951 for (j=0; j<dimq[lnkno1]; j++)
00952 ptqlink->bitmap[0][j]
00953 = ptqlink->bitmap[dimt[lnkno1]-1][j] = OBSTACLE;
00954 }
00955
00956
00957 path_found = find_main_path(lnkno, &pinitial, &pfinal,
00958 ptqlink, &mainpath, narrow_flag);
00959
00960 if (!path_found) {
00961 if((pblock_last[0].t == mainpath.d[0].t) && (pblock_last[0].q == mainpath.d[0].q))
00962 {
00963 *block_flag = 1;
00964 pblock_last[0].t = 0;
00965 pblock_last[0].q = 0;
00966 }
00967 else
00968 {
00969 *block_flag = 0;
00970 pblock_last[0].t = mainpath.d[0].t;
00971 pblock_last[0].q = mainpath.d[0].q;
00972 }
00973 if (lnkno > 2) {
00974 for (i = 0 ; i < MIN(mainpath.dim, 99) ; i++) {
00975 pblock[i].t = lnkcps[lnkno1-1].d[mainpath.d[i].t].t;
00976 pblock[i].q = lnkcps[lnkno1-1].d[mainpath.d[i].t].q;
00977 }
00978 }
00979
00980 *bl = MIN(mainpath.dim, 99);
00981
00982 free_space_for_link_cs(lnkno1);
00983
00984 return(SP_TQ_NOT_OK);
00985 }
00986
00987
00988 if (lnkno != arm_degree)
00989 find_forward_extension(lnkno, &pinitial, &pfinal, ptqlink, &frwdpath);
00990 else
00991 frwdpath.dim = 0;
00992
00993
00994 if (lnkno != arm_degree)
00995 find_backward_extension(lnkno, &pinitial, &pfinal, ptqlink, &bkwdpath);
00996 else
00997 bkwdpath.dim = 0;
00998
00999
01000 combine_three_paths(&combpath, &mainpath, &frwdpath, &bkwdpath, &ipos, &fpos);
01001
01002 if (NO_PATH_COMPRESS) {
01003 if (ptqlink->p_bound && (lnkno != arm_degree)) {
01004 dimt[lnkno] = combpath.dim+2;
01005 allocate_space_for_Link_tq_path(lnkno1, dimt[lnkno]);
01006 reparameterize_path(ptqlink->p_bound, &combpath, &(lnkcps[lnkno1]),
01007 &ipos, &fpos, lnkno);
01008 }
01009 else {
01010 if (ptqlink->p_bound)
01011 {
01012 dimt[lnkno] = combpath.dim+2;
01013 }
01014 else
01015 {
01016 dimt[lnkno] = combpath.dim;
01017 }
01018 allocate_space_for_Link_tq_path(lnkno1, dimt[lnkno]);
01019 reparameterize_path(ptqlink->p_bound, &combpath, &(lnkcps[lnkno1]),
01020 &ipos, &fpos, lnkno);
01021 }
01022 }
01023 else {
01024 dimt[lnkno] = dimt[0];
01025 allocate_space_for_Link_tq_path(lnkno1, dimt[lnkno]);
01026
01027 reparameterize_path(ptqlink->p_bound, &combpath, &(lnkcps[lnkno1]),
01028 &ipos, &fpos, lnkno);
01029 }
01030
01031
01032 initial_tq[lnkno1].t = ipos;
01033 initial_tq[lnkno1].q = lnkcps[lnkno1].d[ipos].q;
01034
01035 final_tq[lnkno1].t = fpos;
01036 final_tq[lnkno1].q = lnkcps[lnkno1].d[fpos].q;
01037 *bl = 0;
01038
01039 dimt[lnkno] = lnkcps[lnkno1].dim;
01040
01041
01042 if (sp_debug_level > 0) {
01043 for (i=0; i<dimt[lnkno]; i++) {
01044 lnkcps[lnkno1].d[i].q = lnkcps[lnkno1].d[i].q % dimq[lnkno1];
01045 }
01046 }
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074 free_space_for_link_cs(lnkno1);
01075
01076 return(SP_TQ_OK);
01077
01078 }
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090 int PL_Sequential::compute_a_column_for_link_cspace(TQLinkCSpace *tqlcs,
01091 int lnkno,
01092 int col,
01093 TQConfig *para)
01094
01095 {
01096 int is, ie, lnkno1, k;
01097 unsigned char *row;
01098 Configuration config;
01099 config.SetNumDOF( arm_degree ) ;
01100
01101 lnkno1 = lnkno - 1;
01102
01103 row = tqlcs->bitmap[col];
01104
01105 if (joint_limit[lnkno1].status) {
01106 is = joint_limit[lnkno1].start+1;
01107 ie = joint_limit[lnkno1].end;
01108
01109 is = MAX(0,is);
01110 ie = MIN(tqlcs->dimq, ie);
01111
01112 for (int i = 0; i < is; i++) {
01113 row[i] = OBSTACLE;
01114 }
01115 for (i = ie; i < tqlcs->dimq; i++) {
01116 row[i] = OBSTACLE;
01117 }
01118
01119 for( i = is; i < ie; i++ )
01120 {
01121 row[ i ] = FREESPACE ;
01122 }
01123 }
01124 else {
01125 is = 0;
01126 ie = tqlcs->dimq;
01127 }
01128
01129
01130 config = GetStartConfig();
01131
01132
01133 for( int j = 0; j < lnkno1; j++ )
01134 {
01135 config[ j ] = (index_to_rad(para[j].q))*180/PI ;
01136 }
01137
01138
01139
01140 k = is;
01141 while ( k < ie)
01142 {
01143 double joint1Config = index_to_rad( k ) * 180.0 / PI ;
01144 config[lnkno1] = joint1Config;
01145 if ( !collisionDetector->IsInterfering( config ) ){
01146 row[k] = FREESPACE;
01147 }
01148 else {
01149 row[k] = OBSTACLE;
01150 }
01151 k++;
01152 }
01153
01154 return(SP_TQ_OK);
01155
01156 }
01157
01158
01159
01160
01161
01162
01163
01164
01165 int PL_Sequential::compute_cspace_obstacle_map(int lnkno)
01166 {
01167 int tq_rc=SP_TQ_OK, lnkno1;
01168 TQConfig para[MAX_DEGREE];
01169
01170 lnkno1 = lnkno - 1;
01171
01172
01173 for (int i = 0; i < dimt[lnkno1]; i++) {
01174
01175 para[lnkno1-1].q = lnkcps[lnkno1-1].d[i].q;
01176 para[lnkno1-1].t = lnkcps[lnkno1-1].d[i].t;
01177
01178
01179 for (int j=lnkno1-2; j>=0; j--) {
01180 para[j].t = lnkcps[j].d[ para[j+1].t].t;
01181 para[j].q = lnkcps[j].d[ para[j+1].t].q;
01182 }
01183
01184
01185
01186
01187
01188
01189
01190 tq_rc = compute_a_column_for_link_cspace(&(tqcs.tqlink[lnkno1]),
01191 lnkno, i, para);
01192 if (tq_rc == SP_STOPPING) {
01193 break;
01194 }
01195 }
01196
01197 return(tq_rc);
01198
01199 }
01200
01201
01202
01203
01204
01205
01206
01207 void PL_Sequential::convert_angle_to_index(void)
01208 {
01209
01210 for (int i=0; i<arm_degree; i++) {
01211 initial_config[i] = rad_to_index(orig_initial_config[i]);
01212 final_config[i] = rad_to_index(orig_final_config[i]);
01213 joint_limit[i].status = original_joint_limit[i].status;
01214 joint_limit[i].start = rad_to_index(original_joint_limit[i].start);
01215 joint_limit[i].end = rad_to_index(original_joint_limit[i].end);
01216 }
01217 }
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227 int PL_Sequential::rad_to_index( double angle )
01228 {
01229 return((int)(COEFD_Q*(angle+PI)+0.5));
01230 }
01231
01232
01233
01234
01235
01236
01237
01238
01239 double PL_Sequential::index_to_rad( int index )
01240 {
01241 return((double)(COEFQ_D*index - PI));
01242 }
01243
01244
01245
01246
01247
01248
01249 void PL_Sequential::modify_cspace_obstacle_map(TQLinkCSpace *ptqlink,
01250 TQConfig *tqi,
01251 TQConfig *tqf,
01252 TQConfig *pblock,
01253 int len,
01254 int L1,
01255 int *BT_FLAG,
01256 int *narrow_flag)
01257
01258 {
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351 }
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366 PL_Sequential::find_main_path( int lnkno,
01367 IntPoint *tqinitial,
01368 IntPoint *tqfinal,
01369 TQLinkCSpace *ptqlink,
01370 LinkPath *result,
01371 int *narrow_flag)
01372 {
01373 int i, j, k, wallpos, mid, t1, t2, width, lnkno1, found;
01374 unsigned char *wtmp;
01375
01376 lnkno1 = lnkno - 1;
01377
01378
01379 if ((tqinitial->x !=0 ) && (tqfinal->x != 0))
01380 wallpos =0;
01381 else
01382 wallpos = dimt[lnkno1]-1;
01383
01384 wtmp = ptqlink->bitmap[wallpos];
01385 ptqlink->bitmap[wallpos] = wall;
01386
01387
01388 distance2d(ptqlink->bitmap, dimt[lnkno1], dimq[lnkno1],
01389 ptqlink->distmap, ptqlink->voro);
01390
01391
01392 if ((ptqlink->distmap[tqfinal->x][tqfinal->y] < 1 )
01393 || (ptqlink->distmap[tqfinal->x][tqfinal->y] == MAXSHORTINT))
01394 {
01395 result->d[0].t = tqfinal->x;
01396 result->d[0].q = tqfinal->y;
01397 result->dim = 1;
01398
01399 ptqlink->bitmap[wallpos] = wtmp;
01400 return(false);
01401 }
01402
01403
01404 voro_heuristic2d(ptqlink->bitmap, dimt[lnkno1],
01405 dimq[lnkno1], *tqfinal,
01406 ptqlink->distmap, ptqlink->voro,
01407 ptqlink->potential, narrow_flag);
01408
01409
01410
01411
01412
01413 int startpot = 0;
01414
01415 startpot = ptqlink->potential[tqinitial->x][tqinitial->y];
01416
01417 if ((startpot < 1) || (startpot >= MAXSHORTINT)) {
01418 if((tqinitial->x == tqfinal->x) && (tqinitial->y == tqfinal->y)) {
01419 result->d[0].t = tqinitial->x;
01420 result->d[0].q = tqinitial->y;
01421 result->dim = 1;
01422 ptqlink->bitmap[wallpos] = wtmp;
01423 return(true);
01424 }
01425
01426 else if (tqinitial->x != tqfinal->x)
01427 {
01428 found = false;
01429
01430 for (i = tqinitial->x + 2; i < tqfinal->x - 2; i++)
01431 {
01432 width = (i - tqinitial->x) / 2;
01433 width = MAX(1, width);
01434 width = MIN(dimq[lnkno1]/10, width);
01435
01436 mid = tqinitial->y + (tqfinal->y - tqinitial->y) * (i - tqinitial->x)
01437 / (tqfinal->x - tqinitial->x);
01438 j = k = mid;
01439
01440 while ((k >= 0) || (j < dimq[lnkno1]))
01441 {
01442 t1 = i;
01443 if ((k >= 0) && ( (mid - k) <= width))
01444 {
01445 if ((ptqlink->potential[t1][k] > 0)
01446 && (ptqlink->potential[t1][k] < MAXSHORTINT))
01447 {
01448 found = true;
01449 result->d[0].t = t1;
01450 result->d[0].q = k;
01451 result->dim = 1;
01452 break;
01453 }
01454 }
01455 k--;
01456 t2 = i;
01457 if ((j < dimq[lnkno1]) && !found && ((j - mid) <= width))
01458 {
01459 if ((ptqlink->potential[t2][j] > 0)
01460 &&(ptqlink->potential[t2][j] < MAXSHORTINT))
01461 {
01462 found = true;
01463 result->d[0].t = t2;
01464 result->d[0].q = j;
01465 result->dim = 1;
01466 break;
01467 }
01468 }
01469 j++;
01470 }
01471 if (found) break;
01472 }
01473
01474
01475
01476
01477 i = result->d[0].t-1;
01478 j = result->d[0].q;
01479 k = result->dim;
01480
01481
01482
01483
01484
01485
01486
01487 while ((((ptqlink->potential[i][j] < 0) || (ptqlink->potential[i][j] == MAXSHORTINT))
01488 && (k < MAXBLOCKLENGTH))
01489 || ( k < MAXBLOCKLENGTH / 4))
01490 {
01491 result->d[k].t = i;
01492 result->d[k].q = j;
01493 i--; k++;
01494 }
01495
01496 result->dim = k;
01497
01498
01499 ptqlink->bitmap[wallpos] = wtmp;
01500
01501 return(false);
01502
01503 }
01504
01505 else
01506 {
01507 found = false;
01508 int pos1, pos2, pos3, pos4;
01509 int cur_x = tqinitial->x;
01510 int cur_y = ROUND((tqinitial->y + tqfinal->y) / 2);
01511
01512 for (i = 0; ((cur_x + i) < dimt[lnkno1])
01513 && ((cur_x - i) >= 0)
01514 && ((cur_y + i) < dimq[lnkno1])
01515 && ((cur_y - i) >= 0); i++)
01516 {
01517 pos1 = (cur_x + i + dimt[lnkno1]) % dimt[lnkno1];
01518 pos2 = (cur_x - i + dimt[lnkno1]) % dimt[lnkno1];
01519 pos3 = (cur_y + i + dimq[lnkno1]) % dimq[lnkno1];
01520 pos4 = (cur_y - i + dimq[lnkno1]) % dimq[lnkno1];
01521
01522 if ((ptqlink->potential[pos1][pos3] > 0)
01523 && (ptqlink->potential[pos1][pos3] < MAXSHORTINT))
01524 {
01525 found = true;
01526 result->d[0].t = pos1;
01527 result->d[0].q = pos3;
01528 result->dim = 1;
01529 break;
01530 }
01531
01532 if ((ptqlink->potential[pos2][pos3] > 0)
01533 && (ptqlink->potential[pos2][pos3] < MAXSHORTINT))
01534 {
01535 found = true;
01536 result->d[0].t = pos2;
01537 result->d[0].q = pos3;
01538 result->dim = 1;
01539 break;
01540 }
01541
01542 if ((ptqlink->potential[pos1][pos4] > 0)
01543 && (ptqlink->potential[pos1][pos4] < MAXSHORTINT))
01544 {
01545 found = true;
01546 result->d[0].t = pos1;
01547 result->d[0].q = pos4;
01548 result->dim = 1;
01549 break;
01550 }
01551
01552 if ((ptqlink->potential[pos2][pos4] > 0)
01553 && (ptqlink->potential[pos2][pos4] < MAXSHORTINT))
01554 {
01555 found = true;
01556 result->d[0].t = pos2;
01557 result->d[0].q = pos4;
01558 result->dim = 1;
01559 break;
01560 }
01561
01562 if ((ptqlink->potential[pos1][cur_y] > 0)
01563 && (ptqlink->potential[pos1][cur_y] < MAXSHORTINT))
01564 {
01565 found = true;
01566 result->d[0].t = pos1;
01567 result->d[0].q = cur_y;
01568 result->dim = 1;
01569 break;
01570 }
01571 if ((ptqlink->potential[pos2][cur_y] > 0)
01572 && (ptqlink->potential[pos2][cur_y] < MAXSHORTINT))
01573 {
01574 found = true;
01575 result->d[0].t = pos2;
01576 result->d[0].q = cur_y;
01577 result->dim = 1;
01578 break;
01579 }
01580 if (found) {
01581 break;
01582 }
01583 }
01584 if (!found)
01585 {
01586 result->d[0].t = tqinitial->x;
01587 result->d[0].q = tqinitial->y;
01588 result->dim = 1;
01589 }
01590
01591
01592 ptqlink->bitmap[wallpos] = wtmp;
01593
01594 return(false);
01595
01596 }
01597
01598 }
01599
01600
01601 find_path_by_heuristic(result, ptqlink->potential,
01602 dimt[lnkno1], dimq[lnkno1], tqinitial, tqfinal);
01603
01604
01605 ptqlink->bitmap[wallpos] = wtmp;
01606
01607 return(true);
01608
01609 }
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621 void PL_Sequential::find_forward_extension(int lnkno,
01622 IntPoint *tqinitial,
01623 IntPoint *tqfinal,
01624 TQLinkCSpace *ptqlink,
01625 LinkPath *result)
01626 {
01627 int i, j, lnkno1, changesign, found, t, q;
01628 unsigned char *wtmp;
01629 IntPoint tqp1, fstart;
01630 int narrow_flag1 = 0;
01631
01632 lnkno1 = lnkno - 1 ;
01633
01634
01635 wtmp = ptqlink->bitmap[(tqinitial->x+1)%dimt[lnkno1]];
01636 ptqlink->bitmap[(tqinitial->x+1)%dimt[lnkno1]] = wall;
01637
01638
01639 distance2d(ptqlink->bitmap, dimt[lnkno1], dimq[lnkno1],
01640 ptqlink->distmap, ptqlink->voro);
01641
01642
01643 voro_heuristic2d(ptqlink->bitmap, dimt[lnkno1],
01644 dimq[lnkno1], *tqfinal,
01645 ptqlink->distmap, ptqlink->voro,
01646 ptqlink->potential, &narrow_flag1);
01647
01648
01649 ptqlink->bitmap[(tqinitial->x+1)%dimt[lnkno1]] = wtmp;
01650
01651
01652
01653 tqp1.x = dimt[lnkno1]-1;
01654
01655
01656
01657 if (tqinitial->y < tqfinal->y) {
01658 changesign = -1;
01659 if (joint_limit[lnkno-1].status)
01660 tqp1.y = joint_limit[lnkno-1].end;
01661 else
01662 tqp1.y = dimq[lnkno1]-1;
01663 }
01664 else {
01665 changesign = 1;
01666 if (joint_limit[lnkno-1].status)
01667 tqp1.y = joint_limit[lnkno-1].start;
01668 else
01669 tqp1.y = 0;
01670 }
01671
01672
01673 found = FALSE;
01674 i=0;
01675 do {
01676 for (j = i; j >= 0; j--) {
01677 t = (tqp1.x - j + dimt[lnkno1]) % dimt[lnkno1];
01678 q = (tqp1.y + ((i-j)*changesign) + dimq[lnkno1]) % dimq[lnkno1];
01679 if ( ( ptqlink->potential[t][q] > SAFETY_DISTANCE)
01680 && ( ptqlink->potential[t][q] != MAXSHORTINT))
01681 {
01682 found = TRUE;
01683 break;
01684 }
01685 }
01686 i++;
01687 } while (!found);
01688
01689 fstart.x = t; fstart.y = q;
01690
01691
01692 find_path_by_heuristic(result, ptqlink->potential,
01693 dimt[lnkno1], dimq[lnkno1], tqfinal, &fstart);
01694
01695 }
01696
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706
01707
01708 void PL_Sequential::find_backward_extension( int lnkno,
01709 IntPoint *tqinitial,
01710 IntPoint *tqfinal,
01711 TQLinkCSpace *ptqlink,
01712 LinkPath *result)
01713 {
01714 int i, j, lnkno1, changesign, found, t, q;
01715 unsigned char *wtmp;
01716 IntPoint tqp1, fstart;
01717 int narrow_flag2 = 0;
01718
01719 lnkno1 = lnkno - 1 ;
01720
01721
01722 wtmp = ptqlink->bitmap[(tqfinal->x+dimt[lnkno1]-1)%dimt[lnkno1]];
01723 ptqlink->bitmap[(tqfinal->x+dimt[lnkno1]-1)%dimt[lnkno1]] = wall;
01724
01725
01726 distance2d(ptqlink->bitmap, dimt[lnkno1], dimq[lnkno1],
01727 ptqlink->distmap, ptqlink->voro);
01728
01729
01730 voro_heuristic2d(ptqlink->bitmap, dimt[lnkno1],
01731 dimq[lnkno1], *tqinitial,
01732 ptqlink->distmap, ptqlink->voro,
01733 ptqlink->potential, &narrow_flag2);
01734
01735
01736 ptqlink->bitmap[(tqfinal->x+dimt[lnkno1]-1)%dimt[lnkno1]] = wtmp;
01737
01738
01739
01740 tqp1.x = 0;
01741
01742
01743
01744 if (tqinitial->y < tqfinal->y) {
01745 changesign = 1;
01746 if (joint_limit[lnkno1].status)
01747 tqp1.y = joint_limit[lnkno1].start;
01748 else
01749 tqp1.y = 0;
01750 }
01751 else {
01752 changesign = -1;
01753 if (joint_limit[lnkno1].status)
01754 tqp1.y = joint_limit[lnkno1].end;
01755 else
01756 tqp1.y = dimq[lnkno1]-1;
01757 }
01758
01759
01760 found = FALSE;
01761 i=0;
01762 do {
01763 for (j=0; j<=i; j++) {
01764 t = (tqp1.x + j)%dimt[lnkno1];
01765 q = (tqp1.y + (i-j)*changesign +dimq[lnkno1])%dimq[lnkno1];
01766 if ( ( ptqlink->potential[t][q] > SAFETY_DISTANCE)
01767 && ( ptqlink->potential[t][q] != MAXSHORTINT))
01768 { found = TRUE; break; }
01769 }
01770 i++;
01771 } while (!found);
01772
01773 fstart.x = t; fstart.y = q;
01774
01775
01776 find_path_by_heuristic(result, ptqlink->potential,
01777 dimt[lnkno1], dimq[lnkno1], &fstart, tqinitial);
01778
01779 }
01780
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793 void PL_Sequential::combine_three_paths( LinkPath *combpath,
01794 LinkPath *mainpath,
01795 LinkPath *frwdpath,
01796 LinkPath *bkwdpath,
01797 int *iposition,
01798 int *fposition)
01799 {
01800 int dim, i;
01801
01802 dim = 0;
01803
01804
01805 for (i=0; i<bkwdpath->dim-1; i++) {
01806 combpath->d[dim].t = bkwdpath->d[i].t;
01807 combpath->d[dim].q = bkwdpath->d[i].q;
01808 dim++;
01809 }
01810
01811 *iposition = dim;
01812
01813
01814 for (i=0; i<mainpath->dim; i++) {
01815 combpath->d[dim].t = mainpath->d[i].t;
01816 combpath->d[dim].q = mainpath->d[i].q;
01817 dim++;
01818 }
01819
01820 *fposition = dim-1;
01821
01822
01823 for (i=frwdpath->dim-2; i>=0; i--) {
01824 combpath->d[dim].t = frwdpath->d[i].t;
01825 combpath->d[dim].q = frwdpath->d[i].q;
01826 dim++;
01827 }
01828
01829 combpath->dim = dim;
01830
01831
01832 if (combpath->dim > combpath->maxdim)
01833 printf("***** Warning, Combined path limit over.*****\n");
01834
01835 }
01836
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846
01847
01848 void PL_Sequential::distance2d(unsigned char **in, int dimx, int dimy, short int **V,
01849 unsigned char **voro)
01850 {
01851 int i, dimx1, dimy1;
01852 register int x,y;
01853 Int2DPoint *FRONT,*NEXT,*TEMP;
01854 Int2DPoint *MFRONT, *MNEXT;
01855 int nbfront = 0, nbnext = 0 , nbskele = 0;
01856 Int2DPoint ***pere,*pepere;
01857 Int2DPoint *Mpepere;
01858 int px, py;
01859
01860 dimx1 = dimx - 1;
01861 dimy1 = dimy - 1;
01862
01863
01864
01865
01866
01867
01868 MFRONT= FRONT = (Int2DPoint *)malloc(100*(dimx+dimy)*sizeof(Int2DPoint));
01869 MNEXT= NEXT = (Int2DPoint *)malloc(100*(dimx+dimy)*sizeof(Int2DPoint));
01870
01871
01872
01873
01874
01875
01876
01877
01878 Mpepere= pepere = (Int2DPoint *)malloc(100*(dimx+dimy)*sizeof(Int2DPoint));
01879 pere = (Int2DPoint ***)malloc2D(dimx,dimy,sizeof(Int2DPoint *));
01880
01881
01882
01883
01884
01885 for(x=0;x<dimx;++x) {
01886 for(y=0;y<dimy;++y) {
01887 if(in[x][y] == OBSTACLE)
01888 {
01889 V[x][y] = 0;
01890 continue;
01891 }
01892 if(in[(x+1)%dimx][y] == OBSTACLE ||
01893 in[(x+dimx1)%dimx][y] == OBSTACLE ||
01894 in[x][(y+1)%dimy] == OBSTACLE ||
01895 in[x][(y+dimy1)%dimy] == OBSTACLE)
01896 {
01897 V[x][y] = 1;
01898 FRONT[nbfront].x = pepere->x = x;
01899 FRONT[nbfront].y = pepere->y = y;
01900 nbfront++;
01901 pere[x][y] = pepere++;
01902 }
01903 else {
01904 V[x][y] = MAXSHORTINT;
01905
01906 }
01907 }
01908 }
01909
01910
01911
01912
01913 for(i = 0; i < nbfront; ++i){
01914 x = FRONT[i].x;
01915 y = FRONT[i].y;
01916
01917 if(in[(x+1)%dimx][y] == OBSTACLE )
01918 pere[(x+1)%dimx][y] = pere[x][y];
01919
01920 if(in[(x+dimx1)%dimx][y] == OBSTACLE )
01921 pere[(x+dimx1)%dimx][y] = pere[x][y];
01922
01923 if(in[x][(y+1)%dimy] == OBSTACLE )
01924 pere[x][(y+1)%dimy] = pere[x][y];
01925
01926 if(in[x][(y+dimy1)%dimy] == OBSTACLE )
01927 pere[x][(y+dimy1)%dimy] = pere[x][y];
01928
01929 if(in[(x+1)%dimx][(y+1)%dimy] == OBSTACLE )
01930 pere[(x+1)%dimx][(y+1)%dimy] = pere[x][y];
01931
01932 if(in[(x+1)%dimx][(y+dimy1)%dimy] == OBSTACLE )
01933 pere[(x+1)%dimx][(y+dimy1)%dimy] = pere[x][y];
01934
01935 if(in[(x+dimx1)%dimx][(y+1)%dimy] == OBSTACLE )
01936 pere[(x+dimx1)%dimx][(y+1)%dimy] = pere[x][y];
01937
01938 if(in[(x+dimx1)%dimx][(y+dimy1)%dimy] == OBSTACLE )
01939 pere[(x+dimx1)%dimx][(y+dimy1)%dimy] = pere[x][y];
01940 }
01941
01942
01943
01944
01945
01946
01947
01948
01949
01950 for (x = 0; x < dimx; x++)
01951 {
01952 for (y = 0; y < dimy; y++)
01953 {
01954 voro[x][y] = 0;
01955 }
01956 }
01957
01958 while(nbfront > 0) {
01959 nbnext = 0;
01960
01961 for(i = 0; i < nbfront; ++i)
01962 {
01963 x = FRONT[i].x;
01964 y = FRONT[i].y;
01965
01966 px = pere[x][y]->x;
01967 py = pere[x][y]->y;
01968
01969 if(V[(x+1)%dimx][y] >= MAXSHORTINT ) {
01970 V[(x+1)%dimx][y] = V[x][y] + 1;
01971 nbnext++;
01972 NEXT[nbnext-1].x = (x+1)%dimx;
01973 NEXT[nbnext-1].y = y;
01974 pere[(x+1)%dimx][y] = pere[x][y];
01975 }
01976 else if(ABS(pere[(x+1)%dimx][y]->x - px) + ABS(pere[(x+1)%dimx][y]->y - py)> 3) {
01977 voro[(x+1)%dimx][y] = 1;
01978 nbskele++;
01979 }
01980 if(V[(x+dimx1)%dimx][y] >= MAXSHORTINT ) {
01981 V[(x+dimx1)%dimx][y] = V[x][y] + 1;
01982 nbnext++;
01983 NEXT[nbnext-1].x = (x+dimx1)%dimx;
01984 NEXT[nbnext-1].y = y;
01985 pere[(x+dimx1)%dimx][y] = pere[x][y];
01986 }
01987 else if(ABS(pere[(x+dimx1)%dimx][y]->x - px) + ABS(pere[(x+dimx1)%dimx][y]->y - py)> 3) {
01988 voro[(x+dimx1)%dimx][y] = 1;
01989 nbskele++;
01990 }
01991 if(V[x][(y+1)%dimy] >= MAXSHORTINT ) {
01992 V[x][(y+1)%dimy] = V[x][y] + 1;
01993 nbnext++;
01994 NEXT[nbnext-1].x = x;
01995 NEXT[nbnext-1].y = (y+1)%dimy;
01996 pere[x][(y+1)%dimy] = pere[x][y];
01997 }
01998 else if(ABS(pere[x][(y+1)%dimy]->x - px) + ABS(pere[x][(y+1)%dimy]->y - py)> 3) {
01999 voro[x][(y+1)%dimy] = 1;
02000 nbskele++;
02001 }
02002
02003 if(V[x][(y+dimy1)%dimy] >= MAXSHORTINT ) {
02004 V[x][(y+dimy1)%dimy] = V[x][y] + 1;
02005 nbnext++;
02006 NEXT[nbnext-1].x = x;
02007 NEXT[nbnext-1].y = (y+dimy1)%dimy;
02008 pere[x][(y+dimy1)%dimy] = pere[x][y];
02009 }
02010 else if(ABS(pere[x][(y+dimy1)%dimy]->x - px) + ABS(pere[x][(y+dimy1)%dimy]->y - py)> 3) {
02011 voro[x][(y+dimy1)%dimy] = 1;
02012 nbskele++;
02013 }
02014 }
02015
02016 TEMP = FRONT;
02017 FRONT = NEXT;
02018 NEXT = TEMP;
02019
02020 nbfront = nbnext;
02021 }
02022
02023 free2D( ( void** )pere );
02024 free( Mpepere );
02025 free( MNEXT );
02026 free( MFRONT );
02027
02028 }
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041
02042
02043
02044 void PL_Sequential::voro_heuristic2d(unsigned char **in,
02045 int dimx,
02046 int dimy,
02047 IntPoint goal,
02048 short int **V,
02049 unsigned char **voro,
02050 short int **heurpot,
02051 int *flag)
02052 {
02053 int k, l, dimx1, dimy1;
02054 int x, y, i;
02055 int nbvoid, nbfront, nbsavofront, nbsavonext;
02056 IntPoint **FRONT, *PFRONT, *SAVOFRONT, *SAVONEXT, *TEMP;
02057 IntPoint **MFRONT, *MPFRONT, *MSAVOFRONT, *MSAVONEXT;
02058 int xnew,ynew;
02059 short int value;
02060 short int Vmax;
02061 int narrow_flag = 0;
02062
02063 dimx1 = dimx - 1;
02064 dimy1 = dimy - 1;
02065
02066
02067 MFRONT = FRONT = (IntPoint **)calloc(50*(dimx+dimy),sizeof(IntPoint *));
02068 MPFRONT = PFRONT = (IntPoint *)calloc(50*(dimx+dimy),sizeof(IntPoint));
02069
02070 MSAVOFRONT = SAVOFRONT = (IntPoint *)calloc(50*(dimx+dimy),sizeof(IntPoint));
02071 MSAVONEXT = SAVONEXT = (IntPoint *)calloc(50*(dimx+dimy),sizeof(IntPoint));
02072
02073
02074
02075 nbvoid = 0;
02076 for ( x=0 ; x < dimx ; ++x ) {
02077 for ( y=0 ; y < dimy ; ++y ) {
02078 if(in[x][y] == OBSTACLE) {
02079 heurpot[x][y] = MAXSHORTINT;
02080 }
02081 else {
02082 heurpot[x][y] = UNDONE;
02083 nbvoid++;
02084 }
02085 }
02086 }
02087
02088
02089 heurpot[goal.x][goal.y] = 0;
02090 *flag = narrow_flag;
02091
02092
02093
02094 x = goal.x;
02095 y = goal.y;
02096
02097 if(voro[x][y] == 0) {
02098 narrow_flag = 1;
02099 }
02100
02101
02102 while(voro[x][y] == 0) {
02103 Vmax = V[x][y];
02104
02105 xnew = x;
02106 ynew = y;
02107
02108 for ( k = dimx-1 ; k <= dimx+1 ; ++k){
02109 for ( l = dimy-1 ; l <= dimy+1 ; ++l){
02110 if ( Vmax < V[(x+k)%dimx][(y+l)%dimy] ) {
02111 Vmax = V[(x+k)%dimx][(y+l)%dimy];
02112 xnew = (x+k)%dimx;
02113 ynew = (y+l)%dimy;
02114 }
02115 }
02116 }
02117 if ((xnew == x) && (ynew == y)) {
02118 if(narrow_flag == 1) {
02119 *flag = narrow_flag;
02120 }
02121 }
02122 voro[x][y] = 1;
02123 x = xnew; y = ynew;
02124 }
02125
02126 FRONT[1] = PFRONT++;
02127 FRONT[1]->x = goal.x;
02128 FRONT[1]->y = goal.y;
02129 nbfront = 1;
02130
02131
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143
02144 x = goal.x;
02145 y = goal.y;
02146
02147 nbsavofront = 0;
02148 while(nbfront > 0) {
02149 SAVOFRONT[nbsavofront].x = FRONT[1]->x;
02150 SAVOFRONT[nbsavofront].y = FRONT[1]->y;
02151 nbsavofront++;
02152 nbfront = deletemin(x, y, FRONT, nbfront, V);
02153 value = heurpot[x][y] + 1;
02154
02155 for (k = dimx-1 ; k <= dimx+1 ; ++k) {
02156 for (l = dimy-1 ; l <= dimy+1 ; ++l) {
02157 if ( voro[(x+k)%dimx][(y+l)%dimy] == 0 )
02158 continue;
02159 if ( heurpot[(x+k)%dimx][(y+l)%dimy] == UNDONE ) {
02160 heurpot[(x+k)%dimx][(y+l)%dimy] = value;
02161 nbfront = insert((x+k)%dimx, (y+l)%dimy, FRONT, PFRONT, nbfront, V);
02162 }
02163 }
02164 }
02165 }
02166
02167
02168
02169
02170
02171
02172
02173
02174
02175
02176
02177 while (nbsavofront) {
02178 nbsavonext = 0;
02179
02180
02181 for (i = 0 ; i < nbsavofront ; ++i) {
02182
02183 x = SAVOFRONT[i].x;
02184 y = SAVOFRONT[i].y;
02185
02186 value = heurpot[x][y] + 1;
02187
02188 if(heurpot[(x+1)%dimx][y] == UNDONE){
02189 nbsavonext++;
02190 heurpot[(x+1)%dimx][y] = value;
02191 SAVONEXT[nbsavonext-1].x = (x+1)%dimx;
02192 SAVONEXT[nbsavonext-1].y = y;
02193 }
02194
02195 if(heurpot[(x+dimx1)%dimx][y] == UNDONE){
02196 nbsavonext++;
02197 heurpot[(x+dimx1)%dimx][y] = value;
02198 SAVONEXT[nbsavonext-1].x = (x+dimx1)%dimx;
02199 SAVONEXT[nbsavonext-1].y = y;
02200 }
02201
02202 if(heurpot[x][(y+1)%dimy] == UNDONE){
02203 nbsavonext++;
02204 heurpot[x][(y+1)%dimy] = value;
02205 SAVONEXT[nbsavonext-1].x = x;
02206 SAVONEXT[nbsavonext-1].y = (y+1)%dimy;
02207 }
02208
02209 if(heurpot[x][(y+dimy1)%dimy] == UNDONE){
02210 nbsavonext++;
02211 heurpot[x][(y+dimy1)%dimy] = value;
02212 SAVONEXT[nbsavonext-1].x = x;
02213 SAVONEXT[nbsavonext-1].y = (y+dimy1)%dimy;
02214 }
02215 }
02216
02217 TEMP = SAVOFRONT;
02218 SAVOFRONT = SAVONEXT;
02219 SAVONEXT = TEMP;
02220
02221 nbsavofront = nbsavonext;
02222 }
02223
02224
02225 free(MFRONT);
02226 free(MPFRONT);
02227 free(MSAVOFRONT);
02228 free(MSAVONEXT);
02229
02230 }
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240
02241
02242
02243
02244 void PL_Sequential::find_path_by_heuristic( LinkPath *result,
02245 short int **heurpot,
02246 int dimx,
02247 int dimy,
02248 IntPoint *start,
02249 IntPoint *goal)
02250 {
02251 int value, x, y, xnew, ynew, i, found;
02252
02253
02254 x = start->x;
02255 y = start->y;
02256 value = heurpot[x][y];
02257 result->d[0].t = start->x;
02258 result->d[0].q = start->y;
02259 i = 1;
02260
02261
02262 while ((heurpot[x][y] > 1) && (i < result->maxdim)) {
02263 for ( xnew = x+dimx-1 ; xnew <= x+dimx+1 ; xnew++ ) {
02264 found = FALSE;
02265 for ( ynew = y + dimy - 1 ; ynew <= y + dimy + 1; ynew++ )
02266 {
02267 if (heurpot[xnew % dimx][ynew % dimy] < value) {
02268
02269 result->d[i].t = x = xnew % dimx;
02270 result->d[i].q = y = ynew % dimy;
02271 i++;
02272 found = TRUE;
02273
02274 value = heurpot[xnew % dimx][ynew % dimy];
02275 break;
02276 }
02277 }
02278 if (found) break;
02279 }
02280 }
02281
02282
02283 if (i >= result->maxdim)
02284 printf("**** Warning, Path is not correct, overflow ! ****\n");
02285
02286
02287 if (heurpot[x][y] > 0) {
02288 result->d[i].t = goal->x;
02289 result->d[i].q = goal->y;
02290 i++;
02291 }
02292
02293
02294 result->dim = i;
02295
02296 }
02297
02298
02299
02300
02301
02302
02303
02304
02305
02306
02307
02308 void PL_Sequential::retrieve_global_path(double **globalpath, int pathlen,
02309 double *startdof, double *goaldof)
02310 {
02311 int i, j, lnkno1;
02312 TQConfig para[MAX_DEGREE];
02313
02314 lnkno1 = arm_degree - 1;
02315
02316
02317 for (j = 0; j < arm_degree; j++)
02318 globalpath[0][j] = startdof[j];
02319
02320
02321 for (i = 0; i < pathlen-2; i++) {
02322
02323 para[lnkno1].q = lnkcps[lnkno1].d[i].q;
02324 para[lnkno1].t = lnkcps[lnkno1].d[i].t;
02325
02326
02327 for (j = lnkno1-1; j >= 0; j--) {
02328 para[j].t = lnkcps[j].d[ para[j+1].t].t;
02329 para[j].q = lnkcps[j].d[ para[j+1].t].q;
02330 }
02331
02332
02333 for (j = 0; j < arm_degree; j++) {
02334 globalpath[i+1][j] = index_to_rad(para[j].q);
02335 }
02336 }
02337
02338
02339 for (j = 0; j < arm_degree; j++)
02340 {
02341 globalpath[pathlen-1][j] = goaldof[j];
02342 }
02343
02344 }
02345
02346
02347
02348
02349
02350
02351
02352
02353
02354
02355
02356
02357
02358 void PL_Sequential::reparameterize_path(int bound,
02359 LinkPath *whole_path,
02360 LinkPath *result,
02361 int *ipos,
02362 int *fpos,
02363 int L1)
02364 {
02365 double lold, lnew;
02366 int i, j;
02367
02368 if (bound)
02369 lnew = dimt[L1]-3;
02370 else
02371 lnew = dimt[L1]-1;
02372 lold = whole_path->dim-1;
02373
02374 if (bound) {
02375 (*ipos)++;
02376 (*fpos)++;
02377 result->d[0].t = whole_path->d[0].t;
02378 result->d[0].q = whole_path->d[0].q;
02379 result->d[dimt[L1]-1].t = whole_path->d[whole_path->dim-1].t;
02380 result->d[dimt[L1]-1].q = whole_path->d[whole_path->dim-1].q;
02381 for (i=1; i<dimt[L1]-1; i++) {
02382 if (NO_PATH_COMPRESS) j = i-1;
02383 else j = ROUND( lold*(i-1)/lnew);
02384 result->d[i].t = whole_path->d[j].t;
02385 result->d[i].q = whole_path->d[j].q;
02386 }
02387 }
02388 else
02389 for (i =0; i<dimt[L1]; i++) {
02390 if (NO_PATH_COMPRESS) j = i;
02391 else j = ROUND(lold * (i-1) / lnew);
02392 result->d[i].t = whole_path->d[j].t;
02393 result->d[i].q = whole_path->d[j].q;
02394 }
02395
02396 result->dim = dimt[L1];
02397
02398 }
02399
02400
02401
02402
02403
02404
02405
02406
02407
02408 void PL_Sequential::allocate_space_for_Link_tq_path(int link1, int length)
02409
02410 {
02411 lnkcps[link1].maxdim = length;
02412 lnkcps[link1].dim = length;
02413 lnkcps[link1].d = (TQConfig *) malloc (length * sizeof(TQConfig));
02414 assert( lnkcps[link1].d != NULL );
02415 }
02416
02417
02418
02419
02420
02421
02422
02423
02424 void PL_Sequential::free_space_for_link_cs(int L)
02425
02426 {
02427
02428 if (tqcs.tqlink[L].bitmap)
02429 free2D((void **)tqcs.tqlink[L].bitmap);
02430
02431
02432 if (tqcs.tqlink[L].distmap)
02433 free2D((void **)tqcs.tqlink[L].distmap );
02434
02435
02436 if (tqcs.tqlink[L].voro)
02437 free2D((void **)tqcs.tqlink[L].voro );
02438
02439
02440 if (tqcs.tqlink[L].potential)
02441 free2D((void **)tqcs.tqlink[L].potential );
02442 }
02443
02444
02445
02446
02447
02448
02449
02450
02451
02452 void PL_Sequential::allocate_space_for_link_cs(int L)
02453 {
02454
02455 tqcs.tqlink[L].dimt = dimt[L];
02456 tqcs.tqlink[L].dimq = dimq[L];
02457
02458
02459 tqcs.tqlink[L].bitmap = (unsigned char **)
02460 malloc2D(dimt[L], dimq[L], sizeof(unsigned char));
02461 assert( tqcs.tqlink[L].bitmap != NULL );
02462
02463 tqcs.tqlink[L].distmap = (short int **)
02464 malloc2D(dimt[L], dimq[L], sizeof(short int));
02465 assert( tqcs.tqlink[L].distmap != NULL );
02466
02467
02468 tqcs.tqlink[L].voro = (unsigned char **)
02469 malloc2D(dimt[L], dimq[L], sizeof(unsigned char));
02470 assert( tqcs.tqlink[L].voro != NULL );
02471
02472
02473 tqcs.tqlink[L].potential = (short int **)
02474 malloc2D(dimt[L], dimq[L], sizeof(short int));
02475 assert( tqcs.tqlink[L].potential != NULL );
02476
02477 }
02478
02479
02480
02481
02482
02483
02484
02485
02486
02487 void PL_Sequential::allocate_space_for_working_tq_path()
02488 {
02489
02490 bkwdpath.maxdim = MAX_PART_PATH;
02491 bkwdpath.d = (TQConfig *)malloc(bkwdpath.maxdim*sizeof(TQConfig));
02492 assert( bkwdpath.d != NULL );
02493
02494 frwdpath.maxdim = MAX_PART_PATH;
02495 frwdpath.d = (TQConfig *)malloc(frwdpath.maxdim*sizeof(TQConfig));
02496 assert( frwdpath.d != NULL );
02497
02498 mainpath.maxdim = MAX_PART_PATH;
02499 mainpath.d = (TQConfig *)malloc(mainpath.maxdim*sizeof(TQConfig));
02500 assert( mainpath.d != NULL );
02501
02502 combpath.maxdim = MAX_CS_PATH_LEN;
02503 combpath.d = (TQConfig *)malloc(combpath.maxdim*sizeof(TQConfig));
02504 assert( combpath.d != NULL );
02505 }
02506
02507
02508
02509
02510
02511
02512
02513
02514
02515
02516 char** PL_Sequential::malloc2D (int dy,int dz,int oc)
02517 {
02518
02519 char *bv;
02520 char **v;
02521 char *p_bv;
02522 char **p_v;
02523
02524 bv = ( char* )malloc( dz * dy * sizeof( char ) * oc );
02525 assert( bv != NULL );
02526 v = ( char** )malloc( dy * sizeof( char* ) );
02527 assert ( v != NULL );
02528
02529 p_bv = bv;
02530 p_v = v;
02531
02532 for( p_bv = bv; p_bv - bv < dz * dy * oc; p_bv += dz * oc )
02533 {
02534 *p_v++ = p_bv;
02535 }
02536
02537 return(v);
02538 }
02539
02540
02541
02542
02543
02544
02545
02546
02547
02548
02549
02550
02551
02552 void PL_Sequential::write_map(char *flnm, FileType filetype,
02553 char **map, int dimx, int dimy, int bitsize)
02554 {
02555
02556 FileDescript fdt;
02557 int i;
02558 FILE *fout;
02559
02560 fout = fopen(flnm, "w");
02561 assert( fout != NULL );
02562
02563 fdt.ftype = filetype;
02564 i=0;
02565 while ( (fdt.comment[i]=flnm[i]) != '\0' ) i++;
02566
02567 fdt.octet = bitsize;
02568 fdt.dimx = dimx;
02569 fdt.dimy = dimy;
02570 fdt.dimz = 0;
02571
02572 fwrite(&fdt, sizeof(FileDescript), 1, fout);
02573 fwrite(&(map[0][0]), bitsize, dimx*dimy, fout);
02574
02575 fclose(fout);
02576
02577 }
02578
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588
02589 void PL_Sequential::write_global_path(char *flnm, int length, int degree, double **gpath)
02590 {
02591 int i, j;
02592 FILE *fout;
02593
02594 fout=fopen(flnm, "w");
02595 assert( fout != NULL );
02596
02597 fprintf(fout, "%d\n",degree);
02598 fprintf(fout, "%d\n",length);
02599
02600 for (i = 0; i < length; i++) {
02601 fprintf(fout, " Step %3d:\t", i);
02602 for (j = 0; j < degree; j++) {
02603 fprintf(fout, "%8.3f ",gpath[i][j]);
02604 }
02605 fprintf(fout,"\n");
02606 }
02607 fprintf(fout,"\n");
02608 fclose(fout);
02609 }
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619
02620
02621
02622 int PL_Sequential::smoothpath(int nb_degree, int intervals, double **dofpath, int *pathlen)
02623 {
02624 int path_len, prev_path_len;
02625 int pt, radius, delta;
02626 int intervals2;
02627 int pt1, pt2 ;
02628 double factor;
02629
02630 double **gpath;
02631
02632 gpath = (double **) malloc2D(*pathlen, arm_degree,
02633 sizeof(double) );
02634 assert( gpath != NULL );
02635
02636 factor = 1.0;
02637 path_len = *pathlen;
02638 prev_path_len=path_len;
02639
02640
02641
02642
02643 do {
02644
02645 delta = intervals / 3;
02646 pt = delta;
02647
02648
02649 do {
02650
02651
02652 radius=(int)((factor*path_len)/2);
02653 pt1=MAX(0,pt-radius);
02654 pt2=MIN(path_len-1,pt+radius);
02655
02656 intervals2 = pt2-pt1;
02657
02658
02659
02660
02661
02662
02663 if (line_free(dofpath, intervals2, pt1, pt2, gpath, &path_len))
02664 {
02665 path_len = shift_dofpath(dofpath,path_len, gpath, intervals2,
02666 pt1,pt2);
02667 }
02668
02669 pt += delta;
02670 } while (pt < path_len);
02671
02672 factor /= 2;
02673 intervals /= 2;
02674
02675 } while (delta > 2);
02676
02677 if ((sp_debug_level > 0) && ( prev_path_len != path_len))
02678 printf("Global Path shorten from %d to %d\n",
02679 prev_path_len, path_len);
02680
02681 free(gpath);
02682
02683 *pathlen = path_len;
02684
02685 return(path_len);
02686
02687 }
02688
02689
02690
02691
02692
02693
02694
02695
02696
02697
02698
02699
02700 void PL_Sequential::write_linkpath(char *flnm, FileType filetype,
02701 char *map, int dim, int bitsize)
02702
02703 {
02704 FileDescript fdt;
02705 int i;
02706 FILE *fout;
02707
02708 fout = fopen(flnm, "w");
02709 if ( fout==0) {
02710 printf("Inside write_linkpath()\n");
02711 printf("File Open Failed for file %s.\n", flnm);
02712 assert(false);
02713 }
02714
02715 fdt.ftype = filetype;
02716 i=0;
02717 while ( (fdt.comment[i]=flnm[i]) != '\0' ) i++;
02718 fdt.octet = bitsize;
02719 fdt.dimx = dim;
02720 fdt.dimy = 1;
02721 fdt.dimz = 0;
02722
02723 fwrite(&fdt, sizeof(FileDescript), 1, fout);
02724 fwrite(&(map[0]), bitsize, dim, fout);
02725
02726 fclose(fout);
02727 }
02728
02729
02730
02731
02732
02733
02734
02735
02736
02737
02738 void PL_Sequential::write_linkpathtxt(char *flnm, TQConfig *map, int dim)
02739 {
02740 int i;
02741 FILE *fout;
02742
02743
02744 fout = fopen(flnm, "w");
02745 if ( fout==0 ) {
02746 printf("Inside write_linkpathtxt()\n");
02747 printf("File Open Failed for file %s.\n", flnm);
02748 assert(false);
02749 }
02750
02751 fprintf(fout,"\n\t*** Link %s TxQ Config Path ***\n\n", flnm);
02752 fprintf(fout,"Index\tT\tQ\t\n");
02753
02754 for (i=0; i <dim; i++)
02755 fprintf(fout,"%d\t%d\t%d\t\n",i,map[i].t, map[i].q);
02756
02757 fclose(fout);
02758
02759 }
02760
02761
02762
02763
02764
02765
02766
02767
02768
02769
02770 void PL_Sequential::write_tqif(char *flnm, TQConfig tqi, TQConfig tqf)
02771 {
02772 FILE *fout;
02773
02774 fout = fopen(flnm, "w");
02775 if ( fout==0 ) {
02776 printf("Inside write_tqif()\n");
02777 printf("File Open Failed for file %s.\n", flnm);
02778 assert(false);
02779 }
02780
02781 fwrite(&tqi, sizeof(TQConfig), 1, fout);
02782 fwrite(&tqf, sizeof(TQConfig), 1, fout);
02783 fclose(fout);
02784
02785 }
02786
02787
02788
02789
02790
02791
02792
02793
02794
02795
02796
02797
02798
02799
02800
02801 int PL_Sequential::line_free(double **dofpath, int intervals, int start, int end,
02802 double **tmppath, int *path_len)
02803 {
02804 return(true);
02805 }
02806
02807
02808
02809
02810
02811
02812
02813
02814
02815
02816
02817
02818
02819
02820 int PL_Sequential::shift_dofpath(double **dofpath,
02821 int pathlen,
02822 double **gpath,
02823 int npathlen,
02824 int pt1,
02825 int pt2)
02826 {
02827 int i, iold, j, pathdiff;
02828 double diff, min_motion;
02829
02830
02831
02832
02833 min_motion = 0.9*(2*PI)/dimq[0];
02834
02835
02836 iold = 1;
02837 pathdiff = 0;
02838 for (i = 1; i < npathlen; i++) {
02839 diff = qdist(dofpath[pt1+iold-1], gpath[i]);
02840
02841
02842
02843 if (diff > min_motion) {
02844 for (j=0; j<arm_degree; j++) {
02845 dofpath[iold+pt1][j] = gpath[i][j];
02846 }
02847 iold++;
02848 }
02849 else {
02850 pathdiff++;
02851 }
02852 }
02853
02854
02855 pathdiff = pt2-pt1 - iold;
02856
02857 if (pathdiff > 0) {
02858 for (i=pt2; i< pathlen; i++ ) {
02859 for (j=0; j<arm_degree; j++)
02860 dofpath[i-pathdiff][j] = dofpath[i][j];
02861 }
02862 pathlen = pathlen- pathdiff;
02863 }
02864
02865 return(pathlen);
02866 }
02867
02868
02869
02870
02871
02872
02873
02874
02875 void PL_Sequential::free2D(void **pp)
02876 {
02877 if( pp == NULL )
02878 {
02879 return;
02880 }
02881 if( pp[ 0 ] != NULL )
02882 {
02883 free(pp[0]);
02884 pp[0] = NULL;
02885 }
02886 free(pp);
02887 pp = NULL;
02888 }
02889
02890
02891
02892
02893
02894
02895
02896
02897
02898
02899 double PL_Sequential::qdist(double *q1, double *q2)
02900 {
02901 int i;
02902 double dist;
02903
02904 dist = 0.0;
02905 for (i=0; i<arm_degree; i++)
02906 dist = dist + ABS(q1[i]-q2[i]);
02907
02908 return(dist);
02909
02910 }
02911
02912
02913
02914