PersonVisual.cpp
Go to the documentation of this file.
1 /*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2013-2015, Timm Linder, Social Robotics Lab, University of Freiburg
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * * Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include <rviz/mesh_loader.h>
32 #include <ros/console.h>
33 #include <ros/package.h>
34 
35 #include <OgreSceneManager.h>
36 #include <OgreSubEntity.h>
37 #include <OgreMaterialManager.h>
38 #include <OgreTextureManager.h>
39 #include <OgreTechnique.h>
40 #include <OgreAnimation.h>
41 
43 
44 
45 namespace fs = boost::filesystem;
46 
47 namespace tuw_object_rviz {
48 
49 /*
50  * Generic Person Visual
51  */
53  m_sceneManager(args.sceneManager)
54 {
56  m_sceneNode = args.parentNode->createChildSceneNode();
57 
58  Ogre::Vector3 scale(1,1,1);
59  m_sceneNode->setScale(scale);
60 }
61 
63  m_sceneManager->destroySceneNode(m_sceneNode->getName());
64 };
65 
66 void PersonVisual::setPosition(const Ogre::Vector3& position) {
67  m_sceneNode->setPosition(position);
68 }
69 
70 const Ogre::Vector3& PersonVisual::getPosition() const {
71  return m_sceneNode->getPosition();
72 }
73 
74 void PersonVisual::setOrientation(const Ogre::Quaternion& orientation) {
75  m_sceneNode->setOrientation(orientation);
76 }
77 
78 const Ogre::Quaternion& PersonVisual::getOrientation() const {
79  return m_sceneNode->getOrientation();
80 }
81 
82 void PersonVisual::setScalingFactor(double scalingFactor) {
83  m_sceneNode->setScale(scalingFactor, scalingFactor, scalingFactor);
84 }
85 
86 void PersonVisual::setVisible(bool visible) {
87  m_sceneNode->setVisible(visible, true);
88 }
89 
91  return m_parentSceneNode;
92 }
93 
94 void PersonVisual::update(float deltaTime) {}
95 
96 /*
97  * CylinderPersonVisual
98  */
100 {
103 
104  const float headDiameter = 0.4;
105  const float totalHeight = getHeight();
106  const float cylinderHeight = totalHeight - headDiameter;
107 
108  m_bodyShape->setScale(Ogre::Vector3(headDiameter, headDiameter, cylinderHeight));
109  m_headShape->setScale(Ogre::Vector3(headDiameter, headDiameter, headDiameter));
110 
111  m_bodyShape->setPosition(Ogre::Vector3(0, 0, cylinderHeight / 2 - totalHeight / 2));
112  m_headShape->setPosition(Ogre::Vector3(0, 0, totalHeight / 2 - headDiameter / 2 ));
113 }
114 
116  delete m_bodyShape;
117  delete m_headShape;
118 }
119 
120 void CylinderPersonVisual::setColor(const Ogre::ColourValue& c) {
121  m_bodyShape->setColor(c);
122  m_headShape->setColor(c);
123  m_color = c;
124 }
125 
126 Ogre::ColourValue& CylinderPersonVisual::getColor()
127 {
128  return m_color;
129 }
130 
132  return 1.75;
133 }
134 
135 /*
136  * Bounding Box Visual
137  */
138 
140 {
141  m_width = width; m_height = height; m_scalingFactor = scalingFactor; m_lineWidth = 0.03;
142  m_wireframe = NULL;
144 }
145 
147  delete m_wireframe;
148 }
149 
150 void BoundingBoxPersonVisual::setColor(const Ogre::ColourValue& c) {
151  m_wireframe->setColor(c.r, c.g, c.b, c.a);
152  m_color = c;
153 }
154 
156 {
157  return m_color;
158 }
159 
161  return m_height;
162 }
163 
165  m_wireframe->setLineWidth(lineWidth);
166 }
167 
169  delete m_wireframe;
171 
175 
177  Ogre::Vector3 bottomLeft(0, -w, 0), bottomRight(0, 0, 0), topLeft(0, -w, h), topRight(0, 0, h);
178  Ogre::Vector3 rear(w, 0, 0);
179 
180  // Front quad
181  m_wireframe->addPoint(bottomLeft); m_wireframe->addPoint(bottomRight);
182  m_wireframe->newLine(); m_wireframe->addPoint(bottomRight); m_wireframe->addPoint(topRight);
183  m_wireframe->newLine(); m_wireframe->addPoint(topRight); m_wireframe->addPoint(topLeft);
184  m_wireframe->newLine(); m_wireframe->addPoint(topLeft); m_wireframe->addPoint(bottomLeft);
185 
186  // Rear quad
187  m_wireframe->newLine(); m_wireframe->addPoint(bottomLeft + rear); m_wireframe->addPoint(bottomRight + rear);
188  m_wireframe->newLine(); m_wireframe->addPoint(bottomRight + rear); m_wireframe->addPoint(topRight + rear);
189  m_wireframe->newLine(); m_wireframe->addPoint(topRight + rear); m_wireframe->addPoint(topLeft + rear);
190  m_wireframe->newLine(); m_wireframe->addPoint(topLeft + rear); m_wireframe->addPoint(bottomLeft + rear);
191 
192  // Four connecting lines between front and rear
193  m_wireframe->newLine(); m_wireframe->addPoint(bottomLeft); m_wireframe->addPoint(bottomLeft + rear);
194  m_wireframe->newLine(); m_wireframe->addPoint(bottomRight); m_wireframe->addPoint(bottomRight + rear);
195  m_wireframe->newLine(); m_wireframe->addPoint(topRight); m_wireframe->addPoint(topRight + rear);
196  m_wireframe->newLine(); m_wireframe->addPoint(topLeft); m_wireframe->addPoint(topLeft + rear);
197 
198  m_wireframe->setPosition(Ogre::Vector3(-w/2, w/2, -h/2));
199 }
200 
201 /*
202  * Helper Class for loading skeletons
203  */
204 
205 RosPackagePathResourceLoadingListener::RosPackagePathResourceLoadingListener(const fs::path& parentPath) : _parentPath(parentPath) {
206 }
207 
209 Ogre::DataStreamPtr RosPackagePathResourceLoadingListener::resourceLoading(const Ogre::String &name, const Ogre::String &group, Ogre::Resource *resource) {
210  fs::path absolutePath = _parentPath / name;
211  ROS_INFO_STREAM("RosPackagePathResourceLoadingListener loading resource: " << absolutePath.string());
212 
213  try
214  {
216  _lastResource = retriever.get(absolutePath.string()); // not thread-safe!
217  return Ogre::DataStreamPtr(new Ogre::MemoryDataStream(_lastResource.data.get(), _lastResource.size));
218  }
220  {
221  ROS_ERROR("In RosPackagePathResourceLoadingListener: %s", e.what());
222  return Ogre::DataStreamPtr();
223  }
224 }
225 
226 void RosPackagePathResourceLoadingListener::resourceStreamOpened(const Ogre::String &name, const Ogre::String &group, Ogre::Resource *resource, Ogre::DataStreamPtr& dataStream) {
227 }
228 
229 bool RosPackagePathResourceLoadingListener::resourceCollision(Ogre::Resource *resource, Ogre::ResourceManager *resourceManager) {
230  return false;
231 }
232 
233 
234 /*
235  * Mesh Person Visual
236  */
237 
238 MeshPersonVisual::MeshPersonVisual(const PersonVisualDefaultArgs& args) : PersonVisual(args), entity_(NULL), m_animationState(NULL), m_walkingSpeed(1.0)
239 {
240  m_childSceneNode = m_sceneNode->createChildSceneNode();
241  m_childSceneNode->setVisible(false);
242 
243  std::string meshResource = "package://" ROS_PACKAGE_NAME "/media/animated_walking_man.mesh";
244 
246  fs::path model_path(meshResource);
247  fs::path parent_path(model_path.parent_path());
248 
249  Ogre::ResourceLoadingListener *newListener = new RosPackagePathResourceLoadingListener(parent_path),
250  *oldListener = Ogre::ResourceGroupManager::getSingleton().getLoadingListener();
251 
252  Ogre::ResourceGroupManager::getSingleton().setLoadingListener(newListener);
253  Ogre::ResourceGroupManager::getSingleton().setLoadingListener(oldListener);
254 
255  delete newListener;
256 
257 
258  // Create scene entity
259  static size_t count = 0;
260  std::stringstream ss;
261  ss << "mesh_person_visual" << count++;
262  std::string id = ss.str();
263 
264  entity_ = m_sceneManager->createEntity(id, meshResource);
265  m_childSceneNode->attachObject(entity_);
266 
267  // set up animation
268  setAnimationState("");
269 
270  // set up material
271  ss << "Material";
272  Ogre::MaterialPtr default_material = Ogre::MaterialManager::getSingleton().create( ss.str(), "rviz" );
273  default_material->setReceiveShadows(false);
274  default_material->getTechnique(0)->setLightingEnabled(true);
275  default_material->getTechnique(0)->setAmbient( 0.5, 0.5, 0.5 );
276  materials_.insert( default_material );
277  entity_->setMaterial( default_material );
278 
279  // set position
280  Ogre::Quaternion quat1; quat1.FromAngleAxis(Ogre::Degree(90), Ogre::Vector3(0,1,0));
281  Ogre::Quaternion quat2; quat2.FromAngleAxis(Ogre::Degree(90), Ogre::Vector3(0,0,1));
282  m_childSceneNode->setOrientation(quat1 * quat2);
283 
284  double scaleFactor = 0.243 * 1.75;
285  m_childSceneNode->setScale(Ogre::Vector3(scaleFactor, scaleFactor, scaleFactor));
286  m_childSceneNode->setPosition(Ogre::Vector3(0, 0, -1));
287 
288  m_childSceneNode->setVisible(true);
289 }
290 
292  m_sceneManager->destroyEntity( entity_ );
293 
294  // destroy all the materials we've created
295  std::set<Ogre::MaterialPtr>::iterator it;
296  for ( it = materials_.begin(); it!=materials_.end(); it++ )
297  {
298  Ogre::MaterialPtr material = *it;
299  if (!material.isNull())
300  {
301  material->unload();
302  Ogre::MaterialManager::getSingleton().remove(material->getName());
303  }
304  }
305  materials_.clear();
306 
307  m_sceneManager->destroySceneNode(m_childSceneNode->getName());
308 }
309 
310 void MeshPersonVisual::setColor(const Ogre::ColourValue& c) {
311 
312  m_color = c;
313 
314  Ogre::SceneBlendType blending;
315  bool depth_write;
316 
317  if ( c.a < 0.9998 )
318  {
319  blending = Ogre::SBT_TRANSPARENT_ALPHA;
320  depth_write = false;
321  }
322  else
323  {
324  blending = Ogre::SBT_REPLACE;
325  depth_write = true;
326  }
327 
328  std::set<Ogre::MaterialPtr>::iterator it;
329  for( it = materials_.begin(); it != materials_.end(); it++ )
330  {
331  Ogre::Technique* technique = (*it)->getTechnique( 0 );
332 
333  technique->setAmbient( c.r*0.5, c.g*0.5, c.b*0.5 );
334  technique->setDiffuse( c.r, c.g, c.b, c.a );
335  technique->setSceneBlending( blending );
336  technique->setDepthWriteEnabled( depth_write );
337  technique->setLightingEnabled( true );
338  }
339 }
340 
341 Ogre::ColourValue& MeshPersonVisual::getColor()
342 {
343  return m_color;
344 }
345 
346 void MeshPersonVisual::setAnimationState(const std::string& nameOfAnimationState) {
347  Ogre::AnimationStateSet *animationStates = entity_->getAllAnimationStates();
348  if(animationStates != NULL)
349  {
350  Ogre::AnimationStateIterator animationsIterator = animationStates->getAnimationStateIterator();
351  while (animationsIterator.hasMoreElements())
352  {
353  Ogre::AnimationState *animationState = animationsIterator.getNext();
354  if(animationState->getAnimationName() == nameOfAnimationState || nameOfAnimationState.empty()) {
355  animationState->setLoop(true);
356  animationState->setEnabled(true);
357  m_animationState = animationState;
358  return;
359  }
360  }
361 
362  // Not found. Set first animation state then.
363  ROS_WARN_STREAM_ONCE("Person mesh animation state " << nameOfAnimationState << " does not exist in mesh!");
364  setAnimationState("");
365  }
366 }
367 
368 void MeshPersonVisual::setWalkingSpeed(float walkingSpeed) {
369  m_walkingSpeed = walkingSpeed;
370 }
371 
372 
373 void MeshPersonVisual::update(float deltaTime) {
374  if(m_animationState) {
375  m_animationState->addTime(0.7 * deltaTime * m_walkingSpeed);
376  }
377 }
378 
379 
380 } // end of namespace spencer_tracking_rviz_plugin
#define NULL
void addPoint(const Ogre::Vector3 &point)
Ogre::SceneNode * getParentSceneNode()
Ogre::AnimationState * m_animationState
Definition: PersonVisual.h:166
virtual Ogre::DataStreamPtr resourceLoading(const Ogre::String &name, const Ogre::String &group, Ogre::Resource *resource)
Ogre::ColourValue m_color
Definition: PersonVisual.h:94
void setWalkingSpeed(float walkingSpeed)
virtual void resourceStreamOpened(const Ogre::String &name, const Ogre::String &group, Ogre::Resource *resource, Ogre::DataStreamPtr &dataStream)
virtual void setColor(const Ogre::ColourValue &c)
Ogre::SceneManager * m_sceneManager
Definition: PersonVisual.h:92
virtual void setLineWidth(double lineWidth)
void setPosition(const Ogre::Vector3 &position)
virtual void setPosition(const Ogre::Vector3 &position)
virtual void setColor(float r, float g, float b, float a)
virtual void setColor(float r, float g, float b, float a)
Ogre::SceneNode * m_childSceneNode
Definition: PersonVisual.h:164
Ogre::SceneNode * m_sceneNode
Definition: PersonVisual.h:93
virtual Ogre::ColourValue & getColor()
void setOrientation(const Ogre::Quaternion &orientation)
const Ogre::Vector3 & getPosition() const
boost::shared_array< uint8_t > data
virtual void setPosition(const Ogre::Vector3 &position)
virtual bool resourceCollision(Ogre::Resource *resource, Ogre::ResourceManager *resourceManager)
virtual Ogre::ColourValue & getColor()
PersonVisual(const PersonVisualDefaultArgs &args)
BoundingBoxPersonVisual(const PersonVisualDefaultArgs &args, double height=1.75, double width=0.6, double scalingFactor=1.0)
std::set< Ogre::MaterialPtr > materials_
Definition: PersonVisual.h:167
void setNumLines(uint32_t num)
void setMaxPointsPerLine(uint32_t max)
MemoryResource get(const std::string &url)
virtual void update(float deltaTime)
TFSIMD_FORCE_INLINE const tfScalar & w() const
#define ROS_INFO_STREAM(args)
virtual void setScalingFactor(double scalingFactor)
virtual void setColor(const Ogre::ColourValue &c)
#define ROS_WARN_STREAM_ONCE(args)
virtual void setColor(const Ogre::ColourValue &c)
Ogre::SceneNode * m_parentSceneNode
Definition: PersonVisual.h:93
void setAnimationState(const std::string &nameOfAnimationState)
void setVisible(bool visible)
resource_retriever::MemoryResource _lastResource
Definition: PersonVisual.h:112
virtual void update(float deltaTime)
RosPackagePathResourceLoadingListener(const fs::path &parentPath)
#define ROS_ERROR(...)
virtual Ogre::ColourValue & getColor()
virtual void setScale(const Ogre::Vector3 &scale)
CylinderPersonVisual(const PersonVisualDefaultArgs &args)
Base class for all person visualization types.
Definition: PersonVisual.h:63
void setLineWidth(float width)
const Ogre::Quaternion & getOrientation() const
MeshPersonVisual(const PersonVisualDefaultArgs &args)


tuw_object_rviz
Author(s): Florian Beck
autogenerated on Mon Jun 10 2019 15:40:17