17 #ifndef GZ_RENDERING_BASE_BASEVISUAL_HH_
18 #define GZ_RENDERING_BASE_BASEVISUAL_HH_
23 #include <gz/math/AxisAlignedBox.hh>
34 inline namespace GZ_RENDERING_VERSION_NAMESPACE {
47 public:
virtual void SetLocalPose(
const math::Pose3d &_pose)
override;
49 public:
virtual unsigned int GeometryCount()
const override;
54 public:
virtual GeometryPtr GeometryByIndex(
unsigned int _index)
const
57 public:
virtual void AddGeometry(
GeometryPtr _geometry)
override;
62 public:
virtual GeometryPtr RemoveGeometryByIndex(
unsigned int _index)
65 public:
virtual void RemoveGeometries()
override;
67 public:
virtual void SetMaterial(
const std::string &_name,
68 bool _unique =
true)
override;
70 public:
virtual void SetMaterial(
MaterialPtr _material,
71 bool _unique =
true)
override;
73 public:
virtual void SetChildMaterial(
MaterialPtr _material,
74 bool _unique =
true)
override;
76 public:
virtual void SetGeometryMaterial(
MaterialPtr _material,
77 bool _unique =
true)
override;
83 public:
virtual void SetWireframe(
bool _show)
override;
86 public:
virtual bool Wireframe()
const override;
89 public:
virtual void SetVisible(
bool _visible)
override;
92 public:
virtual void SetVisibilityFlags(uint32_t _flags)
override;
95 public:
virtual uint32_t VisibilityFlags()
const override;
98 public:
virtual void AddVisibilityFlags(uint32_t _flags)
override;
101 public:
virtual void RemoveVisibilityFlags(uint32_t _flags)
override;
104 public:
virtual void PreRender()
override;
107 public:
virtual void Destroy()
override;
119 NodePtr _newParent)
const override;
121 protected:
virtual void PreRenderChildren()
override;
123 protected:
virtual void PreRenderGeometries();
127 protected:
virtual bool AttachGeometry(
GeometryPtr _geometry) = 0;
129 protected:
virtual bool DetachGeometry(
GeometryPtr _geometry) = 0;
141 protected:
bool wireframe =
false;
162 rawPose.
Pos() += rawPose.
Rot() * (scale * this->origin);
172 rawPose.
Pos() -= rawPose.
Rot() * (scale * this->origin);
176 gzerr <<
"Unable to set pose of a node: "
177 <<
"non-finite (nan, inf) values detected." <<
std::endl;
181 this->SetRawLocalPose(rawPose);
188 return this->Geometries()->Size();
195 return this->Geometries()->Contains(_geometry);
202 return this->Geometries()->GetByIndex(_index);
209 if (this->AttachGeometry(_geometry))
211 this->Geometries()->Add(_geometry);
219 if (this->DetachGeometry(_geometry))
221 this->Geometries()->Remove(_geometry);
230 return this->RemoveGeometry(this->GeometryByIndex(_index));
237 for (
unsigned int i = this->GeometryCount(); i > 0; --i)
239 this->RemoveGeometryByIndex(i-1);
248 if (mat) this->SetMaterial(mat, _unique);
255 _material = (_unique) ? _material->Clone() : _material;
256 this->SetChildMaterial(_material,
false);
257 this->SetGeometryMaterial(_material,
false);
258 this->material = _material;
265 unsigned int count = this->ChildCount();
266 _material = (_unique && count > 0) ? _material->Clone() : _material;
269 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
273 gzerr <<
"Cast failed in BaseVisual::SetChildMaterial" <<
std::endl;
276 for (
auto it = children_->Begin(); it != children_->End(); ++it)
279 VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
280 if (visual) visual->SetMaterial(_material,
false);
288 unsigned int count = this->GeometryCount();
289 _material = (_unique && count > 0) ? _material->Clone() : _material;
291 for (
unsigned int i = 0; i < count; ++i)
294 geometry->SetMaterial(_material,
false);
302 return this->material;
310 this->PreRenderChildren();
311 this->PreRenderGeometries();
318 this->Geometries()->DestroyAll();
319 this->Children()->RemoveAll();
320 this->material.reset();
329 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
333 gzerr <<
"Cast failed in BaseVisual::PreRenderChildren" <<
std::endl;
336 for (
auto it = children_->Begin(); it != children_->End(); ++it)
338 it->second->PreRender();
346 unsigned int count = this->GeometryCount();
348 for (
unsigned int i = 0; i < count; ++i)
351 geometry->PreRender();
359 return this->wireframe;
366 gzerr <<
"SetWireframe(" << _show <<
") not supported for "
375 gzerr <<
"SetVisible(" << _visible <<
") not supported for "
388 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
392 gzerr <<
"Cast failed in BaseVisual::LocalBoundingBox" <<
std::endl;
395 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
398 VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
417 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
424 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
427 VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
429 box.
Merge(visual->BoundingBox());
438 this->SetVisibilityFlags(this->VisibilityFlags() | _flags);
445 this->SetVisibilityFlags(this->VisibilityFlags() & ~(_flags));
452 this->visibilityFlags = _flags;
456 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
460 gzerr <<
"Cast failed in BaseVisual::SetVisibiltyFlags" <<
std::endl;
463 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
466 VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
468 visual->SetVisibilityFlags(_flags);
476 return this->visibilityFlags;
485 if (
nullptr == scene_)
487 gzerr <<
"Cloning a visual failed because the visual to be cloned is "
488 <<
"not attached to a scene.\n";
493 result = scene_->CreateVisual();
495 result = scene_->CreateVisual(_name);
497 if (
nullptr != _newParent)
499 auto parentScene = _newParent->Scene();
500 if (
nullptr != parentScene && parentScene->Id() != scene_->Id())
502 gzerr <<
"Cloning a visual failed because the desired parent of the "
503 <<
"cloned visual belongs to a different scene.\n";
504 scene_->DestroyVisual(result);
507 _newParent->AddChild(result);
510 result->SetOrigin(this->Origin());
511 result->SetInheritScale(this->InheritScale());
512 result->SetLocalScale(this->LocalScale());
513 result->SetLocalPose(this->LocalPose());
514 result->SetVisibilityFlags(this->VisibilityFlags());
515 result->SetWireframe(this->Wireframe());
519 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
523 gzerr <<
"Cast failed in BaseVisual::Clone\n";
524 scene_->DestroyVisual(result);
527 for (
auto it = children_->Begin(); it != children_->End(); ++it)
530 VisualPtr visual = std::dynamic_pointer_cast<Visual>(child);
533 if (!visual || !visual->Clone(
"", result))
535 gzerr <<
"Cloning a child visual failed.\n";
536 scene_->DestroyVisual(result,
true);
541 for (
unsigned int i = 0; i < this->GeometryCount(); ++i)
542 result->AddGeometry(this->GeometryByIndex(i)->Clone());
545 result->SetMaterial(this->Material());
547 for (
const auto &[key, val] : this->userData)
548 result->SetUserData(key, val);