[Bf-blender-cvs] [938ecae64fa] collada: Refactor: Use more general appraoch to get data from BCMatrix

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


Commit: 938ecae64fad6fa4a016834e97a6b1ee2dfcfa48
Author: Gaia Clary
Date:   Fri Mar 23 15:49:06 2018 +0100
Branches: collada
https://developer.blender.org/rB938ecae64fad6fa4a016834e97a6b1ee2dfcfa48

Refactor: Use more general appraoch to get data from BCMatrix

Added virtual function get_value_for() that can pick the correct data
from a Matrix (and later also for a vector or a float array) (wip)

this is in preparation for adding Sample data for Materials, light and camera
Those animations do not need matrix values, but either simple float arrays
or float vector arrays. I still want to handle those animation samples
in the same way as the Matrix samples. So i try to use a class hierarchy here
with the base class BCSample and derived classes where BCMatrix is the only
implemented subclass so far. (wip)

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

M	source/blender/collada/BCAnimationCurve.cpp
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/BCAnimationCurve.cpp b/source/blender/collada/BCAnimationCurve.cpp
index 8cee56184da..a110ae6f3e3 100644
--- a/source/blender/collada/BCAnimationCurve.cpp
+++ b/source/blender/collada/BCAnimationCurve.cpp
@@ -394,37 +394,22 @@ void BCAnimationCurve::add_value(const float val, const int frame_index)
 	}
 }
 
+
 /*
 Pick the value from the matrix according to the definition of the FCurve
 Note: This works only for "scale", "rotation", "rotation_euler" and "location"
 */
