Gazebo Rendering

API Reference

8.3.0
BaseArrowVisual.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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 GZ_RENDERING_BASE_BASEARROWVISUAL_HH_
18 #define GZ_RENDERING_BASE_BASEARROWVISUAL_HH_
19 
20 #include <string>
21 
22 #include <gz/common/MeshManager.hh>
23 
25 #include "gz/rendering/Scene.hh"
26 
27 namespace gz
28 {
29  namespace rendering
30  {
31  inline namespace GZ_RENDERING_VERSION_NAMESPACE {
32  //
33  template <class T>
35  public virtual ArrowVisual,
36  public virtual T
37  {
39  protected: BaseArrowVisual();
40 
42  public: virtual ~BaseArrowVisual();
43 
44  // Documentation inherited.
45  protected: virtual void Destroy() override;
46 
47  // Documentation inherited.
48  public: virtual VisualPtr Head() const override;
49 
50  // Documentation inherited.
51  public: virtual VisualPtr Shaft() const override;
52 
53  // Documentation inherited.
54  public: virtual VisualPtr Rotation() const override;
55 
56  // Documentation inherited
57  public: virtual void ShowArrowHead(bool _b) override;
58 
59  // Documentation inherited
60  public: virtual void ShowArrowShaft(bool _b) override;
61 
62  // Documentation inherited
63  public: virtual void ShowArrowRotation(bool _b) override;
64 
65  // Documentation inherited
66  public: virtual void SetVisible(bool _visible) override;
67 
68  // Documentation inherited.
69  protected: virtual void Init() override;
70 
72  protected: bool rotationVisible = false;
73  };
74 
76  template <class T>
78  {
79  }
80 
82  template <class T>
84  {
85  }
86 
88  template <class T>
90  {
91  while (this->ChildCount() > 0u)
92  {
93  auto visual = std::dynamic_pointer_cast<Visual>(this->ChildByIndex(0));
94  if (visual)
95  {
96  visual->Destroy();
97  }
98  }
99  T::Destroy();
100  }
101 
103  template <class T>
105  {
106  return std::dynamic_pointer_cast<Visual>(this->ChildByIndex(0));
107  }
108 
110  template <class T>
112  {
113  return std::dynamic_pointer_cast<Visual>(this->ChildByIndex(1));
114  }
115 
117  template <class T>
119  {
120  return std::dynamic_pointer_cast<Visual>(this->ChildByIndex(2));
121  }
122 
124  template <class T>
126  {
127  this->Head()->SetVisible(_b);
128  }
129 
131  template <class T>
133  {
134  this->Shaft()->SetVisible(_b);
135  }
136 
138  template <class T>
140  {
141  this->Rotation()->SetVisible(_b);
142  this->rotationVisible = _b;
143  }
144 
146  template <class T>
147  void BaseArrowVisual<T>::SetVisible(bool _visible)
148  {
149  T::SetVisible(_visible);
150 
151  // Force rotation visual visibility to false
152  // if the arrow visual is not visible.
153  // Else, rotation visual's visibility overrides
154  // its parent's visibility.
155  this->Rotation()->SetVisible(this->rotationVisible && _visible);
156  }
157 
159  template <class T>
161  {
162  T::Init();
163 
164  VisualPtr cone = this->Scene()->CreateVisual();
165  cone->AddGeometry(this->Scene()->CreateCone());
166  cone->SetOrigin(0, 0, -0.5);
167  cone->SetLocalPosition(0, 0, 0);
168  cone->SetLocalScale(0.1, 0.1, 0.25);
169  this->AddChild(cone);
170 
171  VisualPtr cylinder = this->Scene()->CreateVisual();
172  cylinder->AddGeometry(this->Scene()->CreateCylinder());
173  cylinder->SetOrigin(0, 0, 0.5);
174  cylinder->SetLocalPosition(0, 0, 0);
175  cylinder->SetLocalScale(0.05, 0.05, 0.5);
176  this->AddChild(cylinder);
177 
179  std::string rotMeshName = "arrow_rotation";
180  if (!meshMgr->HasMesh(rotMeshName))
181  meshMgr->CreateTube(rotMeshName, 0.070f, 0.075f, 0.01f, 1, 32);
182 
183  VisualPtr rotationVis = this->Scene()->CreateVisual();
184  rotationVis->AddGeometry(this->Scene()->CreateMesh(rotMeshName));
185  rotationVis->SetOrigin(0, 0, -0.125);
186  rotationVis->SetLocalPosition(0, 0, 0);
187  rotationVis->SetVisible(this->rotationVisible);
188  this->AddChild(rotationVis);
189 
190  this->SetOrigin(0, 0, -0.5);
191  }
192  }
193  }
194 }
195 #endif