server.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_server/server.h>
00031 #include <rve_render_server/billboard_text.h>
00032 #include <rve_render_server/convert.h>
00033 #include <rve_render_server/renderer.h>
00034 #include <rve_render_server/render_window.h>
00035 #include <rve_render_server/screen_rect.h>
00036 #include <rve_render_server/scene.h>
00037 #include <rve_render_server/camera.h>
00038 #include <rve_render_server/material.h>
00039 #include <rve_render_server/mesh_instance.h>
00040 #include <rve_render_server/mesh_loader.h>
00041 #include <rve_render_server/mesh.h>
00042 #include <rve_render_server/texture.h>
00043 #include <rve_render_server/texture_resource.h>
00044 #include <rve_render_server/batching/points_manager.h>
00045 #include <rve_render_server/batching/lines_manager.h>
00046 #include <rve_render_server/batching/triangles_manager.h>
00047 #include <rve_common/uuid.h>
00048 
00049 #include <ros/ros.h>
00050 #include <ros/callback_queue.h>
00051 
00052 #include <rve_interfaces/BillboardText.h>
00053 #include <rve_interfaces/Camera.h>
00054 #include <rve_interfaces/RenderTarget.h>
00055 #include <rve_interfaces/ScreenRect.h>
00056 #include <rve_interfaces/Scene.h>
00057 #include <rve_interfaces/Material.h>
00058 #include <rve_interfaces/Texture.h>
00059 #include <rve_interfaces/MeshInstance.h>
00060 #include <rve_interfaces/Mesh.h>
00061 #include <rve_interfaces/Sphere.h>
00062 #include <rve_interfaces/Box.h>
00063 #include <rve_interfaces/Cylinder.h>
00064 #include <rve_interfaces/Cone.h>
00065 #include <rve_interfaces/Points.h>
00066 #include <rve_interfaces/Lines.h>
00067 #include <rve_interfaces/Triangles.h>
00068 
00069 #include <rve_interfaces/RenderTarget_pickRequest.h>
00070 #include <rve_interfaces/RenderTarget_pickResponse.h>
00071 
00072 #include <rve_rpc/call_handle.h>
00073 #include <rve_rpc/server.h>
00074 
00075 #include <OGRE/OgreSceneNode.h>
00076 
00077 using namespace rve_common;
00078 
00079 namespace rve_render_server
00080 {
00081 
00082 rve_render_server::Scene* lookupScene(Renderer* rend, const rve_common::UUID& id)
00083 {
00084   rve_render_server::Scene* scene = rend->getScene(id);
00085   if (!scene)
00086   {
00087     throw std::runtime_error("Scene [" + UUID(id).toString() + "] does not exist");
00088   }
00089 
00090   return scene;
00091 }
00092 
00093 rve_render_server::Camera* lookupCamera(Renderer* rend, const rve_common::UUID& scene_id, const rve_common::UUID& id)
00094 {
00095   rve_render_server::Scene* scene = lookupScene(rend, scene_id);
00096   rve_render_server::Camera* cam = scene->getCamera(id);
00097   if (!cam)
00098   {
00099     std::stringstream ss;
00100     ss << "Invalid camera [" << id << "]";
00101     throw std::runtime_error(ss.str());
00102   }
00103 
00104   return cam;
00105 }
00106 
00107 class CameraServer : public rve_interfaces::CameraServer
00108 {
00109 public:
00110   CameraServer(Renderer* rend, rve_rpc::Server& server)
00111   : rve_interfaces::CameraServer(server)
00112   , renderer_(rend)
00113   {
00114   }
00115 
00116   rve_render_server::Camera* lookupCamera(const rve_common::UUID& scene_id, const rve_common::UUID& id)
00117   {
00118     return rve_render_server::lookupCamera(renderer_, scene_id, id);
00119   }
00120 
00121   virtual void create(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& camera_id)
00122   {
00123     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00124     scene->createCamera(camera_id);
00125   }
00126 
00127   virtual void destroy(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& camera_id)
00128   {
00129     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00130     scene->destroyCamera(camera_id);
00131   }
00132 
00133   virtual void setPosition(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id, const rve_msgs::Vector3& pos)
00134   {
00135     lookupCamera(scene_id, id)->setPosition(fromRobot(pos));
00136   }
00137 
00138   virtual void setOrientation(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id, const rve_msgs::Quaternion& orient)
00139   {
00140     lookupCamera(scene_id, id)->setOrientation(fromRobotRelative(orient));
00141   }
00142 
00143   virtual void setProjectionMatrix(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id, const rve_msgs::Matrix4& projection_matrix)
00144   {
00145     lookupCamera(scene_id, id)->setProjectionMatrix(convert(projection_matrix));
00146   }
00147 
00148 
00149 private:
00150   Renderer* renderer_;
00151 };
00152 
00153 class RenderTargetServer : public rve_interfaces::RenderTargetServer
00154 {
00155 public:
00156   RenderTargetServer(Renderer* rend, rve_rpc::Server& server)
00157   : rve_interfaces::RenderTargetServer(server)
00158   , renderer_(rend)
00159   {
00160   }
00161 
00162   virtual void resize(const rve_msgs::UUID& id, uint32_t width, uint32_t height)
00163   {
00164     rve_render_server::RenderTarget* wnd = renderer_->getRenderTarget(id);
00165     wnd->resize(width, height);
00166   }
00167 
00168   virtual void attachCamera(const rve_msgs::UUID& id, const rve_msgs::UUID& camera_scene_id, const rve_msgs::UUID& camera_id)
00169   {
00170     rve_render_server::Camera* cam = lookupCamera(renderer_, camera_scene_id, camera_id);
00171     rve_render_server::RenderTarget* wnd = renderer_->getRenderTarget(id);
00172     wnd->attachCamera(cam);
00173     cam->addRenderTarget(wnd);
00174   }
00175 
00176   virtual void detachCamera(const rve_msgs::UUID& id)
00177   {
00178     rve_render_server::RenderTarget* wnd = renderer_->getRenderTarget(id);
00179     wnd->getCamera()->removeRenderTarget(wnd);
00180     wnd->attachCamera(0);
00181   }
00182 
00183   virtual void pick(pickHandle& handle)
00184   {
00185     const pickRequestConstPtr& req = handle.getRequest();
00186     uint32_t x0 = req->x0;
00187     uint32_t y0 = req->y0;
00188     uint32_t x1 = req->x1;
00189     uint32_t y1 = req->y1;
00190     const rve_common::UUID& id = req->id;
00191     rve_render_server::RenderTarget* wnd = renderer_->getRenderWindow(id);
00192     wnd->pick(x0, y0, x1, y1, boost::bind(&RenderTargetServer::pickCallback, this, _1, handle));
00193   }
00194 
00195   void pickCallback(V_PickRenderValues& vals, pickHandle& handle)
00196   {
00197     std::sort(vals.begin(), vals.end());
00198     V_Picked picked;
00199     renderer_->translatePickIDs(vals, picked);
00200 
00201     pickResponsePtr res(new pickResponse);
00202     V_Picked::iterator it = picked.begin();
00203     V_Picked::iterator end = picked.end();
00204     for (; it != end; ++it)
00205     {
00206       rve_msgs::PickResult pr;
00207       pr.id = it->object_id;
00208       pr.extra = it->object_extra;
00209       res->results.results.push_back(pr);
00210     }
00211 
00212     handle.respond(res);
00213   }
00214 
00215   virtual void requestRender(const rve_msgs::UUID& id)
00216   {
00217     rve_render_server::RenderTarget* target = renderer_->getRenderTarget(id);
00218     target->requestRender();
00219   }
00220 
00221   virtual void createWindow(const rve_msgs::UUID& id, const std::string& parent_window, uint32_t width, uint32_t height)
00222   {
00223     renderer_->createRenderWindow(id, parent_window, width, height);
00224   }
00225 
00226   virtual void createTexture(const rve_msgs::UUID& id, uint32_t width, uint32_t height)
00227   {
00228     renderer_->createRenderTexture(id, width, height);
00229   }
00230 
00231   virtual void destroy(const rve_msgs::UUID& id)
00232   {
00233     renderer_->destroyRenderTarget(id);
00234   }
00235 
00236 private:
00237   Renderer* renderer_;
00238 };
00239 
00240 class SceneServer : public rve_interfaces::SceneServer
00241 {
00242 public:
00243   SceneServer(Renderer* rend, rve_rpc::Server& server)
00244   : rve_interfaces::SceneServer(server)
00245   , renderer_(rend)
00246   {
00247   }
00248 
00249   virtual void create(const rve_msgs::UUID& id)
00250   {
00251     rve_render_server::Scene* scene = renderer_->createScene(id);
00252     if (!scene)
00253     {
00254       throw std::runtime_error("Could not create scene [" + UUID(id).toString() + "]");
00255     }
00256   }
00257 
00258   virtual void destroy(const rve_msgs::UUID& id)
00259   {
00260     renderer_->destroyScene(id);
00261   }
00262 
00263 private:
00264   Renderer* renderer_;
00265 };
00266 
00267 class PointsServer : public rve_interfaces::PointsServer
00268 {
00269 public:
00270   PointsServer(Renderer* rend, rve_rpc::Server& server)
00271   : rve_interfaces::PointsServer(server)
00272   , renderer_(rend)
00273   {
00274   }
00275 
00276   virtual void create(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& points_id, const rve_msgs::Points& points)
00277   {
00278     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00279     scene->getPointsManager()->add(points_id, points);
00280   }
00281 
00282   virtual void destroy(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& points_id)
00283   {
00284     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00285     scene->getPointsManager()->remove(points_id);
00286   }
00287 
00288 private:
00289   Renderer* renderer_;
00290 };
00291 
00292 class LinesServer : public rve_interfaces::LinesServer
00293 {
00294 public:
00295   LinesServer(Renderer* rend, rve_rpc::Server& server)
00296   : rve_interfaces::LinesServer(server)
00297   , renderer_(rend)
00298   {
00299   }
00300 
00301   virtual void create(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& lines_id, const rve_msgs::Lines& lines)
00302   {
00303     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00304     scene->getLinesManager()->add(lines_id, lines);
00305   }
00306 
00307   virtual void destroyLines(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& lines_id)
00308   {
00309     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00310 
00311     scene->getLinesManager()->remove(lines_id);
00312   }
00313 
00314 private:
00315   Renderer* renderer_;
00316 };
00317 
00318 class TrianglesServer : public rve_interfaces::TrianglesServer
00319 {
00320 public:
00321   TrianglesServer(Renderer* rend, rve_rpc::Server& server)
00322   : rve_interfaces::TrianglesServer(server)
00323   , renderer_(rend)
00324   {
00325   }
00326 
00327   virtual void create(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& tris_id, const rve_msgs::Triangles& tris)
00328   {
00329     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00330     scene->getTrianglesManager()->add(tris_id, tris);
00331   }
00332 
00333   virtual void destroy(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& triangles_id)
00334   {
00335     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00336 
00337     scene->getTrianglesManager()->remove(triangles_id);
00338   }
00339 
00340 private:
00341   Renderer* renderer_;
00342 };
00343 
00344 class MaterialServer : public rve_interfaces::MaterialServer
00345 {
00346 public:
00347   MaterialServer(Renderer* rend, rve_rpc::Server& server)
00348   : rve_interfaces::MaterialServer(server)
00349   , renderer_(rend)
00350   {
00351   }
00352 
00353   virtual void create(const rve_msgs::UUID& id)
00354   {
00355     rve_render_server::MaterialPtr mat(new rve_render_server::Material(id));
00356     renderer_->addMaterial(id, mat);
00357   }
00358 
00359   virtual void destroy(const rve_msgs::UUID& id)
00360   {
00361     renderer_->removeMaterial(id);
00362   }
00363 
00364   virtual void setMaterial(const rve_msgs::UUID& id, const rve_msgs::Material& in_mat)
00365   {
00366     MaterialPtr mat = renderer_->getMaterial(id);
00367     if (!mat)
00368     {
00369       std::stringstream ss;
00370       ss << "Material [" << id << "] does not exist!";
00371       throw std::runtime_error(ss.str());
00372     }
00373 
00374     mat->setMaterial(in_mat);
00375   }
00376 
00377 private:
00378   Renderer* renderer_;
00379 };
00380 
00381 class TextureServer : public rve_interfaces::TextureServer
00382 {
00383 public:
00384   TextureServer(Renderer* rend, rve_rpc::Server& server)
00385   : rve_interfaces::TextureServer(server)
00386   , renderer_(rend)
00387   {
00388   }
00389 
00390   virtual void create(const rve_msgs::UUID& id, uint32_t num_mip_maps)
00391   {
00392     rve_render_server::TexturePtr tex(new rve_render_server::Texture(id, num_mip_maps));
00393     renderer_->addTexture(id, tex);
00394   }
00395 
00396   virtual void destroy(const rve_msgs::UUID& id)
00397   {
00398     renderer_->removeTexture(id);
00399   }
00400 
00401   virtual void setImage(const rve_msgs::UUID& id, const sensor_msgs::Image& image)
00402   {
00403     TexturePtr tex = renderer_->getTexture(id);
00404     tex->setImage(image);
00405   }
00406 
00407 private:
00408   Renderer* renderer_;
00409 };
00410 
00411 class MeshServer : public rve_interfaces::MeshServer
00412 {
00413 public:
00414   MeshServer(Renderer* rend, rve_rpc::Server& server)
00415   : rve_interfaces::MeshServer(server)
00416   , renderer_(rend)
00417   {
00418   }
00419 
00420   virtual void create(const rve_msgs::UUID& id, const rve_msgs::Mesh& mesh)
00421   {
00422     convertMesh(id, mesh);
00423   }
00424 
00425   virtual void destroy(const rve_msgs::UUID& id)
00426   {
00427     renderer_->removeMesh(id);
00428   }
00429 
00430 private:
00431   Renderer* renderer_;
00432 };
00433 
00434 template<typename S>
00435 class SimpleMeshObjectServer : public S
00436 {
00437 public:
00438   SimpleMeshObjectServer(Renderer* rend, rve_rpc::Server& server, const rve_common::UUID& mesh_id)
00439   : S(server)
00440   , renderer_(rend)
00441   , mesh_id_(mesh_id)
00442   {
00443   }
00444 
00445   virtual void create(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id)
00446   {
00447     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00448     scene->createMeshInstance(inst_id, mesh_id_);
00449   }
00450 
00451   virtual void destroy(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id)
00452   {
00453     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00454     scene->destroyMeshInstance(inst_id);
00455   }
00456 
00457   virtual void setMaterial(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, const rve_msgs::UUID& material_id)
00458   {
00459     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00460     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00461     rve_render_server::MaterialPtr mat = renderer_->getMaterial(material_id);
00462     inst->setMaterial(mat);
00463   }
00464 
00465   virtual void setPosition(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, const rve_msgs::Vector3& pos)
00466   {
00467     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00468     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00469     Ogre::SceneNode* node = inst->getSceneNode();
00470     node->setPosition(fromRobot(pos));
00471   }
00472 
00473   virtual void setOrientation(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, const rve_msgs::Quaternion& orient)
00474   {
00475     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00476     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00477     Ogre::SceneNode* node = inst->getSceneNode();
00478     node->setOrientation(fromRobot(orient));
00479   }
00480 
00481   virtual void setScale(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, const rve_msgs::Vector3& scale)
00482   {
00483     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00484     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00485     Ogre::SceneNode* node = inst->getSceneNode();
00486     node->setScale(convert(scale));
00487   }
00488 
00489 protected:
00490   Renderer* renderer_;
00491 
00492 private:
00493   rve_common::UUID mesh_id_;
00494 };
00495 
00496 class MeshInstanceServer : public SimpleMeshObjectServer<rve_interfaces::MeshInstanceServer>
00497 {
00498 public:
00499   MeshInstanceServer(Renderer* rend, rve_rpc::Server& server)
00500   : SimpleMeshObjectServer<rve_interfaces::MeshInstanceServer>(rend, server, rve_common::UUID::Null)
00501   {
00502   }
00503 
00504   virtual void create(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, const rve_msgs::UUID& mesh_id)
00505   {
00506     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00507     scene->createMeshInstance(inst_id, mesh_id);
00508   }
00509 
00510   virtual void destroy(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id)
00511   {
00512     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00513     scene->destroyMeshInstance(inst_id);
00514   }
00515 
00516   virtual void setMaterialForSubMesh(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, uint32_t submesh_index, const rve_msgs::UUID& material_id)
00517   {
00518     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00519 
00520     MaterialPtr material = renderer_->getMaterial(material_id);
00521     if (!material)
00522     {
00523       throw std::runtime_error("Material [" + UUID(material_id).toString() + "] does not exist");
00524     }
00525 
00526     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00527     if (!inst)
00528     {
00529       throw std::runtime_error("MeshInstance [" + UUID(material_id).toString() + "] does not exist");
00530     }
00531 
00532     inst->setMaterial(submesh_index, material);
00533   }
00534 };
00535 
00536 class SphereServer : public SimpleMeshObjectServer<rve_interfaces::SphereServer>
00537 {
00538 public:
00539   SphereServer(Renderer* rend, rve_rpc::Server& server)
00540   : SimpleMeshObjectServer<rve_interfaces::SphereServer>(rend, server, loadMesh("package://rve_render_server/media/models/sphere.dae")->getID())
00541   {
00542   }
00543 
00544   virtual void setRadius(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, float radius)
00545   {
00546     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00547     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00548     Ogre::SceneNode* node = inst->getSceneNode();
00549     node->setScale(Ogre::Vector3(radius, radius, radius));
00550   }
00551 };
00552 
00553 class BoxServer : public SimpleMeshObjectServer<rve_interfaces::BoxServer>
00554 {
00555 public:
00556   BoxServer(Renderer* rend, rve_rpc::Server& server)
00557   : SimpleMeshObjectServer<rve_interfaces::BoxServer>(rend, server, loadMesh("package://rve_render_server/media/models/cube.dae")->getID())
00558   {
00559   }
00560 };
00561 
00562 class CylinderServer : public SimpleMeshObjectServer<rve_interfaces::CylinderServer>
00563 {
00564 public:
00565   CylinderServer(Renderer* rend, rve_rpc::Server& server)
00566   : SimpleMeshObjectServer<rve_interfaces::CylinderServer>(rend, server, loadMesh("package://rve_render_server/media/models/cylinder.dae")->getID())
00567   {
00568   }
00569 
00570   virtual void setRadius(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, float radius)
00571   {
00572     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00573     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00574     Ogre::SceneNode* node = inst->getSceneNode();
00575     Ogre::Vector3 scale = node->getScale();
00576     scale.x = scale.y = radius;
00577     node->setScale(scale);
00578   }
00579 
00580   virtual void setHeight(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, float height)
00581   {
00582     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00583     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00584     Ogre::SceneNode* node = inst->getSceneNode();
00585     Ogre::Vector3 scale = node->getScale();
00586     scale.z = height;
00587     node->setScale(scale);
00588   }
00589 };
00590 
00591 class ConeServer : public SimpleMeshObjectServer<rve_interfaces::ConeServer>
00592 {
00593 public:
00594   ConeServer(Renderer* rend, rve_rpc::Server& server)
00595   : SimpleMeshObjectServer<rve_interfaces::ConeServer>(rend, server, loadMesh("package://rve_render_server/media/models/cone.dae")->getID())
00596   {
00597   }
00598 
00599   virtual void setRadius(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, float radius)
00600   {
00601     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00602     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00603     Ogre::SceneNode* node = inst->getSceneNode();
00604     Ogre::Vector3 scale = node->getScale();
00605     scale.x = scale.y = radius;
00606     node->setScale(scale);
00607   }
00608 
00609   virtual void setHeight(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& inst_id, float height)
00610   {
00611     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00612     rve_render_server::MeshInstance* inst = scene->getMeshInstance(inst_id);
00613     Ogre::SceneNode* node = inst->getSceneNode();
00614     Ogre::Vector3 scale = node->getScale();
00615     scale.z = height;
00616     node->setScale(scale);
00617   }
00618 };
00619 
00620 class ScreenRectServer : public rve_interfaces::ScreenRectServer
00621 {
00622 public:
00623   ScreenRectServer(Renderer* rend, rve_rpc::Server& server)
00624   : rve_interfaces::ScreenRectServer(server)
00625   , renderer_(rend)
00626   {
00627   }
00628 
00629   virtual void create(const rve_msgs::UUID& render_target_id, const rve_msgs::UUID& id, uint32_t zorder, float x0, float y0, float x1, float y1)
00630   {
00631     RenderTarget* target = renderer_->getRenderTarget(render_target_id);
00632     target->createScreenRect(id, zorder, x0, y0, x1, y1);
00633   }
00634 
00635   virtual void destroy(const rve_msgs::UUID& render_target_id, const rve_msgs::UUID& id)
00636   {
00637     RenderTarget* target = renderer_->getRenderTarget(render_target_id);
00638     target->destroyScreenRect(id);
00639   }
00640 
00641   virtual void setCorners(const rve_msgs::UUID& render_target_id, const rve_msgs::UUID& id, float x0, float y0, float x1, float y1)
00642   {
00643     RenderTarget* target = renderer_->getRenderTarget(render_target_id);
00644     rve_render_server::ScreenRect* rect = target->getScreenRect(id);
00645     rect->setCorners(x0, y0, x1, y1);
00646   }
00647 
00648   virtual void setZOrder(const rve_msgs::UUID& render_target_id, const rve_msgs::UUID& id, uint32_t zorder)
00649   {
00650     RenderTarget* target = renderer_->getRenderTarget(render_target_id);
00651     rve_render_server::ScreenRect* rect = target->getScreenRect(id);
00652     rect->setZOrder(zorder);
00653   }
00654 
00655   virtual void setColor(const rve_msgs::UUID& render_target_id, const rve_msgs::UUID& id, const rve_msgs::ColorRGB& color)
00656   {
00657     RenderTarget* target = renderer_->getRenderTarget(render_target_id);
00658     rve_render_server::ScreenRect* rect = target->getScreenRect(id);
00659     Ogre::ColourValue c(color.r, color.g, color.b);
00660     rect->setColor(c);
00661   }
00662 
00663   virtual void setOpacity(const rve_msgs::UUID& render_target_id, const rve_msgs::UUID& id, float opacity)
00664   {
00665     //RenderTarget* target = renderer_->getRenderTarget(render_target_id);
00666     //rve_render_server::ScreenRect* rect = target->getScreenRect(id);
00667     // TODO rect->setOpacity(opacity);
00668   }
00669 
00670   virtual void setTexture(const rve_msgs::UUID& render_target_id, const rve_msgs::UUID& id, const rve_msgs::UUID& texture_id)
00671   {
00672     RenderTarget* target = renderer_->getRenderTarget(render_target_id);
00673     rve_render_server::ScreenRect* rect = target->getScreenRect(id);
00674     TexturePtr tex = renderer_->getTexture(texture_id);
00675     rect->setTexture(tex->getOgreTexture()->getName());
00676   }
00677 
00678   virtual void setTextureByResource(const rve_msgs::UUID& render_target_id, const rve_msgs::UUID& id, const std::string& resource_name)
00679   {
00680     RenderTarget* target = renderer_->getRenderTarget(render_target_id);
00681     rve_render_server::ScreenRect* rect = target->getScreenRect(id);
00682 
00683     std::string ogre_resource;
00684     if (lookupTextureResource(resource_name, ogre_resource))
00685     {
00686       rect->setTexture(ogre_resource);
00687     }
00688     else
00689     {
00690       throw std::runtime_error("Resource [" + resource_name + "] is not a loaded texture or render texture");
00691     }
00692   }
00693 
00694 private:
00695   Renderer* renderer_;
00696 };
00697 
00698 class BillboardTextServer: public rve_interfaces::BillboardTextServer
00699 {
00700 public:
00701   BillboardTextServer(Renderer* rend, rve_rpc::Server& server)
00702   : rve_interfaces::BillboardTextServer(server)
00703   , renderer_(rend)
00704   {
00705   }
00706 
00707   virtual void create(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id)
00708   {
00709     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00710     scene->createBillboardText(id);
00711   }
00712   virtual void destroy(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id)
00713   {
00714     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00715     scene->destroyBillboardText(id);
00716   }
00717   virtual void setPosition(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id, const rve_msgs::Vector3& position)
00718   {
00719     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00720     rve_render_server::BillboardText* bbt = scene->getBillboardText(id);
00721     bbt->setPosition( position );
00722   }
00723   virtual void setText(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id, const std::string& text)
00724   {
00725     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00726     rve_render_server::BillboardText* bbt = scene->getBillboardText(id);
00727     bbt->setText( text );
00728   }
00729   virtual void setCharacterHeight(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id, float height)
00730   {
00731     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00732     rve_render_server::BillboardText* bbt = scene->getBillboardText(id);
00733     bbt->setCharacterHeight(height);
00734   }
00735   virtual void setAlignment(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id, const rve_msgs::TextAlignment& align)
00736   {
00737     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00738     rve_render_server::BillboardText* bbt = scene->getBillboardText(id);
00739     bbt->setAlignment(align);
00740   }
00741   virtual void setColor(const rve_msgs::UUID& scene_id, const rve_msgs::UUID& id, const rve_msgs::ColorRGB& color)
00742   {
00743     rve_render_server::Scene* scene = lookupScene(renderer_, scene_id);
00744     rve_render_server::BillboardText* bbt = scene->getBillboardText(id);
00745     bbt->setColor(color);
00746   }
00747 
00748 private:
00749   Renderer* renderer_;
00750 };
00751 
00752 Server::Server(Renderer* renderer, const ros::NodeHandle& nh)
00753 : renderer_(renderer)
00754 , nh_(new ros::NodeHandle(nh))
00755 {
00756   renderer_->registerInitFunction(boost::bind(&Server::init, this));
00757 }
00758 
00759 Server::~Server()
00760 {
00761 }
00762 
00763 void Server::init()
00764 {
00765   nh_->setCallbackQueue(renderer_->getServerThreadCallbackQueue());
00766   server_.reset(new rve_rpc::Server("render_server", *nh_));
00767 
00768   interfaces_.push_back(rve_interface_gen::InterfacePtr(new CameraServer(renderer_, *server_)));
00769   interfaces_.push_back(rve_interface_gen::InterfacePtr(new RenderTargetServer(renderer_, *server_)));
00770   interfaces_.push_back(rve_interface_gen::InterfacePtr(new ScreenRectServer(renderer_, *server_)));
00771   interfaces_.push_back(rve_interface_gen::InterfacePtr(new SceneServer(renderer_, *server_)));
00772   interfaces_.push_back(rve_interface_gen::InterfacePtr(new MaterialServer(renderer_, *server_)));
00773   interfaces_.push_back(rve_interface_gen::InterfacePtr(new TextureServer(renderer_, *server_)));
00774   interfaces_.push_back(rve_interface_gen::InterfacePtr(new MeshServer(renderer_, *server_)));
00775   interfaces_.push_back(rve_interface_gen::InterfacePtr(new MeshInstanceServer(renderer_, *server_)));
00776   interfaces_.push_back(rve_interface_gen::InterfacePtr(new SphereServer(renderer_, *server_)));
00777   interfaces_.push_back(rve_interface_gen::InterfacePtr(new BoxServer(renderer_, *server_)));
00778   interfaces_.push_back(rve_interface_gen::InterfacePtr(new CylinderServer(renderer_, *server_)));
00779   interfaces_.push_back(rve_interface_gen::InterfacePtr(new ConeServer(renderer_, *server_)));
00780   interfaces_.push_back(rve_interface_gen::InterfacePtr(new PointsServer(renderer_, *server_)));
00781   interfaces_.push_back(rve_interface_gen::InterfacePtr(new LinesServer(renderer_, *server_)));
00782   interfaces_.push_back(rve_interface_gen::InterfacePtr(new TrianglesServer(renderer_, *server_)));
00783   interfaces_.push_back(rve_interface_gen::InterfacePtr(new BillboardTextServer(renderer_, *server_)));
00784 
00785   server_->ready();
00786 }
00787 
00788 } // namespace rve_render_server


rve_render_server
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:15