-void BCAnimationCurve::add_value(BCMatrix mat, int frame)
+bool BCAnimationCurve::add_value(const BCSample &sample, int frame)
 {
 	std::string target = get_channel_target();
 	const int array_index = curve_key.index();
-	float val=0;
+	float val = 0;
 
-	if (target == "location") {
-		const float(&loc)[3] = mat.location();
-		val = loc[array_index];
-	}
-	else if (target == "scale") {
-		const float(&size)[3] = mat.scale();
-		val = size[array_index];
+	bool good = sample.get_value_for(target, array_index, &val);
+	if (good) {
+		add_value(val, frame);
 	}
-	else if (
-		target == "rotation" ||
-		target == "rotation_euler") {
-		const float(&rot)[3] = mat.rotation();
-		val = rot[array_index];
-	}
-	else
-	{
-		const float(&quat)[4] = mat.quat();
-		val = quat[array_index];
-	}
-
-	add_value(val, frame);
+	return good;
 }
 
 /*
diff --git a/source/blender/collada/BCAnimationCurve.h b/source/blender/collada/BCAnimationCurve.h
index a8144d56470..deb3db734b4 100644
--- a/source/blender/collada/BCAnimationCurve.h
+++ b/source/blender/collada/BCAnimationCurve.h
@@ -165,17 +165,13 @@ public:
 	Bone *get_bone(Object *ob) const;
 
 	void calchandles();
+	bool add_value(const BCSample &sample, int frame);
 	void add_value(const float val, const int frame);
 	void init(const BC_animation_curve_type type, const std::string rna_path, const int index);
 	void init(const BC_animation_curve_type type, FCurve *fcu);
 	FCurve *get_edit_fcurve();
 	const FCurve *get_fcurve() const;
 
-	/*
-	Pick the value from the matrix accoridng to the definition of the FCurve
-	Note: This works only for "scale", "rotation_quaternion", "rotation_euler" and "location"
-	*/
-	void add_value(BCMatrix mat, int frame);
 	void remove_unused_keyframes();
 
 	/*
diff --git a/source/blender/collada/BCAnimationCurveContainer.cpp b/source/blender/collada/BCAnimationCurveContainer.cpp
index 6ce2c496aaf..11cdf23486f 100644
--- a/source/blender/collada/BCAnimationCurveContainer.cpp
+++ b/source/blender/collada/BCAnimationCurveContainer.cpp
@@ -400,29 +400,6 @@ bool BCAnimationSampler::get_matrix_set(BCMatrixMap &matrices, Object *ob)
 	return is_flat_line(matrices);
 }
 
-const float BCAnimationSampler::get_value(const BCMatrix &mat, const std::string &path, const int array_index) const
-{
-	std::string channel = bc_string_after(path, '.');
-
-	if (channel == "location") {
-		const float(&loc)[3] = mat.location();
-		return loc[array_index];
-	}
-	else if (channel == "scale") {
-		const float(&size)[3] = mat.scale();
-		return size[array_index];
-	}
-	else if (channel == "rotation_euler") {
-		const float(&rot)[3] = mat.rotation();
-		return rot[array_index];
-	}
-	else
-	{
-		const float(&quat)[4] = mat.quat();
-		return quat[array_index];
-	}
-}
-
 /*
    Add sampled values to FCurve 
    If no FCurve exists, create a temporary FCurve;
@@ -434,16 +411,21 @@ void BCAnimationSampler::add_value_set(
 	BCAnimationCurve &curve, 
 	BC_export_animation_type animation_type)
 {
-	std::string rna_path = curve.get_rna_path();
 	int array_index = curve.get_array_index();
+	std::string target = curve.get_channel_target();
 
 	BCMatrixMap::iterator it;
 	for (it = matrices.begin(); it != matrices.end(); ++it) {
 		const int frame_index = it->first;
 		if (animation_type == BC_ANIMATION_TYPE_SAMPLE || curve.is_keyframe(frame_index)) {
+
 			const BCMatrix *matrix = it->second;
-			float val = get_value(*matrix, rna_path, array_index);
-			curve.add_value(val, frame_index);
+			float val = 0;
+
+			bool good = matrix->get_value_for(target, array_index, &val);
+			if (good) {
+				curve.add_value(val, frame_index);
+			}
 		}
 	}
 	curve.remove_unused_keyframes();
@@ -568,7 +550,7 @@ void BCSampleFrame::add(Object *ob, Bone *bone, BCMatrix &matrix)
 }
 
 /* Get the matrix for the given key, returns Unity when the key does not exist */
-const BCMatrix &BCSampleFrame::matrix(const BCSampleKey key) const
+const BCMatrix &BCSampleFrame::get_sample(const BCSampleKey key) const
 {
 	BCSamplesMap::const_iterator it = sampleMap.find(key);
 	if (it == sampleMap.end()) {
@@ -578,17 +560,17 @@ const BCMatrix &BCSampleFrame::matrix(const BCSampleKey key) const
 }
 
 /* Get the matrix for the given Object, returns Unity when the Objewct is not sampled */
-const BCMatrix &BCSampleFrame::matrix(Object *ob) const
+const BCMatrix &BCSampleFrame::get_sample(Object *ob) const
 {
 	const BCSampleKey key(ob);
-	return matrix(key);
+	return get_sample(key);
 }
 
 /* Get the matrix for the given Bone, returns Unity when the Objewct is not sampled */
-const BCMatrix &BCSampleFrame::matrix(Object *ob, Bone *bone) const
+const BCMatrix &BCSampleFrame::get_sample(Object *ob, Bone *bone) const
 {
 	const BCSampleKey key(ob, bone);
-	return matrix(key);
+	return get_sample(key);
 }
 
 /* Check if the key is in this BCSampleFrame */
@@ -692,7 +674,7 @@ const int BCSampleFrames::get_matrices(const BCSampleKey &key, BCMatrixMap &matr
 	for (it = sample_frames.begin(); it != sample_frames.end(); ++it) {
 		const BCSampleFrame &frame = it->second;
 		if (frame.contains(key)) {
-			const BCMatrix &matrix = frame.matrix(key);
+			const BCMatrix &matrix = frame.get_sample(key);
 			matrices[it->first] = &matrix;
 		}
 	}
diff --git a/source/blender/collada/BCAnimationCurveContainer.h b/source/blender/collada/BCAnimationCurveContainer.h
index 65b35abe76b..0148fc34158 100644
--- a/source/blender/collada/BCAnimationCurveContainer.h
+++ b/source/blender/collada/BCAnimationCurveContainer.h
@@ -23,8 +23,8 @@
 * ***** END GPL LICENSE BLOCK *****
 */
 
-#ifndef __ANIMATION_CURVE_CONTAINER_H__
-#define __ANIMATION_CURVE_CONTAINER_H__
+#ifndef __BC_ANIMATION_CURVE_CONTAINER_H__
+#define __BC_ANIMATION_CURVE_CONTAINER_H__
 
 #include "BCAnimationCurve.h"
 #include "BCSampleData.h"
@@ -108,13 +108,13 @@ public:
 	void add(Object *ob, Bone *bone, BCMatrix &matrix);
 
 	/* Get the matrix for the given key, returns Unity when the key does not exist */
-	const BCMatrix &matrix(const BCSampleKey key) const;
+	const BCMatrix &get_sample(const BCSampleKey key) const;
 
 	/* Get the matrix for the given Object, returns Unity when the Objewct is not sampled */
-	const BCMatrix &matrix(Object *ob) const;
+	const BCMatrix &get_sample(Object *ob) const;
 
 	/* Get the matrix for the given Bone, returns Unity when the Objewct is not sampled */
-	const BCMatrix &matrix(Object *ob, Bone *bone) const;
+	const BCMatrix &get_sample(Object *ob, Bone *bone) const;
 
 	/* Check if the key is in this BCSampleFrame */
 	const bool contains(const BCSampleKey &key) const;
@@ -244,7 +244,6 @@ public:
 	bool get_matrix_set(BCMatrixMap &matrices, Object *ob, Bone *bone);
 	bool get_matrix_set(BCMatrixMap &matrices, Object *ob);
 
-	const float get_value(const BCMatrix &matrix, const std::string &path, const int array_index) const;
 	void add_value_set(BCMatrixMap &matrices, BCAnimationCurve &curve, BC_export_animation_type animation_type);
 	const bool get_value_set(BCValues &values, BCFrames &frames, BCAnimationCurve &curve);
 	void get_curves(BCAnimationCurveMap &curves, Object *ob);
diff --git a/source/blender/collada/BCSampleData.cpp b/source/blender/collada/BCSampleData.cpp
index be948244413..da0512b1fc7 100644
--- a/source/blender/collada/BCSampleData.cpp
+++ b/source/blender/collada/BCSampleData.cpp
@@ -46,6 +46,35 @@ BCMatrix::~BCMatrix()
 	int x = 0;
 }
 
+const bool BCMatrix::get_value_for(const std::string &target, const int array_index, float *val) const
+{
+	if (target == "location") {
+		const float(&floc)[3] = location();
+		*val = floc[array_index];
+	}
+	else if (target == "scale") {
+		const float(&fsize)[3] = scale();
+		*val = fsize[array_index];
+	}
+	else if (
+		target == "rotation" ||
+		target == "rotation_euler") {
+		const float(&frot)[3] = rotation();
+		*val = frot[array_index];
+	}
+	else if (
+		target == "rotation_quaternion") {
+		const float(&qt)[4] = quat();
+		*val = qt[array_index];
+	}
+	else
+	{
+		*val = 0;
+		return false;
+	}
+	return true;
+}
+
 void BCMatrix::copy(float(&r)[4][4], float(&a)[4][4])
 {
 	/* destination comes first: */
@@ -108,7 +137,7 @@ void BCMatrix::get_matrix(float(&mat)[4][4]) const
 			mat[i][j] = matrix[i][j];
 }
 
-bool BCMatrix::in_range(BCMatrix other, float distance) const
+bool BCMatrix::in_range(const BCMatrix &other, float distance) const
 {
 	for (int i = 0; i < 4; i++) {
 		for (int j = 0; j < 4; j++) {
diff --git a/source/blender/collada/BCSampleData.h b/source/blender/collada/BCSampleData.h
index 3e49996d845..a3ebbee855c 100644
--- a/source/blender/collada/BCSampleData.h
+++ b/source/blender/collada/BCSampleData.h
@@ -26,10 +26,11 @@
 #ifndef __BC_SAMPLE_H__
 #define __BC_SAMPLE_H__
 
+#include <string>
 
 class BCSample {
-private:
 public:
+	virtual const bool get_value_for(const std::string &target, const int array_index, float *val) const = 0;
 };
 
 class BCMatrix: public BCSample {
@@ -57,7 +58,8 @@ public:
 	void get_matrix(float(&mat)[4][4]) const;
 	static void sanitize(float(&mat)[4][4], int precision);
 	static void transpose(float(&mat)[4][4]);
-	bool in_range(BCMatrix other, float distance) const;
+	bool in_range(const BCMatrix &other, const float distance) const;
+	const bool get_value_for(const std::string &target, const int array_index, float *val) const;
 
 	const float(&location() const)[3];
 	const float(&rotation() const)[3];



More information about the Bf-blender-cvs mailing list