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 the copyright holder(s) 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$
00035  *
00036  */
00037 
00038 #include <pcl/common/intersections.h>
00039 #include <pcl/console/print.h>
00040 
00041 bool
00042 pcl::lineWithLineIntersection (const Eigen::VectorXf &line_a, 
00043                                const Eigen::VectorXf &line_b, 
00044                                Eigen::Vector4f &point, double sqr_eps)
00045 {
00046   Eigen::Vector4f p1, p2;
00047   lineToLineSegment (line_a, line_b, p1, p2);
00048 
00049   // If the segment size is smaller than a pre-given epsilon...
00050   double sqr_dist = (p1 - p2).squaredNorm ();
00051   if (sqr_dist < sqr_eps)
00052   {
00053     point = p1;
00054     return (true);
00055   }
00056   point.setZero ();
00057   return (false);
00058 }
00059 
00060 bool
00061 pcl::lineWithLineIntersection (const pcl::ModelCoefficients &line_a, 
00062                                const pcl::ModelCoefficients &line_b, 
00063                                Eigen::Vector4f &point, double sqr_eps)
00064 {
00065   Eigen::VectorXf coeff1 = Eigen::VectorXf::Map (&line_a.values[0], line_a.values.size ());
00066   Eigen::VectorXf coeff2 = Eigen::VectorXf::Map (&line_b.values[0], line_b.values.size ());
00067   return (lineWithLineIntersection (coeff1, coeff2, point, sqr_eps));
00068 }
00069 
00070 bool 
00071 pcl::planeWithPlaneIntersection (const Eigen::Vector4f &plane_a, 
00072                                  const Eigen::Vector4f &plane_b,
00073                                  Eigen::VectorXf &line,
00074                                  double angular_tolerance)
00075 {
00076   //planes shouldn't be parallel
00077   double test_cosine = plane_a.head<3>().dot(plane_b.head<3>());
00078   double upper_limit = 1 + angular_tolerance;
00079   double lower_limit = 1 - angular_tolerance;
00080 
00081   if ((test_cosine < upper_limit) && (test_cosine > lower_limit))
00082   {
00083       PCL_ERROR ("Plane A and Plane B are Parallel");
00084       return (false);
00085   }
00086 
00087   if ((test_cosine > -upper_limit) && (test_cosine < -lower_limit))
00088   {
00089       PCL_ERROR ("Plane A and Plane B are Parallel");
00090       return (false);
00091   }
00092 
00093   Eigen::Vector4f line_direction = plane_a.cross3(plane_b);
00094   line_direction.normalized();
00095 
00096   //construct system of equations using lagrange multipliers with one objective function and two constraints
00097   Eigen::MatrixXf langegrange_coefs(5,5);
00098   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;
00099 
00100   Eigen::VectorXf b;
00101   b.resize(5);
00102   b << 0, 0, 0, -plane_a[3], -plane_b[3];
00103 
00104   //solve for the lagrange Multipliers
00105   Eigen::VectorXf x;
00106   x.resize(5);
00107   x = langegrange_coefs.colPivHouseholderQr().solve(b);
00108 
00109   line.resize(6);
00110   line.head<3>() = x.head<3>(); // the x[3] and x[4] are the values of the lagrange multipliers and are neglected
00111   line[3] = line_direction[0];
00112   line[4] = line_direction[1];
00113   line[5] = line_direction[2];
00114   return true;
00115 } 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:25:02