Exotica
convex_hull.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_CORE_TASK_MAPS_CONVEXHULL_H_
31 #define EXOTICA_CORE_TASK_MAPS_CONVEXHULL_H_
32 
33 #include <Eigen/Dense>
34 
35 #include <exotica_core/tools.h>
36 
37 namespace exotica
38 {
41 {
42  return (p(1) - p1(1)) * (p2(0) - p1(0)) - (p2(1) - p1(1)) * (p(0) - p1(0));
43 }
44 
45 std::list<int> QuickHull(Eigen::MatrixXdRefConst points, std::list<int>& half_points, int p1, int p2)
46 {
47  int ind = -1;
48  double max_dist = 0;
49  std::list<int> new_half_points;
50  for (const int& i : half_points)
51  {
52  double d = DetDiff2D(points.row(p1).transpose(), points.row(p2).transpose(), points.row(i).transpose());
53  if (d >= 0.0)
54  {
55  new_half_points.push_back(i);
56  }
57  if (d > max_dist)
58  {
59  ind = i;
60  max_dist = d;
61  }
62  }
63 
64  std::list<int> hull;
65  if (ind == -1)
66  {
67  hull.push_back(p2);
68  }
69  else
70  {
71  hull.splice(hull.begin(), QuickHull(points, new_half_points, p1, ind));
72  hull.splice(hull.end(), QuickHull(points, new_half_points, ind, p2));
73  }
74 
75  return hull;
76 }
77 
78 std::list<int> ConvexHull2D(Eigen::MatrixXdRefConst points)
79 {
80  if (points.cols() != 2) ThrowPretty("Input must contain 2D points!");
81 
82  int n = points.rows();
83 
84  std::list<int> hull;
85  std::list<int> half_points;
86  if (n < 3)
87  {
88  for (int i = 0; i < n; ++i) hull.push_back(i);
89  }
90  else
91  {
92  int min_x = 0, max_x = 0;
93  half_points.push_back(0);
94  for (int i = 1; i < n; ++i)
95  {
96  if (points(i, 0) < points(min_x, 0))
97  min_x = i;
98  if (points(i, 0) > points(max_x, 0))
99  max_x = i;
100  half_points.push_back(i);
101  }
102  hull.splice(hull.begin(), QuickHull(points, half_points, min_x, max_x));
103  hull.splice(hull.end(), QuickHull(points, half_points, max_x, min_x));
104  }
105 
106  return hull;
107 }
108 } // namespace exotica
109 
110 #endif // EXOTICA_CORE_TASK_MAPS_CONVEXHULL_H_
exotica
Definition: cartpole_dynamics_solver.h:38
Eigen::VectorXdRefConst
const typedef Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:52
Eigen::MatrixXdRefConst
const typedef Eigen::Ref< const Eigen::MatrixXd > & MatrixXdRefConst
Definition: conversions.h:53
exotica::DetDiff2D
double DetDiff2D(Eigen::VectorXdRefConst p1, Eigen::VectorXdRefConst p2, Eigen::VectorXdRefConst p)
DetDiff2D Computes the 2D determinant (analogous to a 2D cross product) of a two vectors defined by P...
Definition: convex_hull.h:40
exotica::QuickHull
std::list< int > QuickHull(Eigen::MatrixXdRefConst points, std::list< int > &half_points, int p1, int p2)
Definition: convex_hull.h:45
exotica::ConvexHull2D
std::list< int > ConvexHull2D(Eigen::MatrixXdRefConst points)
Definition: convex_hull.h:78
ThrowPretty
#define ThrowPretty(m)
Definition: exception.h:36
tools.h