scene.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/scene.h"
00031 #include "rve_render_client/single_scene_object.h"
00032 #include "rve_render_client/scene_object_collection.h"
00033 #include <rve_render_client/client_context.h>
00034 #include <rve_common/uuid.h>
00035 
00036 #include <rve_interfaces/Scene.h>
00037 
00038 #include <ros/assert.h>
00039 
00040 #include <boost/unordered_set.hpp>
00041 #include <boost/bind.hpp>
00042 
00043 #include <algorithm>
00044 
00045 using namespace rve_common;
00046 
00047 namespace rve_render_client
00048 {
00049 
00050 struct Scene::Impl
00051 {
00052   Impl(Scene* parent);
00053 
00054   void addObject(SceneObject* obj);
00055   void removeObject(SceneObject* obj);
00056 
00057   void create(ClientContext* context);
00058   void destroy(ClientContext* context);
00059 
00060   const rve_common::UUID& getID() { return id_; }
00061   void getDependencies(V_UUID& deps);
00062 
00063   rve_interfaces::SceneProxy* getProxy(ClientContext* context);
00064 
00065   Scene* parent_;
00066   rve_common::UUID id_;
00067 
00068   typedef boost::unordered_set<SceneObject*> S_SceneObject;
00069   S_SceneObject objects_;
00070 
00071   typedef boost::unordered_set<ClientContext*> S_ClientContext;
00072   S_ClientContext contexts_;
00073 };
00074 
00075 Scene::Impl::Impl(Scene* parent)
00076 : parent_(parent)
00077 , id_(rve_common::UUID::generate())
00078 {}
00079 
00080 void Scene::Impl::addObject(SceneObject* obj)
00081 {
00082   bool inserted = objects_.insert(obj).second;
00083   ROS_ASSERT(inserted);
00084 
00085   // for each context, call obj->create(this, context);
00086   std::for_each(contexts_.begin(), contexts_.end(), boost::bind(&SceneObject::create, obj, parent_, _1));
00087 }
00088 
00089 void Scene::Impl::removeObject(SceneObject* obj)
00090 {
00091   // for each context, call obj->destroy(this, context);
00092   std::for_each(contexts_.begin(), contexts_.end(), boost::bind(&SceneObject::destroy, obj, parent_, _1));
00093 
00094   objects_.erase(obj);
00095 }
00096 
00097 void Scene::Impl::create(ClientContext* context)
00098 {
00099   bool inserted = contexts_.insert(context).second;
00100   ROS_ASSERT(inserted);
00101 
00102   rve_interfaces::SceneProxy* proxy = getProxy(context);
00103   proxy->create(getID());
00104 
00105   // for each object, call obj->create(this, context);
00106   std::for_each(objects_.begin(), objects_.end(), boost::bind(&SceneObject::create, _1, parent_, context));
00107 }
00108 
00109 void Scene::Impl::destroy(ClientContext* context)
00110 {
00111   contexts_.erase(context);
00112 
00113   // for each object, call obj->destroy(this, context);
00114   std::for_each(objects_.begin(), objects_.end(), boost::bind(&SceneObject::destroy, _1, parent_, context));
00115 
00116   rve_interfaces::SceneProxy* proxy = getProxy(context);
00117   proxy->destroy(getID());
00118 }
00119 
00120 rve_interfaces::SceneProxy* Scene::Impl::getProxy(ClientContext* context)
00121 {
00122   // We don't need the scene proxy very often, so caching it is likely not a win and is (very) slightly more complex
00123   InterfaceHandle handle = context->lookupInterface("scene");
00124   return context->getInterface<rve_interfaces::SceneProxy>(handle);
00125 }
00126 
00127 void Scene::Impl::getDependencies(V_UUID& deps)
00128 {
00129   std::for_each(objects_.begin(), objects_.end(), boost::bind(&SceneObject::getContextDependencies, _1, deps));
00130 }
00131 
00135 
00136 Scene::Scene()
00137 : impl_(new Impl(this))
00138 {
00139 }
00140 
00141 Scene::~Scene()
00142 {
00143 }
00144 
00145 void Scene::addObject(SceneObject* obj)
00146 {
00147   impl_->addObject(obj);
00148 }
00149 
00150 void Scene::removeObject(SceneObject* obj)
00151 {
00152   impl_->removeObject(obj);
00153 }
00154 
00155 void Scene::create(ClientContext* context)
00156 {
00157   impl_->create(context);
00158 }
00159 
00160 void Scene::destroy(ClientContext* context)
00161 {
00162   impl_->destroy(context);
00163 }
00164 
00165 const rve_common::UUID& Scene::getID()
00166 {
00167   return impl_->getID();
00168 }
00169 
00170 void Scene::getDependencies(V_UUID& deps)
00171 {
00172   impl_->getDependencies(deps);
00173 }
00174 
00178 
00179 void destroyScene(Scene* scene)
00180 {
00181   while (!scene->impl_->contexts_.empty())
00182   {
00183     ClientContext* context = *scene->impl_->contexts_.begin();
00184     context->removeObject(scene);
00185   }
00186 
00187   delete scene;
00188 }
00189 
00190 ScenePtr createScene(const ClientContextPtr& context)
00191 {
00192   ScenePtr scene(new Scene, destroyScene);
00193   context->addObject(scene.get());
00194   return scene;
00195 }
00196 
00197 } // namespace rve_render_client


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