Gazebo Rendering

API Reference

3.7.2
gz/rendering/base/BaseMesh.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_BASEMESH_HH_
18 #define GZ_RENDERING_BASE_BASEMESH_HH_
19 
20 #include <map>
21 #include <string>
22 #include "gz/rendering/Mesh.hh"
23 #include "gz/rendering/Storage.hh"
25 
26 namespace ignition
27 {
28  namespace rendering
29  {
30  inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
31  //
33  template <class T>
34  class BaseMesh :
35  public virtual Mesh,
36  public virtual T
37  {
38  protected: BaseMesh();
39 
40  public: virtual ~BaseMesh();
41 
42  // Documentation inherited.
43  public: virtual bool HasSkeleton() const override;
44 
45  // Documentation inherited.
47  SkeletonLocalTransforms() const override;
48 
49  // Documentation inherited.
50  public: virtual void SetSkeletonLocalTransforms(
52  override;
53 
54  public: virtual unsigned int SubMeshCount() const override;
55 
56  public: virtual bool HasSubMesh(ConstSubMeshPtr _subMesh) const override;
57 
58  public: virtual bool HasSubMeshName(const std::string &_name) const
59  override;
60 
61  public: virtual SubMeshPtr SubMeshByName(
62  const std::string &_name) const override;
63 
64  public: virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const
65  override;
66 
67  // Documentation inherited.
68  public: virtual MaterialPtr Material() const override;
69 
70  // Documentation inherited.
71  public: virtual void SetMaterial(const std::string &_name,
72  bool _unique = true) override;
73 
74  // Documentation inherited.
75  public: virtual void SetMaterial(MaterialPtr _material,
76  bool _unique = true) override;
77 
78  public: virtual void PreRender() override;
79 
80  // Documentation inherited
81  public: virtual void Destroy() override;
82 
83  protected: virtual SubMeshStorePtr SubMeshes() const = 0;
84 
87  protected: bool ownsMaterial = false;
88 
90  protected: MaterialPtr material;
91  };
92 
94  template <class T>
95  class BaseSubMesh :
96  public virtual SubMesh,
97  public virtual T
98  {
99  protected: BaseSubMesh();
100 
101  public: virtual ~BaseSubMesh();
102 
103  // Documentation inherited
104  public: virtual MaterialPtr Material() const override;
105 
106  // Documentation inherited
107  public: virtual void SetMaterial(const std::string &_name,
108  bool _unique = true) override;
109 
110  // Documentation inherited
111  public: virtual void SetMaterial(MaterialPtr _material,
112  bool _unique = true) override;
113 
116  public: virtual void SetMaterialImpl(MaterialPtr _material) = 0;
117 
118  public: virtual void PreRender() override;
119 
120  // Documentation inherited
121  public: virtual void Destroy() override;
122 
125  protected: bool ownsMaterial = false;
126 
128  protected: MaterialPtr material;
129  };
130 
132  // BaseMesh
134  template <class T>
136  {
137  }
138 
140  template <class T>
142  {
143  }
144 
146  template <class T>
148  {
149  return false;
150  }
151 
153  template <class T>
156  {
158  return tmpMap;
159  }
160 
162  template <class T>
165  {
166  }
167 
169  template <class T>
170  unsigned int BaseMesh<T>::SubMeshCount() const
171  {
172  return this->SubMeshes()->Size();
173  }
174 
176  template <class T>
178  {
179  return this->SubMeshes()->Contains(_subMesh);
180  }
181 
183  template <class T>
184  bool BaseMesh<T>::HasSubMeshName(const std::string &_name) const
185  {
186  return this->SubMeshes()->ContainsName(_name);
187  }
188 
190  template <class T>
192  {
193  return this->SubMeshes()->GetByName(_name);
194  }
195 
197  template <class T>
198  SubMeshPtr BaseMesh<T>::SubMeshByIndex(unsigned int _index) const
199  {
200  return this->SubMeshes()->GetByIndex(_index);
201  }
202 
204  template <class T>
206  {
207  unsigned int count = this->SubMeshCount();
208  return (count > 0) ? this->SubMeshByIndex(0)->Material() :
209  MaterialPtr();
210  }
211 
213  template <class T>
214  void BaseMesh<T>::SetMaterial(const std::string &_name, bool _unique)
215  {
216  MaterialPtr mat = this->Scene()->Material(_name);
217  if (mat) this->SetMaterial(mat, _unique);
218  }
219 
221  template <class T>
222  void BaseMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
223  {
224  // todo(anyone) take ownership of reference _material if _unique
225  // and destroy the reference material when the mesh is destroyed
226  unsigned int count = this->SubMeshCount();
227  _material = (_unique && count > 0) ? _material->Clone() : _material;
228 
229  for (unsigned int i = 0; i < count; ++i)
230  {
231  SubMeshPtr subMesh = this->SubMeshByIndex(i);
232  subMesh->SetMaterial(_material, false);
233  }
234 
235  if (this->material && this->ownsMaterial)
236  this->Scene()->DestroyMaterial(this->material);
237 
238  this->ownsMaterial = _unique;
239  this->material = _material;
240  }
241 
243  template <class T>
245  {
246  unsigned int count = this->SubMeshCount();
247 
248  for (unsigned int i = 0; i < count; ++i)
249  {
250  SubMeshPtr subMesh = this->SubMeshByIndex(i);
251  subMesh->PreRender();
252  }
253 
254  T::PreRender();
255  }
256 
258  template <class T>
260  {
261  T::Destroy();
262  this->SubMeshes()->DestroyAll();
263  if (this->material && this->ownsMaterial)
264  this->Scene()->DestroyMaterial(this->material);
265  this->material.reset();
266  }
267 
269  // BaseSubMesh
271  template <class T>
273  {
274  }
275 
277  template <class T>
279  {
280  }
281 
283  template <class T>
285  {
286  T::Destroy();
287  if (this->material && this->ownsMaterial)
288  this->Scene()->DestroyMaterial(this->material);
289  this->material.reset();
290  }
291 
292 
294  template <class T>
295  void BaseSubMesh<T>::SetMaterial(const std::string &_name, bool _unique)
296  {
297  MaterialPtr mat = this->Scene()->Material(_name);
298  if (mat) this->SetMaterial(mat, _unique);
299  }
300 
302  template <class T>
303  void BaseSubMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
304  {
305  _material = (_unique) ? _material->Clone() : _material;
306 
307  MaterialPtr origMaterial = this->material;
308  bool origUnique = this->ownsMaterial;
309 
310  this->SetMaterialImpl(_material);
311 
312  if (origMaterial && origUnique)
313  this->Scene()->DestroyMaterial(origMaterial);
314 
315  this->material = _material;
316  this->ownsMaterial = _unique;
317  }
318 
320  template <class T>
322  {
323  return this->material;
324  }
325 
327  template <class T>
329  {
330  T::PreRender();
331  if (this->Material())
332  this->Material()->PreRender();
333  }
334  }
335  }
336 }
337 #endif
STL class.
STL class.
virtual bool HasSkeleton() const override
Check whether the mesh has skeleton.
Definition: gz/rendering/base/BaseMesh.hh:147
virtual unsigned int SubMeshCount() const override
Get the sub-mesh count.
Definition: gz/rendering/base/BaseMesh.hh:170
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: gz/rendering/base/BaseMesh.hh:259
virtual MaterialPtr Material() const override
Get the currently assigned material.
Definition: gz/rendering/base/BaseMesh.hh:321
bool ownsMaterial
Flag to indicate whether or not this submesh should be responsible for destroying the material.
Definition: gz/rendering/base/BaseMesh.hh:125
virtual bool HasSubMeshName(const std::string &_name) const override
Determine if has sub-mesh with given name.
Definition: gz/rendering/base/BaseMesh.hh:184
Definition: gz/rendering/base/BaseMesh.hh:95
MaterialPtr material
Pointer to currently assigned material.
Definition: gz/rendering/base/BaseMesh.hh:90
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node....
Definition: gz/rendering/Scene.hh:48
MaterialPtr material
Pointer to currently assigned material.
Definition: gz/rendering/base/BaseMesh.hh:128
Represents a collection of mesh geometries.
Definition: gz/rendering/Mesh.hh:35
virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const override
Get sub-mesh at given index.
Definition: gz/rendering/base/BaseMesh.hh:198
BaseSubMesh()
Definition: gz/rendering/base/BaseMesh.hh:272
virtual SubMeshPtr SubMeshByName(const std::string &_name) const override
Get sub-mesh with given name.
Definition: gz/rendering/base/BaseMesh.hh:191
virtual SubMeshStorePtr SubMeshes() const =0
virtual void PreRender()=0
Prepare this object and any of its children for rendering. This should be called for each object in a...
Represents a single mesh geometry.
Definition: gz/rendering/Mesh.hh:91
virtual void PreRender() override
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: gz/rendering/base/BaseMesh.hh:244
Definition: gz/rendering/base/BaseMesh.hh:34
STL class.
virtual ~BaseMesh()
Definition: gz/rendering/base/BaseMesh.hh:141
virtual MaterialPtr Material(const std::string &_name) const =0
Get material registered under the given name. If no material is registered under the given name,...
virtual std::map< std::string, math::Matrix4d > SkeletonLocalTransforms() const override
Get the skeleton local transforms.
Definition: gz/rendering/base/BaseMesh.hh:155
virtual void SetMaterial(const std::string &_name, bool _unique=true) override
Set the materials of this SubMesh. The specified material will be retrieved from the parent Scene....
Definition: gz/rendering/base/BaseMesh.hh:295
virtual void PreRender() override
Prepare this object and any of its children for rendering. This should be called for each object in a...
Definition: gz/rendering/base/BaseMesh.hh:328
virtual void SetMaterial(const std::string &_name, bool _unique=true) override
Set the materials of this Geometry. The specified material will be retrieved from the parent Scene....
Definition: gz/rendering/base/BaseMesh.hh:214
virtual void Destroy() override
Destroy any resources associated with this object. Invoking any other functions after destroying an o...
Definition: gz/rendering/base/BaseMesh.hh:284
BaseMesh()
Definition: gz/rendering/base/BaseMesh.hh:135
virtual ~BaseSubMesh()
Definition: gz/rendering/base/BaseMesh.hh:278
bool ownsMaterial
Flag to indicate whether or not this mesh should be responsible for destroying the material.
Definition: gz/rendering/base/BaseMesh.hh:87
virtual void SetSkeletonLocalTransforms(const std::map< std::string, math::Matrix4d > &_tfs) override
Set transforms for the skeleton.
Definition: gz/rendering/base/BaseMesh.hh:163
shared_ptr< Material > MaterialPtr
Shared pointer to Material.
Definition: gz/rendering/RenderTypes.hh:143
Represents a surface material of a Geometry.
Definition: gz/rendering/Material.hh:47
virtual bool HasSubMesh(ConstSubMeshPtr _subMesh) const override
Determine if has given sub-mesh.
Definition: gz/rendering/base/BaseMesh.hh:177
virtual void SetMaterialImpl(MaterialPtr _material)=0
Engine implementation for setting the material of this SubMesh.
virtual MaterialPtr Material() const override
Get the material of this geometry.
Definition: gz/rendering/base/BaseMesh.hh:205
virtual void DestroyMaterial(MaterialPtr _material)=0
Unregister and destroy a material.