Alembic: Renamed create_rotation_matrix to create_swapped_rotation_matrix and more:
authorSybren A. Stüvel <sybren@stuvel.eu>
Wed, 5 Apr 2017 13:05:24 +0000 (15:05 +0200)
committerSybren A. Stüvel <sybren@stuvel.eu>
Thu, 6 Apr 2017 14:47:01 +0000 (16:47 +0200)
Also replaced the bool param "to_yup" with "AbcAxisSwapMode mode", so that
it's more explicit that axes are swapped.

Also added unittests for create_swapped_rotation_matrix.

source/blender/alembic/intern/abc_util.cc
source/blender/alembic/intern/abc_util.h
tests/gtests/CMakeLists.txt
tests/gtests/alembic/CMakeLists.txt [new file with mode: 0644]
tests/gtests/alembic/abc_matrix_test.cc [new file with mode: 0644]

index 1536c4752812fe0debccaf068b1913bb9f750c20..87ba0fc9cc83151cebb49e314c89a58a248c2f1e 100644 (file)
@@ -132,15 +132,28 @@ void split(const std::string &s, const char delim, std::vector<std::string> &tok
        }
 }
 
-/* Create a rotation matrix for each axis from euler angles.
- * Euler angles are swaped to change coordinate system. */
-static void create_rotation_matrix(
+void create_swapped_rotation_matrix(
         float rot_x_mat[3][3], float rot_y_mat[3][3],
-        float rot_z_mat[3][3], const float euler[3], const bool to_yup)
+        float rot_z_mat[3][3], const float euler[3],
+        AbcAxisSwapMode mode)
 {
        const float rx = euler[0];
-       const float ry = (to_yup) ?  euler[2] : -euler[2];
-       const float rz = (to_yup) ? -euler[1] :  euler[1];
+       float ry;
+       float rz;
+
+       /* Apply transformation */
+       switch(mode) {
+               case ABC_ZUP_FROM_YUP:
+                       ry = -euler[2];
+                       rz = euler[1];
+                       break;
+               case ABC_YUP_FROM_ZUP:
+                       ry = euler[2];
+                       rz = -euler[1];
+                       break;
+               default:
+                       BLI_assert(false);
+       }
 
        unit_m3(rot_x_mat);
        unit_m3(rot_y_mat);
@@ -190,12 +203,11 @@ void copy_m44_axis_swap(float dst_mat[4][4], float src_mat[4][4], AbcAxisSwapMod
        mat3_to_eulO(euler, ROT_MODE_XYZ, src_rot);
 
        /* Create X, Y, Z rotation matrices from euler angles. */
-       create_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
-                              mode == ABC_YUP_FROM_ZUP);
+       create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler, mode);
 
        /* Concatenate rotation matrices. */
-       mul_m3_m3m3(dst_rot, dst_rot, rot_y_mat);
        mul_m3_m3m3(dst_rot, dst_rot, rot_z_mat);
+       mul_m3_m3m3(dst_rot, dst_rot, rot_y_mat);
        mul_m3_m3m3(dst_rot, dst_rot, rot_x_mat);
 
        mat3_to_eulO(euler, ROT_MODE_XYZ, dst_rot);
index 92d2fcae7a939aeed8ebf10bc4e1ef208178c894..6be86056e45e875fc3740718aacc2844fe63aee8 100644 (file)
@@ -155,6 +155,13 @@ typedef enum {
        ABC_YUP_FROM_ZUP = 2,
 } AbcAxisSwapMode;
 
+/* Create a rotation matrix for each axis from euler angles.
+ * Euler angles are swaped to change coordinate system. */
+void create_swapped_rotation_matrix(
+        float rot_x_mat[3][3], float rot_y_mat[3][3],
+        float rot_z_mat[3][3], const float euler[3],
+        AbcAxisSwapMode mode);
+
 void copy_m44_axis_swap(float dst_mat[4][4], float src_mat[4][4], AbcAxisSwapMode mode);
 
 /* *************************** */
index 1d363f31119f38c2b0f5746e80b78fe3721cd88e..ad77b1389f625cef1ee0bcfdb399a219278681ee 100644 (file)
@@ -14,5 +14,7 @@ if(WITH_GTESTS)
        add_subdirectory(blenlib)
        add_subdirectory(guardedalloc)
        add_subdirectory(bmesh)
