查看: 3770|回复: 0

[二次开发源码] UG NX二次开发源码分享:矩阵操作的基础算法代码

[复制链接]

4

主题

3

回帖

48

积分

管理员

积分
48
发表于 2014-8-25 15:14:54 | 显示全部楼层 |阅读模式

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

×

9 w8 @$ f* @+ G% S8 x1 q
" q9 s. E5 q* `1 x8 M, K; t3 `
/ `+ N) y% @$ h1 ?! q#include <math.h>
( D7 U& p8 ?& C//矩阵复制4 m- _  E, _1 v1 s. ~! s: D" C
void Project_Matrix_Copy(double from_mtx[4][4], double to_mtx[4][4])
  l1 P" `2 L% m& S{7 u& X, J3 `7 |+ R, ~- [  ^! \
        int i,j;. ?% B: E6 G; }9 _! R% e( X, \  r
1 l, b5 P6 ]+ ~; _
        for(i=0;i<4;i++)
, B. d' x& v$ t' J4 v2 s                for(j=0;j<4;j++)3 y! M* \3 Z% k% W
                        to_mtx[i][j]=from_mtx[i][j];, J2 t! m5 M9 {+ B3 g& J
}
" g& |4 d- N4 Z//矩阵初始化
4 I8 \/ r) u( ]4 Z/ x; cvoid Project_Matrix_Identity(double mtx[4][4])
' Y8 Q1 e% M% m( I8 o' E, g1 V2 K{
% H. x, a' |+ s8 W        int i,j;: c' L, [- J2 q+ F  P
        " h; F6 x' @8 D  |0 w& T% j" d
        for(i=0;i<4;i++)  v' Z6 C, f( w
                for(j=0;j<4;j++)
0 ~& C" u/ ^' t/ @; v2 a0 a" E& g9 B                        if(i==j)
, Y: g' p! c6 @+ h                                mtx[i][j]=1.0;/ y9 y6 L+ A2 \+ }6 o/ n0 G* K
                        else
) T  j: O, U3 t* D                                mtx[i][j]=0.0;- o3 R0 A- j3 W! R9 |
}
/ `4 V# K9 ?: ?8 U' Y) D//矩阵相乘
% x$ }7 h( j1 Avoid Project_Matrix_Product(double left_mtx[4][4],double right_mtx[4][4],double get_mtx[4][4])* J4 O! H8 ]! @% h" b4 j
{) d2 ?! @- S" a

! ^. H% x, A, P7 J$ ?; I# G# y, E        int i,j,k;
$ m* l7 j# W/ S8 ?0 g& r( n- c        double left[4][4],right[4][4];
6 i. }6 Y7 E. c9 h" [6 i# Z0 ~! V' d+ H3 ?9 U! Z
        Project_Matrix_Copy(left_mtx,left);
  K1 r# I0 |6 L3 s. P( i# {0 P        Project_Matrix_Copy(right_mtx,right);
7 J- E4 _( U& z, C2 ?5 v        for(i=0;i<4;i++)
+ N) D% ?6 R1 G% O$ K! I3 j                for(j=0;j<4;j++)* X' L' [- y6 @% m3 \  k  k; z% l
                {! |, H. N" M  x' N8 C! j
                        get_mtx[i][j]=0.0;
( e# Q0 ^' P" A4 K                        for(k=0;k<4;k++)' y2 ]8 u( {1 i8 K
                                get_mtx[i][j]+=left[i][k]*right[k][j];9 O! D/ r, g7 x3 a2 R; L* N6 m" s, H
                }. G; X6 |8 T) h/ y
}
1 u- j! N/ x' T) i, q//转置矩阵
8 z" n1 u3 L8 H; k" g2 Rvoid Project_MatrixGet_Transpose(double mtx[4][4],double transpose_mtx[4][4])
% N) _: _( y: a/ j1 L1 B" y5 m! z{# M. d- d4 o9 ^
        int i,j;$ h+ f3 _3 |9 |2 E% M
        for(i=0;i<4;i++)# K# P+ S2 D! }5 j
        {
3 j& g# E5 Z9 b: m                for(j=0;j<4;j++)
0 K% f0 z+ Q$ M2 O, l- l                {, r+ U2 D! K6 U/ w0 k+ r
                        transpose_mtx[i][j]=mtx[j][i];/ ]1 L) k- m7 P% m9 P# ~" _- M
                }
5 j  N" Y0 g- y6 g        }, P8 C% H. z2 l  Y
}" o- b6 ?6 ^# U( f7 h
//从11元组获取变换矩阵$ F  i+ b$ C1 L9 r4 G1 d
void Project_Matrix_Get_From_Move(double translation[3],double point[3],double rotation[3],double angle,double pos[4][4])& B& K0 _5 j" j* p6 |6 p8 m+ I5 ~
{
4 r; @5 V; h" Y
) T( T- I( w. Y        //Project_Matrix_Get_Rotation(rotation,point,angle,pos);
6 N" n  e/ \, U' X( v, n8 N1 g        int i,j;- ~1 L! M2 _7 i7 _% o/ l, f
        for(i=0;i<4;i++). \+ ^8 _7 v! W( D; n6 u- }' {
        for(j=0;j<4;j++)
: O0 @3 {, ]9 z% t0 {. a; Q, g                if(i==j)3 I0 q7 e% C7 N: w' w$ \! T
                        pos[i][j]=1.0;" U3 o, m' A3 }2 U
                else
. V3 Z5 O& \+ j( L% Y3 Y$ `$ W                        pos[i][j]=0.0;8 n# R1 v# H4 ~/ F7 J: G: S
        pos[3][0]+=translation[0];
) z+ \+ W6 k+ u% U* c9 }        pos[3][1]+=translation[1];
0 y" B. B- T5 Q9 \* O0 d        pos[3][2]+=translation[2];
0 n  C. t9 S; L  A$ R+ s3 t}. T" R3 o6 l# l5 p6 n9 j
//向量缩放8 G2 A+ O1 B+ P7 ]; d
void Project_Vector_Scale(double a[3],double scale,double b[3])
& e* c1 O# Z# v, y( |  F' g& N{
. l; i  j. [7 E7 `        b[0]=a[0]*scale;' F# E3 ]# Q& w% B
        b[1]=a[1]*scale;
8 \5 x" o. p4 d" q2 g' m8 ~# N        b[2]=a[2]*scale;
$ f1 Z$ d: q1 V. c8 Y: s/ {5 {}
1 |7 T9 V: q- @+ [; n/ v/ q, Z" o0 t9 ?7 p
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.doteam.tech
回复

使用道具 举报

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

本版积分规则

在本版发帖
关注公众号
QQ客服返回顶部