00001 /* 00002 * marker_container.h 00003 * 00004 * Created on: 14.11.2012 00005 * Author: josh 00006 */ 00007 00008 #ifndef MARKER_CONTAINER_H_ 00009 #define MARKER_CONTAINER_H_ 00010 00011 #include <rosbag/bag.h> 00012 #include "marker_list.h" 00013 00014 namespace cob_3d_marker { 00015 00016 class MarkerCreator { 00017 public: 00018 virtual void marker(cob_3d_marker::MarkerContainer &mc) const=0; 00019 }; 00020 00021 class MarkerWrite { 00022 public: 00023 virtual void write(const visualization_msgs::Marker &marker)=0; 00024 }; 00025 00026 class MarkerPublisher : public MarkerWrite { 00027 ros::Publisher &pub_; 00028 public: 00029 MarkerPublisher(ros::Publisher &pub):pub_(pub) {} 00030 00031 virtual void write(const visualization_msgs::Marker &marker) { 00032 pub_.publish(marker); 00033 } 00034 }; 00035 00036 class MarkerBagfile : public MarkerWrite { 00037 rosbag::Bag *bag_; 00038 std::string topic_; 00039 ros::Time time_; 00040 public: 00041 MarkerBagfile(rosbag::Bag *bag, const std::string topic="/markers", const ros::Time &time = ros::Time::now()):bag_(bag),topic_(topic), time_(time) {} 00042 00043 virtual void write(const visualization_msgs::Marker &marker) { 00044 bag_->write(topic_, time_, marker); 00045 } 00046 }; 00047 00048 class MarkerClean {}; 00049 00050 class MarkerContainer { 00051 typedef std::map<int, boost::shared_ptr<MarkerList> > MT; 00052 MT markers_; 00053 public: 00054 00055 void add(MarkerList *m) {markers_[m->getId()].reset(m);} 00056 00057 boost::shared_ptr<MarkerList> get(const int id) { 00058 MT::iterator it = markers_.find(id); 00059 if(it==markers_.end()) 00060 return boost::shared_ptr<MarkerList>(); 00061 return it->second; 00062 } 00063 00064 void write(MarkerWrite *mw) { 00065 for(MT::const_iterator it=markers_.begin(); it!=markers_.end(); it++) 00066 for(size_t i=0; i<it->second->marker_.size(); i++) 00067 mw->write(it->second->marker_[i]); 00068 } 00069 00070 void clear() { 00071 markers_.clear(); 00072 } 00073 00074 MarkerContainer &operator<<(MarkerList *m) {add(m);return *this;} 00075 MarkerContainer &operator<<(const MarkerCreator &c) {c.marker(*this);return *this;} 00076 MarkerContainer &operator<<(const MarkerClean &c) {clean();return *this;} 00077 00078 MarkerContainer &operator>>(MarkerWrite *bag) {write(bag);return *this;} 00079 00080 void clean() { 00081 for(MT::const_iterator it=markers_.begin(); it!=markers_.end(); it++) 00082 for(size_t i=0; i<it->second->marker_.size(); i++) 00083 it->second->marker_[i].action = visualization_msgs::Marker::DELETE; 00084 } 00085 }; 00086 } 00087 00088 00089 00090 #endif /* MARKER_CONTAINER_H_ */