intersections.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: intersections.cpp 6126 2012-07-03 20:19:58Z aichim $
00035  *
00036  */
00037 
00038 #include <pcl/common/intersections.h>
00039 
00040 bool
00041 pcl::lineWithLineIntersection (const Eigen::VectorXf &line_a, 
00042                                const Eigen::VectorXf &line_b, 
00043                                Eigen::Vector4f &point, double sqr_eps)
00044 {
00045   Eigen::Vector4f p1, p2;
00046   lineToLineSegment (line_a, line_b, p1, p2);
00047 
00048   // If the segment size is smaller than a pre-given epsilon...
00049   double sqr_dist = (p1 - p2).squaredNorm ();
00050   if (sqr_dist < sqr_eps)
00051   {
00052     point = p1;
00053     return (true);
00054   }
00055   point.setZero ();
00056   return (false);
00057 }
00058 
00059 bool
00060 pcl::lineWithLineIntersection (const pcl::ModelCoefficients &line_a, 
00061                                const pcl::ModelCoefficients &line_b, 
00062                                Eigen::Vector4f &point, double sqr_eps)
00063 {
00064   Eigen::VectorXf coeff1 = Eigen::VectorXf::Map (&line_a.values[0], line_a.values.size ());
00065   Eigen::VectorXf coeff2 = Eigen::VectorXf::Map (&line_b.values[0], line_b.values.size ());
00066   return (lineWithLineIntersection (coeff1, coeff2, point, sqr_eps));
00067 }
00068 
00069 bool 
00070 pcl::planeWithPlaneIntersection (const Eigen::Vector4f &plane_a, 
00071                                  const Eigen::Vector4f &plane_b,
00072                                  Eigen::VectorXf &line,
00073                                  double angular_tolerance)
00074 {
00075   //planes shouldn't be parallel
00076   double test_cosine = plane_a.head<3>().dot(plane_b.head<3>());
00077   double upper_limit = 1 + angular_tolerance;
00078   double lower_limit = 1 - angular_tolerance;
00079 
00080   if ((test_cosine < upper_limit) && (test_cosine > lower_limit))
00081   {
00082       PCL_ERROR ("Plane A and Plane B are Parallel");
00083       return (false);
00084   }
00085 
00086   if ((test_cosine > -upper_limit) && (test_cosine < -lower_limit))
00087   {
00088       PCL_ERROR ("Plane A and Plane B are Parallel");
00089       return (false);
00090   }
00091 
00092   Eigen::Vector4f line_direction = plane_a.cross3(plane_b);
00093   line_direction.normalized();
00094 
00095   //construct system of equations using lagrange multipliers with one objective function and two constraints
00096   Eigen::MatrixXf langegrange_coefs(5,5);
00097   langegrange_coefs << 2,0,0,plane_a[0],plane_b[0],  0,2,0,plane_a[1],plane_b[1],  0,0,2, plane_a[2], plane_b[2], plane_a[0], plane_a[1] , plane_a[2], 0,0, plane_b[0], plane_b[1], plane_b[2], 0,0;
00098 
00099   Eigen::VectorXf b;
00100   b.resize(5);
00101   b << 0, 0, 0, -plane_a[3], -plane_b[3];
00102 
00103   //solve for the lagrange Multipliers
00104   Eigen::VectorXf x;
00105   x.resize(5);
00106   x = langegrange_coefs.colPivHouseholderQr().solve(b);
00107 
00108   line.resize(6);
00109   line.head<3>() = x.head<3>(); // the x[3] and x[4] are the values of the lagrange multipliers and are neglected
00110   line[3] = line_direction[0];
00111   line[4] = line_direction[1];
00112   line[5] = line_direction[2];
00113   return true;
00114 } 


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:15:30