mesh_shape.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 "mesh_shape.h"
00031 
00032 #include <OgreMeshManager.h>
00033 #include <OgreSceneManager.h>
00034 #include <OgreSceneNode.h>
00035 #include <OgreEntity.h>
00036 #include <OgreMaterialManager.h>
00037 #include <OgreManualObject.h>
00038 
00039 #include <ros/console.h>
00040 #include <boost/lexical_cast.hpp>
00041 
00042 namespace rviz
00043 {
00044 
00045 MeshShape::MeshShape(Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node )
00046 : Shape( Shape::Mesh, scene_manager, parent_node )
00047 , started_(false)
00048 {
00049   static uint32_t count = 0;
00050   manual_object_ = scene_manager->createManualObject("MeshShape_ManualObject" + boost::lexical_cast<std::string>(count++)); 
00051   material_->setCullingMode(Ogre::CULL_NONE);
00052 }
00053 
00054 MeshShape::~MeshShape()
00055 {
00056   clear();
00057   scene_manager_->destroyManualObject(manual_object_);
00058 }
00059 
00060 void MeshShape::estimateVertexCount(size_t vcount)
00061 {
00062   if (entity_ == NULL && started_ == false)
00063     manual_object_->estimateVertexCount(vcount);
00064 }
00065 
00066 void MeshShape::beginTriangles()
00067 {
00068   if (!started_ && entity_)
00069   {
00070     ROS_WARN("Cannot modify mesh once construction is complete");
00071     return;
00072   }
00073   
00074   if (!started_)
00075   {
00076     started_ = true;
00077     manual_object_->begin(material_name_, Ogre::RenderOperation::OT_TRIANGLE_LIST);
00078   }
00079 }
00080 
00081 void MeshShape::addVertex(const Ogre::Vector3& position)
00082 {
00083   beginTriangles();
00084   manual_object_->position(position);
00085 }
00086 
00087 void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal)
00088 {
00089   beginTriangles();
00090   manual_object_->position(position);
00091   manual_object_->normal(normal);
00092 }
00093 
00094 void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal, const Ogre::ColourValue &color)
00095 {
00096   beginTriangles();
00097   manual_object_->position(position);
00098   manual_object_->normal(normal);
00099   manual_object_->colour(color);
00100 }
00101 
00102 void MeshShape::addNormal(const Ogre::Vector3& normal)
00103 {
00104   manual_object_->normal(normal);
00105 }
00106 
00107 void MeshShape::addColor(const Ogre::ColourValue &color)
00108 {
00109   manual_object_->colour(color);
00110 }
00111 
00112 void MeshShape::addTriangle(unsigned int v1, unsigned int v2, unsigned int v3)
00113 {
00114   manual_object_->triangle(v1, v2, v3);
00115 }
00116 
00117 void MeshShape::endTriangles()
00118 {
00119   if (started_)
00120   {
00121     started_ = false;
00122     manual_object_->end();
00123     static uint32_t count = 0;
00124     std::string name = "ConvertedMeshShape@" + boost::lexical_cast<std::string>(count++);
00125     manual_object_->convertToMesh(name);
00126     entity_ = scene_manager_->createEntity(name);
00127     if (entity_)
00128     {
00129       entity_->setMaterialName(material_name_);
00130       offset_node_->attachObject(entity_);
00131     }
00132     else
00133       ROS_ERROR("Unable to construct triangle mesh");
00134   }
00135   else
00136     ROS_ERROR("No triangles added");
00137 }
00138 
00139 void MeshShape::clear()
00140 {
00141   if (entity_)
00142   {
00143     entity_->detachFromParent();
00144     Ogre::MeshManager::getSingleton().remove(entity_->getMesh()->getName());
00145     scene_manager_->destroyEntity( entity_ );
00146     entity_ = NULL;
00147   }
00148   manual_object_->clear();
00149   started_ = false;
00150 }
00151 
00152 } // namespace rviz
00153 


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