標籤:

【越飛越高講堂15】用LISP論矩陣

本帖最後由 highflybir 於 2013-1-9 12:51 編輯 用LISP論矩陣矩陣的LISP程序,國內討論的比較少, 而國外的研究比較深入。經過長時間的收藏和探索,我綜合成了這篇帖子。這個帖子裡面的函數主要是跟CAD 相關。附件包含了本帖完整的lisp代碼,還有測試樣例。是一個比較完整的矩陣庫。另外,本帖附上了一些矩陣相關鏈接。錯誤和紕漏之處請大家多多指教。樣例及演示可以參考12樓,另外附件中也有樣例。

Matrix-Lib.LSP(50.58 KB, 下載次數: 81, 售價: 1 個明經幣)一、向量的運算向量,矩陣是息息相關的。這裡列出了向量的一些基本運算。普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;兩向量相加addition;;

  3. ;;;Input:v1,v2-vectorsinR^n;;

  4. ;;;OutPut:Avector;;

  5. ;;;-----------------------------------------------------------;;

  6. (defunMAT:v+v(v1v2)

  7. (mapcar"+v1v2)

  8. )

  9. ;;;-----------------------------------------------------------;;

  10. ;;;兩向量相減subtraction;;

  11. ;;;Input:v1,v2-vectorsinR^n;;

  12. ;;;OutPut:Avector;;

  13. ;;;-----------------------------------------------------------;;

  14. (defunMAT:v-v(v1v2)

  15. (mapcar"-v1v2)

  16. )

  17. ;;;-----------------------------------------------------------;;

  18. ;;;兩向量相乘multiplication;;

  19. ;;;Input:v1,v2-vectorsinR^n;;

  20. ;;;OutPut:Avector;;

  21. ;;;-----------------------------------------------------------;;

  22. (defunMAT:v*v(v1v2)

  23. (mapcar"*v1v2)

  24. )

  25. ;;;-----------------------------------------------------------;;

  26. ;;;兩向量相除division;;

  27. ;;;Input:v1,v2-vectorsinR^n;;

  28. ;;;OutPut:Avector;;

  29. ;;;-----------------------------------------------------------;;

  30. (defunMAT:v/v(v1v2)

  31. (mapcar"/v1v2)

  32. )

  33. ;;;-----------------------------------------------------------;;

  34. ;;;向量乘標量(係數);;

  35. ;;;VectorxScalar-LeeMac;;

  36. ;;;Args:v-vectorinR^n,s-realscalar;;

  37. ;;;-----------------------------------------------------------;;

  38. (defunMAT:vxs(vs)

  39. (mapcar(function(lambda(n)(*ns)))v)

  40. )

  41. ;;;-----------------------------------------------------------;;

  42. ;;;兩向量的點積;;

  43. ;;;VectorDotProduct;;

  44. ;;;Input:v1,v2-vectorsinR^n;;

  45. ;;;-----------------------------------------------------------;;

  46. (defunMAT:Dot(v1v2)

  47. (apply"+(mapcar"*v1v2))

  48. )

  49. ;;;-----------------------------------------------------------;;

  50. ;;;兩向量的叉積;;

  51. ;;;VectorCrossProduct;;

  52. ;;;Args:u,v-vectorsinR^3;;

  53. ;;;-----------------------------------------------------------;;

  54. (defunMAT:vxv(uv)

  55. (list

  56. (-(*(cadru)(caddrv))(*(cadrv)(caddru)))

  57. (-(*(carv)(caddru))(*(caru)(caddrv)))

  58. (-(*(caru)(cadrv))(*(carv)(cadru)))

  59. )

  60. )

  61. ;;;-----------------------------------------------------------;;

  62. ;;;線性組合標量組乘向量組;;

  63. ;;;Linearcombination-highflybird;;

  64. ;;;Input:Vectors-vectors,Scalars,-arealnumberlist;;

  65. ;;;Output:avector;;

  66. ;;;-----------------------------------------------------------;;

  67. (defunMAT:SxVs(VectorsScalars)

  68. (apply"mapcar(cons"+(mapcar"MAT:vxsVectorsScalars)))

  69. )

  70. ;;;-----------------------------------------------------------;;

  71. ;;;向量的模(長度);;

  72. ;;;VectorNorm-LeeMac;;

  73. ;;;Args:v-vectorinR^n;;

  74. ;;;-----------------------------------------------------------;;

  75. (defunMAT:norm(v)

  76. (sqrt(apply"+(mapcar"*vv)))

  77. )

  78. ;;;-----------------------------------------------------------;;

  79. ;;;向量的模(長度);;

  80. ;;;VectorNorm-highflybird;;

  81. ;;;Args:v-vectorinR^3;;

  82. ;;;-----------------------------------------------------------;;

  83. (defunMAT:Norm3D(v)

  84. (distance"(000)v)

  85. )

  86. ;;;-----------------------------------------------------------;;

  87. ;;;單位向量;;

  88. ;;;UnitVector-LeeMac;;

  89. ;;;Args:v-vectorinR^n;;

  90. ;;;-----------------------------------------------------------;;

  91. (defunMAT:Unitization(v)

  92. ((lambda(n)

  93. (if(equal0.0n1e-14)

  94. nil

  95. (MAT:vxsv(/1.0n))

  96. )

  97. )

  98. (MAT:normv)

  99. )

  100. )

  101. ;;;-----------------------------------------------------------;;

  102. ;;;單位向量;;

  103. ;;;UnitVector-highflybird;;

  104. ;;;Args:v-vectorinR^3;;

  105. ;;;-----------------------------------------------------------;;

  106. (defunMAT:unit(v/l)

  107. (cond

  108. ((=(setql(MAT:Norm3Dv))1.0)v)

  109. ((>l1e-14)(MAT:vxsv(/1.0l)))

  110. )

  111. )

  112. ;;;-----------------------------------------------------------;;

  113. ;;;兩個2d向量的叉積的數值;;

  114. ;;;輸入:兩個點(或者兩個向量);;

  115. ;;;輸出:一個數值.如果為正則是逆時針,兩向量形成的平面法線向量;;

  116. ;;;向上,為負則是順時針,為零則兩向量共線或平行。;;

  117. ;;;這個數值也為原點,P1,P2三點面積的兩倍。;;

  118. ;;;-----------------------------------------------------------;;

  119. (defunMAT:Det2V(v1v2)

  120. (-(*(carv1)(cadrv2))(*(carv2)(cadrv1)))

  121. )

二、向量的旋轉普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;旋轉一個向量或者點90度;;

  3. ;;;輸入:一個向量;;

  4. ;;;輸出:被旋轉90度後的向量;;

  5. ;;;-----------------------------------------------------------;;

  6. (defunMAT:Rot90(vec)

  7. (vl-list*(-(cadrvec))(carvec)(cddrvec))

  8. )

  9. ;;;-----------------------------------------------------------;;

  10. ;;;旋轉向量到指定角度;;

  11. ;;;輸入:一個向量和指定的角度;;

  12. ;;;輸出:被旋轉後的向量;;

  13. ;;;-----------------------------------------------------------;;

  14. (defunMAT:Rot2D(va/csxy)

  15. (setqc(cosa)s(sina))

  16. (setqx(carv)y(cadrv))

  17. (list(-(*xc)(*ys))(+(*xs)(*yc)))

  18. )

三、 行列式這裡只討論二階和三階的行列式普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;2d行列式determinantinR^2;;

  3. ;;;Args:4numbers;;

  4. ;;;-----------------------------------------------------------;;

  5. (defunMAT:Det2(x1y1x2y2)

  6. (-(*x1y2)(*x2y1))

  7. )

  8. ;;;-----------------------------------------------------------;;

  9. ;;;3d行列式determinantinR^3;;

  10. ;;;Args:9numbers;;

  11. ;;;-----------------------------------------------------------;;

  12. (defunMAT:Det3(a1b1c1a2b2c2a3b3c3)

  13. (+(*a1(-(*b2c3)(*b3c2)))

  14. (*a2(-(*b3c1)(*b1c3)))

  15. (*a3(-(*b1c2)(*b2c1)))

  16. )

  17. )

四、 矩陣的基本運算普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;矩陣轉置;;

  3. ;;;MAT:trpTransposeamatrix-DougWilson-;;

  4. ;;;輸入:矩陣;;

  5. ;;;輸出:轉置後的矩陣;;

  6. ;;;-----------------------------------------------------------;;

  7. (defunMAT:trp(m)

  8. (apply"mapcar(cons"listm))

  9. )

  10. ;;;-----------------------------------------------------------;;

  11. ;;;矩陣相加;;

  12. ;;;Matrix+Matrix-LeeMac;;

  13. ;;;Args:m,n-nxnmatrices;;

  14. ;;;-----------------------------------------------------------;;

  15. (defunMAT:m+m(mn)

  16. (mapcar"(lambda(rs)(mapcar"+rs))mn)

  17. )

  18. ;;;-----------------------------------------------------------;;

  19. ;;;矩陣相減;;

  20. ;;;Matrix-Matrix-LeeMac;;

  21. ;;;Args:m,n-nxnmatrices;;

  22. ;;;-----------------------------------------------------------;;

  23. (defunMAT:m-m(mn)

  24. (mapcar"(lambda(rs)(mapcar"-rs))mn)

  25. )

  26. ;;;-----------------------------------------------------------;;

  27. ;;;矩陣相乘;;

  28. ;;;MAT:mxmMultiplytwomatrices-VladimirNesterovsky-;;

  29. ;;;-----------------------------------------------------------;;

  30. (defunMAT:mxm(mq)

  31. (mapcar(function(lambda(r)(MAT:mxv(MAT:trpq)r)))m)

  32. )

  33. ;;;-----------------------------------------------------------;;

  34. ;;;矩陣乘標量;;

  35. ;;;MatrixxScalar-LeeMac;;

  36. ;;;Args:m-nxnmatrix,n-realscalar;;

  37. ;;;-----------------------------------------------------------;;

  38. (defunMAT:mxs(ms)

  39. (mapcar(function(lambda(v)(MAT:VxSvs)))m)

  40. )

