Go to the documentation of this file.00001 #include "hexagon_map/hexagon_map.h"
00002 #include <sensor_msgs/PointCloud.h>
00003 #include <sensor_msgs/PointCloud2.h>
00004
00005 hexagon_map::hexagon_map(float R,float resolution)
00006 {
00007 std::cout<<"R "<<R<<" resolution "<<Resulution_L<<std::endl;
00008
00009 Cover_R = R;
00010 Resulution_L = resolution;
00011
00012 RectangleL = Resulution_L * 1.5;
00013 RectangleW = tan(60 * 3.1415926/180) * Resulution_L;
00014
00015 widthnum = ceil(Cover_R/(sqrt(21.0) * Resulution_L));
00016
00017 std::cout<<"widthnum "<<widthnum<<std::endl;
00018
00019 limitednum = 1;
00020 int lastHexcount = 1;
00021 for(int i=2;i<=widthnum;i++)
00022 {
00023
00024
00025 limitednum = limitednum + 6 * lastHexcount;
00026
00027 lastHexcount = (i - 1) * 6;
00028 }
00029
00030 std::cout<<"limitednum "<<limitednum<<std::endl;
00031
00032 int occupymapwidth = ceil(4.5 * Resulution_L * ( widthnum - 1) + 3 * Resulution_L);
00033 int occupymapheight = ceil(sin(60 * 3.1415926 /180) * 5 * Resulution_L * (widthnum - 1) + sin(60 * 3.1415926 /180) * 4 * Resulution_L);
00034
00035 int occpymapsize = 0;
00036 if(occupymapwidth>occupymapheight)
00037 {
00038 occpymapsize = occupymapwidth;
00039 }
00040 else
00041 {
00042 occpymapsize = occupymapheight;
00043 }
00044
00045 if(occpymapsize%2!=0)
00046 {
00047 occpymapsize = occpymapsize + 1;
00048 }
00049
00050 gridmap.header.frame_id = "/rslidar";
00051 gridmap.header.stamp = ros::Time::now();
00052 gridmap.info.resolution = 0.01;
00053 gridmap.info.width = int(float(occpymapsize * 2)/0.01);
00054 gridmap.info.height = int(float(occpymapsize * 2)/0.01);
00055 gridmap.info.origin.position.x = -float(occpymapsize);
00056 gridmap.info.origin.position.y = -float(occpymapsize);
00057 gridmap.info.origin.position.z = 0.0;
00058
00059 std::cout<<"gridmap.info.resolution "<<gridmap.info.resolution
00060 <<" gridmap.info.width "<<gridmap.info.width<<" gridmap.info.height "
00061 <<gridmap.info.height<<" gridmap.info.origin.position "<<gridmap.info.origin.position.x
00062 <<" "<< gridmap.info.origin.position.y<<std::endl;
00063
00064 gridmap.data.assign(gridmap.info.width*gridmap.info.height,0);
00065 flagmap.assign(gridmap.info.width*gridmap.info.height,0);
00066
00067 buildmap();
00068 }
00069
00070 hexagon_map::~hexagon_map()
00071 {
00072
00073
00074
00075 }
00076
00077 CPoint2d hexagon_map::JudgeHexCenpoint(CPoint2d &Checkpoint)
00078 {
00079 int cx = floor((Checkpoint.x - 0.0)/RectangleL);
00080 int cy = floor((Checkpoint.y - 0.0)/RectangleW);
00081
00082
00083
00084
00085
00086 CPoint2d finalpoint;
00087 CPoint2d hexcenpoint1;
00088 CPoint2d hexcenpoint2;
00089 CPoint2d hexcenpoint3;
00090 float dis = 10000;
00091
00092 if(cx%2==0)
00093 {
00094 hexcenpoint1.x = cx * RectangleL;
00095 hexcenpoint1.y = cy * RectangleW;
00096
00097 float temdis = Checkpoint.Dist(hexcenpoint1);
00098 if(temdis<dis)
00099 {
00100 finalpoint = hexcenpoint1;
00101 dis = temdis;
00102 }
00103
00104
00105 hexcenpoint2.x = cx * RectangleL;
00106 hexcenpoint2.y = (cy + 1) * RectangleW;
00107
00108 float temdis1 = Checkpoint.Dist(hexcenpoint2);
00109 if(temdis1<dis)
00110 {
00111 finalpoint = hexcenpoint2;
00112 dis = temdis1;
00113 }
00114
00115
00116 hexcenpoint3.x = (cx + 1) * RectangleL;
00117 hexcenpoint3.y = (cy + 0.5) * RectangleW;
00118
00119 float temdis2 = Checkpoint.Dist(hexcenpoint3);
00120 if(temdis2<dis)
00121 {
00122 finalpoint = hexcenpoint3;
00123 dis = temdis2;
00124 }
00125
00126
00127
00128
00129 }
00130 else
00131 {
00132 hexcenpoint1.x = cx * RectangleL;
00133 hexcenpoint1.y = (cy + 0.5) * RectangleW;
00134
00135
00136 float temdis = Checkpoint.Dist(hexcenpoint1);
00137 if(temdis<dis)
00138 {
00139 finalpoint = hexcenpoint1;
00140 dis = temdis;
00141 }
00142
00143 hexcenpoint2.x = (cx + 1) * RectangleL;
00144 hexcenpoint2.y = cy * RectangleW;
00145
00146 float temdis1 = Checkpoint.Dist(hexcenpoint2);
00147 if(temdis1<dis)
00148 {
00149 finalpoint = hexcenpoint2;
00150 dis = temdis1;
00151 }
00152
00153 hexcenpoint3.x = (cx + 1) * RectangleL;
00154 hexcenpoint3.y = (cy + 1) * RectangleW;
00155
00156 float temdis2 = Checkpoint.Dist(hexcenpoint3);
00157 if(temdis2<dis)
00158 {
00159 finalpoint = hexcenpoint3;
00160 dis = temdis2;
00161 }
00162
00163
00164
00165
00166 }
00167
00168 return finalpoint;
00169 }
00170
00171 void hexagon_map::drawplogon(const float &C_x,const float &C_y,const float &L,int count)
00172 {
00173 int dx = floor((C_x - gridmap.info.origin.position.x)/gridmap.info.resolution);
00174 int dy = floor((C_y - gridmap.info.origin.position.y)/gridmap.info.resolution);
00175
00176 float h = L * cos(30.0/180.0*3.1415926);
00177
00178 int h_index = ceil(h/gridmap.info.resolution);
00179
00180
00181 for(int i=-h_index;i<=h_index;i++)
00182 {
00183 float this_h = h - i * gridmap.info.resolution;
00184
00185 if(i<0)
00186 {
00187 this_h = h + i * gridmap.info.resolution;
00188 }
00189 else
00190 {
00191 this_h = h - i * gridmap.info.resolution;
00192
00193 }
00194
00195 float addlen = this_h * tan(30.0/180.0*3.1415926);
00196
00197 int addlen_index = ceil(addlen/gridmap.info.resolution);
00198
00199 int L_index = ceil(L/2/gridmap.info.resolution);
00200
00201 int half_index = addlen_index + L_index;
00202
00203 for(int j=-half_index;j<=half_index;j++)
00204 {
00205 int deta_x = dx + j;
00206 int deta_y = dy + i;
00207 int index = deta_x + deta_y * gridmap.info.width;
00208
00209
00210 gridmap.data[index] = count;
00211
00212 }
00213 }
00214 }
00215
00216 bool hexagon_map::isInpointset(CPoint2d &Checkpoint)
00217 {
00218 for(int i = 0;i<Pointset.size();i++)
00219 {
00220 CPoint2d tempoint = Pointset[i];
00221
00222 float dis = (tempoint.x - Checkpoint.x) * (tempoint.x - Checkpoint.x)
00223 + (tempoint.y - Checkpoint.y) * (tempoint.y - Checkpoint.y);
00224
00225 if(dis<0.0001)
00226 {
00227 return true;
00228 }
00229 }
00230 return false;
00231 }
00232
00233 void hexagon_map::drawallmap()
00234 {
00235 int singleangle[]={30,90,150,210,270,330};
00236 float r1 = Resulution_L * tan(60*3.1415926/180);
00237 int count = 10;
00238 for(int i = 0;i<Pointset.size();i++)
00239 {
00240
00241 drawplogon(Pointset[i].x,Pointset[i].y,Resulution_L,count);
00242
00243
00244 for(int j=0;j<6;j++)
00245 {
00246 CPoint2d temppoint;
00247 temppoint.x = Pointset[i].x + r1 * cos(singleangle[j] * 3.1415926/180);
00248 temppoint.y = Pointset[i].y + r1 * sin(singleangle[j] * 3.1415926/180);
00249
00250 drawplogon(temppoint.x,temppoint.y,Resulution_L,count);
00251 }
00252 }
00253 }
00254
00255 void hexagon_map::buildmap()
00256 {
00257 CPoint2d Centerpoint;
00258 Centerpoint.x = 0.0;
00259 Centerpoint.y = 0.0;
00260
00261 int limitcount = 0;
00262
00263
00264 int d_x = (Centerpoint.x - gridmap.info.origin.position.x)/gridmap.info.resolution;
00265 int d_y = (Centerpoint.y - gridmap.info.origin.position.y)/gridmap.info.resolution;
00266 int inde_x = d_x + d_y * gridmap.info.width;
00267
00268 flagmap[inde_x] = 1;
00269 limitcount++;
00270
00271 Pointset.push_back(Centerpoint);
00272
00273 Pointqueue.push(Centerpoint);
00274
00275 while(!Pointqueue.empty())
00276 {
00277 int size = Pointset.size();
00278 int size1 = Pointqueue.size();
00279
00280
00281
00282
00283 if(limitcount==limitednum)
00284 {
00285 break;
00286
00287 }
00288
00289 int angle[]={60,120,180,240,300};
00290
00291 CPoint2d Genepoint;
00292 Genepoint = Pointqueue.front();
00293 Pointqueue.pop();
00294
00295
00296
00297 CPoint2d tempoint;
00298
00299 tempoint.x = Genepoint.x + 4.5 * Resulution_L;
00300 tempoint.y = Genepoint.y + -Resulution_L * tan(60*3.1415926/180)/2;
00301 tempoint.pointangle = atan2(-Resulution_L * tan(60*3.1415926/180)/2, 4.5 * Resulution_L);
00302 float R = sqrt(4.5 * Resulution_L * 4.5 * Resulution_L + -Resulution_L * tan(60*3.1415926/180)/2 * -Resulution_L * tan(60*3.1415926/180)/2);
00303
00304 int dx_ = (tempoint.x - gridmap.info.origin.position.x)/gridmap.info.resolution;
00305 int dy_ = (tempoint.y - gridmap.info.origin.position.y)/gridmap.info.resolution;
00306 int index_ = dx_ + dy_ * gridmap.info.width;
00307
00308
00309 limitcount++;
00310
00311 if(flagmap[index_]!=0 || isInpointset(tempoint))
00312 {
00313
00314 }
00315 else
00316 {
00317 Pointset.push_back(tempoint);
00318 Pointqueue.push(tempoint);
00319 flagmap[index_] = 1;
00320 }
00321
00322
00323
00324 for(int i=0;i<5;i++)
00325 {
00326 CPoint2d Othertempoint;
00327 Othertempoint.x = Genepoint.x + R * cos((tempoint.pointangle * 180 /3.1415926 + angle[i])*3.1415926/180);
00328 Othertempoint.y = Genepoint.y + R * sin((tempoint.pointangle * 180 /3.1415926 + angle[i])*3.1415926/180);
00329
00330
00331
00332 int d_x2 = (Othertempoint.x - gridmap.info.origin.position.x)/gridmap.info.resolution;
00333 int d_y2 = (Othertempoint.y - gridmap.info.origin.position.y)/gridmap.info.resolution;
00334 int inde_x2 = d_x2 + d_y2 * gridmap.info.width;
00335
00336
00337
00338 limitcount++;
00339
00340 if(flagmap[inde_x2]!=0 || isInpointset(Othertempoint))
00341 {
00342
00343 continue;
00344 }
00345 else
00346 {
00347 Pointset.push_back(Othertempoint);
00348 Pointqueue.push(Othertempoint);
00349 flagmap[inde_x2] = 1;
00350 }
00351 }
00352 }
00353
00354 std::cout<<"--------------- Pointset.size() "<<Pointset.size()<<std::endl;
00355
00356 for(int i=0;i<Pointset.size();i++)
00357 {
00358 std::cout<<"Pointset "<<Pointset[i].x<<" "<<Pointset[i].y<<std::endl;
00359
00360 }
00361
00362
00363 drawallmap();
00364 }
00365
00366
00367 int hexagon_map::findindexoccpmap(CPoint2d becheckp)
00368 {
00369
00370 for(int i=0;i<HexGridData.size();i++)
00371 {
00372
00373 float dis = HexGridData[i].Centerp.Dist(becheckp);
00374
00375 if(dis<0.0001)
00376 {
00377 return i;
00378 }
00379 }
00380 return -1;
00381 }
00382
00383
00384 void hexagon_map::updateMap(sensor_msgs::PointCloud2 &pointClouds_msg)
00385 {
00386 if(!HexGridData.empty())
00387 {
00388 HexGridData.clear();
00389 }
00390
00391 pcl::PointCloud<pcl::PointXYZI>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZI>);
00392 pcl::moveFromROSMsg(pointClouds_msg,*cloud);
00393
00394 float minx = gridmap.info.origin.position.x;
00395 float miny = gridmap.info.origin.position.y;
00396 float maxx = minx * (-1);
00397 float maxy = miny * (-1);
00398
00399 std::cout<<"cloud->points.size() "<<cloud->points.size()<<std::endl;
00400 std::cout<<" minx miny"<<minx<<" miny "<<miny<<" maxx "<<maxx<<" maxy "<<maxy<<std::endl;
00401
00402
00403 pcl::PointCloud<pcl::PointXYZI>::Ptr prcloud (new pcl::PointCloud<pcl::PointXYZI>);
00404 for(int i=0;i<cloud->points.size();i++)
00405 {
00406 if(cloud->points[i].x < minx || cloud->points[i].y < miny
00407 || cloud->points[i].x > maxx || cloud->points[i].y > maxy)
00408 {
00409
00410 continue;
00411 }
00412
00413 prcloud->points.push_back(cloud->points[i]);
00414 }
00415
00416 std::cout<<"prcloud "<<prcloud->points.size()<<std::endl;
00417
00418
00419 HexGridData.assign(gridmap.info.width * gridmap.info.height,GridData());
00420
00421
00422 for(int j=0;j<prcloud->points.size();j++)
00423 {
00424 CPoint2d Checkpoint;
00425 Checkpoint.x = prcloud->points[j].x;
00426 Checkpoint.y = prcloud->points[j].y;
00427
00428 CPoint2d Centerpoint;
00429 Centerpoint = JudgeHexCenpoint(Checkpoint);
00430
00431 int dx,dy;
00432 dx = (Centerpoint.x - gridmap.info.origin.position.x)/gridmap.info.resolution;
00433 dy = (Centerpoint.y - gridmap.info.origin.position.y)/gridmap.info.resolution;
00434
00435 int index = dx + dy * gridmap.info.width;
00436 int index_up = index + 1;
00437 int index_down = index - 1;
00438
00439 if(index<0 || index>(gridmap.info.width * gridmap.info.height)
00440 || (gridmap.data[index] == 0))
00441 {
00442
00443 continue;
00444 }
00445 else
00446 {
00447
00448 if(index_up<(gridmap.info.width * gridmap.info.height) && gridmap.data[index_up] == 0)
00449 {
00450 continue;
00451 }
00452
00453 if(index_down>0 && gridmap.data[index_down] == 0)
00454 {
00455 continue;
00456 }
00457
00458
00459
00460 if(HexGridData[index].top<prcloud->points[j].z)
00461 {
00462 HexGridData[index].top = prcloud->points[j].z;
00463 }
00464
00465 if(HexGridData[index].bottom>prcloud->points[j].z)
00466 {
00467 HexGridData[index].bottom = prcloud->points[j].z;
00468 }
00469
00470 HexGridData[index].numPoint++;
00471 HexGridData[index].Centerp = Centerpoint;
00472
00473 }
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 drawallmap();
00617
00618 float threshold_height = 0.1;
00619
00620 int obstacle = 250;
00621 std::cout<<"HexGridData.size() "<<HexGridData.size()<<std::endl;
00622
00623 for(int i=0;i<HexGridData.size();i++)
00624 {
00625 if(HexGridData[i].numPoint==0)
00626 {
00627 continue;
00628 }
00629
00630 if((HexGridData[i].top - HexGridData[i].bottom)>threshold_height)
00631 {
00632 drawplogon(HexGridData[i].Centerp.x,HexGridData[i].Centerp.y,Resulution_L,obstacle);
00633 }
00634
00635 }
00636
00637
00638 }
00639
00640 nav_msgs::OccupancyGrid hexagon_map::getmap()
00641 {
00642 return gridmap;
00643 }