AnimatLab  2
Test
OsgSimulator.cpp
1 // OsgSimulator.cpp: implementation of the OsgSimulator class.
2 //
4 
5 #include "StdAfx.h"
6 #include <stdarg.h>
7 #include <osgUtil/Version>
8 
9 #include "OsgMovableItem.h"
10 #include "OsgOrganism.h"
11 #include "OsgStructure.h"
12 #include "OsgSimulator.h"
13 
14 #include "OsgMouseSpring.h"
15 #include "OsgLight.h"
16 #include "OsgCameraManipulator.h"
17 #include "OsgDragger.h"
18 #include "OsgMeshMinVertexDistanceVisitor.h"
19 #include "OsgSimulator.h"
20 
21 namespace OsgAnimatSim
22 {
23 
25 // Construction/Destruction
27 
28 
29 OsgSimulator::OsgSimulator()
30 {
31  m_grpScene = NULL;
32  m_vsWinMgr = NULL;
33  m_vsWinMgr = new OsgSimulationWindowMgr;
34  m_lpWinMgr = m_vsWinMgr;
35  m_lpWinMgr->SetSystemPointers(this, NULL, NULL, NULL, true);
36  m_dblTotalStepTime = 0;
37  m_lStepTimeCount = 0;
38  m_dblTotalStepTime= 0;
39  m_lStepTimeCount = 0;
40  m_lpMeshMgr = NULL;
41  m_osgAlphafunc = NULL;
42  m_lpMouseSpring = new OsgMouseSpring;
43  m_lpMouseSpring->Initialize();
44  m_lpMatrixUtil = NULL;
45 }
46 
47 OsgSimulator::~OsgSimulator()
48 {
49 
50 try
51 {
52  if(m_lpMeshMgr)
53  {
54  delete m_lpMeshMgr;
55  m_lpMeshMgr = NULL;
56  }
57 
58  m_bShuttingDown = true;
59 
60  if(m_lpMouseSpring)
61  delete m_lpMouseSpring;
62 
63  if(m_lpMatrixUtil)
64  delete m_lpMatrixUtil;
65 
66  Reset();
67 }
68 catch(...)
69 {Std_TraceMsg(0, "Caught Error in desctructor of Simulator\r\n", "", -1, false, true);}
70 }
71 
72 void OsgSimulator::AlphaThreshold(float fltValue)
73 {
74  Simulator::AlphaThreshold(fltValue);
75 
76  if(m_osgAlphafunc)
77  m_osgAlphafunc->setFunction(osg::AlphaFunc::GEQUAL, m_fltAlphaThreshold);
78 }
79 
81 {
82  Simulator::Reset();
83 
84  if(m_osgCmdMgr.valid())
85  m_osgCmdMgr.release();
86 
87  if(m_lpMeshMgr)
88  {
89  delete m_lpMeshMgr;
90  m_lpMeshMgr = NULL;
91  }
92 
93 }
94 
96 {
97  if(m_bPaused)
98  SimStarting();
99  else
100  SimPausing();
101 
102  m_bPaused = !m_bPaused;
103 }
104 
106 {
107  SimStopping();
108  if(!m_bPaused)
110  m_bSimRunning = false;
111 }
112 
113 
114 osg::NotifySeverity OsgSimulator::ConvertTraceLevelToOSG()
115 {
116  int iLevel = Std_GetTraceLevel();
117 
118  switch (iLevel)
119  {
120  case 0:
121  return osg::NotifySeverity::FATAL;
122  case 10:
123  return osg::NotifySeverity::WARN;
124  case 20:
125  return osg::NotifySeverity::INFO;
126  case 30:
127  return osg::NotifySeverity::DEBUG_INFO;
128  case 40:
129  return osg::NotifySeverity::DEBUG_FP;
130  default:
131  return osg::NotifySeverity::WARN;
132  }
133 }
134 
135 //Timer Methods
136 unsigned long long OsgSimulator::GetTimerTick()
137 {
138  m_lLastTickTaken = osg::Timer::instance()->tick();
139  return m_lLastTickTaken;
140 }
141 
142 double OsgSimulator::TimerDiff_n(unsigned long long lStart, unsigned long long lEnd)
143 {return osg::Timer::instance()->delta_n(lStart, lEnd);}
144 
145 double OsgSimulator::TimerDiff_u(unsigned long long lStart, unsigned long long lEnd)
146 {return osg::Timer::instance()->delta_u(lStart, lEnd);}
147 
148 double OsgSimulator::TimerDiff_m(unsigned long long lStart, unsigned long long lEnd)
149 {return osg::Timer::instance()->delta_m(lStart, lEnd);}
150 
151 double OsgSimulator::TimerDiff_s(unsigned long long lStart, unsigned long long lEnd)
152 {return osg::Timer::instance()->delta_s(lStart, lEnd);}
153 
154 void OsgSimulator::MicroSleep(unsigned int iMicroTime)
155 {OpenThreads::Thread::microSleep(iMicroTime);}
156 
157 void OsgSimulator::WriteToConsole(std::string strMessage)
158 {
159  osg::notify(osg::NOTICE) << strMessage << std::endl;
160 }
161 
162 void OsgSimulator::Initialize(int argc, const char **argv)
163 {
164 #ifndef WIN32
165  std::string strVersion = osgUtilGetVersion();
166  strVersion = "/usr/lib/osgPlugins-" + strVersion;
167  osgDB::setLibraryFilePathList(strVersion);
168 #endif
169 
171 
172  m_oDataChartMgr.Initialize();
173  m_oExternalStimuliMgr.Initialize();
174  if(m_lpSimRecorder) m_lpSimRecorder->Initialize();
175 
176  //realize the osg viewer
177  m_vsWinMgr->Realize();
178 }
179 
180 void OsgSimulator::UpdateSimulationWindows()
181 {
182  m_bStopSimulation = !m_vsWinMgr->Update();
183 }
184 
186 {
187  SimStopping();
188  m_bForceSimulationStop = true;
189 }
190 
192 {
193  SimPausing();
194  m_bPaused = true;
195  return true;
196 }
197 
199 {
200  m_lStartSimTick = GetTimerTick();
201 
202  SimStarting();
203  m_bSimRunning = true;
204  m_bPaused = false;
205  return true;
206 }
207 
208 float *OsgSimulator::GetDataPointer(const std::string &strDataType)
209 {
210  float *lpData=NULL;
211  std::string strType = Std_CheckString(strDataType);
212 
213  //if(strType == "FRAMEDT")
214  // lpData = &m_fltFrameDt;
215  //else
216  //{
217  lpData = Simulator::GetDataPointer(strDataType);
218  if(!lpData)
219  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Simulator DataType: " + strDataType);
220  //}
221 
222  return lpData;
223 }
224 
226 {
228  m_lpSimStopPoint = dynamic_cast<KeyFrame *>(CreateObject("AnimatLab", "KeyFrame", "Snapshot"));
229  if(!m_lpSimStopPoint)
230  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "KeyFrame");
231 
232  m_lpSimStopPoint->StartSlice(m_lTimeSlice);
233  m_lpSimStopPoint->EndSlice(m_lTimeSlice);
234  m_lpSimStopPoint->Activate();
235 }
236 
237 OsgSimulator *OsgSimulator::ConvertSimulator(Simulator *lpSim)
238 {
239  if(!lpSim)
240  THROW_ERROR(Al_Err_lSimulationNotDefined, Al_Err_strSimulationNotDefined);
241 
242  OsgSimulator *lpVsSim = dynamic_cast<OsgSimulator *>(lpSim);
243 
244  if(!lpVsSim)
245  THROW_ERROR(Osg_Err_lUnableToConvertToVsSimulator, Osg_Err_strUnableToConvertToVsSimulator);
246 
247  return lpVsSim;
248 }
249 
250 void OsgSimulator::Save(std::string strFile)
251 {
252  std::string strOsgFile = strFile + ".osg";
253 
254  osgDB::writeNodeFile(*OSGRoot(), strOsgFile.c_str());
255 }
256 
257 
258 } //OsgAnimatSim
virtual void StopSimulation()
Stops the simulation and resets it.
KeyFrame * m_lpSimStopPoint
The pointer to a simulation stop point.
Definition: Simulator.h:448
bool m_bPaused
true if the simulation is paused.
Definition: Simulator.h:143
virtual void ShutdownSimulation()
Shuts down the simulation.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
ExternalStimuliMgr m_oExternalStimuliMgr
Manager for external stimuli.
Definition: Simulator.h:103
virtual bool PauseSimulation()
Pauses the simulation.
Declares the vortex Light class.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void InitializeStructures()
Initializes all of the structures of this simulation.
Definition: Simulator.cpp:1928
bool m_bShuttingDown
True if the simulation is shutting down. This is used by other objects in their destructor to know wh...
Definition: Simulator.h:158
bool m_bForceSimulationStop
Set to true to stop the simulation. This is a more forceful way of stopping the sim.
Definition: Simulator.h:137
long m_lTimeSlice
The current time slice. This a long value.
Definition: Simulator.h:251
float m_fltAlphaThreshold
The threshold value to use when setting the AlphaFunc for the entire scene.
Definition: Simulator.h:312
bool m_bSimRunning
true if the simulation is running
Definition: Simulator.h:149
unsigned long long m_lLastTickTaken
This is the last tick taken by a GetTickCount. It is used in debugging.
Definition: Simulator.h:378
SimulationWindowMgr * m_lpWinMgr
Manager for SimulationWindows.
Definition: Simulator.h:112
virtual CStdSerialize * CreateObject(std::string strModule, std::string strClassName, std::string strType, bool bThrowError=true)
Creates an object using a class factory.
Definition: Simulator.cpp:3440
bool m_bStopSimulation
Set to true to stop the simulation.
Definition: Simulator.h:134
virtual void SnapshotStopFrame()
Takes a snapshot of the current frame.
DataChartMgr m_oDataChartMgr
Manager for data charts.
Definition: Simulator.h:100
int Std_GetTraceLevel()
Gets the trace level.
virtual float * GetDataPointer(const std::string &strDataType)
Returns a float pointer to a data item of interest in this object.
SimulationRecorder * m_lpSimRecorder
The pointer to a simulation recorder.
Definition: Simulator.h:106
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual void Reset()
Resets all objects of the simulation to their unloaded state.
Declares the vortex organism class.
unsigned long long m_lStartSimTick
The tick count for when the simulation first begins running.
Definition: Simulator.h:267
virtual void Initialize()
Initializes this object with no argc/argv params.
Definition: Simulator.cpp:5304
virtual void SimPausing()
Simulation pausing event.
Definition: Simulator.cpp:2739
virtual bool Update()
Updates all of the windows.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void SimStopping()
Simulation stopping event.
Definition: Simulator.cpp:2760
Declares the vortex structure class.
virtual void SimStarting()
Simulation starting event.
Definition: Simulator.cpp:2718
virtual bool StartSimulation()
Starts the simulation.
virtual void Save(std::string strFile)
Saves the simulation file.
virtual void ToggleSimulation()
Toggles the simulation between running and paused.