$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00037 #include "collision_space_ccd/environment_objects.h" 00038 #include <geometric_shapes/shape_operations.h> 00039 00040 namespace collision_space_ccd 00041 { 00042 00043 std::vector<std::string> EnvironmentObjects::getNamespaces(void) const 00044 { 00045 std::vector<std::string> ns; 00046 for (std::map<std::string, NamespaceObjects>::const_iterator it = objects_.begin() ; it != objects_.end() ; ++it) 00047 ns.push_back(it->first); 00048 return ns; 00049 } 00050 00051 const EnvironmentObjects::NamespaceObjects& EnvironmentObjects::getObjects(const std::string &ns) const 00052 { 00053 std::map<std::string, NamespaceObjects>::const_iterator it = objects_.find(ns); 00054 if (it == objects_.end()) 00055 return empty_; 00056 else 00057 return it->second; 00058 } 00059 00060 EnvironmentObjects::NamespaceObjects& EnvironmentObjects::getObjects(const std::string &ns) 00061 { 00062 return objects_[ns]; 00063 } 00064 00065 void EnvironmentObjects::addObject(const std::string &ns, shapes::StaticShape *shape) 00066 { 00067 objects_[ns].static_shape.push_back(shape); 00068 } 00069 00070 void EnvironmentObjects::addObject(const std::string &ns, shapes::Shape *shape, const btTransform &pose) 00071 { 00072 objects_[ns].shape.push_back(shape); 00073 objects_[ns].shape_pose.push_back(pose); 00074 } 00075 00076 bool EnvironmentObjects::removeObject(const std::string &ns, const shapes::Shape *shape) 00077 { 00078 std::map<std::string, NamespaceObjects>::iterator it = objects_.find(ns); 00079 if (it != objects_.end()) 00080 { 00081 unsigned int n = it->second.shape.size(); 00082 for (unsigned int i = 0 ; i < n ; ++i) 00083 if (it->second.shape[i] == shape) 00084 { 00085 it->second.shape.erase(it->second.shape.begin() + i); 00086 it->second.shape_pose.erase(it->second.shape_pose.begin() + i); 00087 return true; 00088 } 00089 } 00090 return false; 00091 } 00092 00093 bool EnvironmentObjects::removeObject(const std::string &ns, const shapes::StaticShape *shape) 00094 { 00095 std::map<std::string, NamespaceObjects>::iterator it = objects_.find(ns); 00096 if (it != objects_.end()) 00097 { 00098 unsigned int n = it->second.static_shape.size(); 00099 for (unsigned int i = 0 ; i < n ; ++i) 00100 if (it->second.static_shape[i] == shape) 00101 { 00102 it->second.static_shape.erase(it->second.static_shape.begin() + i); 00103 return true; 00104 } 00105 } 00106 return false; 00107 } 00108 00109 void EnvironmentObjects::clearObjects(const std::string &ns) 00110 { 00111 std::map<std::string, NamespaceObjects>::iterator it = objects_.find(ns); 00112 if (it != objects_.end()) 00113 { 00114 unsigned int n = it->second.static_shape.size(); 00115 for (unsigned int i = 0 ; i < n ; ++i) 00116 delete it->second.static_shape[i]; 00117 n = it->second.shape.size(); 00118 for (unsigned int i = 0 ; i < n ; ++i) 00119 delete it->second.shape[i]; 00120 objects_.erase(it); 00121 } 00122 } 00123 00124 void EnvironmentObjects::clearObjects(void) 00125 { 00126 std::vector<std::string> ns = getNamespaces(); 00127 for (unsigned int i = 0 ; i < ns.size() ; ++i) 00128 clearObjects(ns[i]); 00129 } 00130 00131 void EnvironmentObjects::addObjectNamespace(const std::string ns) 00132 { 00133 if(objects_.find(ns) == objects_.end()) 00134 { 00135 objects_[ns] = NamespaceObjects(); 00136 } 00137 //doesn't do anything if the object is already in objects_ 00138 } 00139 00140 EnvironmentObjects* EnvironmentObjects::clone(void) const 00141 { 00142 EnvironmentObjects *c = new EnvironmentObjects(); 00143 for (std::map<std::string, NamespaceObjects>::const_iterator it = objects_.begin() ; it != objects_.end() ; ++it) 00144 { 00145 NamespaceObjects &ns = c->objects_[it->first]; 00146 unsigned int n = it->second.static_shape.size(); 00147 for (unsigned int i = 0 ; i < n ; ++i) 00148 ns.static_shape.push_back(shapes::cloneShape(it->second.static_shape[i])); 00149 n = it->second.shape.size(); 00150 for (unsigned int i = 0 ; i < n ; ++i) 00151 ns.shape.push_back(shapes::cloneShape(it->second.shape[i])); 00152 ns.shape_pose = it->second.shape_pose; 00153 } 00154 return c; 00155 } 00156 00157 }