Atoms Crowd  7.0.0
AnimationClip.impl.h
1 // ===========================================================================
2 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
3 //
4 // Use of this software is subject to the terms of the Toolchefs license
5 // agreement provided at the time of installation or download, or which
6 // otherwise accompanies this software in either electronic or hard copy form.
7 // ===========================================================================
8 
9 namespace Atoms
10 {
11  /***************************
12  JointClipData
13  ***************************/
14  const std::string& JointClipData::name() const
15  {
16  return m_name;
17  }
18 
19  void JointClipData::setName(const std::string& name)
20  {
21  m_name = name;
22  }
23 
24  void JointClipData::setTranslation(const AtomsCore::Vector3 &translation, const unsigned int frame)
25  {
26  if (m_translation.size() > frame)
27  m_translation[frame].setValue(translation);
28  }
29 
30  void JointClipData::setRotation(const AtomsCore::Quaternion &rotation, const unsigned int frame)
31  {
32  if (m_rotation.size() > frame)
33  m_rotation[frame] = rotation;
34  }
35 
36  void JointClipData::setScale(const AtomsCore::Vector3 &scale, const unsigned int frame)
37  {
38  if (m_scale.size() > frame)
39  m_scale[frame].setValue(scale);
40  }
41 
42  const AtomsCore::Vector3& JointClipData::getTranslation(const unsigned int frame) const
43  {
44  return frame > m_numberOfFrames - 1 ? m_translation[m_numberOfFrames - 1] : m_translation[frame];
45  }
46 
47  const AtomsCore::Quaternion& JointClipData::getRotation(const unsigned int frame) const
48  {
49  return frame > m_numberOfFrames - 1 ? m_rotation[m_numberOfFrames - 1] : m_rotation[frame];
50  }
51 
52  const AtomsCore::Vector3& JointClipData::getScale(const unsigned int frame) const
53  {
54  return frame > m_numberOfFrames - 1 ? m_scale[m_numberOfFrames - 1] : m_scale[frame];
55  }
56 
57  unsigned int JointClipData::getArrayMetadataType(const std::string &name) const
58  {
59  return m_metadata.at(name)->typeId();
60  }
61 
62  template <class T> inline T JointClipData::getArrayMetadataValueAtFrame(const std::string &name, const unsigned int frame) const
63  {
64  auto metaIt = m_metadata.find(name);
65  if (metaIt == m_metadata.end())
66  return T();
67 
68  AtomsPtr<AtomsCore::TypedArrayMetadata<T>> vec = std::static_pointer_cast<AtomsCore::TypedArrayMetadata<T >>(metaIt->second);
69  if (!vec)
70  return T();
71  return vec->getBasicTypeValue(frame > m_numberOfFrames - 1 ? m_numberOfFrames - 1 : frame);
72  }
73 
74  template <class T>
75  T JointClipData::getBlendedArrayMetadataValue(const std::string& name, const unsigned int initialFrame, const unsigned int finalFrame, const double weight) const
76  {
77  auto metaIt = m_metadata.find(name);
78  if (metaIt == m_metadata.end())
79  return T();
80 
81  if (initialFrame > m_numberOfFrames - 1)
82  {
83  //TODO - check this at
84  return std::static_pointer_cast<AtomsCore::TypedArrayMetadata<T>>(metaIt->second)->getBasicTypeValue(m_numberOfFrames - 1);
85  }
86 
87  if (weight < 0.0001)
88  {
89  return std::static_pointer_cast<AtomsCore::TypedArrayMetadata<T>>(metaIt->second)->getBasicTypeValue(initialFrame);
90  }
91 
92  unsigned int fframe = finalFrame > m_numberOfFrames - 1 ? m_numberOfFrames - 1 : finalFrame;
93  if (weight > 0.9999)
94  {
95  return std::static_pointer_cast<AtomsCore::TypedArrayMetadata<T>>(metaIt->second)->getBasicTypeValue(fframe);
96  }
97 
98  return std::static_pointer_cast<AtomsCore::TypedArrayMetadata<T>>(metaIt->second)->getBlendedBasicTypeValue(initialFrame, fframe, weight);
99  }
100 
101  AtomsPtr<AtomsCore::Metadata> JointClipData::getArrayMetadataAtFrame(const std::string &name, const unsigned int frame) const
102  {
103  auto metaIt = m_metadata.find(name);
104  if (metaIt != m_metadata.cend())
105  return metaIt->second->getBasicTypeMetadata(frame > m_numberOfFrames - 1 ? m_numberOfFrames - 1 : frame);
106  return nullptr;
107  }
108 
109  template <class T> void JointClipData::setArrayMetadataValue(const std::string &name, const unsigned int frame, T value)
110  {
111  AtomsPtr<AtomsCore::TypedArrayMetadata<T>> vec = std::static_pointer_cast<AtomsCore::TypedArrayMetadata<T>>( m_metadata[name] );
112  vec->get()[frame] = value;
113  }
114 
115  std::vector<std::string> JointClipData::getArrayMetadataNames() const
116  {
117  //return m_metadataNames;
118  std::vector<std::string> metadataNames;
119  metadataNames.reserve(m_metadata.size());
120  for (auto it = m_metadata.cbegin(); it != m_metadata.cend(); it++)
121  metadataNames.push_back(it->first);
122  return metadataNames;
123  }
124 
125  bool JointClipData::hasArrayMetadata(const std::string &name) const
126  {
127  return m_metadata.find(name) != m_metadata.cend();
128  }
129 
130  void JointClipData::deleteArrayMetadata(const std::string& name)
131  {
132  auto it = m_metadata.find(name);
133  if (it != m_metadata.end())
134  m_metadata.erase(it);
135  }
136 
137  template<typename T, typename V> inline void JointClipData::getArrayMetadataValues(const std::string& name, std::vector<V>& values)
138  {
139  if (m_metadata.find(name) == m_metadata.end())
140  return;
141  AtomsPtr<AtomsCore::TypedArrayMetadata<T>> vec = std::static_pointer_cast<AtomsCore::TypedArrayMetadata<T>>(m_metadata[name]);
142  if (!vec)
143  return;
144  values = vec->get();
145  }
146 
147  template<class T> void JointClipData::addArrayMetadata(const std::string &name, const bool force)
148  {
149  if (m_metadata.find(name) == m_metadata.end() || force)
150  {
151  AtomsPtr<AtomsCore::Metadata> data = (AtomsCore::MetadataFactory::instance().createMetadata(T::staticTypeStr()));
152 
153  AtomsPtr<T> d = std::static_pointer_cast<T>(data);
154  d->get().resize(static_cast<unsigned int>(m_numberOfFrames));
155 
156  m_metadata[name] = d;
157 
158  // m_metadataNames.push_back(name);
159  }
160  }
161 
162  void JointClipData::addArrayMetadata(const std::string &name, AtomsPtr<AtomsCore::BaseTypedArrayMetadata>& data)
163  {
164  m_metadata[name] = data;
165  }
166 
167  AtomsPtr<AtomsCore::BaseTypedArrayMetadata> JointClipData::getArrayMetadata(const std::string& name)
168  {
169  auto it = m_metadata.find(name);
170  return (it != m_metadata.end()) ? it->second : AtomsPtr<AtomsCore::BaseTypedArrayMetadata>();
171  }
172 
173  std::vector<std::string> JointClipData::getStaticMetadataNames() const
174  {
175  return m_staticMetadata.getKeys();
176  }
177 
178  void JointClipData::setStaticMetadata(const std::string &name, AtomsCore::Metadata &metadata)
179  {
180  m_staticMetadata.addEntry(name, &metadata);
181  }
182 
183  AtomsPtr<AtomsCore::Metadata> JointClipData::getStaticMetadata(const std::string &name)
184  {
185  return m_staticMetadata.getEntry(name);
186  }
187 
188  AtomsPtr<const AtomsCore::Metadata> JointClipData::getStaticMetadata(const std::string &name) const
189  {
190  return m_staticMetadata.getEntry(name);
191  }
192 
193  AtomsCore::MapMetadata& JointClipData::staticMetadata()
194  {
195  return m_staticMetadata;
196  }
197 
198  const AtomsCore::MapMetadata& JointClipData::staticMetadata() const
199  {
200  return m_staticMetadata;
201  }
202 
203  std::map<std::string, AtomsPtr<AtomsCore::BaseTypedArrayMetadata>>& JointClipData::metadata()
204  {
205  return m_metadata;
206  }
207 
208  const std::map<std::string, AtomsPtr<AtomsCore::BaseTypedArrayMetadata>>& JointClipData::metadata() const
209  {
210  return m_metadata;
211  }
212 
213  bool JointClipData::isTransformAnimated() const
214  {
215  return m_isTransformAnimated;
216  }
217 
218  inline const std::vector<std::string>& JointClipData::animatedMetadata() const
219  {
220  return m_animatedMetadata;
221  }
222 
223  /***************************
224  AnimationClip
225  ***************************/
227  {
228  return static_cast<int>(m_joints.size());
229  }
230 
232  {
233  return m_numberOfFrames;
234  }
235 
236  double AnimationClip::fps() const
237  {
238  return m_fps;
239  }
240 
241  void AnimationClip::setFps(double value)
242  {
243  m_fps = value;
244  }
245 
246  unsigned int AnimationClip::mapTimeToFrame(const double time, double &reminder) const
247  {
248  double f = time * m_fps;
249  unsigned int ret = static_cast<unsigned int>(f);
250  reminder = f - ret;
251  return ret;
252  }
253 
254  void AnimationClip::setJointName(const unsigned int index, const std::string &jointName)
255  {
256  m_jointIndex[jointName] = index;
257  m_joints[index].setName(jointName);
258  }
259 
260  int AnimationClip::jointId(const std::string& jointName) const
261  {
262  auto it = m_jointIndex.find(jointName);
263  return it != m_jointIndex.end() ? it->second : -1;
264  }
265 
266  inline JointClipData& AnimationClip::operator[](unsigned short index)
267  {
268  return m_joints[index];
269  }
270 
271  inline const JointClipData& AnimationClip::operator[](unsigned short index) const
272  {
273  return m_joints[index];
274  }
275 
276  JointClipData& AnimationClip::at(unsigned short index)
277  {
278  return m_joints.at(index);
279  }
280 
281  const JointClipData& AnimationClip::at(unsigned short index) const
282  {
283  return m_joints.at(index);
284  }
285 
287  {
288  return m_metadata;
289  }
290 
292  {
293  return m_metadata;
294  }
295 
297  {
298  m_direction = direction;
299  }
300 
302  {
303  return m_direction;
304  }
305 
306  void AnimationClip::setDirectionType(unsigned int directionType)
307  {
308  m_directionType = directionType;
309  }
310 
311  unsigned int AnimationClip::directionType() const
312  {
313  return m_directionType;
314  }
315 
316  void AnimationClip::setDirectionFromJoints(unsigned int fromJoint, unsigned int toJoint)
317  {
318  m_directionFromJoints.first = fromJoint;
319  m_directionFromJoints.second = toJoint;
320  }
321 
322 
323  const std::pair<unsigned int, unsigned int>& AnimationClip::directionFromJoints() const
324  {
325  return m_directionFromJoints;
326  }
327 
329  {
330  m_isFootLockDisabled = value;
331  }
332 
334  {
335  return m_isFootLockDisabled;
336  }
337 
338  const std::vector<int>& AnimationClip::transitionFrames() const
339  {
340  return m_transitionFrames;
341  }
342 
343  unsigned int AnimationClip::additiveMode() const
344  {
345  return m_additiveMode;
346  }
347 
348  const std::string & AnimationClip::additiveReferenceClip() const
349  {
350  return m_additiveReferenceClip;
351  }
352 
353 }
unsigned int directionType() const
Gets the clip direction type.
Definition: AnimationClip.impl.h:311
AtomsCore::MapMetadata & metadata()
Gets metadata map.
Definition: AnimationClip.impl.h:286
int numFrames() const
Gets the number of frames.
Definition: AnimationClip.impl.h:231
int numJoints() const
Gets the number of joints.
Definition: AnimationClip.impl.h:226
const std::pair< unsigned int, unsigned int > & directionFromJoints() const
Gets the two joint ids used for evaluating the direction when the direction type is set to kJoints.
Definition: AnimationClip.impl.h:323
AtomsCore::Vector3 direction() const
Gets the clip direction.
Definition: AnimationClip.impl.h:301
JointClipData & at(unsigned short index)
Gets joint clip data.
Definition: AnimationClip.impl.h:276
unsigned int additiveMode() const
Gets the clip additive mode.
Definition: AnimationClip.impl.h:343
int jointId(const std::string &jointName) const
Get joint index from joint name.
Definition: AnimationClip.impl.h:260
const std::vector< int > & transitionFrames() const
Gets the transition frames.
Definition: AnimationClip.impl.h:338
const std::string & additiveReferenceClip() const
Gets the clip additive mode.
Definition: AnimationClip.impl.h:348
bool isFootLockDisabled() const
Returns false if the the foot lock is disabled.
Definition: AnimationClip.impl.h:333
unsigned int mapTimeToFrame(const double time, double &reminder) const
Maps time to frame.
Definition: AnimationClip.impl.h:246
void setDirectionFromJoints(unsigned int fromJoint, unsigned int toJoint)
Sets the two joint ids used for evaluating the direction when the direction type is set to kJoints.
Definition: AnimationClip.impl.h:316
void setFps(double value)
Sets the frame per second.
Definition: AnimationClip.impl.h:241
void setDirection(const AtomsCore::Vector3 &direction)
Sets the clip direction.
Definition: AnimationClip.impl.h:296
JointClipData & operator[](unsigned short index)
Gets joint clip data.
Definition: AnimationClip.impl.h:266
void setJointName(const unsigned int index, const std::string &jointName)
Sets the joint name.
Definition: AnimationClip.impl.h:254
void setDirectionType(unsigned int directionType)
Sets the clip direction type.
Definition: AnimationClip.impl.h:306
double fps() const
Gets the frame per second.
Definition: AnimationClip.impl.h:236
void disableFootLock(bool value)
Sets the clip foot lock state.
Definition: AnimationClip.impl.h:328
Joint Clip data.
Definition: AnimationClip.h:38
void setRotation(const AtomsCore::Quaternion &rotation, const unsigned int frame)
Sets rotation at frame.
Definition: AnimationClip.impl.h:30
const AtomsCore::Vector3 & getTranslation(const unsigned int frame) const
Gets translation at frame.
Definition: AnimationClip.impl.h:42
const std::string & name() const
Gets name.
Definition: AnimationClip.impl.h:14
void setScale(const AtomsCore::Vector3 &scale, const unsigned int frame)
Sets scale at frame.
Definition: AnimationClip.impl.h:36
void setName(const std::string &name)
Sets name.
Definition: AnimationClip.impl.h:19
const AtomsCore::Quaternion & getRotation(const unsigned int frame) const
Gets rotation at frame.
Definition: AnimationClip.impl.h:47
void setTranslation(const AtomsCore::Vector3 &translation, const unsigned int frame)
Sets translation at frame.
Definition: AnimationClip.impl.h:24
const AtomsCore::Vector3 & getScale(const unsigned int frame) const
Gets scale at frame.
Definition: AnimationClip.impl.h:52
MapMetadata class.
Definition: MapMetadata.h:24
AtomsPtr< Metadata > getEntry(const Key &key)
Get an entry.
std::vector< Key > getKeys() const
Get keys.
void addEntry(const Key &key, AtomsPtr< Metadata > &data, bool cloneData=true)
Add an entry.
static MetadataFactory & instance()
Singleton access.
AtomsPtr< Metadata > createMetadata(const std::string &typeName)
Create a metadata.
Base Metadata class.
Definition: Metadata.h:24
AtomsMath::Quaternion Quaternion
Quaternion class.
Definition: AtomsMath.h:67
AtomsMath::Vector3 Vector3
Vector3 class.
Definition: AtomsMath.h:57
Atoms namespace.
Definition: Agent.h:29