Gazebo Rendering

API Reference

8.2.1
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  }
143 
145  template <class T>
146  void BaseArrowVisual<T>::SetVisible(bool _visible)
147  {
148  T::SetVisible(_visible);
149 
150  // Force rotation visual visibility to false
151  // if the arrow visual is not visible.
152  // Else, rotation visual's visibility overrides
153  // its parent's visibility.
154  this->Rotation()->SetVisible(this->rotationVisible && _visible);
155  }
156 
158  template <class T>
160  {
161  T::Init();
162 
163  VisualPtr cone = this->Scene()->CreateVisual();
164  cone->AddGeometry(this->Scene()->CreateCone());
165  cone->SetOrigin(0, 0, -0.5);
166  cone->SetLocalPosition(0, 0, 0);
167  cone->SetLocalScale(0.1, 0.1, 0.25);
168  this->AddChild(cone);
169 
170  VisualPtr cylinder = this->Scene()->CreateVisual();
171  cylinder->AddGeometry(this->Scene()->CreateCylinder());
172  cylinder->SetOrigin(0, 0, 0.5);
173  cylinder->SetLocalPosition(0, 0, 0);
174  cylinder->SetLocalScale(0.05, 0.05, 0.5);
175  this->AddChild(cylinder);
176 
178  std::string rotMeshName = "arrow_rotation";
179  if (!meshMgr->HasMesh(rotMeshName))
180  meshMgr->CreateTube(rotMeshName, 0.070f, 0.075f, 0.01f, 1, 32);
181 
182  VisualPtr rotationVis = this->Scene()->CreateVisual();
183  rotationVis->AddGeometry(this->Scene()->CreateMesh(rotMeshName));
184  rotationVis->SetOrigin(0, 0, -0.125);
185  rotationVis->SetLocalPosition(0, 0, 0);
186  rotationVis->SetVisible(this->rotationVisible);
187  this->AddChild(rotationVis);
188 
189  this->SetOrigin(0, 0, -0.5);
190  }
191  }
192  }
193 }
194 #endif