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