YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeLaserWithMotor_utils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#define _USE_MATH_DEFINES
7
9
10#include <yarp/os/Time.h>
11#include <yarp/os/Log.h>
12#include <yarp/os/LogStream.h>
14#include <yarp/math/Vec2D.h>
15#include <iostream>
16#include <limits>
17#include <cstring>
18#include <cstdlib>
19#include <cmath>
20
21//#define LASER_DEBUG
22#ifndef DEG2RAD
23#define DEG2RAD M_PI/180.0
24#endif
25
26YARP_LOG_COMPONENT(FAKE_LASER_UTILS, "yarp.devices.fakeLaserWithMotor.Utils")
27
30using namespace yarp::dev::Nav2D;
31
32void FakeLaserWithMotor::wall_the_robot(double siz, double dist)
33{
34 //double res;
35 //m_map.getResolution(res);
36 //size_t siz_cell = siz / res;
37 //size_t dist_cell = dist / res;
38 XYCell robot = m_map.world2Cell(XYWorld(m_robot_loc_x, m_robot_loc_y));
39
41 XYWorld start (0+dist, 0 - siz);
42 ray_start.x = start.x * cos(m_robot_loc_t * DEG2RAD) - start.y * sin(m_robot_loc_t * DEG2RAD) + m_robot_loc_x;
43 ray_start.y = start.x * sin(m_robot_loc_t * DEG2RAD) + start.y * cos(m_robot_loc_t * DEG2RAD) + m_robot_loc_y;
44 XYCell start_cell = m_map.world2Cell(ray_start);
45
47 XYWorld end(0 + dist, 0 + siz);
48 ray_end.x = end.x * cos(m_robot_loc_t * DEG2RAD) - end.y * sin(m_robot_loc_t * DEG2RAD) + m_robot_loc_x;
49 ray_end.y = end.x * sin(m_robot_loc_t * DEG2RAD) + end.y * cos(m_robot_loc_t * DEG2RAD) + m_robot_loc_y;
50 XYCell end_cell = m_map.world2Cell(ray_end);
51
52 drawStraightLine(start_cell,end_cell);
53}
54
55void FakeLaserWithMotor::obst_the_robot(double siz, double dist)
56{
57 //NOT YET IMPLEMENTED
58 /*double res;
59 m_map.getResolution(res);
60 size_t siz_cell = size_t(siz / res);
61 size_t dist_cell = size_t(dist / res);
62 XYCell robot = m_map.world2Cell(XYWorld(m_robot_loc_x, m_robot_loc_y));*/
63}
64
65void FakeLaserWithMotor::trap_the_robot(double siz)
66{
67 double res;
69 size_t siz_cell = size_t(siz / res);
70 size_t x=0;
71 size_t y=0;
73
74 y = robot.y - siz_cell;
75 for (x= robot.x- siz_cell; x< robot.x + siz_cell; x++)
76 {
78 }
79
80 y = robot.y + siz_cell;
81 for (x = robot.x - siz_cell; x < robot.x + siz_cell; x++)
82 {
84 }
85
86 x = robot.x - siz_cell;
87 for (y = robot.y - siz_cell; y < robot.y + siz_cell; y++)
88 {
90 }
91
92 x = robot.x + siz_cell;
93 for (y = robot.y - siz_cell; y < robot.y + siz_cell; y++)
94 {
96 }
97}
98
99void FakeLaserWithMotor::free_the_robot()
100{
102}
103
104void FakeLaserWithMotor::drawStraightLine(XYCell src, XYCell dst)
105{
106 long int x, y;
107 long int dx, dy, dx1, dy1, px, py, xe, ye, i;
108 dx = (long int)dst.x - (long int)src.x;
109 dy = (long int)dst.y - (long int)src.y;
110 dx1 = abs(dx);
111 dy1 = abs(dy);
112 px = 2 * dy1 - dx1;
113 py = 2 * dx1 - dy1;
114 if (dy1 <= dx1)
115 {
116 if (dx >= 0)
117 {
118 x = (long int)src.x;
119 y = (long int)src.y;
120 xe = (long int)dst.x;
121 }
122 else
123 {
124 x = (long int)dst.x;
125 y = (long int)dst.y;
126 xe = (long int)src.x;
127 }
129 for (i = 0; x < xe; i++)
130 {
131 x = x + 1;
132 if (px < 0)
133 {
134 px = px + 2 * dy1;
135 }
136 else
137 {
138 if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
139 {
140 y = y + 1;
141 }
142 else
143 {
144 y = y - 1;
145 }
146 px = px + 2 * (dy1 - dx1);
147 }
149 }
150 }
151 else
152 {
153 if (dy >= 0)
154 {
155 x = (long int)src.x;
156 y = (long int)src.y;
157 ye = (long int)dst.y;
158 }
159 else
160 {
161 x = (long int)dst.x;
162 y = (long int)dst.y;
163 ye = (long int)src.y;
164 }
166 for (i = 0; y < ye; i++)
167 {
168 y = y + 1;
169 if (py <= 0)
170 {
171 py = py + 2 * dx1;
172 }
173 else
174 {
175 if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
176 {
177 x = x + 1;
178 }
179 else
180 {
181 x = x - 1;
182 }
183 py = py + 2 * (dx1 - dy1);
184 }
186 }
187 }
188}
189
190double FakeLaserWithMotor::checkStraightLine(XYCell src, XYCell dst)
191{
192 //BEWARE: src and dest must be already clipped and >0 in this function
194 test_point.x = src.x;
195 test_point.y = src.y;
196
197 //here using the fast Bresenham algorithm
198 int dx = abs(int(dst.x - src.x));
199 int dy = abs(int(dst.y - src.y));
200 int err = dx - dy;
201
202 int sx;
203 int sy;
204 if (src.x < dst.x) { sx = 1; } else { sx = -1; }
205 if (src.y < dst.y) { sy = 1; } else { sy = -1; }
206
207 while (true)
208 {
209 //the test point is going to move from src until it reaches dst OR
210 //if it reaches the boundaries of the image
211 if (test_point.x==0 || test_point.y ==0 || test_point.x>=m_map.width() || test_point.y>=m_map.height())
212 {
213 break;
214 }
215
216 //if (m_map.isFree(src) == false)
218 {
221 double dist = sqrt(pow(world_start.x - world_end.x, 2) + pow(world_start.y - world_end.y, 2));
222 return dist;
223 }
224
225 if (test_point.x == dst.x && test_point.y == dst.y)
226 {
227 break;
228 }
229
230 int e2 = err * 2;
231 if (e2 > -dy)
232 {
233 err = err - dy;
234 test_point.x += sx;
235 }
236 if (e2 < dx)
237 {
238 err = err + dx;
239 test_point.y += sy;
240 }
241 }
242 return std::numeric_limits<double>::infinity();
243}
244
245bool FakeLaserWithMotor::LiangBarsky_clip(int edgeLeft, int edgeRight, int edgeTop, int edgeBottom,
248{
249 double t0 = 0.0; double t1 = 1.0;
250 double xdelta = double(dst.x - src.x);
251 double ydelta = double(dst.y - src.y);
252 double p, q, r;
253
254 for (int edge = 0; edge < 4; edge++)
255 {
256 if (edge == 0) { p = -xdelta; q = -(edgeLeft - src.x); }
257 if (edge == 1) { p = xdelta; q = (edgeRight - src.x); }
258 if (edge == 2) { p = -ydelta; q = -(edgeTop - src.y); }
259 if (edge == 3) { p = ydelta; q = (edgeBottom - src.y); }
260 r = q / p;
261 if (p == 0 && q < 0) { return false; } //line is outside (parallel)
262
263 if (p < 0)
264 {
265 if (r > t1) { return false; } //line is outside.
266 else if (r > t0) { t0 = r; } //line is clipped
267 }
268 else if (p > 0)
269 {
270 if (r < t0) { return false; } //line is outside.
271 else if (r < t1) { t1 = r; } //line is clipped
272 }
273 }
274
275 src_clipped.x = size_t(double(src.x) + t0 * xdelta);
276 src_clipped.y = size_t(double(src.y) + t0 * ydelta);
277 dst_clipped.x = size_t(double(src.x) + t1 * xdelta);
278 dst_clipped.y = size_t(double(src.y) + t1 * ydelta);
279
280 return true; //line is clipped
281}
const yarp::os::LogComponent & FAKE_LASER_UTILS()
#define DEG2RAD
size_t size_t
fakeLaserWithMotor : fake sensor device driver for testing purposes and reference for IRangefinder2D ...
yarp::dev::Nav2D::MapGrid2D m_originally_loaded_map
yarp::dev::Nav2D::MapGrid2D m_map
XYCell world2Cell(XYWorld world) const
XYWorld cell2World(XYCell cell) const
size_t height() const
Retrieves the map height, expressed in cells.
bool setMapFlag(XYCell cell, map_flags flag)
Set the flag of a specific cell of the map.
size_t width() const
Retrieves the map width, expressed in cells.
bool isWall(XYCell cell) const
Checks if a specific cell of the map contains a wall.
void getResolution(double &resolution) const
Retrieves the resolution of the map, i.e.
A mini-server for performing network communication in the background.
#define YARP_LOG_COMPONENT(name,...)
yarp::math::Vec2D< size_t > XYCell
Definition NavTypes.h:16
yarp::math::Vec2D< double > XYWorld
Definition NavTypes.h:17
The main, catch-all namespace for YARP.
Definition dirs.h:16