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;
321 this->PreRenderGeometries();
328 this->Geometries()->DestroyAll();
329 this->Children()->RemoveAll();
330 this->material.reset();
339 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
343 gzerr <<
"Cast failed in BaseVisual::PreRenderChildren" <<
std::endl;
346 for (
auto it = children_->Begin(); it != children_->End(); ++it)
356 unsigned int count = this->GeometryCount();
358 for (
unsigned int i = 0; i < count; ++i)
361 geometry->PreRender();
369 return this->wireframe;
376 gzerr <<
"SetWireframe(" << _show <<
") not supported for "
385 gzerr <<
"SetVisible(" << _visible <<
") not supported for "
401 gzerr <<
"SetStatic(" << _static <<
") not supported for "
414 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
418 gzerr <<
"Cast failed in BaseVisual::LocalBoundingBox" <<
std::endl;
421 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
423 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
442 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
449 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
451 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
453 box.
Merge(visual->BoundingBox());
462 this->SetVisibilityFlags(this->VisibilityFlags() | _flags);
469 this->SetVisibilityFlags(this->VisibilityFlags() & ~(_flags));
476 this->visibilityFlags = _flags;
480 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
484 gzerr <<
"Cast failed in BaseVisual::SetVisibiltyFlags" <<
std::endl;
487 for (
auto it = childNodes->Begin(); it != childNodes->End(); ++it)
489 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
491 visual->SetVisibilityFlags(_flags);
499 return this->visibilityFlags;
508 if (
nullptr == scene_)
510 gzerr <<
"Cloning a visual failed because the visual to be cloned is "
511 <<
"not attached to a scene.\n";
516 result = scene_->CreateVisual();
518 result = scene_->CreateVisual(_name);
520 if (
nullptr != _newParent)
522 auto parentScene = _newParent->Scene();
523 if (
nullptr != parentScene && parentScene->Id() != scene_->Id())
525 gzerr <<
"Cloning a visual failed because the desired parent of the "
526 <<
"cloned visual belongs to a different scene.\n";
527 scene_->DestroyVisual(result);
530 _newParent->AddChild(result);
533 result->SetOrigin(this->Origin());
534 result->SetInheritScale(this->InheritScale());
535 result->SetLocalScale(this->LocalScale());
536 result->SetLocalPose(this->LocalPose());
537 result->SetVisibilityFlags(this->VisibilityFlags());
538 result->SetWireframe(this->Wireframe());
542 std::dynamic_pointer_cast<BaseStore<gz::rendering::Node, T>>(
546 gzerr <<
"Cast failed in BaseVisual::Clone\n";
547 scene_->DestroyVisual(result);
550 for (
auto it = children_->Begin(); it != children_->End(); ++it)
552 VisualPtr visual = std::dynamic_pointer_cast<Visual>(*it);
555 if (!visual || !visual->Clone(
"", result))
557 gzerr <<
"Cloning a child visual failed.\n";
558 scene_->DestroyVisual(result,
true);
563 for (
unsigned int i = 0; i < this->GeometryCount(); ++i)
564 result->AddGeometry(this->GeometryByIndex(i)->Clone());
567 result->SetMaterial(this->Material());
569 for (
const auto &[key, val] : this->userData)
570 result->SetUserData(key, val);
573 result->SetStatic(this->Static());