Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1|回复: 0
打印 上一主题 下一主题

[代码|Code]四元数≒矩阵|Quaternions≒Matrix

[复制链接]

189

主题

189

帖子

580

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
580
跳转到指定楼层
楼主
发表于 2017-7-14 16:37:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
by Akatsuki

  1. // 四元数转矩阵[] Quaternions To Matrix
  2. void QuatMatrix(const float in[4], float out[4][4])
  3. {
  4.     float xx, xy, xz, xw;
  5.     float yy, yz, yw;
  6.     float zz, zw;

  7.     xx = in[0]*in[0];
  8.     xy = in[0]*in[1];
  9.     xz = in[0]*in[2];
  10.     xw = in[0]*in[3];
  11.     yy = in[1]*in[1];
  12.     yz = in[1]*in[2];
  13.     yw = in[1]*in[3];
  14.     zz = in[2]*in[2];
  15.     zw = in[2]*in[3];

  16.     out[0][0] = 1-2*(yy+zz);
  17.     out[0][1] =   2*(xy-zw);
  18.     out[0][2] =   2*(xz+yw);
  19.     out[0][3] =   0;
  20.     out[1][0] =   2*(xy+zw);
  21.     out[1][1] = 1-2*(xx+zz);
  22.     out[1][2] =   2*(yz-xw);
  23.     out[1][3] =   0;
  24.     out[2][0] =   2*(xz-yw);
  25.     out[2][1] =   2*(yz+xw);
  26.     out[2][2] = 1-2*(xx+yy);
  27.     out[2][3] =   0;
  28.     out[3][0] =   0;
  29.     out[3][1] =   0;
  30.     out[3][2] =   0;
  31.     out[3][3] =   1;
  32. }

  33. // 矩阵转四元数[] Matrix To Quaternions
  34. void MatrixQuat(const float in[4][4], float out[4])
  35. {
  36.     float s;

  37.     if (in[0][0]>in[1][1] && in[0][0]>in[2][2])
  38.     {
  39.         s = 2*sqrt(1.0+in[0][0]-in[1][1]-in[2][2]);
  40.         out[0] = 0.25*s;
  41.         out[1] = (in[1][0]+in[0][1])/s;
  42.         out[2] = (in[0][2]+in[2][0])/s;
  43.         out[3] = (in[2][1]-in[1][2])/s;
  44.     }
  45.     else if (in[1][1]>in[2][2])
  46.     {
  47.         s = 2*sqrt(1.0+in[1][1]-in[0][0]-in[2][2]);
  48.         out[0] = (in[1][0]+in[0][1])/s;
  49.         out[1] = 0.25*s;
  50.         out[2] = (in[2][1]+in[1][2])/s;
  51.         out[3] = (in[0][2]-in[2][0])/s;
  52.     }
  53.     else
  54.     {
  55.         s = 2*sqrt(1.0+in[2][2]-in[0][0]-in[1][1]);
  56.         out[0] = (in[0][2]+in[2][0])/s;
  57.         out[1] = (in[2][1]+in[1][2])/s;
  58.         out[2] = 0.25*s;
  59.         out[3] = (in[1][0]-in[0][1])/s;
  60.     }
  61. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Comsenz Inc.  

GMT+8, 2026-10-26 07:01 , Processed in 0.228931 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表