VTK  9.4.20241222
vtkVRCollaborationClient.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
4// This class provides collaboration support for VR using avatars
5// It relies on ZeroMQ to communicate with a collaboration server
6// to exchange avatar names and poses and potentially other messages.
7
8#ifndef vtkVRCollaborationClient_h
9#define vtkVRCollaborationClient_h
10
11#include "vtkEventData.h" // for ivars
12#include "vtkLogger.h" // for Verbosity enum
13#include "vtkObject.h"
14#include "vtkRenderingVRModule.h" // For export macro
15#include "vtkSmartPointer.h" // method sig
16#include "vtksys/CommandLineArguments.hxx" // for method sig
17#include <array> // for ivar
18#include <functional> // for ivars
19#include <map> // for ivars
20#include <memory> // for ivars, shared_ptr
21#include <string> // for ivars
22#include <vector> // for ivars
23
24VTK_ABI_NAMESPACE_BEGIN
26class vtkOpenGLAvatar;
29class vtkTransform;
30class vtkVRCollaborationClientInternal;
31
32class VTKRENDERINGVR_EXPORT vtkVRCollaborationClient : public vtkObject
33{
34public:
37 void PrintSelf(ostream& os, vtkIndent indent) override;
40
41 // when sending messages we have to marshal arguments so we have a simple
42 // class to encapsulate an argument. The method to send a message takes a
43 // std::vector of arguments and there is a method to return a std::vector
44 // of arguments when receiving a message
46 {
47 Double = 0,
49 String
50 };
51
52 class VTKRENDERINGVR_EXPORT Argument
53 {
54 public:
55 bool GetString(std::string& result);
56 void SetString(std::string const& in);
57 bool GetStringVector(std::vector<std::string>& result);
58 void SetStringVector(std::vector<std::string> const& in);
59
60 bool GetDoubleVector(std::vector<double>& result);
61 void SetDoubleVector(double const* in, uint16_t size);
62 void SetDouble(double val);
63 bool GetDouble(double& result);
64
65 bool GetInt32Vector(std::vector<int32_t>& result);
66 void SetInt32Vector(int32_t const* in, uint16_t size);
67 void SetInt32(int32_t val);
68 bool GetInt32(int32_t& result);
69
71 uint16_t Count = 0;
72 std::shared_ptr<void> Data;
73 };
74
76
79 void SendAMessage(std::string const& msgType);
80 void SendAMessage(std::string const& msgType, std::vector<Argument> const& args);
81 std::vector<Argument> GetMessageArguments();
82 void SendPoseMessage(std::string const& msgType, int index, double pos[3], double dir[3]);
84
85 // call during the render loop to handle collaboration messages
86 virtual void Render();
87
88 // required call, true on success, pass the renderer you want the avatars added to
90
91 // close the connection
92 void Disconnect();
93
94 // set the values for the collaboration connection
95 // Can be done through Set* methods or by passing in
96 // the command line arguments via AddArguments
97 virtual void AddArguments(vtksys::CommandLineArguments& arguments);
98 void SetCollabHost(std::string const& val) { this->CollabHost = val; }
99 void SetCollabSession(std::string const& val) { this->CollabSession = val; }
100 void SetCollabName(std::string const& val) { this->CollabName = val; }
101 void SetCollabPort(int val) { this->CollabPort = val; }
102
103 // to receive log/warning/error output
105 std::function<void(std::string const& data, vtkLogger::Verbosity verbosity)> cb)
106 {
107 this->Callback = cb;
108 }
109
110 // to override the default method of getting avatar scales
111 void SetScaleCallback(std::function<double()> cb) { this->ScaleCallback = cb; }
112
113 // return the renderer being used by this instance (assigned during Initialize())
114 vtkOpenGLRenderer* GetRenderer() { return this->Renderer; }
115
116 // is this instance connected to a collaboration server?
117 bool GetConnected() { return this->Connected; }
118
120
123 vtkSetVector3Macro(AvatarInitialUpVector, double);
124 vtkGetVector3Macro(AvatarInitialUpVector, double);
126
128
137
138protected:
141
142 void Log(vtkLogger::Verbosity verbosity, std::string const& msg);
143
144 // provided values
145 std::string CollabID;
146 std::string CollabHost;
147 std::string CollabSession;
148 std::string CollabName;
150
151 std::function<void(std::string const& data, vtkLogger::Verbosity)> Callback;
152 std::function<double()> ScaleCallback;
153
157 bool AvatarIdle(std::string id);
160 double NeedReply;
162
164
165 // get existing avatar, or create new one, if needed, and return it.
167
170
171 virtual void HandleBroadcastMessage(std::string const& otherID, std::string const& type);
172
174 static void EventCallback(
175 vtkObject* object, unsigned long event, void* clientdata, void* calldata);
177
180
182
183 // used to throttle outgoing pose messages
185 bool HasPoseForDevice[vtkEventDataNumberOfDevices];
186 struct Pose
187 {
188 std::array<double, 3> Position;
189 std::array<double, 4> Orientation;
190 };
192
193 double AvatarInitialUpVector[3];
194
195 // dynamic set of avatars, keyed on IDs sent with updates.
196 std::map<std::string, vtkSmartPointer<vtkOpenGLAvatar>> Avatars;
197 std::map<std::string, double[vtkEventDataNumberOfDevices]> AvatarUpdateTime;
198
199 // PIMPL to keep zeromq out of the interface for this class
200 vtkVRCollaborationClientInternal* Internal;
201};
202
203VTK_ABI_NAMESPACE_END
204#endif
supports function callbacks
a simple class to control print indentation
Definition vtkIndent.h:108
Allocate and hold a VTK object.
Definition vtkNew.h:167
abstract base class for most VTK objects
Definition vtkObject.h:162
OpenGL Avatar.
OpenGL rendering window.
OpenGL renderer.
Hold a reference to a vtkObjectBase instance.
describes linear transformations via a 4x4 matrix
void SetDoubleVector(double const *in, uint16_t size)
bool GetStringVector(std::vector< std::string > &result)
bool GetDouble(double &result)
void SetStringVector(std::vector< std::string > const &in)
bool GetInt32(int32_t &result)
void SetInt32Vector(int32_t const *in, uint16_t size)
bool GetString(std::string &result)
bool GetDoubleVector(std::vector< double > &result)
void SetString(std::string const &in)
bool GetInt32Vector(std::vector< int32_t > &result)
static vtkVRCollaborationClient * New()
bool AvatarIdle(std::string id)
static void EventCallback(vtkObject *object, unsigned long event, void *clientdata, void *calldata)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void SendAMessage(std::string const &msgType, std::vector< Argument > const &args)
method signatures to send messages with Arguments and extract them out of messages
std::map< std::string, double[vtkEventDataNumberOfDevices]> AvatarUpdateTime
vtkOpenGLRenderWindow * RenderWindow
vtkVRCollaborationClient & operator=(const vtkVRCollaborationClient &)=delete
vtkVRCollaborationClient(const vtkVRCollaborationClient &)=delete
~vtkVRCollaborationClient() override
void SetCollabSession(std::string const &val)
void SendAMessage(std::string const &msgType)
method signatures to send messages with Arguments and extract them out of messages
vtkVRCollaborationClientInternal * Internal
void SetCollabName(std::string const &val)
virtual bool Initialize(vtkOpenGLRenderer *)
std::function< double()> ScaleCallback
std::function< void(std::string const &data, vtkLogger::Verbosity)> Callback
virtual void HandleBroadcastMessage(std::string const &otherID, std::string const &type)
void SetCollabHost(std::string const &val)
std::map< std::string, vtkSmartPointer< vtkOpenGLAvatar > > Avatars
vtkNew< vtkTransform > TempTransform
virtual void AddArguments(vtksys::CommandLineArguments &arguments)
void SendPoseMessage(std::string const &msgType, int index, double pos[3], double dir[3])
method signatures to send messages with Arguments and extract them out of messages
void Log(vtkLogger::Verbosity verbosity, std::string const &msg)
std::vector< Argument > GetMessageArguments()
method signatures to send messages with Arguments and extract them out of messages
vtkObject * GetMoveEventSource()
Get/Set the object to which this class will add an observer for Move3D events (based upon which,...
void SetScaleCallback(std::function< double()> cb)
void SetLogCallback(std::function< void(std::string const &data, vtkLogger::Verbosity verbosity)> cb)
vtkSmartPointer< vtkOpenGLAvatar > GetAvatar(std::string id)
void SetMoveEventSource(vtkObject *)
Get/Set the object to which this class will add an observer for Move3D events (based upon which,...
const int vtkEventDataNumberOfDevices