V-ART
action.cpp
Go to the documentation of this file.
1 
5 #include "vart/action.h"
6 #include "vart/jointmover.h"
7 #include "vart/dofmover.h"
8 #include "vart/time.h"
9 #include "vart/dof.h"
10 #include "vart/callback.h"
11 #include "vart/dmmodifier.h"
12 
13 //#include <iostream>
14 using namespace std;
15 
16 list<VART::Action*> VART::Action::activeInstances;
17 float VART::Action::frameFrequency = 0.0f;
18 
19 VART::Action::Action() : callbackPtr(NULL), active(false), duration(0.0f),
20  timeToLive(604800.0f) // a week, in seconds
21 {
22 }
23 
25 {
26  cycle = action.cycle;
27  speed = action.speed;
28  duration = action.duration;
29  timeToLive = action.timeToLive;
30  priority = action.priority;
31  initialTime = action.initialTime;
32  description = action.description;
33  jointMoverList = jointMoverList;
34  return *this;
35 }
36 
38 {
39  list<VART::JointMover*>::iterator iter;
40  // destroy all joint movers
41  for (iter = jointMoverList.begin(); iter != jointMoverList.end(); ++iter)
42  delete *iter;
43 }
44 
45 void VART::Action::Set(float newSpeed, unsigned int newPriority, bool cyclic)
46 {
47  speed = newSpeed;
48  priority = newPriority;
49  cycle = cyclic;
50 }
51 
53  float newDuration, const VART::Interpolator& interpolator)
54 {
55  VART::JointMover* jointMoverPtr = new VART::JointMover(jointPtr, newDuration, interpolator);
56  jointMoverList.push_back(jointMoverPtr);
57  if (newDuration > duration)
58  duration = newDuration;
59  return jointMoverPtr;
60 }
61 
63 {
64  VART::JointMover* jointMoverPtr;
65  jointMoverPtr = AddJointMover( jointPtr, jointMover->GetDuration(), *jointMover->GetInterpolator() );
66  jointMoverPtr->CopyFrom(*jointMover, jointPtr);
67  return jointMoverPtr;
68 }
69 
71 // Note: An action passes parameters to joint movers by setting some JointMover class attributes.
72 // This should be a bit faster than passing some parameter to every joint mover.
73 // Joint movers pass parameters to dof movers using the same scheme.
74 {
75  static VART::Time currentTime;
76  list<VART::JointMover*>::iterator iter;
77 
78  // compute timeDiff
79  currentTime.Set();
80  if (frameFrequency < 0.001)
81  // real time
82  timeDiff = (currentTime - initialTime).AsFloat() * speed;
83  else
84  // fake time
85  timeDiff += (frameFrequency * speed);
86 
87  // Check and enforce timeToLive
88  if (timeDiff > timeToLive)
89  {
90  Deactivate();
91  timeToLive = 604800.0f; // a week, in seconds
92  return;
93  }
94 
95  // deactivate if finished
96  if (timeDiff > duration)
97  {
98  if (cycle)
99  {
100  timeDiff -= duration;
101  initialTime += duration;
102  DeactivateDofMovers();
103  timeToLive -= duration;
104  }
105  else
106  {
107  Deactivate();
108  return;
109  }
110  }
111 
112  // pass modified elapsed time to joint movers
113  // joint movers see time as [0:action_duration] according to action activation and speed
114  VART::JointMover::goalTime = timeDiff;
115  // pass priority to DOF movers
116  VART::DofMover::priority = priority;
117  // pass cycle flag to joint movers
118  VART::DofMover::cycle = cycle;
119 
120  // Tell joint movers to move their joints:
121  for (iter = jointMoverList.begin(); iter != jointMoverList.end(); ++iter)
122  {
123  (*iter)->Move();
124  }
125 }
126 
128 {
129  if (!active)
130  {
131  initialTime.Set();
132 
133  // Add action to list of active instances (using priority order)
134  list<Action*>::iterator iter = activeInstances.begin();
135  bool notFinished = true;
136  while (notFinished && (iter != activeInstances.end()))
137  {
138  // Actions with higher priority must come first in the list so that those
139  // with lower priority will never have the chance to move DOFs. If they did,
140  // transitions between DOF movers of the same action would get ugly.
141  if ((*iter)->priority < priority)
142  {
143  activeInstances.insert(iter,this);
144  notFinished = false;
145  }
146  ++iter;
147  }
148  if (notFinished)
149  activeInstances.push_back(this);
150  active = true;
151  timeDiff = 0.0f;
152  Move(); // ugly fix to prevent lower priority actions from changing target dofs
153  }
154 }
155 
157 {
158  if (active)
159  {
160  list<VART::Action*>::iterator iter = activeInstances.begin();
161 
162  // Remove this instance from list and deactivate all dof movers so that they must be
163  // recomputed if the action is activated again.
164  active = false;
165  while (iter != activeInstances.end())
166  {
167  //~ (*iter)->DeactivateDofMovers();
168  if ((*iter) == this)
169  {
170  DeactivateDofMovers();
171  //~ activeInstances.erase(iter++);
172  activeInstances.erase(iter);
173  iter = activeInstances.end();
174  }
175  else
176  ++iter;
177  }
178  if (callbackPtr)
179  callbackPtr->Activate();
180  }
181 }
182 
183 void VART::Action::Deactivate(float seconds)
184 {
185  timeToLive = seconds;
186 }
187 
189 {
190  list<VART::JointMover*>::iterator iter;
191  for (iter = jointMoverList.begin(); iter != jointMoverList.end(); ++iter)
192  (*iter)->DeactivateDofMovers();
193 }
194 
196 // static method
197 {
198  list<VART::Action*>::iterator iter = activeInstances.begin();
199  list<VART::Action*>::iterator tempIter;
200 
201  // reset dof update priorities -- new draw cycle has begun
203  // move joints
204  while (iter != activeInstances.end())
205  {
206  tempIter = iter;
207  ++iter;
208  // the action could remove itself from the list, so use a private iterator copy
209  (*tempIter)->Move();
210  }
211 
212  return activeInstances.size();
213 }
214 
215 void VART::Action::GetFinalTimes(std::list<float>* resultPtr)
216 {
217  list<VART::JointMover*>::iterator iter;
218 
219  resultPtr->clear();
220  for (iter = jointMoverList.begin(); iter != jointMoverList.end(); ++iter)
221  (*iter)->GetFinalTimes(resultPtr);
222 }
223 
225 {
226  VART::Action* thisCopy;
227  VART::Joint* joint;
228  VART::JointMover* jointMover;
229  std::list<JointMover*>::iterator jointMoverIter;
230 
231  thisCopy = new VART::Action();
232  thisCopy->Action::operator=(*this);
233  thisCopy->jointMoverList.clear();
234 
235  for( jointMoverIter = jointMoverList.begin(); jointMoverIter != jointMoverList.end(); jointMoverIter ++ )
236  {
237  joint = dynamic_cast<VART::Joint*>( targetNode.FindChildByName( (*jointMoverIter)->GetAttachedJoint()->GetDescription() ) );
238  if( joint )
239  {
240  jointMover = thisCopy->AddJointMover( joint, *jointMoverIter );
241  }
242  }
243  return thisCopy;
244 }
245 
247 {
248  // select joint movers that act on given joint
249  list<JointMover*>::iterator jointIter = jointMoverList.begin();
250  for(; jointIter != jointMoverList.end(); ++jointIter)
251  (*jointIter)->ModifyDofMovers(modifier);
252 }
253 
254 ostream& VART::operator<<(ostream& output, const VART::Action& action)
255 {
256  list<JointMover*>::const_iterator iter = action.jointMoverList.begin();
257 
258  output << "Action '" << action.description << "'. Speed: " << action.speed << ". ";
259  if (action.cycle)
260  output << "Cyclic.\n";
261  else
262  output << "Not cyclic.\n";
263  while (iter != action.jointMoverList.end())
264  {
265  output << (**iter);
266  ++iter;
267  }
268  return output;
269 }
Time initialTime
Definition: action.h:133
VART::Action & operator=(const VART::Action &action)
Copy action data, except the active atribute (that remains unmodified). Used in Action::Copy() method...
Definition: action.cpp:24
Base class for objects that compose a scene graph.
Definition: scenenode.h:25
void ModifyDofMovers(DMModifier &mod)
Modifies dof movers.
Definition: action.cpp:246
Header file for V-ART class "Dof".
Action()
Creates an unitialized action.
Definition: action.cpp:19
Representation of joints.
Definition: joint.h:34
void Set(float newSpeed, unsigned int newPriority, bool cyclic)
Sets speed, priority and cycle attibutes.
Definition: action.cpp:45
static bool cycle
Indicates whether the current action is cyclic.
Definition: dofmover.h:76
Header file for V-ART class "Time".
A coordinated movement of joints in an articulated body.
Definition: action.h:29
static float frameFrequency
Fake animation time.
Definition: action.h:108
static float goalTime
Definition: jointmover.h:92
static void ClearPriorities()
Resets priorities of all DOF instances.
Definition: dof.cpp:418
bool cycle
Definition: action.h:124
void GetFinalTimes(std::list< float > *resultPtr)
Returns a ordered list with all different final times for every dof mover.
Definition: action.cpp:215
std::ostream & operator<<(std::ostream &output, const Joint::DofID &dofId)
Header file for V-ART class "CallBack".
void Set()
Set to current time.
Definition: time.cpp:49
std::string description
Textual description.
Definition: action.h:111
unsigned int priority
seconds to auto deactivation
Definition: action.h:131
Header file for V-ART class "JointMover".
void CopyFrom(VART::JointMover &jointMover, VART::Joint *targetJoint)
Copy jointMover data to this jointMover, setting its jointPtr atribute to the targetJoint.
Definition: jointmover.cpp:130
float speed
Definition: action.h:128
void DeactivateDofMovers()
Deactivates DOF movers in every joint mover.
Definition: action.cpp:188
float GetDuration()
Return the total movement duration (in seconds).
Definition: jointmover.h:61
Header file for V-ART class "Action".
An object that modifies noisy dof movers.
Definition: dmmodifier.h:15
static std::list< Action * > activeInstances
Definition: action.h:135
Interpolator representation.
Definition: interpolator.h:17
Controllers for joint movement.
Definition: jointmover.h:22
void Move()
Animate joints.
Definition: action.cpp:70
static unsigned int MoveAllActive()
Moves all active actions.
Definition: action.cpp:195
Header file for V-ART class "DofMover".
void Activate()
Activate action.
Definition: action.cpp:127
void Deactivate()
Deactivate action.
Definition: action.cpp:156
std::list< JointMover * > jointMoverList
Definition: action.h:132
VART::Action * Copy(VART::SceneNode &targetNode)
Returns a copy of an action, aplied to an targetNode sceneNode.
Definition: action.cpp:224
SceneNode * FindChildByName(const std::string &name) const
Recusively searches its children for a given name.
Definition: scenenode.cpp:96
JointMover * AddJointMover(Joint *jointPtr, float newDuration, const Interpolator &interpolator)
Adds a joint mover to the action.
Definition: action.cpp:52
static unsigned int priority
Priority of active action.
Definition: dofmover.h:80
const Interpolator * GetInterpolator()
Returns a pointer to the associated interpolator. NULL is returned if there are no interpolator assoc...
Definition: jointmover.h:68
float duration
Definition: action.h:129
float timeToLive
Definition: action.h:130
Elapsed time representation.
Definition: time.h:15