[Bf-blender-cvs] [57deb68e547] collada: Refactor: Now only one Sample per frame and per object

Gaia Clary noreply at git.blender.org
Wed Mar 28 21:31:16 CEST 2018


Commit: 57deb68e547f283387f1e27056e6de3bc0ce982a
Author: Gaia Clary
Date:   Sat Mar 24 22:30:52 2018 +0100
Branches: collada
https://developer.blender.org/rB57deb68e547f283387f1e27056e6de3bc0ce982a

Refactor: Now only one Sample per frame and per object

Previously the Bone aniumations where stored in separate BCSample
instances. Now the BCSample class has one more entry for the animated
bones (a matrix map)

I am now ready for final cleaning up and testing.

===================================================================

M	source/blender/collada/AnimationExporter.cpp
M	source/blender/collada/AnimationExporter.h
M	source/blender/collada/BCAnimationCurve.h
M	source/blender/collada/BCAnimationCurveContainer.cpp
M	source/blender/collada/BCAnimationCurveContainer.h
M	source/blender/collada/BCSampleData.cpp
M	source/blender/collada/BCSampleData.h

===================================================================

diff --git a/source/blender/collada/AnimationExporter.cpp b/source/blender/collada/AnimationExporter.cpp
index 59189969573..f2c6aafb4cc 100644
--- a/source/blender/collada/AnimationExporter.cpp
+++ b/source/blender/collada/AnimationExporter.cpp
@@ -222,7 +222,7 @@ void AnimationExporter::export_matrix_animation_set(Object *ob, BCAnimationSampl
 	std::vector<float> frames;
 	sampler.get_frame_set(frames, ob);
 	if (frames.size() > 0) {
-		BCFrameSampleMap samples;
+		BCMatrixSampleMap samples;
 		bool is_flat = sampler.get_samples(samples, ob);
 		if (!is_flat) {
 			export_matrix_animation(ob, frames, samples, sampler); // there is just one curve to export here
@@ -233,7 +233,7 @@ void AnimationExporter::export_matrix_animation_set(Object *ob, BCAnimationSampl
 void AnimationExporter::export_matrix_animation(
 	Object *ob, 
 	BCFrames &frames, 
-	BCFrameSampleMap &samples,
+	BCMatrixSampleMap &samples,
 	BCAnimationSampler &sampler)
 {
 	bAction *action = bc_getSceneObjectAction(ob);
@@ -255,7 +255,7 @@ void AnimationExporter::export_bone_animation_recursive(Object *ob, Bone *bone,
 	sampler.get_frame_set(frames, ob, bone);
 	
 	if (frames.size()) {
-		BCFrameSampleMap samples;
+		BCMatrixSampleMap samples;
 		bool is_flat = sampler.get_samples(samples, ob, bone);
 		if (!is_flat) {
 			export_bone_animation(ob, bone, frames, samples);
@@ -378,7 +378,7 @@ void AnimationExporter::export_curve_animation(Object *ob, const BCAnimationCurv
 	export_collada_curve_animation(id, curve_name, target, axis, curve);
 }
 
-void AnimationExporter::export_bone_animation(Object *ob, Bone *bone, BCFrames &frames, BCFrameSampleMap &samples)
+void AnimationExporter::export_bone_animation(Object *ob, Bone *bone, BCFrames &frames, BCMatrixSampleMap &samples)
 {
 	bAction* action = bc_getSceneObjectAction(ob);
 	std::string bone_name(bone->name);
@@ -457,7 +457,7 @@ void AnimationExporter::export_collada_curve_animation(
 	closeAnimation();
 }
 
-void AnimationExporter::export_collada_matrix_animation(std::string id, std::string name, std::string target, BCFrames &frames, BCFrameSampleMap &samples)
+void AnimationExporter::export_collada_matrix_animation(std::string id, std::string name, std::string target, BCFrames &frames, BCMatrixSampleMap &samples)
 {
 	fprintf(stdout, "Export animation matrix %s (%d control points)\n", id.c_str(), int(frames.size()));
 
@@ -759,7 +759,7 @@ std::string AnimationExporter::create_source_from_values(COLLADASW::InputSemanti
 /*
  * Create a collada matrix source for a set of samples
 */
-std::string AnimationExporter::create_4x4_source_from_values(BCFrameSampleMap &samples, const std::string &anim_id)
+std::string AnimationExporter::create_4x4_source_from_values(BCMatrixSampleMap &samples, const std::string &anim_id)
 {
 	COLLADASW::InputSemantic::Semantics semantic = COLLADASW::InputSemantic::OUTPUT;
 	std::string source_id = anim_id + get_semantic_suffix(semantic);
@@ -775,11 +775,11 @@ std::string AnimationExporter::create_4x4_source_from_values(BCFrameSampleMap &s
 
 	source.prepareToAppendValues();
 
-	BCFrameSampleMap::iterator it;
+	BCMatrixSampleMap::iterator it;
 	int j = 0;
 	int precision = (this->export_settings->limit_precision) ? 6 : -1; // could be made configurable
 	for (it = samples.begin(); it != samples.end(); it++) {
-		const BCSample *sample = it->second;
+		const BCMatrix *sample = it->second;
 		double daemat[4][4];
 		sample->get_matrix(daemat, true, precision );
 		source.appendValues(daemat);
diff --git a/source/blender/collada/AnimationExporter.h b/source/blender/collada/AnimationExporter.h
index 280b96e15f0..cd3d7a4604f 100644
--- a/source/blender/collada/AnimationExporter.h
+++ b/source/blender/collada/AnimationExporter.h
@@ -131,10 +131,10 @@ protected:
 		Object *ob,
 		BCAnimationSampler &sampler);
 
-	void export_matrix_animation (
+	void export_matrix_animation(
 		Object *ob,
 		BCFrames &frames,
-		BCFrameSampleMap &outmats,
+		BCMatrixSampleMap &outmats,
 		BCAnimationSampler &sampler);
 
 	void export_bone_animation_recursive(
@@ -146,14 +146,14 @@ protected:
 		Object *ob,
 		Bone *bone,
 		BCFrames &frames,
-		BCFrameSampleMap &outmats);
+		BCMatrixSampleMap &outmats);
 
 	void export_collada_matrix_animation(
 		std::string id,
 		std::string name,
 		std::string target,
 		BCFrames &frames,
-		BCFrameSampleMap &outmats);
+		BCMatrixSampleMap &outmats);
 
 	/* Helper functions */
 	void openAnimationWithClip(std::string id, std::string name);
@@ -162,7 +162,7 @@ protected:
 
 	std::string create_source_from_values(COLLADASW::InputSemantic::Semantics semantic, std::vector<float> &values, bool is_rot, const std::string& anim_id, const std::string axis_name);
 	std::string create_4x4_source_from_values(
-		BCFrameSampleMap &cache,
+		BCMatrixSampleMap &cache,
 		const std::string& anim_id);
 
 	std::string create_linear_interpolation_source(int tot, const std::string& anim_id);
diff --git a/source/blender/collada/BCAnimationCurve.h b/source/blender/collada/BCAnimationCurve.h
index ee0540c551d..9ae9044bf94 100644
--- a/source/blender/collada/BCAnimationCurve.h
+++ b/source/blender/collada/BCAnimationCurve.h
@@ -38,7 +38,6 @@ extern "C"
 #include "ED_keyframes_edit.h"
 }
 
-typedef float(BCMatrix)[4][4];
 typedef std::set<float> BCFrameSet;
 typedef std::vector<float> BCFrames;
 typedef std::vector<float> BCValues;
diff --git a/source/blender/collada/BCAnimationCurveContainer.cpp b/source/blender/collada/BCAnimationCurveContainer.cpp
index 777c293d04a..b1e80a36c10 100644
--- a/source/blender/collada/BCAnimationCurveContainer.cpp
+++ b/source/blender/collada/BCAnimationCurveContainer.cpp
@@ -47,6 +47,7 @@ extern "C" {
 static std::string EMPTY_STRING;
 static BCAnimationCurveMap BCEmptyAnimationCurves;
 static const BCSample UNIT_SAMPLE;
+static const BCMatrix UNIT_MATRIX;
 
 /* 
     BCAnimationSampler
@@ -144,20 +145,21 @@ void BCAnimationSampler::sample_scene(
 					needs_update = false;
 				}
 
-				BCMatrix mat;
+				Matrix mat;
+				BKE_object_matrix_local_get(ob, mat);
+				BCSample &ob_sample = sample_data.add(ob, mat, frame_index);
+
 				if (ob->type == OB_ARMATURE) {
 					bPoseChannel *pchan;
 					for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
 						Bone *bone = pchan->bone;
 						if (bone_matrix_local_get(ob, bone, mat, for_opensim)) {
+							ob_sample.set_bone(bone, mat);
 							sample_data.add(ob, bone, mat, frame_index);
 						}
 					}
 				}
 
-				BKE_object_matrix_local_get(ob, mat);
-				BCSample &ob_sample = sample_data.add(ob, mat, frame_index);
-
 				if (ob->type == OB_CAMERA) {
 					Camera *camera = (Camera *)ob->data;
 					ob_sample.set_value(BC_ANIMATION_TYPE_XFOV, 0, camera->lens);
@@ -371,6 +373,29 @@ bool BCAnimationSampler::is_flat_line(BCFrameSampleMap &values) const
 	return true;
 }
 
+bool BCAnimationSampler::is_flat_line(BCMatrixSampleMap &values) const
+{
+	static float MIN_DISTANCE = 0.00001;
+
+	if (values.size() < 2)
+		return true; // need at least 2 entries to be not flat
+
+	BCMatrixSampleMap::iterator it;
+	const BCMatrix *refmat = nullptr;
+	for (it = values.begin(); it != values.end(); ++it) {
+		const BCMatrix *matrix = it->second;
+
+		if (refmat == nullptr) {
+			refmat = matrix;
+			continue;
+		}
+
+		if (!matrix->in_range(*refmat, MIN_DISTANCE))
+			return false;
+	}
+	return true;
+}
+
 bool BCAnimationSampler::is_flat_line(std::vector<float> &values) const
 {
 	return BCAnimationCurve::is_flat_line(values);
@@ -393,10 +418,9 @@ void BCAnimationSampler::get_frame_set(BCFrames &frames, Object *ob, const BCAni
 	curve.get_sampled_frames(frames);
 }
 
-bool BCAnimationSampler::get_samples(BCFrameSampleMap &samples, Object *ob, Bone *bone)
+bool BCAnimationSampler::get_samples(BCMatrixSampleMap &samples, Object *ob, Bone *bone)
 {
-	const BCSampleKey key(ob, bone);
-	sample_data.get_matrices(key, samples);
+	sample_data.get_matrices(ob, bone, samples);
 	return is_flat_line(samples);
 }
 
@@ -407,6 +431,13 @@ bool BCAnimationSampler::get_samples(BCFrameSampleMap &samples, Object *ob)
 	return is_flat_line(samples);
 }
 
+bool BCAnimationSampler::get_samples(BCMatrixSampleMap &samples, Object *ob)
+{
+	const BCSampleKey key(ob);
+	sample_data.get_matrices(key, samples);
+	return is_flat_line(samples);
+}
+
 /*
    Add sampled values to FCurve 
    If no FCurve exists, create a temporary FCurve;
@@ -613,7 +644,7 @@ bool BCAnimationSampler::has_animations(Scene *sce, LinkNode *export_set)
 }
 /* ==================================================================== */
 
-BCSample &BCSampleFrame::add(Object *ob, BCMatrix &mat)
+BCSample &BCSampleFrame::add(Object *ob, Matrix &mat)
 {
 	BCSample *sample = new BCSample(mat);
 	sampleMap[BCSampleKey(ob)] = sample;
@@ -621,7 +652,7 @@ BCSample &BCSampleFrame::add(Object *ob, BCMatrix &mat)
 }
 
 /* Add a new Bone to this map with the given Matrix*/
-BCSample &BCSampleFrame::add(Object *ob, Bone *bone, BCMatrix &mat)
+BCSample &BCSampleFrame::add(Object *ob, Bone *bone, Matrix &mat)
 {
 	BCSample *sample = new BCSample(mat);
 	sampleMap[BCSampleKey(ob, bone)] = sample;
@@ -638,6 +669,16 @@ const BCSample &BCSampleFrame::get_sample(const BCSampleKey key) const
 	return *it->second;
 }
 
+const BCMatrix &BCSampleFrame::get_sample_matrix(const BCSampleKey key) const
+{
+	BCSampleKeysMap::const_iterator it = sampleMap.find(key);
+	if (it == sampleMap.end()) {
+		return UNIT_MATRIX;
+	}
+	BCSample *sample = it->second;
+	return *sample->get_sampled_matrix();
+}
+
 /* Get the matrix for the given Object, returns Unity when the Objewct is not sampled */
 const BCSample &BCSampleFrame::get_sample(Object *ob) const
 {
@@ -646,18 +687,18 @@ const BCSample &BCSampleFrame::get_sample(Object *ob) const
 }
 
 /* Get the matrix for the given Bone, returns Unity when the Objewct is not sampled */
-const BCSample &BCSampleFrame::get_sample(Object *ob, Bone *bone) const
+const BCMatrix *BCSampleFrame::get_sample_matrix(Object *ob, Bone *bone) const
 {
-	const BCSampleKey key(ob, bone);
-	return get_sample(key);
-}
+	const BCSampleKey key(ob);
+	BCSampleKeysMap::const_iterator it = sampleMap.find(key);
+	if (it == sampleMap.end()) {
+		return nullptr;
+	}
 
-/* Get the matrix for t

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list