48 #include "geometry_msgs/Pose.h"
51 #include "nav_msgs/OccupancyGrid.h"
89 return x == v ||
y == v;
97 printf(
"[%d,%d]\n",
x,
y);
112 int dx = this->
x - other.
x;
113 int dy = this->
y - other.
y;
114 return sqrt((dx * dx) + (dy * dy));
120 int dx = rA.
x - rB.
x;
121 int dy = rA.
y - rB.
y;
122 return (dx * dx) + (dy * dy);
125 #define INLINE_OPERATION_IMPLEMENTATION_INT(TTYPE)\
126 static inline TTYPE operator/( const TTYPE& vecA, const TTYPE& vecB ){ return (TTYPE(vecA)/=vecB); } \
127 static inline TTYPE operator/( const TTYPE& vec , const int value ){ return (TTYPE(vec)/=value); } \
128 static inline TTYPE operator/( const int value, const TTYPE& vec ){ return (TTYPE::Create(value)/=vec); } \
129 static inline TTYPE operator*( const TTYPE& vecA, const TTYPE& vecB ){ return (TTYPE(vecA)*=vecB); } \
130 static inline TTYPE operator*( const TTYPE& vec , const int value ){ return (TTYPE(vec)*=value); } \
131 static inline TTYPE operator*( const int value, const TTYPE& vec ){ return (TTYPE::Create(value)*=vec); } \
132 static inline TTYPE operator+( const TTYPE& vecA, const TTYPE& vecB ){ return (TTYPE(vecA)+=vecB); } \
133 static inline TTYPE operator+( const TTYPE& vec , const int value ){ return (TTYPE(vec)+=value); } \
134 static inline TTYPE operator+( const int value, const TTYPE& vec ){ return (TTYPE::Create(value)+=vec); } \
135 static inline TTYPE operator-( const TTYPE& vecA, const TTYPE& vecB ){ return (TTYPE(vecA)-=vecB); } \
136 static inline TTYPE operator-( const TTYPE& vec , const int value ){ return (TTYPE(vec)-=value); } \
137 static inline TTYPE operator-( const int value, const TTYPE& vec ){ return (TTYPE::Create(value)-=vec); }
142 #define MATRIX_THROW_OUT_OF_BOUND_EXCEPTION
145 #if defined(MATRIX_THROW_OUT_OF_BOUND_EXCEPTION)
147 template <
typename T>
150 template <
typename _T>
160 if (x < 0 || x >=
width)
161 throw std::overflow_error(
"column index is out of bounds.");
165 if (x < 0 || x >=
width)
166 throw std::overflow_error(
"column index is out of bounds.");
176 template <
typename T>
class Matrix {
206 throw std::out_of_range(
"matrices need to be of the same size.");
210 throw std::out_of_range(
"box is out of range.");
216 for(
int y = start.
y; y < end.
y; ++y) {
220 for(
int x = start.
x; x < end.
x; ++x) {
222 val = pOther.
array[index];
228 if(val != 50 &&
array[index] == 50) {
246 Vec2i start = rStart;
248 if(start.
x < 0) start.
x = 0;
250 if(start.
y < 0) start.
y = 0;
252 if(end.
x < 0) end.
x = 0;
254 if(end.
y < 0) end.
y = 0;
259 for(
int y = start.
y; y < end.
y + 1; ++y) {
260 for(
int x = start.
x; x < end.
x + 1; ++x) {
267 #if defined(MATRIX_THROW_OUT_OF_BOUND_EXCEPTION)
270 throw std::overflow_error(
"row index is out of bounds.");
275 throw std::overflow_error(
"row index is out of bounds.");
294 if (this->size ==
size)
297 throw std::bad_alloc();
306 if(rCoord.
x < 0)
return false;
307 if(rCoord.
y < 0)
return false;
315 for(
int i=0;i<s;++i) {
324 for(
int x = 0; x < rInput.
size.
width; ++x) {
325 if(rInput[y][x] == 100)
327 else if(rInput[y][x] == 50)
339 for(
int x = 0; x < rInput.
size.
width; ++x) {
340 printf(
"%c", rInput[y][x]);
351 for(
int x = 0; x < rInput.
size.
width; ++x) {
352 if(rInput[y][x] == 100)
354 else if(rInput[y][x] == 50)
361 cmap[rPos.
y][rPos.
x] =
'R';
367 std::list<Vec2i>& rPath,
374 for(
int x = 0; x < rInput.
size.
width; ++x) {
375 if(rInput[y][x] == 100)
376 path_map[y][x] = 251;
377 else if(rInput[y][x] == 50)
378 path_map[y][x] =
'?';
380 path_map[y][x] =
'.';
385 std::list<Vec2i>::iterator it = rPath.begin();
386 for(;it != rPath.end(); ++it) {
388 path_map[p.
y][p.
x] =
'*';
391 path_map[rStart.
y][rStart.
x] =
'S';
392 path_map[rEnd.
y][rEnd.
x] =
'E';
401 inline void PoseToVector3(
const geometry_msgs::Pose& rInput, tf::Vector3& rOut) {
402 rOut.setX(rInput.position.x);
403 rOut.setY(rInput.position.y);
404 rOut.setZ(rInput.position.z);
408 inline void Vector3ToPose(
const tf::Vector3& rInput, geometry_msgs::Pose& rOutPose) {
409 rOutPose.position.x = rInput.getX();
410 rOutPose.position.y = rInput.getY();
411 rOutPose.position.z = rInput.getZ();
414 inline void WorldToMap(nav_msgs::OccupancyGrid& rOcc, geometry_msgs::Pose& rWorld,
Vec2i& rOutput) {
415 double res = rOcc.info.resolution;
416 rOutput.
x = (int)((rWorld.position.x - rOcc.info.origin.position.x)/res);
417 rOutput.
y = (int)((rWorld.position.y - rOcc.info.origin.position.y)/res);
420 inline void WorldToMap(nav_msgs::OccupancyGrid& rOcc, geometry_msgs::Pose& rWorld, tf::Vector3& rOutput) {
421 double res = rOcc.info.resolution;
422 rOutput.setX((
int)((rWorld.position.x - rOcc.info.origin.position.x)/res));
423 rOutput.setY((
int)((rWorld.position.y - rOcc.info.origin.position.y)/res));
426 inline void WorldToMap(nav_msgs::OccupancyGrid& rOcc, tf::Vector3& rWorld,
Vec2i& rOutput) {
427 double res = rOcc.info.resolution;
428 rOutput.
x = (int)((rWorld.getX() - rOcc.info.origin.position.x)/res);
429 rOutput.
y = (int)((rWorld.getY() - rOcc.info.origin.position.y)/res);
432 inline void WorldToMap(nav_msgs::OccupancyGrid& rOcc, tf::Vector3& rWorld, tf::Vector3&rOutput) {
433 double res = rOcc.info.resolution;
434 rOutput.setX((
int)((rWorld.getX() - rOcc.info.origin.position.x)/res));
435 rOutput.setY((
int)((rWorld.getY() - rOcc.info.origin.position.y)/res));
439 inline void MapToWorld(nav_msgs::OccupancyGrid& rOcc,
Vec2i& rMap, geometry_msgs::Pose& rWorld) {
440 double res = rOcc.info.resolution;
441 double d_x = (double)rMap.
x;
442 double d_y = (
double)rMap.
y;
443 rWorld.position.x = rOcc.info.origin.position.x + (d_x * res) + (res / 2.0);
444 rWorld.position.y = rOcc.info.origin.position.y + (d_y * res) + (res / 2.0);
445 rWorld.position.z = 0.0;
449 double res = rOcc.info.resolution;
450 double d_x = (double)rMap.
x;
451 double d_y = (
double)rMap.
y;
452 rWorld.
x = rOcc.info.origin.position.x + (d_x * res) + (res / 2.0);
453 rWorld.
y = rOcc.info.origin.position.y + (d_y * res) + (res / 2.0);
456 inline void MapToWorld(nav_msgs::OccupancyGrid& rOcc,
Vec2i& rMap, tf::Vector3& rWorld) {
457 double res = rOcc.info.resolution;
458 double d_x = (double)rMap.
x;
459 double d_y = (
double)rMap.
y;
460 rWorld.setX(rOcc.info.origin.position.x + (d_x * res) + (res / 2.0));
461 rWorld.setY(rOcc.info.origin.position.y + (d_y * res) + (res / 2.0));
471 std::vector<int8_t>& rArr,
475 const int8_t& occupancyThreshold = 90,
476 const bool& ignoreObstacles=
false) {
479 int r_squared = rRadius * rRadius;
480 for(
int y = rY - rRadius; y <= rY + rRadius; ++y) {
481 for(
int x = rX - rRadius; x <= rX + rRadius; ++x) {
482 if(x >= rWidth || y >= rHeight ||x < 0 || y < 0)
continue;
484 index = y * rWidth + x;
488 if(ignoreObstacles ==
true && rArr[index] >= occupancyThreshold)
continue;
496 if(dx + dy <= r_squared) rArr[index] = rVal;