forked from blender/blender
main sync #3
@ -79,6 +79,9 @@ template<typename T> uint64_t vector_hash(const T &vec)
|
||||
|
||||
template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size> {
|
||||
|
||||
BLI_STATIC_ASSERT(alignof(T) <= sizeof(T),
|
||||
"VecBase is not compatible with aligned type for now.");
|
||||
|
||||
static constexpr int type_length = Size;
|
||||
|
||||
using base_type = T;
|
||||
@ -176,16 +179,36 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
|
||||
|
||||
/** Swizzling. */
|
||||
|
||||
template<BLI_ENABLE_IF_VEC(Size, >= 3)> VecBase<T, 2> xy() const
|
||||
template<BLI_ENABLE_IF_VEC(Size, >= 2)> VecBase<T, 2> xy() const
|
||||
{
|
||||
return *reinterpret_cast<const VecBase<T, 2> *>(this);
|
||||
}
|
||||
|
||||
template<BLI_ENABLE_IF_VEC(Size, >= 4)> VecBase<T, 3> xyz() const
|
||||
template<BLI_ENABLE_IF_VEC(Size, >= 3)> VecBase<T, 2> yz() const
|
||||
{
|
||||
return *reinterpret_cast<const VecBase<T, 2> *>(&((*this)[1]));
|
||||
}
|
||||
|
||||
template<BLI_ENABLE_IF_VEC(Size, >= 4)> VecBase<T, 2> zw() const
|
||||
{
|
||||
return *reinterpret_cast<const VecBase<T, 2> *>(&((*this)[2]));
|
||||
}
|
||||
|
||||
template<BLI_ENABLE_IF_VEC(Size, >= 3)> VecBase<T, 3> xyz() const
|
||||
{
|
||||
return *reinterpret_cast<const VecBase<T, 3> *>(this);
|
||||
}
|
||||
|
||||
template<BLI_ENABLE_IF_VEC(Size, >= 4)> VecBase<T, 3> yzw() const
|
||||
{
|
||||
return *reinterpret_cast<const VecBase<T, 3> *>(&((*this)[1]));
|
||||
}
|
||||
|
||||
template<BLI_ENABLE_IF_VEC(Size, >= 4)> VecBase<T, 4> xyzw() const
|
||||
{
|
||||
return *reinterpret_cast<const VecBase<T, 4> *>(this);
|
||||
}
|
||||
|
||||
#undef BLI_ENABLE_IF_VEC
|
||||
|
||||
/** Conversion from pointers (from C-style vectors). */
|
||||
|
@ -259,4 +259,25 @@ TEST(math_vec_types, DivideFloatByVectorSmall)
|
||||
EXPECT_FLOAT_EQ(result.y, 1.0f);
|
||||
}
|
||||
|
||||
TEST(math_vec_types, SwizzleReinterpret)
|
||||
{
|
||||
const float2 v01(0, 1);
|
||||
const float2 v12(1, 2);
|
||||
const float2 v23(2, 3);
|
||||
const float3 v012(0, 1, 2);
|
||||
const float3 v123(1, 2, 3);
|
||||
const float4 v0123(0, 1, 2, 3);
|
||||
/* Identity. */
|
||||
EXPECT_EQ(v01.xy(), v01);
|
||||
EXPECT_EQ(v012.xyz(), v012);
|
||||
EXPECT_EQ(v0123.xyzw(), v0123);
|
||||
/* Masking. */
|
||||
EXPECT_EQ(v012.xy(), v01);
|
||||
EXPECT_EQ(v0123.xyz(), v012);
|
||||
/* Offset. */
|
||||
EXPECT_EQ(v0123.yz(), v12);
|
||||
EXPECT_EQ(v0123.zw(), v23);
|
||||
EXPECT_EQ(v0123.yzw(), v123);
|
||||
}
|
||||
|
||||
} // namespace blender::tests
|
||||
|
Loading…
Reference in New Issue
Block a user