test_playpen.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_client_common/client_context.h"
00031 #include "rve_render_server/init.h"
00032 #include "rve_render_client/init.h"
00033 #include "rve_render_client/render_window.h"
00034 #include "rve_render_client/render_texture.h"
00035 #include "rve_render_client/screen_rect.h"
00036 #include "rve_render_client/scene.h"
00037 #include "rve_render_client/camera.h"
00038 #include "rve_render_client/material.h"
00039 #include "rve_render_client/mesh_instance.h"
00040 #include "rve_render_client/sphere.h"
00041 #include "rve_render_client/box.h"
00042 #include "rve_render_client/cylinder.h"
00043 #include "rve_render_client/cone.h"
00044 #include "rve_render_client/points.h"
00045 #include "rve_render_client/lines.h"
00046 #include "rve_render_client/triangles.h"
00047 
00048 
00049 #include <rve_wx/app.h>
00050 #include <rve_wx/render_window.h>
00051 
00052 #include <rve_msgs/Points.h>
00053 #include <rve_msgs/Lines.h>
00054 #include <rve_msgs/Triangles.h>
00055 #include <rve_msgs/Mesh.h>
00056 
00057 #include <rve_msgs/make_vector3.h>
00058 #include <rve_msgs/make_quaternion.h>
00059 
00060 #include <Eigen/Geometry>
00061 
00062 #include <ros/package.h>
00063 #include <ros/time.h>
00064 
00065 #include <wx/wx.h>
00066 
00067 #include <ros/time.h>
00068 #include <ros/ros.h>
00069 
00070 using namespace rve_client_common;
00071 using namespace rve_render_client;
00072 
00073 class MyFrame : public wxFrame
00074 {
00075 public:
00076   MyFrame(const ClientContextPtr& context, wxWindow* parent)
00077   : wxFrame(parent, -1, _("test_playpen"), wxDefaultPosition, wxSize(1024, 768), wxDEFAULT_FRAME_STYLE)
00078   , private_nh_("~")
00079   , left_mouse_down_( false )
00080   , middle_mouse_down_( false )
00081   , right_mouse_down_( false )
00082   , mouse_x_( 0 )
00083   , mouse_y_( 0 )
00084   , timer_(this)
00085   {
00086     context_ = context;
00087     rve_wx::RenderWindow* rw = new rve_wx::RenderWindow(context.get(), this, wxID_ANY);
00088     render_window_ = rw;
00089 
00090     scene_ = rve_render_client::createScene(context_);
00091     camera_ = rve_render_client::createCamera(scene_.get());
00092     camera_->setPosition(rve_msgs::makeVector3(-10, 0, 0));
00093     camera_->lookAt(rve_msgs::makeVector3(0, 0, 0));
00094     camera_->setAutoAspectRatio(true);
00095     render_window_->getRemoteRenderWindow()->attachCamera(camera_);
00096 
00097 #if 01
00098     MeshInstancePtr inst = createMeshInstance(scene_.get(), "package://pr2_description/meshes/base_v0/base.dae");
00099     inst->setPosition(rve_msgs::makeVector3(0.0, 0.0, 2.0));
00100     scene_objects_.push_back(inst);
00101 
00102 #if 01
00103     SpherePtr sphere = createSphere(scene_.get());
00104     MaterialPtr mat = createMaterial(context_.get());
00105     sphere->setMaterial(mat);
00106     mat->setDiffuse(Color(1.0, 0.0, 0.0));
00107 
00108     scene_objects_.push_back(sphere);
00109     materials_.push_back(mat);
00110 
00111     BoxPtr box = createBox(scene_.get());
00112     mat = createMaterial(context_.get());
00113     mat->setDiffuse(Color(0.0, 1.0, 0.0));
00114     box->setMaterial(mat);
00115     box->setPosition(rve_msgs::makeVector3(0, 2, 0));
00116     box->setScale(rve_msgs::makeVector3(1.0, 0.5, 2.0));
00117     scene_objects_.push_back(box);
00118     materials_.push_back(mat);
00119 
00120     CylinderPtr cyl = createCylinder(scene_.get());
00121     mat = createMaterial(context_.get());
00122     mat->setDiffuse(Color(0.0, 0.0, 1.0));
00123     cyl->setMaterial(mat);
00124     cyl->setPosition(rve_msgs::makeVector3(0, -2, 0));
00125     cyl->setRadius(0.2);
00126     cyl->setHeight(2.0);
00127     scene_objects_.push_back(cyl);
00128     materials_.push_back(mat);
00129 
00130     ConePtr cone = createCone(scene_.get());
00131     mat = createMaterial(context_.get());
00132     mat->setDiffuse(Color(1.0, 1.0, 1.0));
00133     cone->setMaterial(mat);
00134     cone->setPosition(rve_msgs::makeVector3(0, -4, 0));
00135     cone->setRadius(0.2);
00136     cone->setHeight(2.0);
00137     scene_objects_.push_back(cone);
00138     materials_.push_back(mat);
00139 #endif
00140 #endif
00141 
00142 #if 0
00143     {
00144       rve_msgs::PointsPtr points(new rve_msgs::Points);
00145       points->type = rve_msgs::Points::TYPE_BILLBOARDS;
00146       points->scale.x = 0.05;
00147       points->scale.y = 0.05;
00148       points->scale.z = 0.05;
00149 
00150       for (uint32_t x = 0; x < 100; ++x)
00151       {
00152         for (uint32_t y = 0; y < 100; ++y)
00153         {
00154           for (uint32_t z = 0; z < 10; ++z)
00155           {
00156             rve_msgs::Vector3 pos;
00157             pos.x = -10.0 - x*0.1;
00158             pos.y = y*0.1;
00159             pos.z = z*0.1;
00160             points->positions.push_back(pos);
00161 
00162             std_msgs::ColorRGBA col;
00163             col.r = x * 0.1;
00164             col.g = y * 0.1;
00165             col.b = z * 0.1;
00166             col.a = 1.0;//(z % 2 == 0) ? 1.0 : 0.5;
00167             points->colors.push_back(col);
00168           }
00169         }
00170       }
00171 
00172       PointsPtr p = createPoints(scene_.get(), points);
00173       scene_objects_.push_back(p);
00174     }
00175 #endif
00176 
00177 #if 0
00178     {
00179       rve_msgs::LinesPtr lines(new rve_msgs::Lines);
00180       lines->type = rve_msgs::Lines::TYPE_BILLBOARD_LIST;
00181       lines->scale.x = 0.05;
00182       lines->scale.y = 0.05;
00183       lines->scale.z = 0.05;
00184 
00185       for (uint32_t x = 0; x < 10; ++x)
00186       {
00187         for (uint32_t y = 0; y < 10; ++y)
00188         {
00189           for (uint32_t z = 0; z <= 10; z += 10)
00190           {
00191             rve_msgs::Vector3 pos;
00192             pos.x = -10.0 - x*0.1;
00193             pos.y = y*0.1;
00194             pos.z = z*0.1;
00195             lines->positions.push_back(pos);
00196 
00197             std_msgs::ColorRGBA col;
00198             col.r = x * 0.1;
00199             col.g = y * 0.1;
00200             col.b = z * 0.1;
00201             col.a = 1.0;//(z % 2 == 0) ? 1.0 : 0.5;
00202             lines->colors.push_back(col);
00203           }
00204         }
00205       }
00206 
00207       LinesPtr l = createLines(scene_.get(), lines);
00208       scene_objects_.push_back(l);
00209     }
00210 #endif
00211 
00212 #if 0
00213     {
00214       rve_msgs::TrianglesPtr tris(new rve_msgs::Triangles);
00215 
00216       Eigen::Vector3f offsets[3] =
00217       {
00218         Eigen::Vector3f(0.0, 0.0, 0.0),
00219         Eigen::Vector3f(0.0, 0.0, 0.1),
00220         Eigen::Vector3f(0.1, 0.0, 0.0)
00221       };
00222 
00223       for (uint32_t x = 0; x < 10; ++x)
00224       {
00225         for (uint32_t y = 0; y < 10; ++y)
00226         {
00227           for (uint32_t z = 0; z < 10; ++z)
00228           {
00229             for (uint32_t j = 0; j < 3; ++j)
00230             {
00231               rve_msgs::Vector3 pos;
00232               pos.x = -10.0 - x*0.1 + offsets[j].x();
00233               pos.y = y*0.1 + offsets[j].y();
00234               pos.z = z*0.1 + offsets[j].z();
00235               tris->positions.push_back(pos);
00236 
00237               std_msgs::ColorRGBA col;
00238               col.r = x * 0.1;
00239               col.g = y * 0.1;
00240               col.b = z * 0.1;
00241               col.a = 1.0;//(z % 2 == 0) ? 1.0 : 0.5;
00242               tris->colors.push_back(col);
00243             }
00244           }
00245         }
00246       }
00247 
00248       TrianglesPtr t = createTriangles(scene_.get(), tris);
00249       scene_objects_.push_back(t);
00250     }
00251 #endif
00252 
00254 
00255 #if 01
00256     rtt_scene_ = createScene(context_);
00257     rtt_camera_ = createCamera(rtt_scene_.get());
00258     rtt_camera_->setPosition(rve_msgs::makeVector3(-2, 0, 0));
00259     rtt_camera_->lookAt(rve_msgs::makeVector3(0, 0, 0));
00260     rtt_camera_->setAutoAspectRatio(true);
00261     rtt_ = createRenderTexture(context_.get(), 500, 500);
00262     rtt_->attachCamera(rtt_camera_);
00263 
00264     scene_objects_.push_back(rve_render_client::createMeshInstance(rtt_scene_.get(), "package://rve_render_server/media/models/sphere.dae"));
00265 #endif
00266 
00268     {
00269       rve_msgs::MeshPtr mesh(new rve_msgs::Mesh);
00270       mesh->submeshes.resize(1);
00271 
00272       rve_msgs::SubMesh& sm = mesh->submeshes[0];
00273       sm.positions.push_back(rve_msgs::makeVector3(0.0, -0.5, 0.5));
00274       sm.positions.push_back(rve_msgs::makeVector3(0.0, 0.5, 0.5));
00275       sm.positions.push_back(rve_msgs::makeVector3(0.0, -0.5, -0.5));
00276       sm.positions.push_back(rve_msgs::makeVector3(0.0, 0.5, -0.5));
00277       sm.indices.push_back(0);
00278       sm.indices.push_back(1);
00279       sm.indices.push_back(2);
00280       sm.indices.push_back(2);
00281       sm.indices.push_back(1);
00282       sm.indices.push_back(3);
00283 
00284       sm.normals.push_back(rve_msgs::makeVector3(-1.0, 0.0, 0.0));
00285       sm.normals.push_back(rve_msgs::makeVector3(-1.0, 0.0, 0.0));
00286       sm.normals.push_back(rve_msgs::makeVector3(-1.0, 0.0, 0.0));
00287       sm.normals.push_back(rve_msgs::makeVector3(-1.0, 0.0, 0.0));
00288 
00289       sm.tex_coords.resize(1);
00290       rve_msgs::SubMesh::_tex_coords_type& tc = sm.tex_coords;
00291       tc[0].dims = 2;
00292       tc[0].array.resize(4);
00293       tc[0].array[0].uvw[0] = 1.0;
00294       tc[0].array[0].uvw[1] = 0.0;
00295 
00296       tc[0].array[1].uvw[0] = 0.0;
00297       tc[0].array[1].uvw[1] = 0.0;
00298 
00299       tc[0].array[2].uvw[0] = 1.0;
00300       tc[0].array[2].uvw[1] = 1.0;
00301 
00302       tc[0].array[3].uvw[0] = 0.0;
00303       tc[0].array[3].uvw[1] = 1.0;
00304 
00305       sm.material_index = 0;
00306 
00307       mesh->materials.resize(1);
00308       rve_msgs::Material& mat = mesh->materials[0];
00309       mat.id = rve_common::UUID::Generate();
00310       mat.opacity = 1.0;
00311       mat.has_texture = true;
00312       mat.texture = "rtt://" + rtt_->getID().toString();
00313       mat.disable_shading = true;
00314       mat.cull_mode = rve_msgs::Material::CULL_NONE;
00315       //mat.texture = "package://rve_render_server/media/textures/no_image.png";
00316 
00317       MeshPtr m = createMesh(context_.get(), mesh);
00318       MeshInstancePtr inst = createMeshInstance(scene_.get(), m);
00319       inst->setPosition(rve_msgs::makeVector3(0.0, 2.0, 2.0));
00320       scene_objects_.push_back(inst);
00321     }
00322 
00324     {
00325       ScreenRectPtr rect = render_window_->getRemoteRenderWindow()->createScreenRect(0, 0.8f, 0.8f, 1.0f, 1.0f);
00326       rect->setTexture("rtt://" + rtt_->getID().toString());
00327       rect->setOpacity(0.5);
00328       screen_rect_ = rect;
00329     }
00330 
00331     Connect(wxEVT_TIMER, wxTimerEventHandler(MyFrame::onTimer));
00332 
00333     render_window_->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( MyFrame::onMouseEvents ), NULL, this );
00334     render_window_->Connect( wxEVT_MIDDLE_DOWN, wxMouseEventHandler( MyFrame::onMouseEvents ), NULL, this );
00335     render_window_->Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( MyFrame::onMouseEvents ), NULL, this );
00336     render_window_->Connect( wxEVT_MOTION, wxMouseEventHandler( MyFrame::onMouseEvents ), NULL, this );
00337     render_window_->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( MyFrame::onMouseEvents ), NULL, this );
00338     render_window_->Connect( wxEVT_MIDDLE_UP, wxMouseEventHandler( MyFrame::onMouseEvents ), NULL, this );
00339     render_window_->Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( MyFrame::onMouseEvents ), NULL, this );
00340     render_window_->Connect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( MyFrame::onMouseEvents ), NULL, this );
00341 
00342     timer_.Start(16);
00343   }
00344 
00345   ~MyFrame()
00346   {
00347   }
00348 
00349   void onTimer(wxTimerEvent& evt)
00350   {
00351 #if 0
00352     if (last_update_.isZero())
00353     {
00354       last_update_ = ros::WallTime::now();
00355       return;
00356     }
00357 
00358     ros::WallTime cur = ros::WallTime::now();
00359     ros::WallDuration diff = cur - last_update_;
00360     last_update_ = cur;
00361     double dt = diff.toSec();
00362 #endif
00363   }
00364 
00365   void onMouseEvents( wxMouseEvent& event )
00366   {
00367     int lastX = mouse_x_;
00368     int lastY = mouse_y_;
00369 
00370     mouse_x_ = event.GetX();
00371     mouse_y_ = event.GetY();
00372 
00373     if ( event.LeftDown() )
00374     {
00375      left_mouse_down_ = true;
00376      middle_mouse_down_ = false;
00377      right_mouse_down_ = false;
00378     }
00379     else if ( event.MiddleDown() )
00380     {
00381      left_mouse_down_ = false;
00382      middle_mouse_down_ = true;
00383      right_mouse_down_ = false;
00384     }
00385     else if ( event.RightDown() )
00386     {
00387      left_mouse_down_ = false;
00388      middle_mouse_down_ = false;
00389      right_mouse_down_ = true;
00390 
00391 #if 0
00392       if (context_->isConnected())
00393       {
00394         context_->close();
00395       }
00396       else
00397       {
00398         context_->reopen();
00399       }
00400 #endif
00401     }
00402     else if ( event.LeftUp() )
00403     {
00404      left_mouse_down_ = false;
00405     }
00406     else if ( event.MiddleUp() )
00407     {
00408      middle_mouse_down_ = false;
00409     }
00410     else if ( event.RightUp() )
00411     {
00412      right_mouse_down_ = false;
00413     }
00414     else if ( event.Dragging() )
00415     {
00416      int32_t diff_x = mouse_x_ - lastX;
00417      int32_t diff_y = mouse_y_ - lastY;
00418 
00419      bool handled = false;
00420      if ( left_mouse_down_ )
00421      {
00422        Eigen::Quaternionf quat(Eigen::AngleAxisf(diff_y * 0.002, Eigen::Vector3f::UnitY()));
00423        Eigen::Quaternionf quat2(Eigen::AngleAxisf(-diff_x * 0.002, Eigen::Vector3f::UnitZ()));
00424        Eigen::Quaternionf combined = quat * quat2;
00425        camera_->rotate(rve_msgs::makeQuaternion(combined.x(), combined.y(), combined.z(), combined.w()));
00426 
00427        handled = true;
00428      }
00429      else if ( middle_mouse_down_ )
00430      {
00431        camera_->moveRelative(Eigen::Vector3f(0.0, -diff_x * 0.01, -diff_y * 0.01));
00432 
00433        handled = true;
00434      }
00435      else if ( right_mouse_down_ )
00436      {
00437        camera_->moveRelative(Eigen::Vector3f(-diff_y * 0.01, 0.0, 0.0));
00438 
00439        handled = true;
00440      }
00441     }
00442 
00443     if ( event.GetWheelRotation() != 0 )
00444     {
00445      //camera_->scrollWheel( event.GetWheelRotation(), event.CmdDown(), event.AltDown(), event.ShiftDown() );
00446     }
00447 
00448     if (!left_mouse_down_ && !right_mouse_down_ && !middle_mouse_down_)
00449     {
00450       render_window_->getRemoteRenderWindow()->pick(mouse_x_, mouse_y_);
00451     }
00452   }
00453 
00454 private:
00455   ros::NodeHandle private_nh_;
00456   rve_wx::RenderWindow* render_window_;
00457 
00458   ClientContextPtr context_;
00459   ScenePtr scene_;
00460   CameraPtr camera_;
00461 
00462   ScenePtr rtt_scene_;
00463   CameraPtr rtt_camera_;
00464   RenderTexturePtr rtt_;
00465 
00466   ScreenRectPtr screen_rect_;
00467 
00468   std::vector<SceneObjectPtr> scene_objects_;
00469   std::vector<MaterialPtr> materials_;
00470 
00471   // Mouse handling
00472   bool left_mouse_down_;
00473   bool middle_mouse_down_;
00474   bool right_mouse_down_;
00475   int mouse_x_;
00476   int mouse_y_;
00477 
00478   wxTimer timer_;
00479 
00480   ros::WallTime last_update_;
00481 };
00482 
00483 // our normal wxApp-derived class, as usual
00484 class MyApp : public rve_wx::App
00485 {
00486 public:
00487   MyApp(const ClientContextPtr& context)
00488   : context_(context)
00489   {}
00490 
00491   bool OnInit()
00492   {
00493     if (!rve_wx::App::OnInit())
00494     {
00495       return false;
00496     }
00497 
00498     wxFrame* frame = new MyFrame(context_, NULL);
00499     SetTopWindow(frame);
00500     frame->Show();
00501     return true;
00502   }
00503 
00504 private:
00505   ClientContextPtr context_;
00506 };
00507 
00508 DECLARE_APP(MyApp);
00509 
00510 int main(int argc, char** argv)
00511 {
00512   try
00513   {
00514     ros::init(argc, argv, "test_playpen", ros::init_options::NoSigintHandler);
00515     rve_render_server::init(true, "render_server");
00516     rve_render_client::init();
00517     ros::AsyncSpinner as(0);
00518     as.start();
00519 
00520     ClientContextPtr context = createContext("render_server", ros::NodeHandle("render_server"));
00521     wxApp* app = new MyApp(context);
00522     wxApp::SetInstance(app);
00523     wxEntry(argc, argv);
00524 
00525     rve_render_client::shutdown();
00526     rve_render_server::shutdown();
00527   }
00528   catch (std::exception& e)
00529   {
00530     ROS_ERROR("Caught exception: %s", e.what());
00531   }
00532 }
00533 


test_rve
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:32:08