Go to the documentation of this file.00001
00063 #ifndef __COB_3D_SEGMENTATION_EDGE_HANDLER_H__
00064 #define __COB_3D_SEGMENTATION_EDGE_HANDLER_H__
00065
00066 #include <cob_3d_mapping_common/label_defines.h>
00067 #include "cob_3d_segmentation/edge_types.h"
00068
00069 #include <boost/function.hpp>
00070
00071 #include <pcl/point_cloud.h>
00072 #include <pcl/point_types.h>
00073
00074 namespace cob_3d_segmentation
00075 {
00076 template<typename EdgeT>
00077 class EdgeHandlerBase
00078 {
00079 public:
00080 typedef EdgeT EdgeType;
00081 typedef typename std::list<EdgeT>::iterator EdgePtr;
00082 typedef typename std::list<EdgeT>::reverse_iterator reverse_iterator;
00083 typedef typename std::list<EdgeT>::size_type size_type;
00084
00085 public:
00086 EdgeHandlerBase()
00087 { };
00088 virtual ~EdgeHandlerBase() { };
00089
00090 inline EdgePtr begin() { return edges_.begin(); }
00091 inline EdgePtr end() { return edges_.end(); }
00092 inline reverse_iterator rbegin() { return edges_.rbegin(); }
00093 inline reverse_iterator rend() { return edges_.rend(); }
00094
00095 inline std::pair<EdgePtr,EdgePtr> getEdges() { return std::make_pair(edges_.begin(),edges_.end()); }
00096 inline size_type numEdges() const { return edges_.size(); }
00097 virtual void erase(EdgePtr e) { edges_.erase(e); }
00098 virtual void clear() { edges_.clear(); }
00099
00100 inline EdgePtr createEdge() { edges_.push_back(EdgeType()); return --edges_.end(); }
00101 virtual void merge(EdgePtr source, EdgePtr target) = 0;
00102 virtual void move(int old_cid, int new_cid, EdgePtr e) = 0;
00103
00104 private:
00105 std::list<EdgeType> edges_;
00106 };
00107
00108 template<typename LabelT, typename PointT>
00109 class BoundaryPointsEdgeHandler : public EdgeHandlerBase<BoundaryPointsEdge>
00110 {
00111 public:
00112 typedef boost::shared_ptr<BoundaryPointsEdgeHandler<LabelT,PointT> > Ptr;
00113
00114 typedef pcl::PointCloud<LabelT> LabelCloud;
00115 typedef typename LabelCloud::ConstPtr LabelCloudConstPtr;
00116 typedef pcl::PointCloud<PointT> PointCloud;
00117 typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00118
00119 public:
00120 BoundaryPointsEdgeHandler()
00121 : edge_validator(BoundarySmoothnessValidator(0.8f))
00122 ,boundary_points_()
00123 { };
00124
00125 ~BoundaryPointsEdgeHandler() { };
00126
00127 void mapBoundaryPoints(pcl::PointCloud<pcl::PointXYZRGB>::Ptr points)
00128 {
00129 uint32_t color = LBL_BORDER;
00130 for(std::map<int,BoundaryPoint>::iterator b_it = boundary_points_.begin(); b_it != boundary_points_.end(); ++b_it)
00131 {
00132 points->points[b_it->first].rgb = *reinterpret_cast<float*>(&color);
00133 }
00134 }
00135
00136 void mapBoundaryPoints(pcl::PointCloud<pcl::PointXYZRGB>::Ptr points, pcl::PointCloud<pcl::Normal>::Ptr normals)
00137 {
00138 points->clear();
00139 normals->clear();
00140 points->resize(boundary_points_.size());
00141 normals->resize(boundary_points_.size());
00142 points->width = normals->width = 1;
00143 points->height = normals->height = boundary_points_.size();
00144 pcl::PointCloud<pcl::PointXYZRGB>::iterator p_it = points->begin();
00145 pcl::PointCloud<pcl::Normal>::iterator n_it = normals->begin();
00146 for(std::map<int,BoundaryPoint>::iterator b_it = boundary_points_.begin();
00147 b_it != boundary_points_.end(); ++b_it, ++p_it, ++n_it)
00148 {
00149 *p_it = surface_->points[b_it->first];
00150 n_it->getNormalVector3fMap() = b_it->second.normal;
00151 }
00152 }
00153
00154 void erase(EdgePtr e);
00155 void merge(EdgePtr source, EdgePtr target);
00156 void move(int old_cid, int new_cid, EdgePtr e);
00157
00158 inline void updateProperties(EdgePtr e, const int cid1, const int idx1, const int cid2, const int idx2)
00159 {
00160 e->boundary_pairs[cid1].push_back(idx1);
00161 e->boundary_pairs[cid2].push_back(idx2);
00162 boundary_points_.insert(std::make_pair(idx1, BoundaryPoint(idx2)));
00163 boundary_points_.insert(std::make_pair(idx2, BoundaryPoint(idx1)));
00164 }
00165 inline BoundaryPoint& getBoundaryPoint(const int idx) { return boundary_points_[idx]; }
00166
00167 const boost::function<bool (EdgePtr)> edge_validator;
00168
00169 inline void setLabelCloudIn(LabelCloudConstPtr labels) { labels_ = labels; }
00170 inline void setPointCloudIn(PointCloudConstPtr points) { surface_ = points; }
00171
00172 private:
00173 std::map<int,BoundaryPoint> boundary_points_;
00174 LabelCloudConstPtr labels_;
00175 PointCloudConstPtr surface_;
00176
00177 class BoundarySmoothnessValidator
00178 {
00179 public:
00180 BoundarySmoothnessValidator(float min_smoothness) : min_smoothness_(min_smoothness)
00181 { }
00182
00183 inline bool operator() (EdgePtr e) { return (e->smoothness > min_smoothness_); }
00184 private:
00185 float min_smoothness_;
00186 };
00187 };
00188 }
00189
00190 #endif