material.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 materials 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/material.h>
00031 #include <rve_render_client/client_context.h>
00032 
00033 #include <rve_interfaces/Material.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, MaterialWPtr> M_Material;
00043 M_Material g_materials;
00044 
00045 MaterialPtr getMaterial(const rve_common::UUID& id)
00046 {
00047   M_Material::iterator it = g_materials.find(id);
00048   if (it == g_materials.end())
00049   {
00050     return MaterialPtr();
00051   }
00052 
00053   return it->second.lock();
00054 }
00055 
00056 void destroyMaterial(Material* mat)
00057 {
00058   while (!mat->contexts_.empty())
00059   {
00060     ClientContext* context = mat->contexts_.begin()->first;
00061     context->removeObject(mat);
00062   }
00063 
00064   g_materials.erase(mat->getID());
00065 
00066   delete mat;
00067 }
00068 
00069 MaterialPtr createMaterial()
00070 {
00071   MaterialPtr mat(new Material, destroyMaterial);
00072   g_materials.insert(std::make_pair(mat->getID(), mat));
00073   return mat;
00074 }
00075 
00076 MaterialPtr createMaterial(ClientContext* context)
00077 {
00078   MaterialPtr mat = createMaterial();
00079   context->addObject(mat.get());
00080   return mat;
00081 }
00082 
00083 Material::Material()
00084 : id_(rve_common::UUID::generate())
00085 {
00086   material_.opacity = 1.0;
00087   material_.color.r = 1.0;
00088   material_.color.g = 1.0;
00089   material_.color.b = 1.0;
00090 }
00091 
00092 void Material::create(ClientContext* context)
00093 {
00094   ContextInfo info;
00095   info.context = context;
00096   info.proxy_index = context->lookupInterface("material");
00097   contexts_[context] = info;
00098 
00099   rve_interfaces::MaterialProxy* proxy = context->getInterface<rve_interfaces::MaterialProxy>(info.proxy_index);
00100   proxy->createAsync(getID());
00101   proxy->setMaterialAsync(getID(), material_);
00102 }
00103 
00104 void Material::destroy(ClientContext* context)
00105 {
00106   M_ContextInfo::iterator it = contexts_.find(context);
00107   ROS_ASSERT(it != contexts_.end());
00108 
00109   ContextInfo& info = it->second;
00110   rve_interfaces::MaterialProxy* proxy = context->getInterface<rve_interfaces::MaterialProxy>(info.proxy_index);
00111   proxy->destroyAsync(getID());
00112   contexts_.erase(it);
00113 }
00114 
00115 void Material::send()
00116 {
00117   M_ContextInfo::iterator it = contexts_.begin();
00118   M_ContextInfo::iterator end = contexts_.end();
00119   for (; it != end; ++it)
00120   {
00121     ContextInfo& info = it->second;
00122     rve_interfaces::MaterialProxy* proxy = info.context->getInterface<rve_interfaces::MaterialProxy>(info.proxy_index);
00123     proxy->setMaterialAsync(getID(), material_);
00124   }
00125 }
00126 
00127 void Material::disableDiffuse()
00128 {
00129   material_.has_color = false;
00130   send();
00131 }
00132 
00133 void Material::setDiffuse(Color c)
00134 {
00135   material_.has_color = true;
00136   material_.color.r = c.r;
00137   material_.color.g = c.g;
00138   material_.color.b = c.b;
00139   material_.opacity = c.a;
00140   send();
00141 }
00142 
00143 Color Material::getDiffuse()
00144 {
00145   return Color(material_.color.r, material_.color.g, material_.color.b);
00146 }
00147 
00148 void Material::setOpacity(float opacity)
00149 {
00150   material_.opacity = opacity;
00151   send();
00152 }
00153 
00154 float Material::getOpacity()
00155 {
00156   return material_.opacity;
00157 }
00158 
00159 void Material::setMaterial(const rve_msgs::Material& mat)
00160 {
00161   material_ = mat;
00162   send();
00163 }
00164 
00165 void Material::setShadingEnabled(bool enable)
00166 {
00167   material_.disable_shading = !enable;
00168 }
00169 
00170 bool Material::getShadingEnabled()
00171 {
00172   return !material_.disable_shading;
00173 }
00174 
00175 } // namespace rve_render_client


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