V-ART
xmljointaction.cpp
Go to the documentation of this file.
1 
5 #include "vart/xmljointaction.h"
6 #include "vart/joint.h"
7 #include "vart/noisydofmover.h"
8 #include "vart/jointmover.h"
10 #include "vart/sineinterpolator.h"
13 #include <xercesc/dom/DOMNodeList.hpp>
14 #include <xercesc/dom/DOMElement.hpp>
15 
16 
17 //#include <iostream>
18 using namespace std;
19 
21 {
22  cout << "XmlJointAction::XmlJointAction()" << endl;
23 }
24 
26  XERCES_CPP_NAMESPACE::DOMNode* dofMovItemPtr)
27 // Called by XmlJointAction::CreateJointMover to create a dof mover from a XML entry.
28 {
29  using namespace XERCES_CPP_NAMESPACE;
30 
31  DOMNamedNodeMap* dofAttrs = dofMovItemPtr->getAttributes();
32  // read initial time
33  float inTime;
34  GetAttributeValue(dofAttrs, "initialTime", &inTime);
35  // read final time
36  float fTime;
37  GetAttributeValue(dofAttrs, "finalTime", &fTime);
38  // read final position
39  float fPos;
40  GetAttributeValue(dofAttrs, "finalPosition", &fPos);
41  // Read value for dofID
42  string dofIDString;
43  GetAttributeValue(dofAttrs, "dofID", &dofIDString);
44  VART::Joint::DofID dofID;
45  if ((dofIDString == "FLEX") || (dofIDString == "FLEXION"))
46  dofID = VART::Joint::FLEXION;
47  else
48  {
49  if ((dofIDString == "ADDUCT") || (dofIDString == "ADDUCTION"))
50  dofID = VART::Joint::ADDUCTION;
51  else
52  {
53  if (dofIDString == "TWIST")
54  dofID = VART::Joint::TWIST;
55  else
56  {
57  cerr << "XmlAction::ParseDofMovement: Unknown DOF ID (" << dofIDString << ")."
58  << endl;
59  exit (1);
60  }
61  }
62  }
63 
64  // Treat child elements of dof movers
65  DOMNodeList* dofMovElements = dofMovItemPtr->getChildNodes();
66  float noiseAmplitude;
67  float noiseWaveLenght;
68  bool foundNoise = false; // any noise elements found inside the dof mover?
69  bool foundPositionalError = false; // any positional error elements found inside the dof mover?
70  // for each child node of the dof mover (may be "noise" or "error"):
71  for (unsigned int count = 0; count < dofMovElements->getLength(); ++count)
72  {
73  string nodeName(TempCString(dofMovElements->item(count)->getNodeName()));
74  if (nodeName == "noise")
75  {
76  DOMNamedNodeMap* noiseAttrs = dofMovElements->item(count)->getAttributes();
77  GetAttributeValue(noiseAttrs, "amplitude", &noiseAmplitude);
78  GetAttributeValue(noiseAttrs, "length", &noiseWaveLenght);
79  foundNoise = true;
80  }
81  else
82  if (nodeName == "error")
83  {
84  DOMNamedNodeMap* errorAttrs = dofMovElements->item(count)->getAttributes();
85 
86  float overshoot;
87  GetAttributeValue(errorAttrs, "overshoot", &overshoot);
88  NoisyDofMover::SetDefaultOvershoot(overshoot);
89 
90  float offset;
91  GetAttributeValue(errorAttrs, "offset", &offset);
92  NoisyDofMover::SetDefaultOffset(offset);
93 
94  float peakTime;
95  GetAttributeValue(errorAttrs, "peak_time", &peakTime);
96  NoisyDofMover::SetDefaultPeakTime(peakTime);
97 
98  foundPositionalError = true;
99  }
100  }
101 
102  // Ready to create a dof mover
103  if (foundPositionalError || foundNoise)
104  {
105  VART::NoisyDofMover* noisyDofMoverPtr;
106 
107  noisyDofMoverPtr = jointMPtr->AddNoisyDofMover(dofID, inTime, fTime, fPos);
108  if (foundNoise)
109  noisyDofMoverPtr->SetNoise(noiseAmplitude, noiseWaveLenght);
110  }
111  else
112  jointMPtr->AddDofMover(dofID, inTime, fTime, fPos);
113 }
114 
116  XERCES_CPP_NAMESPACE::DOMNode* xmlNodePtr)
117 {
118  using namespace XERCES_CPP_NAMESPACE;
119 
120  static VART::LinearInterpolator linearInterpolator; // common interpolator for all joint movers
121  static VART::SineInterpolator sineInterpolator; // common interpolator for all joint movers
122 
123  DOMNamedNodeMap* jmAttrs = xmlNodePtr->getAttributes();
124  // Read movement duration
125  float moverDuration;
126  GetAttributeValue(jmAttrs, "duration", &moverDuration);
127  // Read target (joint) name
128  string jointName;
129  GetAttributeValue(jmAttrs, "joint_name", &jointName);
130  // Get a pointer to target joint
131  DescriptionLocator locator(jointName); // FixMe: Can we relay on description uniqueness?
132  sNode.LocateBreadthFirst(&locator);
133  Joint* targetJointPtr = const_cast<Joint*>(dynamic_cast<const Joint*>(locator.LocatedNode()));
134  if (targetJointPtr)
135  {
136  JointMover* jointMoverPtr = AddJointMover(targetJointPtr, moverDuration, sineInterpolator);
137  DOMNodeList* childrenList = xmlNodePtr->getChildNodes();
138  // For each child of a joint movement (may be interpolator or dof movement):
139  for (unsigned int i=0; i < childrenList->getLength(); ++i)
140  {
141  string name(TempCString(childrenList->item(i)->getNodeName()));
142  if (name == "interpolation")
143  {
144  DOMNamedNodeMap* itpAttrs = childrenList->item(i)->getAttributes();
145  // Read interpolation type
146  string itpType;
147  GetAttributeValue(itpAttrs, "type", &itpType);
148  // Default is a SineInterpolator. Change interpolator if different from default.
149  if (itpType != "ease-in_ease-out")
150  {
151  if (itpType == "linear")
152  jointMoverPtr->SetInterpolator(linearInterpolator);
153  else
154  {
155  if (itpType == "range_sine")
156  {
157  // FixMe: Why isn't the RangeSineInterpolator static like others?
158  Interpolator* interpolatorPtr = new RangeSineInterpolator;
159  jointMoverPtr->SetInterpolator(*interpolatorPtr);
160  }
161  else
162  cerr << "Error: Unknown interpolator type: '" << itpType
163  << "'. Using: 'ease-in_ease-out'." << endl;
164  }
165  }
166  }
167  else // child (of joint mover) is a dof movement
168  {
169  if (name == "dof_movement")
170  ParseDofMovement(jointMoverPtr, childrenList->item(i));
171  }
172  }
173  }
174 }
175 
176 bool VART::XmlJointAction::LoadFromFile(const std::string& fileName, const VART::SceneNode& sNode)
177 {
178  using namespace XERCES_CPP_NAMESPACE;
179 
180  //~ cout << "XmlJointAction loading from " << fileName << endl;
181  if(ParseFile(fileName))
182  { // XML parser sucessfull
183  // Get action attributes (getDocumentElement returns a pointer to the root element)
184  DOMNamedNodeMap* actionAttrs = documentPtr->getDocumentElement()->getAttributes();
185  // Read action name
186  bool result = GetAttributeValue(actionAttrs, "action_name", &description);
187  // Read action speed
188  result |= GetAttributeValue(actionAttrs, "speed", &speedModifier);
189  // Read cycle flag (boolean)
190  result |= GetAttributeValue(actionAttrs, "cycle", &cyclic);
191  // Load joint movers
192  DOMNodeList* jmList = documentPtr->getDocumentElement()->getChildNodes();
193  // For each joint movement:
194  for (unsigned int jm = 0; jm < jmList->getLength(); ++jm)
195  {
196  string name(TempCString(jmList->item(jm)->getNodeName()));
197  if (name == "joint_movement")
198  CreateJointMover(sNode, jmList->item(jm));
199  }
200  Terminate();
201  return result;
202  }
203  else
204  { //the parser has failled!
205  Terminate();
206  return false;
207  }
208 }
209 
Locates a SceneNode by its description.
Base class for objects that compose a scene graph.
Definition: scenenode.h:25
Representation of joints.
Definition: joint.h:34
Linear interpolation.
Smooth (sine function) interpolator.
Header file for V-ART class "SineInterpolator".
Header file for V-ART class "JointMover".
void SetNoise(float newNoiseAmplitude, float newNoiseWaveLenght)
Sets the noise attributes.
Smooth (sine function) interpolator with range.
virtual void LocateBreadthFirst(SNLocator *locatorPtr) const
Seaches for a particular scene node (breadth first)
Definition: scenenode.cpp:226
Header file for V-ART class "Joint".
Header file for V-ART class "XmlJointAction".
Interpolator representation.
Definition: interpolator.h:17
Controllers for joint movement.
Definition: jointmover.h:22
void ParseDofMovement(JointMover *jointMPtr, XERCES_CPP_NAMESPACE::DOMNode *dofMovItemPtr)
Noisy Controller for DOF movement.
Definition: noisydofmover.h:17
Header file for V-ART class "NoisyDofMover".
Class for auto conversion from Xerces strings to C strings.
Definition: xmlbase.h:47
void CreateJointMover(const VART::SceneNode &sNode, XERCES_CPP_NAMESPACE::DOMNode *xmlNodePtr)
Header file for V-ART class "LinearInterpolator".
Header file for V-ART class "SineInterpolator".
void SetInterpolator(const Interpolator &interp)
Changes the associated interpolator.
Definition: jointmover.h:64
bool LoadFromFile(const std::string &fileName, const SceneNode &sNode)
Loads JointAction from a XML file.
void AddDofMover(Joint::DofID dof, float iniTime, float finTime, float finPos)
Creates a DofMover.
Definition: jointmover.cpp:59
NoisyDofMover * AddNoisyDofMover(Joint::DofID dof, float iniTime, float finTime, float finPos)
Creates a noisy DofMover.
Definition: jointmover.cpp:72
const SceneNode * LocatedNode() const
Returns the located node.
Definition: snlocator.h:72