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