AnimatLab  2
Test
VsPrismatic.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "VsMovableItem.h"
9 #include "VsBody.h"
10 #include "VsJoint.h"
11 #include "VsMotorizedJoint.h"
12 #include "VsPrismaticLimit.h"
13 #include "VsRigidBody.h"
14 #include "VsPrismatic.h"
15 #include "VsStructure.h"
16 #include "VsSimulator.h"
17 #include "VsOsgUserData.h"
18 #include "VsOsgUserDataVisitor.h"
19 #include "VsDragger.h"
20 
21 namespace VortexAnimatSim
22 {
23  namespace Environment
24  {
25  namespace Joints
26  {
27 
35 {
36  SetThisPointers();
37  m_vxPrismatic = NULL;
38 
42 
43  m_lpUpperLimit->LimitPos(1, false);
44  m_lpLowerLimit->LimitPos(-1, false);
45  m_lpPosFlap->LimitPos(Prismatic::JointPosition(), false);
47 
48  m_lpUpperLimit->Color(0, 0, 1, 1);
49  m_lpLowerLimit->Color(1, 1, 0.333, 1);
50  m_lpPosFlap->Color(1, 0, 1, 1);
51 
54 }
55 
63 {
64  //ConstraintLimits are deleted in the base objects.
65  try
66  {
67  DeleteGraphics();
68  DeletePhysics();
69  }
70  catch(...)
71  {Std_TraceMsg(0, "Caught Error in desctructor of VsPrismatic/\r\n", "", -1, false, true);}
72 }
73 
74 
76 {
77  Prismatic::EnableLimits(bVal);
78 
79  if(m_vxPrismatic)
80  m_vxPrismatic->setLimitsActive(m_vxPrismatic->kLinearCoordinate, m_bEnableLimits);
81 
82  if(m_bEnableLimits)
83  {
86  }
87 }
88 
89 void VsPrismatic::JointPosition(float fltPos)
90 {
91  m_fltPosition = fltPos;
92  if(m_lpPosFlap)
93  m_lpPosFlap->LimitPos(fltPos);
94 }
95 
96 
97 void VsPrismatic::SetAlpha()
98 {
99  VsJoint::SetAlpha();
100 
104 }
105 
106 void VsPrismatic::DeleteJointGraphics()
107 {
108  VsPrismaticLimit *lpUpperLimit = dynamic_cast<VsPrismaticLimit *>(m_lpUpperLimit);
109  VsPrismaticLimit *lpLowerLimit = dynamic_cast<VsPrismaticLimit *>(m_lpLowerLimit);
110  VsPrismaticLimit *lpPosFlap = dynamic_cast<VsPrismaticLimit *>(m_lpPosFlap);
111 
112  if(m_osgJointMT.valid())
113  {
114  if(lpUpperLimit && lpUpperLimit->BoxMT()) m_osgJointMT->removeChild(lpUpperLimit->BoxMT());
115  if(lpUpperLimit && lpUpperLimit->CylinderMT()) m_osgJointMT->removeChild(lpUpperLimit->CylinderMT());
116 
117  if(lpLowerLimit && lpLowerLimit->BoxMT()) m_osgJointMT->removeChild(lpLowerLimit->BoxMT());
118  if(lpLowerLimit && lpLowerLimit->CylinderMT()) m_osgJointMT->removeChild(lpLowerLimit->CylinderMT());
119 
120  if(lpPosFlap && lpPosFlap->BoxMT()) m_osgJointMT->removeChild(lpPosFlap->BoxMT());
121  }
122 
126 }
127 
129 {
130  VsPrismaticLimit *lpUpperLimit = dynamic_cast<VsPrismaticLimit *>(m_lpUpperLimit);
131  VsPrismaticLimit *lpLowerLimit = dynamic_cast<VsPrismaticLimit *>(m_lpLowerLimit);
132  VsPrismaticLimit *lpPosFlap = dynamic_cast<VsPrismaticLimit *>(m_lpPosFlap);
133 
134  lpPosFlap->LimitPos(Prismatic::JointPosition());
135 
136  lpUpperLimit->SetupGraphics();
137  lpLowerLimit->SetupGraphics();
138  lpPosFlap->SetupGraphics();
139 
140  m_osgJointMT->addChild(lpUpperLimit->BoxMT());
141  m_osgJointMT->addChild(lpUpperLimit->CylinderMT());
142 
143  m_osgJointMT->addChild(lpLowerLimit->BoxMT());
144  m_osgJointMT->addChild(lpLowerLimit->CylinderMT());
145 
146  m_osgJointMT->addChild(lpPosFlap->BoxMT());
147 }
148 
150 {
151  //The parent osg object for the joint is actually the child rigid body object.
152  m_osgParent = ParentOSG();
153 
154  if(m_osgParent.valid())
155  {
156  //Add the parts to the group node.
157  CStdFPoint vPos(0, 0, 0), vRot(VX_PI/2, 0, 0);
158  vPos.Set(0, 0, 0); vRot.Set(0, VX_PI/2, 0);
159  m_osgJointMT = new osg::MatrixTransform();
160  m_osgJointMT->setMatrix(SetupMatrix(vPos, vRot));
161 
163 
164  m_osgNode = m_osgJointMT.get();
165 
166  BuildLocalMatrix();
167 
168  SetAlpha();
169  SetCulling();
170  SetVisible(m_lpThisMI->IsVisible());
171 
172  //Add it to the scene graph.
173  m_osgParent->addChild(m_osgRoot.get());
174 
175  //Set the position with the world coordinates.
176  Physics_UpdateAbsolutePosition();
177 
178  //We need to set the UserData on the OSG side so we can do picking.
179  //We need to use a node visitor to set the user data for all drawable nodes in all geodes for the group.
180  osg::ref_ptr<VsOsgUserDataVisitor> osgVisitor = new VsOsgUserDataVisitor(this);
181  osgVisitor->traverse(*m_osgMT);
182  }
183 }
184 
185 void VsPrismatic::DeletePhysics()
186 {
187  if(!m_vxPrismatic)
188  return;
189 
190  if(GetVsSimulator() && GetVsSimulator()->Universe())
191  {
192  GetVsSimulator()->Universe()->removeConstraint(m_vxPrismatic);
193  delete m_vxPrismatic;
194 
195  if(m_lpChild && m_lpParent)
197  }
198 
199  m_vxPrismatic = NULL;
200  m_vxJoint = NULL;
201 }
202 
203 void VsPrismatic::SetupPhysics()
204 {
205  if(m_vxPrismatic)
206  DeletePhysics();
207 
208  if(!m_lpParent)
209  THROW_ERROR(Al_Err_lParentNotDefined, Al_Err_strParentNotDefined);
210 
211  if(!m_lpChild)
212  THROW_ERROR(Al_Err_lChildNotDefined, Al_Err_strChildNotDefined);
213 
214  VsRigidBody *lpVsParent = dynamic_cast<VsRigidBody *>(m_lpParent);
215  if(!lpVsParent)
216  THROW_ERROR(Vs_Err_lUnableToConvertToVsRigidBody, Vs_Err_strUnableToConvertToVsRigidBody);
217 
218  VsRigidBody *lpVsChild = dynamic_cast<VsRigidBody *>(m_lpChild);
219  if(!lpVsChild)
220  THROW_ERROR(Vs_Err_lUnableToConvertToVsRigidBody, Vs_Err_strUnableToConvertToVsRigidBody);
221 
222  CStdFPoint vGlobal = this->GetOSGWorldCoords();
223 
224  Vx::VxReal44 vMT;
225  VxOSG::copyOsgMatrix_to_VxReal44(this->GetOSGWorldMatrix(true), vMT);
226  Vx::VxTransform vTrans(vMT);
227  Vx::VxReal3 vxRot;
228  vTrans.getRotationEulerAngles(vxRot);
229 
230  CStdFPoint vLocalRot(vxRot[0], vxRot[1], vxRot[2]);
231 
232  VxVector3 pos((double) vGlobal.x, (double) vGlobal.y, (double) vGlobal.z);
233  VxVector3 axis = NormalizeAxis(vLocalRot);
234 
235  m_vxPrismatic = new VxPrismatic(lpVsParent->Part(), lpVsChild->Part(), pos.v, axis.v);
236  m_vxPrismatic->setName(m_strID.c_str());
237 
238  GetVsSimulator()->Universe()->addConstraint(m_vxPrismatic);
239 
240  //Disable collisions between this object and its parent
242 
243  VsPrismaticLimit *lpUpperLimit = dynamic_cast<VsPrismaticLimit *>(m_lpUpperLimit);
244  VsPrismaticLimit *lpLowerLimit = dynamic_cast<VsPrismaticLimit *>(m_lpLowerLimit);
245 
246  lpUpperLimit->PrismaticRef(m_vxPrismatic);
247  lpLowerLimit->PrismaticRef(m_vxPrismatic);
248 
249  //Re-enable the limits once we have initialized the joint
251 
252  m_vxJoint = m_vxPrismatic;
253  m_iCoordID = m_vxPrismatic->kLinearCoordinate;
254 
255  //If the motor is enabled then it will start out with a velocity of zero.
257 
258  Prismatic::Initialize();
259  VsJoint::Initialize();
260 }
261 
263 {
264  SetupGraphics();
265  SetupPhysics();
266 }
267 
268 #pragma region DataAccesMethods
269 
270 float *VsPrismatic::GetDataPointer(const std::string &strDataType)
271 {
272  float *lpData=NULL;
273  std::string strType = Std_CheckString(strDataType);
274 
275  if(strType == "JOINTROTATION")
276  return &m_fltPosition;
277  else if(strType == "JOINTPOSITION")
278  return &m_fltPosition;
279  else if(strType == "JOINTACTUALVELOCITY")
280  return &m_fltVelocity;
281  else if(strType == "JOINTFORCE")
282  return &m_fltForce;
283  else if(strType == "JOINTDESIREDVELOCITY")
284  return &m_fltReportSetVelocity;
285  else if(strType == "JOINTSETVELOCITY")
286  return &m_fltReportSetVelocity;
287  else if(strType == "JOINTDESIREDPOSITION")
288  return &m_fltReportSetPosition;
289  else if(strType == "JOINTSETPOSITION")
290  return &m_fltReportSetPosition;
291  else if(strType == "ENABLE")
292  return &m_fltEnabled;
293  else if(strType == "MOTORFORCETOAX")
294  return &m_fltNullReport;
295  else if(strType == "MOTORFORCETOAY")
296  return &m_fltNullReport;
297  else if(strType == "MOTORFORCETOAZ")
298  return &m_fltNullReport;
299  else if(strType == "MOTORFORCETOBX")
300  return &m_fltNullReport;
301  else if(strType == "MOTORFORCETOBY")
302  return &m_fltNullReport;
303  else if(strType == "MOTORFORCETOBZ")
304  return &m_fltNullReport;
305  else if(strType == "MOTORTORQUETOAX")
306  return &m_fltNullReport;
307  else if(strType == "MOTORTORQUETOAY")
308  return &m_fltNullReport;
309  else if(strType == "MOTORTORQUETOAZ")
310  return &m_fltNullReport;
311  else if(strType == "MOTORTORQUETOBX")
312  return &m_fltNullReport;
313  else if(strType == "MOTORTORQUETOBY")
314  return &m_fltNullReport;
315  else if(strType == "MOTORTORQUETOBZ")
316  return &m_fltNullReport;
317  else if(strType == "CONTACTCOUNT")
318  THROW_PARAM_ERROR(Al_Err_lMustBeContactBodyToGetCount, Al_Err_strMustBeContactBodyToGetCount, "JointID", m_strName);
319  else
320  {
321  lpData = Prismatic::GetDataPointer(strDataType);
322  if(lpData) return lpData;
323 
324  THROW_TEXT_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "JointID: " + STR(m_strName) + " DataType: " + strDataType);
325  }
326 
327  return lpData;
328 }
329 
330 bool VsPrismatic::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
331 {
332  if(VsJoint::Physics_SetData(strDataType, strValue))
333  return true;
334 
335  if(Prismatic::SetData(strDataType, strValue, false))
336  return true;
337 
338  //If it was not one of those above then we have a problem.
339  if(bThrowError)
340  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
341 
342  return false;
343 }
344 
345 void VsPrismatic::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
346 {
347  VsJoint::Physics_QueryProperties(aryProperties);
348  Prismatic::QueryProperties(aryProperties);
349 
350  aryProperties.Add(new TypeProperty("JointRotation", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
351  aryProperties.Add(new TypeProperty("JointPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
352  aryProperties.Add(new TypeProperty("JointActualVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
353  aryProperties.Add(new TypeProperty("JointForce", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
354  aryProperties.Add(new TypeProperty("JointRotationDeg", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
355  aryProperties.Add(new TypeProperty("JointDesiredPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
356  aryProperties.Add(new TypeProperty("JointSetPosition", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
357  aryProperties.Add(new TypeProperty("JointDesiredVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
358  aryProperties.Add(new TypeProperty("JointSetVelocity", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
359  aryProperties.Add(new TypeProperty("Enable", AnimatPropertyType::Boolean, AnimatPropertyDirection::Get));
360  aryProperties.Add(new TypeProperty("MotorForceToAX", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
361  aryProperties.Add(new TypeProperty("MotorForceToAY", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
362  aryProperties.Add(new TypeProperty("MotorForceToAZ", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
363  aryProperties.Add(new TypeProperty("MotorForceToBX", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
364  aryProperties.Add(new TypeProperty("MotorForceToBY", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
365  aryProperties.Add(new TypeProperty("MotorForceToBZ", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
366  aryProperties.Add(new TypeProperty("MotorTorqueToAX", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
367  aryProperties.Add(new TypeProperty("MotorTorqueToAY", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
368  aryProperties.Add(new TypeProperty("MotorTorqueToAZ", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
369  aryProperties.Add(new TypeProperty("MotorTorqueToBX", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
370  aryProperties.Add(new TypeProperty("MotorTorqueToBY", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
371  aryProperties.Add(new TypeProperty("MotorTorqueToBZ", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
372 }
373 
374 #pragma endregion
375 
377 {
378  UpdateData();
380 }
381 
382  } //Joints
383  } // Environment
384 } //VortexAnimatSim
Declares the vs prismatic class.
virtual void DisableCollision(RigidBody *lpBody)
Disables collision between the past-in object and this object.
Definition: RigidBody.cpp:1374
virtual void CreateJoint()
Creates the joint.
virtual void EnableCollision(RigidBody *lpBody)
Enables collision between the past-in object and this object.
Definition: RigidBody.cpp:1343
virtual float LimitPos()
Gets the limit position.
virtual void UpdateData()
Called to collect any body data for this part.
Definition: BodyPart.cpp:80
float m_fltNullReport
This is used to report back 0 from GetDataPointer for items that are not supported in vortex...
Definition: VsMovableItem.h:40
A common class for all rigid body data specific to vortex.
Definition: VsRigidBody.h:55
virtual void Alpha(float fltA)=0
Sets the alpha color value for this constraint.
float m_fltEnabled
This is used for reporting the enabled state in a GetDataPointer call.
Definition: Node.h:35
virtual void StepSimulation()
Step the simulation for this object.
virtual void IsShowPosition(bool bVal)
Sets whether this contstraint is actually just being used to show the current position of the joint...
std::string m_strID
The unique Id for this object.
Definition: AnimatBase.h:55
ConstraintLimit * m_lpUpperLimit
Upper limit constring pointer.
Definition: Prismatic.h:37
virtual void SetupGraphics()
Sets up the graphics for the constraint.
virtual void SetupGraphics()
Sets up the graphics for the joint.
virtual bool EnableMotor()
Tells if the motor is enabled.
RigidBody * m_lpChild
The child rigid body for this joint.
Definition: Joint.h:44
bool m_bEnableLimits
If true then any ConstraintLimits for this joint are enabled.
Definition: Joint.h:47
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
ConstraintLimit * m_lpLowerLimit
Lower limit constring pointer.
Definition: Prismatic.h:40
float m_fltForce
The current force being applied to the joint by the motor.
Definition: Joint.h:58
osg::ref_ptr< osg::MatrixTransform > m_osgJointMT
The osg joint matrix transform.
Definition: VsJoint.h:61
float m_fltReportSetPosition
This is the variable that is reported to AnimatLab on what the set position was.
virtual void SetLimitPos()=0
Sets the limit position using the current value set within the object.
float m_fltAlpha
The current alpha transparency for this body part.
Definition: MovableItem.h:76
float m_fltReportSetVelocity
This is the variable that is reported to AnimatLab on what the set veloicty was.
ConstraintLimit * m_lpPosFlap
Pointer to a constraint that is used to represent the position flap.
Definition: Prismatic.h:43
virtual void SetVelocityToDesired()
Sets the desired velocity to use for the motor.
virtual float JointPosition()
Gets the joint position.
Definition: Joint.cpp:532
virtual void DeleteGraphics()=0
Deletes up the graphics for the constraint.
virtual void Color(float fltR, float fltG, float fltB, float fltA)
Sets the color to use when displaying this contraint.
virtual void IsLowerLimit(bool bVal)
Sets whether this is a lower limit or not..
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual bool EnableLimits()
Tells if ConstraintLimits are enabled.
Definition: Joint.cpp:139
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61
Declares the vortex structure class.
virtual void CreateJointGraphics()
Creates the default ball graphics.