AnimatLab  2
Test
VortexAnimatSim/OsgLinearPath.cpp
1 #include "StdAfx.h"
2 #include "VsMovableItem.h"
3 #include "VsBody.h"
4 #include "VsJoint.h"
5 #include "VsMotorizedJoint.h"
6 #include "VsRigidBody.h"
7 #include "VsOrganism.h"
8 #include "VsStructure.h"
9 #include "VsClassFactory.h"
10 #include "VsSimulator.h"
11 
12 //#include "VsSimulationRecorder.h"
13 #include "VsMouseSpring.h"
14 #include "VsLight.h"
15 #include "VsCameraManipulator.h"
16 #include "VsDragger.h"
17 #include "MeshMinVertexDistanceVisitor.h"
18 
19 #include "OsgLinearPath.h"
20 #include "VsSimulationWindow.h"
21 #include "VsScriptedSimulationWindow.h"
22 
23 namespace VortexAnimatSim
24 {
25  namespace Visualization
26  {
27 
34 : osg::Referenced(),
35  AnimatBase(),
36  m_ControlPoints(),
37  m_CurveIsValid(false),
38  m_Interpolated(0.0, 0.0, 0.0),
39  m_lpTrackBody(NULL),
40  m_bVisible(true),
41  m_bVisibleInSim(false),
42  m_bShowWaypoints(false),
43  m_dblStartTime(0),
44  m_dblEndTime(1),
45  m_lpParentWindow(NULL),
46  m_lpCurrentWaypoint(NULL),
47  m_iCurrentWaypointIdx(-1)
48 {
49 }
50 
57 {
58  //Do not delete
59  m_lpTrackBody = NULL;
60  m_lpCurrentWaypoint = NULL;
61  RemoveCurve();
62 }
63 
74 bool OsgLinearPath::AddControlPoint(const osg::Vec3d point,
75  const double time)
76 {
77  bool ret = false;
78 
79  osg::ref_ptr<ControlPoint> p = new ControlPoint(
80  time, point, osg::Vec3d(0.0, 0.0, 0.0));
81 
82  ret = AddControlPoint(p.get());
83 
84  return ret;
85 }
86 
97 {
98  bool ret = false;
99 
100  assert(p != NULL);
101 
102  if ((p != NULL) && (p->m_T >= 0.0)) {
103 
104  bool valid = true;
105  unsigned int afterI = -1;
106 
107  for (unsigned int i = 0; i < m_ControlPoints.size(); ++i) {
108  if (p->m_T < m_ControlPoints[i]->m_T) {
109  // insert point will be previous index
110  afterI = i - 1;
111  break;
112  } else if (p->m_T == m_ControlPoints[i]->m_T) {
113  // invalid time
114  valid = false;
115  break;
116  }
117 
118  // update insert point
119  afterI = i;
120  }
121 
122  if (valid) {
123 
124  // index to insert waypoint into
125  unsigned int index = afterI + 1;
126 
127  // insert the new waypoint into the list
128  PointListType::iterator iter = m_ControlPoints.begin() + index;
129  m_ControlPoints.insert(iter, p);
130  ret = true;
131  }
132  }
133 
134  // note curve as invalid
135  InvalidateCurve();
136 
137  return ret;
138 }
139 
146 {
147  // clear the list of control points
148  m_ControlPoints.clear();
149 
150  // note curve as invalid
151  InvalidateCurve();
152 }
153 
160 {
161  // curve is not valid
162  m_CurveIsValid = false;
163 }
164 
171 {
172  int iCount = m_ControlPoints.size();
173  for(int iIdx=0; iIdx<iCount; iIdx++)
174  {
175  ControlPoint *lpP0 = m_ControlPoints[iIdx].get();
176 
177  //If there are more points then calculate vel and unit vector to next one.
178  if(iIdx<(iCount-1))
179  {
180  ControlPoint *lpP1 = m_ControlPoints[iIdx+1].get();
181 
182  lpP0->m_vDist = lpP1->m_Pos - lpP0->m_Pos;
183  lpP0->m_dblDist = sqrt(pow(lpP0->m_vDist.x(), 2.0) + pow(lpP0->m_vDist.y(), 2.0) + pow(lpP0->m_vDist.z(), 2.0));
184  lpP0->m_V = lpP0->m_vDist / (lpP1->m_T - lpP0->m_T);
185  lpP0->m_Uv = lpP0->m_vDist;
186  lpP0->m_Uv.normalize();
187  lpP0->m_Tnext = lpP1->m_T;
188  }
189  //Otherwise set them to 0
190  else
191  {
192  lpP0->m_vDist.set(0, 0, 0);
193  lpP0->m_dblDist = 0;
194  lpP0->m_V.set(0, 0, 0);
195  lpP0->m_Uv.set(0, 0, 0);
196  lpP0->m_Tnext = lpP0->m_T;
197  }
198  }
199 
200  // mark curve as valid ready for interpolation
201  m_CurveIsValid = true;
202  return true;
203 }
204 
205 osg::Vec3d OsgLinearPath::GetPositionAtTime(const double t)
206 {
207  Interpolate(t);
208  return GetInterpPosition();
209 }
210 
211 void OsgLinearPath::FindCurrentWaypoint(const double t)
212 {
213  int iCount = m_ControlPoints.size();
214  if(m_lpCurrentWaypoint && (t > m_lpCurrentWaypoint->m_Tnext) && (m_iCurrentWaypointIdx < (iCount-1)) )
215  {
216  m_iCurrentWaypointIdx++;
217  m_lpCurrentWaypoint = m_ControlPoints[m_iCurrentWaypointIdx].get();
218 
219  //If the new one is within the specified time then return
220  if(m_lpCurrentWaypoint && ( (t >= m_lpCurrentWaypoint->m_T) && (t <= m_lpCurrentWaypoint->m_Tnext) ) )
221  return;
222  }
223 
224  //If we got here then we could not find the waypoint the easy way, so just go through the list of them.
225  for(int iIdx=0; iIdx<iCount; iIdx++)
226  {
227  ControlPoint *lpP0 = m_ControlPoints[iIdx].get();
228 
229  if( (t >= lpP0->m_T) && (t <= lpP0->m_Tnext) )
230  {
231  m_iCurrentWaypointIdx= iIdx;
232  m_lpCurrentWaypoint = lpP0;
233  return;
234  }
235  }
236 
237  //If we got here we could not find anything, so set it to null
238  m_iCurrentWaypointIdx = -1;
239  m_lpCurrentWaypoint = NULL;
240 }
241 
252 bool OsgLinearPath::Interpolate(const double t)
253 {
254  double interpTime = t;
255 
256  // clear last interpolated values
257  m_Interpolated.set(0.0, 0.0, 0.0);
258 
259  if (!m_CurveIsValid) {
260  // reconstruct the curve
261  BuildCurve();
262  }
263 
264  if(!m_lpCurrentWaypoint || (m_lpCurrentWaypoint && ( (t < m_lpCurrentWaypoint->m_T) || (t > m_lpCurrentWaypoint->m_Tnext) ) ) )
265  FindCurrentWaypoint(t);
266 
267  if(m_lpCurrentWaypoint)
268  {
269  //Time from start of this waypoint
270  double dblWPT = (t-m_lpCurrentWaypoint->m_T);
271  m_Interpolated = (m_lpCurrentWaypoint->m_V*dblWPT) + m_lpCurrentWaypoint->m_Pos;
272  }
273 
274  return true;
275 }
276 
288 osg::Node *OsgLinearPath::CreateTestGeom(const bool showControlPoints,
289  const unsigned int numSamples,
291 {
292  osg::Node *ret = NULL;
293 
294  if (!m_CurveIsValid) {
295  // reconstruct the curve
296  BuildCurve();
297  }
298 
299  if(m_ControlPoints.size() < 2)
300  return NULL;
301 
302  if (m_CurveIsValid) {
303 
304  osg::Group *group = new osg::Group();
305  group->setNodeMask(0xffffffff);
306 
307  if (showControlPoints) {
308 
309  // create the control point vertexes
310  osg::ref_ptr<osg::Vec3Array> controlVerts = new osg::Vec3Array;
311 
312  for (unsigned int i = 0; i < m_ControlPoints.size() - 1; ++i) {
313  osg::Vec3d p1 = m_ControlPoints[i]->m_Pos;
314  osg::Vec3d p2 = m_ControlPoints[i+1]->m_Pos;
315 
316  if (xform != NULL) {
317  p1 = xform->Transform(p1);
318  p2 = xform->Transform(p2);
319  }
320 
321  controlVerts->push_back(p1);
322  controlVerts->push_back(p2);
323  }
324 
325  osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
326  geometry->setVertexArray(controlVerts.get());
327 
328  osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
329  color->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));
330  geometry->setColorArray(color.get());
331  geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
332 
333  geometry->addPrimitiveSet(
334  new osg::DrawArrays(osg::PrimitiveSet::LINES,
335  0, controlVerts->size()));
336 
337  osg::ref_ptr<osg::Geode> geode = new osg::Geode;
338  geode->addDrawable(geometry.get());
339 
340  // attach geode to scene graph
341  group->addChild(geode.get());
342 
343  }
344 
345  {
346  // ------- Curve Geometry ------
347 
348  // create the curve vertexes
349  osg::ref_ptr<osg::Vec3Array> curveVerts = new osg::Vec3Array;
350 
351  for (unsigned int i = 0; i < m_ControlPoints.size() - 1; ++i) {
352 
353  // interpolate from one point to the next based on time
354 
355  double t = m_ControlPoints[i]->m_T;
356  double dt = (m_ControlPoints[i+1]->m_T - t) /
357  double(numSamples-1);
358 
359  for (unsigned int ti = 0; ti < numSamples; ++ti) {
360 
361  // get next point
363 
364  osg::Vec3d p = GetInterpPosition();
365 
366  if (xform != NULL) {
367  p = xform->Transform(p);
368  }
369 
370  // add point to the list
371  curveVerts->push_back(p);
372 
373  if (ti == (numSamples - 1)) {
374  // last point, complete the line strip with an extra point
375  curveVerts->push_back(p);
376  }
377 
378  // increment time
379  t += dt;
380 
381  }
382  }
383 
384  osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
385  geometry->setVertexArray(curveVerts.get());
386 
387  osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
388  color->push_back(osg::Vec4(m_vLineColor.r(), m_vLineColor.g(), m_vLineColor.b(), m_vLineColor.a()));
389  geometry->setColorArray(color.get());
390  geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
391 
392  geometry->addPrimitiveSet(
393  new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,
394  0, curveVerts->size()));
395 
396  osg::ref_ptr<osg::Geode> geode = new osg::Geode;
397  geode->addDrawable(geometry.get());
398 
399  // attach geode to scene graph
400  group->addChild(geode.get());
401 
402  // no lighting
403  group->getOrCreateStateSet()->setMode(GL_LIGHTING,
404  osg::StateAttribute::OFF);
405  }
406 
407  ret = group;
408  }
409 
410  return ret;
411 }
412 
421 CStdColor *OsgLinearPath::LineColor() {return &m_vLineColor;}
422 
431 void OsgLinearPath::LineColor(CStdColor &aryColor)
432 {
433  m_vLineColor = aryColor;
434  RedrawCurve();
435 }
436 
445 void OsgLinearPath::LineColor(float *aryColor)
446 {
447  CStdColor vColor(aryColor[0], aryColor[1], aryColor[2], aryColor[3], 1);
448  LineColor(vColor);
449 }
450 
460 void OsgLinearPath::LineColor(std::string strXml)
461 {
462  CStdColor vColor(1);
463  vColor.Load(strXml, "LineColor");
464  LineColor(vColor);
465 }
466 
475 std::string OsgLinearPath::PartID() {return m_strPartID;}
476 
485 void OsgLinearPath::PartID(std::string strID)
486 {
487  m_strPartID = strID;
488 }
489 
490 double OsgLinearPath::StartTime() {return m_dblStartTime;}
491 
492 void OsgLinearPath::StartTime(double dblTime, bool bSortPaths)
493 {
494  Std_IsAboveMin((double) 0, dblTime, true, "StartTime", true);
495  m_dblStartTime = dblTime;
496  RedrawCurve();
497 
498  if(bSortPaths && m_lpParentWindow)
499  m_lpParentWindow->SortPaths();
500 }
501 
502 double OsgLinearPath::EndTime() {return m_dblStartTime;}
503 
504 void OsgLinearPath::EndTime(double dblTime, bool bSortPaths)
505 {
506  Std_IsAboveMin((double) m_dblStartTime, dblTime, true, "EndTime", true);
507  m_dblEndTime = dblTime;
508 
509  if(bSortPaths && m_lpParentWindow)
510  m_lpParentWindow->SortPaths();
511 }
512 
513 bool OsgLinearPath::Visible()
514 {return m_bVisible;}
515 
516 void OsgLinearPath::Visible(bool bVal)
517 {
518  m_bVisible = bVal;
519  MakeVisible(bVal);
520 }
521 
522 bool OsgLinearPath::VisibleInSim()
523 {return m_bVisibleInSim;}
524 
525 void OsgLinearPath::VisibleInSim(bool bVal)
526 {m_bVisibleInSim = bVal;}
527 
528 void OsgLinearPath::MakeVisible(bool bVal)
529 {
530  if(m_osgSpline)
531  {
532  if(bVal)
533  m_osgSpline->setNodeMask(0x1);
534  else
535  m_osgSpline->setNodeMask(0x0);
536  }
537 }
538 
539 bool OsgLinearPath::ShowWaypoints()
540 {return m_bShowWaypoints;}
541 
542 void OsgLinearPath::ShowWaypoints(bool bVal)
543 {
544  m_bShowWaypoints = bVal;
545  RedrawCurve();
546 }
547 bool OsgLinearPath::BeforePathTime(double dblTime)
548 {
549  if(dblTime < m_dblStartTime)
550  return true;
551  else
552  return false;
553 }
554 
555 bool OsgLinearPath::WithinPathTime(double dblTime)
556 {
557  if(dblTime >= m_dblStartTime && dblTime <= m_dblEndTime)
558  return true;
559  else
560  return false;
561 }
562 
563 bool OsgLinearPath::AfterPathTime(double dblTime)
564 {
565  if(dblTime > m_dblEndTime)
566  return true;
567  else
568  return false;
569 }
570 
571 void OsgLinearPath::RemoveCurve()
572 {
573  VsSimulator *lpVsSim = dynamic_cast<VsSimulator *>(m_lpSim);
574 
575  if(lpVsSim && lpVsSim->OSGRoot())
576  {
577  //If we already have a curve then get rid of it first.
578  if(m_osgSpline.valid())
579  {
580  lpVsSim->OSGRoot()->removeChild(m_osgSpline.get());
581  m_osgSpline.release();
582  }
583  }
584 }
585 
586 void OsgLinearPath::RedrawCurve()
587 {
588  VsSimulator *lpVsSim = dynamic_cast<VsSimulator *>(m_lpSim);
589 
590  RemoveCurve();
591 
592  if(lpVsSim && lpVsSim->OSGRoot())
593  {
594  //Now recreate it.
595  InvalidateCurve();
596  m_osgSpline = CreateTestGeom(m_bShowWaypoints);
597  MakeVisible(m_bVisible);
598  lpVsSim->OSGRoot()->addChild(m_osgSpline.get());
599  }
600 }
601 
602 
604 {
605  MakeVisible(m_bVisibleInSim);
606 }
607 
609 {
610  MakeVisible(m_bVisible);
611 
612  int iCount = m_ControlPoints.size();
613  for(int iIndex=0; iIndex<iCount; iIndex++)
614  m_ControlPoints[iIndex]->ResetSimulation();
615 }
616 
617 
618 bool OsgLinearPath::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
619 {
620  std::string strType = Std_CheckString(strDataType);
621 
622  if(AnimatBase::SetData(strType, strValue, false))
623  return true;
624 
625  if(strType == "TRACKPARTID")
626  {
627  PartID(strValue);
628  Initialize();
629  return true;
630  }
631 
632  if(strType == "STARTTIME")
633  {
634  StartTime((double) atof(strValue.c_str()));
635  return true;
636  }
637 
638  if(strType == "ENDTIME")
639  {
640  EndTime((double) atof(strValue.c_str()));
641  return true;
642  }
643 
644  if(strType == "LINECOLOR")
645  {
646  LineColor(strValue);
647  return true;
648  }
649 
650  if(strDataType == "LINECOLOR.RED")
651  {
652  float aryVal[4] = {atof(strValue.c_str()), m_vLineColor.g(), m_vLineColor.b(), m_vLineColor.a()};
653  LineColor(aryVal);
654  return true;
655  }
656 
657  if(strDataType == "LINECOLOR.GREEN")
658  {
659  float aryVal[4] = {m_vLineColor.r(), atof(strValue.c_str()), m_vLineColor.b(), m_vLineColor.a()};
660  LineColor(aryVal);
661  return true;
662  }
663 
664  if(strDataType == "LINECOLOR.BLUE")
665  {
666  float aryVal[4] = {m_vLineColor.r(), m_vLineColor.g(), atof(strValue.c_str()), m_vLineColor.a()};
667  LineColor(aryVal);
668  return true;
669  }
670 
671  if(strDataType == "LINECOLOR.ALPHA")
672  {
673  float aryVal[4] = {m_vLineColor.r(), m_vLineColor.g(), m_vLineColor.b(), atof(strValue.c_str())};
674  LineColor(aryVal);
675  return true;
676  }
677 
678  if(strDataType == "VISIBLE")
679  {
680  Visible(Std_ToBool(strValue));
681  return true;
682  }
683 
684  if(strDataType == "VISIBLEINSIM")
685  {
686  VisibleInSim(Std_ToBool(strValue));
687  return true;
688  }
689 
690  if(strDataType == "SHOWWAYPOINTS")
691  {
692  ShowWaypoints(Std_ToBool(strValue));
693  return true;
694  }
695 
696  //If it was not one of those above then we have a problem.
697  if(bThrowError)
698  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
699 
700  return false;
701 }
702 
703 void OsgLinearPath::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
704 {
705  AnimatBase::QueryProperties(aryProperties);
706 
707  aryProperties.Add(new TypeProperty("TrackPartID", AnimatPropertyType::String, AnimatPropertyDirection::Set));
708  aryProperties.Add(new TypeProperty("StartTime", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
709  aryProperties.Add(new TypeProperty("EndTime", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
710  aryProperties.Add(new TypeProperty("LineColor", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
711  aryProperties.Add(new TypeProperty("LineColor.Red", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
712  aryProperties.Add(new TypeProperty("LineColor.Green", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
713  aryProperties.Add(new TypeProperty("LineColor.Blue", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
714  aryProperties.Add(new TypeProperty("LineColor.Alpha", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
715  aryProperties.Add(new TypeProperty("VisibleInSim", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
716 }
717 
718 
727 void OsgLinearPath::AddWaypoint(std::string strXml)
728 {
729  CStdXml oXml;
730  oXml.Deserialize(strXml);
731  oXml.FindElement("Root");
732  oXml.FindChildElement("Waypoint");
733 
734  osg::ref_ptr<ControlPoint> lpWaypoint = LoadWaypoint(oXml);
735 
736  lpWaypoint->Initialize();
737  RedrawCurve();
738 }
739 
750 void OsgLinearPath::RemoveWaypoint(std::string strID, bool bThrowError)
751 {
752  int iPos = FindWaypointPos(strID, bThrowError);
753  m_ControlPoints.erase(m_ControlPoints.begin()+iPos);
754  RedrawCurve();
755 }
756 
770 int OsgLinearPath::FindWaypointPos(std::string strID, bool bThrowError)
771 {
772  std::string sID = Std_ToUpper(Std_Trim(strID));
773 
774  int iCount = m_ControlPoints.size();
775  for(int iIndex=0; iIndex<iCount; iIndex++)
776  if(m_ControlPoints[iIndex]->ID() == sID)
777  return iIndex;
778 
779  if(bThrowError)
780  THROW_TEXT_ERROR(Al_Err_lBodyOrJointIDNotFound, Al_Err_strBodyOrJointIDNotFound, "ID");
781 
782  return -1;
783 }
784 
785 bool OsgLinearPath::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
786 {
787  std::string strType = Std_CheckString(strItemType);
788 
789  if(strType == "WAYPOINT")
790  {
791  AddWaypoint(strXml);
792  return true;
793  }
794 
795  //If it was not one of those above then we have a problem.
796  if(bThrowError)
797  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
798 
799  return false;
800 }
801 
802 bool OsgLinearPath::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
803 {
804  std::string strType = Std_CheckString(strItemType);
805 
806  if(strType == "WAYPOINT")
807  {
808  RemoveWaypoint(strID);
809  return true;
810  }
811 
812  //If it was not one of those above then we have a problem.
813  if(bThrowError)
814  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
815 
816  return false;
817 }
818 
820 {
821  AnimatBase::Initialize();
822 
823  m_lpTrackBody = NULL;
824  if(!Std_IsBlank(m_strPartID))
825  m_lpTrackBody = dynamic_cast<BodyPart *>(m_lpSim->FindByID(m_strPartID));
826 
827  RedrawCurve();
828 }
829 
830 void OsgLinearPath::Load(CStdXml &oXml)
831 {
832  AnimatBase::Load(oXml);
833 
834  oXml.IntoElem(); //Into Spline Element
835 
836  m_vLineColor.Load(oXml, "LineColor", false);
837 
838  EndTime(oXml.GetChildDouble("EndTime", m_dblEndTime), false);
839  StartTime(oXml.GetChildDouble("StartTime", m_dblStartTime), false);
840  PartID(oXml.GetChildString("LinkedBodyPartID", m_strPartID));
841  VisibleInSim(oXml.GetChildBool("VisibleInSim", m_bVisibleInSim));
842 
843  if(oXml.FindChildElement("Waypoints", false))
844  {
845  oXml.IntoElem(); //IntoOf Waypoints Element
846 
847  int iCount = oXml.NumberOfChildren();
848  for(int iIndex=0; iIndex<iCount; iIndex++)
849  {
850  oXml.FindChildByIndex(iIndex);
851  LoadWaypoint(oXml);
852  }
853 
854  oXml.OutOfElem(); //OutOf Waypoints Element
855  }
856 
857  oXml.OutOfElem(); //OutOf Spline Element
858 
859  if(m_lpParentWindow)
860  m_lpParentWindow->SortPaths();
861 }
862 
873 osg::ref_ptr<ControlPoint> OsgLinearPath::LoadWaypoint(CStdXml &oXml)
874 {
875  ControlPoint *lpPoint = NULL;
876 
877 try
878 {
879  osg::ref_ptr<ControlPoint> lpPoint(new ControlPoint());
880 
881  lpPoint->SetSystemPointers(m_lpSim, NULL, NULL, NULL, true);
882 
883  lpPoint->Load(oXml);
884  lpPoint->ParentSpline(this);
885 
886  if(!AddControlPoint(lpPoint.get()))
887  THROW_PARAM_ERROR(Vs_Err_lUnableToAddWaypoint, Vs_Err_strUnableToAddWaypoint, "Waypoint ID", lpPoint->ID());
888 
889  return lpPoint;
890 }
891 catch(CStdErrorInfo oError)
892 {
893  RELAY_ERROR(oError);
894  return NULL;
895 }
896 catch(...)
897 {
898  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
899  return NULL;
900 }
901 }
902 
903 
912 osg::Vec3d ControlPoint::Position() {return m_Pos;}
913 
924 void ControlPoint::Position(CStdFPoint &oPoint, bool bUseScaling)
925 {
926  CStdFPoint oNewPoint, oReportPosition;
927  if(bUseScaling && m_lpSim)
928  {
929  oNewPoint = oPoint * m_lpSim->InverseDistanceUnits();
930  oReportPosition = oNewPoint * m_lpSim->DistanceUnits();
931  }
932  else
933  {
934  oNewPoint = oPoint;
935  oReportPosition = oPoint;
936  }
937 
938  m_Pos.set(oNewPoint.x, oNewPoint.y, oNewPoint.z);
939  m_ReportPos.set(oReportPosition.x, oReportPosition.y, oReportPosition.z);
940 
941  if(m_lpParentSpline)
942  m_lpParentSpline->RedrawCurve();
943 }
944 
957 void ControlPoint::Position(float fltX, float fltY, float fltZ, bool bUseScaling)
958 {
959  CStdFPoint vPos(fltX, fltY, fltZ);
960  Position(vPos, bUseScaling);
961 }
962 
974 void ControlPoint::Position(std::string strXml, bool bUseScaling)
975 {
976  CStdXml oXml;
977  oXml.Deserialize(strXml);
978  oXml.FindElement("Root");
979  oXml.FindChildElement("Position");
980 
981  CStdFPoint vPos;
982  Std_LoadPoint(oXml, "Position", vPos);
983  Position(vPos, bUseScaling);
984 }
985 
986 double ControlPoint::Time()
987 {return m_T;}
988 
989 void ControlPoint::Time(double dblVal)
990 {
991  Std_IsAboveMin((double) 0, dblVal, true, "Waypoint Time", true);
992  m_T = dblVal;
993  if(m_lpParentSpline)
994  m_lpParentSpline->RedrawCurve();
995 }
996 
997 bool ControlPoint::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
998 {
999  std::string strType = Std_CheckString(strDataType);
1000 
1001  if(AnimatBase::SetData(strType, strValue, false))
1002  return true;
1003 
1004  if(strDataType == "POSITION")
1005  {
1006  Position(strValue);
1007  return true;
1008  }
1009 
1010  if(strDataType == "POSITION.X")
1011  {
1012  Position(atof(strValue.c_str()), m_ReportPos.y(), m_ReportPos.z());
1013  return true;
1014  }
1015 
1016  if(strDataType == "POSITION.Y")
1017  {
1018  Position(m_ReportPos.x(), atof(strValue.c_str()), m_ReportPos.z());
1019  return true;
1020  }
1021 
1022  if(strDataType == "POSITION.Z")
1023  {
1024  Position(m_ReportPos.x(), m_ReportPos.y(), atof(strValue.c_str()));
1025  return true;
1026  }
1027 
1028  if(strDataType == "TIME")
1029  {
1030  Time((double) atof(strValue.c_str()));
1031  return true;
1032  }
1033 
1034  //If it was not one of those above then we have a problem.
1035  if(bThrowError)
1036  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
1037 
1038  return false;
1039 }
1040 
1041 
1042 void ControlPoint::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
1043 {
1044  AnimatBase::QueryProperties(aryProperties);
1045 
1046  aryProperties.Add(new TypeProperty("Position", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
1047  aryProperties.Add(new TypeProperty("Position.X", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
1048  aryProperties.Add(new TypeProperty("Position.Y", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
1049  aryProperties.Add(new TypeProperty("Position.Z", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
1050  aryProperties.Add(new TypeProperty("Time", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
1051 }
1052 
1054 {
1055  AnimatBase::Load(oXml);
1056 
1057  oXml.IntoElem(); //Into Spline Element
1058 
1059  CStdFPoint vPos;
1060  Std_LoadPoint(oXml, "Position", vPos);
1061  Position(vPos);
1062 
1063  Time(oXml.GetChildDouble("Time", 0));
1064 
1065  oXml.OutOfElem(); //OutOf Spline Element
1066 }
1067 
1068  } // Visualization
1069 //} //VortexAnimatSim
1070 
1071 }
1072 
1073 
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
virtual osg::Node * CreateTestGeom(const bool showControlPoints=false, const unsigned int numSamples=3, ControlPointTransformFunctor *xform=NULL)
create and return a scene graph making up the curve
virtual CStdColor * LineColor()
Gets the LineColor color value.
virtual void RemoveWaypoint(std::string strID, bool bThrowError=true)
Removes the waypoint with the specified ID.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual std::string ID()
Gets the unique GUID ID of this object.
Definition: AnimatBase.cpp:167
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
virtual void AddWaypoint(std::string strXml)
Creates and adds a waypoint.
bool AddControlPoint(const osg::Vec3d point, const double time)
void InvalidateCurve()
mark curve as invalid (needs to be regenerated)
virtual void DistanceUnits(std::string strUnits)
Sets the distance units.
Definition: Simulator.cpp:1717
virtual double GetChildDouble(std::string strElementName)
Gets a double value from the element with the specified name.
Definition: StdXml.cpp:538
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Declares the vortex organism class.
virtual void SimStarting()
Called just before the simulation starts.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
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 bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
std::string Std_Trim(std::string strVal)
Trims a string.
virtual int FindWaypointPos(std::string strID, bool bThrowError=true)
Finds the array index for the waypoint with the specified ID.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual osg::Vec3d Position()
Gets the local position. (m_oPosition)
virtual AnimatBase * FindByID(std::string strID, bool bThrowError=true)
Searches for the object with the specified ID.
Definition: Simulator.cpp:4008
bool Std_ToBool(int iVal)
Converts a value toa bool.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void ResetSimulation()
Resets the simulation back to time 0.
osg::ref_ptr< ControlPoint > LoadWaypoint(CStdXml &oXml)
Loads a camera path waypoint.
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
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.
Declares the vortex Light class.
bool Std_LoadPoint(CStdXml &oXml, std::string strName, CStdIPoint &oPoint, bool bThrowError)
Standard load point.
virtual bool Interpolate(const double t)
Interpolate spline at time t.
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
virtual std::string PartID()
Gets the GUID ID of the part this camera will track while on this path.
Declares the vortex structure class.