moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
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 <cstdint>
32
33#include <OgreMesh.h>
34#include <OgreMeshManager.h>
35#include <OgreSceneManager.h>
36#include <OgreSceneNode.h>
37#include <OgreEntity.h>
38#include <OgreMaterialManager.h>
39#include <OgreManualObject.h>
40
41#include <rviz_common/logging.hpp>
42#include <string>
43
44namespace rviz_rendering
45{
46MeshShape::MeshShape(Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node)
47 : Shape(Shape::Mesh, scene_manager, parent_node), started_(false)
48{
49 static uint32_t count = 0;
50 manual_object_ = scene_manager->createManualObject("MeshShape_ManualObject" + std::to_string(count++));
51 material_->setCullingMode(Ogre::CULL_NONE);
52}
53
55{
56 clear();
57 scene_manager_->destroyManualObject(manual_object_);
58}
59
61{
62 if (entity_ == nullptr && !started_)
63 manual_object_->estimateVertexCount(vcount);
64}
65
67{
68 if (!started_ && entity_)
69 {
70 RVIZ_COMMON_LOG_WARNING("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, "rviz_rendering");
78 }
79}
80
81void MeshShape::addVertex(const Ogre::Vector3& position)
82{
84 manual_object_->position(position);
85}
86
87void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal)
88{
90 manual_object_->position(position);
91 manual_object_->normal(normal);
92}
93
94void MeshShape::addVertex(const Ogre::Vector3& position, const Ogre::Vector3& normal, const Ogre::ColourValue& color)
95{
97 manual_object_->position(position);
98 manual_object_->normal(normal);
99 manual_object_->colour(color);
100}
101
102void MeshShape::addNormal(const Ogre::Vector3& normal)
103{
104 manual_object_->normal(normal);
105}
106
107void MeshShape::addColor(const Ogre::ColourValue& color)
108{
109 manual_object_->colour(color);
110}
111
112void MeshShape::addTriangle(unsigned int v1, unsigned int v2, unsigned int v3)
113{
114 manual_object_->triangle(v1, v2, v3);
115}
116
118{
119 if (started_)
120 {
121 started_ = false;
122 manual_object_->end();
123 static uint32_t count = 0;
124 std::string name = "ConvertedMeshShape@" + std::to_string(count++);
125 manual_object_->convertToMesh(name);
126 entity_ = scene_manager_->createEntity(name);
127 if (entity_)
128 {
129 entity_->setMaterialName(material_name_, "rviz_rendering");
130 offset_node_->attachObject(entity_);
131 }
132 else
133 {
134 RVIZ_COMMON_LOG_ERROR("Unable to construct triangle mesh");
135 }
136 }
137 else
138 {
139 RVIZ_COMMON_LOG_ERROR("No triangles added");
140 }
141}
142
144{
145 if (entity_)
146 {
147 entity_->detachFromParent();
148 const auto& mesh_name = entity_->getMesh()->getName();
149 if (Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().getByName(mesh_name))
150 {
151 Ogre::MeshManager::getSingleton().remove(mesh);
152 }
153 scene_manager_->destroyEntity(entity_);
154 entity_ = nullptr;
155 }
156 manual_object_->clear();
157 started_ = false;
158}
159
160} // namespace rviz_rendering
void addColor(const Ogre::ColourValue &color)
Add color for a vertex.
void beginTriangles()
Start adding triangles to the mesh.
void addTriangle(unsigned int p1, unsigned int p2, unsigned int p3)
Add a triangle by indexing in the defined vertices.
void addVertex(const Ogre::Vector3 &position)
Add a vertex to the mesh (no normal defined). If using this function only (not using addTriangle()) i...
MeshShape(Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node=nullptr)
Constructor.
void clear()
Clear the mesh.
void estimateVertexCount(size_t vcount)
void endTriangles()
Notify that the set of triangles to add is complete. No more triangles can be added,...
void addNormal(const Ogre::Vector3 &normal)
Add normal for a vertex.