custom extended vector type: e.g. float4 b = v.xxyz;

我只是一个虾纸丫 提交于 2019-12-02 15:53:06

问题


OpenCL, GCC, and Clang have convinent vector type extensions.

One of the features I like the best is the ability to do a swizzle like this:

float4 a(1,2,3,4);
float4 b = a.xxyw;  //1124

How can I make my own vector extensions to do this with e.g. MSVC as well? The best I have come up with is something that would do float4 b = a.xxyw() (see the code below) . So my main question is how it would be possible to do this without the () notation.

In case anyone is interested I came up with some code which creates all the permutations using defines

#define DEF_SWIZ4(a,b,c,d)  Vec4 a##b##c##d() const { return Vec4(a, b, c, d); }
#define DEF_SWIZ3(a,b,c)    DEF_SWIZ4(a,b,c,x) DEF_SWIZ4(a,b,c,y) DEF_SWIZ4(a,b,c,z) DEF_SWIZ4(a,b,c,w)
#define DEF_SWIZ2(a,b)      DEF_SWIZ3(a,b,x) DEF_SWIZ3(a,b,y) DEF_SWIZ3(a,b,z) DEF_SWIZ3(a,b,w)
#define DEF_SWIZ1(a)        DEF_SWIZ2(a,x) DEF_SWIZ2(a,y) DEF_SWIZ2(a,z) DEF_SWIZ2(a,w)
#define DEF_SWIZ()          DEF_SWIZ1(x) DEF_SWIZ1(y) DEF_SWIZ1(z) DEF_SWIZ1(w)

class Vec4
{
public:
    double x, y, z, w;
    Vec4() : x(0), y(0), z(0), w(0) {}
    Vec4(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {}
    DEF_SWIZ()
};

#include <iostream>

int main()
{
    Vec4 v(1, 2, 3, 4);
    Vec4 s = v.yyxw();
    std::cout << s.x << " " << s.y << " " << s.z << " " << s.w << std::endl;
}

来源:https://stackoverflow.com/questions/19923882/custom-extended-vector-type-e-g-float4-b-v-xxyz

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!