C++实现:根据旋转前后的向量求旋转矩阵

// 获取从一个向量转到另一个向量的矩阵

1

2

3

4

5

static AcGeVector3d CrossProduct(AcGeVector3d a, AcGeVector3d b);

static double DotProduct(AcGeVector3d a, AcGeVector3d b);

static double Absolute(AcGeVector3d v);

static AcGeMatrix3d RotationMatrix(double angle, AcGeVector3d u);

static AcGeMatrix3d CalculationMtrix(AcGeVector3d vectorBefore, AcGeVector3d vectorAfter);

// 封装函数:得到从一个向量转到另一个向量的矩阵

注:向量长度不能为0或两向量平行

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

AcGeVector3d CrossProduct(AcGeVector3d a, AcGeVector3d b)

{

       AcGeVector3d c;

       ///*c.x = a[1] * b[2] - a[2] * b[1];

       //c.y = a[2] * b[0] - a[0] * b[2];

       //c.z = a[0] * b[1] - a[1] * b[0];*/

       c.x = a.y * b.z - a.z * b.y;

       c.y = a.z * b.x - a.x * b.z;

       c.z = a.x * b.y - a.y * b.x;

       return c;

}

double DotProduct(AcGeVector3d a, AcGeVector3d b)

{

       double result;

       //result = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];

       result = a.x * b.x + a.y * b.y + a.z * b.z;

       return result;

}

double Absolute(AcGeVector3d v)

{

       double result;

       result = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);

       return result;

}

AcGeMatrix3d RotationMatrix(double angle, AcGeVector3d u)

{

       double norm = Absolute(u);

       u.x = u.x / norm;

       u.y = u.y / norm;

       u.z = u.z / norm;

       AcGeMatrix3d rotatinMatrix;

       rotatinMatrix.entry[0][0] = cos(angle) + u.x * u.x * (1 - cos(angle));

       rotatinMatrix.entry[0][1] = u.x * u.y * (1 - cos(angle)) - u.z * sin(angle);

       rotatinMatrix.entry[0][2] = u.y * sin(angle) + u.x * u.z * (1 - cos(angle));

       rotatinMatrix.entry[0][3] = 0.0;

       rotatinMatrix.entry[1][0] = u.z * sin(angle) + u.x * u.y * (1 - cos(angle));

       rotatinMatrix.entry[1][1] = cos(angle) + u.y * u.y * (1 - cos(angle));

       rotatinMatrix.entry[1][2] = -u.x * sin(angle) + u.y * u.z * (1 - cos(angle));

       rotatinMatrix.entry[1][3] = 0.0;

       rotatinMatrix.entry[2][0] = -u.y * sin(angle) + u.x * u.z * (1 - cos(angle));

       rotatinMatrix.entry[2][1] = u.x * sin(angle) + u.y * u.z * (1 - cos(angle));

       rotatinMatrix.entry[2][2] = cos(angle) + u.z * u.z * (1 - cos(angle));

       rotatinMatrix.entry[2][3] = 0.0;

       rotatinMatrix.entry[3][0] = 0.0;

       rotatinMatrix.entry[3][1] = 0.0;

       rotatinMatrix.entry[3][2] = 0.0;

       rotatinMatrix.entry[3][3] = 1.0;

       return rotatinMatrix;

}

AcGeMatrix3d CalculationMtrix(AcGeVector3d vectorBefore, AcGeVector3d vectorAfter)

{

       double rotationAngle = acos(DotProduct(vectorBefore, vectorAfter) / Absolute(vectorBefore) / Absolute(vectorAfter));

       AcGeVector3d rotationAxis = CrossProduct(vectorBefore, vectorAfter);

       AcGeMatrix3d rotationMatrix = RotationMatrix(rotationAngle, rotationAxis);

       return rotationMatrix;

}

模型矩阵变换:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

                        // 平移矩阵

                            AcGeMatrix3d transMatx;

                            transMatx.setToTranslation(100,100,0);

                            // 缩放矩阵

                            AcGeMatrix3d scaleMatx1;

                            scaleMatx1.setToScaling(0.5, AcGePoint3d(0, 0, 0));

                            // 旋转矩阵

                            AcGeVector3d vecOrign(1,0,0);

                            vecOrign.normalize();

                            AcGeVector3d vecTarget(12,25,10);

                            vecTarget.normalize();

                            AcGeMatrix3d rotMatx = CLib_Global::CalculationMtrix(vecOrign,vecTarget);

                           

                            // 复合矩阵

                            AcGeMatrix3d compMatx1;

                            compMatx1 = scaleMatx1.preMultBy(rotMatx);

                            compMatx1 = compMatx1.preMultBy(transMatx);

                            AcGeMatrix3d  entityMatrix = compMatx1;

                            ……………………

  • 26
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值