VTK  9.1.0
vtkVRCollaborationClient.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4 
5  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
6  All rights reserved.
7  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without even
10  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  PURPOSE. See the above copyright notice for more information.
12 
13 =========================================================================*/
14 
15 // This class provides collaboration support for VR using avatars
16 // It relies on ZeroMQ to communicate with a collaboration server
17 // to exchance avatar names and poses and potentially other messages.
18 
19 #ifndef vtkVRCollaborationClient_h
20 #define vtkVRCollaborationClient_h
21 
22 #include "vtkEventData.h" // for ivars
23 #include "vtkLogger.h" // for Verbosity enum
24 #include "vtkObject.h"
25 #include "vtkRenderingVRModule.h" // For export macro
26 #include "vtkSmartPointer.h" // method sig
27 #include "vtksys/CommandLineArguments.hxx" // for method sig
28 #include <array> // for ivar
29 #include <functional> // for ivars
30 #include <map> // for ivars
31 #include <memory> // for ivars, shared_ptr
32 #include <string> // for ivars
33 #include <vector> // for ivars
34 
35 class vtkCallbackCommand;
36 class vtkOpenGLAvatar;
37 class vtkOpenGLRenderer;
39 class vtkTransform;
40 class vtkVRCollaborationClientInternal;
41 
42 class VTKRENDERINGVR_EXPORT vtkVRCollaborationClient : public vtkObject
43 {
44 public:
47  void PrintSelf(ostream& os, vtkIndent indent) override;
50 
51  // when sending messages we have to marshal arguments so we have a simple
52  // class to encapsulate an argument. The method to send a message takes a
53  // std::vector of arguments and there is a method to return a std::vector
54  // of arguments when receiving a message
56  {
57  Double = 0,
59  String
60  };
61 
62  class VTKRENDERINGVR_EXPORT Argument
63  {
64  public:
65  bool GetString(std::string& result);
66  void SetString(std::string const& in);
67  bool GetStringVector(std::vector<std::string>& result);
68  void SetStringVector(std::vector<std::string> const& in);
69 
70  bool GetDoubleVector(std::vector<double>& result);
71  void SetDoubleVector(double const* in, uint16_t size);
72  void SetDouble(double val);
73  bool GetDouble(double& result);
74 
75  bool GetInt32Vector(std::vector<int32_t>& result);
76  void SetInt32Vector(int32_t const* in, uint16_t size);
77  void SetInt32(int32_t val);
78  bool GetInt32(int32_t& result);
79 
81  uint16_t Count = 0;
82  std::shared_ptr<void> Data;
83  };
84 
86 
89  void SendAMessage(std::string const& msgType);
90  void SendAMessage(std::string const& msgType, std::vector<Argument> const& args);
91  std::vector<Argument> GetMessageArguments();
92  void SendPoseMessage(std::string const& msgType, int index, double pos[3], double dir[3]);
94 
95  // call during the render loop to handle collaboration messages
96  virtual void Render();
97 
98  // required call, true on success, pass the renderer you want the avatars added to
99  virtual bool Initialize(vtkOpenGLRenderer*);
100 
101  // close the connection
102  void Disconnect();
103 
104  // set the values for the collaboration connection
105  // Can be done through Set* methods or by passing in
106  // the command line arguments via AddArguments
107  virtual void AddArguments(vtksys::CommandLineArguments& arguments);
108  void SetCollabHost(std::string const& val) { this->CollabHost = val; }
109  void SetCollabSession(std::string const& val) { this->CollabSession = val; }
110  void SetCollabName(std::string const& val) { this->CollabName = val; }
111  void SetCollabPort(int val) { this->CollabPort = val; }
112 
113  // to receive log/warning/error output
115  std::function<void(std::string const& data, vtkLogger::Verbosity verbosity)> cb)
116  {
117  this->Callback = cb;
118  }
119 
120  // to override the default method of getting avatar scales
121  void SetScaleCallback(std::function<double()> cb) { this->ScaleCallback = cb; }
122 
123  // return the renderer being used by this instance (assigned during Initialize())
124  vtkOpenGLRenderer* GetRenderer() { return this->Renderer; }
125 
126  // is this instance connected to a collaboration server?
127  bool GetConnected() { return this->Connected; }
128 
129 protected:
132 
133  void Log(vtkLogger::Verbosity verbosity, std::string const& msg);
134 
135  // provided values
141 
144 
151  double NeedReply;
153 
154  bool Connected;
155 
156  // get existing avatar, or create new one, if needed, and return it.
158 
161 
162  virtual void HandleBroadcastMessage(std::string const& otherID, std::string const& type);
163 
165  static void EventCallback(
166  vtkObject* object, unsigned long event, void* clientdata, void* calldata);
168 
171 
173 
174  // used to throttle outgoing pose messages
176  bool HasPoseForDevice[vtkEventDataNumberOfDevices];
177  struct Pose
178  {
179  std::array<double, 3> Position;
180  std::array<double, 4> Orientation;
181  };
183 
184  // dynamic set of avatars, keyed on IDs sent with updates.
185  std::map<std::string, vtkSmartPointer<vtkOpenGLAvatar>> Avatars;
186  std::map<std::string, double[vtkEventDataNumberOfDevices]> AvatarUpdateTime;
187 
188  // PIMPL to keep zeromq out of the interface for this class
189  vtkVRCollaborationClientInternal* Internal;
190 };
191 
192 #endif
vtkVRCollaborationClient::Argument::GetDouble
bool GetDouble(double &result)
vtkX3D::function
@ function
Definition: vtkX3D.h:255
vtkVRCollaborationClient::AvatarUpdateTime
std::map< std::string, double[vtkEventDataNumberOfDevices]> AvatarUpdateTime
Definition: vtkVRCollaborationClient.h:186
vtkVRCollaborationClient::ArgumentType
ArgumentType
Definition: vtkVRCollaborationClient.h:56
vtkVRCollaborationClient::vtkVRCollaborationClient
vtkVRCollaborationClient(const vtkVRCollaborationClient &)=delete
vtkOpenGLAvatar
OpenGL Avatar.
Definition: vtkOpenGLAvatar.h:38
vtkVRCollaborationClient::SendAMessage
void SendAMessage(std::string const &msgType)
method signatures to send messages with Arguments and extract them out of messages
vtkVRCollaborationClient::EventCommand
vtkCallbackCommand * EventCommand
Definition: vtkVRCollaborationClient.h:164
vtkVRCollaborationClient::Argument::Type
ArgumentType Type
Definition: vtkVRCollaborationClient.h:80
vtkVRCollaborationClient::UpdateAvatarPoseFromCamera
void UpdateAvatarPoseFromCamera()
vtkVRCollaborationClient::SendLatestDevicePoses
void SendLatestDevicePoses()
vtkVRCollaborationClient::NeedHeartbeat
double NeedHeartbeat
Definition: vtkVRCollaborationClient.h:150
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkVRCollaborationClient::Argument::SetInt32
void SetInt32(int32_t val)
vtkVRCollaborationClient::RetryCount
int RetryCount
Definition: vtkVRCollaborationClient.h:152
vtkX3D::data
@ data
Definition: vtkX3D.h:321
vtkVRCollaborationClient::MoveObserver
long MoveObserver
Definition: vtkVRCollaborationClient.h:167
vtkVRCollaborationClient::EventCallback
static void EventCallback(vtkObject *object, unsigned long event, void *clientdata, void *calldata)
vtkVRCollaborationClient::YourLastAvatarUpdateTime
double YourLastAvatarUpdateTime
Definition: vtkVRCollaborationClient.h:175
vtkLogger::Verbosity
Verbosity
Definition: vtkLogger.h:203
vtkVRCollaborationClient::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkVRCollaborationClient::Argument::GetStringVector
bool GetStringVector(std::vector< std::string > &result)
vtkVRCollaborationClient::TempTransform
vtkNew< vtkTransform > TempTransform
Definition: vtkVRCollaborationClient.h:172
vtkVRCollaborationClient::vtkVRCollaborationClient
vtkVRCollaborationClient()
vtkVRCollaborationClient::Callback
std::function< void(std::string const &data, vtkLogger::Verbosity)> Callback
Definition: vtkVRCollaborationClient.h:142
vtkSmartPointer
Hold a reference to a vtkObjectBase instance.
Definition: vtkSmartPointer.h:145
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:82
vtkX3D::dir
@ dir
Definition: vtkX3D.h:330
vtkVRCollaborationClient::SetCollabPort
void SetCollabPort(int val)
Definition: vtkVRCollaborationClient.h:111
vtkVRCollaborationClient::New
static vtkVRCollaborationClient * New()
vtkLogger.h
vtkVRCollaborationClient::Argument::SetString
void SetString(std::string const &in)
vtkVRCollaborationClient::GetMessageArguments
std::vector< Argument > GetMessageArguments()
method signatures to send messages with Arguments and extract them out of messages
vtkVRCollaborationClient::SetScaleCallback
void SetScaleCallback(std::function< double()> cb)
Definition: vtkVRCollaborationClient.h:121
vtkVRCollaborationClient::Connected
bool Connected
Definition: vtkVRCollaborationClient.h:154
vtkVRCollaborationClient::operator=
vtkVRCollaborationClient & operator=(const vtkVRCollaborationClient &)=delete
vtkVRCollaborationClient::CollabHost
std::string CollabHost
Definition: vtkVRCollaborationClient.h:137
vtkVRCollaborationClient::SetCollabHost
void SetCollabHost(std::string const &val)
Definition: vtkVRCollaborationClient.h:108
vtkTransform
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:164
vtkVRCollaborationClient::Argument::GetDoubleVector
bool GetDoubleVector(std::vector< double > &result)
vtkVRCollaborationClient::Argument::GetString
bool GetString(std::string &result)
vtkVRCollaborationClient::Argument::SetDouble
void SetDouble(double val)
vtkVRCollaborationClient::Internal
vtkVRCollaborationClientInternal * Internal
Definition: vtkVRCollaborationClient.h:189
vtkVRCollaborationClient::HandleCollabMessage
void HandleCollabMessage()
vtkVRCollaborationClient::SendAMessage
void SendAMessage(std::string const &msgType, std::vector< Argument > const &args)
method signatures to send messages with Arguments and extract them out of messages
vtkEventDataNumberOfDevices
const int vtkEventDataNumberOfDevices
Definition: vtkEventData.h:36
vtkVRCollaborationClient::DisplayOwnAvatar
bool DisplayOwnAvatar
Definition: vtkVRCollaborationClient.h:145
vtkVRCollaborationClient::SetCollabName
void SetCollabName(std::string const &val)
Definition: vtkVRCollaborationClient.h:110
vtkVRCollaborationClient
Definition: vtkVRCollaborationClient.h:43
vtkVRCollaborationClient::GetAvatar
vtkSmartPointer< vtkOpenGLAvatar > GetAvatar(std::string id)
vtkVRCollaborationClient::Argument::SetInt32Vector
void SetInt32Vector(int32_t const *in, uint16_t size)
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkVRCollaborationClient::AvatarIdle
bool AvatarIdle(std::string id)
vtkSmartPointer.h
vtkOpenGLRenderer
OpenGL renderer.
Definition: vtkOpenGLRenderer.h:128
vtkVRCollaborationClient::GetConnected
bool GetConnected()
Definition: vtkVRCollaborationClient.h:127
vtkVRCollaborationClient::CollabName
std::string CollabName
Definition: vtkVRCollaborationClient.h:139
vtkEventData.h
vtkVRCollaborationClient::EraseIdleAvatars
void EraseIdleAvatars()
vtkVRCollaborationClient::SendPoseMessage
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
vtkVRCollaborationClient::Pose::Orientation
std::array< double, 4 > Orientation
Definition: vtkVRCollaborationClient.h:180
vtkX3D::size
@ size
Definition: vtkX3D.h:259
vtkNew< vtkTransform >
vtkVRCollaborationClient::HandleBroadcastMessage
virtual void HandleBroadcastMessage(std::string const &otherID, std::string const &type)
vtkVRCollaborationClient::CollabID
std::string CollabID
Definition: vtkVRCollaborationClient.h:136
vtkVRCollaborationClient::RenderWindow
vtkOpenGLRenderWindow * RenderWindow
Definition: vtkVRCollaborationClient.h:170
vtkObject.h
vtkVRCollaborationClient::Pose::Position
std::array< double, 3 > Position
Definition: vtkVRCollaborationClient.h:179
vtkVRCollaborationClient::CollabPort
int CollabPort
Definition: vtkVRCollaborationClient.h:140
vtkX3D::string
@ string
Definition: vtkX3D.h:496
vtkVRCollaborationClient::CollabSession
std::string CollabSession
Definition: vtkVRCollaborationClient.h:138
vtkVRCollaborationClient::Argument::GetInt32Vector
bool GetInt32Vector(std::vector< int32_t > &result)
vtkVRCollaborationClient::Disconnect
void Disconnect()
vtkVRCollaborationClient::GetRenderer
vtkOpenGLRenderer * GetRenderer()
Definition: vtkVRCollaborationClient.h:124
vtkVRCollaborationClient::PublishAvailable
bool PublishAvailable
Definition: vtkVRCollaborationClient.h:146
vtkVRCollaborationClient::Pose
Definition: vtkVRCollaborationClient.h:178
vtkVRCollaborationClient::SetCollabSession
void SetCollabSession(std::string const &val)
Definition: vtkVRCollaborationClient.h:109
vtkVRCollaborationClient::NeedReply
double NeedReply
Definition: vtkVRCollaborationClient.h:151
vtkVRCollaborationClient::Avatars
std::map< std::string, vtkSmartPointer< vtkOpenGLAvatar > > Avatars
Definition: vtkVRCollaborationClient.h:185
vtkCallbackCommand
supports function callbacks
Definition: vtkCallbackCommand.h:154
vtkVRCollaborationClient::Argument::Data
std::shared_ptr< void > Data
Definition: vtkVRCollaborationClient.h:82
vtkVRCollaborationClient::Initialize
virtual bool Initialize(vtkOpenGLRenderer *)
vtkVRCollaborationClient::Argument::SetStringVector
void SetStringVector(std::vector< std::string > const &in)
vtkVRCollaborationClient::Argument::SetDoubleVector
void SetDoubleVector(double const *in, uint16_t size)
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:83
vtkVRCollaborationClient::Render
virtual void Render()
vtkVRCollaborationClient::Argument::GetInt32
bool GetInt32(int32_t &result)
vtkVRCollaborationClient::Renderer
vtkOpenGLRenderer * Renderer
Definition: vtkVRCollaborationClient.h:169
vtkVRCollaborationClient::Argument
Definition: vtkVRCollaborationClient.h:63
vtkX3D::index
@ index
Definition: vtkX3D.h:252
vtkVRCollaborationClient::Int32
@ Int32
Definition: vtkVRCollaborationClient.h:58
vtkVRCollaborationClient::ScaleCallback
std::function< double()> ScaleCallback
Definition: vtkVRCollaborationClient.h:143
vtkVRCollaborationClient::AddArguments
virtual void AddArguments(vtksys::CommandLineArguments &arguments)
vtkVRCollaborationClient::~vtkVRCollaborationClient
~vtkVRCollaborationClient() override
vtkVRCollaborationClient::SetLogCallback
void SetLogCallback(std::function< void(std::string const &data, vtkLogger::Verbosity verbosity)> cb)
Definition: vtkVRCollaborationClient.h:114
vtkVRCollaborationClient::Log
void Log(vtkLogger::Verbosity verbosity, std::string const &msg)