2 #include "OsgMovableItem.h"
4 #include "OsgRigidBody.h"
9 #include "OsgMouseSpring.h"
11 #include "OsgCameraManipulator.h"
12 #include "OsgDragger.h"
13 #include "OsgMeshMinVertexDistanceVisitor.h"
15 #include "OsgLinearPath.h"
16 #include "OsgSimulationWindow.h"
17 #include "OsgScriptedSimulationWindow.h"
18 #include "OsgSimulator.h"
22 namespace Visualization
34 m_CurveIsValid(false),
35 m_Interpolated(0.0, 0.0, 0.0),
38 m_bVisibleInSim(false),
39 m_bShowWaypoints(false),
42 m_lpParentWindow(NULL),
43 m_lpCurrentWaypoint(NULL),
44 m_iCurrentWaypointIdx(-1)
57 m_lpCurrentWaypoint = NULL;
77 time, point, osg::Vec3d(0.0, 0.0, 0.0));
99 if ((p != NULL) && (p->m_T >= 0.0)) {
102 unsigned int afterI = -1;
104 for (
unsigned int i = 0; i < m_ControlPoints.size(); ++i) {
105 if (p->m_T < m_ControlPoints[i]->m_T) {
109 }
else if (p->m_T == m_ControlPoints[i]->m_T) {
122 unsigned int index = afterI + 1;
125 PointListType::iterator iter = m_ControlPoints.begin() + index;
126 m_ControlPoints.insert(iter, p);
145 m_ControlPoints.clear();
159 m_CurveIsValid =
false;
169 int iCount = m_ControlPoints.size();
170 for(
int iIdx=0; iIdx<iCount; iIdx++)
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;
189 lpP0->m_vDist.set(0, 0, 0);
191 lpP0->m_V.set(0, 0, 0);
192 lpP0->m_Uv.set(0, 0, 0);
193 lpP0->m_Tnext = lpP0->m_T;
198 m_CurveIsValid =
true;
202 osg::Vec3d OsgLinearPath::GetPositionAtTime(
const double t)
205 return GetInterpPosition();
208 void OsgLinearPath::FindCurrentWaypoint(
const double t)
210 int iCount = m_ControlPoints.size();
211 if(m_lpCurrentWaypoint && (t > m_lpCurrentWaypoint->m_Tnext) && (m_iCurrentWaypointIdx < (iCount-1)) )
213 m_iCurrentWaypointIdx++;
214 m_lpCurrentWaypoint = m_ControlPoints[m_iCurrentWaypointIdx].get();
217 if(m_lpCurrentWaypoint && ( (t >= m_lpCurrentWaypoint->m_T) && (t <= m_lpCurrentWaypoint->m_Tnext) ) )
222 for(
int iIdx=0; iIdx<iCount; iIdx++)
226 if( (t >= lpP0->m_T) && (t <= lpP0->m_Tnext) )
228 m_iCurrentWaypointIdx= iIdx;
229 m_lpCurrentWaypoint = lpP0;
235 m_iCurrentWaypointIdx = -1;
236 m_lpCurrentWaypoint = NULL;
251 double interpTime = t;
254 m_Interpolated.set(0.0, 0.0, 0.0);
256 if (!m_CurveIsValid) {
261 if(!m_lpCurrentWaypoint || (m_lpCurrentWaypoint && ( (t < m_lpCurrentWaypoint->m_T) || (t > m_lpCurrentWaypoint->m_Tnext) ) ) )
262 FindCurrentWaypoint(t);
264 if(m_lpCurrentWaypoint)
267 double dblWPT = (t-m_lpCurrentWaypoint->m_T);
268 m_Interpolated = (m_lpCurrentWaypoint->m_V*dblWPT) + m_lpCurrentWaypoint->m_Pos;
286 const unsigned int numSamples,
289 osg::Node *ret = NULL;
291 if (!m_CurveIsValid) {
296 if(m_ControlPoints.size() < 2)
299 if (m_CurveIsValid) {
301 osg::Group *group =
new osg::Group();
302 group->setNodeMask(0xffffffff);
304 if (showControlPoints) {
307 osg::ref_ptr<osg::Vec3Array> controlVerts =
new osg::Vec3Array;
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;
314 p1 = xform->Transform(p1);
315 p2 = xform->Transform(p2);
318 controlVerts->push_back(p1);
319 controlVerts->push_back(p2);
322 osg::ref_ptr<osg::Geometry> geometry =
new osg::Geometry;
323 geometry->setVertexArray(controlVerts.get());
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);
330 geometry->addPrimitiveSet(
331 new osg::DrawArrays(osg::PrimitiveSet::LINES,
332 0, controlVerts->size()));
334 osg::ref_ptr<osg::Geode> geode =
new osg::Geode;
335 geode->addDrawable(geometry.get());
338 group->addChild(geode.get());
346 osg::ref_ptr<osg::Vec3Array> curveVerts =
new osg::Vec3Array;
348 for (
unsigned int i = 0; i < m_ControlPoints.size() - 1; ++i) {
352 double t = m_ControlPoints[i]->m_T;
353 double dt = (m_ControlPoints[i+1]->m_T - t) /
354 double(numSamples-1);
356 for (
unsigned int ti = 0; ti < numSamples; ++ti) {
361 osg::Vec3d p = GetInterpPosition();
364 p = xform->Transform(p);
368 curveVerts->push_back(p);
370 if (ti == (numSamples - 1)) {
372 curveVerts->push_back(p);
381 osg::ref_ptr<osg::Geometry> geometry =
new osg::Geometry;
382 geometry->setVertexArray(curveVerts.get());
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);
389 geometry->addPrimitiveSet(
390 new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,
391 0, curveVerts->size()));
393 osg::ref_ptr<osg::Geode> geode =
new osg::Geode;
394 geode->addDrawable(geometry.get());
397 group->addChild(geode.get());
400 group->getOrCreateStateSet()->setMode(GL_LIGHTING,
401 osg::StateAttribute::OFF);
430 m_vLineColor = aryColor;
444 CStdColor vColor(aryColor[0], aryColor[1], aryColor[2], aryColor[3], 1);
460 vColor.Load(strXml,
"LineColor");
487 double OsgLinearPath::StartTime() {
return m_dblStartTime;}
489 void OsgLinearPath::StartTime(
double dblTime,
bool bSortPaths)
492 m_dblStartTime = dblTime;
495 if(bSortPaths && m_lpParentWindow)
496 m_lpParentWindow->SortPaths();
499 double OsgLinearPath::EndTime() {
return m_dblStartTime;}
501 void OsgLinearPath::EndTime(
double dblTime,
bool bSortPaths)
503 Std_IsAboveMin((
double) m_dblStartTime, dblTime,
true,
"EndTime",
true);
504 m_dblEndTime = dblTime;
506 if(bSortPaths && m_lpParentWindow)
507 m_lpParentWindow->SortPaths();
510 bool OsgLinearPath::Visible()
513 void OsgLinearPath::Visible(
bool bVal)
519 bool OsgLinearPath::VisibleInSim()
520 {
return m_bVisibleInSim;}
522 void OsgLinearPath::VisibleInSim(
bool bVal)
523 {m_bVisibleInSim = bVal;}
525 void OsgLinearPath::MakeVisible(
bool bVal)
530 m_osgSpline->setNodeMask(0x1);
532 m_osgSpline->setNodeMask(0x0);
536 bool OsgLinearPath::ShowWaypoints()
537 {
return m_bShowWaypoints;}
539 void OsgLinearPath::ShowWaypoints(
bool bVal)
541 m_bShowWaypoints = bVal;
544 bool OsgLinearPath::BeforePathTime(
double dblTime)
546 if(dblTime < m_dblStartTime)
552 bool OsgLinearPath::WithinPathTime(
double dblTime)
554 if(dblTime >= m_dblStartTime && dblTime <= m_dblEndTime)
560 bool OsgLinearPath::AfterPathTime(
double dblTime)
562 if(dblTime > m_dblEndTime)
568 void OsgLinearPath::RemoveCurve()
572 if(lpVsSim && lpVsSim->OSGRoot())
575 if(m_osgSpline.valid())
577 lpVsSim->OSGRoot()->removeChild(m_osgSpline.get());
578 m_osgSpline.release();
583 void OsgLinearPath::RedrawCurve()
589 if(lpVsSim && lpVsSim->OSGRoot())
594 MakeVisible(m_bVisible);
595 lpVsSim->OSGRoot()->addChild(m_osgSpline.get());
602 MakeVisible(m_bVisibleInSim);
607 MakeVisible(m_bVisible);
609 int iCount = m_ControlPoints.size();
610 for(
int iIndex=0; iIndex<iCount; iIndex++)
619 if(AnimatBase::SetData(strType, strValue,
false))
622 if(strType ==
"TRACKPARTID")
629 if(strType ==
"STARTTIME")
631 StartTime((
double) atof(strValue.c_str()));
635 if(strType ==
"ENDTIME")
637 EndTime((
double) atof(strValue.c_str()));
641 if(strType ==
"LINECOLOR")
647 if(strDataType ==
"LINECOLOR.RED")
649 float aryVal[4] = {(float) atof(strValue.c_str()), m_vLineColor.g(), m_vLineColor.b(), m_vLineColor.a()};
654 if(strDataType ==
"LINECOLOR.GREEN")
656 float aryVal[4] = {m_vLineColor.r(), (float) atof(strValue.c_str()), m_vLineColor.b(), m_vLineColor.a()};
661 if(strDataType ==
"LINECOLOR.BLUE")
663 float aryVal[4] = {m_vLineColor.r(), m_vLineColor.g(), (float) atof(strValue.c_str()), m_vLineColor.a()};
668 if(strDataType ==
"LINECOLOR.ALPHA")
670 float aryVal[4] = {m_vLineColor.r(), m_vLineColor.g(), m_vLineColor.b(), (float) atof(strValue.c_str())};
675 if(strDataType ==
"VISIBLE")
681 if(strDataType ==
"VISIBLEINSIM")
687 if(strDataType ==
"SHOWWAYPOINTS")
695 THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"Data Type", strDataType);
700 void OsgLinearPath::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
702 AnimatBase::QueryProperties(aryProperties);
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));
727 oXml.Deserialize(strXml);
728 oXml.FindElement(
"Root");
729 oXml.FindChildElement(
"Waypoint");
731 osg::ref_ptr<ControlPoint> lpWaypoint =
LoadWaypoint(oXml);
733 lpWaypoint->Initialize();
750 m_ControlPoints.erase(m_ControlPoints.begin()+iPos);
771 int iCount = m_ControlPoints.size();
772 for(
int iIndex=0; iIndex<iCount; iIndex++)
773 if(m_ControlPoints[iIndex]->
ID() == sID)
777 THROW_TEXT_ERROR(Al_Err_lBodyOrJointIDNotFound, Al_Err_strBodyOrJointIDNotFound,
"ID");
782 bool OsgLinearPath::AddItem(
const std::string &strItemType,
const std::string &strXml,
bool bThrowError,
bool bDoNotInit)
786 if(strType ==
"WAYPOINT")
794 THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType,
"Item Type", strItemType);
803 if(strType ==
"WAYPOINT")
811 THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType,
"Item Type", strItemType);
818 AnimatBase::Initialize();
820 m_lpTrackBody = NULL;
822 m_lpTrackBody =
dynamic_cast<BodyPart *
>(
m_lpSim->
FindByID(m_strPartID));
829 AnimatBase::Load(oXml);
833 m_vLineColor.Load(oXml,
"LineColor",
false);
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));
840 if(oXml.FindChildElement(
"Waypoints",
false))
844 int iCount = oXml.NumberOfChildren();
845 for(
int iIndex=0; iIndex<iCount; iIndex++)
847 oXml.FindChildByIndex(iIndex);
857 m_lpParentWindow->SortPaths();
878 lpPoint->SetSystemPointers(
m_lpSim, NULL, NULL, NULL,
true);
881 lpPoint->ParentSpline(
this);
884 THROW_PARAM_ERROR(Osg_Err_lUnableToAddWaypoint, Osg_Err_strUnableToAddWaypoint,
"Waypoint ID", lpPoint->ID());
888 catch(CStdErrorInfo oError)
895 THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
923 CStdFPoint oNewPoint, oReportPosition;
932 oReportPosition = oPoint;
935 m_Pos.set(oNewPoint.x, oNewPoint.y, oNewPoint.z);
936 m_ReportPos.set(oReportPosition.x, oReportPosition.y, oReportPosition.z);
939 m_lpParentSpline->RedrawCurve();
956 CStdFPoint vPos(fltX, fltY, fltZ);
957 Position(vPos, bUseScaling);
974 oXml.Deserialize(strXml);
975 oXml.FindElement(
"Root");
976 oXml.FindChildElement(
"Position");
980 Position(vPos, bUseScaling);
983 double ControlPoint::Time()
986 void ControlPoint::Time(
double dblVal)
991 m_lpParentSpline->RedrawCurve();
998 if(AnimatBase::SetData(strType, strValue,
false))
1001 if(strDataType ==
"POSITION")
1007 if(strDataType ==
"POSITION.X")
1009 Position(atof(strValue.c_str()), m_ReportPos.y(), m_ReportPos.z());
1013 if(strDataType ==
"POSITION.Y")
1015 Position(m_ReportPos.x(), atof(strValue.c_str()), m_ReportPos.z());
1019 if(strDataType ==
"POSITION.Z")
1021 Position(m_ReportPos.x(), m_ReportPos.y(), atof(strValue.c_str()));
1025 if(strDataType ==
"TIME")
1027 Time((
double) atof(strValue.c_str()));
1033 THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType,
"Data Type", strDataType);
1039 void ControlPoint::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
1041 AnimatBase::QueryProperties(aryProperties);
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));
1052 AnimatBase::Load(oXml);
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.
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 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.
virtual double GetChildDouble(std::string strElementName)
Gets a double value from the element with the specified name.
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.
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.
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.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
void ClearAllControlPoints()
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.
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.