YARP
Yet Another Robot Platform
MapGrid2DInfo.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #define _USE_MATH_DEFINES
10 
11 #include <cmath>
12 #include <yarp/dev/MapGrid2DInfo.h>
13 
14 using namespace yarp::dev;
15 using namespace yarp::dev::Nav2D;
16 using namespace yarp::sig;
17 using namespace yarp::os;
18 using namespace yarp::math;
19 using namespace std;
20 
21 #ifndef DEG2RAD
22 #define DEG2RAD M_PI/180.0
23 #endif
24 
26 {
27  x = 0;
28  y = 0;
29  theta = 0;
30 }
31 
32 void MapGrid2DOrigin::setOrigin(double x_init, double y_init, double t_init)
33 {
34  x = x_init;
35  y = y_init;
36  theta = fmod(t_init, 360.0);
37  sa = sin (theta * DEG2RAD);
38  ca = cos (theta * DEG2RAD);
39 }
40 
41 MapGrid2DOrigin::MapGrid2DOrigin(double x_init, double y_init, double t_init)
42 {
43  setOrigin(x_init, y_init, t_init);
44 }
45 
47 {
48  if (x != other.x) return true;
49  if (y != other.y) return true;
50  if (theta != other.theta) return true; //should I check for 360 wrap?
51  return false;
52 }
53 
54 //--------------------------------------------------------------------
55 
57 {
58  m_map_name = "";
59  m_resolution = 0;
60  m_origin = MapGrid2DOrigin(0, 0, 0);
61  m_width = 0;
62  m_height = 0;
63 }
64 
66 {
67  //convert a cell (from the upper-left corner) to the map reference frame (located in m_origin, measured in meters)
68  //beware: the location of m_origin is referred to the lower-left corner (ROS convention)
69  XYWorld v1;
70  XYWorld v2;
71  v1.x = double(cell.x) * this->m_resolution;
72  v1.y = double(cell.y) * this->m_resolution;
73  v1.x = +v1.x + m_origin.get_x() + 0 * this->m_resolution;
74  v1.y = -v1.y + m_origin.get_y() + (m_height - 1) * this->m_resolution;
75  //note that in the following operation, we are using -get_sa()
76  //this is an optimization, since sin(-a)=-sin(a) and cos(-a)=cos(a)
77  //we need -a instead of a because of we are using the inverse tranformation respect to world2cell
78  v2.x = v1.x * m_origin.get_ca() - v1.y * -m_origin.get_sa();
79  v2.y = v1.x * -m_origin.get_sa() + v1.y * m_origin.get_ca();
80  return v2;
81 }
82 
84 {
85  //convert a world location (wrt the map reference frame located in m_origin, measured in meters), to a cell from the upper-left corner.
86  //beware: the location of m_origin is referred to the lower-left corner (ROS convention)
87  XYWorld world2;
88  world2.x = world.x * m_origin.get_ca() - world.y * m_origin.get_sa();
89  world2.y = world.x * m_origin.get_sa() + world.y * m_origin.get_ca();
90  int x = int((+world2.x - this->m_origin.get_x()) / this->m_resolution) + 0;
91  int y = int((-world2.y + this->m_origin.get_y()) / this->m_resolution) + m_height - 1;
92  XYCell c;
93  c.x = (x < 0) ? 0 : x;
94  c.y = (y < 0) ? 0 : y;
95  c.x = (c.x >= m_width) ? m_width-1 : c.x;
96  c.y = (c.y >= m_height) ? m_height-1 : c.y;
97  return c;
98 }
99 
101 {
102  //convert a world location (wrt the map reference frame located in m_origin, measured in meters), to a cell from the upper-left corner.
103  //beware: the location of m_origin is referred to the lower-left corner (ROS convention)
104  XYWorld world2;
105  world2.x = world.x * m_origin.get_ca() - world.y * m_origin.get_sa();
106  world2.y = world.x * m_origin.get_sa() + world.y * m_origin.get_ca();
107  XYCell c;
108  c.x = int((+world2.x - this->m_origin.get_x()) / this->m_resolution) + 0;
109  c.y = int((-world2.y + this->m_origin.get_y()) / this->m_resolution) + m_height - 1;
110  return c;
111 }
112 
114 {
115  XYCell cell = world2Cell_unsafeFast(world);
116  return isInsideMap(cell);
117 }
118 
120 {
121  //if (cell.x < 0) return false;
122  //if (cell.y < 0) return false;
123  if (cell.x >= m_width)
124  return false;
125  if (cell.y >= m_height)
126  return false;
127  return true;
128 }
129 
131 {
132  XYWorld wrld = cell2World(cell);
133  return Map2DLocation(this->m_map_name, wrld);
134 }
135 
137 {
138  return Map2DLocation(this->m_map_name, wrld);
139 }
140 
142 {
143  XYWorld wrld(loc.x, loc.y);
144  XYCell cell = world2Cell(wrld);
145  return cell;
146 }
147 
149 {
150  return XYWorld(loc.x, loc.y);
151 }
yarp::dev::Nav2D::MapGrid2DInfo::toLocation
yarp::dev::Nav2D::Map2DLocation toLocation(XYCell cell) const
Definition: MapGrid2DInfo.cpp:130
yarp::dev::Map2DLocationData::y
double y
Definition: Map2DLocationData.h:32
yarp::sig
Signal processing.
Definition: Image.h:25
yarp::math::Vec2D::x
T x
Definition: Vec2D.h:27
yarp::dev::Nav2D::XYWorld
yarp::math::Vec2D< double > XYWorld
Definition: NavTypes.h:25
yarp::dev::Nav2D::MapGrid2DInfo::world2Cell_unsafeFast
XYCell world2Cell_unsafeFast(XYWorld world) const
Definition: MapGrid2DInfo.cpp:100
yarp::math
Definition: FrameTransform.h:18
yarp::math::Vec2D< double >
yarp::dev::Nav2D::MapGrid2DOrigin
Definition: MapGrid2DInfo.h:32
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::Nav2D::MapGrid2DInfo::cell2World
XYWorld cell2World(XYCell cell) const
Definition: MapGrid2DInfo.cpp:65
yarp::dev::Nav2D::MapGrid2DInfo::toXYCell
XYCell toXYCell(yarp::dev::Nav2D::Map2DLocation loc) const
Definition: MapGrid2DInfo.cpp:141
yarp::dev::Nav2D::MapGrid2DOrigin::MapGrid2DOrigin
MapGrid2DOrigin()
Definition: MapGrid2DInfo.cpp:25
yarp::dev::Nav2D::MapGrid2DInfo::isInsideMap
bool isInsideMap(XYCell cell) const
Checks if a cell is inside the map.
Definition: MapGrid2DInfo.cpp:119
yarp::dev::Nav2D::MapGrid2DOrigin::operator!=
bool operator!=(const MapGrid2DOrigin &other) const
Definition: MapGrid2DInfo.cpp:46
yarp::dev::Nav2D
Definition: ILocalization2D.h:21
yarp::dev::Nav2D::MapGrid2DInfo::toXYWorld
XYWorld toXYWorld(yarp::dev::Nav2D::Map2DLocation loc) const
Definition: MapGrid2DInfo.cpp:148
DEG2RAD
#define DEG2RAD
Definition: MapGrid2DInfo.cpp:22
yarp::dev::Nav2D::Map2DLocation
Definition: Map2DLocation.h:30
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::dev::Nav2D::MapGrid2DInfo::MapGrid2DInfo
MapGrid2DInfo()
Definition: MapGrid2DInfo.cpp:56
yarp::dev::Nav2D::MapGrid2DInfo::world2Cell
XYCell world2Cell(XYWorld world) const
Definition: MapGrid2DInfo.cpp:83
yarp::dev::Map2DLocationData::x
double x
Definition: Map2DLocationData.h:31
yarp::dev::Nav2D::MapGrid2DOrigin::setOrigin
void setOrigin(double x_init, double y_init, double t_init)
Definition: MapGrid2DInfo.cpp:32
MapGrid2DInfo.h
yarp::math::Vec2D::y
T y
Definition: Vec2D.h:31