Visual.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GAZEBO_RENDERING_VISUAL_HH_
18 #define GAZEBO_RENDERING_VISUAL_HH_
19 
20 #include <boost/enable_shared_from_this.hpp>
21 #include <boost/function.hpp>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <sdf/sdf.hh>
27 #include <ignition/math/Box.hh>
28 #include <ignition/math/Color.hh>
29 #include <ignition/math/Pose3.hh>
30 #include <ignition/math/Quaternion.hh>
31 #include <ignition/math/Vector3.hh>
32 #include <ignition/msgs/MessageTypes.hh>
33 
34 #include "gazebo/common/Color.hh"
35 #include "gazebo/common/Mesh.hh"
36 #include "gazebo/common/Time.hh"
37 
38 #include "gazebo/msgs/MessageTypes.hh"
39 
41 #include "gazebo/util/system.hh"
42 
43 namespace Ogre
44 {
45  class MovableObject;
46  class SceneNode;
47 }
48 
49 namespace gazebo
50 {
51  namespace rendering
52  {
53  class VisualPrivate;
54 
57 
60  class GZ_RENDERING_VISIBLE Visual :
61  public std::enable_shared_from_this<Visual>
62  {
65  public: enum VisualType
66  {
82  VT_PHYSICS
83  };
84 
90  public: Visual(const std::string &_name, VisualPtr _parent,
91  bool _useRTShader = true);
92 
98  public: Visual(const std::string &_name, ScenePtr _scene,
99  bool _useRTShader = true);
100 
102  public: virtual ~Visual();
103 
105  public: void Init();
106 
108  public: virtual void Fini();
109 
114  public: VisualPtr Clone(const std::string &_name, VisualPtr _newParent);
115 
118  public: void LoadFromMsg(ConstVisualPtr &_msg);
119 
122  public: void Load(sdf::ElementPtr _sdf);
123 
125  public: virtual void Load();
126 
128  public: void Update();
129 
136  public: sdf::ElementPtr GetSDF() const;
137 
140  public: void SetName(const std::string &_name);
141 
144  public: std::string Name() const;
145 
148  public: void AttachVisual(VisualPtr _vis);
149 
152  public: void DetachVisual(VisualPtr _vis);
153 
156  public: void DetachVisual(const std::string &_name);
157 
160  public: void AttachObject(Ogre::MovableObject *_obj);
161 
164  public: bool HasAttachedObject(const std::string &_name);
165 
168  public: unsigned int GetAttachedObjectCount() const;
169 
171  public: void DetachObjects();
172 
175  public: unsigned int GetChildCount();
176 
181  public: VisualPtr GetChild(unsigned int _index);
182 
190  public: Ogre::MovableObject *AttachMesh(const std::string &_meshName,
191  const std::string &_subMesh="",
192  bool _centerSubmesh = false,
193  const std::string &_objName="");
194 
197  public: void SetScale(const ignition::math::Vector3d &_scale);
198 
201  public: ignition::math::Vector3d Scale() const;
202 
205  public: ignition::math::Vector3d DerivedScale() const;
206 
209  public: bool GetLighting() const;
210 
213  public: void SetLighting(bool _lighting);
214 
221  public: void SetMaterial(const std::string &_materialName,
222  bool _unique = true, const bool _cascade = true);
223 
233  public: void SetMaterialShaderParam(const std::string &_paramName,
234  const std::string &_shaderType, const std::string &_value);
235 
240  public: void SetAmbient(const common::Color &_color,
241  const bool _cascade = true) GAZEBO_DEPRECATED(9.0);
242 
246  public: void SetAmbient(const ignition::math::Color &_color,
247  const bool _cascade = true);
248 
253  public: void SetDiffuse(const common::Color &_color,
254  const bool _cascade = true) GAZEBO_DEPRECATED(9.0);
255 
259  public: void SetDiffuse(const ignition::math::Color &_color,
260  const bool _cascade = true);
261 
266  public: void SetSpecular(const common::Color &_color,
267  const bool _cascade = true) GAZEBO_DEPRECATED(9.0);
268 
272  public: void SetSpecular(const ignition::math::Color &_color,
273  const bool _cascade = true);
274 
279  public: virtual void SetEmissive(const common::Color &_color,
280  const bool _cascade = true) GAZEBO_DEPRECATED(9.0);
281 
285  public: virtual void SetEmissive(const ignition::math::Color &_color,
286  const bool _cascade = true);
287 
291  public: common::Color GetAmbient() const GAZEBO_DEPRECATED(9.0);
292 
295  public: ignition::math::Color Ambient() const;
296 
300  public: common::Color GetDiffuse() const GAZEBO_DEPRECATED(9.0);
301 
304  public: ignition::math::Color Diffuse() const;
305 
309  public: common::Color GetSpecular() const GAZEBO_DEPRECATED(9.0);
310 
313  public: ignition::math::Color Specular() const;
314 
318  public: common::Color GetEmissive() const GAZEBO_DEPRECATED(9.0);
319 
322  public: ignition::math::Color Emissive() const;
323 
326  public: void SetWireframe(bool _show);
327 
330  public: bool Wireframe() const;
331 
335  private: void SetTransparencyInnerLoop(Ogre::SceneNode *_sceneNode);
336 
340  public: void SetTransparency(float _trans);
341 
344  public: float GetTransparency();
345 
349  public: float DerivedTransparency() const;
350 
353  public: void SetInheritTransparency(const bool _inherit);
354 
357  public: bool InheritTransparency() const;
358 
362  public: virtual void SetHighlighted(bool _highlighted);
363 
367  public: bool GetHighlighted() const;
368 
371  public: bool GetCastShadows() const;
372 
375  public: void SetCastShadows(bool _shadows);
376 
380  public: virtual void SetVisible(bool _visible, bool _cascade = true);
381 
383  public: void ToggleVisible();
384 
387  public: bool GetVisible() const;
388 
391  public: void SetPosition(const ignition::math::Vector3d &_pos);
392 
395  public: void SetRotation(const ignition::math::Quaterniond &_rot);
396 
399  public: void SetPose(const ignition::math::Pose3d &_pose);
400 
403  public: ignition::math::Vector3d Position() const;
404 
407  public: ignition::math::Quaterniond Rotation() const;
408 
411  public: ignition::math::Pose3d Pose() const;
412 
415  public: ignition::math::Pose3d InitialRelativePose() const;
416 
419  public: ignition::math::Pose3d WorldPose() const;
420 
423  public: void SetWorldPose(const ignition::math::Pose3d &_pose);
424 
427  public: void SetWorldPosition(const ignition::math::Vector3d &_pos);
428 
431  public: void SetWorldRotation(const ignition::math::Quaterniond &_rot);
432 
435  public: Ogre::SceneNode *GetSceneNode() const;
436 
438  public: void MakeStatic();
439 
442  public: bool IsStatic() const;
443 
446  public: void EnableTrackVisual(VisualPtr _vis);
447 
449  public: void DisableTrackVisual();
450 
453  public: std::string GetNormalMap() const;
454 
457  public: void SetNormalMap(const std::string &_nmap);
458 
464  public: void SetRibbonTrail(bool _value,
465  const common::Color &_initialColor,
466  const common::Color &_changeColor) GAZEBO_DEPRECATED(9.0);
467 
472  public: void SetRibbonTrail(bool _value,
473  const ignition::math::Color &_initialColor,
474  const ignition::math::Color &_changeColor);
475 
478  public: ignition::math::Box BoundingBox() const;
479 
483  public: DynamicLines *CreateDynamicLine(
485 
488  public: void DeleteDynamicLine(DynamicLines *_line);
489 
493  public: void AttachLineVertex(DynamicLines *_line,
494  unsigned int _index);
495 
498  public: std::string GetMaterialName() const;
499 
504  public: void InsertMesh(const std::string &_meshName,
505  const std::string &_subMesh = "",
506  bool _centerSubmesh = false);
507 
512  public: static void InsertMesh(const common::Mesh *_mesh,
513  const std::string &_subMesh = "",
514  bool _centerSubmesh = false);
515 
518  public: void UpdateFromMsg(ConstVisualPtr &_msg);
519 
522  public: bool IsPlane() const;
523 
526  public: VisualPtr GetParent() const;
527 
531  public: VisualPtr GetRootVisual();
532 
539  public: VisualPtr GetNthAncestor(unsigned int _n);
540 
544  public: bool IsAncestorOf(const rendering::VisualPtr _visual) const;
545 
549  public: bool IsDescendantOf(const rendering::VisualPtr _visual) const;
550 
554  public: unsigned int GetDepth() const;
555 
559  public: std::string GetShaderType() const;
560 
564  public: void SetShaderType(const std::string &_type);
565 
569  public: void MoveToPosition(const ignition::math::Pose3d &_pose,
570  const double _time);
571 
576  public: void MoveToPositions(
577  const std::vector<ignition::math::Pose3d> &_pts, const double _time,
578  std::function<void()> _onComplete = nullptr);
579 
585  public: void SetVisibilityFlags(uint32_t _flags);
586 
592  public: uint32_t GetVisibilityFlags();
593 
595  public: void ShowBoundingBox();
596 
600  public: void ShowCollision(bool _show);
601 
604  public: void ShowSkeleton(bool _show);
605 
608  public: void SetScene(ScenePtr _scene);
609 
612  public: ScenePtr GetScene() const;
613 
616  public: void ShowJoints(bool _show);
617 
620  public: void ShowCOM(bool _show);
621 
624  public: void ShowInertia(bool _show);
625 
628  public: void ShowLinkFrame(bool _show);
629 
632  public: void SetSkeletonPose(const msgs::PoseAnimation &_pose);
633 
638  public: void LoadPlugin(const std::string &_filename,
639  const std::string &_name,
640  sdf::ElementPtr _sdf);
641 
644  public: void RemovePlugin(const std::string &_name);
645 
647  public: uint32_t GetId() const;
648 
650  public: void SetId(uint32_t _id);
651 
654  public: std::string GetGeometryType() const;
655 
658  public: ignition::math::Vector3d GetGeometrySize() const;
659 
662  public: std::string GetMeshName() const;
663 
667  public: std::string GetSubMeshName() const;
668 
670  public: void ClearParent();
671 
675  public: void ToggleLayer(const int32_t _layer);
676 
679  public: void SetLayer(const int32_t _layer);
680 
683  public: Visual::VisualType GetType() const;
684 
687  public: void SetType(const Visual::VisualType _type);
688 
691  public: bool UseRTShader() const;
692 
696  public: void SetTypeMsg(const google::protobuf::Message *_msg);
697 
702  public: void AddPendingChild(std::pair<VisualType,
703  const google::protobuf::Message *> _pair);
704 
708  public: static Visual::VisualType ConvertVisualType(
709  const msgs::Visual::Type &_type);
710 
714  public: static msgs::Visual::Type ConvertVisualType(
715  const Visual::VisualType &_type);
716 
720  public: void FillMaterialMsg(ignition::msgs::Material &_msg) const;
721 
729  protected: Visual(VisualPrivate &_dataPtr,
730  const std::string &_name, VisualPtr _parent,
731  bool _useRTShader = true);
732 
740  protected: Visual(VisualPrivate &_dataPtr,
741  const std::string &_name, ScenePtr _scene,
742  bool _useRTShader = true);
743 
746  protected: void ProcessMaterialMsg(const ignition::msgs::Material &_msg);
747 
754  private: void Init(const std::string &_name, ScenePtr _scene,
755  bool _useRTShader);
756 
763  private: void Init(const std::string &_name, VisualPtr _parent,
764  bool _useRTShader);
765 
768  private: void LoadPlugins();
769 
770  private: void LoadPlugin(sdf::ElementPtr _sdf);
771 
775  private: void BoundsHelper(Ogre::SceneNode *_node,
776  ignition::math::Box &_box) const;
777 
781  private: bool GetCenterSubMesh() const;
782 
785  private: void DestroyAllAttachedMovableObjects(
786  Ogre::SceneNode *_sceneNode);
787 
791  private: void UpdateGeomSize(const ignition::math::Vector3d &_scale);
792 
795  private: void UpdateTransparency(const bool _cascade = true);
796 
799  protected: VisualPrivate *dataPtr;
800  };
802  }
803 }
804 #endif
void SetWorldRotation(const ignition::math::Quaterniond &_rot)
Set the world orientation of the visual.
common::Color GetAmbient() const GAZEBO_DEPRECATED(9.0)
Get the ambient color of the visual.
void SetRibbonTrail(bool _value, const common::Color &_initialColor, const common::Color &_changeColor) GAZEBO_DEPRECATED(9.0)
True on or off a ribbon trail.
static Visual::VisualType ConvertVisualType(const msgs::Visual::Type &_type)
Convert from msgs::Visual::Type to VisualType.
ScenePtr GetScene() const
Get current.
void RemovePlugin(const std::string &_name)
Remove a running plugin.
@ VT_MODEL
Model visual.
Definition: Visual.hh:70
@ VT_SENSOR
Sensor visual.
Definition: Visual.hh:78
ignition::math::Vector3d GetGeometrySize() const
Get the geometry size.
Definition: JointMaker.hh:39
Definition: Model.hh:40
unsigned int GetAttachedObjectCount() const
Return the number of attached movable objects.
virtual void Load()
Load the visual with default parameters.
Visual(const std::string &_name, VisualPtr _parent, bool _useRTShader=true)
Constructor.
virtual void SetVisible(bool _visible, bool _cascade=true)
Set whether the visual is visible.
Ogre::SceneNode * GetSceneNode() const
Return the scene Node of this visual entity.
ignition::math::Box BoundingBox() const
Get the bounding box for the visual.
common::Color GetEmissive() const GAZEBO_DEPRECATED(9.0)
Get the emissive color of the visual.
void SetType(const Visual::VisualType _type)
Set type of visual.
virtual ~Visual()
Destructor.
void SetRotation(const ignition::math::Quaterniond &_rot)
Set the rotation of the visual.
Forward declarations for the common classes.
Definition: Animation.hh:26
void ShowBoundingBox()
Display the bounding box visual.
void SetScene(ScenePtr _scene)
Set current scene.
void SetMaterialShaderParam(const std::string &_paramName, const std::string &_shaderType, const std::string &_value)
Set a shader program parameter associated to this visual's material.
void SetNormalMap(const std::string &_nmap)
Set the normal map.
void DetachObjects()
Detach all objects.
@ VT_COLLISION
Collision visual.
Definition: Visual.hh:76
void FillMaterialMsg(ignition::msgs::Material &_msg) const
Fill an ignition::msgs::Material message based on this visual's material properties.
ignition::math::Pose3d WorldPose() const
Get the global pose of the visual.
void SetPosition(const ignition::math::Vector3d &_pos)
Set the position of the visual.
common::Color GetSpecular() const GAZEBO_DEPRECATED(9.0)
Get the specular color of the visual.
@ VT_ENTITY
Entity visual.
Definition: Visual.hh:68
VisualPrivate * dataPtr
Definition: Visual.hh:799
void SetTypeMsg(const google::protobuf::Message *_msg)
Set a message specific for this visual type.
void SetMaterial(const std::string &_materialName, bool _unique=true, const bool _cascade=true)
Set the material.
common
Definition: FuelModelDatabase.hh:37
@ VT_VISUAL
Visual visual.
Definition: Visual.hh:74
void DeleteDynamicLine(DynamicLines *_line)
Delete a dynamic line.
std::string GetMaterialName() const
Get the name of the material.
VisualPtr Clone(const std::string &_name, VisualPtr _newParent)
Clone the visual with a new name.
void SetId(uint32_t _id)
Set the id associated with this visual.
void Init()
Helper for the contructor.
ignition::math::Color Diffuse() const
Get the diffuse color of the visual.
ignition::math::Vector3d Scale() const
Get the scale.
void SetPose(const ignition::math::Pose3d &_pose)
Set the pose of the visual.
unsigned int GetDepth() const
Get the depth of this visual, where 0 is the depth of the world visual.
bool GetLighting() const
Get whether or not lighting is enabled.
void SetLighting(bool _lighting)
Set whether or not to enable or disable lighting.
void EnableTrackVisual(VisualPtr _vis)
Set one visual to track/follow another.
uint32_t GetVisibilityFlags()
Get visibility flags for this visual and all children.
bool UseRTShader() const
Get whether this visual uses RT shader system.
VisualPtr GetChild(unsigned int _index)
Get an attached visual based on an index.
void SetShaderType(const std::string &_type)
Set the shader type for the visual's material.
void AddPendingChild(std::pair< VisualType, const google::protobuf::Message * > _pair)
Push a message for a child of this visual which hasn't been loaded yet.
Class for drawing lines that can change.
Definition: DynamicLines.hh:42
@ VT_GUI
GUI visual.
Definition: Visual.hh:80
VisualPtr GetParent() const
Get the parent visual, if one exists.
ignition::math::Color Emissive() const
Get the emissive color of the visual.
void SetWorldPose(const ignition::math::Pose3d &_pose)
Set the world pose of the visual.
sdf::ElementPtr GetSDF() const
Get the visual SDF.
bool IsAncestorOf(const rendering::VisualPtr _visual) const
Check if this visual is an ancestor of another visual.
void Update()
Update the visual.
@ RENDERING_LINE_STRIP
A strip of connected lines, 1 vertex per line plus 1 start vertex.
Definition: RenderTypes.hh:226
void SetWorldPosition(const ignition::math::Vector3d &_pos)
Set the world linear position of the visual.
void ShowCOM(bool _show)
Display Center of Mass visuals.
VisualPtr GetNthAncestor(unsigned int _n)
Get the nth ancestor counting from the world visual.
void ShowCollision(bool _show)
Display the collision visuals.
bool GetVisible() const
Get whether the visual is visible.
common::Color GetDiffuse() const GAZEBO_DEPRECATED(9.0)
Get the diffuse color of the visual.
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:82
std::string GetGeometryType() const
Get the geometry type.
RenderOpType
Type of render operation for a drawable.
Definition: RenderTypes.hh:216
std::string GetMeshName() const
The name of the mesh set in the visual's SDF.
void MoveToPosition(const ignition::math::Pose3d &_pose, const double _time)
Move to a pose and over a given time.
bool Wireframe() const
Get whether wireframe is enabled for this visual.
void SetAmbient(const common::Color &_color, const bool _cascade=true) GAZEBO_DEPRECATED(9.0)
Set the ambient color of the visual.
void MoveToPositions(const std::vector< ignition::math::Pose3d > &_pts, const double _time, std::function< void()> _onComplete=nullptr)
Move to a series of pose and over a given time.
void ProcessMaterialMsg(const ignition::msgs::Material &_msg)
Process a material message.
void ShowLinkFrame(bool _show)
Display link frame visuals.
ignition::math::Pose3d InitialRelativePose() const
Get the initial relative pose of the visual.
rendering
Definition: RenderEngine.hh:31
void SetScale(const ignition::math::Vector3d &_scale)
Set the scale.
A renderable object.
Definition: Visual.hh:60
VisualType
Type of visual.
Definition: Visual.hh:65
Ogre::MovableObject * AttachMesh(const std::string &_meshName, const std::string &_subMesh="", bool _centerSubmesh=false, const std::string &_objName="")
Attach a mesh to this visual by name.
std::string GetShaderType() const
Get the shader type.
void ToggleLayer(const int32_t _layer)
Toggle layer visibility.
void LoadPlugin(const std::string &_filename, const std::string &_name, sdf::ElementPtr _sdf)
Load a plugin.
bool IsPlane() const
Return true if the visual is a plane.
void SetWireframe(bool _show)
Enable or disable wireframe for this visual.
void SetName(const std::string &_name)
Set the name of the visual.
void AttachVisual(VisualPtr _vis)
Attach a visual to this visual.
void ClearParent()
Clear parents.
@ VT_LINK
Link visual.
Definition: Visual.hh:72
void UpdateFromMsg(ConstVisualPtr &_msg)
Update a visual based on a message.
DynamicLines * CreateDynamicLine(RenderOpType _type=RENDERING_LINE_STRIP)
Add a line to the visual.
std::string GetNormalMap() const
Get the normal map.
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
void SetInheritTransparency(const bool _inherit)
Set whether to inherit transparency from parent.
uint32_t GetId() const
Get the id associated with this visual.
virtual void SetEmissive(const common::Color &_color, const bool _cascade=true) GAZEBO_DEPRECATED(9.0)
Set the emissive value.
void SetSkeletonPose(const msgs::PoseAnimation &_pose)
Set animation skeleton pose.
Defines a color.
Definition: Color.hh:36
void SetVisibilityFlags(uint32_t _flags)
Set visibility flags for this visual and all children.
float DerivedTransparency() const
Get the transparency of the visual as inherited from all parents.
void SetLayer(const int32_t _layer)
Set the layer this visual belongs to.
bool IsStatic() const
Return true if the visual is a static geometry.
std::string GetSubMeshName() const
Get the name of the sub mesh set in the visual's SDF.
unsigned int GetChildCount()
Get the number of attached visuals.
void ShowSkeleton(bool _show)
Display the skeleton visuals.
#define GAZEBO_DEPRECATED(version)
Definition: system.hh:328
void SetSpecular(const common::Color &_color, const bool _cascade=true) GAZEBO_DEPRECATED(9.0)
Set the specular color of the visual.
void SetTransparency(float _trans)
Set the transparency.
bool GetHighlighted() const
Get whether or not the visual is visually highlighted.
void AttachObject(Ogre::MovableObject *_obj)
Attach a renerable object to the visual.
ignition::math::Color Ambient() const
Get the ambient color of the visual.
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:114
void AttachLineVertex(DynamicLines *_line, unsigned int _index)
Attach a vertex of a line to the position of the visual.
Visual::VisualType GetType() const
Get type of visual.
ignition::math::Vector3d Position() const
Get the position of the visual.
void InsertMesh(const std::string &_meshName, const std::string &_subMesh="", bool _centerSubmesh=false)
Insert a mesh into Ogre.
void ToggleVisible()
Toggle whether this visual is visible.
bool GetCastShadows() const
Get whether the visual casts shadows.
void SetCastShadows(bool _shadows)
Set whether the visual should cast shadows.
std::string Name() const
Get the name of the visual.
void ShowJoints(bool _show)
Display joint visuals.
bool HasAttachedObject(const std::string &_name)
Returns true if an object with _name is attached.
void DisableTrackVisual()
Disable tracking of a visual.
virtual void Fini()
Helper for the destructor.
void SetDiffuse(const common::Color &_color, const bool _cascade=true) GAZEBO_DEPRECATED(9.0)
Set the diffuse color of the visual.
ignition::math::Vector3d DerivedScale() const
Get the scale of the visual as inherited from all parents.
ignition::math::Quaterniond Rotation() const
Get the rotation of the visual.
ignition::math::Color Specular() const
Get the specular color of the visual.
Definition: ConfigWidget.hh:33
bool IsDescendantOf(const rendering::VisualPtr _visual) const
Check if this visual is a descendant of another visual.
VisualPtr GetRootVisual()
Get the root visual.
void LoadFromMsg(ConstVisualPtr &_msg)
Load from a message.
void DetachVisual(VisualPtr _vis)
Detach a visual.
bool InheritTransparency() const
Get whether this visual inherits transparency from parent.
virtual void SetHighlighted(bool _highlighted)
Set the visual to be visually highlighted.
void ShowInertia(bool _show)
Display inertia visuals.
ignition::math::Pose3d Pose() const
Get the pose of the visual.
float GetTransparency()
Get the transparency.
void MakeStatic()
Make the visual objects static renderables.
@ VT_PHYSICS
Physics data visual.
Definition: Visual.hh:82