Point Cloud Library (PCL)  1.7.1
distances.h
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 #ifndef PCL_DISTANCES_H_
38 #define PCL_DISTANCES_H_
39 
40 #include <pcl/common/common.h>
41 
42 /**
43  * \file pcl/common/distances.h
44  * Define standard C methods to do distance calculations
45  * \ingroup common
46  */
47 
48 /*@{*/
49 namespace pcl
50 {
51  /** \brief Get the shortest 3D segment between two 3D lines
52  * \param line_a the coefficients of the first line (point, direction)
53  * \param line_b the coefficients of the second line (point, direction)
54  * \param pt1_seg the first point on the line segment
55  * \param pt2_seg the second point on the line segment
56  * \ingroup common
57  */
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 
62  /** \brief Get the square distance from a point to a line (represented by a point and a direction)
63  * \param pt a point
64  * \param line_pt a point on the line (make sure that line_pt[3] = 0 as there are no internal checks!)
65  * \param line_dir the line direction
66  * \ingroup common
67  */
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 
76  /** \brief Get the square distance from a point to a line (represented by a point and a direction)
77  * \note This one is useful if one has to compute many distances to a fixed line, so the vector length can be pre-computed
78  * \param pt a point
79  * \param line_pt a point on the line (make sure that line_pt[3] = 0 as there are no internal checks!)
80  * \param line_dir the line direction
81  * \param sqr_length the squared norm of the line direction
82  * \ingroup common
83  */
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 
92  /** \brief Obtain the maximum segment in a given set of points, and return the minimum and maximum points.
93  * \param[in] cloud the point cloud dataset
94  * \param[out] pmin the coordinates of the "minimum" point in \a cloud (one end of the segment)
95  * \param[out] pmax the coordinates of the "maximum" point in \a cloud (the other end of the segment)
96  * \return the length of segment length
97  * \ingroup common
98  */
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 
130  /** \brief Obtain the maximum segment in a given set of points, and return the minimum and maximum points.
131  * \param[in] cloud the point cloud dataset
132  * \param[in] indices a set of point indices to use from \a cloud
133  * \param[out] pmin the coordinates of the "minimum" point in \a cloud (one end of the segment)
134  * \param[out] pmax the coordinates of the "maximum" point in \a cloud (the other end of the segment)
135  * \return the length of segment length
136  * \ingroup common
137  */
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 
169  /** \brief Calculate the squared euclidean distance between the two given points.
170  * \param[in] p1 the first point
171  * \param[in] p2 the second point
172  */
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  }
179 
180  /** \brief Calculate the squared euclidean distance between the two given points.
181  * \param[in] p1 the first point
182  * \param[in] p2 the second point
183  */
184  template<> inline float
185  squaredEuclideanDistance (const PointXY& p1, const PointXY& p2)
186  {
187  float diff_x = p2.x - p1.x, diff_y = p2.y - p1.y;
188  return (diff_x*diff_x + diff_y*diff_y);
189  }
190 
191  /** \brief Calculate the euclidean distance between the two given points.
192  * \param[in] p1 the first point
193  * \param[in] p2 the second point
194  */
195  template<typename PointType1, typename PointType2> inline float
196  euclideanDistance (const PointType1& p1, const PointType2& p2)
197  {
198  return (sqrtf (squaredEuclideanDistance (p1, p2)));
199  }
200 }
201 /*@*/
202 #endif //#ifndef PCL_DISTANCES_H_
203