texture.cpp
Go to the documentation of this file.
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 textures 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/texture.h>
00031 #include <rve_render_client/client_context.h>
00032 
00033 #include <rve_interfaces/Texture.h>
00034 
00035 #include <ros/assert.h>
00036 
00037 #include <map>
00038 
00039 namespace rve_render_client
00040 {
00041 
00042 typedef std::map<rve_common::UUID, TextureWPtr> M_Texture;
00043 M_Texture g_textures;
00044 
00045 typedef std::map<std::string, rve_common::UUID> M_ResourceToID;
00046 M_ResourceToID g_texture_resource_to_id;
00047 
00048 void destroyTexture(Texture* texture)
00049 {
00050   while (!texture->contexts_.empty())
00051   {
00052     ClientContext* context = texture->contexts_.begin()->first;
00053     context->removeObject(texture);
00054   }
00055 
00056   g_textures.erase(texture->getID());
00057 
00058   delete texture;
00059 }
00060 
00061 TexturePtr createTexture(uint32_t num_mips)
00062 {
00063   TexturePtr texture(new Texture(num_mips), destroyTexture);
00064   g_textures.insert(std::make_pair(texture->getID(), texture));
00065   return texture;
00066 }
00067 
00068 TexturePtr createTexture(ClientContext* context, uint32_t num_mips)
00069 {
00070   TexturePtr texture = createTexture(num_mips);
00071   context->addObject(texture.get());
00072   return texture;
00073 }
00074 
00075 TexturePtr loadTexture(const std::string& resource_path, uint32_t num_mips)
00076 {
00077   M_ResourceToID::iterator it = g_texture_resource_to_id.find(resource_path);
00078   if (it != g_texture_resource_to_id.end())
00079   {
00080     TextureWPtr texw = g_textures[it->second];
00081     TexturePtr tex = texw.lock();
00082     if (tex)
00083     {
00084       return tex;
00085     }
00086 
00087     g_texture_resource_to_id.erase(it);
00088   }
00089 
00090   TexturePtr tex = createTexture(num_mips);
00091   tex->loadResource(resource_path);
00092   g_texture_resource_to_id.insert(std::make_pair(resource_path, tex->getID()));
00093 
00094   return tex;
00095 }
00096 
00097 TexturePtr loadTexture(ClientContext* context, const std::string& resource_path, uint32_t num_mips)
00098 {
00099   TexturePtr tex = loadTexture(resource_path, num_mips);
00100   context->addObject(tex.get());
00101   return tex;
00102 }
00103 
00104 TexturePtr getTexture(const rve_common::UUID& id)
00105 {
00106   M_Texture::iterator it = g_textures.find(id);
00107   if (it == g_textures.end())
00108   {
00109     return TexturePtr();
00110   }
00111 
00112   return it->second.lock();
00113 }
00114 
00115 Texture::Texture(uint32_t num_mips)
00116 : id_(rve_common::UUID::generate())
00117 , num_mips_(num_mips)
00118 {
00119 }
00120 
00121 void Texture::create(ClientContext* context)
00122 {
00123   ContextInfo info;
00124   info.context = context;
00125   info.proxy_index = context->lookupInterface("texture");
00126   contexts_[context] = info;
00127 
00128   rve_interfaces::TextureProxy* proxy = context->getInterface<rve_interfaces::TextureProxy>(info.proxy_index);
00129   proxy->createAsync(getID(), num_mips_);
00130 }
00131 
00132 void Texture::destroy(ClientContext* context)
00133 {
00134   M_ContextInfo::iterator it = contexts_.find(context);
00135   ROS_ASSERT(it != contexts_.end());
00136 
00137   ContextInfo& info = it->second;
00138   rve_interfaces::TextureProxy* proxy = context->getInterface<rve_interfaces::TextureProxy>(info.proxy_index);
00139   proxy->destroyAsync(getID());
00140   contexts_.erase(it);
00141 }
00142 
00143 void Texture::setFromImage(const sensor_msgs::ImageConstPtr& image)
00144 {
00145   resource_.clear();
00146   image_ = image;
00147   send();
00148 }
00149 
00150 void Texture::loadResource(const std::string& resource_path)
00151 {
00152   resource_ = resource_path;
00153   image_.reset();
00154   send();
00155 }
00156 
00157 void Texture::send()
00158 {
00159   M_ContextInfo::iterator it = contexts_.begin();
00160   M_ContextInfo::iterator end = contexts_.end();
00161   for (; it != end; ++it)
00162   {
00163     ContextInfo& info = it->second;
00164     rve_interfaces::TextureProxy* proxy = info.context->getInterface<rve_interfaces::TextureProxy>(info.proxy_index);
00165 
00166     if (image_)
00167     {
00168       proxy->setImageAsync(getID(), *image_);
00169     }
00170     else if (!resource_.empty())
00171     {
00172       proxy->setFileAsync(getID(), resource_);
00173     }
00174   }
00175 }
00176 
00177 } // namespace rve_render_client
00178 


rve_render_client
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:32