Util.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package <PACKAGE_NAME>::<CATEGORY>::Util
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
26 
27 namespace armarx
28 {
29  // Mapping from labels used in topic from SecondHands to corresponding ids and labals in the MPI-Model
30  const std::map<std::string, std::pair<unsigned int, std::string>> MPII_TO_MPI{
31  {"head", {0, "Head"}},
32  {"neck", {1, "Neck"}},
33  {"right_shoulder", {2, "RShoulder"}},
34  {"right_elbow", {3, "RElbow"}},
35  {"right_hand", {4, "RWrist"}},
36  {"left_shoulder", {5, "LShoulder"}},
37  {"left_elbow", {6, "LElbow"}},
38  {"left_hand", {7, "LWrist"}},
39  {"right_waist", {8, "RHip"}},
40  {"right_knee", {9, "RKnee"}},
41  {"right_foot", {10, "RAnkle"}},
42  {"left_waist", {11, "LHip"}},
43  {"left_knee", {12, "LKnee"}},
44  {"left_foot", {13, "LAnkle"}},
45  };
46 
47  struct Polygon2D
48  {
49  struct Point
50  {
51  float x;
52  float y;
53 
54  bool
55  operator==(const Point& p) const
56  {
57  return this->x - p.x < std::numeric_limits<float>::epsilon() &&
58  this->y - p.y < std::numeric_limits<float>::epsilon();
59  }
60  };
61 
62  using PointList = std::vector<Point>;
63  // ordered list of polygon points
64  // assert(points[0] == points[n]
65  std::vector<Point> points;
66 
68  {
69  }
70 
72  {
73  ARMARX_CHECK_EXPRESSION(points[0] == points[points.size() - 1]);
74  this->points = points;
75  }
76 
77  // isLeft(): tests if a point is Left|On|Right of an infinite line.
78  // Input: three points p0, p1, and p2
79  // Return: >0 for p2 left of the line through p0 and p1
80  // =0 for p2 on the line
81  // <0 for p2 right of the line
82  int
83  isLeft(Point p0, Point p1, Point p2) const
84  {
85  return static_cast<int>((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
86  }
87 
88  // Algorithm from http://geomalgorithms.com/a03-_inclusion.html#wn_PnPoly() (Date: 05/16/2018)
89  bool
91  {
92  Point p;
93  p.x = point->x;
94  p.y = point->y;
95  int wn = 0; // the winding number counter
96  // loop through all edges of the polygon
97  for (unsigned int i = 0; i < points.size() - 1; i++) // edge from V[i] to V[i+1]
98  {
99  if (points[i].y <= p.y) // start y <= P.y
100  {
101  if (points[i + 1].y > p.y) // an upward crossing
102  if (isLeft(points[i], points[i + 1], p) > 0) // P left of edge
103  {
104  ++wn; // have a valid up intersect
105  }
106  }
107  else // start y > P.y (no test needed)
108  {
109  if (points[i + 1].y <= p.y) // a downward crossing
110  if (isLeft(points[i], points[i + 1], p) < 0) // P right of edge
111  {
112  --wn; // have a valid down intersect
113  }
114  }
115  }
116  return wn == 1;
117  }
118  };
119 } // namespace armarx
armarx::Polygon2D::Polygon2D
Polygon2D()
Definition: Util.h:67
armarx::Polygon2D::isInside
bool isInside(FramedPositionPtr point) const
Definition: Util.h:90
armarx::Polygon2D::points
std::vector< Point > points
Definition: Util.h:65
IceInternal::Handle< FramedPosition >
armarx::Polygon2D::isLeft
int isLeft(Point p0, Point p1, Point p2) const
Definition: Util.h:83
armarx::Polygon2D::Polygon2D
Polygon2D(PointList points)
Definition: Util.h:71
FramedPose.h
armarx::Polygon2D
Definition: Util.h:47
armarx::Polygon2D::Point::operator==
bool operator==(const Point &p) const
Definition: Util.h:55
ExpressionException.h
armarx::Polygon2D::Point::x
float x
Definition: Util.h:51
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::MPII_TO_MPI
const std::map< std::string, std::pair< unsigned int, std::string > > MPII_TO_MPI
Definition: Util.h:30
armarx::Polygon2D::Point
Definition: Util.h:49
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::Polygon2D::Point::y
float y
Definition: Util.h:52
armarx::Polygon2D::PointList
std::vector< Point > PointList
Definition: Util.h:62