Overte C++ Documentation
AnimNode.h
1 //
2 // AnimNode.h
3 //
4 // Created by Anthony J. Thibault on 9/2/15.
5 // Copyright (c) 2015 High Fidelity, Inc. All rights reserved.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 //
10 
11 #ifndef hifi_AnimNode_h
12 #define hifi_AnimNode_h
13 
14 #include <string>
15 #include <vector>
16 #include <memory>
17 #include <cassert>
18 #include <glm/glm.hpp>
19 #include <glm/gtc/quaternion.hpp>
20 
21 #include "AnimSkeleton.h"
22 #include "AnimVariant.h"
23 #include "AnimContext.h"
24 
25 class QJsonObject;
26 
27 // Base class for all elements in the animation blend tree.
28 // It provides the following categories of functions:
29 //
30 // * id getter, id is used to identify a node, useful for debugging and node searching.
31 // * type getter, helpful for determining the derived type of this node.
32 // * hierarchy accessors, for adding, removing and iterating over child AnimNodes
33 // * skeleton accessors, the skeleton is from the model whose bones we are going to manipulate
34 // * evaluate method, perform actual joint manipulations here and return result by reference.
35 // Also, append any triggers that are detected during evaluation.
36 
37 class AnimNode : public std::enable_shared_from_this<AnimNode> {
38 public:
39  using Type = AnimNodeType;
40  using Pointer = std::shared_ptr<AnimNode>;
41  using ConstPointer = std::shared_ptr<const AnimNode>;
42 
43  friend class AnimDebugDraw;
44  friend void buildChildMap(std::map<QString, Pointer>& map, Pointer node);
45  friend class AnimStateMachine;
46  friend class AnimRandomSwitch;
47 
48  AnimNode(Type type, const QString& id) : _type(type), _id(id) {}
49  virtual ~AnimNode() {}
50 
51  const QString& getID() const { return _id; }
52  Type getType() const { return _type; }
53 
54  void addOutputJoint(const QString& outputJointName) { _outputJointNames.push_back(outputJointName); }
55 
56  // hierarchy accessors
57  Pointer getParent();
58  void addChild(Pointer child);
59  void removeChild(Pointer child);
60  void replaceChild(Pointer oldChild, Pointer newChild);
61  Pointer getChild(int i) const;
62  int getChildCount() const { return (int)_children.size(); }
63 
64  // pair this AnimNode graph with a skeleton.
65  void setSkeleton(AnimSkeleton::ConstPointer skeleton);
66 
67  AnimSkeleton::ConstPointer getSkeleton() const { return _skeleton; }
68 
69  virtual const AnimPoseVec& evaluate(const AnimVariantMap& animVars, const AnimContext& context, float dt, AnimVariantMap& triggersOut) = 0;
70  virtual const AnimPoseVec& overlay(const AnimVariantMap& animVars, const AnimContext& context, float dt, AnimVariantMap& triggersOut,
71  const AnimPoseVec& underPoses) {
72  return evaluate(animVars, context, dt, triggersOut);
73  }
74 
75  void setCurrentFrame(float frame);
76  void setActive(bool active);
77 
78  template <typename F>
79  bool traverse(F func) {
80  if (func(shared_from_this())) {
81  for (auto&& child : _children) {
82  if (!child->traverse(func)) {
83  return false;
84  }
85  }
86  }
87  return true;
88  }
89 
90  Pointer findByName(const QString& id) {
91  Pointer result;
92  traverse([&](Pointer node) {
93  if (id == node->getID()) {
94  result = node;
95  return false;
96  }
97  return true;
98  });
99  return result;
100  }
101 
102  int findChildIndexByName(const QString& id) {
103  for (size_t i = 0; i < _children.size(); ++i) {
104  if (_children[i]->getID() == id) {
105  return (int)i;
106  }
107  }
108  return -1;
109  }
110 
111  const AnimPoseVec& getPoses() const { return getPosesInternal(); }
112 
113 protected:
114 
115  virtual void setCurrentFrameInternal(float frame) {}
116  virtual void setSkeletonInternal(AnimSkeleton::ConstPointer skeleton) { _skeleton = skeleton; }
117  virtual void setActiveInternal(bool active) {}
118 
119  // for AnimDebugDraw rendering
120  virtual const AnimPoseVec& getPosesInternal() const = 0;
121 
122  void processOutputJoints(AnimVariantMap& triggersOut) const;
123 
124  Type _type;
125  QString _id;
126  std::vector<AnimNode::Pointer> _children;
127  AnimSkeleton::ConstPointer _skeleton;
128  std::weak_ptr<AnimNode> _parent;
129  std::vector<QString> _outputJointNames;
130  bool _active { false };
131 
132  // no copies
133  AnimNode(const AnimNode&) = delete;
134  AnimNode& operator=(const AnimNode&) = delete;
135 };
136 
137 #endif // hifi_AnimNode_h