project_inliers.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2012, Willow Garage, Inc.
00006  *  Copyright (c) 2012-, Open Perception, Inc.
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of the copyright holder(s) nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  * $Id$
00038  *
00039  */
00040 
00041 #include <pcl/filters/impl/project_inliers.hpp>
00042 
00044 void
00045 pcl::ProjectInliers<pcl::PCLPointCloud2>::applyFilter (PCLPointCloud2 &output)
00046 {
00047   if (indices_->empty ())
00048   {
00049     PCL_WARN ("[pcl::%s::applyFilter] No indices given or empty indices!\n", getClassName ().c_str ());
00050     output.width = output.height = 0;
00051     output.data.clear ();
00052     return;
00053   }
00054 
00055   //Eigen::Map<Eigen::VectorXf, Eigen::Aligned> model_coefficients (&model_->values[0], model_->values.size ());
00056   // More expensive than a map but safer (32bit architectures seem to complain)
00057   Eigen::VectorXf model_coefficients (model_->values.size ());
00058   for (size_t i = 0; i < model_->values.size (); ++i)
00059     model_coefficients[i] = model_->values[i];
00060 
00061   // Construct the model and project
00062   if (!initSACModel (model_type_))
00063   {
00064     PCL_ERROR ("[pcl::%s::segment] Error initializing the SAC model!\n", getClassName ().c_str ());
00065     output.width = output.height = 0;
00066     output.data.clear ();
00067     return;
00068   }
00069   pcl::PointCloud<pcl::PointXYZ> cloud_out;
00070 
00071   if (!copy_all_fields_)
00072     sacmodel_->projectPoints (*indices_, model_coefficients, cloud_out, false);
00073   else
00074     sacmodel_->projectPoints (*indices_, model_coefficients, cloud_out, true);
00075 
00076   if (copy_all_data_)
00077   {
00078     output.height       = input_->height;
00079     output.width        = input_->width;
00080     output.is_bigendian = input_->is_bigendian;
00081     output.point_step   = input_->point_step;
00082     output.row_step     = input_->row_step;
00083     output.data         = input_->data;
00084     output.is_dense     = input_->is_dense;
00085 
00086     // Get the distance field index
00087     int x_idx = -1, y_idx = -1, z_idx = -1;
00088     for (int d = 0; d < static_cast<int> (output.fields.size ()); ++d)
00089     {
00090       if (output.fields[d].name == "x") x_idx = d;
00091       if (output.fields[d].name == "y") y_idx = d;
00092       if (output.fields[d].name == "z") z_idx = d;
00093     }
00094     if (x_idx == -1 || y_idx == -1 || z_idx == -1)
00095     {
00096       PCL_ERROR ("[pcl::%s::segment] X (%d) Y (%d) Z (%d) field dimensions not found!\n", getClassName ().c_str (), x_idx, y_idx, z_idx);
00097       output.width = output.height = 0;
00098       output.data.clear ();
00099       return;
00100     }
00101 
00102     // Copy the projected points
00103     for (size_t i = 0; i < indices_->size (); ++i)
00104     {
00105       memcpy (&output.data[(*indices_)[i] * output.point_step + output.fields[x_idx].offset], &cloud_out.points[(*indices_)[i]].x, sizeof (float));
00106       memcpy (&output.data[(*indices_)[i] * output.point_step + output.fields[y_idx].offset], &cloud_out.points[(*indices_)[i]].y, sizeof (float));
00107       memcpy (&output.data[(*indices_)[i] * output.point_step + output.fields[z_idx].offset], &cloud_out.points[(*indices_)[i]].z, sizeof (float));
00108     }
00109   }
00110   else
00111   {
00112     if (!copy_all_fields_)
00113     {
00114       pcl::toPCLPointCloud2<pcl::PointXYZ> (cloud_out, output);
00115     }
00116     else
00117     {
00118       // Copy everything
00119       output.height       = 1;
00120       output.width        = static_cast<uint32_t> (indices_->size ());
00121       output.point_step   = input_->point_step;
00122       output.data.resize (output.width * output.point_step);
00123       output.is_bigendian = input_->is_bigendian;
00124       output.row_step     = output.point_step * output.width;
00125       // All projections should return valid data, so is_dense = true
00126       output.is_dense     = true;
00127 
00128       // Get the distance field index
00129       int x_idx = -1, y_idx = -1, z_idx = -1;
00130       for (int d = 0; d < static_cast<int> (output.fields.size ()); ++d)
00131       {
00132         if (output.fields[d].name == "x") x_idx = d;
00133         if (output.fields[d].name == "y") y_idx = d;
00134         if (output.fields[d].name == "z") z_idx = d;
00135       }
00136 
00137       if (x_idx == -1 || y_idx == -1 || z_idx == -1)
00138       {
00139         PCL_ERROR ("[pcl::%s::segment] X (%d) Y (%d) Z (%d) field dimensions not found!\n", getClassName ().c_str (), x_idx, y_idx, z_idx);
00140         output.width = output.height = 0;
00141         output.data.clear ();
00142         return;
00143       }
00144 
00145       // Copy the projected points
00146       for (size_t i = 0; i < indices_->size (); ++i)
00147       {
00148         memcpy (&output.data[i * output.point_step], &input_->data[(*indices_)[i] * input_->point_step], output.point_step);
00149         memcpy (&output.data[i * output.point_step + output.fields[x_idx].offset], &cloud_out.points[(*indices_)[i]].x, sizeof (float));
00150         memcpy (&output.data[i * output.point_step + output.fields[y_idx].offset], &cloud_out.points[(*indices_)[i]].y, sizeof (float));
00151         memcpy (&output.data[i * output.point_step + output.fields[z_idx].offset], &cloud_out.points[(*indices_)[i]].z, sizeof (float));
00152       }
00153     }
00154   }
00155 }
00156 
00158 bool
00159 pcl::ProjectInliers<pcl::PCLPointCloud2>::initSACModel (int model_type)
00160 {
00161   // Convert the input data
00162   PointCloud<PointXYZ> cloud;
00163   fromPCLPointCloud2 (*input_, cloud);
00164   PointCloud<PointXYZ>::Ptr cloud_ptr = cloud.makeShared ();
00165 
00166   // Build the model
00167   switch (model_type)
00168   {
00169     case SACMODEL_PLANE:
00170     {
00171       //PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: SACMODEL_PLANE\n", getClassName ().c_str ());
00172       sacmodel_.reset (new SampleConsensusModelPlane<pcl::PointXYZ> (cloud_ptr));
00173       break;
00174     }
00175     case SACMODEL_LINE:
00176     {
00177       //PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: SACMODEL_LINE\n", getClassName ().c_str ());
00178       sacmodel_.reset (new SampleConsensusModelLine<pcl::PointXYZ> (cloud_ptr));
00179       break;
00180     }
00181     case SACMODEL_CIRCLE2D:
00182     {
00183       //PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: SACMODEL_CIRCLE2D\n", getClassName ().c_str ());
00184       sacmodel_.reset (new SampleConsensusModelCircle2D<pcl::PointXYZ> (cloud_ptr));
00185       break;
00186     }
00187     case SACMODEL_SPHERE:
00188     {
00189       //PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: SACMODEL_SPHERE\n", getClassName ().c_str ());
00190       sacmodel_.reset (new SampleConsensusModelSphere<pcl::PointXYZ> (cloud_ptr));
00191       break;
00192     }
00193     case SACMODEL_PARALLEL_LINE:
00194     {
00195       //PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: SACMODEL_PARALLEL_LINE\n", getClassName ().c_str ());
00196       sacmodel_.reset (new SampleConsensusModelParallelLine<pcl::PointXYZ> (cloud_ptr));
00197       break;
00198     }
00199     case SACMODEL_PERPENDICULAR_PLANE:
00200     {
00201       //PCL_DEBUG ("[pcl::%s::initSACModel] Using a model of type: SACMODEL_PERPENDICULAR_PLANE\n", getClassName ().c_str ());
00202       sacmodel_.reset (new SampleConsensusModelPerpendicularPlane<pcl::PointXYZ> (cloud_ptr));
00203       break;
00204     }
00205     case SACMODEL_CYLINDER:
00206     {
00207       //PCL_DEBUG ("[pcl::%s::segment] Using a model of type: SACMODEL_CYLINDER\n", getClassName ().c_str ());
00208       sacmodel_.reset (new SampleConsensusModelCylinder<pcl::PointXYZ, Normal> (cloud_ptr));
00209       break;
00210     }
00211     case SACMODEL_NORMAL_PLANE:
00212     {
00213       //PCL_DEBUG ("[pcl::%s::segment] Using a model of type: SACMODEL_NORMAL_PLANE\n", getClassName ().c_str ());
00214       sacmodel_.reset (new SampleConsensusModelNormalPlane<pcl::PointXYZ, Normal> (cloud_ptr));
00215       break;
00216     }
00217     case SACMODEL_CONE:
00218     {
00219       //PCL_DEBUG ("[pcl::%s::segment] Using a model of type: SACMODEL_CONE\n", getClassName ().c_str ());
00220       sacmodel_.reset (new SampleConsensusModelCone<pcl::PointXYZ, Normal> (cloud_ptr));
00221       break;
00222     }
00223     case SACMODEL_NORMAL_SPHERE:
00224     {
00225       //PCL_DEBUG ("[pcl::%s::segment] Using a model of type: SACMODEL_NORMAL_SPHERE\n", getClassName ().c_str ());
00226       sacmodel_.reset (new SampleConsensusModelNormalSphere<pcl::PointXYZ, Normal> (cloud_ptr));
00227       break;
00228     }
00229     case SACMODEL_NORMAL_PARALLEL_PLANE:
00230     {
00231       //PCL_DEBUG ("[pcl::%s::segment] Using a model of type: SACMODEL_NORMAL_PARALLEL_PLANE\n", getClassName ().c_str ());
00232       sacmodel_.reset (new SampleConsensusModelNormalParallelPlane<pcl::PointXYZ, Normal> (cloud_ptr));
00233       break;
00234     }
00235     case SACMODEL_PARALLEL_PLANE:
00236     {
00237       //PCL_DEBUG ("[pcl::%s::segment] Using a model of type: SACMODEL_PARALLEL_PLANE\n", getClassName ().c_str ());
00238       sacmodel_.reset (new SampleConsensusModelParallelPlane<pcl::PointXYZ> (cloud_ptr));
00239       break;
00240     }
00241     default:
00242     {
00243       PCL_ERROR ("[pcl::%s::initSACModel] No valid model given!\n", getClassName ().c_str ());
00244       return (false);
00245     }
00246   }
00247   return (true);
00248 }
00249 
00250 #ifndef PCL_NO_PRECOMPILE
00251 #include <pcl/impl/instantiate.hpp>
00252 #include <pcl/point_types.h>
00253 
00254 // Instantiations of specific point types
00255 #ifdef PCL_ONLY_CORE_POINT_TYPES
00256   PCL_INSTANTIATE(ProjectInliers, (pcl::PointXYZ)(pcl::PointXYZI)(pcl::PointXYZRGB)(pcl::PointXYZRGBA))
00257 #else
00258   PCL_INSTANTIATE(ProjectInliers, PCL_XYZ_POINT_TYPES)
00259 #endif
00260 
00261 #endif    // PCL_NO_PRECOMPILE
00262 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:31:21