convex_polygon.h
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2014, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #ifndef JSK_RECOGNITION_UTILS_GEO_CONVEX_POLYGON_H_
00037 #define JSK_RECOGNITION_UTILS_GEO_CONVEX_POLYGON_H_
00038 
00039 #include "jsk_recognition_utils/geo/polygon.h"
00040 #include <pcl/segmentation/extract_polygonal_prism_data.h>
00041 #include <pcl/filters/project_inliers.h>
00042 #include <pcl/surface/convex_hull.h>
00043 #include "jsk_recognition_utils/pcl_util.h"
00044 
00045 
00046 namespace jsk_recognition_utils
00047 {
00048   class ConvexPolygon: public Polygon
00049   {
00050   public:
00051     typedef boost::shared_ptr<ConvexPolygon> Ptr;
00052     typedef Eigen::Vector3f Vertex;
00053     typedef std::vector<Eigen::Vector3f,
00054                         Eigen::aligned_allocator<Eigen::Vector3f> > Vertices;
00055     // vertices should be CW
00056     ConvexPolygon(const Vertices& vertices);
00057     ConvexPolygon(const Vertices& vertices,
00058                   const std::vector<float>& coefficients);
00059     //virtual Polygon flip();
00060     virtual void project(const Eigen::Vector3f& p, Eigen::Vector3f& output);
00061     virtual void project(const Eigen::Vector3d& p, Eigen::Vector3d& output);
00062     virtual void project(const Eigen::Vector3d& p, Eigen::Vector3f& output);
00063     virtual void project(const Eigen::Vector3f& p, Eigen::Vector3d& output);
00064     virtual void projectOnPlane(const Eigen::Vector3f& p,
00065                                 Eigen::Vector3f& output);
00066     virtual void projectOnPlane(const Eigen::Affine3f& p,
00067                                 Eigen::Affine3f& output);
00068     virtual bool isProjectableInside(const Eigen::Vector3f& p);
00069     // p should be a point on the plane
00070     virtual ConvexPolygon flipConvex();
00071     virtual Eigen::Vector3f getCentroid();
00072     virtual Ptr magnify(const double scale_factor);
00073     virtual Ptr magnifyByDistance(const double distance);
00074     
00075     static ConvexPolygon fromROSMsg(const geometry_msgs::Polygon& polygon);
00076     static ConvexPolygon::Ptr fromROSMsgPtr(const geometry_msgs::Polygon& polygon);
00077     bool distanceSmallerThan(
00078       const Eigen::Vector3f& p, double distance_threshold);
00079     bool distanceSmallerThan(
00080       const Eigen::Vector3f& p, double distance_threshold,
00081       double& output_distance);
00082     bool allEdgesLongerThan(double thr);
00083     double distanceFromVertices(const Eigen::Vector3f& p);
00084     geometry_msgs::Polygon toROSMsg();
00085   protected:
00086 
00087   private:
00088   };
00089 
00090   template<class PointT>
00091   ConvexPolygon::Ptr convexFromCoefficientsAndInliers(
00092     const typename pcl::PointCloud<PointT>::Ptr cloud,
00093     const pcl::PointIndices::Ptr inliers,
00094     const pcl::ModelCoefficients::Ptr coefficients) {
00095     typedef typename pcl::PointCloud<PointT> POINTCLOUD;
00096     typename POINTCLOUD::Ptr projected_cloud(new pcl::PointCloud<PointT>);
00097     // check inliers has enough points
00098     if (inliers->indices.size() == 0) {
00099       return ConvexPolygon::Ptr();
00100     }
00101     // project inliers based on coefficients
00102     pcl::ProjectInliers<PointT> proj;
00103     proj.setModelType(pcl::SACMODEL_PERPENDICULAR_PLANE);
00104     proj.setInputCloud(cloud);
00105     proj.setModelCoefficients(coefficients);
00106     proj.setIndices(inliers);
00107     proj.filter(*projected_cloud);
00108     // compute convex with giant mutex
00109     {
00110       boost::mutex::scoped_lock lock(global_chull_mutex);
00111       typename POINTCLOUD::Ptr convex_cloud(new pcl::PointCloud<PointT>);
00112       pcl::ConvexHull<PointT> chull;
00113       chull.setDimension(2);
00114       chull.setInputCloud (projected_cloud);
00115       chull.reconstruct (*convex_cloud);
00116       if (convex_cloud->points.size() > 0) {
00117         // convert pointcloud to vertices
00118         Vertices vs;
00119         for (size_t i = 0; i < convex_cloud->points.size(); i++) {
00120           Eigen::Vector3f v(convex_cloud->points[i].getVector3fMap());
00121           vs.push_back(v);
00122         }
00123         return ConvexPolygon::Ptr(new ConvexPolygon(vs));
00124       }
00125       else {
00126         return ConvexPolygon::Ptr();
00127       }
00128     }
00129   }
00130 }
00131 
00132 #endif


jsk_recognition_utils
Author(s):
autogenerated on Sun Oct 8 2017 02:42:48