mesh_shape.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "mesh_shape.h"
31 
32 #include <OgreMesh.h>
33 #include <OgreMeshManager.h>
34 #include <OgreSceneManager.h>
35 #include <OgreSceneNode.h>
36 #include <OgreEntity.h>
37 #include <OgreMaterialManager.h>
38 #include <OgreManualObject.h>
39 
40 #include <ros/console.h>
41 #include <boost/lexical_cast.hpp>
42 
43 namespace rviz
44 {
45 
46 MeshShape::MeshShape(Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node )
47 : Shape( Shape::Mesh, scene_manager, parent_node )
48 , started_(false)
49 {
50  static uint32_t count = 0;
51  manual_object_ = scene_manager->createManualObject("MeshShape_ManualObject" + boost::lexical_cast<std::string>(count++));
52  material_->setCullingMode(Ogre::CULL_NONE);
53 }
54 
56 {
57  clear();
58  scene_manager_->destroyManualObject(manual_object_);
59 }
60 
61 void MeshShape::estimateVertexCount(size_t vcount)
62 {
63  if (entity_ == NULL && started_ == false)
64  manual_object_->estimateVertexCount(vcount);
65 }
66 
68 {
69  if (!started_ && entity_)
70  {
71  ROS_WARN("Cannot modify mesh once construction is complete");
72  return;
73  }
74 
75  if (!started_)
76  {
77  started_ = true;
78  manual_object_->begin(material_name_, Ogre::RenderOperation::OT_TRIANGLE_LIST);
79  }
80 }
81 
82 void MeshShape::addVertex(const Ogre::Vector3& position)
83 {
85  manual_object_->position(position);
86 }
87 
88 void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal)
89 {
91  manual_object_->position(position);
92  manual_object_->normal(normal);
93 }
94 
95 void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal, const Ogre::ColourValue &color)
96 {
98  manual_object_->position(position);
99  manual_object_->normal(normal);
100  manual_object_->colour(color);
101 }
102 
103 void MeshShape::addNormal(const Ogre::Vector3& normal)
104 {
105  manual_object_->normal(normal);
106 }
107 
108 void MeshShape::addColor(const Ogre::ColourValue &color)
109 {
110  manual_object_->colour(color);
111 }
112 
113 void MeshShape::addTriangle(unsigned int v1, unsigned int v2, unsigned int v3)
114 {
115  manual_object_->triangle(v1, v2, v3);
116 }
117 
119 {
120  if (started_)
121  {
122  started_ = false;
123  manual_object_->end();
124  static uint32_t count = 0;
125  std::string name = "ConvertedMeshShape@" + boost::lexical_cast<std::string>(count++);
126  manual_object_->convertToMesh(name);
127  entity_ = scene_manager_->createEntity(name);
128  if (entity_)
129  {
130  entity_->setMaterialName(material_name_);
131  offset_node_->attachObject(entity_);
132  }
133  else
134  ROS_ERROR("Unable to construct triangle mesh");
135  }
136  else
137  ROS_ERROR("No triangles added");
138 }
139 
141 {
142  if (entity_)
143  {
144  entity_->detachFromParent();
145  Ogre::MeshManager::getSingleton().remove(entity_->getMesh()->getName());
146  scene_manager_->destroyEntity( entity_ );
147  entity_ = NULL;
148  }
149  manual_object_->clear();
150  started_ = false;
151 }
152 
153 } // namespace rviz
154 
void addColor(const Ogre::ColourValue &color)
Add color for a vertex.
Definition: mesh_shape.cpp:108
#define NULL
Definition: global.h:37
void endTriangles()
Notify that the set of triangles to add is complete. No more triangles can be added, beginTriangles() can no longer be called unless clear() was called.
Definition: mesh_shape.cpp:118
Ogre::SceneNode * offset_node_
Definition: shape.h:112
Ogre::Entity * entity_
Definition: shape.h:113
std::string material_name_
Definition: shape.h:115
Ogre::MaterialPtr material_
Definition: shape.h:114
void addVertex(const Ogre::Vector3 &position)
Add a vertex to the mesh (no normal defined). If using this function only (not using addTriangle()) i...
Definition: mesh_shape.cpp:82
#define ROS_WARN(...)
void clear()
Clear the mesh.
Definition: mesh_shape.cpp:140
void addTriangle(unsigned int p1, unsigned int p2, unsigned int p3)
Add a triangle by indexing in the defined vertices.
Definition: mesh_shape.cpp:113
MeshShape(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node=NULL)
Constructor.
Definition: mesh_shape.cpp:46
Ogre::ManualObject * manual_object_
Definition: mesh_shape.h:137
void addNormal(const Ogre::Vector3 &normal)
Add normal for a vertex.
Definition: mesh_shape.cpp:103
void estimateVertexCount(size_t vcount)
Definition: mesh_shape.cpp:61
void beginTriangles()
Start adding triangles to the mesh.
Definition: mesh_shape.cpp:67
Ogre::SceneManager * scene_manager_
Ogre scene manager this object is part of.
Definition: object.h:103
#define ROS_ERROR(...)
virtual ~MeshShape()
Definition: mesh_shape.cpp:55


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51