grid.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 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 "grid.h"
00031 #include "billboard_line.h"
00032 
00033 #include <OgreSceneManager.h>
00034 #include <OgreSceneNode.h>
00035 #include <OgreVector3.h>
00036 #include <OgreQuaternion.h>
00037 #include <OgreManualObject.h>
00038 #include <OgreMaterialManager.h>
00039 #include <OgreTechnique.h>
00040 
00041 #include <sstream>
00042 
00043 namespace rviz
00044 {
00045 
00046 Grid::Grid( Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node, Style style, uint32_t cell_count, float cell_length, float line_width, const Ogre::ColourValue& color )
00047 : scene_manager_( scene_manager )
00048 , style_(style)
00049 , cell_count_(cell_count)
00050 , cell_length_(cell_length)
00051 , line_width_(line_width)
00052 , height_(0)
00053 , color_(color)
00054 {
00055   static uint32_t gridCount = 0;
00056   std::stringstream ss;
00057   ss << "Grid" << gridCount++;
00058 
00059   manual_object_ = scene_manager_->createManualObject( ss.str() );
00060 
00061   if ( !parent_node )
00062   {
00063     parent_node = scene_manager_->getRootSceneNode();
00064   }
00065 
00066   scene_node_ = parent_node->createChildSceneNode();
00067   scene_node_->attachObject( manual_object_ );
00068 
00069   billboard_line_ = new BillboardLine(scene_manager, scene_node_);
00070 
00071   ss << "Material";
00072   material_ = Ogre::MaterialManager::getSingleton().create( ss.str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
00073   material_->setReceiveShadows(false);
00074   material_->getTechnique(0)->setLightingEnabled(false);
00075 
00076   setColor(color_);
00077 }
00078 
00079 Grid::~Grid()
00080 {
00081   delete billboard_line_;
00082 
00083   scene_manager_->destroySceneNode( scene_node_->getName() );
00084   scene_manager_->destroyManualObject( manual_object_ );
00085 
00086   material_->unload();
00087 }
00088 
00089 void Grid::setCellCount(uint32_t count)
00090 {
00091   cell_count_ = count;
00092 
00093   create();
00094 }
00095 
00096 void Grid::setCellLength(float len)
00097 {
00098   cell_length_ = len;
00099 
00100   create();
00101 }
00102 
00103 void Grid::setLineWidth(float width)
00104 {
00105   line_width_ = width;
00106 
00107   create();
00108 }
00109 
00110 void Grid::setColor(const Ogre::ColourValue& color)
00111 {
00112   color_ = color;
00113 
00114   if ( color_.a < 0.9998 )
00115   {
00116     material_->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA );
00117     material_->setDepthWriteEnabled( false );
00118   }
00119   else
00120   {
00121     material_->setSceneBlending( Ogre::SBT_REPLACE );
00122     material_->setDepthWriteEnabled( true );
00123   }
00124 
00125   create();
00126 }
00127 
00128 void Grid::setStyle(Style style)
00129 {
00130   style_ = style;
00131 
00132   create();
00133 }
00134 
00135 void Grid::setHeight(uint32_t height)
00136 {
00137   height_ = height;
00138 
00139   create();
00140 }
00141 
00142 void Grid::create()
00143 {
00144   manual_object_->clear();
00145   billboard_line_->clear();
00146 
00147   float extent = (cell_length_*((double)cell_count_))/2;
00148 
00149   if (style_ == Billboards)
00150   {
00151     billboard_line_->setColor(color_.r, color_.g, color_.b, color_.a);
00152     billboard_line_->setLineWidth(line_width_);
00153     billboard_line_->setMaxPointsPerLine(2);
00154     billboard_line_->setNumLines((cell_count_+1) * 2 * (height_ + 1)
00155                               + ((cell_count_ + 1) * (cell_count_ + 1)) * height_);
00156   }
00157   else
00158   {
00159     manual_object_->estimateVertexCount( cell_count_ * 4 * (height_ + 1) + ((cell_count_ + 1) * (cell_count_ + 1) * height_));
00160     manual_object_->begin( material_->getName(), Ogre::RenderOperation::OT_LINE_LIST );
00161   }
00162 
00163   for (uint32_t h = 0; h <= height_; ++h)
00164   {
00165     float h_real = (height_ / 2.0f - (float)h) * cell_length_;
00166     for( uint32_t i = 0; i <= cell_count_; i++ )
00167     {
00168       float inc = extent - ( i * cell_length_ );
00169 
00170       Ogre::Vector3 p1(inc, h_real, -extent);
00171       Ogre::Vector3 p2(inc, h_real, extent);
00172       Ogre::Vector3 p3(-extent, h_real, inc);
00173       Ogre::Vector3 p4(extent, h_real, inc);
00174 
00175       if (style_ == Billboards)
00176       {
00177         if (h != 0 || i != 0)
00178         {
00179           billboard_line_->newLine();
00180         }
00181 
00182         billboard_line_->addPoint(p1);
00183         billboard_line_->addPoint(p2);
00184 
00185         billboard_line_->newLine();
00186 
00187         billboard_line_->addPoint(p3);
00188         billboard_line_->addPoint(p4);
00189       }
00190       else
00191       {
00192         manual_object_->position(p1);
00193         manual_object_->colour( color_ );
00194         manual_object_->position(p2);
00195         manual_object_->colour( color_ );
00196 
00197         manual_object_->position(p3);
00198         manual_object_->colour( color_ );
00199         manual_object_->position(p4);
00200         manual_object_->colour( color_ );
00201       }
00202     }
00203   }
00204 
00205   if (height_ > 0)
00206   {
00207     for (uint32_t x = 0; x <= cell_count_; ++x)
00208     {
00209       for (uint32_t z = 0; z <= cell_count_; ++z)
00210       {
00211         float x_real = extent - x * cell_length_;
00212         float z_real = extent - z * cell_length_;
00213 
00214         float y_top = (height_ / 2.0f) * cell_length_;
00215         float y_bottom = -y_top;
00216 
00217         if (style_ == Billboards)
00218         {
00219           billboard_line_->newLine();
00220 
00221           billboard_line_->addPoint( Ogre::Vector3(x_real, y_bottom, z_real) );
00222           billboard_line_->addPoint( Ogre::Vector3(x_real, y_top, z_real) );
00223         }
00224         else
00225         {
00226           manual_object_->position( x_real, y_bottom, z_real );
00227           manual_object_->colour( color_ );
00228           manual_object_->position(x_real, y_top, z_real);
00229           manual_object_->colour( color_ );
00230         }
00231       }
00232     }
00233   }
00234 
00235   if (style_ == Lines)
00236   {
00237     manual_object_->end();
00238   }
00239 }
00240 
00241 void Grid::setUserData( const Ogre::Any& data )
00242 {
00243   manual_object_->setUserAny( data );
00244 }
00245 
00246 } // namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Jun 6 2019 18:02:15