00001 /* 00002 * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA) 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 00018 #include <string> 00019 #include "cob_obstacle_distance/shapes_manager.hpp" 00020 00021 ShapesManager::ShapesManager(const ros::Publisher& pub) : pub_(pub) 00022 { 00023 } 00024 00025 00026 ShapesManager::~ShapesManager() 00027 { 00028 this->clear(); 00029 } 00030 00031 00032 void ShapesManager::addShape(const std::string& id, PtrIMarkerShape_t s) 00033 { 00034 this->shapes_[id] = s; 00035 } 00036 00037 00038 void ShapesManager::removeShape(const std::string& id) 00039 { 00040 if (this->shapes_.count(id)) 00041 { 00042 PtrIMarkerShape_t s = this->shapes_[id]; 00043 visualization_msgs::Marker marker = s->getMarker(); 00044 marker.action = visualization_msgs::Marker::DELETE; 00045 this->pub_.publish(marker); 00046 } 00047 00048 this->shapes_.erase(id); 00049 } 00050 00051 00052 bool ShapesManager::getShape(const std::string& id, PtrIMarkerShape_t& s) 00053 { 00054 bool success = false; 00055 if (this->shapes_.count(id)) 00056 { 00057 s = this->shapes_[id]; 00058 success = true; 00059 } 00060 00061 return success; 00062 } 00063 00064 00065 void ShapesManager::draw() 00066 { 00067 visualization_msgs::MarkerArray marker_array; 00068 for (MapIter_t iter = shapes_.begin(); iter != shapes_.end(); ++iter) 00069 { 00070 PtrIMarkerShape_t elem = iter->second; 00071 if(elem->isDrawable()) 00072 { 00073 visualization_msgs::Marker marker = elem->getMarker(); 00074 marker_array.markers.push_back(marker); 00075 } 00076 } 00077 00078 this->pub_.publish(marker_array); 00079 sleep(0.1); // it takes some time for Rviz to compute and show the marker! 00080 } 00081 00082 00083 void ShapesManager::clear() 00084 { 00085 this->shapes_.clear(); 00086 } 00087 00088 00089 uint32_t ShapesManager::count() const 00090 { 00091 return this->shapes_.size(); 00092 } 00093 00094 00095 uint32_t ShapesManager::count(const std::string& id) const 00096 { 00097 return this->shapes_.count(id); 00098 }