Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "rve_render_client/render_target.h"
00031 #include <rve_render_client/client_context.h>
00032 #include "rve_render_client/camera.h"
00033 #include "rve_render_client/scene.h"
00034 #include "rve_render_client/screen_rect.h"
00035
00036 #include <rve_interfaces/RenderTarget.h>
00037 #include <rve_interfaces/RenderTarget_pickResponse.h>
00038 #include <rve_rpc/method_response.h>
00039
00040 #include <ros/node_handle.h>
00041
00042 #include <boost/bind.hpp>
00043
00044 using namespace rve_common;
00045
00046 namespace rve_render_client
00047 {
00048
00049 RenderTarget::RenderTarget(uint32_t width, uint32_t height)
00050 : context_(0)
00051 , proxy_handle_(NullHandle)
00052 , width_(width)
00053 , height_(height)
00054 , id_(UUID::generate())
00055 {
00056 }
00057
00058 RenderTarget::~RenderTarget()
00059 {
00060 }
00061
00062 rve_interfaces::RenderTargetProxy* RenderTarget::getProxy()
00063 {
00064 ROS_ASSERT(proxy_handle_ != NullHandle);
00065 ROS_ASSERT(context_);
00066 return context_->getInterface<rve_interfaces::RenderTargetProxy>(proxy_handle_);
00067 }
00068
00069 void RenderTarget::create(ClientContext* context)
00070 {
00071
00072 ROS_ASSERT(!context_);
00073 ROS_ASSERT(proxy_handle_ == NullHandle);
00074 context_ = context;
00075 proxy_handle_ = context->lookupInterface("render_target");
00076
00077
00078 doCreate();
00079
00080 if (camera_id_ != UUID::Null)
00081 {
00082 rve_interfaces::RenderTargetProxy* proxy = getProxy();
00083 proxy->attachCamera(getID(), camera_scene_id_, camera_id_);
00084 }
00085 rve_render_client::CameraPtr cam = camera_.lock();
00086 if( cam )
00087 {
00088 cam->setAspectRatio( float(width_)/float(height_) );
00089 }
00090 }
00091
00092 void RenderTarget::destroy(ClientContext* context)
00093 {
00094 ROS_ASSERT(context == context_);
00095 rve_interfaces::RenderTargetProxy* proxy = getProxy();
00096 proxy->destroy(getID());
00097
00098 proxy_handle_ = NullHandle;
00099 context_ = 0;
00100 }
00101
00102 void RenderTarget::resize(uint32_t width, uint32_t height)
00103 {
00104 width_ = width;
00105 height_ = height;
00106
00107 CameraPtr cam = camera_.lock();
00108 if (cam)
00109 {
00110 cam->setAspectRatio( float(width)/float(height) );
00111 }
00112
00113 if (context_)
00114 {
00115 rve_interfaces::RenderTargetProxy* proxy = getProxy();
00116 proxy->resizeAsync(getID(), width, height);
00117 }
00118 }
00119
00120 void RenderTarget::attachCamera(const CameraPtr& cam)
00121 {
00122 camera_ = cam;
00123 if (context_)
00124 {
00125 rve_interfaces::RenderTargetProxy* proxy = getProxy();
00126 if (cam)
00127 {
00128 cam->setAspectRatio( float(width_)/float(height_) );
00129
00130 proxy->attachCameraAsync(getID(), cam->getScene()->getID(), cam->getID());
00131 camera_id_ = cam->getID();
00132 camera_scene_id_ = cam->getScene()->getID();
00133 }
00134 else
00135 {
00136 proxy->detachCameraAsync(getID());
00137 camera_id_ = UUID::Null;
00138 camera_scene_id_ = UUID::Null;
00139 }
00140 }
00141 }
00142
00143 void RenderTarget::pick(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2,
00144 boost::function<void(const rve_rpc::MethodResponse<rve_interfaces::RenderTarget::pickResponse>&)> callback)
00145 {
00146 if (context_)
00147 {
00148 rve_interfaces::RenderTargetProxy* proxy = getProxy();
00149 proxy->pickAsync(getID(), x1, y1, x2, y2, callback);
00150 }
00151 }
00152
00153 void RenderTarget::requestRender()
00154 {
00155 if (context_)
00156 {
00157 rve_interfaces::RenderTargetProxy* proxy = getProxy();
00158 proxy->requestRenderAsync(getID());
00159 }
00160 }
00161
00162 void RenderTarget::destroyScreenRect(ScreenRect* rect)
00163 {
00164 if (context_)
00165 {
00166 rect->destroy(context_);
00167 }
00168
00169 delete rect;
00170 }
00171
00172 ScreenRectPtr RenderTarget::createScreenRect(uint32_t zorder, float x0, float y0, float x1, float y1)
00173 {
00174 ScreenRectPtr rect(new ScreenRect(getID(), zorder, x0, y0, x1, y1), boost::bind(&RenderTarget::destroyScreenRect, this, _1));
00175 if (context_)
00176 {
00177 rect->create(context_);
00178 }
00179 return rect;
00180 }
00181
00182 std::string dashToUnderscore(const std::string& str)
00183 {
00184 std::string out = str;
00185 for (size_t i = 0; i < out.size(); ++i)
00186 {
00187 if (out[i] == '-')
00188 {
00189 out[i] = '_';
00190 }
00191 }
00192
00193 return out;
00194 }
00195
00196 std::string RenderTarget::getNamespace()
00197 {
00198 return context_->getNodeHandle().getNamespace() + "/render_targets/" + dashToUnderscore(getID().toString());
00199 }
00200
00201 }
00202