2 #include "VsMovableItem.h"
5 #include "VsMotorizedJoint.h"
6 #include "VsRigidBody.h"
9 #include "VsClassFactory.h"
10 #include "VsSimulator.h"
13 #include "VsMouseSpring.h"
15 #include "VsCameraManipulator.h"
16 #include "VsDragger.h"
17 #include "MeshMinVertexDistanceVisitor.h"
19 #include "OsgLinearPath.h"
20 #include "VsSimulationWindow.h"
21 #include "VsScriptedSimulationWindow.h"
25 namespace Visualization
37 m_CurveIsValid(false),
38 m_Interpolated(0.0, 0.0, 0.0),
41 m_bVisibleInSim(false),
42 m_bShowWaypoints(false),
45 m_lpParentWindow(NULL),
46 m_lpCurrentWaypoint(NULL),
47 m_iCurrentWaypointIdx(-1)
60 m_lpCurrentWaypoint = NULL;
80 time, point, osg::Vec3d(0.0, 0.0, 0.0));
102 if ((p != NULL) && (p->m_T >= 0.0)) {
105 unsigned int afterI = -1;
107 for (
unsigned int i = 0; i < m_ControlPoints.size(); ++i) {
108 if (p->m_T < m_ControlPoints[i]->m_T) {
112 }
else if (p->m_T == m_ControlPoints[i]->m_T) {
125 unsigned int index = afterI + 1;
128 PointListType::iterator iter = m_ControlPoints.begin() + index;
129 m_ControlPoints.insert(iter, p);
148 m_ControlPoints.clear();
162 m_CurveIsValid =
false;
172 int iCount = m_ControlPoints.size();
173 for(
int iIdx=0; iIdx<iCount; iIdx++)
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;
192 lpP0->m_vDist.set(0, 0, 0);
194 lpP0->m_V.set(0, 0, 0);
195 lpP0->m_Uv.set(0, 0, 0);
196 lpP0->m_Tnext = lpP0->m_T;
201 m_CurveIsValid =
true;
205 osg::Vec3d OsgLinearPath::GetPositionAtTime(
const double t)
208 return GetInterpPosition();
211 void OsgLinearPath::FindCurrentWaypoint(
const double t)
213 int iCount = m_ControlPoints.size();
214 if(m_lpCurrentWaypoint && (t > m_lpCurrentWaypoint->m_Tnext) && (m_iCurrentWaypointIdx < (iCount-1)) )
216 m_iCurrentWaypointIdx++;
217 m_lpCurrentWaypoint = m_ControlPoints[m_iCurrentWaypointIdx].get();
220 if(m_lpCurrentWaypoint && ( (t >= m_lpCurrentWaypoint->m_T) && (t <= m_lpCurrentWaypoint->m_Tnext) ) )
225 for(
int iIdx=0; iIdx<iCount; iIdx++)
229 if( (t >= lpP0->m_T) && (t <= lpP0->m_Tnext) )
231 m_iCurrentWaypointIdx= iIdx;
232 m_lpCurrentWaypoint = lpP0;
238 m_iCurrentWaypointIdx = -1;
239 m_lpCurrentWaypoint = NULL;
254 double interpTime = t;
257 m_Interpolated.set(0.0, 0.0, 0.0);
259 if (!m_CurveIsValid) {
264 if(!m_lpCurrentWaypoint || (m_lpCurrentWaypoint && ( (t < m_lpCurrentWaypoint->m_T) || (t > m_lpCurrentWaypoint->m_Tnext) ) ) )
265 FindCurrentWaypoint(t);
267 if(m_lpCurrentWaypoint)
270 double dblWPT = (t-m_lpCurrentWaypoint->m_T);
271 m_Interpolated = (m_lpCurrentWaypoint->m_V*dblWPT) + m_lpCurrentWaypoint->m_Pos;
289 const unsigned int numSamples,
292 osg::Node *ret = NULL;
294 if (!m_CurveIsValid) {
299 if(m_ControlPoints.size() < 2)
302 if (m_CurveIsValid) {
304 osg::Group *group =
new osg::Group();
305 group->setNodeMask(0xffffffff);
307 if (showControlPoints) {
310 osg::ref_ptr<osg::Vec3Array> controlVerts =
new osg::Vec3Array;
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;
317 p1 = xform->Transform(p1);
318 p2 = xform->Transform(p2);
321 controlVerts->push_back(p1);
322 controlVerts->push_back(p2);
325 osg::ref_ptr<osg::Geometry> geometry =
new osg::Geometry;
326 geometry->setVertexArray(controlVerts.get());
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);
333 geometry->addPrimitiveSet(
334 new osg::DrawArrays(osg::PrimitiveSet::LINES,
335 0, controlVerts->size()));
337 osg::ref_ptr<osg::Geode> geode =
new osg::Geode;
338 geode->addDrawable(geometry.get());
341 group->addChild(geode.get());
349 osg::ref_ptr<osg::Vec3Array> curveVerts =
new osg::Vec3Array;
351 for (
unsigned int i = 0; i < m_ControlPoints.size() - 1; ++i) {
355 double t = m_ControlPoints[i]->m_T;
356 double dt = (m_ControlPoints[i+1]->m_T - t) /
357 double(numSamples-1);
359 for (
unsigned int ti = 0; ti < numSamples; ++ti) {
364 osg::Vec3d p = GetInterpPosition();
367 p = xform->Transform(p);
371 curveVerts->push_back(p);
373 if (ti == (numSamples - 1)) {
375 curveVerts->push_back(p);
384 osg::ref_ptr<osg::Geometry> geometry =
new osg::Geometry;
385 geometry->setVertexArray(curveVerts.get());
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);
392 geometry->addPrimitiveSet(
393 new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,
394 0, curveVerts->size()));
396 osg::ref_ptr<osg::Geode> geode =
new osg::Geode;
397 geode->addDrawable(geometry.get());
400 group->addChild(geode.get());
403 group->getOrCreateStateSet()->setMode(GL_LIGHTING,
404 osg::StateAttribute::OFF);
433 m_vLineColor = aryColor;
447 CStdColor vColor(aryColor[0], aryColor[1], aryColor[2], aryColor[3], 1);
463 vColor.Load(strXml,
"LineColor");
490 double OsgLinearPath::StartTime() {
return m_dblStartTime;}
492 void OsgLinearPath::StartTime(
double dblTime,
bool bSortPaths)
495 m_dblStartTime = dblTime;
498 if(bSortPaths && m_lpParentWindow)
499 m_lpParentWindow->SortPaths();
502 double OsgLinearPath::EndTime() {
return m_dblStartTime;}
504 void OsgLinearPath::EndTime(
double dblTime,
bool bSortPaths)
506 Std_IsAboveMin((
double) m_dblStartTime, dblTime,
true,
"EndTime",
true);
507 m_dblEndTime = dblTime;
509 if(bSortPaths && m_lpParentWindow)
510 m_lpParentWindow->SortPaths();
513 bool OsgLinearPath::Visible()
516 void OsgLinearPath::Visible(
bool bVal)
522 bool OsgLinearPath::VisibleInSim()
523 {
return m_bVisibleInSim;}
525 void OsgLinearPath::VisibleInSim(
bool bVal)
526 {m_bVisibleInSim = bVal;}
528 void OsgLinearPath::MakeVisible(
bool bVal)
533 m_osgSpline->setNodeMask(0x1);
535 m_osgSpline->setNodeMask(0x0);
539 bool OsgLinearPath::ShowWaypoints()
540 {
return m_bShowWaypoints;}
542 void OsgLinearPath::ShowWaypoints(
bool bVal)
544 m_bShowWaypoints = bVal;
547 bool OsgLinearPath::BeforePathTime(
double dblTime)
549 if(dblTime < m_dblStartTime)
555 bool OsgLinearPath::WithinPathTime(
double dblTime)
557 if(dblTime >= m_dblStartTime && dblTime <= m_dblEndTime)
563 bool OsgLinearPath::AfterPathTime(
double dblTime)
565 if(dblTime > m_dblEndTime)
571 void OsgLinearPath::RemoveCurve()
575 if(lpVsSim && lpVsSim->OSGRoot())
578 if(m_osgSpline.valid())
580 lpVsSim->OSGRoot()->removeChild(m_osgSpline.get());
581 m_osgSpline.release();
586 void OsgLinearPath::RedrawCurve()
592 if(lpVsSim && lpVsSim->OSGRoot())
597 MakeVisible(m_bVisible);
598 lpVsSim->OSGRoot()->addChild(m_osgSpline.get());
605 MakeVisible(m_bVisibleInSim);
610 MakeVisible(m_bVisible);
612 int iCount = m_ControlPoints.size();
613 for(
int iIndex=0; iIndex<iCount; iIndex++)
622 if(AnimatBase::SetData(strType, strValue,
false))
625 if(strType ==
"TRACKPARTID")
632 if(strType ==
"STARTTIME")
634 StartTime((
double) atof(strValue.c_str()));
638 if(strType ==
"ENDTIME")
640 EndTime((
double) atof(strValue.c_str()));
644 if(strType ==
"LINECOLOR")
650 if(strDataType ==
"LINECOLOR.RED")
652 float aryVal[4] = {atof(strValue.c_str()), m_vLineColor.g(), m_vLineColor.b(), m_vLineColor.a()};
657 if(strDataType ==
"LINECOLOR.GREEN")
659 float aryVal[4] = {m_vLineColor.r(), atof(strValue.c_str()), m_vLineColor.b(), m_vLineColor.a()};
664 if(strDataType ==
"LINECOLOR.BLUE")
666 float aryVal[4] = {m_vLineColor.r(), m_vLineColor.g(), atof(strValue.c_str()), m_vLineColor.a()};
671 if(strDataType ==
"LINECOLOR.ALPHA")
673 float aryVal[4] = {m_vLineColor.r(), m_vLineColor.g(), m_vLineColor.b(), atof(strValue.c_str())};
678 if(strDataType ==
"VISIBLE")
684 if(strDataType ==
"VISIBLEINSIM")
690 if(strDataType ==
"SHOWWAYPOINTS")
698 THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"Data Type", strDataType);
703 void OsgLinearPath::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
705 AnimatBase::QueryProperties(aryProperties);
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));
730 oXml.Deserialize(strXml);
731 oXml.FindElement(
"Root");
732 oXml.FindChildElement(
"Waypoint");
734 osg::ref_ptr<ControlPoint> lpWaypoint =
LoadWaypoint(oXml);
736 lpWaypoint->Initialize();
753 m_ControlPoints.erase(m_ControlPoints.begin()+iPos);
774 int iCount = m_ControlPoints.size();
775 for(
int iIndex=0; iIndex<iCount; iIndex++)
776 if(m_ControlPoints[iIndex]->
ID() == sID)
780 THROW_TEXT_ERROR(Al_Err_lBodyOrJointIDNotFound, Al_Err_strBodyOrJointIDNotFound,
"ID");
785 bool OsgLinearPath::AddItem(
const std::string &strItemType,
const std::string &strXml,
bool bThrowError,
bool bDoNotInit)
789 if(strType ==
"WAYPOINT")
797 THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType,
"Item Type", strItemType);
806 if(strType ==
"WAYPOINT")
814 THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType,
"Item Type", strItemType);
821 AnimatBase::Initialize();
823 m_lpTrackBody = NULL;
825 m_lpTrackBody =
dynamic_cast<BodyPart *
>(
m_lpSim->
FindByID(m_strPartID));
832 AnimatBase::Load(oXml);
836 m_vLineColor.Load(oXml,
"LineColor",
false);
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));
843 if(oXml.FindChildElement(
"Waypoints",
false))
847 int iCount = oXml.NumberOfChildren();
848 for(
int iIndex=0; iIndex<iCount; iIndex++)
850 oXml.FindChildByIndex(iIndex);
860 m_lpParentWindow->SortPaths();
881 lpPoint->SetSystemPointers(
m_lpSim, NULL, NULL, NULL,
true);
884 lpPoint->ParentSpline(
this);
887 THROW_PARAM_ERROR(Vs_Err_lUnableToAddWaypoint, Vs_Err_strUnableToAddWaypoint,
"Waypoint ID", lpPoint->ID());
891 catch(CStdErrorInfo oError)
898 THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
926 CStdFPoint oNewPoint, oReportPosition;
935 oReportPosition = oPoint;
938 m_Pos.set(oNewPoint.x, oNewPoint.y, oNewPoint.z);
939 m_ReportPos.set(oReportPosition.x, oReportPosition.y, oReportPosition.z);
942 m_lpParentSpline->RedrawCurve();
959 CStdFPoint vPos(fltX, fltY, fltZ);
960 Position(vPos, bUseScaling);
977 oXml.Deserialize(strXml);
978 oXml.FindElement(
"Root");
979 oXml.FindChildElement(
"Position");
983 Position(vPos, bUseScaling);
986 double ControlPoint::Time()
989 void ControlPoint::Time(
double dblVal)
994 m_lpParentSpline->RedrawCurve();
1001 if(AnimatBase::SetData(strType, strValue,
false))
1004 if(strDataType ==
"POSITION")
1010 if(strDataType ==
"POSITION.X")
1012 Position(atof(strValue.c_str()), m_ReportPos.y(), m_ReportPos.z());
1016 if(strDataType ==
"POSITION.Y")
1018 Position(m_ReportPos.x(), atof(strValue.c_str()), m_ReportPos.z());
1022 if(strDataType ==
"POSITION.Z")
1024 Position(m_ReportPos.x(), m_ReportPos.y(), atof(strValue.c_str()));
1028 if(strDataType ==
"TIME")
1030 Time((
double) atof(strValue.c_str()));
1036 THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"Data Type", strDataType);
1042 void ControlPoint::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
1044 AnimatBase::QueryProperties(aryProperties);
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));
1055 AnimatBase::Load(oXml);
virtual void Initialize()
Initializes this object.
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.
virtual std::string ID()
Gets the unique GUID ID of this object.
virtual bool IntoElem()
Goes into the next element where the cursor is located.
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.
virtual double GetChildDouble(std::string strElementName)
Gets a double value from the element with the specified name.
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.
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.
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.
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.
void ClearAllControlPoints()
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.