Gazebo Rendering

API Reference

8.2.1
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 gz
29 {
30  namespace rendering
31  {
32  inline namespace GZ_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.
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  gzerr << "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  gzerr << "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  gzerr << "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