18 #ifndef AGX_SIMULATION_CONTROLLER_H
19 #define AGX_SIMULATION_CONTROLLER_H
21 #include <agx/config/AGX_USE_WEBSOCKETS.h>
23 #if AGX_USE_WEBSOCKETS()
73 SimulationController();
95 void clearSimulation();
116 void startRunLoopThread();
123 void play(
bool wasTemporarilyPaused =
false);
130 void pause(
bool isTemporarilyPaused =
false);
136 void stepTo(
agx::Real time, CallbackWrapper_String* errorCallback =
nullptr,
bool wasTemporarilyPaused =
false);
141 void stepToFrame(
agx::UInt frameIndex, CallbackWrapper_String* errorCallback =
nullptr,
bool wasTemporarilyPaused =
false);
147 void singleStep(
bool asynchronous =
true );
152 void setRepeat(
bool repeat );
153 bool getRepeat()
const;
159 void setRealTimeSync(
bool realTime );
160 bool getRealTimeSync()
const;
187 bool openJournal(
const agx::String& journalPath=
"Journal.agxJournal",
const agx::String& sessionName=
"",
bool loadScene=
true,
bool attach=
true );
192 bool sanityCheck()
const;
201 bool truncate(
agx::UInt firstFrameToRemove);
212 void setSaveSceneToDisk(
bool flag );
217 bool getSaveSceneToDisk()
const;
223 void signalSceneChanged();
225 bool checkAndResetPlotSavedState();
231 void setEnableSimulationStatisticsListener(
bool flag );
233 bool getEnableSimulationStatisticsListener( )
const;
247 bool deleteSessionFromDisk(
const agx::String& sessionName );
251 bool stopRecording(
bool resetScene=
true,
bool blocking=
false);
254 bool isTrackRunning()
const;
255 bool isRecording()
const;
256 bool isRunning()
const;
262 bool renameRecording(
const agx::String& sessionName );
281 agx::UInt getAccumulatedNumFrames()
const;
310 agx::Real getFirstFrameTimeStamp()
const;
325 bool endPlayback(
bool resetScene =
true);
327 void registerStartPlaybackSignalCallback(CallbackWrapper_Real* callback);
328 void registerStartRecordingSignalCallback(CallbackWrapper_Real* callback);
329 void registerStopPlaybackSignalCallback(CallbackWrapper_Real* callback);
330 void registerStopRecordingSignalCallback(CallbackWrapper_Real* callback);
332 void registerPreStepSignalCallback( CallbackWrapper_Real* callback );
333 void registerPostStepSignalCallback(CallbackWrapper_Real* callback);
334 void registerPlaySignalCallback( CallbackWrapper* callback );
335 void registerPauseSignalCallback( CallbackWrapper* callback );
336 void registerRealTimeSignalCallback( CallbackWrapper_Bool* callback );
337 void registerJumpSignalCallback( CallbackWrapper_Real* callback );
338 void registerErrorSignalCallback( CallbackWrapper_String* callback );
341 void unregisterErrorSignalCallback( CallbackWrapper_String* callback );
342 void unregisterPreStepSignalCallback( CallbackWrapper_Real* callback );
343 void unregisterPostStepSignalCallback(CallbackWrapper_Real* callback);
345 void unregisterStartPlaybackSignalCallback(CallbackWrapper_Real* callback);
346 void unregisterStartRecordingSignalCallback(CallbackWrapper_Real* callback);
347 void unregisterStopPlaybackSignalCallback(CallbackWrapper_Real* callback);
348 void unregisterStopRecordingSignalCallback(CallbackWrapper_Real* callback);
369 void requestExclusiveAccess();
370 void relinquishExclusiveAccess();
379 bool getContacts( std::vector<agx::Vec3>& contactPoints, std::vector<agx::Vec3>& normals, std::vector<agx::Real>& depths )
const;
390 size_t calculateDirectSolvedContacts()
const;
393 virtual ~SimulationController();
397 void stepTo(std::function<
bool(
agx::Clock *)> stopPredicate, CallbackWrapper_String* errorCallback,
bool wasTemporarilyPaused);
410 bool httpPlaySimulation(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
411 bool httpPauseSimulation(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
412 bool httpEnableRealTimeSync(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
413 bool httpDisableRealTimeSync(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
416 bool httpEnableRecording(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
417 bool httpDisableRecording(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
418 bool httpListJournals(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
419 bool httpLoadJournal(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
420 bool httpUnloadJournal(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
421 bool httpSetPlaybackPosition(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
424 bool httpAttachRemoteViewer(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
428 bool httpGetControlChannel(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
429 bool httpInitSynchronizationSocket(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
431 void signalStartPlayback();
432 void signalStartRecording();
433 void signalStopPlayback();
434 void signalStopRecording();
437 void signalPreStep();
438 void signalPostStep();
441 void signalRealTime();
448 void webSocketClientConnected( agxNet::WebSocket* client );
449 void webSocketClientDisconnected( agxNet::WebSocket* client );
450 void webSocketMessageReceived( agxNet::WebSocket *socket,
agx::UInt8* payload,
size_t size );
451 void sendWebSocketMessage(
const agxJson::Value& eMessage);
495 agxNet::WebSocketRefVector m_webSocketClients;
496 agxNet::WebSocketServerRef m_webSocketServer;
500 agxSDK::RunLoopThread* m_runLoopThread;
543 enum PlaybackMode {FROM_SIMULATION, FROM_JOURNAL} m_playbackMode;
567 agxNet::WebSocket::ControlChannelRef m_controlChannel;
568 void controlChannelSocketConnectedCallback( agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket );
569 agxNet::WebSocket::ControlChannel::SocketEvent::CallbackType m_controlChannelSocketConnectedCallback;
570 agxNet::WebSocket::ControlChannel::SocketEvent::CallbackType m_controlChannelSocketDisconnectedCallback;
572 void ccGetTreeStructure( agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message );
573 agxJson::Value& createArrayElement( agxJson::Value& eParent,
const char* groupName );
574 void signalPlaybackStarting();
575 void signalPlaybackEnding();
577 agx::JournalReaderRef m_journalReader;
579 bool m_simulationStatisticsListenerEnabled;
580 bool m_saveSimulationToDisk;
582 bool m_shouldSignalStartPlayback;
583 bool m_shouldSignalStartRecording;
584 bool m_shouldSignalStopPlayback;
585 bool m_shouldSignalStopRecording;
586 bool m_shouldJumpAfterStop;
588 bool m_isTemporarilyPaused;
599 ScopedStepLock( SimulationController* simulationController );
602 ScopedStepLock(
const ScopedStepLock& ) {};
603 void operator=(
const ScopedStepLock& ) {};
605 SimulationController* m_simulationController;
615 AGX_FORCE_INLINE bool SimulationController::getRealTimeSync()
const {
return m_realTime; }
617 AGX_FORCE_INLINE bool SimulationController::isRecording()
const {
return m_isRecording; }
618 AGX_FORCE_INLINE bool SimulationController::isRunning()
const {
return m_autoStep; }
#define AGX_DECLARE_POINTER_TYPES(type)
#define AGXPHYSICS_EXPORT
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Block synchronization primitive.
Clock class keeps representation of virtual/simulated time.
An event with one argument.
An event with no arguments.
Representation of a name string.
Base class providing referencing counted objects.
Contains classes for low level data storage for AGX.
agx::ref_ptr< Track > TrackRef
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agx::ref_ptr< Journal > JournalRef
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
std::recursive_mutex ReentrantMutex
void AGXPHYSICS_EXPORT init()
Initialize agx.