Atoms Crowd  7.0.0
BlendSpaceOperator.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 
11 #include <AtomsGraph/Ports.h>
12 #include <Atoms/Globals.h>
13 #include <Atoms/Graph/Operators/StateMachineBaseNodeOperator.h>
14 #include <Atoms/Graph/Operators/ClipReaderOperator.h>
15 #include <Atoms/AnimationClip.h>
16 #include <Atoms/Graph/AgentBehaviourNetwork.h>
17 #include <Atoms/Locomotion.h>
18 
19 #include <deque>
20 
21 namespace Atoms
22 {
23 
25  {
26  public:
27 
28  enum WaitingState
29  {
30  kNone = 0,
31  kIn,
32  kOut,
33  kWaitingForIn,
34  kWaitingForOut
35  };
36 
37  struct OutputData
38  {
39  AtomsGraph::DoublePort* frameRatePort;
40  AtomsGraph::DoublePort* timePort;
41  AtomsGraph::DoublePort* weightPort;
42  AtomsGraph::BooleanPort* activePort;
43  Atoms::ClipReaderOperator* clipReaderOp;
44  AnimationClipPtr clip;
45  AtomsMath::Vector2 position;
46  double time;
47  double length;
48  double fps;
49  int start;
50  int end;
51  bool active;
52  bool hasLoop;
53  bool unsynced;
54  int blendIn;
55  int blendOut;
56  int waitFrames;
57  std::string name;
58  };
59 
60  NODE_STANDARD_MEMBERS
61 
63 
64  virtual ~BlendSpaceOperator();
65 
66  bool compute(const AtomsGraph::ComputeData* computeData);
67 
68  void reset();
69 
70  void resetTime();
71 
72  inline OutputData& outputPortData(size_t index) { return m_outPortData[index]; }
73 
74  inline size_t numOutputPorts() const { return m_outPortData.size(); }
75 
76  void setLocomotionTriangles(const std::vector<AtomsUtils::Triangle2>& triangles) { m_locomotionTriangles = triangles; };
77 
78  void setSkipTriangles(const std::set<int>& skipTriangles) { m_skipTriangles = skipTriangles; };
79 
80  inline void setMaxAngularAcceleration(const double value) { m_maxAngularAcceleration = value; };
81 
82  inline void setMaxLinearAcceleration(const double value) { m_maxLinearAcceleration = value; };
83 
84  inline void setLinearAverageNumFrames(const int value) { m_linearAverageNumFrames = value; };
85 
86  inline void setAngularAverageNumFrames(const int value) { m_angularAverageNumFrames = value; };
87 
88  inline void setForcedUnsyncedClips(const bool value) { m_forceUnsyncedClips = value; };
89 
90  bool isReadyToTransition(const OutputData& clipData, double frameStep);
91 
92  // methods from the AnimationStateOperator
93  bool isReadyToTransition(double frameRate = 1.0, bool checkTransitionFrames = true, bool checkForceTransition = true);
94  void advanceSingleClip(double timeStep, double frameStep, bool randomClip = false);
95  double computeBlendInOffset(double frameRate, bool checkTransitionFrames = true);
96  void computeBlendInOffsetInPlace(double frameRate, bool checkTransitionFrames = true);
97  double getBlendInOffset();
98  void prepareForBlendIn(double offset, double timeStep);
99  bool isBlendInStage(double offset, unsigned int blend);
100  bool isBlendOutStage(double& frameDifference, unsigned int blend);
101  double computeBlendInWeight(double offset, unsigned int blend);
102  double computeBlendOutWeight(unsigned int blend);
103  void setUpBlendOutFrame(double offset, double timeStep);
104  bool isBlendOutFinished(unsigned int blend);
105  void initSubState(double frame);
106  bool hasClips() { return false; };
107  void checkAndPlayUnsynced(const int syncedMajorId, const double deltaTime, double& useClipDirection);
108 
109  void buildNetwork(const AtomsPtr<Atoms::StateMachine>& stateMachine, const Atoms::AnimationState& state, std::map<std::string, std::string>& clipToAgentTypeNames, std::mutex& mutex, const double timeOffset, AtomsCore::Rand32* idRandom);
110 
111  void buildNetwork(const Atoms::LocomotionCPtr& loco, const double timeOffset);
112 
113  WaitingState waitingState() { return m_waitingState; };
114  const AtomsCore::Vector2f &currentPosition() { return m_cachedPosition; };
115  const AtomsCore::Vector2f &getInputPosition() { return m_inputPosition; };
116  const AtomsCore::Vector2f &getUnsyncPosition() { return m_unsyncPosition; };
117  const AtomsCore::Vector3f &barycentricCoordinates() { return m_prevBarycentricCoordinates; };
118  const AtomsUtils::Triangle2 &currentTriangle() { return m_prevTriangle; };
119  const int unsyncedClipId() { return m_unsyncedId; };
120  const bool forceUnsyncedClips() { return m_forceUnsyncedClips; };
121 
122  private:
123 
124  bool updateInputPosition();
125  bool shouldCheckNextUnsyncWaitFrames(const OutputData& clipData, double frameStep, const int waitFrames);
126  void setClipWeights(const std::vector<BlendSpaceOperator::OutputData>& datas, const AtomsCore::Vector3f& weights, const AtomsUtils::Triangle2& triangle, const double multiplier = 1.0);
127  void cacheBarycentricCoordinatesAndTriangle(const AtomsCore::Vector3f& barycentricCoords, const AtomsUtils::Triangle2& triangle);
128  bool computeBarycentricCoordinatesAndTriangle(AtomsMath::Vector3f& barycentricCoords, AtomsUtils::Triangle2& outTriangle, const bool cacheOnly = false);
129 
130  std::vector<OutputData> m_outPortData;
131 
132  std::vector<AtomsUtils::Triangle2> m_locomotionTriangles;
133  std::set<int> m_skipTriangles;
134 
135  std::deque<float> m_angularVelocities;
136 
137  std::deque<float> m_linearVelocities;
138 
139  AtomsCore::Vector3f m_prevBarycentricCoordinates;
140  AtomsCore::Vector3f m_cachedWeights;
141  AtomsUtils::Triangle2 m_prevTriangle;
142  AtomsUtils::Triangle2 m_cachedTriangle;
143 
144  AtomsCore::Vector2f m_inputPosition;
145  AtomsCore::Vector2f m_cachedPosition;
146  AtomsCore::Vector2f m_unsyncPosition;
147 
148  double m_time;
149 
150  double m_previousTime;
151 
152  double m_previousUseClipDirection;
153 
154  double m_maxAngularAcceleration;
155 
156  double m_maxLinearAcceleration;
157 
158  int m_currentUnsyncedStep;
159 
160  int m_previousUnsyncedMajorId;
161 
162  int m_currentMajorId;
163 
164  int m_majorId;
165 
166  int m_unsyncedId;
167 
168  int m_nextUnsyncedId;
169 
170  int m_numWaitFrames;
171 
172  int m_totNumWaitFrames;
173 
174  int m_angularAverageNumFrames;
175 
176  int m_linearAverageNumFrames;
177 
178  WaitingState m_waitingState;
179 
180  bool m_forceUnsyncedClips;
181 
182  bool m_playingUnsynced;
183 
184  };
185 }
Animation State.
Definition: AnimationState.h:25
Definition: BlendSpaceOperator.h:25
bool compute(const AtomsGraph::ComputeData *computeData)
Compute function.
void reset()
Operator reset function.
Definition: ClipReaderOperator.h:22
Definition: StateMachineBaseNodeOperator.h:27
Definition: Node.h:21
Generic node port class.
Definition: PortTemplate.h:24
Triangle 2D class.
Definition: Triangle.h:87
AtomsMath::Vector2f Vector2f
Vector2 class.
Definition: AtomsMath.h:55
AtomsMath::Vector3f Vector3f
Vector3 class.
Definition: AtomsMath.h:58
Atoms namespace.
Definition: Agent.h:29
AtomsPtr< AnimationClip > AnimationClipPtr
animation clip pointer
Definition: AnimationClip.h:28
AtomsPtr< const Locomotion > LocomotionCPtr
locomotion const pointer
Definition: Locomotion.h:86
Definition: BlendSpaceOperator.h:38