mesh_instance.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/mesh_instance.h>
00031 #include <rve_render_client/material.h>
00032 #include <rve_render_client/scene.h>
00033 #include <rve_render_client/client_context.h>
00034 
00035 #include <rve_interfaces/MeshInstance.h>
00036 #include <rve_msgs/make_vector3.h>
00037 #include <rve_msgs/Mesh.h>
00038 
00039 #include <boost/bind.hpp>
00040 
00041 namespace rve_render_client
00042 {
00043 
00044 MeshInstancePtr createMeshInstance(Scene* scene, const std::string& mesh_resource)
00045 {
00046   MeshPtr mesh = loadMesh(mesh_resource);
00047 
00048   return createMeshInstance(scene, loadMesh(mesh_resource));
00049 }
00050 
00051 MeshInstancePtr createMeshInstance(const std::string& mesh_resource)
00052 {
00053   MeshPtr mesh = loadMesh(mesh_resource);
00054 
00055   return createMeshInstance(loadMesh(mesh_resource));
00056 }
00057 
00058 MeshInstancePtr createMeshInstance(const MeshPtr& mesh)
00059 {
00060   MeshInstancePtr inst(new MeshInstance(mesh), destroySceneObject);
00061   return inst;
00062 }
00063 
00064 MeshInstancePtr createMeshInstance(Scene* scene, const MeshPtr& mesh)
00065 {
00066   MeshInstancePtr inst = createMeshInstance(mesh);
00067   scene->addObject(inst.get());
00068   return inst;
00069 }
00070 
00071 MeshInstance::MeshInstance(const MeshPtr& mesh)
00072  : mesh_(mesh)
00073 {
00074   orientation_.w = 1.0;
00075   scale_ = rve_msgs::makeVector3(1.0, 1.0, 1.0);
00076   proxy_index_ = addProxy("mesh_instance");
00077 }
00078 
00079 MeshInstance::~MeshInstance()
00080 {
00081 }
00082 
00083 rve_interfaces::MeshInstanceProxy* MeshInstance::getProxy(const ContextInfo& context)
00084 {
00085   return SceneObject::getProxy<rve_interfaces::MeshInstanceProxy>(context, proxy_index_);
00086 }
00087 
00088 void MeshInstance::doCreate(ContextInfo& context)
00089 {
00090   if (!context.context->hasObject(mesh_.get()))
00091   {
00092     context.context->addObject(mesh_.get());
00093   }
00094 
00095   rve_interfaces::MeshInstanceProxy* proxy = getProxy(context);
00096   proxy->create(getSceneID(context.scene), getID(), mesh_->getID());
00097 
00098   rve_common::UUID scene_id = getSceneID(context.scene);
00099   proxy->setPositionAsync(scene_id, getID(), position_);
00100   proxy->setOrientationAsync(scene_id, getID(), orientation_);
00101   proxy->setScaleAsync(scene_id, getID(), scale_);
00102 
00103   {
00104     M_Material::iterator it = materials_.begin();
00105     M_Material::iterator end = materials_.end();
00106     for (; it != end; ++it)
00107     {
00108       proxy->setMaterialForSubMesh(scene_id, getID(), it->first, it->second->getID());
00109     }
00110   }
00111 }
00112 
00113 void MeshInstance::doDestroy(ContextInfo& context)
00114 {
00115   rve_interfaces::MeshInstanceProxy* proxy = getProxy(context);
00116   proxy->destroyAsync(getSceneID(context.scene), getID());
00117 }
00118 
00119 void MeshInstance::getContextDependencies(V_UUID& deps)
00120 {
00121   M_Material::iterator it = materials_.begin();
00122   M_Material::iterator end = materials_.end();
00123   for (; it != end; ++it)
00124   {
00125     const MaterialPtr& mat = it->second;
00126     deps.push_back(mat->getID());
00127   }
00128 }
00129 
00130 #define MESH_INSTANCE_MULTIPLEX(func, ...) \
00131     multiplex<rve_interfaces::MeshInstanceProxy>(boost::bind(&rve_interfaces::MeshInstanceProxy::func, _1, _2, getID(), __VA_ARGS__), proxy_index_);
00132 
00133 void MeshInstance::setPosition(const rve_msgs::Vector3& pos)
00134 {
00135   position_ = pos;
00136   MESH_INSTANCE_MULTIPLEX(setPositionAsync, pos);
00137 }
00138 
00139 void MeshInstance::setOrientation(const rve_msgs::Quaternion& orient)
00140 {
00141   orientation_ = orient;
00142   MESH_INSTANCE_MULTIPLEX(setOrientationAsync, orient);
00143 }
00144 
00145 void MeshInstance::setScale(const rve_msgs::Vector3& scale)
00146 {
00147   scale_ = scale;
00148   MESH_INSTANCE_MULTIPLEX(setScaleAsync, scale);
00149 }
00150 
00151 void MeshInstance::setMaterial(const MaterialPtr& mat)
00152 {
00153   materials_.clear();
00154   for (size_t i = 0; i < mesh_->getMesh()->submeshes.size(); ++i)
00155   {
00156     materials_[i] = mat;
00157   }
00158 
00159   MESH_INSTANCE_MULTIPLEX(setMaterialAsync, mat->getID());
00160 }
00161 
00162 void MeshInstance::setMaterial(uint32_t submesh_index, const MaterialPtr& mat)
00163 {
00164   materials_[submesh_index] = mat;
00165   MESH_INSTANCE_MULTIPLEX(setMaterialForSubMeshAsync, submesh_index, mat->getID());
00166 }
00167 
00168 } // namespace rviz_render_client
00169 


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