Gazebo Rendering

API Reference

6.6.3
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 <unordered_map>
23 #include "gz/rendering/Mesh.hh"
25 #include "gz/rendering/Storage.hh"
27 
28 namespace ignition
29 {
30  namespace rendering
31  {
32  inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
33  //
35  template <class T>
36  class BaseMesh :
37  public virtual Mesh,
38  public virtual T
39  {
40  protected: BaseMesh();
41 
42  public: virtual ~BaseMesh();
43 
44  // Documentation inherited.
45  public: virtual bool HasSkeleton() const override;
46 
47  // Documentation inherited.
49  SkeletonLocalTransforms() const override;
50 
51  // Documentation inherited.
52  public: virtual void SetSkeletonLocalTransforms(
54  override;
55 
56  // Documentation inherited.
57  public: virtual std::unordered_map<std::string, float> SkeletonWeights()
58  const override;
59 
60  // Documentation inherited.
61  public: virtual void SetSkeletonWeights(
63  override;
64 
65  // Documentation inherited.
66  public: virtual void SetSkeletonAnimationEnabled(const std::string &_name,
67  bool _enabled, bool _loop = true, float _weight = 1.0) override;
68 
69  // Documentation inherited.
70  public: virtual bool SkeletonAnimationEnabled(const std::string &_name)
71  const override;
72 
73  // Documentation inherited.
74  public: virtual void UpdateSkeletonAnimation(
75  std::chrono::steady_clock::duration _time) override;
76 
77  public: virtual unsigned int SubMeshCount() const override;
78 
79  public: virtual bool HasSubMesh(ConstSubMeshPtr _subMesh) const override;
80 
81  public: virtual bool HasSubMeshName(const std::string &_name) const
82  override;
83 
84  public: virtual SubMeshPtr SubMeshByName(
85  const std::string &_name) const override;
86 
87  public: virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const
88  override;
89 
90  // Documentation inherited.
91  public: virtual MaterialPtr Material() const override;
92 
93  // Documentation inherited.
94  public: virtual void SetMaterial(const std::string &_name,
95  bool _unique = true) override;
96 
97  // Documentation inherited.
98  public: virtual void SetMaterial(MaterialPtr _material,
99  bool _unique = true) override;
100 
101  public: virtual void PreRender() override;
102 
103  // Documentation inherited.
104  public: virtual GeometryPtr Clone() const override;
105 
106  // Documentation inherited.
107  public: void SetDescriptor(const MeshDescriptor &_desc) override;
108 
109  // Documentation inherited.
110  public: const MeshDescriptor &Descriptor() const override;
111 
112  // Documentation inherited
113  public: virtual void Destroy() override;
114 
115  protected: virtual SubMeshStorePtr SubMeshes() const = 0;
116 
119  protected: bool ownsMaterial = false;
120 
122  protected: MaterialPtr material;
123 
126  };
127 
129  template <class T>
130  class BaseSubMesh :
131  public virtual SubMesh,
132  public virtual T
133  {
134  protected: BaseSubMesh();
135 
136  public: virtual ~BaseSubMesh();
137 
138  // Documentation inherited
139  public: virtual MaterialPtr Material() const override;
140 
141  // Documentation inherited
142  public: virtual void SetMaterial(const std::string &_name,
143  bool _unique = true) override;
144 
145  // Documentation inherited
146  public: virtual void SetMaterial(MaterialPtr _material,
147  bool _unique = true) override;
148 
151  public: virtual void SetMaterialImpl(MaterialPtr _material) = 0;
152 
153  public: virtual void PreRender() override;
154 
155  // Documentation inherited
156  public: virtual void Destroy() override;
157 
160  protected: bool ownsMaterial = false;
161 
163  protected: MaterialPtr material;
164  };
165 
167  // BaseMesh
169  template <class T>
171  {
172  }
173 
175  template <class T>
177  {
178  }
179 
181  template <class T>
183  {
184  return false;
185  }
186 
188  template <class T>
191  {
193  return tmpMap;
194  }
195 
197  template <class T>
200  {
201  }
202 
204  template <class T>
206  {
208  return tmpMap;
209  }
210 
212  template <class T>
215  {
216  ignerr << "SetSkeletonWeights not supported for render engine: "
217  << this->Scene()->Engine()->Name() << std::endl;
218  }
219 
221  template <class T>
223  bool, float)
224  {
225  }
226 
228  template <class T>
230  {
231  return false;
232  }
233 
235  template <class T>
237  std::chrono::steady_clock::duration)
238  {
239  }
240 
242  template <class T>
243  unsigned int BaseMesh<T>::SubMeshCount() const
244  {
245  return this->SubMeshes()->Size();
246  }
247 
249  template <class T>
251  {
252  return this->SubMeshes()->Contains(_subMesh);
253  }
254 
256  template <class T>
257  bool BaseMesh<T>::HasSubMeshName(const std::string &_name) const
258  {
259  return this->SubMeshes()->ContainsName(_name);
260  }
261 
263  template <class T>
265  {
266  return this->SubMeshes()->GetByName(_name);
267  }
268 
270  template <class T>
271  SubMeshPtr BaseMesh<T>::SubMeshByIndex(unsigned int _index) const
272  {
273  return this->SubMeshes()->GetByIndex(_index);
274  }
275 
277  template <class T>
279  {
280  unsigned int count = this->SubMeshCount();
281  return (count > 0) ? this->SubMeshByIndex(0)->Material() :
282  MaterialPtr();
283  }
284 
286  template <class T>
287  void BaseMesh<T>::SetMaterial(const std::string &_name, bool _unique)
288  {
289  MaterialPtr mat = this->Scene()->Material(_name);
290  if (mat) this->SetMaterial(mat, _unique);
291  }
292 
294  template <class T>
295  void BaseMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
296  {
297  // todo(anyone) take ownership of reference _material if _unique
298  // and destroy the reference material when the mesh is destroyed
299  unsigned int count = this->SubMeshCount();
300  _material = (_unique && count > 0) ? _material->Clone() : _material;
301 
302  for (unsigned int i = 0; i < count; ++i)
303  {
304  SubMeshPtr subMesh = this->SubMeshByIndex(i);
305  subMesh->SetMaterial(_material, false);
306  }
307 
308  if (this->material && this->ownsMaterial)
309  this->Scene()->DestroyMaterial(this->material);
310 
311  this->ownsMaterial = _unique;
312  this->material = _material;
313  }
314 
316  template <class T>
318  {
319  unsigned int count = this->SubMeshCount();
320 
321  for (unsigned int i = 0; i < count; ++i)
322  {
323  SubMeshPtr subMesh = this->SubMeshByIndex(i);
324  subMesh->PreRender();
325  }
326 
327  T::PreRender();
328  }
329 
331  template <class T>
333  {
334  if (!this->Scene())
335  {
336  ignerr << "Cloning a mesh failed because the mesh to be "
337  << "cloned does not belong to a scene.\n";
338  return nullptr;
339  }
340  else if (this->meshDescriptor.meshName.empty())
341  {
342  ignerr << "Cloning a geometry failed because the name of the mesh is "
343  << "missing.\n";
344  return nullptr;
345  }
346 
347  auto result = this->Scene()->CreateMesh(this->meshDescriptor);
348  if (result)
349  {
350  if (this->Material())
351  {
352  // this call will set the material for the mesh and its submeshes
353  result->SetMaterial(this->Material());
354  }
355  else
356  {
357  // if the mesh doesn't have a material, clone any existing submesh
358  // materials
359  for (unsigned int i = 0; i < this->SubMeshCount(); ++i)
360  {
361  auto existingSubMeshMaterial = this->SubMeshByIndex(i)->Material();
362  if (existingSubMeshMaterial)
363  result->SubMeshByIndex(i)->SetMaterial(existingSubMeshMaterial);
364  }
365  }
366  }
367 
368  return result;
369  }
370 
372  template <class T>
374  {
375  return this->meshDescriptor;
376  }
377 
379  template <class T>
381  {
382  this->meshDescriptor = _desc;
383  }
384 
386  template <class T>
388  {
389  T::Destroy();
390  this->SubMeshes()->DestroyAll();
391  if (this->material && this->ownsMaterial)
392  this->Scene()->DestroyMaterial(this->material);
393  this->material.reset();
394  this->meshDescriptor = MeshDescriptor();
395  }
396 
398  // BaseSubMesh
400  template <class T>
402  {
403  }
404 
406  template <class T>
408  {
409  }
410 
412  template <class T>
414  {
415  T::Destroy();
416  if (this->material && this->ownsMaterial)
417  this->Scene()->DestroyMaterial(this->material);
418  this->material.reset();
419  }
420 
421 
423  template <class T>
424  void BaseSubMesh<T>::SetMaterial(const std::string &_name, bool _unique)
425  {
426  MaterialPtr mat = this->Scene()->Material(_name);
427  if (mat) this->SetMaterial(mat, _unique);
428  }
429 
431  template <class T>
432  void BaseSubMesh<T>::SetMaterial(MaterialPtr _material, bool _unique)
433  {
434  _material = (_unique) ? _material->Clone() : _material;
435 
436  MaterialPtr origMaterial = this->material;
437  bool origUnique = this->ownsMaterial;
438 
439  this->SetMaterialImpl(_material);
440 
441  if (origMaterial && origUnique)
442  this->Scene()->DestroyMaterial(origMaterial);
443 
444  this->material = _material;
445  this->ownsMaterial = _unique;
446  }
447 
449  template <class T>
451  {
452  return this->material;
453  }
454 
456  template <class T>
458  {
459  T::PreRender();
460  if (this->Material())
461  this->Material()->PreRender();
462  }
463  }
464  }
465 }
466 #endif
STL class.
STL class.
Describes how a Mesh should be loaded.
Definition: gz/rendering/MeshDescriptor.hh:44
void SetDescriptor(const MeshDescriptor &_desc) override
Set the mesh's mesh descriptor.
Definition: gz/rendering/base/BaseMesh.hh:380
virtual bool HasSkeleton() const override
Check whether the mesh has skeleton.
Definition: gz/rendering/base/BaseMesh.hh:182
const MeshDescriptor & Descriptor() const override
Get the mesh's mesh descriptor.
Definition: gz/rendering/base/BaseMesh.hh:373
virtual unsigned int SubMeshCount() const override
Get the sub-mesh count.
Definition: gz/rendering/base/BaseMesh.hh:243
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:387
virtual MaterialPtr Material() const override
Get the currently assigned material.
Definition: gz/rendering/base/BaseMesh.hh:450
virtual std::string Name() const =0
Get name of the render-engine.
virtual RenderEngine * Engine() const =0
Get the creating render-engine of the scene.
bool ownsMaterial
Flag to indicate whether or not this submesh should be responsible for destroying the material.
Definition: gz/rendering/base/BaseMesh.hh:160
virtual bool HasSubMeshName(const std::string &_name) const override
Determine if has sub-mesh with given name.
Definition: gz/rendering/base/BaseMesh.hh:257
Definition: gz/rendering/base/BaseMesh.hh:130
MeshDescriptor meshDescriptor
MeshDescriptor for this mesh.
Definition: gz/rendering/base/BaseMesh.hh:125
virtual void UpdateSkeletonAnimation(std::chrono::steady_clock::duration _time) override
Play the active skeleton animation to the specified time.
Definition: gz/rendering/base/BaseMesh.hh:236
MaterialPtr material
Pointer to currently assigned material.
Definition: gz/rendering/base/BaseMesh.hh:122
#define ignerr
Manages a single scene-graph. This class updates scene-wide properties and holds the root scene node....
Definition: gz/rendering/Scene.hh:49
MaterialPtr material
Pointer to currently assigned material.
Definition: gz/rendering/base/BaseMesh.hh:163
Represents a collection of mesh geometries.
Definition: gz/rendering/Mesh.hh:37
virtual GeometryPtr Clone() const override
Clone the geometry.
Definition: gz/rendering/base/BaseMesh.hh:332
virtual SubMeshPtr SubMeshByIndex(unsigned int _index) const override
Get sub-mesh at given index.
Definition: gz/rendering/base/BaseMesh.hh:271
BaseSubMesh()
Definition: gz/rendering/base/BaseMesh.hh:401
virtual SubMeshPtr SubMeshByName(const std::string &_name) const override
Get sub-mesh with given name.
Definition: gz/rendering/base/BaseMesh.hh:264
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:136
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:317
virtual std::unordered_map< std::string, float > SkeletonWeights() const override
Get skeleton node weight.
Definition: gz/rendering/base/BaseMesh.hh:205
Definition: gz/rendering/base/BaseMesh.hh:36
STL class.
virtual ~BaseMesh()
Definition: gz/rendering/base/BaseMesh.hh:176
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:190
virtual bool SkeletonAnimationEnabled(const std::string &_name) const override
Get whether a skeleton animation is enabled or not.
Definition: gz/rendering/base/BaseMesh.hh:229
T endl(T... args)
virtual MeshPtr CreateMesh(const std::string &_meshName)=0
Create new mesh geomerty. The rendering::Mesh will be created from a common::Mesh retrieved from comm...
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:424
virtual void SetSkeletonAnimationEnabled(const std::string &_name, bool _enabled, bool _loop=true, float _weight=1.0) override
Set whether a skeleton animation should be enabled or not.
Definition: gz/rendering/base/BaseMesh.hh:222
virtual void SetSkeletonWeights(const std::unordered_map< std::string, float > &_weights) override
Set skeleton node weight.
Definition: gz/rendering/base/BaseMesh.hh:213
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:457
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:287
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:413
BaseMesh()
Definition: gz/rendering/base/BaseMesh.hh:170
virtual ~BaseSubMesh()
Definition: gz/rendering/base/BaseMesh.hh:407
bool ownsMaterial
Flag to indicate whether or not this mesh should be responsible for destroying the material.
Definition: gz/rendering/base/BaseMesh.hh:119
virtual void SetSkeletonLocalTransforms(const std::map< std::string, math::Matrix4d > &_tfs) override
Set transforms for the skeleton.
Definition: gz/rendering/base/BaseMesh.hh:198
shared_ptr< Material > MaterialPtr
Shared pointer to Material.
Definition: gz/rendering/RenderTypes.hh:191
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:250
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:278
virtual void DestroyMaterial(MaterialPtr _material)=0
Unregister and destroy a material.