geometry.cpp
Go to the documentation of this file.
1 /*
2  * geometry.cpp
3  *
4  * Created on: Apr 11, 2013
5  * Author: jorge
6  */
7 
8 #include "../../include/yocs_math_toolkit/common.hpp"
9 #include "../../include/yocs_math_toolkit/geometry.hpp"
10 
11 
12 namespace mtk
13 {
14 
15 
16 double wrapAngle(double a)
17 {
18  a = fmod(a + M_PI, 2*M_PI);
19  if (a < 0.0)
20  a += 2.0*M_PI;
21  return a - M_PI;
22 }
23 
24 double roll(const tf::Transform& tf)
25 {
26  double roll, pitch, yaw;
27  tf::Matrix3x3(tf.getRotation()).getRPY(roll, pitch, yaw);
28  return roll;
29 }
30 
31 double roll(geometry_msgs::Pose pose)
32 {
34  pose2tf(pose, tf);
35  return roll(tf);
36 }
37 
38 double roll(geometry_msgs::PoseStamped pose)
39 {
40  return roll(pose.pose);
41 }
42 
43 double pitch(const tf::Transform& tf)
44 {
45  double roll, pitch, yaw;
46  tf::Matrix3x3(tf.getRotation()).getRPY(roll, pitch, yaw);
47  return pitch;
48 }
49 
50 double pitch(geometry_msgs::Pose pose)
51 {
53  pose2tf(pose, tf);
54  return pitch(tf);
55 }
56 
57 double pitch(geometry_msgs::PoseStamped pose)
58 {
59  return pitch(pose.pose);
60 }
61 
62 double yaw(const tf::Transform& tf)
63 {
64  return tf::getYaw(tf.getRotation());
65 }
66 
67 double yaw(geometry_msgs::Pose pose)
68 {
69  return tf::getYaw(pose.orientation);
70 }
71 
72 double yaw(geometry_msgs::PoseStamped pose)
73 {
74  return yaw(pose.pose);
75 }
76 
77 
78 double distance2D(double x, double y)
79 {
80  return std::sqrt(std::pow(x, 2) + std::pow(y, 2));
81 }
82 
83 double distance2D(const tf::Point& p)
84 {
85  return std::sqrt(std::pow(p.x(), 2) + std::pow(p.y(), 2));
86 }
87 
88 double distance2D(double ax, double ay, double bx, double by)
89 {
90  return std::sqrt(std::pow(ax - bx, 2) + std::pow(ay - by, 2));
91 }
92 
93 double distance2D(const tf::Point& p1, const tf::Point& p2)
94 {
95  return std::sqrt(std::pow(p2.x() - p1.x(), 2) + std::pow(p2.y() - p1.y(), 2));
96 }
97 
98 double distance2D(geometry_msgs::Point a, geometry_msgs::Point b)
99 {
100  return distance2D(tf::Vector3(a.x, a.y, a.z), tf::Vector3(b.x, b.y, b.z));
101 }
102 
103 double distance2D(geometry_msgs::Pose a, geometry_msgs::Pose b)
104 {
105  return distance2D(a.position, b.position);
106 }
107 
108 double distance2D(const tf::Transform& a, const tf::Transform& b)
109 {
110  return distance2D(a.getOrigin(), b.getOrigin());
111 }
112 
113 
114 double distance3D(double x, double y, double z)
115 {
116  return std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2));
117 }
118 
119 double distance3D(const tf::Point& p)
120 {
121  return std::sqrt(std::pow(p.x(), 2) + std::pow(p.y(), 2) + std::pow(p.z(), 2));
122 }
123 
124 double distance3D(double ax, double ay, double az, double bx, double by, double bz)
125 {
126  return std::sqrt(std::pow(ax - bx, 2) + std::pow(ay - by, 2) + std::pow(az - bz, 2));
127 }
128 
129 double distance3D(const tf::Point& p1, const tf::Point& p2)
130 {
131  return std::sqrt(std::pow(p2.x() - p1.x(), 2) + std::pow(p2.y() - p1.y(), 2) + std::pow(p2.z() - p1.z(), 2));
132 }
133 
134 double distance3D(geometry_msgs::Point a, geometry_msgs::Point b)
135 {
136  return distance3D(tf::Vector3(a.x, a.y, a.z), tf::Vector3(b.x, b.y, b.z));
137 }
138 
139 double distance3D(geometry_msgs::Pose a, geometry_msgs::Pose b)
140 {
141  return distance3D(a.position, b.position);
142 }
143 
144 double distance3D(const tf::Transform& a, const tf::Transform& b)
145 {
146  return distance3D(a.getOrigin(), b.getOrigin());
147 }
148 
149 
150 double heading(const tf::Vector3& p)
151 {
152  return std::atan2(p.y(), p.x());
153 }
154 
155 double heading(geometry_msgs::Point p)
156 {
157  return heading(tf::Vector3(p.x, p.y, p.z));
158 }
159 
160 double heading(geometry_msgs::Pose p)
161 {
162  return heading(p.position);
163 }
164 
165 double heading(const tf::Transform& t)
166 {
167  return heading(t.getOrigin());
168 }
169 
170 double heading(const tf::Vector3& a, const tf::Vector3& b)
171 {
172  return std::atan2(b.y() - a.y(), b.x() - a.x());
173 }
174 
175 double heading(geometry_msgs::Point a, geometry_msgs::Point b)
176 {
177  return heading(tf::Vector3(a.x, a.y, a.z), tf::Vector3(b.x, b.y, b.z));
178 }
179 
180 double heading(geometry_msgs::Pose a, geometry_msgs::Pose b)
181 {
182  return heading(a.position, b.position);
183 }
184 
185 double heading(const tf::Transform& a, const tf::Transform& b)
186 {
187  return heading(a.getOrigin(), b.getOrigin());
188 }
189 
190 double minAngle(const tf::Quaternion& a, const tf::Quaternion& b)
191 {
192  return tf::angleShortestPath(a, b);
193 }
194 
195 double minAngle(geometry_msgs::Quaternion a, geometry_msgs::Quaternion b)
196 {
197  return minAngle(tf::Quaternion(a.x, a.y, a.z, a.w), tf::Quaternion(b.x, b.y, b.z, b.w));
198 }
199 
200 double minAngle(geometry_msgs::Pose a, geometry_msgs::Pose b)
201 {
202  return minAngle(a.orientation, b.orientation);
203 }
204 
205 double minAngle(const tf::Transform& a, const tf::Transform& b)
206 {
207  return minAngle(a.getRotation(), b.getRotation());
208 }
209 
210 bool sameFrame(const geometry_msgs::PoseStamped& a, const geometry_msgs::PoseStamped& b)
211 {
212  return sameFrame(a.header.frame_id, b.header.frame_id);
213 }
214 
215 bool sameFrame(const std::string& frame_a, const std::string& frame_b)
216 {
217  if (frame_a.length() == 0 && frame_b.length() == 0)
218  {
219  ROS_WARN("Comparing two empty frame ids (considered as the same frame)");
220  return true;
221  }
222 
223  if (frame_a.length() == 0 || frame_b.length() == 0)
224  {
225  ROS_WARN("Comparing %s%s with an empty frame id (considered as different frames)",
226  frame_a.c_str(), frame_b.c_str());
227  return false;
228  }
229 
230  int start_a = frame_a.at(0) == '/' ? 1 : 0;
231  int start_b = frame_b.at(0) == '/' ? 1 : 0;
232 
233  return frame_a.compare(start_a, frame_a.length(), frame_b, start_b, frame_b.length()) == 0;
234 }
235 
236 double pointSegmentDistance(double px, double py, double s1x, double s1y, double s2x, double s2y)
237 {
238  // Return minimum distance between line segment s1-s2 and point p
239 
240  double l = distance2D(s1x, s1y, s2x, s2y); // i.e. |p2 - p1|^2
241  if (l == 0.0)
242  return distance2D(px, py, s1x, s1y); // s1 == s2 case
243 
244  // Consider the line extending the segment, parameterized as s1 + t (s2 - s1).
245  // We find projection of point p onto the line.
246  // It falls where t = [(p - s1) . (s2 - s1)] / |s2 - s1|^2
247  double t = (- s1x * (s2x - s1x) - s1y * (s2y - s1y)) / l;
248  if (t < 0.0) // Beyond the s1 end of the segment
249  return distance2D(s1x, s1y);
250 
251  if (t > 1.0) // Beyond the s2 end of the segment
252  return distance2D(s2x, s2y);
253 
254  // Projection falls on the segment
255  return distance2D(s1x + t * (s2x - s1x), s1y + t * (s2y - s1y));
256 }
257 
258 bool raySegmentIntersection(double r1x, double r1y, double r2x, double r2y,
259  double s1x, double s1y, double s2x, double s2y,
260  double& ix, double& iy, double& distance)
261 {
262  double r, s, d;
263  // Make sure the lines aren't parallel
264  if ((r2y - r1y) / (r2x - r1x) != (s2y - s1y) / (s2x - s1x))
265  {
266  d = (((r2x - r1x) * (s2y - s1y)) - (r2y - r1y) * (s2x - s1x));
267  if (d != 0)
268  {
269  r = (((r1y - s1y) * (s2x - s1x)) - (r1x - s1x) * (s2y - s1y)) / d;
270  s = (((r1y - s1y) * (r2x - r1x)) - (r1x - s1x) * (r2y - r1y)) / d;
271  if (r >= 0)
272  {
273  if (s >= 0 && s <= 1)
274  {
275  ix = r1x + r * (r2x - r1x);
276  iy = r1y + r * (r2y - r1y);
277  distance = distance2D(ix, iy);
278  return true;
279  }
280  }
281  }
282  }
283  return false;
284 }
285 
286 bool rayCircleIntersection(double rx, double ry, double cx, double cy, double radius,
287  double& ix, double& iy, double& distance)
288 {
289  double a = rx * rx + ry * ry;
290  double bBy2 = rx * cx + ry * cy;
291  double c = cx * cx + cy * cy - radius * radius;
292 
293  double pBy2 = bBy2 / a;
294  double q = c / a;
295 
296  double discriminant = pBy2 * pBy2 - q;
297  if (discriminant < 0)
298  return false;
299 
300  // if disc == 0 ... dealt with later
301  double tmpSqrt = std::sqrt(discriminant);
302  double abScalingFactor1 = -pBy2 + tmpSqrt;
303  double abScalingFactor2 = -pBy2 - tmpSqrt;
304 
305  ix = - rx * abScalingFactor1;
306  iy = - ry * abScalingFactor1;
307  distance = distance2D(ix, iy);
308 
309  // discard the backward-pointing half of the ray
310  if ((ix*rx < 0.0) && (iy*ry < 0.0))
311  return false;
312 
313  if (discriminant == 0) // abScalingFactor1 == abScalingFactor2
314  return true;
315 
316  // Check if the second intersection point is close (naively inefficient)
317  double i2x = - rx * abScalingFactor2;
318  double i2y = - ry * abScalingFactor2;
319  double distance2 = distance2D(i2x, i2y);
320 
321  if (distance2 < distance)
322  {
323  ix = i2x;
324  iy = i2y;
325  distance = distance2;
326  }
327 
328  return true;
329 }
330 
331 
332 } /* namespace mtk */
d
void pose2tf(const geometry_msgs::Pose &pose, tf::Transform &tf)
Definition: common.cpp:31
double wrapAngle(double a)
Definition: geometry.cpp:16
static double getYaw(const Quaternion &bt_q)
XmlRpcServer s
double pitch(const tf::Transform &tf)
Definition: geometry.cpp:43
Definition: common.hpp:18
double yaw(const tf::Transform &tf)
Definition: geometry.cpp:62
TFSIMD_FORCE_INLINE tfScalar angleShortestPath(const Quaternion &q1, const Quaternion &q2)
double roll(const tf::Transform &tf)
Definition: geometry.cpp:24
#define ROS_WARN(...)
bool sameFrame(const std::string &frame_a, const std::string &frame_b)
Definition: geometry.cpp:215
bool raySegmentIntersection(double r1x, double r1y, double r2x, double r2y, double s1x, double s1y, double s2x, double s2y, double &ix, double &iy, double &distance)
Definition: geometry.cpp:258
double distance2D(double ax, double ay, double bx, double by)
Definition: geometry.cpp:88
bool rayCircleIntersection(double rx, double ry, double cx, double cy, double radius, double &ix, double &iy, double &distance)
Definition: geometry.cpp:286
TFSIMD_FORCE_INLINE const tfScalar & x() const
TFSIMD_FORCE_INLINE const tfScalar & z() const
TFSIMD_FORCE_INLINE const tfScalar & y() const
double pointSegmentDistance(double px, double py, double s1x, double s1y, double s2x, double s2y)
Definition: geometry.cpp:236
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
Quaternion getRotation() const
TFSIMD_FORCE_INLINE tfScalar distance2(const Vector3 &v) const
double minAngle(geometry_msgs::Quaternion a, geometry_msgs::Quaternion b)
Definition: geometry.cpp:195
double heading(geometry_msgs::Point p)
Definition: geometry.cpp:155
double distance3D(double ax, double ay, double az, double bx, double by, double bz)
Definition: geometry.cpp:124


yocs_math_toolkit
Author(s): Jorge Santos
autogenerated on Mon Jun 10 2019 15:53:40