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 
25 
26 namespace armarx
27 {
28  // Mapping from labels used in topic from SecondHands to corresponding ids and labals in the MPI-Model
29  const std::map<std::string, std::pair<unsigned int, std::string>> MPII_TO_MPI
30  {
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 
48  struct Polygon2D
49  {
50  struct Point
51  {
52  float x;
53  float y;
54  bool operator==(const Point& p) const
55  {
56  return this->x - p.x < std::numeric_limits<float>::epsilon() && this->y - p.y < std::numeric_limits<float>::epsilon();
57  }
58  };
59 
60  using PointList = std::vector<Point>;
61  // ordered list of polygon points
62  // assert(points[0] == points[n]
63  std::vector<Point> points;
64 
65  Polygon2D() {}
67  {
68  ARMARX_CHECK_EXPRESSION(points[0] == points[points.size() - 1]);
69  this->points = points;
70  }
71 
72  // isLeft(): tests if a point is Left|On|Right of an infinite line.
73  // Input: three points p0, p1, and p2
74  // Return: >0 for p2 left of the line through p0 and p1
75  // =0 for p2 on the line
76  // <0 for p2 right of the line
77  int isLeft(Point p0, Point p1, Point p2) const
78  {
79  return static_cast<int>((p1.x - p0.x) * (p2.y - p0.y)
80  - (p2.x - p0.x) * (p1.y - p0.y));
81  }
82 
83  // Algorithm from http://geomalgorithms.com/a03-_inclusion.html#wn_PnPoly() (Date: 05/16/2018)
84  bool isInside(FramedPositionPtr point) const
85  {
86  Point p;
87  p.x = point->x;
88  p.y = point->y;
89  int wn = 0; // the winding number counter
90  // loop through all edges of the polygon
91  for (unsigned int i = 0; i < points.size() - 1; i++) // edge from V[i] to V[i+1]
92  {
93  if (points[i].y <= p.y) // start y <= P.y
94  {
95  if (points[i + 1].y > p.y) // an upward crossing
96  if (isLeft(points[i], points[i + 1], p) > 0) // P left of edge
97  {
98  ++wn; // have a valid up intersect
99  }
100  }
101  else // start y > P.y (no test needed)
102  {
103  if (points[i + 1].y <= p.y) // a downward crossing
104  if (isLeft(points[i], points[i + 1], p) < 0) // P right of edge
105  {
106  --wn; // have a valid down intersect
107  }
108  }
109  }
110  return wn == 1;
111  }
112  };
113 }
armarx::Polygon2D::Polygon2D
Polygon2D()
Definition: Util.h:65
armarx::Polygon2D::isInside
bool isInside(FramedPositionPtr point) const
Definition: Util.h:84
armarx::Polygon2D::points
std::vector< Point > points
Definition: Util.h:63
IceInternal::Handle< FramedPosition >
armarx::Polygon2D::isLeft
int isLeft(Point p0, Point p1, Point p2) const
Definition: Util.h:77
armarx::Polygon2D::Polygon2D
Polygon2D(PointList points)
Definition: Util.h:66
FramedPose.h
armarx::Polygon2D
Definition: Util.h:48
armarx::Polygon2D::Point::operator==
bool operator==(const Point &p) const
Definition: Util.h:54
ExpressionException.h
armarx::Polygon2D::Point::x
float x
Definition: Util.h:52
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:50
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::Polygon2D::Point::y
float y
Definition: Util.h:53
armarx::Polygon2D::PointList
std::vector< Point > PointList
Definition: Util.h:60