cheshirekow  v0.1.0
funcs.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
26 #ifndef MPBLOCKS_DUBINS_CURVES_EIGEN_FUNCS_HPP_
27 #define MPBLOCKS_DUBINS_CURVES_EIGEN_FUNCS_HPP_
28 
29 #include <cmath>
32 
33 namespace mpblocks {
34 namespace dubins {
35 namespace curves_eigen {
36 
39 template <typename Scalar>
40 Eigen::Matrix<Scalar, 2, 1> ccwCenter(const Eigen::Matrix<Scalar, 3, 1>& q,
41  Scalar r) {
42  typedef Eigen::Matrix<Scalar, 2, 1> Vector2d;
43  Vector2d x, v;
44 
45  // calculate the center of the circle to which q1 is tangent
46  x << q[0], q[1];
47  v << -sin(q[2]), cos(q[2]);
48  return x + r * v;
49 }
50 
53 template <typename Scalar>
54 Eigen::Matrix<Scalar, 2, 1> leftCenter(const Eigen::Matrix<Scalar, 3, 1>& q,
55  Scalar r) {
56  return ccwCenter(q, r);
57 }
58 
61 template <typename Scalar>
62 Eigen::Matrix<Scalar, 2, 1> cwCenter(const Eigen::Matrix<Scalar, 3, 1>& q,
63  Scalar r) {
64  typedef Eigen::Matrix<Scalar, 2, 1> Vector2d;
65  Vector2d x, v, dc;
66 
67  x << q[0], q[1];
68  v << sin(q[2]), -cos(q[2]);
69 
70  return x + r * v;
71 }
72 
75 template <typename Scalar>
76 Eigen::Matrix<Scalar, 2, 1> rightCenter(const Eigen::Matrix<Scalar, 3, 1>& q,
77  Scalar r) {
78  return cwCenter(q, r);
79 }
80 
81 template <typename Scalar>
82 Scalar ccwAngleOf(const Scalar q_theta) {
83  return clampRadian(q_theta - M_PI / 2.0);
84 }
85 
86 template <typename Scalar>
87 Scalar leftAngleOf(const Scalar q_theta) {
88  return ccwAngleOf(q_theta);
89 }
90 
91 template <typename Scalar>
92 Scalar ccwAngle_inv(const Scalar alpha) {
93  return clampRadian(alpha + M_PI / 2.0);
94 }
95 
96 template <typename Scalar>
97 Scalar leftAngle_inv(const Scalar alpha) {
98  return ccwAngle_inv(alpha);
99 }
100 
101 template <typename Scalar>
102 Scalar cwAngleOf(const Scalar q_theta) {
103  return clampRadian(q_theta + M_PI / 2.0);
104 }
105 
106 template <typename Scalar>
107 Scalar rightAngleOf(const Scalar q_theta) {
108  return cwAngleOf(q_theta);
109 }
110 
111 template <typename Scalar>
112 Scalar cwAngle_inv(const Scalar alpha) {
113  return clampRadian(alpha - M_PI / 2.0);
114 }
115 
116 template <typename Scalar>
117 Scalar rightAngle_inv(const Scalar alpha) {
118  return cwAngle_inv(alpha);
119 }
120 
121 template <typename Scalar>
122 Scalar ccwAngleOf(const Eigen::Matrix<Scalar, 3, 1>& q) {
123  return clampRadian(q[2] - M_PI / 2.0);
124 }
125 
126 template <typename Scalar>
127 Scalar leftAngleOf(const Eigen::Matrix<Scalar, 3, 1>& q) {
128  return ccwAngleOf(q);
129 }
130 
131 template <typename Scalar>
132 Scalar cwAngleOf(const Eigen::Matrix<Scalar, 3, 1>& q) {
133  return clampRadian(q[2] + M_PI / 2.0);
134 }
135 
136 template <typename Scalar>
137 Scalar rightAngleOf(const Eigen::Matrix<Scalar, 3, 1>& q) {
138  return cwAngleOf(q);
139 }
140 
143 template <typename Scalar>
144 bool coincidentCenterA(const Eigen::Matrix<Scalar, 2, 1>& c0,
145  const Eigen::Matrix<Scalar, 2, 1>& c1,
146  Eigen::Matrix<Scalar, 2, 1>& c2, Scalar r, Scalar& a) {
147  // distance between two circles
148  Scalar d = (c0 - c1).norm();
149 
150  // if the distance is too large, then this primitive is not the solution,
151  // and we can bail here
152  if (d > 4 * r) return false;
153 
154  // the base angle of the isosceles triangle whose vertices are the centers
155  // of the the three circles, note acos returns [0,pi]
156  a = acos(d / (4 * r));
157 
158  // create a clockwise rotation of magnitude alpha
159  Eigen::Rotation2D<Scalar> R(-a);
160 
161  // we find the third vertex of this triangle by taking the vector between
162  // the two circle centers, normalizing it, and rotating it by alpha, and
163  // scaling it to magnitude 2r, then it points from the center of
164  // one the circle tangent to q1 to the third vertex
165  c2 = c0 + R * (c1 - c0).normalized() * 2 * r;
166 
167  return true;
168 }
169 
172 template <typename Scalar>
173 bool coincidentCenterB(const Eigen::Matrix<Scalar, 2, 1>& c0,
174  const Eigen::Matrix<Scalar, 2, 1>& c1,
175  Eigen::Matrix<Scalar, 2, 1>& c2, Scalar r, Scalar& a) {
176  // distance between two circles
177  Scalar d = (c0 - c1).norm();
178 
179  // if the distance is too large, then this primitive is not the solution,
180  // and we can bail here
181  if (d > 4 * r) return false;
182 
183  // the base angle of the isosceles triangle whose vertices are the centers
184  // of the the three circles, note acos returns [0,pi]
185  a = acos(d / (4 * r));
186 
187  // create a clockwise rotation of magnitude alpha
188  Eigen::Rotation2D<Scalar> R(-a);
189 
190  // we find the third vertex of this triangle by taking the vector between
191  // the two circle centers, normalizing it, and rotating it by alpha, and
192  // scaling it to magnitude 2r, then it points from the center of
193  // one the circle tangent to q1 to the third vertex
194  c2 = c1 + R * (c0 - c1).normalized() * 2 * r;
195 
196  return true;
197 }
198 
199 } // curves_eigen
200 } // dubins
201 } // mpblocks
202 
203 #endif // FUNCS_H_
bool coincidentCenterA(const Eigen::Matrix< Scalar, 2, 1 > &c0, const Eigen::Matrix< Scalar, 2, 1 > &c1, Eigen::Matrix< Scalar, 2, 1 > &c2, Scalar r, Scalar &a)
returns the center of a circle which is coincident to the two circles whose centers are given...
Definition: funcs.hpp:144
Scalar leftAngle_inv(const Scalar alpha)
Definition: funcs.hpp:97
Scalar leftAngleOf(const Scalar q_theta)
return the angle of the vector from the center of the counter clockwise (left) circle coincident to q...
Definition: funcs.hpp:87
bool coincidentCenterB(const Eigen::Matrix< Scalar, 2, 1 > &c0, const Eigen::Matrix< Scalar, 2, 1 > &c1, Eigen::Matrix< Scalar, 2, 1 > &c2, Scalar r, Scalar &a)
returns the center of a circle which is coincident to the two circles whose centers are given...
Definition: funcs.hpp:173
Scalar cwAngle_inv(const Scalar alpha)
Definition: funcs.hpp:112
Scalar ccwAngleOf(const Scalar q_theta)
return the angle of the vector from the center of the counter clockwise (left) circle coincident to q...
Definition: funcs.hpp:82
Eigen::Matrix< Scalar, 2, 1 > cwCenter(const Eigen::Matrix< Scalar, 3, 1 > &q, Scalar r)
return the center of a clockwise (right) circle coincident to q with radius r
Definition: funcs.hpp:62
empty struct used to template "variant" of three arc primitives
Definition: types.h:43
Scalar cwAngleOf(const Scalar q_theta)
return the angle of the vector from the center of the clockwise (right) circle coincident to q...
Definition: funcs.hpp:102
empty struct used to template "right turn" primitive
Definition: types.h:37
Eigen::Matrix< Scalar, 2, 1 > ccwCenter(const Eigen::Matrix< Scalar, 3, 1 > &q, Scalar r)
return the center of a counter clockwise (left) circle coincident to q with radius r ...
Definition: funcs.hpp:40
Scalar rightAngle_inv(const Scalar alpha)
Definition: funcs.hpp:117
Scalar rightAngleOf(const Scalar q_theta)
return the angle of the vector from the center of the clockwise (right) circle coincident to q...
Definition: funcs.hpp:107
__host__ __device__ Format_t clampRadian(Format_t a)
wraps the input onto [-pi,pi]
Definition: funcs.hpp:37
__device__ __host__ Scalar norm(const RValue< Scalar, ROWS, COLS, Exp > &M)
compute the norm
Definition: Norm.h:140
Matrix< double, 2, 1 > Vector2d
Definition: matrix.h:144
Eigen::Matrix< Scalar, 2, 1 > rightCenter(const Eigen::Matrix< Scalar, 3, 1 > &q, Scalar r)
return the center of a clockwise (right) circle coincident to q with radius r
Definition: funcs.hpp:76
__host__ __device__ Normalized< Scalar, Exp, Spec > normalized(const RValue< Scalar, Exp, Spec > &exp)
Definition: Normalized.h:72
Scalar ccwAngle_inv(const Scalar alpha)
Definition: funcs.hpp:92
Eigen::Matrix< Scalar, 2, 1 > leftCenter(const Eigen::Matrix< Scalar, 3, 1 > &q, Scalar r)
return the center of a counter clockwise (left) circle coincident to q with radius r ...
Definition: funcs.hpp:54