AnimatLab  2
Test
Joint.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include "Node.h"
13 #include "IPhysicsMovableItem.h"
14 #include "IPhysicsBody.h"
15 #include "BoundingBox.h"
16 #include "MovableItem.h"
17 #include "BodyPart.h"
18 #include "Joint.h"
19 #include "ReceptiveField.h"
20 #include "ContactSensor.h"
21 #include "RigidBody.h"
22 #include "Structure.h"
23 #include "Organism.h"
24 #include "ActivatedItem.h"
25 #include "ActivatedItemMgr.h"
26 #include "DataChartMgr.h"
27 #include "ExternalStimuliMgr.h"
28 #include "KeyFrame.h"
29 #include "SimulationRecorder.h"
30 #include "OdorType.h"
31 #include "Odor.h"
32 #include "Light.h"
33 #include "LightManager.h"
34 #include "Simulator.h"
35 
36 namespace AnimatSim
37 {
38  namespace Environment
39  {
40 
48 {
49  m_lpChild = NULL;
50  m_fltPosition = 0;
51  m_fltVelocity = 0;
52  m_fltForce = 0;
53  m_fltSize = 0.02f;
54  m_bEnableLimits = true;
55 
56  for(int iIdx=0; iIdx<6; iIdx++)
57  m_aryRelaxations[iIdx] = NULL;
58 
59  m_lpFriction = NULL;
60 }
61 
69 {
70 
71 try
72 {
73  //We also do not delete our references to these objects.
74  m_lpParent = NULL;
75  m_lpChild = NULL;
76 
78 
79  if(m_lpFriction) {delete m_lpFriction; m_lpFriction = NULL;}
80 }
81 catch(...)
82 {Std_TraceMsg(0, "Caught Error in desctructor of Joint\r\n", "", -1, false, true);}
83 }
84 
85 
97 bool Joint::UsesRadians() {return true;}
98 
107 float Joint::Size() {return m_fltSize;};
108 
118 void Joint::Size(float fltVal, bool bUseScaling)
119 {
120  Std_IsAboveMin((float) 0, fltVal, true, "Joint.Size");
121  if(bUseScaling)
122  m_fltSize = fltVal * m_lpSim->InverseDistanceUnits();
123  else
124  m_fltSize = fltVal;
125 
126  Resize();
127 }
128 
129 
130 
140 
149 void Joint::EnableLimits(bool bVal) {m_bEnableLimits = bVal;}
150 
160 
170 {
171  if(m_aryRelaxations[0])
172  {
173  delete m_aryRelaxations[0];
174  m_aryRelaxations[0] = NULL;
175  }
176 
177  m_aryRelaxations[0] = lpRelax;
178 }
179 
188 void Joint::Relaxation1(std::string strXml)
189 {
190  CStdXml oXml;
191  oXml.Deserialize(strXml);
192  oXml.FindElement("Root");
193 
194  Relaxation1(LoadConstraintRelaxation(oXml, "Relaxation1"));
195 }
196 
206 
216 {
217  if(m_aryRelaxations[1])
218  {
219  delete m_aryRelaxations[1];
220  m_aryRelaxations[1] = NULL;
221  }
222 
223  m_aryRelaxations[1] = lpRelax;
224 }
225 
234 void Joint::Relaxation2(std::string strXml)
235 {
236  CStdXml oXml;
237  oXml.Deserialize(strXml);
238  oXml.FindElement("Root");
239 
240  Relaxation2(LoadConstraintRelaxation(oXml, "Relaxation2"));
241 }
242 
252 
262 {
263  if(m_aryRelaxations[2])
264  {
265  delete m_aryRelaxations[2];
266  m_aryRelaxations[2] = NULL;
267  }
268 
269  m_aryRelaxations[2] = lpRelax;
270 }
271 
280 void Joint::Relaxation3(std::string strXml)
281 {
282  CStdXml oXml;
283  oXml.Deserialize(strXml);
284  oXml.FindElement("Root");
285 
286  Relaxation3(LoadConstraintRelaxation(oXml, "Relaxation3"));
287 }
288 
298 
308 {
309  if(m_aryRelaxations[3])
310  {
311  delete m_aryRelaxations[3];
312  m_aryRelaxations[3] = NULL;
313  }
314 
315  m_aryRelaxations[3] = lpRelax;
316 }
317 
326 void Joint::Relaxation4(std::string strXml)
327 {
328  CStdXml oXml;
329  oXml.Deserialize(strXml);
330  oXml.FindElement("Root");
331 
332  Relaxation4(LoadConstraintRelaxation(oXml, "Relaxation4"));
333 }
334 
344 
354 {
355  if(m_aryRelaxations[4])
356  {
357  delete m_aryRelaxations[4];
358  m_aryRelaxations[4] = NULL;
359  }
360 
361  m_aryRelaxations[4] = lpRelax;
362 }
363 
372 void Joint::Relaxation5(std::string strXml)
373 {
374  CStdXml oXml;
375  oXml.Deserialize(strXml);
376  oXml.FindElement("Root");
377 
378  Relaxation5(LoadConstraintRelaxation(oXml, "Relaxation5"));
379 }
380 
390 
400 {
401  if(m_aryRelaxations[5])
402  {
403  delete m_aryRelaxations[5];
404  m_aryRelaxations[5] = NULL;
405  }
406 
407  m_aryRelaxations[5] = lpRelax;
408 }
409 
418 void Joint::Relaxation6(std::string strXml)
419 {
420  CStdXml oXml;
421  oXml.Deserialize(strXml);
422  oXml.FindElement("Root");
423 
424  Relaxation6(LoadConstraintRelaxation(oXml, "Relaxation6"));
425 }
426 
436 
446 {
447  if(m_lpFriction)
448  {
449  delete m_lpFriction;
450  m_lpFriction = NULL;
451  }
452 
453  m_lpFriction = lpRelax;
454 }
455 
464 void Joint::Friction(std::string strXml)
465 {
466  CStdXml oXml;
467  oXml.Deserialize(strXml);
468  oXml.FindElement("Root");
469 
471 }
472 
473 
486 {return fltPos;}
487 
495 {return -1;}
496 
497 int Joint::VisualSelectionType() {return JOINT_SELECTION_MODE;}
498 
508 
517 void Joint::Child(RigidBody *lpValue)
518 {
519  m_lpChild = lpValue;
521  m_lpPhysicsMovableItem->Physics_SetChild(lpValue);
522 }
523 
533 
542 void Joint::JointPosition(float fltPos) {m_fltPosition = fltPos;}
543 
553 
562 void Joint::JointVelocity(float fltVel) {m_fltVelocity = fltVel;}
563 
572 float Joint::JointForce() {return m_fltForce;}
573 
582 void Joint::JointForce(float fltForce) {m_fltForce = fltForce;}
583 
584 void Joint::WakeDynamics()
585 {
586  if(m_lpParent)
587  m_lpParent->WakeDynamics();
588 
589  if(m_lpChild)
590  m_lpChild->WakeDynamics();
591 }
592 
603 {}
604 
606 {
608 
609  for(int iIdx=0; iIdx<6; iIdx++)
610  if(m_aryRelaxations[iIdx]) m_aryRelaxations[iIdx]->Initialize();
611 
613 
614 }
615 
617 {
619  UpdateData();
620 }
621 
622 
623 float *Joint::GetDataPointer(const std::string &strDataType)
624 {
625  std::string strType = Std_CheckString(strDataType);
626 
627  float *lpData = NULL;
628 
629  if(strType == "ENABLE")
630  return &m_fltEnabled;
631 
633  {
634  float *lpData = NULL;
635  lpData = m_lpPhysicsMovableItem->Physics_GetDataPointer(strDataType);
636  if(lpData) return lpData;
637  }
638 
639  return BodyPart::GetDataPointer(strDataType);
640 }
641 
642 bool Joint::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
643 {
644  std::string strType = Std_CheckString(strDataType);
645 
646  if(BodyPart::SetData(strType, strValue, false))
647  return true;
648 
649  if(strType == "ENABLELIMITS")
650  {
651  EnableLimits(Std_ToBool(strValue));
652  return true;
653  }
654 
655  if(strType == "SIZE")
656  {
657  Size((float) atof(strValue.c_str()));
658  return true;
659  }
660 
661  if(strType == "RELAXATION1")
662  {
663  Relaxation1(strValue);
664  }
665 
666  if(strType == "RELAXATION2")
667  {
668  Relaxation2(strValue);
669  }
670 
671  if(strType == "RELAXATION3")
672  {
673  Relaxation3(strValue);
674  }
675 
676  if(strType == "RELAXATION4")
677  {
678  Relaxation4(strValue);
679  }
680 
681  if(strType == "RELAXATION5")
682  {
683  Relaxation5(strValue);
684  }
685 
686  if(strType == "RELAXATION6")
687  {
688  Relaxation6(strValue);
689  }
690 
691  if(strType == "FRICTION")
692  {
693  Friction(strValue);
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 Joint::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
704 {
705  BodyPart::QueryProperties(aryProperties);
706 
707  aryProperties.Add(new TypeProperty("Enable", AnimatPropertyType::Boolean, AnimatPropertyDirection::Get));
708 
709  aryProperties.Add(new TypeProperty("EnableLimits", AnimatPropertyType::Boolean, AnimatPropertyDirection::Set));
710  aryProperties.Add(new TypeProperty("Size", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
711  aryProperties.Add(new TypeProperty("Relaxation1", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
712  aryProperties.Add(new TypeProperty("Relaxation2", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
713  aryProperties.Add(new TypeProperty("Relaxation3", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
714  aryProperties.Add(new TypeProperty("Relaxation4", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
715  aryProperties.Add(new TypeProperty("Relaxation5", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
716  aryProperties.Add(new TypeProperty("Relaxation6", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
717  aryProperties.Add(new TypeProperty("Friction", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
718 }
719 
720 void Joint::AddExternalNodeInput(int iTargetDataType, float fltInput) {}
721 
723 {
725 
727  m_lpPhysicsMovableItem->Physics_ResetSimulation();
728 
729  JointPosition(0);
730  JointVelocity(0);
731  JointForce(0);
732 }
733 
735 {
737  m_lpPhysicsMovableItem->Physics_AfterResetSimulation();
738 }
739 
741 {
742  UpdateAbsolutePosition();
743 }
744 
752 {
753  for(int iIdx=0; iIdx<6; iIdx++)
754  if(m_aryRelaxations[iIdx])
755  {
756  delete m_aryRelaxations[iIdx];
757  m_aryRelaxations[iIdx] = NULL;
758  }
759 }
760 
761 void Joint::Load(CStdXml &oXml)
762 {
763  BodyPart::Load(oXml);
764 
765  oXml.IntoElem(); //Into Joint Element
766 
767  if(!m_lpParent)
768  THROW_PARAM_ERROR(Al_Err_lParentNotDefined, Al_Err_strParentNotDefined, "JointID", m_strName);
769 
770  if(!m_lpChild)
771  THROW_PARAM_ERROR(Al_Err_lChildNotDefined, Al_Err_strChildNotDefined, "JointID", m_strName);
772 
773  //Reset the absolute position differently for a joint. It is derived from the child object, not the parent.
774  //AbsolutePosition(m_lpChild->AbsolutePosition() + m_oPosition);
775 
776  EnableLimits(oXml.GetChildBool("EnableLimits", m_bEnableLimits));
777 
778  Size(oXml.GetChildFloat("Size", m_fltSize));
779 
781 
782  if(m_lpFriction) {delete m_lpFriction; m_lpFriction = NULL;}
783 
784  if(oXml.FindChildElement("Relaxation1", false))
785  m_aryRelaxations[0] = LoadConstraintRelaxation(oXml, "Relaxation1");
786 
787  if(oXml.FindChildElement("Relaxation2", false))
788  m_aryRelaxations[1] = LoadConstraintRelaxation(oXml, "Relaxation2");
789 
790  if(oXml.FindChildElement("Relaxation3", false))
791  m_aryRelaxations[2] = LoadConstraintRelaxation(oXml, "Relaxation3");
792 
793  if(oXml.FindChildElement("Relaxation4", false))
794  m_aryRelaxations[3] = LoadConstraintRelaxation(oXml, "Relaxation4");
795 
796  if(oXml.FindChildElement("Relaxation5", false))
797  m_aryRelaxations[4] = LoadConstraintRelaxation(oXml, "Relaxation5");
798 
799  if(oXml.FindChildElement("Relaxation6", false))
800  m_aryRelaxations[5] = LoadConstraintRelaxation(oXml, "Relaxation6");
801 
802  if(oXml.FindChildElement("Friction", false))
804 
805  oXml.OutOfElem(); //OutOf Joint Element
806 }
807 
821 {
822  ConstraintRelaxation *lpConstraintRelaxation = NULL;
823  std::string strModule;
824  std::string strType;
825 
826 try
827 {
828  oXml.FindChildElement(strName);
829  oXml.IntoElem(); //Into Child Element
830  strModule = oXml.GetChildString("ModuleName", "");
831  strType = oXml.GetChildString("Type");
832  oXml.OutOfElem(); //OutOf Child Element
833 
834  lpConstraintRelaxation = dynamic_cast<ConstraintRelaxation *>(m_lpSim->CreateObject(strModule, "ConstraintRelaxation", strType));
835  if(!lpConstraintRelaxation)
836  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "ConstraintRelaxation");
837 
838  lpConstraintRelaxation->SetSystemPointers(m_lpSim, m_lpStructure, m_lpModule, this, true);
839  lpConstraintRelaxation->Load(oXml);
840 
841  return lpConstraintRelaxation;
842 }
843 catch(CStdErrorInfo oError)
844 {
845  if(lpConstraintRelaxation) delete lpConstraintRelaxation;
846  RELAY_ERROR(oError);
847  return NULL;
848 }
849 catch(...)
850 {
851  if(lpConstraintRelaxation) delete lpConstraintRelaxation;
852  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
853  return NULL;
854 }
855 }
856 
857 
871 {
872  ConstraintFriction *lpConstraintFriction = NULL;
873  std::string strModule;
874  std::string strType;
875 
876 try
877 {
878  oXml.FindChildElement("Friction");
879  oXml.IntoElem(); //Into Child Element
880  strModule = oXml.GetChildString("ModuleName", "");
881  strType = oXml.GetChildString("Type");
882  oXml.OutOfElem(); //OutOf Child Element
883 
884  lpConstraintFriction = dynamic_cast<ConstraintFriction *>(m_lpSim->CreateObject(strModule, "ConstraintFriction", strType));
885  if(!lpConstraintFriction)
886  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "ConstraintFriction");
887 
888  lpConstraintFriction->SetSystemPointers(m_lpSim, m_lpStructure, m_lpModule, this, true);
889  lpConstraintFriction->Load(oXml);
890 
891  return lpConstraintFriction;
892 }
893 catch(CStdErrorInfo oError)
894 {
895  if(lpConstraintFriction) delete lpConstraintFriction;
896  RELAY_ERROR(oError);
897  return NULL;
898 }
899 catch(...)
900 {
901  if(lpConstraintFriction) delete lpConstraintFriction;
902  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
903  return NULL;
904 }
905 }
906 
907 
908  } //Environment
909 } //AnimatSim
virtual void Deserialize(std::string &strXml)
Deserializes a string into an xml document.
Definition: StdXml.cpp:162
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Joint.cpp:722
Base class file for all Animat simulation objects.
virtual void Initialize()
Initializes this object.
Definition: BodyPart.cpp:400
Declares the simulation recorder class.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
Root namespace for the base simulation library for AnimatLab.
AnimatSim::Behavior::NeuralModule * m_lpModule
The pointer to this items parentNeuralModule. If this is not relevant for this object then this is NU...
Definition: AnimatBase.h:49
virtual void AfterResetSimulation()
Called after a simulation reset for some objects.
Definition: Joint.cpp:734
virtual ConstraintRelaxation * Relaxation5()
Gets the pointer to the thirdary axis rotation relaxation.
Definition: Joint.cpp:343
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: BodyPart.cpp:393
virtual void UpdateData()
Called to collect any body data for this part.
Definition: BodyPart.cpp:80
Declares the body part class.
virtual float GetLimitRange()
Gets the entire range of movement within the limits. If limits are not enabled then it returns -1...
Definition: Joint.cpp:494
virtual void UpdatePhysicsPosFromGraphics()
Updates the physics position from graphics.
Definition: Joint.cpp:740
virtual float JointForce()
Gets the joint force.
Definition: Joint.cpp:572
virtual bool FindElement(std::string strElementName, bool fThrowError=true)
Finds an element with the specified name.
Definition: StdXml.cpp:179
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
float m_fltEnabled
This is used for reporting the enabled state in a GetDataPointer call.
Definition: Node.h:35
virtual ConstraintRelaxation * Relaxation3()
Gets the pointer to the secondary axis displacement relaxation.
Definition: Joint.cpp:251
Information about the standard error.
Definition: StdErrorInfo.h:19
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
ConstraintFriction * m_lpFriction
The friction for this joint.
Definition: Joint.h:67
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
Declares the key frame class.
virtual float GetPositionWithinLimits(float fltPos)
Gets a position value within the constraint limits.
Definition: Joint.cpp:485
virtual void Initialize()
Initializes this object.
Definition: Joint.cpp:605
Declares the joint class.
Declares the organism class.
virtual float Size()
Gets the size of the graphical representation of this joint.
Definition: Joint.cpp:107
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
virtual ConstraintRelaxation * Relaxation2()
Gets the pointer to the secondary axis displacement relaxation.
Definition: Joint.cpp:205
AnimatSim::Environment::Structure * m_lpStructure
The pointer to this items parent Structure. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:46
virtual void ClearRelaxations()
Clears the relaxation pointers.
Definition: Joint.cpp:751
virtual void CreateJoint()
Creates the joint.
Definition: Joint.cpp:602
Declares a light object.
Joint()
Default constructor.
Definition: Joint.cpp:47
virtual void Resize()
Called when this object has been resized.
Definition: BodyPart.cpp:141
Declares the activated item class.
virtual CStdSerialize * CreateObject(std::string strModule, std::string strClassName, std::string strType, bool bThrowError=true)
Creates an object using a class factory.
Definition: Simulator.cpp:3440
Declares a light manager object.
virtual RigidBody * Child()
Gets the child RigidBody part for this joint.
Definition: Joint.cpp:507
IPhysicsMovableItem * m_lpPhysicsMovableItem
Definition: MovableItem.h:103
Declares the bounding box class.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual void StepSimulation()
Step the simulation for this object.
Definition: AnimatBase.cpp:643
Declares the node class.
virtual int VisualSelectionType()
Gets the visual selection type for this part.
Definition: Joint.cpp:497
virtual float JointVelocity()
Gets the joint velocity.
Definition: Joint.cpp:552
RigidBody * m_lpChild
The child rigid body for this joint.
Definition: Joint.h:44
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
virtual ConstraintRelaxation * LoadConstraintRelaxation(CStdXml &oXml, std::string strName)
Loads constraint relaxation object.
Definition: Joint.cpp:820
bool m_bEnableLimits
If true then any ConstraintLimits for this joint are enabled.
Definition: Joint.h:47
bool Std_ToBool(int iVal)
Converts a value toa bool.
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.
virtual bool GetChildBool(std::string strElementName)
Gets a bool value from the element with the specified name.
Definition: StdXml.cpp:699
float m_fltForce
The current force being applied to the joint by the motor.
Definition: Joint.h:58
virtual ConstraintRelaxation * Relaxation1()
Gets the pointer to the primary axis displacement relaxation.
Definition: Joint.cpp:159
virtual ConstraintFriction * Friction()
Gets the pointer to the friction.
Definition: Joint.cpp:435
virtual ConstraintRelaxation * Relaxation4()
Gets the pointer to the secondary axis rotation relaxation.
Definition: Joint.cpp:297
float m_fltSize
Scales the size of the graphics for this joint.
Definition: Joint.h:61
virtual ConstraintFriction * LoadConstraintFriction(CStdXml &oXml)
Loads constraint Friction object.
Definition: Joint.cpp:870
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void StepSimulation()
Step the simulation for this object.
Definition: Joint.cpp:616
virtual float JointPosition()
Gets the joint position.
Definition: Joint.cpp:532
Declares the data chart manager class.
virtual ConstraintRelaxation * Relaxation6()
Gets the pointer to the thirdary axis rotation relaxation.
Definition: Joint.cpp:389
Declares the rigid body class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
Declares the structure class.
Declares the odor type class.
virtual bool UsesRadians()
Tells whether this joint uses radians or meters for its measurements.
Definition: Joint.cpp:97
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
Declares the odor class.
Declares the simulator class.
virtual bool EnableLimits()
Tells if ConstraintLimits are enabled.
Definition: Joint.cpp:139
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
virtual ~Joint()
Destructor.
Definition: Joint.cpp:68
virtual void AddExternalNodeInput(int iTargetDataType, float fltInput)
Adds an external node input.
Definition: Joint.cpp:720
The base class for all of the basic rigid body type of objects.
Definition: RigidBody.h:66
Declares the receptive field class.
ConstraintRelaxation * m_aryRelaxations[6]
The relaxations for the constraints.
Definition: Joint.h:64
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617