V-ART
jointaction.cpp
Go to the documentation of this file.
1 
5 #include "vart/dofmover.h"
6 #include "vart/jointaction.h"
7 #include "vart/jointmover.h"
8 #include "vart/dof.h"
9 #include "vart/dmmodifier.h"
10 
11 #include <iostream>
12 using namespace std;
13 
14 // class JointAction::Initializer ==================================================================
15 
17 {
18  // There can be only one!
19  static unsigned char counter = 0;
20  if (counter == 0)
21  {
22  // Add itself to the list of initializers
23  BaseAction::initializers.push_back(this);
24  counter = 1;
25  }
26  else
27  cerr << "\aCreate just one instance of JointAction::Initializer!" << endl;
28  //FixMe: - throw an exception instead of error message
29 }
30 
32 {
34 }
35 
36 // class JointAction ===============================================================================
37 
39 {
40  // Create an initializer when the first joint action is constructed
41  static Initializer jointActionInitializer;
42 }
43 
44 // virtual (replaces BaseAction)
46 // Joint actions are inserted so that higher priority actions come first in the active instances list
47 {
48  if (!active)
49  {
50  active = true;
51  lastPositionIndex = 0;
52  // Store current time so that we had data do compute on next Move()
53  lastUpdateTime.Set();
54 
55  // Add action to list of active instances
56  AddToActiveInstancesList();
57  }
58 }
59 
61 {
62  list<BaseAction*>::iterator iter = activeInstances.begin();
63  bool positionNotFound = true;
64  while (positionNotFound && (iter != activeInstances.end()))
65  {
66  JointAction* jaPtr = dynamic_cast<JointAction*>(*iter);
67  // if action is of type JointAction
68  if (jaPtr)
69  {
70  if (jaPtr->priority > priority)
71  ++iter;
72  else
73  positionNotFound = false;
74  }
75  else
76  positionNotFound = false;
77  }
78  if (positionNotFound)
79  // end of list reached
80  activeInstances.push_back(this);
81  else
82  activeInstances.insert(iter, this);
83  //~ iter = activeInstances.begin();
84  //~ cout << "activeInstances: ";
85  //~ for (; iter != activeInstances.end(); ++iter)
86  //~ cout << (*iter)->description << " ";
87  //~ cout << "\n";
88 }
89 
91 {
92  list<VART::JointMover*>::iterator iter = jointMoverList.begin();
93  for (; iter != jointMoverList.end(); ++iter)
94  (*iter)->DeactivateDofMovers();
95 }
96 
97 void VART::JointAction::GetFinalTimes(std::list<float>* resultPtr)
98 {
99  resultPtr->clear();
100  list<VART::JointMover*>::iterator iter = jointMoverList.begin();
101  for (; iter != jointMoverList.end(); ++iter)
102  (*iter)->GetFinalTimes(resultPtr);
103 }
104 
105 void VART::JointAction::GetJoints(std::list<Joint*>* listPtr)
106 {
107  listPtr->clear();
108  list<VART::JointMover*>::iterator iter = jointMoverList.begin();
109  for (; iter != jointMoverList.end(); ++iter)
110  listPtr->push_back(const_cast<Joint*>((*iter)->GetAttachedJoint()));
111 }
112 
114  float newDuration,
115  const Interpolator& interpolator)
116 {
117  JointMover* jointMoverPtr = new JointMover(jointPtr, newDuration, interpolator);
118  jointMoverList.push_back(jointMoverPtr);
119  if (newDuration > duration)
120  duration = newDuration;
121  return jointMoverPtr;
122 }
123 
124 // virtual
126 {
127  bool cycled;
128  ComputePositionIndex(&cycled);
129  if (cycled)
130  DeactivateDofMovers(); // force them to restart from where they currently are
131 
132  // pass modified elapsed time to joint movers
133  // joint movers see time as [0:action_duration] according to action activation and speed
134  // FixMe: Perhaps the multiplication should be taken away. It was kept when porting the
135  // old Action class to this new JointAction.
136  VART::JointMover::goalTime = positionIndex * duration;
137 
138  // pass priority to DOF movers
139  VART::DofMover::priority = priority;
140  // pass cycle flag to joint movers
141  VART::DofMover::cycle = cyclic;
142 
143  // Tell joint movers to move their joints:
144  list<VART::JointMover*>::iterator iter = jointMoverList.begin();
145  for (; iter != jointMoverList.end(); ++iter)
146  (*iter)->Move();
147 }
148 
150 {
151  // select joint movers that act on given joint
152  list<JointMover*>::iterator jointIter = jointMoverList.begin();
153  for(; jointIter != jointMoverList.end(); ++jointIter)
154  (*jointIter)->ModifyDofMovers(modifier);
155 }
156 
158 {
159  list<VART::JointMover*>::iterator iter = jointMoverList.begin();
160  for (; iter != jointMoverList.end(); ++iter)
161  {
162  if (jointPtr == ((*iter)->GetAttachedJoint()))
163  return (*iter)->GetDofMover(dof);
164  }
165  return NULL;
166 }
167 
168 ostream& VART::operator<<(ostream& output, const VART::JointAction& action)
169 {
170  list<JointMover*>::const_iterator iter = action.jointMoverList.begin();
171 
172  output << "<?xml version=\"1.0\"?>\n"
173  << "<!DOCTYPE action SYSTEM \"vartAnimation.dtd\">\n"
174  << "<action action_name=\"" << action.description << "\" speed=\""
175  << action.speedModifier << "\" cycle=\"" << boolalpha << action.cyclic << "\">\n";
176  // for each joint mover
177  while (iter != action.jointMoverList.end())
178  {
179  output << (**iter);
180  ++iter;
181  }
182  output << "</action>\n";
183  return output;
184 }
185 
Header file for V-ART class "JointAction".
Header file for V-ART class "Dof".
Representation of joints.
Definition: joint.h:34
static bool cycle
Indicates whether the current action is cyclic.
Definition: dofmover.h:76
void ModifyDofMovers(DMModifier &mod)
Modifies dof movers.
JointMover * AddJointMover(Joint *jointPtr, float newDuration, const Interpolator &interpolator)
Adds a joint mover to the action.
static float goalTime
Definition: jointmover.h:92
static void ClearPriorities()
Resets priorities of all DOF instances.
Definition: dof.cpp:418
std::ostream & operator<<(std::ostream &output, const Joint::DofID &dofId)
void GetJoints(std::list< Joint * > *listPtr)
Returns a list of all target joints.
std::list< JointMover * > jointMoverList
Definition: jointaction.h:87
float speedModifier
Speed modifier.
Definition: baseaction.h:106
Header file for V-ART class "JointMover".
An object that modifies noisy dof movers.
Definition: dmmodifier.h:15
void AddToActiveInstancesList()
Adds itself to list of active instances, in priority order.
Definition: jointaction.cpp:60
void DeactivateDofMovers()
Definition: jointaction.cpp:90
A coordinated movement of joints in an articulated body.
Definition: jointaction.h:26
Controller for DOF movement.
Definition: dofmover.h:24
Interpolator representation.
Definition: interpolator.h:17
Controllers for joint movement.
Definition: jointmover.h:22
void GetFinalTimes(std::list< float > *resultPtr)
Returns a ordered list with all different final times for every dof mover.
Definition: jointaction.cpp:97
Header file for V-ART class "DofMover".
unsigned int priority
Definition: jointaction.h:88
DofMover * SearchDofMover(Joint *jointPtr, Joint::DofID dof)
Returns a dof mover.
virtual void Activate()
Activates a JointAction.
Definition: jointaction.cpp:45
static unsigned int priority
Priority of active action.
Definition: dofmover.h:80
bool cyclic
Whether the action restarts upon termination.
Definition: baseaction.h:96
std::string description
Definition: baseaction.h:54
virtual void Move()
Make the action update its targets.