Blender  V2.93
BCAnimationSampler.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 #include "BCAnimationCurve.h"
20 #include "BCSampleData.h"
21 #include "collada_utils.h"
22 
23 #include "BKE_action.h"
24 #include "BKE_lib_id.h"
25 
26 #include "BLI_math_rotation.h"
27 
28 #include "DNA_action_types.h"
29 
30 /* Collection of animation curves */
31 class BCAnimation {
32  private:
33  Object *reference = NULL;
34  bContext *mContext;
35 
36  public:
39 
40  BCAnimation(bContext *C, Object *ob) : mContext(C)
41  {
42  Main *bmain = CTX_data_main(mContext);
43  reference = (Object *)BKE_id_copy(bmain, &ob->id);
44  id_us_min(&reference->id);
45  }
46 
48  {
49  BCAnimationCurveMap::iterator it;
50  for (it = curve_map.begin(); it != curve_map.end(); ++it) {
51  delete it->second;
52  }
53 
54  if (reference && reference->id.us == 0) {
55  Main *bmain = CTX_data_main(mContext);
56  BKE_id_delete(bmain, &reference->id);
57  }
58  curve_map.clear();
59  }
60 
62  {
63  return reference;
64  }
65 };
66 
67 typedef std::map<Object *, BCAnimation *> BCAnimationObjectMap;
68 
70 
71  /* Each frame on the timeline that needs to be sampled will have
72  * one BCSampleFrame where we collect sample information about all objects
73  * that need to be sampled for that frame. */
74 
75  private:
76  BCSampleMap sampleMap;
77 
78  public:
80  {
81  BCSampleMap::iterator it;
82  for (it = sampleMap.begin(); it != sampleMap.end(); ++it) {
83  BCSample *sample = it->second;
84  delete sample;
85  }
86  sampleMap.clear();
87  }
88 
89  BCSample &add(Object *ob);
90 
91  /* Following methods return NULL if object is not in the sampleMap*/
92  const BCSample *get_sample(Object *ob) const;
93  const BCMatrix *get_sample_matrix(Object *ob) const;
94  const BCMatrix *get_sample_matrix(Object *ob, Bone *bone) const;
95 
96  bool has_sample_for(Object *ob) const;
97  bool has_sample_for(Object *ob, Bone *bone) const;
98 };
99 
100 typedef std::map<int, BCSampleFrame> BCSampleFrameMap;
101 
103 
104  /*
105  * The BCSampleFrameContainer stores a map of BCSampleFrame objects
106  * with the timeline frame as key.
107  *
108  * Some details on the purpose:
109  * An Animation is made of multiple FCurves where each FCurve can
110  * have multiple keyframes. When we want to export the animation we
111  * also can decide whether we want to export the keyframes or a set
112  * of sample frames at equidistant locations (sample period).
113  * In any case we must resample first need to resample it fully
114  * to resolve things like:
115  *
116  * - animations by constraints
117  * - animations by drivers
118  *
119  * For this purpose we need to step through the entire animation and
120  * then sample each frame that contains at least one keyFrame or
121  * sampleFrame. Then for each frame we have to store the transform
122  * information for all exported objects in a BCSampleframe
123  *
124  * The entire set of BCSampleframes is finally collected into
125  * a BCSampleframneContainer
126  */
127 
128  private:
129  BCSampleFrameMap sample_frames;
130 
131  public:
133  {
134  }
135 
136  BCSample &add(Object *ob, int frame_index);
137  BCSampleFrame *get_frame(int frame_index); /* returns NULL if frame does not exist */
138 
139  int get_frames(std::vector<int> &frames) const;
140  int get_frames(Object *ob, BCFrames &frames) const;
141  int get_frames(Object *ob, Bone *bone, BCFrames &frames) const;
142 
143  int get_samples(Object *ob, BCFrameSampleMap &samples) const;
144  int get_matrices(Object *ob, BCMatrixSampleMap &samples) const;
145  int get_matrices(Object *ob, Bone *bone, BCMatrixSampleMap &samples) const;
146 };
147 
149  private:
150  BCExportSettings &export_settings;
151  BCSampleFrameContainer sample_data;
152  BCAnimationObjectMap objects;
153 
154  void generate_transform(Object *ob, const BCCurveKey &key, BCAnimationCurveMap &curves);
155  void generate_transforms(Object *ob,
156  const std::string prep,
157  const BC_animation_type type,
158  BCAnimationCurveMap &curves);
159  void generate_transforms(Object *ob, Bone *bone, BCAnimationCurveMap &curves);
160 
161  void initialize_curves(BCAnimationCurveMap &curves, Object *ob);
162  void initialize_keyframes(BCFrameSet &frameset, Object *ob);
163  BCSample &sample_object(Object *ob, int frame_index, bool for_opensim);
164  void update_animation_curves(BCAnimation &animation,
165  BCSample &sample,
166  Object *ob,
167  int frame_index);
168  void check_property_is_animated(
169  BCAnimation &animation, float *ref, float *val, std::string data_path, int length);
170 
171  public:
172  BCAnimationSampler(BCExportSettings &export_settings, BCObjectSet &object_set);
174 
175  void add_object(Object *ob);
176 
177  void sample_scene(BCExportSettings &export_settings, bool keyframe_at_end);
178 
180  void get_object_frames(BCFrames &frames, Object *ob);
181  bool get_object_samples(BCMatrixSampleMap &samples, Object *ob);
182  void get_bone_frames(BCFrames &frames, Object *ob, Bone *bone);
183  bool get_bone_samples(BCMatrixSampleMap &samples, Object *ob, Bone *bone);
184 
185  static void get_animated_from_export_set(std::set<Object *> &animated_objects,
186  LinkNode &export_set);
187  static void find_depending_animated(std::set<Object *> &animated_objects,
188  std::set<Object *> &candidates);
189  static bool is_animated_by_constraint(Object *ob,
190  ListBase *conlist,
191  std::set<Object *> &animated_objects);
192 };
BC_animation_type
std::map< BCCurveKey, BCAnimationCurve * > BCAnimationCurveMap
std::vector< float > BCFrames
std::set< float > BCFrameSet
std::map< Object *, BCAnimation * > BCAnimationObjectMap
std::map< int, BCSampleFrame > BCSampleFrameMap
std::map< int, const BCSample * > BCFrameSampleMap
Definition: BCSampleData.h:62
std::map< Object *, BCSample * > BCSampleMap
Definition: BCSampleData.h:61
std::map< int, const BCMatrix * > BCMatrixSampleMap
Definition: BCSampleData.h:63
Blender kernel action and pose functionality.
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
struct ID * BKE_id_copy(struct Main *bmain, const struct ID *id)
void id_us_min(struct ID *id)
Definition: lib_id.c:297
void BKE_id_delete(struct Main *bmain, void *idv) ATTR_NONNULL()
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
#define C
Definition: RandGen.cpp:39
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
void get_object_frames(BCFrames &frames, Object *ob)
static void find_depending_animated(std::set< Object * > &animated_objects, std::set< Object * > &candidates)
bool get_bone_samples(BCMatrixSampleMap &samples, Object *ob, Bone *bone)
static void get_animated_from_export_set(std::set< Object * > &animated_objects, LinkNode &export_set)
void get_bone_frames(BCFrames &frames, Object *ob, Bone *bone)
BCAnimationSampler(BCExportSettings &export_settings, BCObjectSet &object_set)
void add_object(Object *ob)
void sample_scene(BCExportSettings &export_settings, bool keyframe_at_end)
static bool is_animated_by_constraint(Object *ob, ListBase *conlist, std::set< Object * > &animated_objects)
bool get_object_samples(BCMatrixSampleMap &samples, Object *ob)
BCAnimationCurveMap * get_curves(Object *ob)
BCAnimation(bContext *C, Object *ob)
Object * get_reference()
BCFrameSet frame_set
BCAnimationCurveMap curve_map
BCSample & add(Object *ob, int frame_index)
int get_frames(std::vector< int > &frames) const
BCSampleFrame * get_frame(int frame_index)
int get_samples(Object *ob, BCFrameSampleMap &samples) const
int get_matrices(Object *ob, BCMatrixSampleMap &samples) const
const BCMatrix * get_sample_matrix(Object *ob) const
bool has_sample_for(Object *ob) const
const BCSample * get_sample(Object *ob) const
BCSample & add(Object *ob)
std::set< Object * > BCObjectSet
Definition: collada_utils.h:72
static void sample(SocketReader *reader, int x, int y, float color[4])
int us
Definition: DNA_ID.h:293
Definition: BKE_main.h:116