geometry_util.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }


swri_geometry_util
Author(s): Marc Alban
autogenerated on Tue Oct 3 2017 03:19:17