test_gradient_estimation.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  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of the copyright holder(s) nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id$
00037  *
00038  */
00039 
00040 #include <gtest/gtest.h>
00041 #include <pcl/point_cloud.h>
00042 #include <pcl/features/normal_3d.h>
00043 #include <pcl/features/intensity_gradient.h>
00044 
00045 using namespace pcl;
00046 using namespace pcl::io;
00047 using namespace std;
00048 
00050 TEST (PCL, IntensityGradientEstimation)
00051 {
00052   // Create a test cloud
00053   PointCloud<PointXYZI> cloud_xyzi;
00054   cloud_xyzi.height = 1;
00055   cloud_xyzi.is_dense = true;
00056   for (float x = -5.0f; x <= 5.0f; x += 0.1f)
00057   {
00058     for (float y = -5.0f; y <= 5.0f; y += 0.1f)
00059     {
00060       PointXYZI p;
00061       p.x = x;
00062       p.y = y;
00063       p.z = 0.1f * powf (x, 2.0f) + 0.5f * y + 1.0f;
00064       p.intensity = 0.1f * powf (x, 3.0f) + 0.2f * powf (y, 2.0f) + 1.0f * p.z + 20000.0f;
00065 
00066       cloud_xyzi.points.push_back (p);
00067     }
00068   }
00069   cloud_xyzi.width = static_cast<uint32_t> (cloud_xyzi.points.size ());
00070   PointCloud<PointXYZI>::ConstPtr cloud_ptr = cloud_xyzi.makeShared ();
00071 
00072   // Estimate surface normals
00073   PointCloud<Normal>::Ptr normals (new PointCloud<Normal> ());
00074   NormalEstimation<PointXYZI, Normal> norm_est;
00075   norm_est.setInputCloud (cloud_ptr);
00076   search::KdTree<PointXYZI>::Ptr treept1 (new search::KdTree<PointXYZI> (false));
00077   norm_est.setSearchMethod (treept1);
00078   norm_est.setRadiusSearch (0.25);
00079   norm_est.compute (*normals);
00080 
00081   // Estimate intensity gradient
00082   PointCloud<IntensityGradient> gradient;
00083   IntensityGradientEstimation<PointXYZI, Normal, IntensityGradient> grad_est;
00084   grad_est.setInputCloud (cloud_ptr);
00085   grad_est.setInputNormals (normals);
00086   search::KdTree<PointXYZI>::Ptr treept2 (new search::KdTree<PointXYZI> (false));
00087   grad_est.setSearchMethod (treept2);
00088   grad_est.setRadiusSearch (0.25);
00089   grad_est.compute (gradient);
00090 
00091   // Compare to gradient estimates to actual values
00092   for (size_t i = 0; i < cloud_ptr->points.size (); ++i)
00093   {
00094     const PointXYZI &p = cloud_ptr->points[i];
00095 
00096     // A pointer to the estimated gradient values
00097     const float * g_est = gradient.points[i].gradient;
00098 
00099     // Compute the surface normal analytically.
00100     float nx = -0.2f * p.x;
00101     float ny = -0.5f;
00102     float nz = 1.0f;
00103     float magnitude = sqrtf (nx * nx + ny * ny + nz * nz);
00104     nx /= magnitude;
00105     ny /= magnitude;
00106     nz /= magnitude;
00107 
00108     // Compute the intensity gradient analytically...
00109     float tmpx = 0.3f * powf (p.x, 2.0f);
00110     float tmpy = 0.4f * p.y;
00111     float tmpz = 1.0f;
00112     // ...and project the 3-D gradient vector onto the surface's tangent plane.
00113     float gx = (1 - nx * nx) * tmpx + (-nx * ny) * tmpy + (-nx * nz) * tmpz;
00114     float gy = (-ny * nx) * tmpx + (1 - ny * ny) * tmpy + (-ny * nz) * tmpz;
00115     float gz = (-nz * nx) * tmpx + (-nz * ny) * tmpy + (1 - nz * nz) * tmpz;
00116 
00117     // Compare the estimates to the derived values.
00118     const float tolerance = 0.11f;
00119     EXPECT_NEAR (g_est[0], gx, tolerance);
00120     EXPECT_NEAR (g_est[1], gy, tolerance);
00121     EXPECT_NEAR (g_est[2], gz, tolerance);
00122   }
00123 }
00124 
00125 /* ---[ */
00126 int
00127 main (int argc, char** argv)
00128 {
00129   testing::InitGoogleTest (&argc, argv);
00130   return (RUN_ALL_TESTS ());
00131 }
00132 /* ]--- */


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:34:57