AnimatLab  2
Test
OsgScriptedSimulationWindow.cpp
1 #include "StdAfx.h"
2 #include "OsgCameraManipulator.h"
3 #include "OsgMovableItem.h"
4 #include "OsgBody.h"
5 #include "OsgRigidBody.h"
6 #include "OsgJoint.h"
7 #include "OsgStructure.h"
8 #include "OsgLight.h"
9 #include "OsgUserData.h"
10 #include "OsgSimulationWindow.h"
11 #include "OsgScriptedSimulationWindow.h"
12 #include "OsgMouseSpring.h"
13 #include "OsgDraggerHandler.h"
14 #include "OsgDragger.h"
15 #include "OsgSimulator.h"
16 
17 namespace OsgAnimatSim
18 {
19  namespace Visualization
20  {
21 
22 OsgScriptedSimulationWindow::OsgScriptedSimulationWindow()
23 {
24  m_lpCurrentPath = NULL;
25  //Scripted sim window always has track camera as true.
26  m_bTrackCamera = true;
27  m_lpDefaultTrackBody = NULL;
28  m_lpOriginalTrackBody = NULL;
29 }
30 
31 OsgScriptedSimulationWindow::~OsgScriptedSimulationWindow(void)
32 {
33 }
42 CStdFPoint OsgScriptedSimulationWindow::DefaultPosition() {return m_vDefaultPosition;}
43 
54 void OsgScriptedSimulationWindow::DefaultPosition(CStdFPoint &oPoint, bool bUseScaling)
55 {
56  CStdFPoint oNewPoint, oReportPosition;
57  if(bUseScaling && m_lpSim)
58  oNewPoint = oPoint * m_lpSim->InverseDistanceUnits();
59  else
60  oNewPoint = oPoint;
61 
62  m_vDefaultPosition = oNewPoint;
63 
64  if(m_lpTrackBody)
65  {
66  CStdFPoint vTargetPos = m_lpTrackBody->AbsolutePosition();
67  SetCameraPositionAndLookAt(m_vDefaultPosition, vTargetPos);
68  }
69 }
70 
83 void OsgScriptedSimulationWindow::DefaultPosition(float fltX, float fltY, float fltZ, bool bUseScaling)
84 {
85  CStdFPoint vPos(fltX, fltY, fltZ);
86  DefaultPosition(vPos, bUseScaling);
87 }
88 
100 void OsgScriptedSimulationWindow::DefaultPosition(std::string strXml, bool bUseScaling)
101 {
102  CStdXml oXml;
103  oXml.Deserialize(strXml);
104  oXml.FindElement("Root");
105  oXml.FindChildElement("Position");
106 
107  CStdFPoint vPos;
108  Std_LoadPoint(oXml, "Position", vPos);
109  DefaultPosition(vPos, bUseScaling);
110 }
111 
112 std::string OsgScriptedSimulationWindow::DefaultPartID() {return m_strDefaultPartID;}
113 
114 void OsgScriptedSimulationWindow::DefaultPartID(std::string strID)
115 {
116  m_strDefaultPartID = strID;
117 
118  if(m_lpSim)
119  {
120  if(Std_IsBlank(m_strDefaultPartID))
121  m_lpDefaultTrackBody = NULL;
122  else
123  m_lpDefaultTrackBody = dynamic_cast<BodyPart *>(m_lpSim->FindByID(m_strDefaultPartID));
124  }
125 }
126 
127 bool OsgScriptedSimulationWindow::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
128 {
129  std::string strType = Std_CheckString(strDataType);
130 
131  if(OsgSimulationWindow::SetData(strDataType, strValue, false))
132  return true;
133 
134  if(strType == "POSITION")
135  {
136  DefaultPosition(strValue);
137  return true;
138  }
139 
140  if(strType == "DEFAULTPARTID")
141  {
142  DefaultPartID(strValue);
143  return true;
144  }
145 
146  //If it was not one of those above then we have a problem.
147  if(bThrowError)
148  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
149 
150  return false;
151 }
152 
164 {
165  CStdXml oXml;
166  oXml.Deserialize(strXml);
167  oXml.FindElement("Root");
168  oXml.FindChildElement("CameraPath");
169 
170  OsgLinearPath *lpPath = LoadCameraPath(oXml);
171  lpPath->Initialize();
172  SortPaths();
173 }
174 
189 void OsgScriptedSimulationWindow::RemoveCameraPath(std::string strID, bool bThrowError)
190 {
191  int iIdx = FindCameraPath(strID);
192  m_aryCameraPaths.RemoveAt(iIdx);
193  SortPaths();
194 }
195 
196 int OsgScriptedSimulationWindow::FindCameraPath(std::string strID, bool bThrowError)
197 {
198  int iCount = m_aryCameraPaths.GetSize();
199  for(int iIdx=0; iIdx<iCount; iIdx++)
200  if(m_aryCameraPaths[iIdx]->ID() == strID)
201  return iIdx;
202 
203  if(bThrowError)
204  THROW_TEXT_ERROR(Al_Err_lItemNotFound, Al_Err_strItemNotFound, ("ID: " + strID));
205 
206  return -1;
207 }
208 
209 void OsgScriptedSimulationWindow::SortPaths()
210 {
211  m_arySortedCameraPaths.RemoveAll();
212 
213  int iCount = m_aryCameraPaths.GetSize();
214  for(int iIdx=0; iIdx<iCount; iIdx++)
215  {
216  m_arySortedCameraPaths.Add(m_aryCameraPaths[iIdx]->StartTime(), m_aryCameraPaths[iIdx]);
217  }
218 }
219 
220 bool OsgScriptedSimulationWindow::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
221 {
222  std::string strType = Std_CheckString(strItemType);
223 
224  if(strType == "CAMERAPATH")
225  {
226  AddCameraPath(strXml);
227  return true;
228  }
229 
230  //If it was not one of those above then we have a problem.
231  if(bThrowError)
232  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
233 
234  return false;
235 }
236 
237 bool OsgScriptedSimulationWindow::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
238 {
239  std::string strType = Std_CheckString(strItemType);
240 
241  if(strType == "CAMERAPATH")
242  {
243  RemoveCameraPath(strID);
244  return true;
245  }
246 
247  //If it was not one of those above then we have a problem.
248  if(bThrowError)
249  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
250 
251  return false;
252 }
253 
255 {
257 
258  //Try to get the default part to track.
259  DefaultPartID(m_strDefaultPartID);
260 
261  int iCount = m_aryCameraPaths.GetSize();
262  for(int iIdx=0; iIdx<iCount; iIdx++)
263  m_aryCameraPaths[iIdx]->Initialize();
264 }
265 
267 {
268  m_lpOriginalTrackBody = m_lpTrackBody;
269  m_lpTrackBody = m_lpDefaultTrackBody;
270 
271  m_iCurrentPathIter = m_arySortedCameraPaths.begin();
272 
273  if(m_arySortedCameraPaths.GetSize() > 0)
274  m_lpCurrentPath = m_iCurrentPathIter->second;
275  else
276  m_lpCurrentPath = NULL;
277 
278  SetCameraPostion(m_vDefaultPosition);
279 }
280 
282 {
283  m_lpTrackBody = m_lpDefaultTrackBody;
284  TrackCamera();
285 
286  m_lpCurrentPath = NULL;
287 
288  int iCount = m_aryCameraPaths.GetSize();
289  for(int iIndex=0; iIndex<iCount; iIndex++)
290  m_aryCameraPaths[iIndex]->ResetSimulation();
291 }
292 
293 void OsgScriptedSimulationWindow::FindNextCameraPath()
294 {
295  if(m_iCurrentPathIter != m_arySortedCameraPaths.end())
296  {
297  m_iCurrentPathIter++;
298  if(m_iCurrentPathIter != m_arySortedCameraPaths.end())
299  m_lpCurrentPath = m_iCurrentPathIter->second;
300  }
301 }
302 
304 {
305  if(m_lpSim->SimRunning())
306  {
307  if(m_lpCurrentPath && m_lpCurrentPath->AfterPathTime(m_lpSim->Time()))
308  FindNextCameraPath();
309 
310  if(m_lpCurrentPath && m_lpCurrentPath->WithinPathTime(m_lpSim->Time()))
311  {
312  if(m_lpCurrentPath->Interpolate((double) m_lpSim->Time()) && m_lpCurrentPath->TrackBody())
313  {
314  CStdFPoint oPos = m_lpCurrentPath->TrackBody()->AbsolutePosition();
315  osg::Vec3d vTargetPos(oPos.x, oPos.y, oPos.z);
316 
317  osg::Vec3d vPos = m_lpCurrentPath->GetInterpPosition();
318 
319  //std::string strMesage = "Time: " + STR(m_lpSim->Time()) + ", Pos: [" + STR(vPos.x()) + ", " + STR(vPos.y()) + ", " + STR(vPos.z()) + "]\n";
320  //OutputDebugString(strMesage.c_str());
321 
322  SetCameraPositionAndLookAt(vPos, vTargetPos);
323  }
324  }
325  }
326  else
328 }
329 
331 {
332  if(m_lpTrackBody)
333  TrackCamera();
334 
335  m_osgViewer->frame();
336 }
337 
338 
339 void OsgScriptedSimulationWindow::Load(CStdXml &oXml)
340 {
341  OsgSimulationWindow::Load(oXml);
342 
343  oXml.IntoElem(); //Into Window Element
344 
345  std::string m_strDefaultStructureID;
346  std::string m_strDefaultPartID;
347 
348  DefaultPartID(oXml.GetChildString("LookAtBodyID", m_strDefaultPartID));
349 
350  CStdFPoint vPos;
351  Std_LoadPoint(oXml, "DefaultPosition", vPos);
352  DefaultPosition(vPos);
353 
354  if(oXml.FindChildElement("CameraPaths", false))
355  {
356  oXml.IntoElem(); //IntoOf CameraPaths Element
357 
358  int iCount = oXml.NumberOfChildren();
359  for(int iIndex=0; iIndex<iCount; iIndex++)
360  {
361  oXml.FindChildByIndex(iIndex);
362  LoadCameraPath(oXml);
363  }
364 
365  oXml.OutOfElem(); //OutOf CameraPaths Element
366  }
367 
368  oXml.OutOfElem(); //OutOf Window Element
369 
370  SortPaths();
371 }
372 
384 {
385  OsgLinearPath *lpSpline = NULL;
386 
387 try
388 {
389  lpSpline = new OsgLinearPath;
390 
391  lpSpline->SetSystemPointers(m_lpSim, NULL, NULL, NULL, true);
392  lpSpline->ParentWindow(this);
393 
394  lpSpline->Load(oXml);
395  m_aryCameraPaths.Add(lpSpline);
396 
397  return lpSpline;
398 }
399 catch(CStdErrorInfo oError)
400 {
401  if(lpSpline) delete lpSpline;
402  lpSpline = NULL;
403  RELAY_ERROR(oError);
404  return NULL;
405 }
406 catch(...)
407 {
408  if(lpSpline) delete lpSpline;
409  lpSpline = NULL;
410  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
411  return NULL;
412 }
413 }
414 
415 
416  }// end Visualization
417 }// end OsgAnimatSim
virtual void Initialize()
Initializes this object.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Declares the vortex Light class.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
virtual std::string ID()
Gets the unique GUID ID of this object.
Definition: AnimatBase.cpp:167
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void RemoveCameraPath(std::string strID, bool bThrowError=true)
Removes the camera path based on ID.
virtual bool SimRunning()
Gets whether the simulation is running.
Definition: Simulator.cpp:673
virtual void SimStarting()
Called just before the simulation starts.
virtual void TrackCamera()
Implements code to do the camera tracking.
virtual CStdFPoint DefaultPosition()
Gets the local position. (m_oPosition)
virtual void SetCameraPositionAndLookAt(CStdFPoint oCameraPos, CStdFPoint oTarget)
Manually sets a camera position and look at position.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
virtual void SetCameraPostion(CStdFPoint vCameraPos)
Manually sets a camera position.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
virtual void Initialize()
Initializes this object.
virtual AnimatBase * FindByID(std::string strID, bool bThrowError=true)
Searches for the object with the specified ID.
Definition: Simulator.cpp:4008
BodyPart * m_lpOriginalTrackBody
Used to keep track of the body part that was being tracked before sim started.
virtual bool Interpolate(const double t)
Interpolate spline at time t.
virtual void TrackCamera()
Implements code to do the camera tracking.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
bool Std_LoadPoint(CStdXml &oXml, std::string strName, CStdIPoint &oPoint, bool bThrowError)
Standard load point.
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
Declares the vortex structure class.
virtual float Time()
Gets the current simulation time in seconds.
Definition: Simulator.cpp:559
virtual OsgLinearPath * LoadCameraPath(CStdXml &oXml)
Loads the a camera path.
virtual void AddCameraPath(std::string strXml)
Creates and adds a camera path.
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.