facing_visualizer.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2014, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Willow Garage nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #include "facing_visualizer.h"
00037 #include <rviz/uniform_string_stream.h>
00038 #include <rviz/render_panel.h>
00039 #include <rviz/view_manager.h>
00040 #include <rviz/properties/parse_color.h>
00041 #include <QPainter>
00042 
00043 namespace jsk_rviz_plugin
00044 {
00045 
00046   const double minimum_font_size = 0.2;
00047   const float arrow_animation_duration = 1.0;
00048   
00049   SquareObject::SquareObject(Ogre::SceneManager* manager,
00050                              double outer_radius,
00051                              double inner_radius,
00052                              std::string name):
00053     manager_(manager), outer_radius_(outer_radius),
00054     inner_radius_(inner_radius), name_(name)
00055   {
00056     manual_ = manager->createManualObject();
00057     rebuildPolygon();
00058   }
00059 
00060   SquareObject::~SquareObject()
00061   {
00062     manual_->detachFromParent();
00063     manager_->destroyManualObject(manual_);
00064   }
00065 
00066   Ogre::ManualObject* SquareObject::getManualObject()
00067   {
00068     return manual_;
00069   }
00070 
00071   void SquareObject::setOuterRadius(double outer_radius)
00072   {
00073     outer_radius_ = outer_radius;
00074   }
00075 
00076   void SquareObject::setInnerRadius(double inner_radius)
00077   {
00078     inner_radius_ = inner_radius;
00079   }
00080 
00081   void SquareObject::rebuildPolygon()
00082   {
00083     manual_->clear();
00084     manual_->begin(name_,
00085                    Ogre::RenderOperation::OT_TRIANGLE_STRIP);
00086     const size_t resolution = 100;
00087     const double radius_ratio = inner_radius_ / outer_radius_;
00088     const double inner_offset = - outer_radius_ * 0.0;
00089     int counter = 0;
00090     for (size_t i = 0; i < resolution; i++) {
00091       double theta = 2.0 * M_PI / resolution * i;
00092       double next_theta = 2.0 * M_PI / resolution * (i + 1);
00093       
00094       manual_->position(inner_radius_ * cos(theta) + inner_offset,
00095                         inner_radius_ * sin(theta) + inner_offset,
00096                         0.0f);
00097       manual_->textureCoord((1 + radius_ratio *  cos(theta)) / 2.0, (1.0 - radius_ratio * sin(theta)) / 2.0);
00098       manual_->index(counter++);
00099       manual_->position(outer_radius_ * cos(theta),
00100                         outer_radius_ * sin(theta),
00101                         0.0f);
00102       manual_->textureCoord((1 + cos(theta)) / 2.0, (1.0 -sin(theta)) / 2.0);
00103       manual_->index(counter++);
00104       manual_->position(inner_radius_ * cos(next_theta) + inner_offset,
00105                         inner_radius_ * sin(next_theta) + inner_offset,
00106                         0.0f);
00107       manual_->textureCoord((1 + radius_ratio *  cos(next_theta)) / 2.0, (1.0 - radius_ratio * sin(next_theta)) / 2.0);
00108       manual_->index(counter++);
00109       manual_->position(outer_radius_ * cos(next_theta),
00110                         outer_radius_ * sin(next_theta),
00111                         0.0f);
00112       manual_->textureCoord((1 + cos(next_theta)) / 2.0, (1.0 -sin(next_theta)) / 2.0);
00113       manual_->index(counter++);
00114       
00115     }
00116     // for (size_t i = 0; i < resolution; i++) {
00117     // }
00118     // manual_->index(0);
00119     manual_->end();
00120   }
00121   
00122   TextureObject::TextureObject(const int width, const int height,
00123                                const std::string name):
00124     width_(width), height_(height), name_(name)
00125   {
00126     texture_ = Ogre::TextureManager::getSingleton().createManual(
00127       name,
00128       Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
00129       Ogre::TEX_TYPE_2D,
00130        width, height,
00131       0,
00132       Ogre::PF_A8R8G8B8,
00133       Ogre::TU_DEFAULT
00134       );
00135     material_ = Ogre::MaterialManager::getSingleton().create(
00136       getMaterialName(), // name
00137       Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
00138  
00139     material_->getTechnique(0)->getPass(0)->createTextureUnitState(
00140       texture_->getName());
00141     material_->setReceiveShadows(false);
00142     material_->getTechnique(0)->setLightingEnabled(true);
00143     material_->getTechnique(0)->getPass(0)->setCullingMode(Ogre::CULL_NONE);
00144     material_->getTechnique(0)->getPass(0)->setLightingEnabled(false);
00145     material_->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
00146     material_->getTechnique(0)->getPass(0)->setDepthCheckEnabled(true);
00147       
00148     material_->getTechnique(0)->getPass(0)->setVertexColourTracking(Ogre::TVC_DIFFUSE);
00149     material_->getTechnique(0)->getPass(0)->createTextureUnitState(texture_->getName());
00150     material_->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
00151 
00152     material_->getTechnique(0)->getPass(0)
00153       ->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
00154      // material_->getTechnique(0)->getPass(0)
00155      //   ->setSceneBlending(Ogre::SBT_MODULATE);
00156 
00157   }
00158 
00159   TextureObject::~TextureObject()
00160   {
00161     material_->unload();
00162     Ogre::MaterialManager::getSingleton().remove(material_->getName());
00163   }
00164   
00165   ScopedPixelBuffer TextureObject::getBuffer()
00166   {
00167     return ScopedPixelBuffer(texture_->getBuffer());
00168   }
00169 
00170   std::string TextureObject::getMaterialName()
00171   {
00172     return name_ + "Material";
00173   }
00174 
00175   FacingObject::FacingObject(Ogre::SceneManager* manager,
00176                              Ogre::SceneNode* parent,
00177                              double size):
00178     scene_manager_(manager), size_(size), enable_(true), text_("")
00179   {
00180     node_ = parent->createChildSceneNode();
00181   }
00182 
00183   FacingObject::~FacingObject()
00184   {
00185     node_->detachAllObjects();
00186     scene_manager_->destroySceneNode(node_);
00187   }
00188   
00189   void FacingObject::setPosition(Ogre::Vector3& pos)
00190   {
00191     node_->setPosition(pos);
00192   }
00193   
00194   void FacingObject::setOrientation(rviz::DisplayContext* context)
00195   {
00196     rviz::ViewManager* manager = context->getViewManager();
00197     rviz::RenderPanel* panel = manager->getRenderPanel();
00198     Ogre::Camera* camera = panel->getCamera();
00199     node_->setOrientation(camera->getDerivedOrientation());
00200   }
00201 
00202   void FacingObject::setSize(double size)
00203   {
00204     size_ = size;
00205   }
00206 
00207   void FacingObject::setEnable(bool enable)
00208   {
00209     enable_ = enable;
00210     node_->setVisible(enable_);
00211   }
00212 
00213   void FacingObject::setText(std::string text)
00214   {
00215     text_ = text;
00216     //updateTextUnderLine();
00217     updateText();
00218   }
00219 
00220   void FacingObject::setAlpha(double alpha)
00221   {
00222     color_.a = alpha;
00223     updateColor();
00224   }
00225 
00226   void FacingObject::setColor(QColor color)
00227   {
00228     color_.r = color.red() / 255.0;
00229     color_.g = color.green() / 255.0;
00230     color_.b = color.blue() / 255.0;
00231     updateColor();
00232   }
00233 
00234   void FacingObject::setColor(Ogre::ColourValue color)
00235   {
00236     color_ = color;
00237     updateColor();
00238   }
00239   
00240   SimpleCircleFacingVisualizer::SimpleCircleFacingVisualizer(
00241     Ogre::SceneManager* manager,
00242     Ogre::SceneNode* parent,
00243     rviz::DisplayContext* context,
00244     double size,
00245     std::string text):
00246     FacingObject(manager, parent, size)
00247   {
00248     line_ = new rviz::BillboardLine(
00249       context->getSceneManager(),
00250       node_);
00251     text_under_line_ = new rviz::BillboardLine(
00252       context->getSceneManager(),
00253       node_);
00254     target_text_node_ = node_->createChildSceneNode();
00255     msg_ = new rviz::MovableText("not initialized", "Arial", 0.05);
00256     msg_->setVisible(false);
00257     msg_->setTextAlignment(rviz::MovableText::H_LEFT,
00258                            rviz::MovableText::V_ABOVE);
00259     target_text_node_->attachObject(msg_);
00260     createArrows(context);
00261     updateLine();
00262     updateTextUnderLine();
00263     updateText();
00264     setEnable(false);
00265   }
00266 
00267   SimpleCircleFacingVisualizer::~SimpleCircleFacingVisualizer()
00268   {
00269     delete line_;
00270     delete text_under_line_;
00271     delete msg_;
00272     scene_manager_->destroyManualObject(upper_arrow_);
00273     scene_manager_->destroyManualObject(lower_arrow_);
00274     scene_manager_->destroyManualObject(left_arrow_);
00275     scene_manager_->destroyManualObject(right_arrow_);
00276     upper_material_->unload();
00277     lower_material_->unload();
00278     left_material_->unload();
00279     right_material_->unload();
00280     Ogre::MaterialManager::getSingleton().remove(upper_material_->getName());
00281     Ogre::MaterialManager::getSingleton().remove(lower_material_->getName());
00282     Ogre::MaterialManager::getSingleton().remove(left_material_->getName());
00283     Ogre::MaterialManager::getSingleton().remove(right_material_->getName());
00284   }
00285 
00286   void SimpleCircleFacingVisualizer::update(float wall_dt, float ros_dt)
00287   {
00288     double t_ = ros::WallTime::now().toSec();
00289     double t_rate
00290       = fmod(t_, arrow_animation_duration) / arrow_animation_duration;
00291     upper_arrow_node_->setPosition(0, (1.3 - 0.3 * t_rate) * size_, 0);
00292     lower_arrow_node_->setPosition(0, (-1.3 + 0.3 * t_rate) * size_, 0);
00293     left_arrow_node_->setPosition((1.3 - 0.3 * t_rate) * size_, 0, 0);
00294     right_arrow_node_->setPosition((-1.3 + 0.3 * t_rate) * size_, 0, 0);
00295   }
00296   
00297   void SimpleCircleFacingVisualizer::reset()
00298   {
00299     line_->clear();
00300     text_under_line_->clear();
00301     msg_->setVisible(false);
00302   }
00303 
00304   void SimpleCircleFacingVisualizer::updateArrowsObjects(Ogre::ColourValue color)
00305   {
00306     const double size_factor = 0.15;
00307     upper_arrow_node_->setPosition(Ogre::Vector3(0, size_ * 1.0, 0.0));
00308     upper_arrow_->clear();
00309     upper_arrow_->estimateVertexCount(3);
00310     upper_arrow_->begin(upper_material_name_,
00311                         Ogre::RenderOperation::OT_TRIANGLE_LIST);
00312     
00313     upper_arrow_->colour(color);
00314     upper_arrow_->position(Ogre::Vector3(0, size_ * size_factor, 0));
00315     upper_arrow_->colour(color);
00316     upper_arrow_->position(Ogre::Vector3(size_ * size_factor,
00317                                          size_ * size_factor * 2,
00318                                          0));
00319     upper_arrow_->colour(color);
00320     upper_arrow_->position(Ogre::Vector3(-size_ * size_factor,
00321                                          size_ * size_factor * 2,
00322                                          0));
00323     upper_arrow_->end();
00324     
00325     lower_arrow_node_->setPosition(Ogre::Vector3(0, -size_ * 1.0, 0.0));
00326     lower_arrow_->clear();
00327     lower_arrow_->estimateVertexCount(3);
00328     lower_arrow_->begin(lower_material_name_,
00329                         Ogre::RenderOperation::OT_TRIANGLE_LIST);
00330     
00331     lower_arrow_->colour(color);
00332     lower_arrow_->position(Ogre::Vector3(0,
00333                                          -size_ * size_factor,
00334                                          0));
00335     lower_arrow_->colour(color);
00336     lower_arrow_->position(Ogre::Vector3(size_ * size_factor,
00337                                          -size_ * size_factor * 2,
00338                                          0));
00339     lower_arrow_->colour(color);
00340     lower_arrow_->position(Ogre::Vector3(-size_ * size_factor,
00341                                          -size_ * size_factor * 2,
00342                                          0));
00343     lower_arrow_->end();
00344     left_arrow_node_->setPosition(Ogre::Vector3(size_ * 1.0, 0.0, 0.0));
00345     left_arrow_->clear();
00346     left_arrow_->estimateVertexCount(3);
00347     left_arrow_->begin(left_material_name_,
00348                        Ogre::RenderOperation::OT_TRIANGLE_LIST);
00349     
00350     left_arrow_->colour(color);
00351     left_arrow_->position(Ogre::Vector3(size_ * size_factor,
00352                                         0.0,
00353                                         0));
00354     left_arrow_->colour(color);
00355     left_arrow_->position(Ogre::Vector3(size_ * size_factor * 2,
00356                                         size_ * size_factor,
00357                                         0));
00358     left_arrow_->colour(color);
00359     left_arrow_->position(Ogre::Vector3(size_ * size_factor * 2,
00360                                         - size_ * size_factor,
00361                                         0));
00362     left_arrow_->end();
00363     
00364     right_arrow_node_->setPosition(Ogre::Vector3(-size_ * 1.0, 0.0, 0.0));
00365     right_arrow_->clear();
00366     right_arrow_->estimateVertexCount(3);
00367     right_arrow_->begin(right_material_name_,
00368                         Ogre::RenderOperation::OT_TRIANGLE_LIST);
00369     
00370     right_arrow_->colour(color);
00371     right_arrow_->position(Ogre::Vector3(-size_ * size_factor,
00372                                          0.0,
00373                                          0));
00374     right_arrow_->colour(color);
00375     right_arrow_->position(Ogre::Vector3(-size_ * size_factor * 2,
00376                                          size_ * size_factor,
00377                                          0));
00378     right_arrow_->colour(color);
00379     right_arrow_->position(Ogre::Vector3(-size_ * size_factor * 2,
00380                                          - size_ * size_factor,
00381                                          0));
00382     right_arrow_->end();
00383     
00384     
00385     upper_material_->getTechnique(0)->setLightingEnabled(false);
00386     upper_material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00387     upper_material_->getTechnique(0)->setDepthWriteEnabled( false );
00388     lower_material_->getTechnique(0)->setLightingEnabled(false);
00389     lower_material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00390     lower_material_->getTechnique(0)->setDepthWriteEnabled( false );
00391     left_material_->getTechnique(0)->setLightingEnabled(false);
00392     left_material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00393     left_material_->getTechnique(0)->setDepthWriteEnabled( false );
00394     right_material_->getTechnique(0)->setLightingEnabled(false);
00395     right_material_->getTechnique(0)->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00396     right_material_->getTechnique(0)->setDepthWriteEnabled( false );
00397   }
00398   
00399   // allocate material and node for arrrows
00400   void SimpleCircleFacingVisualizer::createArrows(
00401     rviz::DisplayContext* context)
00402   {
00403     static uint32_t count = 0;
00404     rviz::UniformStringStream ss;
00405     ss << "TargetVisualizerDisplayTriangle" << count++;
00406     ss << "Material";
00407     ss << "0";
00408     upper_material_name_ = std::string(ss.str());
00409     ss << "1";
00410     lower_material_name_ = std::string(ss.str());
00411     ss << "2";
00412     left_material_name_ = std::string(ss.str());
00413     ss << "3";
00414     right_material_name_ = std::string(ss.str());
00415     upper_material_ = Ogre::MaterialManager::getSingleton().create(
00416       upper_material_name_,
00417       Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
00418     lower_material_ = Ogre::MaterialManager::getSingleton().create(
00419       lower_material_name_,
00420       Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
00421     left_material_ = Ogre::MaterialManager::getSingleton().create(
00422       left_material_name_,
00423       Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
00424     right_material_ = Ogre::MaterialManager::getSingleton().create(
00425       right_material_name_,
00426       Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
00427     
00428     upper_material_->setReceiveShadows(false);
00429     upper_material_->getTechnique(0)->setLightingEnabled(true);
00430     upper_material_->setCullingMode(Ogre::CULL_NONE);
00431     lower_material_->setReceiveShadows(false);
00432     lower_material_->getTechnique(0)->setLightingEnabled(true);
00433     lower_material_->setCullingMode(Ogre::CULL_NONE);
00434     left_material_->setReceiveShadows(false);
00435     left_material_->getTechnique(0)->setLightingEnabled(true);
00436     left_material_->setCullingMode(Ogre::CULL_NONE);
00437     right_material_->setReceiveShadows(false);
00438     right_material_->getTechnique(0)->setLightingEnabled(true);
00439     right_material_->setCullingMode(Ogre::CULL_NONE);
00440 
00441     upper_arrow_ = context->getSceneManager()->createManualObject(
00442       upper_material_name_);
00443     upper_arrow_node_ = node_->createChildSceneNode();
00444     upper_arrow_node_->attachObject(upper_arrow_);
00445     lower_arrow_ = context->getSceneManager()->createManualObject(
00446       lower_material_name_);
00447     lower_arrow_node_ = node_->createChildSceneNode();
00448     lower_arrow_node_->attachObject(lower_arrow_);
00449     left_arrow_ = context->getSceneManager()->createManualObject(
00450       left_material_name_);
00451     left_arrow_node_ = node_->createChildSceneNode();
00452     left_arrow_node_->attachObject(left_arrow_);
00453     right_arrow_ = context->getSceneManager()->createManualObject(
00454       right_material_name_);
00455     right_arrow_node_ = node_->createChildSceneNode();
00456     right_arrow_node_->attachObject(right_arrow_);
00457     updateArrowsObjects(color_);
00458   }
00459 
00460   void SimpleCircleFacingVisualizer::updateLine()
00461   {
00462     const int resolution = 100;
00463     line_->clear();
00464     line_->setColor(color_.r, color_.g, color_.b, color_.a);
00465     line_->setLineWidth(0.1 * size_);
00466     line_->setNumLines(1);
00467     line_->setMaxPointsPerLine(1024);
00468     for (size_t i = 0; i < resolution + 1; i++) {
00469       double x = size_ * cos(i * 2 * M_PI / resolution);
00470       double y = size_ * sin(i * 2 * M_PI / resolution);
00471       double z = 0;
00472       Ogre::Vector3 p;
00473       p[0] = x;
00474       p[1] = y;
00475       p[2] = z;
00476       line_->addPoint(p);
00477     }
00478   }
00479   
00480   // need msg to be initialized beforehand
00481   void SimpleCircleFacingVisualizer::updateTextUnderLine()
00482   {
00483     Ogre::Vector3 text_position(size_ * cos(45.0 / 180.0 * M_PI)
00484                                 + size_ / 2.0,
00485                                 size_ * sin(45.0 / 180.0 * M_PI)
00486                                 + size_ / 2.0,
00487                                 0);
00488     target_text_node_->setPosition(text_position);
00489     Ogre::Vector3 msg_size = msg_->GetAABB().getSize();
00490     text_under_line_->clear();
00491     text_under_line_->setColor(color_.r, color_.g, color_.b, color_.a);
00492     
00493     text_under_line_->setLineWidth(0.01);
00494     text_under_line_->setNumLines(1);
00495     text_under_line_->setMaxPointsPerLine(1024);
00496     Ogre::Vector3 A(size_ * cos(45.0 / 180.0 * M_PI),
00497                     size_ * sin(45.0 / 180.0 * M_PI),
00498                     0);
00499     Ogre::Vector3 B(text_position + Ogre::Vector3(- size_ / 4.0, 0, 0));
00500     Ogre::Vector3 C(text_position + Ogre::Vector3(msg_size[0], 0, 0));
00501     text_under_line_->addPoint(A);
00502     text_under_line_->addPoint(B);
00503     text_under_line_->addPoint(C);
00504   }
00505 
00506   void SimpleCircleFacingVisualizer::setSize(double size)
00507   {
00508     FacingObject::setSize(size);
00509     updateLine();
00510     updateText();
00511     updateTextUnderLine();
00512   }
00513 
00514   void SimpleCircleFacingVisualizer::setEnable(bool enable)
00515   {
00516     FacingObject::setEnable(enable);
00517     msg_->setVisible(enable);
00518     line_->getSceneNode()->setVisible(enable);
00519     text_under_line_->getSceneNode()->setVisible(enable);
00520   }
00521   
00522   void SimpleCircleFacingVisualizer::updateText()
00523   {
00524     msg_->setCaption(text_);
00525     msg_->setCharacterHeight(std::max(0.2 * size_, minimum_font_size));
00526   }
00527 
00528   void SimpleCircleFacingVisualizer::setText(std::string text)
00529   {
00530     text_ = text;
00531     updateTextUnderLine();
00532     updateText();
00533   }
00534 
00535   void SimpleCircleFacingVisualizer::updateColor()
00536   {
00537     msg_->setColor(color_);
00538     line_->setColor(color_.r, color_.g, color_.b, color_.a);
00539     text_under_line_->setColor(color_.r, color_.g, color_.b, color_.a);
00540     updateArrowsObjects(color_);
00541   }
00542   
00543   FacingTexturedObject::FacingTexturedObject(Ogre::SceneManager* manager,
00544                                              Ogre::SceneNode* parent,
00545                                              double size):
00546     FacingObject(manager, parent, size)
00547   {
00548     rviz::UniformStringStream ss;
00549     static int count = 0;
00550     ss << "FacingVisualizer" << count++;
00551     texture_object_.reset(new TextureObject(128, 128, ss.str()));
00552     square_object_.reset(new SquareObject(manager, size, 0,
00553                                           texture_object_->getMaterialName()));
00554     node_->attachObject(square_object_->getManualObject());
00555   }
00556   
00557 
00558   void FacingTexturedObject::setSize(double size)
00559   {
00560     FacingObject::setSize(size);
00561     square_object_->setOuterRadius(size_);
00562     square_object_->rebuildPolygon();
00563   }
00564 
00565   GISCircleVisualizer::GISCircleVisualizer(Ogre::SceneManager* manager,
00566                                            Ogre::SceneNode* parent,
00567                                            double size,
00568                                            std::string text):
00569     FacingTexturedObject(manager, parent, size), text_(text)
00570   {
00571 
00572   }
00573   
00574   void GISCircleVisualizer::update(float wall_dt, float ros_dt)
00575   {
00576     ros::WallTime now = ros::WallTime::now();
00577     std::string text = text_ + " ";
00578     {
00579       ScopedPixelBuffer buffer = texture_object_->getBuffer();
00580       QColor transparent(0, 0, 0, 0);
00581       QColor foreground = rviz::ogreToQt(color_);
00582       QColor white(255, 255, 255, color_.a * 255);
00583       QImage Hud = buffer.getQImage(128, 128, transparent);
00584       double line_width = 5;
00585       double inner_line_width = 10;
00586       double l = 128;
00587       //double cx = l / 2 - line_width / 4.0;
00588       double cx = l / 2;
00589       //double cy = l / 2 - line_width / 4.0;
00590       double cy = l / 2;
00591       double r = 48;
00592       double inner_r = 40;
00593       double mouse_r = 30;
00594       double mouse_cy_offset = 5;
00595       
00596       QPainter painter( &Hud );
00597       painter.setRenderHint(QPainter::Antialiasing, true);
00598       painter.setPen(QPen(foreground, line_width, Qt::SolidLine));
00599       painter.setBrush(white);
00600       painter.drawEllipse(line_width / 2.0, line_width / 2.0,
00601                           l - line_width, l - line_width);
00602       double offset_rate = fmod(now.toSec(), 10) / 10.0;
00603       double theta_offset = offset_rate * M_PI * 2.0;
00604       for (size_t ci = 0; ci < text.length(); ci++) {
00605         double theta = M_PI * 2.0 / text.length() * ci + theta_offset;
00606         painter.save();
00607         QFont font("DejaVu Sans Mono");
00608         font.setPointSize(10);
00609         font.setBold(true);
00610         painter.setFont(font);
00611         painter.translate(cx + r * cos(theta),
00612                           cy + r * sin(theta));
00613         painter.rotate(theta / M_PI * 180 + 90);
00614         painter.drawText(0, 0, text.substr(ci, 1).c_str());
00615         painter.restore();
00616       }
00617       painter.setPen(QPen(foreground, inner_line_width, Qt::SolidLine));
00618       painter.setBrush(transparent);
00619       painter.drawEllipse(cx - inner_r, cy - inner_r,
00620                           inner_r * 2, inner_r * 2);
00621       double mouse_c_x = cx;
00622       double mouse_c_y = cy - mouse_cy_offset;
00623       double start_angle = -25 * M_PI / 180;
00624       double end_angle = -125 * M_PI / 180;
00625       painter.setPen(QPen(foreground, line_width, Qt::SolidLine));
00626       painter.drawChord(mouse_c_x - mouse_r, mouse_c_y - mouse_r,
00627                         2.0 * mouse_r, 2.0 * mouse_r,
00628                         start_angle * 180 / M_PI * 16,
00629                         end_angle * 180 / M_PI * 16);
00630       painter.end();
00631     }
00632   }
00633 
00634   void GISCircleVisualizer::setAnonymous(bool anonymous)
00635   {
00636     anonymous_ = anonymous;
00637     if (!anonymous_) {
00638       square_object_->setInnerRadius(size_ * 0.6);
00639     }
00640     else {
00641       square_object_->setInnerRadius(0.0);
00642       
00643     }
00644     square_object_->rebuildPolygon();
00645   }
00646 
00647   void GISCircleVisualizer::setSize(double size)
00648   {
00649     FacingObject::setSize(size);
00650     square_object_->setOuterRadius(size_);
00651     if (!anonymous_) {
00652       square_object_->setInnerRadius(size_ * 0.6);
00653     }
00654     else {
00655       square_object_->setInnerRadius(0.0);
00656     }
00657     square_object_->rebuildPolygon();
00658   }
00659 
00660   
00661 }


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Mon Oct 6 2014 01:18:44