YARP
Yet Another Robot Platform
Map2DLocation.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 
7 #include <yarp/os/Log.h>
8 #include <yarp/os/LogComponent.h>
9 #include <yarp/os/LogStream.h>
10 #include <math.h>
11 
12 using namespace yarp::dev::Nav2D;
13 
14 YARP_LOG_COMPONENT(MAP2DLOCATION, "yarp.Map2DLocation")
15 
16 bool Map2DLocation::is_near_to(const Map2DLocation& other_loc, double linear_tolerance, double angular_tolerance) const
17 {
18  if (linear_tolerance < 0) {
19  return false;
20  }
21  if (angular_tolerance < 0) {
22  return false;
23  }
24  yCAssert(MAP2DLOCATION, linear_tolerance >= 0);
25  yCAssert(MAP2DLOCATION, angular_tolerance >= 0);
26 
27  if (this->map_id != other_loc.map_id)
28  {
29  return false;
30  }
31  if (sqrt(pow((this->x - other_loc.x), 2) + pow((this->y - other_loc.y), 2)) > linear_tolerance)
32  {
33  return false;
34  }
35 
36  if (angular_tolerance != std::numeric_limits<double>::infinity())
37  {
38  //In the following blocks, I'm giving two possible solution to the problem of
39  //determining if the difference of two angles is below a certain threshold.
40  //The problem is tricky, because it must take in account the critical points 0,180,360,-180, -360 etc.
41  //Both the formulas lead to the same result, however I'm not sure I they have the same performances.
42  //Please do not remove the unused block, since it may be a useful reference for the future.
43 #if 1
44  //check in the range 0-360
45  double diff = other_loc.theta - this->theta + 180.0;
46  diff = fmod(diff, 360.0) - 180.0;
47  diff = (diff < -180.0) ? (diff + 360.0) : (diff);
48  if (fabs(diff) > angular_tolerance)
49 #else
50  //check in the range 0-180
51  double angle1 = normalize_angle(this->theta);
52  double angle2 = normalize_angle(other_loc.theta);
53  double diff = angle1 - angle2;
54  diff += (diff > 180) ? -360 : (diff < -180) ? 360 : 0;
55  if (fabs(diff) > angular_tolerance)
56 #endif
57  {
58  return false;
59  }
60  }
61  return true;
62 }
const yarp::os::LogComponent & MAP2DLOCATION()
contains the definition of a Map2DLocation type
#define yCAssert(component, x)
Definition: LogComponent.h:169
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77