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