3939#include < gz/rendering/RenderingIface.hh>
4040#include < gz/rendering/Scene.hh>
4141
42+ #include < gz/sim/components/BoundingBoxCamera.hh>
4243#include < gz/sim/components/Camera.hh>
44+ #include < gz/sim/components/DepthCamera.hh>
45+ #include < gz/sim/components/RgbdCamera.hh>
46+ #include < gz/sim/components/SegmentationCamera.hh>
47+ #include < gz/sim/components/ThermalCamera.hh>
48+ #include < gz/sim/components/WideAngleCamera.hh>
4349#include < gz/sim/components/Model.hh>
4450#include < gz/sim/components/Name.hh>
4551#include < gz/sim/components/ParentEntity.hh>
@@ -104,6 +110,28 @@ class CameraZoomPlugin::Impl
104110 return std::optional<sim::Entity>(parent->Data ());
105111 }
106112
113+ // / \todo(srmainwaring) replace with `gz::sim::Sensor` when available.
114+ // / \brief Get the sdf::Sensor stored in any camera-style component
115+ // / (Camera, BoundingBoxCamera, DepthCamera, RgbdCamera,
116+ // / SegmentationCamera, ThermalCamera or WideAngleCamera) attached to
117+ // / the given sensor entity.
118+ // / \note LogicalCamera is intentionally excluded: its sdf::Sensor does
119+ // / not carry an sdf::Camera, so zoom semantics do not apply.
120+ // / \note By convention, gz-sim's SdfEntityCreator attaches exactly one
121+ // / camera-family component per sensor entity (matched to the SDF
122+ // / `<sensor type="...">`); the lookup order below is therefore
123+ // / significant only if that invariant is ever broken.
124+ // / \param[in] _ecm Entity component manager.
125+ // / \param[in] _entity Sensor entity to query.
126+ // / \param[out] _typeId Always written. Set to the matched component
127+ // / type id on success, or to kComponentTypeIdInvalid on failure.
128+ // / \return Pointer to sdf::Sensor held by the matched component, or
129+ // / nullptr if no supported camera component is attached.
130+ public: sdf::Sensor *CameraSensorSdf (
131+ EntityComponentManager &_ecm,
132+ Entity _entity,
133+ ComponentTypeId &_typeId) const ;
134+
107135 // / \brief World occupied by the parent model.
108136 public: World world{kNullEntity };
109137
@@ -239,6 +267,51 @@ void CameraZoomPlugin::Impl::InitialiseCamera()
239267 }
240268}
241269
270+ // ////////////////////////////////////////////////
271+ sdf::Sensor *CameraZoomPlugin::Impl::CameraSensorSdf (
272+ EntityComponentManager &_ecm,
273+ Entity _entity,
274+ ComponentTypeId &_typeId) const
275+ {
276+ _typeId = kComponentTypeIdInvalid ;
277+ if (auto *c = _ecm.Component <components::Camera>(_entity))
278+ {
279+ _typeId = components::Camera::typeId;
280+ return &c->Data ();
281+ }
282+ if (auto *c = _ecm.Component <components::BoundingBoxCamera>(_entity))
283+ {
284+ _typeId = components::BoundingBoxCamera::typeId;
285+ return &c->Data ();
286+ }
287+ if (auto *c = _ecm.Component <components::DepthCamera>(_entity))
288+ {
289+ _typeId = components::DepthCamera::typeId;
290+ return &c->Data ();
291+ }
292+ if (auto *c = _ecm.Component <components::RgbdCamera>(_entity))
293+ {
294+ _typeId = components::RgbdCamera::typeId;
295+ return &c->Data ();
296+ }
297+ if (auto *c = _ecm.Component <components::SegmentationCamera>(_entity))
298+ {
299+ _typeId = components::SegmentationCamera::typeId;
300+ return &c->Data ();
301+ }
302+ if (auto *c = _ecm.Component <components::ThermalCamera>(_entity))
303+ {
304+ _typeId = components::ThermalCamera::typeId;
305+ return &c->Data ();
306+ }
307+ if (auto *c = _ecm.Component <components::WideAngleCamera>(_entity))
308+ {
309+ _typeId = components::WideAngleCamera::typeId;
310+ return &c->Data ();
311+ }
312+ return nullptr ;
313+ }
314+
242315// ////////////////////////////////////////////////
243316void CameraZoomPlugin::Impl::OnRenderTeardown ()
244317{
@@ -409,8 +482,10 @@ void CameraZoomPlugin::PreUpdate(
409482 // / \todo(srmainwaring) replace with `gz::sim::Sensor` when available.
410483 // Entity cameraEntity = this->impl->cameraSensor.Entity();
411484 Entity cameraEntity = this ->impl ->cameraSensorEntity ;
412- auto comp = _ecm.Component <components::Camera>(cameraEntity);
413- if (!comp)
485+ ComponentTypeId sensorTypeId = kComponentTypeIdInvalid ;
486+ sdf::Sensor *sensorSdf =
487+ this ->impl ->CameraSensorSdf (_ecm, cameraEntity, sensorTypeId);
488+ if (!sensorSdf)
414489 return ;
415490
416491 if (this ->impl ->zoomChanged )
@@ -430,8 +505,7 @@ void CameraZoomPlugin::PreUpdate(
430505 }
431506
432507 // Update component.
433- sdf::Sensor &sensor = comp->Data ();
434- sdf::Camera *cameraSdf = sensor.CameraSensor ();
508+ sdf::Camera *cameraSdf = sensorSdf->CameraSensor ();
435509 if (!cameraSdf)
436510 return ;
437511
@@ -478,7 +552,7 @@ void CameraZoomPlugin::PreUpdate(
478552 sensorWidth, newFocalLength);
479553 // Update rendering camera with the latest focal length.
480554 cameraSdf->SetHorizontalFov (newHfov);
481- _ecm.SetChanged (cameraEntity, components::Camera::typeId ,
555+ _ecm.SetChanged (cameraEntity, sensorTypeId ,
482556 ComponentState::OneTimeChange);
483557
484558 // Update rendering camera.
0 commit comments