mirror of
https://github.com/blender/blender
synced 2026-11-22 03:45:13 +03:00
The main goal is to bring a bit of structure to the use of quaternions in the Cycles kernel. Prior to this change there was no dedicated quaternion type and float4 was used instead, and quaternions are stored as (x, y, z, w) matching naming between the quaternion and float4 fields. However, Blender uses (w, x, y, z) quaternion order for attributes, which is different from what Cycles uses. Without dedicated type this either leads to different quaternion orders depending whether it comes from attribute, or makes it intrinsically not possible to use implicit sharing. This change introduces Cycles Quaternion type which is compatible with Blender attribute math::Quaternion in both layout and alignment, making it possible to benefit from implicit sharing and use a nice structure in the kernel. This change does not modify the existing quaternion usage in the kernel which is currently used for transform decomposition and interpolation. There is currently no SIMD for the Quaternion type, which allows to avoid any special alignment requirement and share attributes with Blender, but performance might not be ideal. This attribute type will be used for representing gaussian splat rotation. The attribute access and interpolation matches behavior prior to this change. Added some basic tests for quaternion attribute access for RGB and alpha, mesh and volume attributes. Ref #159470 Pull Request: https://projects.blender.org/blender/blender/pulls/163342
142 lines
4.2 KiB
C++
142 lines
4.2 KiB
C++
/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
|
|
|
/* Note: These fixtures test default micro-architecture optimization defined in the
|
|
* util/optimization.h. */
|
|
|
|
#include "util/math_quaternion.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "util/log.h"
|
|
#include "util/transform.h"
|
|
|
|
#include <iostream>
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
using testing::Not;
|
|
|
|
/* Use as:
|
|
* Quaternion q1;
|
|
* Quaternion q2;
|
|
* EXPECT_THAT(q1, IsNearQuaternion(q2, eps)); */
|
|
MATCHER_P2(IsNearQuaternion, expected, eps, "")
|
|
{
|
|
if (fabsf(arg.w - expected.w) > eps) {
|
|
*result_listener << "component w should be " << expected.w;
|
|
return false;
|
|
}
|
|
if (fabsf(arg.x - expected.x) > eps) {
|
|
*result_listener << "component x should be " << expected.x;
|
|
return false;
|
|
}
|
|
if (fabsf(arg.y - expected.y) > eps) {
|
|
*result_listener << "component y should be " << expected.y;
|
|
return false;
|
|
}
|
|
if (fabsf(arg.z - expected.z) > eps) {
|
|
*result_listener << "component z should be " << expected.z;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* TODO(sergey): Move to log.h? */
|
|
std::ostream &operator<<(std::ostream &os, const Quaternion &q)
|
|
{
|
|
os << "(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
|
|
return os;
|
|
}
|
|
|
|
class QuaternionTest : public ::testing::Test {
|
|
void SetUp() override
|
|
{
|
|
/* The micro-architecture check is not needed here, but use it here as a demonstration of how
|
|
* it can be implemented in a clear way. */
|
|
// GTEST_SKIP() << "Test skipped due to uarch capability";
|
|
}
|
|
};
|
|
|
|
/* Basic test for user-defined matcher. */
|
|
TEST_F(QuaternionTest, IsNearQuaternion)
|
|
{
|
|
const Quaternion identity = identity_quaternion();
|
|
const Quaternion other = make_quaternion(1.0f, 2.0f, 3.0f, 4.0f);
|
|
|
|
EXPECT_THAT(identity, IsNearQuaternion(identity, 1e-6f));
|
|
EXPECT_THAT(other, Not(IsNearQuaternion(identity, 1e-6f)));
|
|
}
|
|
|
|
TEST_F(QuaternionTest, Negate)
|
|
{
|
|
EXPECT_THAT(-make_quaternion(1.0f, -2.0f, 3.0f, -4.0f),
|
|
IsNearQuaternion(make_quaternion(-1.0f, 2.0f, -3.0f, 4.0), 1e-6f));
|
|
}
|
|
|
|
TEST_F(QuaternionTest, MultiplyScalar)
|
|
{
|
|
EXPECT_THAT(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f) * 0.5f,
|
|
IsNearQuaternion(make_quaternion(0.5f, -1.0f, 1.5f, -2.0f), 1e-6f));
|
|
EXPECT_THAT(0.5f * make_quaternion(1.0f, -2.0f, 3.0f, -4.0f),
|
|
IsNearQuaternion(make_quaternion(0.5f, -1.0f, 1.5f, -2.0f), 1e-6f));
|
|
}
|
|
|
|
TEST_F(QuaternionTest, Add)
|
|
{
|
|
EXPECT_THAT(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f) +
|
|
make_quaternion(0.1f, -0.2f, 0.3f, -0.4f),
|
|
IsNearQuaternion(make_quaternion(1.1f, -2.2f, 3.3f, -4.4f), 1e-6f));
|
|
|
|
{
|
|
Quaternion a = make_quaternion(1.0f, -2.0f, 3.0f, -4.0f);
|
|
a += make_quaternion(0.1f, -0.2f, 0.3f, -0.4f);
|
|
EXPECT_THAT(a, IsNearQuaternion(make_quaternion(1.1f, -2.2f, 3.3f, -4.4f), 1e-6f));
|
|
}
|
|
}
|
|
|
|
TEST_F(QuaternionTest, Subtract)
|
|
{
|
|
EXPECT_THAT(make_quaternion(1.1f, -2.2f, 3.3f, -4.4f) -
|
|
make_quaternion(0.1f, -0.2f, 0.3f, -0.4f),
|
|
IsNearQuaternion(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f), 1e-6f));
|
|
|
|
{
|
|
Quaternion a = make_quaternion(1.1f, -2.2f, 3.3f, -4.4f);
|
|
a -= make_quaternion(0.1f, -0.2f, 0.3f, -0.4f);
|
|
EXPECT_THAT(a, IsNearQuaternion(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f), 1e-6f));
|
|
}
|
|
}
|
|
|
|
TEST_F(QuaternionTest, dot)
|
|
{
|
|
EXPECT_NEAR(
|
|
dot(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f), make_quaternion(0.1f, -0.2f, 0.3f, -0.4f)),
|
|
3.0f,
|
|
1e-6f);
|
|
}
|
|
|
|
TEST_F(QuaternionTest, normalized)
|
|
{
|
|
EXPECT_THAT(normalized(make_quaternion(1.0f, 0.0f, 0.0f, 0.0f)),
|
|
IsNearQuaternion(make_quaternion(1.0f, 0.0f, 0.0f, 0.0f), 1e-6f));
|
|
|
|
EXPECT_THAT(
|
|
normalized(make_quaternion(1.2f, 2.3f, 3.4f, 4.5f)),
|
|
IsNearQuaternion(make_quaternion(0.193297f, 0.370486f, 0.547675f, 0.724864f), 1e-5f));
|
|
}
|
|
|
|
TEST_F(QuaternionTest, lerp)
|
|
{
|
|
EXPECT_THAT(
|
|
lerp(make_quaternion(1.2f, 2.3f, 3.4f, 4.5f), make_quaternion(6.7f, 8.9f, 1.0f, 2.3f), 0.2f),
|
|
IsNearQuaternion(make_quaternion(0.349109f, 0.549467f, 0.443217f, 0.616253f), 1e-5f));
|
|
EXPECT_THAT(
|
|
lerp(
|
|
make_quaternion(1.2f, 2.3f, 3.4f, 4.5f), -make_quaternion(6.7f, 8.9f, 1.0f, 2.3f), 0.2f),
|
|
IsNearQuaternion(make_quaternion(0.349109f, 0.549467f, 0.443217f, 0.616253f), 1e-5f));
|
|
}
|
|
|
|
CCL_NAMESPACE_END
|