VR-Engage  2.3
Loading...
Searching...
No Matches
vortexInterface.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2024 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file vortexInterface.h
7//! \brief Provides interface functionality between VR-Forces and the Vortex physics engine
8//! \ingroup vreVrfVortex
9
10#pragma once
11
12#include <vreVrfVortex/export.h>
13
14#include <VxSim/VxSmartInterface.h>
15#include <VxSim/VxVHLInterface.h>
16#include <VxData/Delegate.h>
17
18#include <functional>
19#include <string>
20#include <vector>
21
22namespace makVre
23{
25}
26
27class DtInputPortGroup;
28class DtOutputPortGroup;
29class DtSingleConnectionInputPort;
30class DtSingleConnectionOutputPort;
31class DtVrfObjectStateRepository;
32
33namespace VxData
34{
35class FieldBase;
36}
37
38using DtVortexInterfaceFieldCallback = std::function<void(const VxData::FieldBase&)>;
39
40namespace VxSim
41{
42class VxExtension;
43class VxPluginExtension;
44class VxVHLInterface;
45class VxSimulationState;
46} // namespace VxSim
47
48//! \brief Base class for Vortex interface elements
49//!
50//! Provides a common interface for all Vortex interface elements, allowing for
51//! type-agnostic handling of different port types.
53{
54public:
55 //! \brief Default constructor
57
58 //! \brief Virtual destructor
59 //!
60 //! Ensures proper cleanup of derived classes.
62
63 //! \brief Updates the interface element
64 //!
65 //! Pure virtual function that must be implemented by derived classes
66 //! to handle data transfer between ports and Vortex fields.
67 virtual void update() = 0;
68
69 //! \brief Gets the port name
70 //!
71 //! \return The name of the port
72 virtual std::string portName() = 0;
73
74 //! \brief Gets the field name
75 //!
76 //! \return The name of the field
77 virtual std::string getFieldName() = 0;
78
79 //! \brief Sets the Vortex field associated with this element
80 //!
81 //! \param field Pointer to the Vortex field
82 virtual void setField(VxData::FieldBase* field) = 0;
83
84 //! \brief Gets the Vortex field associated with this element
85 //!
86 //! \return Pointer to the Vortex field
87 virtual VxData::FieldBase* getField() const = 0;
88
89 //! \brief Adds an observer to this element's Vortex field
90 //!
91 //! \param callback Function called when the field changes
92 //! \return Delegate connection that can disconnect or destroy the observer
93 virtual VxData::DelegateConnection addFieldObserver(const DtVortexInterfaceFieldCallback& callback) = 0;
94};
95
96//---------------------------------------------------------------------------------------------------------------------
97
98//! \brief Template class for Vortex interface elements
99//!
100//! Implements the DtVortexInterfaceBaseElement interface for specific port types.
101//! Handles the connection between VR-Forces ports and Vortex fields.
102template <typename T>
104{
105public:
106 //! \brief Constructor
107 //!
108 //! Creates and initializes the port with the specified names.
109 //!
110 //! \param portName Name of the port
111 //! \param vortexName Name of the Vortex field
112 DtVortexInterfaceElement(std::string portName, const std::string& vortexName);
113
114 //! \brief Destructor
115 //!
116 //! Cleans up resources.
117 virtual ~DtVortexInterfaceElement() override;
118
119 //! \brief Updates the interface element
120 //!
121 //! Transfers data between the port and the Vortex field.
122 //! The implementation of this method is specialized for different port types.
123 virtual void update() override;
124
125 //! \brief Gets the port
126 //!
127 //! Provides access to the underlying port object.
128 //!
129 //! \return Pointer to the port
130 T* port() { return myPort; }
131
132 //! \brief Sets the Vortex field
133 //!
134 //! Associates a Vortex field with this interface element.
135 //!
136 //! \param field Pointer to the Vortex field
137 virtual void setField(VxData::FieldBase* field) override;
138
139 //! \brief Gets the Vortex field
140 //!
141 //! \return Pointer to the Vortex field
142 virtual VxData::FieldBase* getField() const override;
143
144 //! \brief Gets the port name
145 //!
146 //! \return The name of the port
147 virtual std::string portName() override;
148
149 //! \brief Gets the field name
150 //!
151 //! \return The name of the field
152 virtual std::string getFieldName() override;
153
154 //! \brief Adds an observer to this element's Vortex field
155 //!
156 //! \param callback Function called when the field changes
157 //! \return Delegate connection that can disconnect or destroy the observer
158 virtual VxData::DelegateConnection addFieldObserver(const DtVortexInterfaceFieldCallback& callback) override;
159
160protected:
161 T* myPort; //!< Pointer to the port
162 VxData::FieldBase* myVortexField; //!< Pointer to the Vortex field
163 std::string myPortName; //!< Name of the port
164 std::string myFieldName; //!< Name of the field
165};
166
167//! \brief Gets the field name
168//!
169//! \return The name of the field
170template <typename T>
172{
173 return myFieldName;
174}
175
176//! \brief Gets the port name
177//!
178//! \return The name of the port
179template <typename T>
181{
182 return myPortName;
183}
184
185//! \brief Gets the Vortex field
186//!
187//! \return Pointer to the Vortex field
188template <typename T>
190{
191 return myVortexField;
192}
193
194//! \brief Sets the Vortex field
195//!
196//! \param field Pointer to the Vortex field
197template <typename T>
198void DtVortexInterfaceElement<T>::setField(VxData::FieldBase* field)
199{
200 myVortexField = field;
201}
202
203template <typename T>
205{
206public:
207 DtVortexInterfaceVectorElement(const std::string& portName, const std::string& vortexName)
208 : DtVortexInterfaceElement<T>(portName, vortexName)
209 {
210 }
211
212 virtual void setIsOffsetVector(bool on) { myIsOffsetVector = on; }
213 virtual bool isOffsetVector() const { return myIsOffsetVector; }
214
215 virtual void setUseWorldCoords(bool on) { myUseWorldCoords = on; }
216 virtual bool useWorldCoords() const { return myUseWorldCoords; }
217
218 virtual void update() override;
219
220protected:
221 bool myIsOffsetVector = false;
222 bool myUseWorldCoords = false;
223};
224
225//---------------------------------------------------------------------------------------------------------------------
226
227//! \brief Class for managing Vortex interfaces
228//!
229//! Provides functionality for creating and managing the connection between VR-Forces ports
230//! and Vortex interfaces. Handles both input and output ports.
232{
233public:
234 //! \brief Constructor
235 //!
236 //! Creates ports based on the descriptor and optionally connects them to the Vortex interface.
237 //!
238 //! \param desc Descriptor containing interface configuration
239 //! \param iface Optional Vortex interface to connect to
241 const VxSim::VxWeakInterface<VxSim::VxVHLInterface>& iface = nullptr);
242
243 //! \brief Destructor
244 //!
245 //! Cleans up resources.
247
248 //! \brief Gets the input port group
249 //!
250 //! Provides access to all input ports.
251 //!
252 //! \return Pointer to the input port group
253 virtual DtInputPortGroup* inputPortGroup();
254
255 //! \brief Gets the output port group
256 //!
257 //! Provides access to all output ports.
258 //!
259 //! \return Pointer to the output port group
260 virtual DtOutputPortGroup* outputPortGroup();
261
262 //! \brief Updates all input elements
263 //!
264 //! Transfers data from input ports to Vortex fields.
265 virtual void updateInputs();
266
267 //! \brief Updates all output elements
268 //!
269 //! Transfers data from Vortex fields to output ports.
270 virtual void updateOutputs();
271
272 //! \brief Updates the object state repository with output values
273 //!
274 //! Transfers data from output ports to the state repository.
275 //!
276 //! \param esr Pointer to the object state repository
277 virtual void updateOutputStateRepository(DtVrfObjectStateRepository* esr);
278
279 //! \brief Sets the Vortex interface
280 //!
281 //! \param iface Vortex interface to connect to
282 virtual void setInterface(VxSim::VxWeakInterface<VxSim::VxVHLInterface> iface);
283
284 //! \brief Connects ports to the Vortex interface
285 //!
286 //! Associates each port with the corresponding Vortex field.
288
289 //! \brief Gets an input element by name
290 //!
291 //! \param name Name of the input element
292 //! \return Pointer to the input element, or nullptr if not found
293 virtual DtVortexInterfaceBaseElement* getInputElement(const std::string& name);
294
295 //! \brief Gets an output element by name
296 //!
297 //! \param name Name of the output element
298 //! \return Pointer to the output element, or nullptr if not found
299 virtual DtVortexInterfaceBaseElement* getOutputElement(const std::string& name);
300
301 //! \brief Adds an observer to an input element's Vortex field
302 //!
303 //! \param name Name of the input element
304 //! \param callback Function called when the field changes
305 //! \return Delegate connection that can disconnect or destroy the observer
306 virtual VxData::DelegateConnection addInputElementObserver(const std::string& name,
307 const DtVortexInterfaceFieldCallback& callback);
308
309 //! \brief Adds an observer to an output element's Vortex field
310 //!
311 //! \param name Name of the output element
312 //! \param callback Function called when the field changes
313 //! \return Delegate connection that can disconnect or destroy the observer
314 virtual VxData::DelegateConnection addOutputElementObserver(const std::string& name,
315 const DtVortexInterfaceFieldCallback& callback);
316
317 //! \brief Gets the interface name
318 //!
319 //! \return The name of the interface
320 virtual std::string getInterfaceName();
321
322protected:
323 //! \brief Creates ports based on the descriptor
324 //!
325 //! Creates input and output ports of appropriate types according to the descriptor.
326 //!
327 //! \param desc Descriptor containing interface configuration
329
330 VxSim::VxWeakInterface<VxSim::VxVHLInterface> myInterface; //!< Vortex interface
331 std::string myInterfaceName; //!< Interface name
332 DtInputPortGroup* myInputPortGroup; //!< Input port group
333 DtOutputPortGroup* myOutputPortGroup; //!< Output port group
334 std::vector<DtVortexInterfaceBaseElement*> myInputElements; //!< Vector of input elements
335 std::vector<DtVortexInterfaceBaseElement*> myOutputElements; //!< Vector of output elements
336};
Base class for Vortex interface elements.
Definition vortexInterface.h:53
virtual void update()=0
Updates the interface element.
virtual void setField(VxData::FieldBase *field)=0
Sets the Vortex field associated with this element.
virtual VxData::DelegateConnection addFieldObserver(const DtVortexInterfaceFieldCallback &callback)=0
Adds an observer to this element's Vortex field.
virtual std::string portName()=0
Gets the port name.
virtual VxData::FieldBase * getField() const =0
Gets the Vortex field associated with this element.
DtVortexInterfaceBaseElement()
Default constructor.
Definition vortexInterface.h:56
virtual ~DtVortexInterfaceBaseElement()
Virtual destructor.
Definition vortexInterface.h:61
virtual std::string getFieldName()=0
Gets the field name.
virtual VxData::FieldBase * getField() const override
Gets the Vortex field.
Definition vortexInterface.h:189
virtual ~DtVortexInterfaceElement() override
Destructor.
VxData::FieldBase * myVortexField
Pointer to the Vortex field.
Definition vortexInterface.h:162
T * myPort
Pointer to the port.
Definition vortexInterface.h:161
virtual std::string getFieldName() override
Gets the field name.
Definition vortexInterface.h:171
std::string myPortName
Name of the port.
Definition vortexInterface.h:163
virtual std::string portName() override
Gets the port name.
Definition vortexInterface.h:180
virtual VxData::DelegateConnection addFieldObserver(const DtVortexInterfaceFieldCallback &callback) override
Adds an observer to this element's Vortex field.
virtual void setField(VxData::FieldBase *field) override
Sets the Vortex field.
Definition vortexInterface.h:198
virtual void update() override
Updates the interface element.
std::string myFieldName
Name of the field.
Definition vortexInterface.h:164
T * port()
Gets the port.
Definition vortexInterface.h:130
DtVortexInterfaceElement(std::string portName, const std::string &vortexName)
Constructor.
DtOutputPortGroup * myOutputPortGroup
Output port group.
Definition vortexInterface.h:333
std::string myInterfaceName
Interface name.
Definition vortexInterface.h:331
virtual VxData::DelegateConnection addOutputElementObserver(const std::string &name, const DtVortexInterfaceFieldCallback &callback)
Adds an observer to an output element's Vortex field.
std::vector< DtVortexInterfaceBaseElement * > myOutputElements
Vector of output elements.
Definition vortexInterface.h:335
virtual DtInputPortGroup * inputPortGroup()
Gets the input port group.
virtual VxData::DelegateConnection addInputElementObserver(const std::string &name, const DtVortexInterfaceFieldCallback &callback)
Adds an observer to an input element's Vortex field.
virtual void connectPortsToInterface()
Connects ports to the Vortex interface.
virtual void setInterface(VxSim::VxWeakInterface< VxSim::VxVHLInterface > iface)
Sets the Vortex interface.
virtual ~DtVortexInterface()
Destructor.
virtual void createPorts(makVre::DtVortexInterfaceDescriptor *desc)
Creates ports based on the descriptor.
std::vector< DtVortexInterfaceBaseElement * > myInputElements
Vector of input elements.
Definition vortexInterface.h:334
DtInputPortGroup * myInputPortGroup
Input port group.
Definition vortexInterface.h:332
virtual std::string getInterfaceName()
Gets the interface name.
virtual DtVortexInterfaceBaseElement * getOutputElement(const std::string &name)
Gets an output element by name.
virtual void updateOutputs()
Updates all output elements.
DtVortexInterface(makVre::DtVortexInterfaceDescriptor *desc, const VxSim::VxWeakInterface< VxSim::VxVHLInterface > &iface=nullptr)
Constructor.
virtual DtVortexInterfaceBaseElement * getInputElement(const std::string &name)
Gets an input element by name.
virtual void updateOutputStateRepository(DtVrfObjectStateRepository *esr)
Updates the object state repository with output values.
VxSim::VxWeakInterface< VxSim::VxVHLInterface > myInterface
Vortex interface.
Definition vortexInterface.h:330
virtual DtOutputPortGroup * outputPortGroup()
Gets the output port group.
virtual void updateInputs()
Updates all input elements.
virtual void setUseWorldCoords(bool on)
Definition vortexInterface.h:215
virtual void update() override
Updates the interface element.
bool myIsOffsetVector
Definition vortexInterface.h:221
DtVortexInterfaceVectorElement(const std::string &portName, const std::string &vortexName)
Definition vortexInterface.h:207
bool myUseWorldCoords
Definition vortexInterface.h:222
virtual void setIsOffsetVector(bool on)
Definition vortexInterface.h:212
virtual bool useWorldCoords() const
Definition vortexInterface.h:216
virtual bool isOffsetVector() const
Definition vortexInterface.h:213
Descriptor for Vortex physics engine interfaces.
Definition vortexInterfaceDescriptor.h:27
Export macros for the VR-Forces Vortex extension library.
#define VREVRFVORTEX_DLL
Export macro for non-Windows platforms.
Definition export.h:30
Definition vortexConfiguration.h:24
Definition vortexConfiguration.h:29
!
Definition glsVreMessageUtil.h:50
std::function< void(const VxData::FieldBase &)> DtVortexInterfaceFieldCallback
Definition vortexInterface.h:38