test_visualization.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2009-2012, Willow Garage, Inc.
00005  *  Copyright (c) 2012-, Open Perception, 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 
00042 #include <pcl/point_types.h>
00043 #include <pcl/io/pcd_io.h>
00044 #include <pcl/features/normal_3d.h>
00045 #include <pcl/visualization/pcl_visualizer.h>
00046 
00047 using namespace pcl;
00048 using namespace pcl::io;
00049 using namespace pcl::visualization;
00050 using namespace std;
00051 
00052 PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ>);
00053 PointCloud<PointNormal>::Ptr cloud_with_normals (new PointCloud<PointNormal>);
00054 search::KdTree<PointXYZ>::Ptr tree;
00055 search::KdTree<PointNormal>::Ptr tree2;
00056 
00057 // add by ktran to test update functions
00058 PointCloud<PointXYZ>::Ptr cloud1 (new PointCloud<PointXYZ>);
00059 PointCloud<PointNormal>::Ptr cloud_with_normals1 (new PointCloud<PointNormal>);
00060 search::KdTree<PointXYZ>::Ptr tree3;
00061 search::KdTree<PointNormal>::Ptr tree4;
00062 
00064 TEST (PCL, PCLVisualizer_camera)
00065 {
00066   PCLVisualizer visualizer;
00067   visualizer.initCameraParameters ();
00068 
00069   // First test if the intrinsic+extrinsic to OpenGL conversion works back and forth
00070   Eigen::Matrix3f given_intrinsics (Eigen::Matrix3f::Identity ());
00071   given_intrinsics (0, 0) = 525.f;
00072   given_intrinsics (1, 1) = 525.f;
00073   given_intrinsics (0, 2) = 320.f;
00074   given_intrinsics (1, 2) = 240.f;
00075 
00076   float M_PIf = static_cast<float> (M_PI);
00077   Eigen::Matrix4f given_extrinsics (Eigen::Matrix4f::Identity ());
00078   given_extrinsics.block<3, 3> (0, 0) = Eigen::AngleAxisf (30.f * M_PIf / 180.f, Eigen::Vector3f (1.f, 0.f, 0.f)).matrix ();
00079   given_extrinsics.block<3, 1> (0, 3) = Eigen::Vector3f (10.f, 15.f, 20.f);
00080 
00081   visualizer.setCameraParameters (given_intrinsics, given_extrinsics);
00082   Eigen::Matrix4f viewer_pose = visualizer.getViewerPose ().matrix ();
00083 
00084   for (size_t i = 0; i < 4; ++i)
00085     for (size_t j = 0; j < 4; ++j)
00086       EXPECT_NEAR (given_extrinsics (i, j), viewer_pose (i, j), 1e-6);
00087 
00088 
00089   // Next, check if setting the OpenGL settings translate well back
00090   // Look towards the x-axis, which equates to a 90 degree rotation around the y-axis
00091   Eigen::Vector3f trans (10.f, 2.f, 20.f);
00092   visualizer.setCameraPosition (trans[0], trans[1], trans[2], trans[0] + 1., trans[1], trans[2], 0., 1., 0.);
00093   viewer_pose = visualizer.getViewerPose ().matrix ();
00094   Eigen::Matrix3f expected_rotation = Eigen::AngleAxisf (M_PIf / 2.0f, Eigen::Vector3f (0.f, 1.f, 0.f)).matrix ();
00095   for (size_t i = 0; i < 3; ++i)
00096     for (size_t j = 0; j < 3; ++j)
00097       EXPECT_NEAR (viewer_pose (i, j), expected_rotation (i, j), 1e-6);
00098   for (size_t i = 0; i < 3; ++i)
00099     EXPECT_NEAR (viewer_pose (i, 3), trans[i], 1e-6);
00100 
00101 
00102   // Now add the bunny point cloud and reset the camera based on the scene (i.e., VTK will compute a new camera pose
00103   // so that it includes the whole scene in the window)
00105 //  visualizer.addPointCloud (cloud1);
00106 //  visualizer.resetCamera ();
00107 //  visualizer.spinOnce ();
00108 //  viewer_pose = visualizer.getViewerPose ().matrix ();
00109 
00110 //  cerr << "reset camera viewer pose:" << endl << viewer_pose << endl;
00111 }
00112 
00113 
00114 
00115 
00116 /* ---[ */
00117 int
00118 main (int argc, char** argv)
00119 {
00120   if (argc < 2)
00121   {
00122     std::cerr << "No test file given. Please download `bunny.pcd` and pass its path to the test." << std::endl;
00123     return (-1);
00124   }
00125 
00126   // Load file
00127   pcl::PCLPointCloud2 cloud_blob;
00128   loadPCDFile (argv[1], cloud_blob);
00129   fromPCLPointCloud2 (cloud_blob, *cloud);
00130 
00131   // Create search tree
00132   tree.reset (new search::KdTree<PointXYZ> (false));
00133   tree->setInputCloud (cloud);
00134 
00135   // Normal estimation
00136   NormalEstimation<PointXYZ, Normal> n;
00137   PointCloud<Normal>::Ptr normals (new PointCloud<Normal> ());
00138   n.setInputCloud (cloud);
00139   //n.setIndices (indices[B);
00140   n.setSearchMethod (tree);
00141   n.setKSearch (20);
00142   n.compute (*normals);
00143 
00144   // Concatenate XYZ and normal information
00145   pcl::concatenateFields (*cloud, *normals, *cloud_with_normals);
00146       
00147   // Create search tree
00148   tree2.reset (new search::KdTree<PointNormal>);
00149   tree2->setInputCloud (cloud_with_normals);
00150 
00151   // Process for update cloud
00152   if (argc == 3)
00153   {
00154     pcl::PCLPointCloud2 cloud_blob1;
00155     loadPCDFile (argv[2], cloud_blob1);
00156     fromPCLPointCloud2 (cloud_blob1, *cloud1);
00157         // Create search tree
00158     tree3.reset (new search::KdTree<PointXYZ> (false));
00159     tree3->setInputCloud (cloud1);
00160 
00161     // Normal estimation
00162     NormalEstimation<PointXYZ, Normal> n1;
00163     PointCloud<Normal>::Ptr normals1 (new PointCloud<Normal> ());
00164     n1.setInputCloud (cloud1);
00165 
00166     n1.setSearchMethod (tree3);
00167     n1.setKSearch (20);
00168     n1.compute (*normals1);
00169 
00170     // Concatenate XYZ and normal information
00171     pcl::concatenateFields (*cloud1, *normals1, *cloud_with_normals1);
00172     // Create search tree
00173     tree4.reset (new search::KdTree<PointNormal>);
00174     tree4->setInputCloud (cloud_with_normals1);
00175   }
00176 
00177   // Testing
00178   testing::InitGoogleTest (&argc, argv);
00179   return (RUN_ALL_TESTS ());
00180 }
00181 /* ]--- */


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:36:18