8 #include "../../include/yocs_math_toolkit/common.hpp" 9 #include "../../include/yocs_math_toolkit/geometry.hpp" 18 a = fmod(a + M_PI, 2*M_PI);
31 double roll(geometry_msgs::Pose pose)
38 double roll(geometry_msgs::PoseStamped pose)
40 return roll(pose.pose);
50 double pitch(geometry_msgs::Pose pose)
57 double pitch(geometry_msgs::PoseStamped pose)
59 return pitch(pose.pose);
67 double yaw(geometry_msgs::Pose pose)
72 double yaw(geometry_msgs::PoseStamped pose)
74 return yaw(pose.pose);
80 return std::sqrt(std::pow(x, 2) + std::pow(y, 2));
85 return std::sqrt(std::pow(p.
x(), 2) + std::pow(p.
y(), 2));
88 double distance2D(
double ax,
double ay,
double bx,
double by)
90 return std::sqrt(std::pow(ax - bx, 2) + std::pow(ay - by, 2));
95 return std::sqrt(std::pow(p2.
x() - p1.
x(), 2) + std::pow(p2.
y() - p1.
y(), 2));
98 double distance2D(geometry_msgs::Point a, geometry_msgs::Point b)
103 double distance2D(geometry_msgs::Pose a, geometry_msgs::Pose b)
116 return std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2));
121 return std::sqrt(std::pow(p.
x(), 2) + std::pow(p.
y(), 2) + std::pow(p.
z(), 2));
124 double distance3D(
double ax,
double ay,
double az,
double bx,
double by,
double bz)
126 return std::sqrt(std::pow(ax - bx, 2) + std::pow(ay - by, 2) + std::pow(az - bz, 2));
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));
134 double distance3D(geometry_msgs::Point a, geometry_msgs::Point b)
139 double distance3D(geometry_msgs::Pose a, geometry_msgs::Pose b)
152 return std::atan2(p.
y(), p.
x());
172 return std::atan2(b.
y() - a.
y(), b.
x() - a.
x());
175 double heading(geometry_msgs::Point a, geometry_msgs::Point b)
180 double heading(geometry_msgs::Pose a, geometry_msgs::Pose b)
182 return heading(a.position, b.position);
195 double minAngle(geometry_msgs::Quaternion a, geometry_msgs::Quaternion b)
200 double minAngle(geometry_msgs::Pose a, geometry_msgs::Pose b)
202 return minAngle(a.orientation, b.orientation);
210 bool sameFrame(
const geometry_msgs::PoseStamped& a,
const geometry_msgs::PoseStamped& b)
212 return sameFrame(a.header.frame_id, b.header.frame_id);
215 bool sameFrame(
const std::string& frame_a,
const std::string& frame_b)
217 if (frame_a.length() == 0 && frame_b.length() == 0)
219 ROS_WARN(
"Comparing two empty frame ids (considered as the same frame)");
223 if (frame_a.length() == 0 || frame_b.length() == 0)
225 ROS_WARN(
"Comparing %s%s with an empty frame id (considered as different frames)",
226 frame_a.c_str(), frame_b.c_str());
230 int start_a = frame_a.at(0) ==
'/' ? 1 : 0;
231 int start_b = frame_b.at(0) ==
'/' ? 1 : 0;
233 return frame_a.compare(start_a, frame_a.length(), frame_b, start_b, frame_b.length()) == 0;
247 double t = (- s1x * (s2x - s1x) - s1y * (s2y - s1y)) / l;
255 return distance2D(s1x + t * (s2x - s1x), s1y + t * (s2y - s1y));
259 double s1x,
double s1y,
double s2x,
double s2y,
260 double& ix,
double& iy,
double& distance)
264 if ((r2y - r1y) / (r2x - r1x) != (s2y - s1y) / (s2x - s1x))
266 d = (((r2x - r1x) * (s2y - s1y)) - (r2y - r1y) * (s2x - s1x));
269 r = (((r1y - s1y) * (s2x - s1x)) - (r1x - s1x) * (s2y - s1y)) / d;
270 s = (((r1y - s1y) * (r2x - r1x)) - (r1x - s1x) * (r2y - r1y)) / d;
273 if (s >= 0 && s <= 1)
275 ix = r1x + r * (r2x - r1x);
276 iy = r1y + r * (r2y - r1y);
287 double& ix,
double& iy,
double& distance)
289 double a = rx * rx + ry * ry;
290 double bBy2 = rx * cx + ry * cy;
291 double c = cx * cx + cy * cy - radius * radius;
293 double pBy2 = bBy2 / a;
296 double discriminant = pBy2 * pBy2 - q;
297 if (discriminant < 0)
301 double tmpSqrt = std::sqrt(discriminant);
302 double abScalingFactor1 = -pBy2 + tmpSqrt;
303 double abScalingFactor2 = -pBy2 - tmpSqrt;
305 ix = - rx * abScalingFactor1;
306 iy = - ry * abScalingFactor1;
310 if ((ix*rx < 0.0) && (iy*ry < 0.0))
313 if (discriminant == 0)
317 double i2x = - rx * abScalingFactor2;
318 double i2y = - ry * abScalingFactor2;
321 if (distance2 < distance)
325 distance = distance2;
void pose2tf(const geometry_msgs::Pose &pose, tf::Transform &tf)
double wrapAngle(double a)
static double getYaw(const Quaternion &bt_q)
double pitch(const tf::Transform &tf)
double yaw(const tf::Transform &tf)
TFSIMD_FORCE_INLINE tfScalar angleShortestPath(const Quaternion &q1, const Quaternion &q2)
double roll(const tf::Transform &tf)
bool sameFrame(const std::string &frame_a, const std::string &frame_b)
bool raySegmentIntersection(double r1x, double r1y, double r2x, double r2y, double s1x, double s1y, double s2x, double s2y, double &ix, double &iy, double &distance)
double distance2D(double ax, double ay, double bx, double by)
bool rayCircleIntersection(double rx, double ry, double cx, double cy, double radius, double &ix, double &iy, double &distance)
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)
TFSIMD_FORCE_INLINE tfScalar distance2(const Vector3 &v) const
double minAngle(geometry_msgs::Quaternion a, geometry_msgs::Quaternion b)
double heading(geometry_msgs::Point p)
double distance3D(double ax, double ay, double az, double bx, double by, double bz)