Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AnimationCurveCache.cpp « collada « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3ad921baffe811de5f1342fc43bcaca7b21607cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed.
*
* ***** END GPL LICENSE BLOCK *****
*/

#include "AnimationCurveCache.h"

extern "C" {
#include "BKE_action.h"
#include "BLI_listbase.h"
}

AnimationCurveCache::AnimationCurveCache(bContext *C):
	mContext(C)
{
}

AnimationCurveCache::~AnimationCurveCache()
{
	clear_cache();
}

void AnimationCurveCache::clear_cache()
{

}

void AnimationCurveCache::clear_cache(Object *ob)
{

}

void AnimationCurveCache::create_curves(Object *ob)
{

}

void AnimationCurveCache::addObject(Object *ob)
{
	cached_objects.push_back(ob);
}

bool AnimationCurveCache::bone_matrix_local_get(Object *ob, Bone *bone, float(&mat)[4][4], bool for_opensim)
{

	/* Ok, lets be super cautious and check if the bone exists */
	bPose *pose = ob->pose;
	bPoseChannel *pchan = BKE_pose_channel_find_name(pose, bone->name);
	if (!pchan) {
		return false;
	}

	bAction *action = bc_getSceneObjectAction(ob);
	bPoseChannel *parchan = pchan->parent;
	enable_fcurves(action, bone->name);
	float ipar[4][4];

	if (bone->parent) {
		invert_m4_m4(ipar, parchan->pose_mat);
		mul_m4_m4m4(mat, ipar, pchan->pose_mat);
	}
	else
		copy_m4_m4(mat, pchan->pose_mat);

	/* OPEN_SIM_COMPATIBILITY
	* AFAIK animation to second life is via BVH, but no
	* reason to not have the collada-animation be correct
	*/
	if (for_opensim) {
		float temp[4][4];
		copy_m4_m4(temp, bone->arm_mat);
		temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
		invert_m4(temp);

		mul_m4_m4m4(mat, mat, temp);

		if (bone->parent) {
			copy_m4_m4(temp, bone->parent->arm_mat);
			temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;

			mul_m4_m4m4(mat, temp, mat);
		}
	}

	return true;
}

void AnimationCurveCache::sampleMain(Scene *scene, BC_export_transformation_type atm_type, bool for_opensim)
{
	std::map<int, std::vector<SamplePoint>>::iterator frame;
	for (frame = sample_frames.begin(); frame != sample_frames.end(); frame++) {
		int frame_index = frame->first;
		std::vector<SamplePoint> sample_points = frame->second;

		bc_update_scene(mContext, scene, frame_index);

		for (int spi = 0; spi < sample_points.size(); spi++) {
			SamplePoint &point = sample_points[spi];
			Object *ob = point.get_object();
			float mat[4][4];

			if (ob->type == OB_ARMATURE) {
				/* For Armatures we need to check if this maybe is a pose sample point*/
				Bone *bone = point.get_bone();
				if (bone) {
					if (bone_matrix_local_get(ob, bone, mat, for_opensim)) {
						point.set_matrix(mat);
					}
					continue;
				}
			}

			/* When this SamplePoint is not for a Bone, 
			 * then we just store the Object local matrix here
			 */

			BKE_object_matrix_local_get(ob, mat);
			point.set_matrix(mat);

		}
	}
}

/*
* enable fcurves driving a specific bone, disable all the rest
* if bone_name = NULL enable all fcurves
*/
void AnimationCurveCache::enable_fcurves(bAction *act, char *bone_name)
{
	FCurve *fcu;
	char prefix[200];

	if (bone_name)
		BLI_snprintf(prefix, sizeof(prefix), "pose.bones[\"%s\"]", bone_name);

	for (fcu = (FCurve *)act->curves.first; fcu; fcu = fcu->next) {
		if (bone_name) {
			if (STREQLEN(fcu->rna_path, prefix, strlen(prefix)))
				fcu->flag &= ~FCURVE_DISABLED;
			else
				fcu->flag |= FCURVE_DISABLED;
		}
		else {
			fcu->flag &= ~FCURVE_DISABLED;
		}
	}
}
/*
* Sample the scene at frames where object fcurves
* have defined keys.
*/
void AnimationCurveCache::sampleScene(Scene *scene, BC_export_transformation_type atm_type, bool for_opensim, bool keyframe_at_end)
{
	create_sample_frames_from_keyframes();
	sampleMain(scene, atm_type, for_opensim);
}

void AnimationCurveCache::sampleScene(Scene *scene, BC_export_transformation_type atm_type, int sampling_rate, bool for_opensim, bool keyframe_at_end)
{
	create_sample_frames_generated(scene->r.sfra, scene->r.efra, sampling_rate, keyframe_at_end);
	sampleMain(scene, atm_type, for_opensim);
}

std::vector<FCurve *> *AnimationCurveCache::getSampledCurves(Object *ob)
{
	std::map<Object *, std::vector<FCurve *>>::iterator fcurves;
	fcurves = cached_curves.find(ob);
	return (fcurves == cached_curves.end()) ? NULL : &fcurves->second;
}

