有没有一种简单的方法可以在C中替换它,而无需使用大型外部库?

时间:2010-08-30 14:11:54

标签: c

TransformationMatrices[mesh_count-1][0] = n->mTransformation.a1; 
TransformationMatrices[mesh_count-1][1] = n->mTransformation.a2;
TransformationMatrices[mesh_count-1][2] = n->mTransformation.a3;
TransformationMatrices[mesh_count-1][3] = n->mTransformation.a4;
TransformationMatrices[mesh_count-1][4] = n->mTransformation.b1;
TransformationMatrices[mesh_count-1][5] = n->mTransformation.b2;
TransformationMatrices[mesh_count-1][6] = n->mTransformation.b3;
TransformationMatrices[mesh_count-1][7] = n->mTransformation.b4;
TransformationMatrices[mesh_count-1][8] = n->mTransformation.c1;
TransformationMatrices[mesh_count-1][9] = n->mTransformation.c2;
TransformationMatrices[mesh_count-1][10] = n->mTransformation.c3;
TransformationMatrices[mesh_count-1][11] = n->mTransformation.c4;
TransformationMatrices[mesh_count-1][12] = n->mTransformation.d1;
TransformationMatrices[mesh_count-1][13] = n->mTransformation.d2;
TransformationMatrices[mesh_count-1][14] = n->mTransformation.d3;
TransformationMatrices[mesh_count-1][15] = n->mTransformation.d4;

编辑:替换为替换为较短的格式。

2 个答案:

答案 0 :(得分:1)

嗯,你有你的选择,

  1. 一种依赖的简单方法 实现 - 定义布局规则。 或
  2. 一种非常复杂的跨平台方式,但代码更多 比你已经有了。
  3. 方法1)会是这样的:

     double *dest = &TransformationMatrices[mesh_count-1][0];
     double *src = &n->mTransformation.a1;    
    
     memcpy(dest, src, sizeof(double) * 16);
    

    这取决于各种未定义的行为,应该工作.....

答案 1 :(得分:1)

我不完全确定我会关注这个问题,但您可以使用预处理器来缩短这一点。例如,

int WriteIndex = 0;
#define WRITE_MATRIX(prefix) \
    do { \
        TransformationMatrices[mesh_count-1][WriteIndex++] = n->mTransformation.prefix ## 1; \
        TransformationMatrices[mesh_count-1][WriteIndex++] = n->mTransformation.prefix ## 2; \
        TransformationMatrices[mesh_count-1][WriteIndex++] = n->mTransformation.prefix ## 3; \
        TransformationMatrices[mesh_count-1][WriteIndex++] = n->mTransformation.prefix ## 4; \
    } while(0)
WRITE_MATRIX(a);
WRITE_MATRIX(b);
WRITE_MATRIX(c);
WRITE_MATRIX(d);

我不会用这种方式编写代码,但它演示了这种技术......

相关问题