40 #ifdef HAVE_SYS_TIME_H
53 PointGrid::PointGrid(
double size_x,
double size_y,
double resolution, geometry_msgs::Point origin,
double max_z,
double obstacle_range,
double min_seperation) :
54 resolution_(resolution), origin_(origin), max_z_(max_z), sq_obstacle_range_(obstacle_range * obstacle_range), sq_min_separation_(min_seperation * min_seperation)
62 double inscribed_radius,
double circumscribed_radius){
64 double outer_square_radius = circumscribed_radius;
67 geometry_msgs::Point c_lower_left, c_upper_right;
68 c_lower_left.x = position.x - outer_square_radius;
69 c_lower_left.y = position.y - outer_square_radius;
71 c_upper_right.x = position.x + outer_square_radius;
72 c_upper_right.y = position.y + outer_square_radius;
83 double inner_square_radius = sqrt((inscribed_radius * inscribed_radius) / 2.0);
86 geometry_msgs::Point i_lower_left, i_upper_right;
87 i_lower_left.x = position.x - inner_square_radius;
88 i_lower_left.y = position.y - inner_square_radius;
90 i_upper_right.x = position.x + inner_square_radius;
91 i_upper_right.y = position.y + inner_square_radius;
94 for(
unsigned int i = 0; i <
points_.size(); ++i){
95 list<geometry_msgs::Point32>* cell_points =
points_[i];
96 if(cell_points != NULL){
97 for(list<geometry_msgs::Point32>::iterator it = cell_points->begin(); it != cell_points->end(); ++it){
98 const geometry_msgs::Point32& pt = *it;
101 if(pt.x > c_lower_left.x && pt.x < c_upper_right.x && pt.y > c_lower_left.y && pt.y < c_upper_right.y){
103 if(pt.x > i_lower_left.x && pt.x < i_upper_right.x && pt.y > i_lower_left.y && pt.y < i_upper_right.y)
125 bool all_left =
false;
126 bool all_right =
false;
127 for(
unsigned int i = 0; i < poly.size() - 1; ++i){
129 if(
orient(poly[i], poly[i + 1], pt) > 0){
142 if(
orient(poly[poly.size() - 1], poly[0], pt) > 0){
155 vector< list<geometry_msgs::Point32>* >& points){
159 geometry_msgs::Point upper_left, lower_right;
160 upper_left.x = lower_left.x;
161 upper_left.y = upper_right.y;
162 lower_right.x = upper_right.x;
163 lower_right.y = lower_left.y;
172 unsigned int lower_left_index =
gridIndex(gx, gy);
176 unsigned int lower_right_index =
gridIndex(gx, gy);
180 unsigned int upper_left_index =
gridIndex(gx, gy);
183 unsigned int x_steps = lower_right_index - lower_left_index + 1;
184 unsigned int y_steps = (upper_left_index - lower_left_index) /
width_ + 1;
195 vector< list<geometry_msgs::Point32> >::iterator cell_iterator =
cells_.begin() + lower_left_index;
197 for(
unsigned int i = 0; i < y_steps; ++i){
198 for(
unsigned int j = 0; j < x_steps; ++j){
199 list<geometry_msgs::Point32>& cell = *cell_iterator;
202 points.push_back(&cell);
207 cell_iterator +=
width_ - (x_steps - 1);
224 unsigned int pt_index =
gridIndex(gx, gy);
227 cells_[pt_index].push_back(pt);
235 for(list<geometry_msgs::Point32>::const_iterator it =
cells_[index].begin(); it !=
cells_[index].end(); ++it){
249 geometry_msgs::Point lower_left, upper_right;
253 geometry_msgs::Point32 check_point;
255 double neighbor_sq_dist =
DBL_MAX;
259 check_point.x = lower_left.x;
260 check_point.y = pt.y;
267 if(gx > 0 && gy <
height_ - 1){
268 check_point.x = lower_left.x;
269 check_point.y = upper_right.y;
277 check_point.x = pt.x;
278 check_point.y = upper_right.y;
286 check_point.x = upper_right.x;
287 check_point.y = upper_right.y;
295 check_point.x = upper_right.x;
296 check_point.y = pt.y;
303 if(gx < width_ - 1 && gy > 0){
304 check_point.x = upper_right.x;
305 check_point.y = lower_left.y;
313 check_point.x = pt.x;
314 check_point.y = lower_left.y;
321 if(gx > 0 && gy > 0){
322 check_point.x = lower_left.x;
323 check_point.y = lower_left.y;
332 return neighbor_sq_dist;
336 const vector<Observation>& observations,
const vector<PlanarLaserScan>& laser_scans){
338 if(laser_scans.empty())
344 for(vector<Observation>::const_iterator it = observations.begin(); it != observations.end(); ++it){
346 const sensor_msgs::PointCloud2& cloud = *(obs.
cloud_);
352 geometry_msgs::Point32 pt;
353 for(; iter_x != iter_x.
end(); ++iter_x, ++iter_y, ++iter_z){
359 double sq_dist = (*iter_x - obs.
origin_.x) * (*iter_x - obs.
origin_.x)
379 if(laser_scan.
cloud.points.size() == 0)
383 geometry_msgs::Point lower_left, upper_right;
384 lower_left.x = laser_scan.
origin.x;
385 lower_left.y = laser_scan.
origin.y;
386 upper_right.x = laser_scan.
origin.x;
387 upper_right.y = laser_scan.
origin.y;
389 for(
unsigned int i = 0; i < laser_scan.
cloud.points.size(); ++i){
390 lower_left.x =
min((
double)lower_left.x, (
double)laser_scan.
cloud.points[i].x);
391 lower_left.y =
min((
double)lower_left.y, (
double)laser_scan.
cloud.points[i].y);
392 upper_right.x = max((
double)upper_right.x, (
double)laser_scan.
cloud.points[i].x);
393 upper_right.y = max((
double)upper_right.y, (
double)laser_scan.
cloud.points[i].y);
403 for(
unsigned int i = 0; i <
points_.size(); ++i){
404 list<geometry_msgs::Point32>* cell_points =
points_[i];
405 if(cell_points != NULL){
406 list<geometry_msgs::Point32>::iterator it = cell_points->begin();
407 while(it != cell_points->end()){
408 const geometry_msgs::Point32& pt = *it;
412 it = cell_points->erase(it);
422 if(!laser_scan.
cloud.points.empty()){
424 double v1_x = laser_scan.
cloud.points[0].x - laser_scan.
origin.x;
425 double v1_y = laser_scan.
cloud.points[0].y - laser_scan.
origin.y;
426 double v2_x = pt.x - laser_scan.
origin.x;
427 double v2_y = pt.y - laser_scan.
origin.y;
429 double perp_dot = v1_x * v2_y - v1_y * v2_x;
430 double dot = v1_x * v2_x + v1_y * v2_y;
433 double vector_angle = atan2(perp_dot,
dot);
437 vector_angle = 2 * M_PI + vector_angle;
442 if(vector_angle < 0 || vector_angle >= total_rads)
446 unsigned int index = (
unsigned int) (vector_angle / laser_scan.
angle_increment);
449 if(index >= laser_scan.
cloud.points.size() - 1){
454 if(
orient(laser_scan.
cloud.points[index], laser_scan.
cloud.points[index + 1], pt) > 0){
470 for(
unsigned int i = 0; i <
cells_.size(); ++i){
471 for(list<geometry_msgs::Point32>::iterator it =
cells_[i].begin(); it !=
cells_[i].end(); ++it){
481 for(
unsigned int i = 0; i <
cells_.size(); ++i){
482 for(list<geometry_msgs::Point32>::iterator it =
cells_[i].begin(); it !=
cells_[i].end(); ++it, ++iter_x, ++iter_y, ++iter_z){
494 geometry_msgs::Point lower_left, upper_right;
495 lower_left.x = poly[0].x;
496 lower_left.y = poly[0].y;
497 upper_right.x = poly[0].x;
498 upper_right.y = poly[0].y;
501 for(
unsigned int i = 1; i < poly.size(); ++i){
502 lower_left.x =
min(lower_left.x, poly[i].x);
503 lower_left.y =
min(lower_left.y, poly[i].y);
504 upper_right.x = max(upper_right.x, poly[i].x);
505 upper_right.y = max(upper_right.y, poly[i].y);
508 ROS_DEBUG(
"Lower: (%.2f, %.2f), Upper: (%.2f, %.2f)\n", lower_left.x, lower_left.y, upper_right.x, upper_right.y);
516 for(
unsigned int i = 0; i <
points_.size(); ++i){
517 list<geometry_msgs::Point32>* cell_points =
points_[i];
518 if(cell_points != NULL){
519 list<geometry_msgs::Point32>::iterator it = cell_points->begin();
520 while(it != cell_points->end()){
521 const geometry_msgs::Point32& pt = *it;
525 it = cell_points->erase(it);
535 const geometry_msgs::Point& u1,
const geometry_msgs::Point& u2, geometry_msgs::Point& result){
537 double a1 = v2.y - v1.y;
538 double b1 = v1.x - v2.x;
539 double c1 = a1 * v1.x + b1 * v1.y;
542 double a2 = u2.y - u1.y;
543 double b2 = u1.x - u2.x;
544 double c2 = a2 * u1.x + b2 * u1.y;
546 double det = a1 * b2 - a2 * b1;
552 result.x = (b2 * c1 - b1 * c2) / det;
553 result.y = (a1 * c2 - a2 * c1) / det;