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 MeshShape::MeshShape(Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node)
46  : Shape(Shape::Mesh, scene_manager, parent_node), started_(false)
47 {
48  static uint32_t count = 0;
49  manual_object_ = scene_manager->createManualObject("MeshShape_ManualObject" +
50  boost::lexical_cast<std::string>(count++));
51  material_->setCullingMode(Ogre::CULL_NONE);
52 }
53 
55 {
56  clear();
57  scene_manager_->destroyManualObject(manual_object_);
58 }
59 
60 void MeshShape::estimateVertexCount(size_t vcount)
61 {
62  if (entity_ == nullptr && !started_)
63  manual_object_->estimateVertexCount(vcount);
64 }
65 
67 {
68  if (!started_ && entity_)
69  {
70  ROS_WARN("Cannot modify mesh once construction is complete");
71  return;
72  }
73 
74  if (!started_)
75  {
76  started_ = true;
77  manual_object_->begin(material_name_, Ogre::RenderOperation::OT_TRIANGLE_LIST);
78  }
79 }
80 
81 void MeshShape::addVertex(const Ogre::Vector3& position)
82 {
84  manual_object_->position(position);
85 }
86 
87 void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal)
88 {
90  manual_object_->position(position);
91  manual_object_->normal(normal);
92 }
93 
94 void MeshShape::addVertex(const Ogre::Vector3& position,
95  const Ogre::Vector3& normal,
96  const Ogre::ColourValue& color)
97 {
99  manual_object_->position(position);
100  manual_object_->normal(normal);
101  manual_object_->colour(color);
102 }
103 
104 void MeshShape::addNormal(const Ogre::Vector3& normal)
105 {
106  manual_object_->normal(normal);
107 }
108 
109 void MeshShape::addColor(const Ogre::ColourValue& color)
110 {
111  manual_object_->colour(color);
112 }
113 
114 void MeshShape::addTriangle(unsigned int v1, unsigned int v2, unsigned int v3)
115 {
116  manual_object_->triangle(v1, v2, v3);
117 }
118 
120 {
121  if (started_)
122  {
123  started_ = false;
124  manual_object_->end();
125  static uint32_t count = 0;
126  std::string name = "ConvertedMeshShape@" + boost::lexical_cast<std::string>(count++);
127  manual_object_->convertToMesh(name);
128  entity_ = scene_manager_->createEntity(name);
129  if (entity_)
130  {
131  entity_->setMaterialName(material_name_);
132  offset_node_->attachObject(entity_);
133  }
134  else
135  ROS_ERROR("Unable to construct triangle mesh");
136  }
137  else
138  ROS_ERROR("No triangles added");
139 }
140 
142 {
143  if (entity_)
144  {
145  entity_->detachFromParent();
146  Ogre::MeshManager::getSingleton().remove(entity_->getMesh()->getName());
147  scene_manager_->destroyEntity(entity_);
148  entity_ = nullptr;
149  }
150  manual_object_->clear();
151  started_ = false;
152 }
153 
154 } // namespace rviz
void addColor(const Ogre::ColourValue &color)
Add color for a vertex.
Definition: mesh_shape.cpp:109
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:119
Ogre::SceneNode * offset_node_
Definition: shape.h:125
Ogre::Entity * entity_
Definition: shape.h:126
std::string material_name_
Definition: shape.h:128
Ogre::MaterialPtr material_
Definition: shape.h:127
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:81
#define ROS_WARN(...)
void clear()
Clear the mesh.
Definition: mesh_shape.cpp:141
void addTriangle(unsigned int p1, unsigned int p2, unsigned int p3)
Add a triangle by indexing in the defined vertices.
Definition: mesh_shape.cpp:114
Ogre::ManualObject * manual_object_
Definition: mesh_shape.h:137
void addNormal(const Ogre::Vector3 &normal)
Add normal for a vertex.
Definition: mesh_shape.cpp:104
MeshShape(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node=nullptr)
Constructor.
Definition: mesh_shape.cpp:45
void estimateVertexCount(size_t vcount)
Definition: mesh_shape.cpp:60
void beginTriangles()
Start adding triangles to the mesh.
Definition: mesh_shape.cpp:66
Ogre::SceneManager * scene_manager_
Ogre scene manager this object is part of.
Definition: object.h:106
#define ROS_ERROR(...)
~MeshShape() override
Definition: mesh_shape.cpp:54


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:24