00001 /* 00002 * Copyright (c) 2010, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other meshs provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include <rve_render_client/mesh.h> 00031 #include <rve_render_client/client_context.h> 00032 #include <rve_mesh_loader/loader.h> 00033 00034 #include <resource_retriever/retriever.h> 00035 00036 #include <rve_interfaces/Mesh.h> 00037 00038 #include <ros/assert.h> 00039 00040 #include <map> 00041 00042 namespace rve_render_client 00043 { 00044 00045 typedef std::map<rve_common::UUID, MeshWPtr> M_Mesh; 00046 M_Mesh g_meshes; 00047 00048 typedef std::map<std::string, rve_common::UUID> M_ResourceToID; 00049 M_ResourceToID g_mesh_resource_to_id; 00050 00051 void destroyMesh(Mesh* mesh) 00052 { 00053 while (!mesh->contexts_.empty()) 00054 { 00055 ClientContext* context = mesh->contexts_.begin()->first; 00056 context->removeObject(mesh); 00057 } 00058 00059 g_meshes.erase(mesh->getID()); 00060 00061 delete mesh; 00062 } 00063 00064 MeshPtr createMesh(const rve_msgs::MeshConstPtr& msg) 00065 { 00066 MeshPtr mesh(new Mesh(msg), destroyMesh); 00067 g_meshes.insert(std::make_pair(mesh->getID(), mesh)); 00068 return mesh; 00069 } 00070 00071 MeshPtr loadMesh(const std::string& resource_path) 00072 { 00073 M_ResourceToID::iterator it = g_mesh_resource_to_id.find(resource_path); 00074 if (it != g_mesh_resource_to_id.end()) 00075 { 00076 MeshPtr mesh = getMesh(it->second); 00077 if (mesh) 00078 { 00079 return mesh; 00080 } 00081 else 00082 { 00083 g_mesh_resource_to_id.erase(it); 00084 } 00085 } 00086 00087 rve_msgs::MeshPtr mesh_msg(new rve_msgs::Mesh); 00088 rve_mesh_loader::load(resource_path, *mesh_msg); 00089 return createMesh(mesh_msg); 00090 } 00091 00092 MeshPtr getMesh(const rve_common::UUID& id) 00093 { 00094 M_Mesh::iterator it = g_meshes.find(id); 00095 if (it == g_meshes.end()) 00096 { 00097 return MeshPtr(); 00098 } 00099 00100 return it->second.lock(); 00101 } 00102 00103 MeshPtr createMesh(ClientContext* context, const rve_msgs::MeshConstPtr& msg) 00104 { 00105 MeshPtr mesh = createMesh(msg); 00106 context->addObject(mesh.get()); 00107 return mesh; 00108 } 00109 00110 Mesh::Mesh(const rve_msgs::MeshConstPtr& mesh) 00111 : id_(rve_common::UUID::generate()) 00112 , mesh_(mesh) 00113 { 00114 } 00115 00116 void Mesh::create(ClientContext* context) 00117 { 00118 ContextInfo info; 00119 info.context = context; 00120 info.proxy_index = context->lookupInterface("mesh"); 00121 contexts_[context] = info; 00122 00123 rve_interfaces::MeshProxy* proxy = context->getInterface<rve_interfaces::MeshProxy>(info.proxy_index); 00124 proxy->createAsync(getID(), *mesh_); 00125 } 00126 00127 void Mesh::destroy(ClientContext* context) 00128 { 00129 M_ContextInfo::iterator it = contexts_.find(context); 00130 ROS_ASSERT(it != contexts_.end()); 00131 00132 ContextInfo& info = it->second; 00133 rve_interfaces::MeshProxy* proxy = context->getInterface<rve_interfaces::MeshProxy>(info.proxy_index); 00134 proxy->destroyAsync(getID()); 00135 contexts_.erase(it); 00136 } 00137 00138 void Mesh::getDependencies(V_UUID& deps) 00139 { 00140 // none of the materials used by the mesh are actually client-side materials... if that 00141 // changes (and it probably should), they need to get added as dependencies 00142 } 00143 00144 } // namespace rve_render_client 00145