VR-Engage  2.3
Loading...
Searching...
No Matches
entityOverlayComponent.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2026 MAK Technologies, Inc.
3** All rights reserved.
4******************************************************************************/
5
6//! \file entityHealthBarsComponent.h
7//! \brief Component for displaying QML overlays above tracked entities (e.g., health bars, name tags)
8
9#pragma once
10
11#include "vrvUtil/signalslib.h"
14#include "vreUtil/attribute.h"
15#include <vrfutil/uuid.h>
16#include <matrix/vlVector.h>
17
18#include <QObject>
19#include <QString>
20#include <QVariantMap>
21
22#include <vector>
23#include <string>
24#include <map>
25#include <unordered_set>
26#include <unordered_map>
27
28class QQuickItem;
29
30namespace makVrv
31{
32class DtSceneObject;
33class DtOsgChannel;
34} // namespace makVrv
35
36//! Component type identifier for factory registration and role configuration
37constexpr auto DtEntityOverlayComponentType = "DtEntityOverlayComponent";
38
39namespace makVre
40{
41
42using SensorContactsMap = std::map<DtUUID, DtEntityType>;
43using TrackedEntitiesMap = std::unordered_map<DtUUID, std::vector<DtAttributeHandle>>;
44
45//! \brief Simple structure to track individual entity overlay information
47{
48 DtEntityIdentifier entityId; //!< Entity being tracked
49 double screenX; //!< Projected screen X coordinate (pixels)
50 double screenY; //!< Projected screen Y coordinate (pixels)
51 double distance; //!< Distance from camera to entity (meters)
52 double additionalHeightAboveEntity; //!< Vertical screen offset (pixels) applied to the overlay position
53
54 QVariantMap extraData; //!< Additional per-entity fields consumed by specialized overlays
55
57 : entityId(DtEntityIdentifier::nullId())
58 , screenX(0.0)
59 , screenY(0.0)
60 , distance(0.0)
62 {
63 }
64
65 //! \brief Convert to QVariantMap for QML consumption
66 QVariantMap toVariantMap() const
67 {
68 QVariantMap map;
69 map["screenX"] = screenX;
70 map["screenY"] = screenY;
71 map["distance"] = distance;
72 map["additionalHeight"] = additionalHeightAboveEntity;
73
74 for (auto it = extraData.begin(); it != extraData.end(); ++it)
75 {
76 map[it.key()] = it.value();
77 }
78 return map;
79 }
80};
81
82//! \brief QML overlay component that displays items above tracked entities
83//!
84//! This component demonstrates how to:
85//! - Track multiple entities in the simulation
86//! - Project 3D world positions to 2D screen coordinates each frame
87//! - Update QML overlays with entity information via the attribute store
88//! - Handle entities going on/off screen
89//!
90//! The component inherits from DtQtQuickOverlay and combines QML overlay rendering
91//! with entity tracking logic. Each frame, it:
92//! 1. Queries entity positions from the EntityResolver
93//! 2. Projects 3D positions to screen coordinates using the main channel
94//! 3. Updates the player attribute store with screen positions + entity data
95//! 4. QML reactively displays items at the calculated positions
96//!
97//! \see DtQtQuickOverlay for QML overlay base functionality
98//! \see DtPlayerAttributeStore for reactive data binding with QML
100{
101public:
102 //! \brief Constructor
104
105 //! \brief Destructor
106 virtual ~DtEntityOverlayComponent() override;
107
108 //! \brief Returns the component type identifier
109 //! \return Component type string used for factory registration
110 virtual const char* type() const override;
111
112 //! \brief Initializes the component with player and configuration
113 //! \param player Pointer to the player station
114 //! \param config Configuration table with component settings
115 //! \return True if initialization succeeded, false otherwise
116 virtual bool initialize(DtPlayerStation* player, DtInitTable& config) override;
117
118 //! \brief Performs post-initialization after all components are created
119 //! \return True if post-initialization succeeded, false otherwise
120 //!
121 //! Sets up the QML overlay and discovers entities to track
122 virtual bool postInitialize() override;
123
124 //! \brief Per-frame update called by the player station
125 //! \param dt Delta time since last tick in seconds
126 //!
127 //! Updates entity positions and projects them to screen coordinates
128 virtual void tick(double dt) override;
129
130 //! \brief Cleanup when component is being destroyed
131 virtual void shutdown() override;
132
133 //! \brief Called when QML root item is loaded
134 //! \param rootItem The root QML item
135 //!
136 //! Overridden to get a reference to the QML data object for direct property setting
137 virtual void initdoc(QQuickItem* rootItem) override;
138
139protected:
140 //! \brief Discovers and updates entity-specific data each frame (proof of concept)
141 //!
142 //! Queries EntityResolver each frame to rebuild the list of entities to track
143 //! and fetches entity-specific information from the simulation (health, position, etc.)
144 virtual void updateEntityData();
145
146 //! \brief Populates overlay-specific data fields for a tracked entity
147 //! \param info Overlay info structure to populate
148 //! \param entityRootAttr Root attribute handle for the tracked entity
149 virtual void populateEntityData(EntityOverlayInfo& info, DtAttributeHandle& entityRootAttr);
150
151 //! \brief Ensures an entity is registered in the tracked entities cache
152 //! \param uuid The entity's UUID from the sensor contacts
153 //! \param entityIdStr The entity identifier string for attribute store lookup
154 virtual void ensureEntityTracked(const DtUUID& uuid, const std::string& entityIdStr);
155
156 //! \brief Computes the world-space position of the top center of an entity's bounding volume
157 //! \param entityAttrRoot Root attribute handle for the entity
158 //! \param sceneObj The entity's scene object for coordinate transformation
159 //! \param[out] sceneObjPoint The resulting scene-object-space position
160 //! \param[out] worldPoint The resulting world (database) coordinates
161 virtual void getEntityTopWorldPosition(DtAttributeHandle& entityAttrRoot,
162 makVrv::DtSceneObject* sceneObj,
163 DtVector& sceneObjPoint,
164 DtVector& worldPoint) const;
165
166 //! \brief Removes tracked entities that are no longer present in the sensor contacts
167 //! \param contacts The current sensor contacts to check against
168 virtual void removeStaleTrackedEntities(const SensorContactsMap& contacts);
169
170 //! \brief Converts a world-space height offset above an entity into a screen-space pixel offset
171 //! \param sceneObjPoint The entity's position in scene-object coordinates
172 //! \param heightAboveEntity The height offset in meters to convert
173 //! \param sceneObj The scene object used for coordinate transformation
174 //! \param baseScreenY The base screen Y coordinate (OpenGL convention) of the entity
175 //! \return The equivalent offset in screen-space pixels (QML Y-down convention)
176 virtual double computeScreenHeightOffset(const DtVector& sceneObjPoint,
177 double heightAboveEntity,
178 makVrv::DtSceneObject* sceneObj,
179 double baseScreenY) const;
180
181 //! \brief Responds to runtime overlay visibility changes from the player attribute store
182 //! \param enabled True to show overlays, false to hide them
183 virtual void onOverlayEnabledChanged(bool enabled);
184
185 //! \brief Sensor contacts used to discover entities that should receive overlays
186 //! \vreAttrKey{sensors.annotation-proximity-sensor.contacts}
188
189 //! \brief Cached per-entity attribute handles for currently tracked contacts
191
192 //! \brief Attribute paths to keep synchronized for tracked external entities
193 std::unordered_set<std::string> myExternalEntityAttributes;
194
195 //! \brief Pointer to the QML data object for direct property updates
197
198 //! \brief Base world-space height offset in meters applied above each entity
200
201 //! \brief Name of the sensor to read contacts from in the attribute store
202 std::string mySensorName;
203
204 //! \brief Player attribute controlling whether entity overlays are visible
205 //! \vreAttrKey{showEntityOverlays}
207};
208} // namespace makVre
Provides attribute handling system for hierarchical data storage and manipulation.
Handle class for safe access to attributes.
Definition attributeHandle.h:30
Type-specific handle class for attributes.
Definition attributeHandle.h:229
double myAdditionalHeightAboveEntity
Base world-space height offset in meters applied above each entity.
Definition entityOverlayComponent.h:199
virtual void removeStaleTrackedEntities(const SensorContactsMap &contacts)
Removes tracked entities that are no longer present in the sensor contacts.
virtual ~DtEntityOverlayComponent() override
Destructor.
virtual const char * type() const override
Returns the component type identifier.
std::string mySensorName
Name of the sensor to read contacts from in the attribute store.
Definition entityOverlayComponent.h:202
virtual bool initialize(DtPlayerStation *player, DtInitTable &config) override
Initializes the component with player and configuration.
virtual void ensureEntityTracked(const DtUUID &uuid, const std::string &entityIdStr)
Ensures an entity is registered in the tracked entities cache.
virtual void getEntityTopWorldPosition(DtAttributeHandle &entityAttrRoot, makVrv::DtSceneObject *sceneObj, DtVector &sceneObjPoint, DtVector &worldPoint) const
Computes the world-space position of the top center of an entity's bounding volume.
virtual void onOverlayEnabledChanged(bool enabled)
Responds to runtime overlay visibility changes from the player attribute store.
virtual void populateEntityData(EntityOverlayInfo &info, DtAttributeHandle &entityRootAttr)
Populates overlay-specific data fields for a tracked entity.
DtAttributeHandleType< SensorContactsMap > mySensorContactsAttr
Sensor contacts used to discover entities that should receive overlays.
Definition entityOverlayComponent.h:187
virtual bool postInitialize() override
Performs post-initialization after all components are created.
virtual void shutdown() override
Cleanup when component is being destroyed.
virtual void initdoc(QQuickItem *rootItem) override
Called when QML root item is loaded.
std::unordered_set< std::string > myExternalEntityAttributes
Attribute paths to keep synchronized for tracked external entities.
Definition entityOverlayComponent.h:193
QObject * myQmlDataObject
Pointer to the QML data object for direct property updates.
Definition entityOverlayComponent.h:196
TrackedEntitiesMap myTrackedEntitesMap
Cached per-entity attribute handles for currently tracked contacts.
Definition entityOverlayComponent.h:190
virtual void tick(double dt) override
Per-frame update called by the player station.
virtual void updateEntityData()
Discovers and updates entity-specific data each frame (proof of concept)
virtual double computeScreenHeightOffset(const DtVector &sceneObjPoint, double heightAboveEntity, makVrv::DtSceneObject *sceneObj, double baseScreenY) const
Converts a world-space height offset above an entity into a screen-space pixel offset.
DtAttributeHandleType< bool > myOverlayEnabledAttr
Player attribute controlling whether entity overlays are visible.
Definition entityOverlayComponent.h:206
Table-based access to Lua state for configuration data.
Definition initializer.h:38
virtual DtPlayerStation & player()
Gets the player station.
Class for managing the user interface for engaged roles.
Definition playerStation.h:51
DtQtQuickOverlay()
Constructor for DtQtQuickOverlay.
constexpr auto DtEntityOverlayComponentType
Component type identifier for factory registration and role configuration.
Definition entityOverlayComponent.h:37
Defines export/import macros for the vreCommonComponents library.
#define VRECOMMONCOMPONENTS_DLL
DLL export/import macro for the vreCommonComponents library.
Definition export.h:28
!
Definition glsVreMessageUtil.h:50
std::unordered_map< DtUUID, std::vector< DtAttributeHandle > > TrackedEntitiesMap
Definition entityOverlayComponent.h:43
std::map< DtUUID, DtEntityType > SensorContactsMap
Definition entityOverlayComponent.h:42
Definition appLauncherComponent.h:28
Contains DtQtQuickOverlay class which provides a Qt Quick overlay component for rendering 2D UI over ...
Simple structure to track individual entity overlay information.
Definition entityOverlayComponent.h:47
double distance
Distance from camera to entity (meters)
Definition entityOverlayComponent.h:51
QVariantMap extraData
Additional per-entity fields consumed by specialized overlays.
Definition entityOverlayComponent.h:54
DtEntityIdentifier entityId
Entity being tracked.
Definition entityOverlayComponent.h:48
double screenY
Projected screen Y coordinate (pixels)
Definition entityOverlayComponent.h:50
QVariantMap toVariantMap() const
Convert to QVariantMap for QML consumption.
Definition entityOverlayComponent.h:66
EntityOverlayInfo()
Definition entityOverlayComponent.h:56
double screenX
Projected screen X coordinate (pixels)
Definition entityOverlayComponent.h:49
double additionalHeightAboveEntity
Vertical screen offset (pixels) applied to the overlay position.
Definition entityOverlayComponent.h:52