Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向量的移位维数3 问题:_C++_Performance_Glm Math_Simplify - Fatal编程技术网

C++ 向量的移位维数3 问题:

C++ 向量的移位维数3 问题:,c++,performance,glm-math,simplify,C++,Performance,Glm Math,Simplify,我想把一个三维向量的x,y和z的维数移动一个数字n vec3 shift(int n, vec3 vector); 如何改进算法以获得最佳性能并简化逻辑?我想这项任务有一个共同的方法,是吗 算法 vec3移位(整数维,vec3向量) { [3]中的浮点={Vector.x,Vector.y,Vector.z}; 浮出水面[3]; 对于(int i=0;i维度-1)n-=维度; out[i]=in[n]; } 返回vec3(out[0],out[1],out[2]); } 例子 例如shift

我想把一个三维向量的x,y和z的维数移动一个数字n

vec3 shift(int n, vec3 vector);
如何改进算法以获得最佳性能并简化逻辑?我想这项任务有一个共同的方法,是吗

算法
vec3移位(整数维,vec3向量)
{
[3]中的浮点={Vector.x,Vector.y,Vector.z};
浮出水面[3];
对于(int i=0;i<3;++i)
{
int n=i+尺寸;
而(n>维度-1)n-=维度;
out[i]=in[n];
}
返回vec3(out[0],out[1],out[2]);
}
例子 例如
shift(2,vec3(12,42,30))
应该给我
vec3(42,30,12)

  • 尺寸0是x,并保留值12。它被2移动到尺寸2,即z。所以z代表12
  • 尺寸1为y,可容纳42。移位2将导致维度4不存在,因此我们将其环绕,其值将成为维度0的值,即x
  • 尺寸2为z,也环绕边缘,其值位于y

    • 在您的情况下,您可以移动2次,第三次将是原来的。因此,与其为两个不同的班次生成一个通用函数,我建议在此基础上生成两个更简单的函数

      vec3 shiftOnce(vec3 Vector)
      {
          return vec3(Vector.z, Vector.x, Vector.y);
      }
      
      vec3 shiftTwice(vec3 Vector)
      {
          return vec3(Vector.y, Vector.z, Vector.x);
      }
      
      这将更快更容易阅读。我不是代码复制的朋友,但在像这样的小案例中,它只是首选的解决方案

      如果需要标注参数:

      vec3 shift(int dimension, vec3 v)
      {
          if(dimension % 3 == 1)      return vec3(v.z, v.x, v.y); // shift once 
          else if(dimension % 3 == 2) return vec3(v.y, v.z, v.x); // shift twice
          else                        return v;
      }
      

      您可以进一步优化算法:

      // copy a reference of your vector, is better than copy all it´s components.
         vec3 shift (int dimension, const vec3& Vector)
         {
             float in [3] = { Vector.x , Vector.y , Vector.z };
             float out [3];
      
             for (int i = 0; i < 3; i++)
                    out [ (i + dimension) % 3 ] = in [i];
             return vec3 (out [0], out [1], out [2])
         }
      
      因此,输出向量必须是: [1]中[2]中[0]=vec3(42,30,12)

      // copy a reference of your vector, is better than copy all it´s components.
         vec3 shift (int dimension, const vec3& Vector)
         {
             float in [3] = { Vector.x , Vector.y , Vector.z };
             float out [3];
      
             for (int i = 0; i < 3; i++)
                    out [ (i + dimension) % 3 ] = in [i];
             return vec3 (out [0], out [1], out [2])
         }
      
               out [(0 + 2) % 3] = in [0] => out [2%3] = in [0] => out [2] = in [0]
               z holds the value of the x component.
      
               out [(1 + 2) % 3] = in [1] => out [3%3] = in [1] => out [0] = in [1]
               x holds the value of the y component
      
               out [(2 + 2) % 3] = in [2] => out [4%3] = in [2] => out [1] = in [2]
               y holds the value of z component.