Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
distances.h
Go to the documentation of this file.
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 Willow Garage, Inc. 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: distances.h 4656 2012-02-22 08:31:48Z rusu $
35  *
36  */
37 #ifndef PCL_DISTANCES_H_
38 #define PCL_DISTANCES_H_
39 
40 #include <pcl/common/common.h>
41 
49 namespace pcl
50 {
58  PCL_EXPORTS void
59  lineToLineSegment (const Eigen::VectorXf &line_a, const Eigen::VectorXf &line_b,
60  Eigen::Vector4f &pt1_seg, Eigen::Vector4f &pt2_seg);
61 
68  double inline
69  sqrPointToLineDistance (const Eigen::Vector4f &pt, const Eigen::Vector4f &line_pt, const Eigen::Vector4f &line_dir)
70  {
71  // Calculate the distance from the point to the line
72  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p1-p0)) / norm(p2-p1)
73  return (line_dir.cross3 (line_pt - pt)).squaredNorm () / line_dir.squaredNorm ();
74  }
75 
84  double inline
85  sqrPointToLineDistance (const Eigen::Vector4f &pt, const Eigen::Vector4f &line_pt, const Eigen::Vector4f &line_dir, const double sqr_length)
86  {
87  // Calculate the distance from the point to the line
88  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p1-p0)) / norm(p2-p1)
89  return (line_dir.cross3 (line_pt - pt)).squaredNorm () / sqr_length;
90  }
91 
99  template <typename PointT> double inline
101  PointT &pmin, PointT &pmax)
102  {
103  double max_dist = std::numeric_limits<double>::min ();
104  int i_min = -1, i_max = -1;
105 
106  for (size_t i = 0; i < cloud.points.size (); ++i)
107  {
108  for (size_t j = i; j < cloud.points.size (); ++j)
109  {
110  // Compute the distance
111  double dist = (cloud.points[i].getVector4fMap () -
112  cloud.points[j].getVector4fMap ()).squaredNorm ();
113  if (dist <= max_dist)
114  continue;
115 
116  max_dist = dist;
117  i_min = i;
118  i_max = j;
119  }
120  }
121 
122  if (i_min == -1 || i_max == -1)
123  return (max_dist = std::numeric_limits<double>::min ());
124 
125  pmin = cloud.points[i_min];
126  pmax = cloud.points[i_max];
127  return (std::sqrt (max_dist));
128  }
129 
138  template <typename PointT> double inline
139  getMaxSegment (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &indices,
140  PointT &pmin, PointT &pmax)
141  {
142  double max_dist = std::numeric_limits<double>::min ();
143  int i_min = -1, i_max = -1;
144 
145  for (size_t i = 0; i < indices.size (); ++i)
146  {
147  for (size_t j = i; j < indices.size (); ++j)
148  {
149  // Compute the distance
150  double dist = (cloud.points[indices[i]].getVector4fMap () -
151  cloud.points[indices[j]].getVector4fMap ()).squaredNorm ();
152  if (dist <= max_dist)
153  continue;
154 
155  max_dist = dist;
156  i_min = i;
157  i_max = j;
158  }
159  }
160 
161  if (i_min == -1 || i_max == -1)
162  return (max_dist = std::numeric_limits<double>::min ());
163 
164  pmin = cloud.points[indices[i_min]];
165  pmax = cloud.points[indices[i_max]];
166  return (std::sqrt (max_dist));
167  }
168 
173  template<typename PointType1, typename PointType2> inline float
174  squaredEuclideanDistance (const PointType1& p1, const PointType2& p2)
175  {
176  float diff_x = p2.x - p1.x, diff_y = p2.y - p1.y, diff_z = p2.z - p1.z;
177  return (diff_x*diff_x + diff_y*diff_y + diff_z*diff_z);
178  }
183  template<typename PointType1, typename PointType2> inline float
184  euclideanDistance (const PointType1& p1, const PointType2& p2)
185  {
186  return (sqrtf (squaredEuclideanDistance (p1, p2)));
187  }
188 }
189 /*@*/
190 #endif //#ifndef PCL_DISTANCES_H_
191