YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Map2DArea.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#include <yarp/dev/api.h>
8#include <yarp/os/Bottle.h>
11#include <sstream>
12#include <string>
13#include <vector>
14#include <cassert>
15#include <yarp/os/LogStream.h>
16#include <random>
17#include <algorithm>
18
19using namespace yarp::dev;
20using namespace yarp::dev::Nav2D;
21using namespace yarp::sig;
22using namespace yarp::os;
23using namespace yarp::math;
24
25int pnpoly(std::vector<yarp::math::Vec2D<double>> points, double testx, double testy)
26{
27 size_t i, j;
28 int c = 0;
29 for (i = 0, j = points.size() - 1; i < points.size(); j = i++)
30 {
31 if (((points[i].y>testy) != (points[j].y>testy)) &&
32 (testx < (points[j].x - points[i].x) * (testy - points[i].y) / (points[j].y - points[i].y) + points[i].x))
33 {
34 c = !c;
35 }
36 }
37 return c;
38}
39
40Map2DArea::Map2DArea(const std::string& map_name, const std::vector<yarp::math::Vec2D<double>>& area_points, const std::string& desc)
41{
42 map_id = map_name;
44 description = desc;
45}
46
47Map2DArea::Map2DArea(const std::string& map_name, const std::vector<Map2DLocation>& area_points, const std::string& desc)
48{
49 map_id = map_name;
50 description = desc;
51 for (auto it = area_points.begin(); it != area_points.end(); it++)
52 {
53#if 0
54 yAssert(it->map_id == map_name);
55#else
56 if (it->map_id != map_name)
57 {
58 map_id = "";
59 points.clear();
60 yError() << "all area_points must belong to the same map:" << map_name;
61 return;
62 }
63#endif
64 points.push_back(yarp::math::Vec2D<double>(it->x, it->y));
65 }
66}
67
69{
70 map_id = "";
71}
72
73std::string Map2DArea::toString() const
74{
75 std::ostringstream stringStream;
76 stringStream.precision(-1);
77 stringStream.width(-1);
78 stringStream << std::string("map_id:") << map_id << " ";
79 for (size_t i = 0; i<points.size(); i++)
80 {
81 stringStream << " point " << i << "(" << points[i].x << "," << points[i].y << ")";
82 }
83 stringStream << std::string("desc:") << description;
84 return stringStream.str();
85}
86
88{
89 if (loc.map_id != this->map_id) {
90 return false;
91 }
92 if (points.size() < 3) {
93 return false;
94 }
95 if (pnpoly(points, loc.x, loc.y) > 0) {
96 return true;
97 }
98 return false;
99}
100
102{
103 if (
104 map_id != r.map_id ||
105 points != r.points
106 )
107 {
108 return true;
109 }
110 return false;
111}
112
114{
115 if (
116 map_id == r.map_id &&
117 points == r.points
118 )
119 {
120 return true;
121 }
122 return false;
123}
124
126{
127 if (points.size() < 3) {
128 return false;
129 }
130 if (map_id == "") {
131 return false;
132 }
133 return true;
134}
135
137{
140 bool ret = findAreaBounds (lt,rb);
141 if (ret == true)
142 {
143 cent.map_id=this->map_id;
144 cent.x = (rb.x - lt.x) / 2.0;
145 cent.y = (rb.y - lt.y) / 2.0;
146 return true;
147 }
148 return false;
149}
150
152{
153 lt.map_id = rb.map_id = this->map_id;
154 lt.x = lt.y = std::numeric_limits<double>::max();
155 rb.x = rb.y = std::numeric_limits<double>::min();
156 if (isValid() == false) {
157 return false;
158 }
159 for (auto it = points.begin(); it != points.end(); it++)
160 {
161 if (it->x > rb.x) { rb.x = it->x; }
162 if (it->y > rb.y) { rb.y = it->y; }
163 if (it->x < lt.x) { lt.x = it->x; }
164 if (it->y < lt.y) { lt.y = it->y; }
165 }
166 return true;
167}
168
170{
173 if (findAreaBounds(lt, rb) == false) {
174 return false;
175 }
176
177 std::random_device rd;
178 std::mt19937 gen(rd());
179 std::uniform_real_distribution<double> dis_x(lt.x, rb.x);
180 std::uniform_real_distribution<double> dis_y(lt.y, rb.y);
181
182 size_t count_trials = 0;
183 do
184 {
185 double rnd_x = dis_x(gen);
186 double rnd_y = dis_y(gen);
187
188 loc.map_id = this->map_id;
189 loc.x = rnd_x;
190 loc.y = rnd_y;
191 loc.theta = 0;
192 count_trials++;
193 if (this->checkLocationInsideArea(loc)) {
194 break;
195 }
196 } while (count_trials < 20);
197
198 if (count_trials >= 20)
199 {
200 yError() << "Problem found in Map2DArea::getRandomLocation()";
201 return false;
202 }
203
204 return true;
205}
206
208{
209 this->description="";
210 this->map_id = "";
211 this->points.clear();
212}
213
215{
216 //std::vector::at() function performs bound check, throwing exception.
217 //[] operator, instead, not.
218 return points.at(index);
219}
bool ret
#define yError(...)
Definition Log.h:361
#define yAssert(x)
Definition Log.h:388
int pnpoly(std::vector< yarp::math::Vec2D< double > > points, double testx, double testy)
Definition Map2DArea.cpp:25
contains the definition of a Map2DArea type
std::string description
user defined string
std::string map_id
name of the map
std::vector< yarp::math::Vec2D< double > > points
list of points which define the vertices of the area
std::string map_id
name of the map
double theta
orientation [deg] in the map reference frame
double y
y position of the location [m], expressed in the map reference frame
double x
x position of the location [m], expressed in the map reference frame
void clear()
Remove all elements from the path.
std::string toString() const
Returns text representation of the area.
Definition Map2DArea.cpp:73
bool operator==(const Map2DArea &r) const
Compares two Map2DArea.
bool operator!=(const Map2DArea &r) const
Compares two Map2DAreas.
Map2DArea()
Default constructor: the map name is empty, coordinates are set to zero.
Definition Map2DArea.cpp:68
bool getRandomLocation(yarp::dev::Nav2D::Map2DLocation &loc)
get a random Map2DLocation inside the Map2DArea @loc the computed Map2DLocation
bool findAreaBounds(yarp::dev::Nav2D::Map2DLocation &lt, yarp::dev::Nav2D::Map2DLocation &rb)
retrieves two Map2DLocations representing the bounding box of the Map2DArea @lt the left-top vertex @...
bool checkLocationInsideArea(yarp::dev::Nav2D::Map2DLocation loc)
Check if a Map2DLocation is inside a Map2DArea.
Definition Map2DArea.cpp:87
bool getCentroid(yarp::dev::Nav2D::Map2DLocation &cent)
retrieves the centroid of the area @cent the centroid
yarp::math::Vec2D< double > & operator[](size_t index)
Returns a vertex of the area.
bool isValid() const
Checks if the Map2DArea is valid return true if the Map2DArea is valid.
A mini-server for performing network communication in the background.
For streams capable of holding different kinds of content, check what they actually have.
Definition jointData.cpp:13
An interface to the operating system, including Port based communication.