Point Cloud Library (PCL)  1.10.1-dev
sac_model_parallel_line.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_PARALLEL_LINE_HPP_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_PARALLEL_LINE_HPP_
43 
44 #include <pcl/common/common.h>
45 #include <pcl/sample_consensus/sac_model_parallel_line.h>
46 
47 //////////////////////////////////////////////////////////////////////////
48 template <typename PointT> void
50  const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
51 {
52  // Check if the model is valid given the user constraints
53  if (!isModelValid (model_coefficients))
54  {
55  inliers.clear ();
56  return;
57  }
58 
59  SampleConsensusModelLine<PointT>::selectWithinDistance (model_coefficients, threshold, inliers);
60 }
61 
62 //////////////////////////////////////////////////////////////////////////
63 template <typename PointT> std::size_t
65  const Eigen::VectorXf &model_coefficients, const double threshold) const
66 {
67  // Check if the model is valid given the user constraints
68  if (!isModelValid (model_coefficients))
69  {
70  return (0);
71  }
72 
73  return (SampleConsensusModelLine<PointT>::countWithinDistance (model_coefficients, threshold));
74 }
75 
76 //////////////////////////////////////////////////////////////////////////
77 template <typename PointT> void
79  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
80 {
81  // Check if the model is valid given the user constraints
82  if (!isModelValid (model_coefficients))
83  {
84  distances.clear ();
85  return;
86  }
87 
88  SampleConsensusModelLine<PointT>::getDistancesToModel (model_coefficients, distances);
89 }
90 
91 //////////////////////////////////////////////////////////////////////////
92 template <typename PointT> bool
93 pcl::SampleConsensusModelParallelLine<PointT>::isModelValid (const Eigen::VectorXf &model_coefficients) const
94 {
95  if (!SampleConsensusModel<PointT>::isModelValid (model_coefficients))
96  return (false);
97 
98  // Check against template, if given
99  if (eps_angle_ > 0.0)
100  {
101  // Obtain the line direction
102  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
103 
104  Eigen::Vector4f axis (axis_[0], axis_[1], axis_[2], 0.0f);
105  double angle_diff = std::abs (getAngle3D (axis, line_dir));
106  angle_diff = (std::min) (angle_diff, M_PI - angle_diff);
107  // Check whether the current line model satisfies our angle threshold criterion with respect to the given axis
108  if (angle_diff > eps_angle_)
109  {
110  return (false);
111  }
112  }
113 
114  return (true);
115 }
116 
117 #define PCL_INSTANTIATE_SampleConsensusModelParallelLine(T) template class PCL_EXPORTS pcl::SampleConsensusModelParallelLine<T>;
118 
119 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_PARALLEL_LINE_HPP_
120 
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
double getAngle3D(const Eigen::Vector4f &v1, const Eigen::Vector4f &v2, const bool in_degree=false)
Compute the smallest angle between two 3D vectors in radians (default) or degree. ...
Definition: common.hpp:46
SampleConsensusModelLine defines a model for 3D line segmentation.
std::vector< index_t > Indices
Type used for indices in PCL.
Definition: types.h:142
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all squared distances from the cloud data to a given line model.
#define M_PI
Definition: pcl_macros.h:140
Define standard C methods and C++ classes that are common to all methods.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:70
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
bool isModelValid(const Eigen::VectorXf &model_coefficients) const override
Check whether a model is valid given the user constraints.