Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <swri_geometry_util/geometry_util.h>
00031
00032 namespace swri_geometry_util
00033 {
00034 double DistanceFromPlane(
00035 const tf::Vector3& plane_normal,
00036 const tf::Vector3& plane_point,
00037 const tf::Vector3& point)
00038 {
00039 return plane_normal.normalized().dot(point - plane_point);
00040 }
00041
00042 double DistanceFromLineSegment(
00043 const tf::Vector3& line_start,
00044 const tf::Vector3& line_end,
00045 const tf::Vector3& point)
00046 {
00047 return point.distance(ProjectToLineSegment(line_start, line_end, point));
00048 }
00049
00050 tf::Vector3 ProjectToLineSegment(
00051 const tf::Vector3& line_start,
00052 const tf::Vector3& line_end,
00053 const tf::Vector3& point)
00054 {
00055 tf::Vector3 v = line_end - line_start;
00056 tf::Vector3 r = point - line_start;
00057
00058 double t = r.dot(v);
00059 if (t <= 0)
00060 {
00061 return line_start;
00062 }
00063
00064 double b = v.dot(v);
00065 if (t >= b)
00066 {
00067 return line_end;
00068 }
00069
00070 return line_start + (t / b) * v;
00071 }
00072
00073 bool ClosestPointToLines(
00074 const tf::Vector3& a1,
00075 const tf::Vector3& a2,
00076 const tf::Vector3& b1,
00077 const tf::Vector3& b2,
00078 tf::Vector3& point)
00079 {
00080 tf::Vector3 u = a1 - a2;
00081 tf::Vector3 v = b1 - b2;
00082 if (u.length() == 0 || v.length() == 0)
00083 {
00084 return false;
00085 }
00086 tf::Vector3 w = u.cross(v);
00087 tf::Vector3 s = b1 - a1;
00088 if (s.length() == 0)
00089 {
00090 point = a1;
00091 return true;
00092 }
00093 double f = w.dot(w);
00094 if (f == 0)
00095 {
00096 return false;
00097 }
00098 tf::Vector3 x = a1 + u * (s.cross(v).dot(w) / f);
00099 tf::Vector3 y = b1 + v * (s.cross(u).dot(w) / f);
00100 point = (x + y) / 2;
00101 return true;
00102 }
00103 }