VTK  9.2.6
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 exchange 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:
45  static vtkVRCollaborationClient* New();
47  void PrintSelf(ostream& os, vtkIndent indent) override;
49  vtkVRCollaborationClient& operator=(const vtkVRCollaborationClient&) = delete;
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:
131  ~vtkVRCollaborationClient() override;
132 
133  void Log(vtkLogger::Verbosity verbosity, std::string const& msg);
134 
135  // provided values
141 
142  std::function<void(std::string const& data, vtkLogger::Verbosity)> Callback;
143  std::function<double()> ScaleCallback;
144 
147  void HandleCollabMessage();
148  bool AvatarIdle(std::string id);
149  void EraseIdleAvatars();
151  double NeedReply;
153 
154  bool Connected;
155 
156  // get existing avatar, or create new one, if needed, and return it.
158 
159  void SendLatestDevicePoses();
160  void UpdateAvatarPoseFromCamera();
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
OpenGL rendering window.
abstract base class for most VTK objects
Definition: vtkObject.h:62
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkVRCollaborationClientInternal * Internal
void SetCollabName(std::string const &val)
void SetCollabSession(std::string const &val)
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:60
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:32
std::map< std::string, vtkSmartPointer< vtkOpenGLAvatar > > Avatars
const int vtkEventDataNumberOfDevices
Definition: vtkEventData.h:36
vtkOpenGLRenderWindow * RenderWindow
supports function callbacks
vtkOpenGLRenderer * GetRenderer()
a simple class to control print indentation
Definition: vtkIndent.h:39
void SetLogCallback(std::function< void(std::string const &data, vtkLogger::Verbosity verbosity)> cb)
std::function< void(std::string const &data, vtkLogger::Verbosity)> Callback
void SetScaleCallback(std::function< double()> cb)
std::map< std::string, double[vtkEventDataNumberOfDevices]> AvatarUpdateTime
std::function< double()> ScaleCallback
void SetCollabHost(std::string const &val)
OpenGL Avatar.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
OpenGL renderer.
vtkNew< vtkTransform > TempTransform