VTK  9.3.20240418
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 
24 VTK_ABI_NAMESPACE_BEGIN
25 class vtkCallbackCommand;
26 class vtkOpenGLAvatar;
27 class vtkOpenGLRenderer;
29 class vtkTransform;
30 class vtkVRCollaborationClientInternal;
31 
32 class VTKRENDERINGVR_EXPORT vtkVRCollaborationClient : public vtkObject
33 {
34 public:
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
89  virtual bool Initialize(vtkOpenGLRenderer*);
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 
119 protected:
122 
123  void Log(vtkLogger::Verbosity verbosity, std::string const& msg);
124 
125  // provided values
131 
134 
141  double NeedReply;
143 
144  bool Connected;
145 
146  // get existing avatar, or create new one, if needed, and return it.
148 
151 
152  virtual void HandleBroadcastMessage(std::string const& otherID, std::string const& type);
153 
155  static void EventCallback(
156  vtkObject* object, unsigned long event, void* clientdata, void* calldata);
158 
161 
163 
164  // used to throttle outgoing pose messages
166  bool HasPoseForDevice[vtkEventDataNumberOfDevices];
167  struct Pose
168  {
169  std::array<double, 3> Position;
170  std::array<double, 4> Orientation;
171  };
173 
174  // dynamic set of avatars, keyed on IDs sent with updates.
175  std::map<std::string, vtkSmartPointer<vtkOpenGLAvatar>> Avatars;
176  std::map<std::string, double[vtkEventDataNumberOfDevices]> AvatarUpdateTime;
177 
178  // PIMPL to keep zeromq out of the interface for this class
179  vtkVRCollaborationClientInternal* Internal;
180 };
181 
182 VTK_ABI_NAMESPACE_END
183 #endif
supports function callbacks
a simple class to control print indentation
Definition: vtkIndent.h:108
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
Definition: vtkTransform.h:160
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)
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(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
vtkVRCollaborationClient & operator=(const vtkVRCollaborationClient &)=delete
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)
static vtkVRCollaborationClient * New()
std::map< std::string, vtkSmartPointer< vtkOpenGLAvatar > > Avatars
vtkNew< vtkTransform > TempTransform
vtkSmartPointer< vtkOpenGLAvatar > GetAvatar(std::string id)
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)
vtkOpenGLRenderer * GetRenderer()
void SetScaleCallback(std::function< double()> cb)
void SetLogCallback(std::function< void(std::string const &data, vtkLogger::Verbosity verbosity)> cb)
std::vector< Argument > GetMessageArguments()
method signatures to send messages with Arguments and extract them out of messages
@ function
Definition: vtkX3D.h:249
@ dir
Definition: vtkX3D.h:324
@ type
Definition: vtkX3D.h:516
@ size
Definition: vtkX3D.h:253
@ index
Definition: vtkX3D.h:246
@ data
Definition: vtkX3D.h:315
@ string
Definition: vtkX3D.h:490
const int vtkEventDataNumberOfDevices
Definition: vtkEventData.h:25