+       if(WITH_ALEMBIC)
+               add_subdirectory(alembic)
+       endif()
 endif()
-
diff --git a/tests/gtests/alembic/CMakeLists.txt b/tests/gtests/alembic/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d4a1e1f
--- /dev/null
@@ -0,0 +1,43 @@
+# ***** 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.
+#
+# The Original Code is Copyright (C) 2014, Blender Foundation
+# All rights reserved.
+#
+# Contributor(s): Sybren A. Stüvel
+#
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+       .
+       ..
+       ../../../source/blender/blenlib
+       ../../../source/blender/alembic
+       ${ALEMBIC_INCLUDE_DIRS}
+       ${BOOST_INCLUDE_DIR}
+       ${HDF5_INCLUDE_DIRS}
+       ${OPENEXR_INCLUDE_DIRS}
+)
+
+include_directories(${INC})
+
+setup_libdirs()
+get_property(BLENDER_SORTED_LIBS GLOBAL PROPERTY BLENDER_SORTED_LIBS_PROP)
+
+# For motivation on doubling BLENDER_SORTED_LIBS, see ../bmesh/CMakeLists.txt
+BLENDER_SRC_GTEST(abc_matrix "abc_matrix_test.cc" "${BLENDER_SORTED_LIBS};${BLENDER_SORTED_LIBS}")
+
+setup_liblinks(abc_matrix_test)
diff --git a/tests/gtests/alembic/abc_matrix_test.cc b/tests/gtests/alembic/abc_matrix_test.cc
new file mode 100644 (file)
index 0000000..bbdcd46
--- /dev/null
@@ -0,0 +1,150 @@
+#include "testing/testing.h"
+
+// Keep first since utildefines defines AT which conflicts with fucking STL
+#include "intern/abc_util.h"
+
+extern "C" {
+#include "BLI_utildefines.h"
+#include "BLI_math.h"
+}
+
+
+#define EXPECT_M3_NEAR(a, b, eps) {\
+       EXPECT_V3_NEAR(a[0], b[0], eps); \
+       EXPECT_V3_NEAR(a[1], b[1], eps); \
+       EXPECT_V3_NEAR(a[2], b[2], eps); \
+}
+
+TEST(abc_matrix, CreateRotationMatrixY_YfromZ) {
+       // Input variables
+       float rot_x_mat[3][3];
+       float rot_y_mat[3][3];
+       float rot_z_mat[3][3];
+       float euler[3] = {0.f, M_PI_4, 0.f};
+
+       // Construct expected matrices
+       float unit[3][3];
+       float rot_z_min_quart_pi[3][3];  // rotation of -pi/4 radians over z-axis
+
+       unit_m3(unit);
+       unit_m3(rot_z_min_quart_pi);
+       rot_z_min_quart_pi[0][0] = M_SQRT1_2;
+       rot_z_min_quart_pi[0][1] = -M_SQRT1_2;
+       rot_z_min_quart_pi[1][0] = M_SQRT1_2;
+       rot_z_min_quart_pi[1][1] = M_SQRT1_2;
+
+       // Run tests
+       create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
+                                      ABC_YUP_FROM_ZUP);
+
+       EXPECT_M3_NEAR(rot_x_mat, unit, 1e-5f);
+       EXPECT_M3_NEAR(rot_y_mat, unit, 1e-5f);
+       EXPECT_M3_NEAR(rot_z_mat, rot_z_min_quart_pi, 1e-5f);
+}
+
+TEST(abc_matrix, CreateRotationMatrixZ_YfromZ) {
+       // Input variables
+       float rot_x_mat[3][3];
+       float rot_y_mat[3][3];
+       float rot_z_mat[3][3];
+       float euler[3] = {0.f, 0.f, M_PI_4};
+
+       // Construct expected matrices
+       float unit[3][3];
+       float rot_y_quart_pi[3][3];  // rotation of pi/4 radians over y-axis
+
+       unit_m3(unit);
+       unit_m3(rot_y_quart_pi);
+       rot_y_quart_pi[0][0] = M_SQRT1_2;
+       rot_y_quart_pi[0][2] = -M_SQRT1_2;
+       rot_y_quart_pi[2][0] = M_SQRT1_2;
+       rot_y_quart_pi[2][2] = M_SQRT1_2;
+
+       // Run tests
+       create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
+                                      ABC_YUP_FROM_ZUP);
+
+       EXPECT_M3_NEAR(rot_x_mat, unit, 1e-5f);
+       EXPECT_M3_NEAR(rot_y_mat, rot_y_quart_pi, 1e-5f);
+       EXPECT_M3_NEAR(rot_z_mat, unit, 1e-5f);
+}
+
+TEST(abc_matrix, CreateRotationMatrixXYZ_YfromZ) {
+       // Input variables
+       float rot_x_mat[3][3];
+       float rot_y_mat[3][3];
+       float rot_z_mat[3][3];
+       // in degrees: X=10, Y=20, Z=30
+       float euler[3] = {0.1745329201221466f, 0.3490658104419708f, 0.5235987901687622f};
+
+       // Construct expected matrices
+       float rot_x_p10[3][3];  // rotation of +10 degrees over x-axis
+       float rot_y_p30[3][3];  // rotation of +30 degrees over y-axis
+       float rot_z_m20[3][3];  // rotation of -20 degrees over z-axis
+
+       unit_m3(rot_x_p10);
+       rot_x_p10[1][1] =  0.9848077297210693f;
+       rot_x_p10[1][2] =  0.1736481785774231f;
+       rot_x_p10[2][1] = -0.1736481785774231f;
+       rot_x_p10[2][2] =  0.9848077297210693f;
+
+       unit_m3(rot_y_p30);
+       rot_y_p30[0][0] =  0.8660253882408142f;
+       rot_y_p30[0][2] = -0.5f;
+       rot_y_p30[2][0] =  0.5f;
+       rot_y_p30[2][2] =  0.8660253882408142f;
+
+       unit_m3(rot_z_m20);
+       rot_z_m20[0][0] =  0.9396926164627075f;
+       rot_z_m20[0][1] = -0.3420201241970062f;
+       rot_z_m20[1][0] =  0.3420201241970062f;
+       rot_z_m20[1][1] =  0.9396926164627075f;
+
+       // Run tests
+       create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
+                                      ABC_YUP_FROM_ZUP);
+
+       EXPECT_M3_NEAR(rot_x_mat, rot_x_p10, 1e-5f);
+       EXPECT_M3_NEAR(rot_y_mat, rot_y_p30, 1e-5f);
+       EXPECT_M3_NEAR(rot_z_mat, rot_z_m20, 1e-5f);
+}
+
+TEST(abc_matrix, CreateRotationMatrixXYZ_ZfromY) {
+       // Input variables
+       float rot_x_mat[3][3];
+       float rot_y_mat[3][3];
+       float rot_z_mat[3][3];
+       // in degrees: X=10, Y=20, Z=30
+       float euler[3] = {0.1745329201221466f, 0.3490658104419708f, 0.5235987901687622f};
+
+       // Construct expected matrices
+       float rot_x_p10[3][3];  // rotation of +10 degrees over x-axis
+       float rot_y_m30[3][3];  // rotation of -30 degrees over y-axis
+       float rot_z_p20[3][3];  // rotation of +20 degrees over z-axis
+
+       unit_m3(rot_x_p10);
+       rot_x_p10[1][1] =  0.9848077297210693f;
+       rot_x_p10[1][2] =  0.1736481785774231f;
+       rot_x_p10[2][1] = -0.1736481785774231f;
+       rot_x_p10[2][2] =  0.9848077297210693f;
+
+       unit_m3(rot_y_m30);
+       rot_y_m30[0][0] =  0.8660253882408142f;
+       rot_y_m30[0][2] =  0.5f;
+       rot_y_m30[2][0] = -0.5f;
+       rot_y_m30[2][2] =  0.8660253882408142f;
+
+       unit_m3(rot_z_p20);
+       rot_z_p20[0][0] =  0.9396926164627075f;
+       rot_z_p20[0][1] =  0.3420201241970062f;
+       rot_z_p20[1][0] = -0.3420201241970062f;
+       rot_z_p20[1][1] =  0.9396926164627075f;
+
+       // Run tests
+       create_swapped_rotation_matrix(rot_x_mat, rot_y_mat, rot_z_mat, euler,
+                                      ABC_ZUP_FROM_YUP);
+
+       EXPECT_M3_NEAR(rot_x_mat, rot_x_p10, 1e-5f);
+       EXPECT_M3_NEAR(rot_y_mat, rot_y_m30, 1e-5f);
+       EXPECT_M3_NEAR(rot_z_mat, rot_z_p20, 1e-5f);
+}