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 "shape.h"
31 #include <ros/assert.h>
32 
34 #include <OgreSceneManager.h>
35 #include <OgreSceneNode.h>
37 #include <OgreQuaternion.h>
38 #include <OgreEntity.h>
39 #include <OgreMaterialManager.h>
40 #include <OgreTextureManager.h>
41 #include <OgreTechnique.h>
42 #include <stdint.h>
43 
44 namespace rviz
45 {
46 Ogre::Entity* Shape::createEntity(const std::string& name, Type type, Ogre::SceneManager* scene_manager)
47 {
48  if (type == Mesh)
49  return nullptr; // the entity is initialized after the vertex data was specified
50 
51  std::string mesh_name;
52  switch (type)
53  {
54  case Cone:
55  mesh_name = "rviz_cone.mesh";
56  break;
57 
58  case Cube:
59  mesh_name = "rviz_cube.mesh";
60  break;
61 
62  case Cylinder:
63  mesh_name = "rviz_cylinder.mesh";
64  break;
65 
66  case Sphere:
67  mesh_name = "rviz_sphere.mesh";
68  break;
69 
70  default:
71  ROS_BREAK();
72  }
73 
74  return scene_manager->createEntity(name, mesh_name);
75 }
76 
77 Shape::Shape(Type type, Ogre::SceneManager* scene_manager, Ogre::SceneNode* parent_node)
78  : Object(scene_manager), type_(type)
79 {
80  static uint32_t count = 0;
81  std::stringstream ss;
82  ss << "Shape" << count++;
83 
84  entity_ = createEntity(ss.str(), type, scene_manager);
85 
86  if (!parent_node)
87  {
88  parent_node = scene_manager_->getRootSceneNode();
89  }
90 
91  scene_node_ = parent_node->createChildSceneNode();
92  offset_node_ = scene_node_->createChildSceneNode();
93  if (entity_)
94  offset_node_->attachObject(entity_);
95 
96  ss << "Material";
97  material_name_ = ss.str();
98  material_ = Ogre::MaterialManager::getSingleton().create(
99  material_name_, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
100  material_->setReceiveShadows(false);
101  material_->getTechnique(0)->setLightingEnabled(true);
102  material_->getTechnique(0)->setAmbient(0.5, 0.5, 0.5);
103 
104  if (entity_)
105  entity_->setMaterial(material_);
106 }
107 
109 {
110  scene_manager_->destroySceneNode(scene_node_);
111  scene_manager_->destroySceneNode(offset_node_);
112 
113  if (entity_)
114  scene_manager_->destroyEntity(entity_);
115 
116  Ogre::MaterialManager::getSingleton().remove(material_->getName());
117 }
118 
119 void Shape::setColor(const Ogre::ColourValue& c)
120 {
121  material_->getTechnique(0)->setAmbient(c * 0.5);
122  material_->getTechnique(0)->setDiffuse(c);
123 
124  if (c.a < 0.9998)
125  {
126  material_->getTechnique(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
127  material_->getTechnique(0)->setDepthWriteEnabled(false);
128  }
129  else
130  {
131  material_->getTechnique(0)->setSceneBlending(Ogre::SBT_REPLACE);
132  material_->getTechnique(0)->setDepthWriteEnabled(true);
133  }
134 }
135 
136 void Shape::setColor(float r, float g, float b, float a)
137 {
138  setColor(Ogre::ColourValue(r, g, b, a));
139 }
140 
141 void Shape::setOffset(const Ogre::Vector3& offset)
142 {
143  offset_node_->setPosition(offset);
144 }
145 
146 void Shape::setPosition(const Ogre::Vector3& position)
147 {
148  scene_node_->setPosition(position);
149 }
150 
151 void Shape::setOrientation(const Ogre::Quaternion& orientation)
152 {
153  scene_node_->setOrientation(orientation);
154 }
155 
156 void Shape::setScale(const Ogre::Vector3& scale)
157 {
158  scene_node_->setScale(scale);
159 }
160 
161 const Ogre::Vector3& Shape::getPosition()
162 {
163  return scene_node_->getPosition();
164 }
165 
166 const Ogre::Quaternion& Shape::getOrientation()
167 {
168  return scene_node_->getOrientation();
169 }
170 
171 void Shape::setUserData(const Ogre::Any& data)
172 {
173  if (entity_)
174  entity_->getUserObjectBindings().setUserAny(data);
175  else
176  ROS_ERROR("Shape not yet fully constructed. Cannot set user data. Did you add triangles to the mesh "
177  "already?");
178 }
179 
180 } // namespace rviz
shape.h
rviz::Shape::Cylinder
@ Cylinder
Definition: shape.h:58
ROS_BREAK
#define ROS_BREAK()
rviz::Shape::offset_node_
Ogre::SceneNode * offset_node_
Definition: shape.h:125
rviz::Shape::setColor
void setColor(float r, float g, float b, float a) override
Set the color of the object. Values are in the range [0, 1].
Definition: shape.cpp:136
rviz::Shape::Shape
Shape(Type shape_type, Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node=nullptr)
Constructor.
Definition: shape.cpp:77
version_check.h
rviz::Shape::material_
Ogre::MaterialPtr material_
Definition: shape.h:127
rviz::Shape::getPosition
const Ogre::Vector3 & getPosition() override
Get the local position of this object.
Definition: shape.cpp:161
rviz::Shape::material_name_
std::string material_name_
Definition: shape.h:128
rviz::Object::scene_manager_
Ogre::SceneManager * scene_manager_
Ogre scene manager this object is part of.
Definition: object.h:103
rviz::Shape::Cone
@ Cone
Definition: shape.h:56
rviz::Shape::Type
Type
Definition: shape.h:54
rviz::Shape::~Shape
~Shape() override
Definition: shape.cpp:108
rviz::Shape::entity_
Ogre::Entity * entity_
Definition: shape.h:126
rviz
Definition: add_display_dialog.cpp:54
rviz::Shape::createEntity
static Ogre::Entity * createEntity(const std::string &name, Type shape_type, Ogre::SceneManager *scene_manager)
Definition: shape.cpp:46
rviz::Shape::setOffset
void setOffset(const Ogre::Vector3 &offset)
Set the offset for this shape.
Definition: shape.cpp:141
ogre_vector.h
rviz::Shape::Sphere
@ Sphere
Definition: shape.h:59
rviz::Shape::setUserData
void setUserData(const Ogre::Any &data) override
Sets user data on all ogre objects we own.
Definition: shape.cpp:171
rviz::Shape::Mesh
@ Mesh
Definition: shape.h:60
ROS_ERROR
#define ROS_ERROR(...)
rviz::Shape::getOrientation
const Ogre::Quaternion & getOrientation() override
Get the local orientation of this object.
Definition: shape.cpp:166
rviz::Object
Base class for visible objects, providing a minimal generic interface.
Definition: object.h:47
rviz::Shape::setScale
void setScale(const Ogre::Vector3 &scale) override
Set the scale of the object. Always relative to the identity orientation of the object.
Definition: shape.cpp:156
assert.h
rviz::Shape::setPosition
void setPosition(const Ogre::Vector3 &position) override
Set the position of this object.
Definition: shape.cpp:146
rviz::Shape::setOrientation
void setOrientation(const Ogre::Quaternion &orientation) override
Set the orientation of the object.
Definition: shape.cpp:151
rviz::Shape::scene_node_
Ogre::SceneNode * scene_node_
Definition: shape.h:124
rviz::Shape::Cube
@ Cube
Definition: shape.h:57


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Dec 13 2024 03:31:03