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 {
68 bool _unique =
true)
override;
71 bool _unique =
true)
override;
74 bool _unique =
true)
override;
77 bool _unique =
true)
override;
92 public:
virtual bool Static()
const override;
95 public:
virtual void SetStatic(
bool _static)
override;
125 NodePtr _newParent)
const override;
147 protected:
bool wireframe =
false;
168 rawPose.
Pos() += rawPose.
Rot() * (scale * this->origin);
178 rawPose.
Pos() -= rawPose.
Rot() * (scale * this->origin);
182 gzerr <<
"Unable to set pose of a node: "
183 <<
"non-finite (nan, inf) values detected." <<
std::endl;
187 this->SetRawLocalPose(rawPose);
194 return this->Geometries()->Size();
201 return this->Geometries()->Contains(_geometry);
208 return this->Geometries()->GetByIndex(_index);
215 if (this->AttachGeometry(_geometry))
217 this->Geometries()->Add(_geometry);
225 if (this->DetachGeometry(_geometry))
227 this->Geometries()->Remove(_geometry);
236 return this->RemoveGeometry(this->GeometryByIndex(_index));
243 for (
unsigned int i = this->GeometryCount(); i > 0; --i)
245 this->RemoveGeometryByIndex(i-1);
254 if (mat) this->SetMaterial(mat, _unique);
261 _material = (_unique) ? _material->Clone() : _material;
262 this->SetChildMaterial(_material,
false);
263 this->SetGeometryMaterial(_material,
false);
264 this->material = _material;
271 unsigned int count = this->ChildCount();
272 _material = (_unique && count > 0) ? _material->Clone() : _material;
275 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
279 gzerr <<
"Cast failed in BaseVisual::SetChildMaterial" <<
std::endl;
282 for (
auto it = children_->Begin(); it != children_->End(); ++it)
284 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
285 if (visual) visual->SetMaterial(_material,
false);
293 unsigned int count = this->GeometryCount();
294 _material = (_unique && count > 0) ? _material->Clone() : _material;
296 for (
unsigned int i = 0; i < count; ++i)
299 geometry->SetMaterial(_material,
false);
307 return this->material;
315 this->PreRenderChildren();
316 this->PreRenderGeometries();
323 this->Geometries()->DestroyAll();
324 this->Children()->RemoveAll();
325 this->material.reset();
334 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
338 gzerr <<
"Cast failed in BaseVisual::PreRenderChildren" <<
std::endl;
341 for (
auto it = children_->Begin(); it != children_->End(); ++it)
351 unsigned int count = this->GeometryCount();
353 for (
unsigned int i = 0; i < count; ++i)
356 geometry->PreRender();
364 return this->wireframe;
371 gzerr <<
"SetWireframe(" << _show <<
") not supported for "
380 gzerr <<
"SetVisible(" << _visible <<
") not supported for "
396 gzerr <<
"SetStatic(" << _static <<
") not supported for "
409 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
413 gzerr <<
"Cast failed in BaseVisual::LocalBoundingBox" <<
std::endl;
416 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
418 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
437 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
444 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
446 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
448 box.
Merge(visual->BoundingBox());
457 this->SetVisibilityFlags(this->VisibilityFlags() | _flags);
464 this->SetVisibilityFlags(this->VisibilityFlags() & ~(_flags));
471 this->visibilityFlags = _flags;
475 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
479 gzerr <<
"Cast failed in BaseVisual::SetVisibiltyFlags" <<
std::endl;
482 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
484 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
486 visual->SetVisibilityFlags(_flags);
494 return this->visibilityFlags;
503 if (
nullptr == scene_)
505 gzerr <<
"Cloning a visual failed because the visual to be cloned is "
506 <<
"not attached to a scene.\n";
511 result = scene_->CreateVisual();
513 result = scene_->CreateVisual(_name);
515 if (
nullptr != _newParent)
517 auto parentScene = _newParent->Scene();
518 if (
nullptr != parentScene && parentScene->Id() != scene_->Id())
520 gzerr <<
"Cloning a visual failed because the desired parent of the "
521 <<
"cloned visual belongs to a different scene.\n";
522 scene_->DestroyVisual(result);
525 _newParent->AddChild(result);
528 result->SetOrigin(this->Origin());
529 result->SetInheritScale(this->InheritScale());
530 result->SetLocalScale(this->LocalScale());
531 result->SetLocalPose(this->LocalPose());
532 result->SetVisibilityFlags(this->VisibilityFlags());
533 result->SetWireframe(this->Wireframe());
537 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
541 gzerr <<
"Cast failed in BaseVisual::Clone\n";
542 scene_->DestroyVisual(result);
545 for (
auto it = children_->Begin(); it != children_->End(); ++it)
547 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
550 if (!visual || !visual->Clone(
"", result))
552 gzerr <<
"Cloning a child visual failed.\n";
553 scene_->DestroyVisual(result,
true);
558 for (
unsigned int i = 0; i < this->GeometryCount(); ++i)
559 result->AddGeometry(this->GeometryByIndex(i)->Clone());
562 result->SetMaterial(this->Material());
564 for (
const auto &[key, val] : this->userData)
565 result->SetUserData(key, val);
568 result->SetStatic(this->Static());