17 #ifndef AGXOSG_EXAMPLEAPPLICATIONCONTROLLER_H 18 #define AGXOSG_EXAMPLEAPPLICATIONCONTROLLER_H 20 #include <agx/config/AGX_USE_WEBSOCKETS.h> 21 #include <agx/config/AGX_USE_WEBPLOT.h> 35 #include <osg/Geometry> 36 #include <osg/PositionAttitudeTransform> 37 #include <osg/LineWidth> 38 #include <osgViewer/Viewer> 39 #include <osg/ShapeDrawable> 47 #include <agxCallable/CallableAccessor.h> 49 #if AGX_USE_WEBSOCKETS() 54 class ExampleApplication;
63 ExampleApplicationController(ExampleApplication *application,
agxNet::PortRange *range =
nullptr);
65 void signalAutoStepping(
bool flag);
66 void pushParameters();
68 void setEnableTickSignals(
bool flag);
71 void updateMouseSpringRenderer();
77 bool needSceneLoading();
78 void performSceneLoading();
81 void updateRemoteViewers(
const RenderFrame& frame);
88 void initPlotCallbacks();
92 void initRemoteConnection();
94 agxNet::WebSocket::ControlChannel *getControlChannel();
97 virtual ~ExampleApplicationController();
100 bool httpGetCurrentState(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
101 bool httpStartSimulation(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
102 bool httpStopSimulation(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
103 bool httpStepForward(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
104 bool httpStepBackward(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
105 bool httpListLuaScenes(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
106 bool httpLoadScene(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
108 bool httpListJournals(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
110 bool httpAddJournalBinding(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
111 bool httpRemoveJournalBinding(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
112 bool httpListJournalBindings(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
113 bool httpSaveCurrentJournalBindings(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
115 bool httpEnableRecording(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
116 bool httpDisableRecording(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
118 bool httpLoadJournal(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
119 bool httpUnloadJournal(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
122 #if AGX_USE_WEBSOCKETS() 123 void webSocketClientConnected(agxNet::WebSocket* client);
124 void webSocketClientDisconnected(agxNet::WebSocket* client);
127 void simulationTickCallback(
agx::Clock *clock);
130 bool httpEnablePlaybackLoop(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
131 bool httpDisablePlaybackLoop(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
133 bool httpEnableRealTimeSync(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
134 bool httpDisableRealTimeSync(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
136 bool httpSetPlaybackPosition(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
138 bool httpAttachVideoStream(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
142 bool httpAttachRemoteSolver(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
143 bool httpDetachRemoteSolver(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
144 bool httpAttachStatisticsViewer(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
145 bool httpGetControlChannel(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
147 bool httpGetRemoteViewChannel(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
149 void buildStateMessage(agxJson::Value& eRoot);
151 #if AGX_USE_WEBSOCKETS() 168 void ccLoadSceneCallback(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage*
message, std::function<
void ()> callback);
169 void ccLoadScene(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
170 void ccKeyDown(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
172 void ccMousePickStart(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
173 void ccMousePickDrag(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
174 void ccMousePickEnd(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
180 void djangoMessage(agxNet::WebSocket *,
agx::UInt8* data,
size_t numBytes);
183 void getConstraintData(
agx::Constraint *constraint, agxJson::Value& eConstraint);
184 void getRigidBodyData(
agx::RigidBody *body, agxJson::Value& eBody);
186 agxJson::Value& createArrayElement( agxJson::Value& eParent,
const char* groupName );
194 void addSocket(agxNet::WebSocket *socket);
197 virtual ~StructureSynchronization();
209 agxNet::WebSocketRefVector m_sockets;
213 StructureSynchronizationTable m_structureSynchronizationTable;
221 MouseSpringRenderer(ExampleApplication *application);
223 void frameCallback(agxData::Frame *frame);
224 virtual void writeFrame(
const agxData::Frame *frame)
override;
225 virtual void endOfStream()
override;
232 virtual ~MouseSpringRenderer();
239 ExampleApplication *m_application;
240 osg::PositionAttitudeTransform *m_mouseAttachTransform;
241 osg::PositionAttitudeTransform *m_mousePositionTransform;
242 osg::Geometry *m_mouseSpringLine;
243 osg::ref_ptr< osg::Switch > m_mouseSpringSwitch;
254 OrbitCamera() : enabled(false), mode(0) {}
264 OrbitCamera m_orbitCamera;
266 ExampleApplication *m_application;
270 bool m_enableTickCallback;
271 MouseSpringRendererRef m_mouseSpringRenderer;
276 bool m_remoteSimulationReceived;
278 agx::Real m_interactiveClientHeartbeatTimeOut;
280 RemoteViewerThreadRef m_remoteViewerThread;
286 bool m_needSceneLoading;
290 class ControlChannelTCP :
public Referenced
293 ControlChannelTCP(ExampleApplicationController *controller,
agx::UInt16 port);
303 void registerMessageHandler(
const agx::String& uri, MessageHandler callback);
306 virtual ~ControlChannelTCP();
314 agxNet::TCPSocketRefVector m_clientSockets;
317 MessageHandlerTable m_messageHandlerTable;
321 bool m_setupComplete;
323 #if AGX_USE_WEBSOCKETS() 324 agxNet::WebSocket::ControlChannelRef m_controlChannel;
325 agxNet::WebSocketRef m_djangoSocket;
326 void controlChannelSocketConnectedCallback(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket);
327 void controlChannelSocketDisconnectedCallback(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket);
328 agxNet::WebSocket::ControlChannel::SocketEvent::CallbackType m_controlChannelSocketConnectedCallback;
329 agxNet::WebSocket::ControlChannel::SocketEvent::CallbackType m_controlChannelSocketDisconnectedCallback;
331 #if AGX_USE_WEBPLOT() 344 UserData& getUserData(agxNet::WebSocket *);
347 UserTable m_userTable;
349 typedef std::queue<agxNet::StructuredMessageRef> FrameMessageQueue;
350 FrameMessageQueue m_frameMessageQueue;
353 ControlChannelTCPRef m_controlChannelTCP;
354 agx::JournalReaderRef m_journalReader;
357 VideoStreamRef m_videoStream;
360 agx::RigidBodyRefVector m_monitoredBodies;
361 agx::ConstraintRefVector m_monitoredConstraints;
362 bool m_djangoProxyResponse;
363 agxCallable::CallableAccessorRefVector m_dataStreamCallables;
367 struct ExampleApplicationController::RenderFrame
369 osg::ref_ptr<osg::Image> image;
377 RemoteViewerThread(ExampleApplication *application);
381 void pushFrame(
const RenderFrame& frame);
386 virtual ~RemoteViewerThread();
389 virtual void run()
override;
391 void connectCallback(agxNet::WebSocket *client);
392 void disconnectCallback(agxNet::WebSocket *client);
393 void messageReceiveCallback(agxNet::WebSocket *,
agx::UInt8 *data,
size_t numBytes);
394 void updatePickHandlers();
397 agxNet::WebSocketServerRef m_serverSocket;
401 RenderFrame m_currentFrame;
402 ExampleApplication *m_application;
405 PickHandlerTable m_pickHandlerTable;
Basic wrapper class aroud std::thread.
void AGXPHYSICS_EXPORT init()
Initialize agx.
Smart pointer for observed objects, that automatically set pointers to them to null when they deleted...
AGXOSG_EXPORT bool setOrbitCamera(osgViewer::Viewer *viewer, agxOSG::GeometryNode *node, const agx::Vec3 &eye, const agx::Vec3 ¢er, const agx::Vec3 &up, int trackerMode)
A component is an object containing other objects, enabling hierarchical structuring.
Generalized callback, using std::function.
The base class for a constraint.
#define AGX_DECLARE_POINTER_TYPES(type)
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Base class providing referencing counted objects.
The geometry representation used by the collision detection engine.
The FrameWriter writes frames to an external target.
Templated callback with one argument.
Templated callback with three arguments.
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
GLenum GLuint GLenum GLsizei const GLchar * message
agx::ref_ptr< const Frame > FrameConstRef
std::condition_variable Condition
The rigid body class, combining a geometric model and a frame of reference.
Templated callback with two arguments.
Handles testing of ports in a range.
Clock class keeps representation of virtual/simulated time.
AGXCORE_EXPORT Component * root()
Inheritance with partial specialization due to bug with ref_ptr containers.
agx::Object is a refcounted object with a name.