std::vector<SamplePoint> &AnimationCurveCache::getFrameInfos(int frame_index)
{
	std::map<int, std::vector<SamplePoint>>::iterator frames = sample_frames.find(frame_index);
	if (frames == sample_frames.end()) {
		std::vector<SamplePoint> sample_points;
		sample_frames[frame_index] = sample_points;
	}
	return sample_frames[frame_index];
}


void AnimationCurveCache::add_sample_point(SamplePoint &point)
{
	int frame_index = point.get_frame();
	std::vector<SamplePoint> &frame_infos = getFrameInfos(frame_index);
	frame_infos.push_back(point);
}

/*
* loop over all cached objects
*     loop over all fcurves
*         record all keyframes
* 
* The vector sample_frames finally contains a list of vectors
* where each vector contains a list of SamplePoints which
* need to be processed when evaluating the animation.
*/
void AnimationCurveCache::create_sample_frames_from_keyframes()
{
	sample_frames.clear();
	for (int i = 0; i < cached_objects.size(); i++) {
		Object *ob = cached_objects[i];
		bAction *action = bc_getSceneObjectAction(ob);
		FCurve *fcu = (FCurve *)action->curves.first;

		for (; fcu; fcu = fcu->next) {
			for (unsigned int i = 0; i < fcu->totvert; i++) {
				float f = fcu->bezt[i].vec[1][0];
				int frame_index = int(f);
				SamplePoint sample_point(frame_index, ob, fcu, i);
				add_sample_point(sample_point);
			}
		}
	}
}

/*
* loop over all cached objects
*     loop over active action using a stesize of sampling_rate
*         record all frames
*
* The vector sample_frames finally contains a list of vectors
* where each vector contains a list of SamplePoints which
* need to be processed when evaluating the animation.
* Note: The FCurves of the objects will not be used here.
*/
void AnimationCurveCache::create_sample_frames_generated(float sfra, float efra, int sampling_rate, int keyframe_at_end)
{
	sample_frames.clear();

	for (int i = 0; i < cached_objects.size(); i++) {

		Object *ob = cached_objects[i];
		float f = sfra;

		do {		
			int frame_index = int(f);
			SamplePoint sample_point(frame_index, ob);
			add_sample_point(sample_point);

			/* Depending on the Object type add more sample points here
			*/

			if (ob && ob->type == OB_ARMATURE) {
				LISTBASE_FOREACH(bPoseChannel *, pchan, &ob->pose->chanbase) {
					SamplePoint point(frame_index, ob, pchan->bone);
					add_sample_point(sample_point);
				}
			}

			if (f == efra)
				break;
			f += sampling_rate;
			if (f > efra)
				if (keyframe_at_end)
					f = efra; // make sure the last frame is always exported
				else
					break;
		} while (true);
	}
}

Matrix::Matrix()
{
	unit_m4(matrix);
}

Matrix::Matrix(float (&mat)[4][4])
{
	set_matrix(mat);
}

void Matrix::set_matrix(float(&mat)[4][4])
{
	copy_m4_m4(matrix, mat);
}

void Matrix::set_matrix(Matrix &mat)
{
	copy_m4_m4(matrix, mat.matrix);
}

void Matrix::get_matrix(float(&mat)[4][4])
{
	copy_m4_m4(mat, matrix);
}

SamplePoint::SamplePoint(int frame, Object *ob)
{
	this->frame = frame;
	this->fcu = NULL;
	this->ob = ob;
	this->pose_bone = NULL;
	this->index = -1;
}

SamplePoint::SamplePoint(int frame, Object *ob, FCurve *fcu, int index)
{
	this->frame = frame;
	this->fcu = fcu;
	this->ob = ob;
	this->pose_bone = NULL;
	this->index = index;
	this->path = std::string(fcu->rna_path);

	/* Further elaborate on what this Fcurve is doing by checking
	 * its rna_path
     */

	if (ob && ob->type == OB_ARMATURE) {
		char *boneName = BLI_str_quoted_substrN(fcu->rna_path, "pose.bones[");
		bPose *pose = ob->pose;
		if (boneName) {
			bPoseChannel *pchan = BKE_pose_channel_find_name(pose, boneName);
			this->pose_bone = pchan->bone;
		}
	}
}


SamplePoint::SamplePoint(int frame, Object *ob, Bone *bone)
{
	this->frame = frame;
	this->fcu = NULL;
	this->ob = ob;
	this->pose_bone = bone;
	this->index = -1;
	this->path = "pose.bones[\"" + id_name(bone) + "\"].matrix";
}

Matrix &SamplePoint::get_matrix()
{
	return matrix;
}

void SamplePoint::set_matrix(Matrix &mat)
{
	this->matrix.set_matrix(mat);
}

void SamplePoint::set_matrix(float(&mat)[4][4])
{

}

Object *SamplePoint::get_object()
{
	return this->ob;
}

Bone *SamplePoint::get_bone()
{
	return this->pose_bone;
}

FCurve *SamplePoint::get_fcurve()
{
	return this->fcu;
}

int SamplePoint::get_frame()
{
	return this->frame;
}

int SamplePoint::get_fcurve_index()
{
	return this->index;
}

std::string &SamplePoint::get_path()
{
	return path;
}