Point Cloud Library (PCL)  1.9.1
intersections.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder(s) nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id$
35  *
36  */
37 
38 #ifndef PCL_COMMON_INTERSECTIONS_IMPL_HPP_
39 #define PCL_COMMON_INTERSECTIONS_IMPL_HPP_
40 
41 #include <pcl/pcl_macros.h>
42 #include <pcl/console/print.h>
43 
44 //////////////////////////////////////////////////////////////////////////////////////////
45 
46 bool
47 pcl::lineWithLineIntersection (const Eigen::VectorXf &line_a,
48  const Eigen::VectorXf &line_b,
49  Eigen::Vector4f &point, double sqr_eps)
50 {
51  Eigen::Vector4f p1, p2;
52  lineToLineSegment (line_a, line_b, p1, p2);
53 
54  // If the segment size is smaller than a pre-given epsilon...
55  double sqr_dist = (p1 - p2).squaredNorm ();
56  if (sqr_dist < sqr_eps)
57  {
58  point = p1;
59  return (true);
60  }
61  point.setZero ();
62  return (false);
63 }
64 
65 bool
67  const pcl::ModelCoefficients &line_b,
68  Eigen::Vector4f &point, double sqr_eps)
69 {
70  Eigen::VectorXf coeff1 = Eigen::VectorXf::Map (&line_a.values[0], line_a.values.size ());
71  Eigen::VectorXf coeff2 = Eigen::VectorXf::Map (&line_b.values[0], line_b.values.size ());
72  return (lineWithLineIntersection (coeff1, coeff2, point, sqr_eps));
73 }
74 
75 template <typename Scalar> bool
76 pcl::planeWithPlaneIntersection (const Eigen::Matrix<Scalar, 4, 1> &plane_a,
77  const Eigen::Matrix<Scalar, 4, 1> &plane_b,
78  Eigen::Matrix<Scalar, Eigen::Dynamic, 1> &line,
79  double angular_tolerance)
80 {
81  typedef Eigen::Matrix<Scalar, 3, 1> Vector3;
82  typedef Eigen::Matrix<Scalar, 4, 1> Vector4;
83  typedef Eigen::Matrix<Scalar, 5, 1> Vector5;
84  typedef Eigen::Matrix<Scalar, 5, 5> Matrix5;
85 
86  // Normalize plane normals
87  Vector3 plane_a_norm (plane_a.template head<3> ());
88  Vector3 plane_b_norm (plane_b.template head<3> ());
89  plane_a_norm.normalize ();
90  plane_b_norm.normalize ();
91 
92  // Test if planes are parallel
93  double test_cos = plane_a_norm.dot (plane_b_norm);
94  double tolerance_cos = 1 - sin (fabs (angular_tolerance));
95 
96  if (fabs (test_cos) > tolerance_cos)
97  {
98  PCL_DEBUG ("Plane A and Plane B are parallel.\n");
99  return (false);
100  }
101 
102  Vector4 line_direction = plane_a.cross3 (plane_b);
103  line_direction.normalized();
104 
105  // Construct system of equations using lagrange multipliers with one objective function and two constraints
106  Matrix5 langrange_coefs;
107  langrange_coefs << 2,0,0, plane_a[0], plane_b[0],
108  0,2,0, plane_a[1], plane_b[1],
109  0,0,2, plane_a[2], plane_b[2],
110  plane_a[0], plane_a[1], plane_a[2], 0, 0,
111  plane_b[0], plane_b[1], plane_b[2], 0, 0;
112 
113  Vector5 b;
114  b << 0, 0, 0, -plane_a[3], -plane_b[3];
115 
116  line.resize(6);
117  // Solve for the lagrange multipliers
118  line.template head<3>() = langrange_coefs.colPivHouseholderQr().solve(b).template head<3> ();
119  line.template tail<3>() = line_direction.template head<3>();
120  return (true);
121 }
122 
123 template <typename Scalar> bool
124 pcl::threePlanesIntersection (const Eigen::Matrix<Scalar, 4, 1> &plane_a,
125  const Eigen::Matrix<Scalar, 4, 1> &plane_b,
126  const Eigen::Matrix<Scalar, 4, 1> &plane_c,
127  Eigen::Matrix<Scalar, 3, 1> &intersection_point,
128  double determinant_tolerance)
129 {
130  typedef Eigen::Matrix<Scalar, 3, 1> Vector3;
131  typedef Eigen::Matrix<Scalar, 3, 3> Matrix3;
132 
133  // TODO: Using Eigen::HyperPlanes is better to solve this problem
134  // Check if some planes are parallel
135  Matrix3 normals_in_lines;
136 
137  for (int i = 0; i < 3; i++)
138  {
139  normals_in_lines (i, 0) = plane_a[i];
140  normals_in_lines (i, 1) = plane_b[i];
141  normals_in_lines (i, 2) = plane_c[i];
142  }
143 
144  Scalar determinant = normals_in_lines.determinant ();
145  if (fabs (determinant) < determinant_tolerance)
146  {
147  // det ~= 0
148  PCL_DEBUG ("At least two planes are parallel.\n");
149  return (false);
150  }
151 
152  // Left part of the 3 equations
153  Matrix3 left_member;
154 
155  for (int i = 0; i < 3; i++)
156  {
157  left_member (0, i) = plane_a[i];
158  left_member (1, i) = plane_b[i];
159  left_member (2, i) = plane_c[i];
160  }
161 
162  // Right side of the 3 equations
163  Vector3 right_member;
164  right_member << -plane_a[3], -plane_b[3], -plane_c[3];
165 
166  // Solve the system
167  intersection_point = left_member.fullPivLu ().solve (right_member);
168  return (true);
169 }
170 
171 #endif //PCL_COMMON_INTERSECTIONS_IMPL_HPP
PCL_EXPORTS bool threePlanesIntersection(const Eigen::Matrix< Scalar, 4, 1 > &plane_a, const Eigen::Matrix< Scalar, 4, 1 > &plane_b, const Eigen::Matrix< Scalar, 4, 1 > &plane_c, Eigen::Matrix< Scalar, 3, 1 > &intersection_point, double determinant_tolerance=1e-6)
Determine the point of intersection of three non-parallel planes by solving the equations.
PCL_EXPORTS bool planeWithPlaneIntersection(const Eigen::Matrix< Scalar, 4, 1 > &plane_a, const Eigen::Matrix< Scalar, 4, 1 > &plane_b, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &line, double angular_tolerance=0.1)
Determine the line of intersection of two non-parallel planes using lagrange multipliers.
std::vector< float > values
PCL_EXPORTS bool lineWithLineIntersection(const Eigen::VectorXf &line_a, const Eigen::VectorXf &line_b, Eigen::Vector4f &point, double sqr_eps=1e-4)
Get the intersection of a two 3D lines in space as a 3D point.
PCL_EXPORTS void lineToLineSegment(const Eigen::VectorXf &line_a, const Eigen::VectorXf &line_b, Eigen::Vector4f &pt1_seg, Eigen::Vector4f &pt2_seg)
Get the shortest 3D segment between two 3D lines.