五、 矩陣與向量的運算普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;向量或點的矩陣變換(向量乘矩陣);;

  3. ;;;MatrixxVector-VladimirNesterovsky;;

  4. ;;;Args:m-nxnmatrix,v-vectorinR^n;;

  5. ;;;-----------------------------------------------------------;;

  6. (defunMAT:mxv(mv)

  7. (mapcar(function(lambda(r)(apply"+(mapcar"*rv))))m)

  8. )

  9. ;;;-----------------------------------------------------------;;

  10. ;;;點的矩陣(4x4matrix)變換;;

  11. ;;;輸入:矩陣m和一個三維點p;;

  12. ;;;輸出:點變換後的位置;;

  13. ;;;-----------------------------------------------------------;;

  14. (defunMAT:mxp(mp)

  15. (reverse(cdr(reverse(MAT:mxvm(appendp"(1.0))))))

  16. )

六、矩陣的平面和空間變換以下是一些矩陣變換的函數。平移變換普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;平移變換矩陣方式1;;

  3. ;;;參數:;;

  4. ;;;v-位移矢量;;

  5. ;;;-----------------------------------------------------------;;

  6. ;;;---------------=={TranslatebyMatrix}==-----------------;;

  7. ;;;;;

  8. ;;;TranslationMatrix;;

  9. ;;;-----------------------------------------------------------;;

  10. ;;;Author:highflybird,Copyright?2012;;

  11. ;;;-----------------------------------------------------------;;

  12. ;;;Arguments:;;

  13. ;;;v-Displacementvectorbywhichtotranslate;;

  14. ;;;-----------------------------------------------------------;;

  15. (defunMAT:Translation(v)

  16. (list

  17. (list1.0.0.(carv))

  18. (list0.1.0.(cadrv))

  19. (list0.0.1.(caddrv))

  20. (list0.0.0.1.)

  21. )

  22. )

等比縮放變換普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;比例縮放矩陣;;

  3. ;;;參數:;;

  4. ;;;Cen-基點;;

  5. ;;;scale-縮放比例;;

  6. ;;;-----------------------------------------------------------;;

  7. ;;;-----------------=={ScalebyMatrix}==-------------------;;

  8. ;;;;;

  9. ;;;ScalingMatrix;;

  10. ;;;-----------------------------------------------------------;;

  11. ;;;Author:highflybird,Copyright?2012;;

  12. ;;;-----------------------------------------------------------;;

  13. ;;;Arguments:;;

  14. ;;;Cen-BasePointforScalingTransformation;;

  15. ;;;scale-ScaleFactorbywhichtoscaleobject;;

  16. ;;;-----------------------------------------------------------;;

  17. (defunMAT:Scaling(Censcale/s)

  18. (setqs(-1scale))

  19. (list

  20. (listscale0.0.(*s(carCen)))

  21. (list0.scale0.(*s(cadrCen)))

  22. (list0.0.scale(*s(caddrCen)))

  23. "(0.0.0.1.)

  24. )

  25. )

二維旋轉變換普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;二維旋轉變換矩陣;;

  3. ;;;參數:;;

  4. ;;;Cen-基點;;

  5. ;;;ang-旋轉角度;;

  6. ;;;-----------------------------------------------------------;;

  7. ;;;-----------------=={RotatebyMatrix}==------------------;;

  8. ;;;;;

  9. ;;;RotationMatrix;;

  10. ;;;-----------------------------------------------------------;;

  11. ;;;Author:highflybird,Copyright?2012;;

  12. ;;;-----------------------------------------------------------;;

  13. ;;;Arguments:;;

  14. ;;;Cen-BasePointforRotationTransformation;;

  15. ;;;ang-Anglethroughwhichtorotateobject;;

  16. ;;;-----------------------------------------------------------;;

  17. (defunMAT:Rotation(Cenang/csxy)

  18. (setqc(cosang)s(sinang))

  19. (setqx(carCen)y(cadrCen))

  20. (list

  21. (listc(-s)0.(-x(-(*cx)(*sy))))

  22. (listsc0.(-y(+(*sx)(*cy))))

  23. "(0.0.1.0.)

  24. "(0.0.0.1.)

  25. )

  26. )

三維旋轉變換普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;三維旋轉變換矩陣;;

  3. ;;;參數:;;

  4. ;;;Cen-基點;;

  5. ;;;Axis-旋轉軸;;

  6. ;;;ang-旋轉角;;

  7. ;;;-----------------------------------------------------------;;

  8. ;;;---------------=={3DRotatebyMatrix}==-----------------;;

  9. ;;;Author:highflybird.;;

  10. ;;;Arguments:;;

  11. ;;;Cen---Inputoriginpointofrotation;;

  12. ;;;Axis---Inputaxisvectorofrotation;;

  13. ;;;Ang---Inputangleofrotation;;

  14. ;;;-----------------------------------------------------------;;

  15. (defunMAT:Rotation3D(CenAxisAng/ABCDMNPxyz)

  16. (setqD(distance"(000)Axis))

  17. (if(or(<D1e-8)(zeropang))

  18. "((1.0.0.0.)(0.1.0.0.)(0.0.1.0.)(0.0.0.1.))

  19. (setqN(mapcar"/Axis(listDDD))

  20. x(carN)

  21. y(cadrN)

  22. z(caddrN)

  23. A(cosAng)

  24. B(sinAng)

  25. C(-1A)

  26. M(list(list(+A(*xxC))

  27. (-(*xyC)(*zB))

  28. (+(*yB)(*xzC))

  29. )

  30. (list(+(*zB)(*xyC))

  31. (+A(*yyC))

  32. (-(*yzC)(*xB))

  33. )

  34. (list(-(*xzC)(*yB))

  35. (+(*xB)(*yzC))

  36. (+A(*zzC))

  37. )

  38. )

  39. p(mapcar"-Cen(Mat:mxvMCen))

  40. M(Mat:DispToMatrixMp)

  41. )

  42. )

  43. )

二維鏡像變換普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;二維鏡像變換矩陣;;

  3. ;;;參數:;;

  4. ;;;p1-鏡像向量第一點;;

  5. ;;;p2-鏡像向量第二點;;

  6. ;;;-----------------------------------------------------------;;

  7. ;;;----------------=={ReflectbyMatrix}==------------------;;

  8. ;;;;;

  9. ;;;ReflectsaVLA-ObjectorPointListusinga;;

  10. ;;;TransformationMatrix;;

  11. ;;;-----------------------------------------------------------;;

  12. ;;;Author:LeeMac,Copyright?2010-<arel="nofollow"href="/plugin.php?id=laoyang_wailianx&url=http://www.lee-mac.com"target="_blank">www.lee-mac.com</a>;;

  13. ;;;-----------------------------------------------------------;;

  14. ;;;Arguments:;;

  15. ;;;target-VLA-ObjectorPointListtotransform;;

  16. ;;;p1,p2-Pointsrepresentingvectorinwhichtoreflect;;

  17. ;;;-----------------------------------------------------------;;

  18. (defunMAT:Reflect(p1p2/acsxy)

  19. (setqa(anglep1p2)a(+aa))

  20. (setqc(cosa)s(sina))

  21. (setqx(carp1)y(cadrp1))

  22. (list

  23. (listcs0.(-x(+(*cx)(*sy))))

  24. (lists(-c)0.(-y(-(*sx)(*cy))))

  25. "(0.0.1.0.)

  26. "(0.0.0.1.)

  27. )

  28. )

三維鏡像變換普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;三維鏡像變換矩陣;;

  3. ;;;參數:;;

  4. ;;;p1,p2,p3-三點定義的鏡像平面;;

  5. ;;;-----------------------------------------------------------;;

  6. ;;;---------------=={3DReflectbyMatrix}==----------------;;

  7. ;;;;;

  8. ;;;Reflectionmatrix;;

  9. ;;;-----------------------------------------------------------;;

  10. ;;;Author:highflybird,Copyright?2012-;;

  11. ;;;-----------------------------------------------------------;;

  12. ;;;Arguments:;;

  13. ;;;p1,p2,p3-Three3Dpointsdefiningthereflectionplane;;

  14. ;;;-----------------------------------------------------------;;

  15. (defunMAT:Reflect3D(p1p2p3/muxuyuz)

  16. (mapcar

  17. "set

  18. "(uxuyuz)

  19. (MAT:unit(MAT:vxv(mapcar"-p2p1)(mapcar"-p3p1)))

  20. )

  21. (setqm(list(list(-1.(*2.uxux))(*-2.uyux)(*-2.uxuz))

  22. (list(*-2.uxuy)(-1.(*2.uyuy))(*-2.uyuz))

  23. (list(*-2.uxuz)(*-2.uyuz)(-1.(*2.uzuz)))

  24. )

  25. )

  26. (Mat:DispToMatrixm(mapcar"-p1(MAT:mxvmp1)))

  27. )

附件中包含Lee-mac的一些演算法,與我的大同小異。區別在於,我的矩陣是為大量運算準備,Lee-mac的為單次次數不多時運用。 七、塊參照,屬性的變換矩陣和逆矩陣附件普通瀏覽複製代碼

  1. ;;;-----------------------------------------------------------;;

  2. ;;;功能:某點在塊內坐標系統和世界或者用戶坐標系統的轉換;;

  3. ;;;參數:pt要變換的點。;;

  4. ;;;rlst用nentselp或者nentsel得到的表的最後一項;;

  5. ;;;from坐標系:0,WCS;1,當前UCS;2,塊參照坐標系RCS;;

  6. ;;;to坐標系:0,WCS;1,當前UCS;2,塊參照坐標系RCS;;

  7. ;;;-----------------------------------------------------------;;

  8. ;;;MAT:TransNested(gile);;

  9. ;;;TranslatesapointcoordinatesfromWCSorUCStoRCS;;

  10. ;;;-coordinatessystemofa;;

  11. ;;;reference(xreforblock)whateveritsnestedlevel-;;

  12. ;;;;;

  13. ;;;Arguments;;

  14. ;;;pt:thepointtotranslate;;

  15. ;;;rlst:theparentsentitieslistfromthedeepestnested;;

  16. ;;;totheoneinsertedincurrentspace-sameas;;

  17. ;;;(last(nentsel))or(last(nentselp));;

  18. ;;;fromto:aswithtransfunction:0.WCS,1.UCS,2.RCS;;

  19. ;;;-----------------------------------------------------------;;

  20. (defunMAT:TransNested(ptrlstfromto/GEOM)

  21. (and(=1from)(setqpt(transpt10)))

  22. (and(=2to)(setqrlst(reverserlst)))

  23. (and(or(=2from)(=2to))

  24. (whilerlst

  25. (setqgeom(if(=2to)

  26. (MAT:RevRefGeom(carrlst))

  27. (MAT:RefGeom(carrlst))

  28. )

  29. rlst(cdrrlst)

  30. pt(mapcar"+(MAT:mxv(cargeom)pt)(cadrgeom))

  31. )

  32. )

  33. )

  34. (if(=1to)

  35. (transpt01)

  36. pt

  37. )

  38. )

  39. ;;;-----------------------------------------------------------;;

  40. ;;;功能:圖塊的變換矩陣;;

  41. ;;;輸入:塊參照的圖元名;;

  42. ;;;輸出:塊參照的變換矩陣;;

  43. ;;;-----------------------------------------------------------;;

  44. ;;;MAT:RefGeom(gile);;

  45. ;;;Returnsalistwhichfirstitemisa3x3transformation;;

  46. ;;;matrix(rotation,scalesnormal)andseconditemtheobject;;

  47. ;;;insertionpointinitsparent(xref,blocorspace);;

  48. ;;;;;

  49. ;;;Argument:anename;;

  50. ;;;-----------------------------------------------------------;;

  51. (defunMAT:RefGeom(ename/elstangnormmat)

  52. (setqelst(entgetename)

  53. ang(cdr(assoc50elst))

  54. norm(cdr(assoc210elst))

  55. )

  56. (list

  57. (setqmat

  58. (MAT:mxm

  59. (mapcar(function(lambda(v)(transv0normT)))

  60. "((1.00.00.0)(0.01.00.0)(0.00.01.0))

  61. )

  62. (MAT:mxm

  63. (list(list(cosang)(-(sinang))0.0)

  64. (list(sinang)(cosang)0.0)

  65. "(0.00.01.0)

  66. )

  67. (list(list(cdr(assoc41elst))0.00.0)

  68. (list0.0(cdr(assoc42elst))0.0)

  69. (list0.00.0(cdr(assoc43elst)))

  70. )

  71. )

  72. )

  73. )

  74. (mapcar

  75. "-

  76. (trans(cdr(assoc10elst))norm0)

  77. (MAT:mxvmat

  78. (cdr(assoc10(tblsearch"BLOCK"(cdr(assoc2elst)))))

  79. )

  80. )

  81. )

  82. )

  83. ;;;-----------------------------------------------------------;;

  84. ;;;功能:圖塊的變換矩陣的逆矩陣;;

  85. ;;;-----------------------------------------------------------;;

  86. ;;;MAT:RevRefGeom(gile);;

  87. ;;;MAT:RefGeominversefunction;;

  88. ;;;輸入:塊參照的圖元名;;

  89. ;;;輸出:塊參照的變換矩陣的逆矩陣;;

  90. ;;;-----------------------------------------------------------;;

  91. (defunMAT:RevRefGeom(ename/entDataangnormmat)

  92. (setqentData(entgetename)

  93. ang(-(cdr(assoc50entData)))

  94. 360docimg_501_norm(cdr(assoc210entData))
  95. 360docimg_502_)
  96. 360docimg_503_(list
  97. 360docimg_504_(setqmat
  98. 360docimg_505_(MAT:mxm
  99. 360docimg_506_(list(list(/1(cdr(assoc41entData)))0.00.0)
  100. 360docimg_507_(list0.0(/1(cdr(assoc42entData)))0.0)
  101. 360docimg_508_(list0.00.0(/1(cdr(assoc43entData))))
  102. 360docimg_509_)
  103. 360docimg_510_(MAT:mxm
  104. 360docimg_511_(list(list(cosang)(-(sinang))0.0)
  105. 360docimg_512_(list(sinang)(cosang)0.0)
  106. 360docimg_513_"(0.00.01.0)
  107. 360docimg_514_)
  108. 360docimg_515_(mapcar(function(lambda(v)(transvnorm0T)))
  109. 360docimg_516_"((1.00.00.0)(0.01.00.0)(0.00.01.0))
  110. 360docimg_517_)
  111. 360docimg_518_)
  112. 360docimg_519_)
  113. 360docimg_520_)
  114. 360docimg_521_(mapcar"-
  115. 360docimg_522_(cdr(assoc10(tblsearch"BLOCK"(cdr(assoc2entData)))))
  116. 360docimg_523_(MAT:mxvmat(trans(cdr(assoc10entData))norm0))
  117. 360docimg_524_)
  118. 360docimg_525_)
  119. 360docimg_526_)
  120. 360docimg_527_
  121. 360docimg_528_;;;-----------------------------------------------------------;;
  122. 360docimg_529_;;;屬性的變換矩陣AttribTransformationMatrix.-highflybird;;
  123. 360docimg_530_;;;輸入:Ename屬性的圖元名;;
  124. 360docimg_531_;;;輸出:屬性的變換矩陣;;
  125. 360docimg_532_;;;-----------------------------------------------------------;;
  126. 360docimg_533_(defunMAT:AttGeom(ename/angnormmatelst)
  127. 360docimg_534_(setqelst(entgetename)
  128. 360docimg_535_ang(cdr(assoc50elst))
  129. 360docimg_536_norm(cdr(assoc210elst))
  130. 360docimg_537_)
  131. 360docimg_538_(list
  132. 360docimg_539_(setqmat
  133. 360docimg_540_(mxm
  134. 360docimg_541_(mapcar(function(lambda(v)(transv0normT)))
  135. 360docimg_542_"((1.00.00.0)(0.01.00.0)(0.00.01.0))
  136. 360docimg_543_)
  137. 360docimg_544_(list(list(cosang)(-(sinang))0.0)
  138. 360docimg_545_(list(sinang)(cosang)0.0)
  139. 360docimg_546_"(0.00.01.0)
  140. 360docimg_547_)
  141. 360docimg_548_)
  142. 360docimg_549_)
  143. 360docimg_550_(trans(cdr(assoc10elst))norm0)
  144. 360docimg_551_)
  145. 360docimg_552_)

八、三點變換矩陣,UCS變換矩陣,圖元變換矩陣和通用變換矩陣普通瀏覽複製代碼

  1. 360docimg_553_;;;-----------------------------------------------------------;;
  2. 360docimg_554_;;;Appenddisplacementvectortoamatrix-Highflybird-;;
  3. 360docimg_555_;;;把位移矢量添加到矩陣中;;
  4. 360docimg_556_;;;輸入:mat--矩陣(3x3),disp--位移矢量;;
  5. 360docimg_557_;;;輸出:一個4X4的變換CAD的標準變換矩陣;;
  6. 360docimg_558_;;;-----------------------------------------------------------;;
  7. 360docimg_559_(defunMat:DispToMatrix(matdisp)
  8. 360docimg_560_(append
  9. 360docimg_561_(mapcar"appendmat(mapcar"listdisp))
  10. 360docimg_562_"((0.0.0.1.))
  11. 360docimg_563_)
  12. 360docimg_564_)
  13. 360docimg_565_
  14. 360docimg_566_;;;-----------------------------------------------------------;;
  15. 360docimg_567_;;;從一個坐標系統到另一個坐標系統的變換矩陣;;
  16. 360docimg_568_;;;輸入:from-源坐標系;to-目的坐標系;;
  17. 360docimg_569_;;;輸出:一個4X4的變換CAD的標準變換矩陣;;
  18. 360docimg_570_;;;-----------------------------------------------------------;;
  19. 360docimg_571_(defunMAT:Trans(fromto)
  20. 360docimg_572_(Mat:DispToMatrix
  21. 360docimg_573_(mapcar
  22. 360docimg_574_(function(lambda(v)(transvfromtot)))
  23. 360docimg_575_"((1.0.0.)(0.1.0.)(0.0.1.))
  24. 360docimg_576_)
  25. 360docimg_577_(trans"(000)tofrom)
  26. 360docimg_578_)
  27. 360docimg_579_)
  28. 360docimg_580_
  29. 360docimg_581_;;;-----------------------------------------------------------;;
  30. 360docimg_582_;;;wcs到ucs矩陣,也可稱UCS的變換矩陣;;
  31. 360docimg_583_;;;-----------------------------------------------------------;;
  32. 360docimg_584_(defunMAT:w2u()(MAT:Trans01))
  33. 360docimg_585_
  34. 360docimg_586_;;;-----------------------------------------------------------;;
  35. 360docimg_587_;;;ucs到wcs矩陣,也可稱UCS的逆變換矩陣;;
  36. 360docimg_588_;;;-----------------------------------------------------------;;
  37. 360docimg_589_(defunMAT:u2w()(MAT:Trans10))
  38. 360docimg_590_
  39. 360docimg_591_;;;-----------------------------------------------------------;;
  40. 360docimg_592_;;;通用變換矩陣byhighflybird;;
  41. 360docimg_593_;;;輸入:from-原坐標系,;;
  42. 360docimg_594_;;;to-目的坐標系,;;
  43. 360docimg_595_;;;Org-目的坐標系的原點相對原坐標系的位置;;
  44. 360docimg_596_;;;Ang-相對於原坐標系的旋轉角度;;
  45. 360docimg_597_;;;輸出:兩個矩陣,一個是從原坐標系變換到目的坐標系的變換矩陣;;
  46. 360docimg_598_;;;一個是從目的坐標系變換到原坐標系的變換矩陣;;
  47. 360docimg_599_;;;-----------------------------------------------------------;;
  48. 360docimg_600_(defunMAT:Trans1(fromtoOrgAng/MatRotInvCen)
  49. 360docimg_601_(setqMat(mapcar(function(lambda(v)(transvfromtoT)))
  50. 360docimg_602_"((1.0.0.)(0.1.0.)(0.0.1.))
  51. 360docimg_603_)
  52. 360docimg_604_)
  53. 360docimg_605_(if(not(equalang01e-14))
  54. 360docimg_606_(setqRot(list(list(cosang)(-(sinang))0.)
  55. 360docimg_607_(list(sinang)(cosang)0.)
  56. 360docimg_608_(list0.0.1.)
  57. 360docimg_609_)
  58. 360docimg_610_mat(MAT:mxmmatRot)
  59. 360docimg_611_)
  60. 360docimg_612_)
  61. 360docimg_613_(setqCen(transOrgtofrom))
  62. 360docimg_614_(setqInv(mat:trpmat))
  63. 360docimg_615_(list
  64. 360docimg_616_(Mat:DispToMatrixInv(mat:mxvInv(mapcar"-Cen)));from->to(transptfromto)
  65. 360docimg_617_(Mat:DispToMatrixmatCen);to->from(transpttofrom)
  66. 360docimg_618_)
  67. 360docimg_619_)
  68. 360docimg_620_
  69. 360docimg_621_;;;-----------------------------------------------------------;;
  70. 360docimg_622_;;;通過兩個坐標軸和坐標原點定義的變換矩陣-byhighflybird;;
  71. 360docimg_623_;;;輸入:Org-坐標系原點,;;
  72. 360docimg_624_;;;Vx-坐標系X方向,;;
  73. 360docimg_625_;;;Vy-坐標系y方向;;
  74. 360docimg_626_;;;輸出:兩個矩陣,一個是該坐標系的變換矩陣,一個是其逆矩陣;;
  75. 360docimg_627_;;;-----------------------------------------------------------;;
  76. 360docimg_628_(defunMAT:2VMatrix(OrgVxVy/VzRot)
  77. 360docimg_629_(if(or(equalVx"(000)1e-14)(equalVy"(000)1e-14))
  78. 360docimg_630_"((1.0.0.0.)(0.1.0.0.)(0.0.1.0.)(0.0.0.1.))
  79. 360docimg_631_(progn
  80. 360docimg_632_(setqVx(Mat:UnitVx));XAxis
  81. 360docimg_633_(setqVy(Mat:UnitVy));YAxis
  82. 360docimg_634_(setqVz(Mat:unit(MAT:vxvVxVy)));ZAxis
  83. 360docimg_635_(setqVy(Mat:unit(MAT:vxvVzVx)));YAxis
  84. 360docimg_636_(setqRot(listVxVyVz));Rotationmatrix
  85. 360docimg_637_(list;InverseRotationmatrix
  86. 360docimg_638_(Mat:DispToMatrix(MAT:trpRot)Org);ThetransformationmatrixfromUCStoWCS
  87. 360docimg_639_(Mat:DispToMatrixRot(MAT:mxvRot(mapcar"-Org)));ThetransformationmatrixfromWCStoUCS
  88. 360docimg_640_)
  89. 360docimg_641_)
  90. 360docimg_642_)
  91. 360docimg_643_)
  92. 360docimg_644_
  93. 360docimg_645_;;;-----------------------------------------------------------;;
  94. 360docimg_646_;;;Mat:3PMatrix-Highflybird-;;
  95. 360docimg_647_;;;通過兩個坐標軸和坐標原點定義的變換矩陣-byhighflybird;;
  96. 360docimg_648_;;;輸入:P1-坐標系原點,;;
  97. 360docimg_649_;;;P2-坐標系的第2點;;
  98. 360docimg_650_;;;P3-坐標系的第3點;;
  99. 360docimg_651_;;;輸出:兩個矩陣,一個是該坐標系的變換矩陣,一個是其逆矩陣;;
  100. 360docimg_652_;;;-----------------------------------------------------------;;
  101. 360docimg_653_(defunMat:3PMatrix(p1p2p3/v1v2v3)
  102. 360docimg_654_(MAT:2VMatrixP1(mapcar"-p2p1)(mapcar"-p3p1))
  103. 360docimg_655_)
  104. 360docimg_656_
  105. 360docimg_657_;;;-----------------------------------------------------------;;
  106. 360docimg_658_;;;平齊實體的變換矩陣-byhighflybird;;
  107. 360docimg_659_;;;輸入:Ent-實體名;;
  108. 360docimg_660_;;;輸出:平齊這個實體的變換矩陣和它的逆矩陣;;
  109. 360docimg_661_;;;-----------------------------------------------------------;;
  110. 360docimg_662_(defunMat:EntityMatrix(Ent/zdxfCenobjanm1matInvorg)
  111. 360docimg_663_(setqdxf(entgetent))
  112. 360docimg_664_(if(setqCen(cdr(assoc10dxf)));Insertpoint,centerorstartpoint,etc.
  113. 360docimg_665_(if(null(caddrCen))
  114. 360docimg_666_(setqCen(appendCen"(0.0)))
  115. 360docimg_667_)
  116. 360docimg_668_(setqCen"(000))
  117. 360docimg_669_)
  118. 360docimg_670_(setqobj(vlax-ename->vla-objectEnt))
  119. 360docimg_671_(if(and(vlax-property-available-pobj"elevation);Ifithaselevationvalue.
  120. 360docimg_672_(wcmatch(vla-get-objectnameobj)"*Polyline");It"sa"AcDb2dPolyline"or"AcDbPolyline"object
  121. 360docimg_673_)
  122. 360docimg_674_(setqz(vla-get-elevationobj)
  123. 360docimg_675_Cen(list(carCen)(cadrCen)(+(caddrCen)z));addelevationvalue
  124. 360docimg_676_)
  125. 360docimg_677_)
  126. 360docimg_678_(if(vlax-property-available-pobj"rotation);ifithasarotaionangle
  127. 360docimg_679_(setqan(vla-get-rotationobj))
  128. 360docimg_680_(setqan0)
  129. 360docimg_681_)
  130. 360docimg_682_(MAT:Trans10EntCenan);returntwomatrices,thefirstisWCS->OCS,thesecondisOCS->WCS
  131. 360docimg_683_)

九、軸測變換矩陣普通瀏覽複製代碼

  1. 360docimg_684_;;;-----------------------------------------------------------;;
  2. 360docimg_685_;;;通用的軸測變換矩陣highflybird2012.12;;
  3. 360docimg_686_;;;AxonometricprojectionsRotationmatrices;;
  4. 360docimg_687_;;;Isometricprojection:a=(/pi4),b=(atan(-(sqrt2)));;
  5. 360docimg_688_;;;Input:a-Rotationangleabouttheverticalaxis;;
  6. 360docimg_689_;;;b-Rotationangleaboutthehorizontalaxis;;
  7. 360docimg_690_;;;Output:transforamtionmatrixofthisprojection;;
  8. 360docimg_691_;;;-----------------------------------------------------------;;
  9. 360docimg_692_(defunMAT:ISO(ab/casacbsb)
  10. 360docimg_693_(setqca(cosa))
  11. 360docimg_694_(setqsa(sina))
  12. 360docimg_695_(setqcb(cosb))
  13. 360docimg_696_(setqsb(sinb))
  14. 360docimg_697_(list(listca(-sa)00)
  15. 360docimg_698_(list(*sacb)(*cacb)(-sb)0)
  16. 360docimg_699_(list(*sasb)(*casb)cb0)
  17. 360docimg_700_(list0001)
  18. 360docimg_701_)
  19. 360docimg_702_)

演示:360docimg_703_十、矩陣的特徵值和特徵向量chlh_jd討論比較深入,參見如下鏈接:http://bbs.mjtd.com/thread-99908-1-1.htmlhttp://www.theswamp.org/index.php?topic=43453.0十一、 矩陣的分解、求逆和解方程組http://www.theswamp.org/index.php?topic=32478.0http://www.theswamp.org/index.php?topic=22638.msg439381#msg439381http://www.theswamp.org/index.php?topic=13505.msg163485#msg163485關於上面的幾個鏈接地址的源代碼已經錄入下面的附件中了。360docimg_704_Matrix.rar(7.62 KB, 下載次數: 90)
推薦閱讀:

C++矩陣處理庫
gal2mat:將gal權重文件轉成n-by-n矩陣
74. Search a 2D Matrix
語料庫語言學基礎知識:矩陣(Haskell版)
矩陣(三)

TAG:矩陣 |