XDSoft 发表于 2013-5-10 23:14:14

和晓东一起学矩阵(2)--不同坐标系之间的矩阵变换

本次教学讲解如何将一个文本,按照文本包围盒的”中心点“贴到另外一个坐标系的指定点。(从平面文字帖到一个楼房的立面上)

1、得到文本实体的实体坐标系(坐标原点是文本的左下点,X轴是文本的输入方向,Y轴是文本的高度方向)

如图:





(setq coord_from ($xdlsp_entity_GetEcsCoordSystem e1))
;;-->((183.7 355.744 0.0) (0.939228 -0.343294 0.0) (0.343294 0.939228 0.0) (0.0 0.0 1.0))


因为我们要对齐中心,所以我们把源坐标系的原点修改为文本包围盒的中心点。


(setq box (xdrx_entity_box e1))
;|
         box=((183.562 336.023 0.0) (244.246 336.023 0.0) (244.246 376.614 0.0) (183.562 376.614 0.0))
|;
(setq pz (mapcar '+ (car box) (mapcar (lambda(x)(/ x 2.0))(mapcar '- (caddr box)(car box)))))
;|
         pz=(213.904 356.319 0.0)
|;
(setq coord_from (cons pz (cdr coord_from)))    ;;原点在文本中心的坐标系
;|
      coord_from=((213.904 356.319 0.0) (0.939228 -0.343294 0.0) (0.343294 0.939228 0.0) (0.0 0.0 1.0))
|;


2、求目标坐标系的原点,X轴,Y轴,Z轴

如图:





(setq vx (xdrx_vector_Normalize (mapcar '- px p0)))
;|
         vx=(0.9219 0.387428 0.0)
|;
(setq vz (xdrx_vector_negate (xdrx_vector_PerpVector vx)))
;|      X轴切向量得到-Z轴,-Z轴向量求反得到VZ
         vz=(-0.387428 0.9219 0.0)
|;
(setq vy (xdrx_vector_crossProduct vz vx))
;|      Z轴向量叉乘X轴向量得到Y轴
         vy=(0.0 0.0 1.0)
|;
(setq pj (getpoint))
;|
          pj=(324.434 383.65 20.0)
|;
(setq coord_to (list pj vx vy vz))
;|   目标坐标系
      coord_to=((324.434 383.65 20.0) (0.9219 0.387428 0.0) (-0.387428 0.9219 0.0) (0.0 0.0 1.0))
|;
(setq mat (xdrx_matrix_alignCoordSystem coord_from coord_to))
;|
       mat=((0.865874 -0.316483 0.387428 251.989) (0.363883 -0.133002 -0.9219 353.204) (0.343294 0.939228 0.0 -388.096) (0.0 0.0 0.0 1.0))
|;
(xdrx_entity_transformedCopy e1 mat)


测试命令TT




(defun c:tt (/ box coord_from coord_to e1 m_from mat p0 pj px pz vx vy vz x)
(if (and
      (setq e1 (car (xdrx_entsel "\n拾取文本<退出>:" '((0 . "text")))))
      (setq p0 (getpoint "\nX轴第一点<退出>:"))
      (setq px (getpoint "\nX轴第二点<退出>:"))
      (setq pj (getpoint "\n拾取插入点<退出>:"))
      )
    (progn
      (setq coord_from ($xdlsp_entity_getecscoordsystem e1))
      (setq box (xdrx_entity_box e1))
      (setq pz (mapcar
               '+
               (car box)
               (mapcar
                   '(lambda (x)
                     (/ x 2.0)
                   )
                   (mapcar
                     '-
                     (caddr box)
                     (car box)
                   )
               )
               )
      )
      (setq coord_from (cons pz (cdr coord_from)))
      (setq vx (xdrx_vector_normalize (mapcar
                                        '-
                                        px
                                        p0
                                    )
               )
      )
      (setq vz (xdrx_vector_negate (xdrx_vector_perpvector vx)))
      (setq vy (xdrx_vector_crossproduct vz vx))
      (setq coord_to (list pj vx vy vz))
      (setq mat (xdrx_matrix_aligncoordsystem coord_from coord_to))
      (xdrx_entity_transformedcopy e1 mat)
      (xdrx_entity_setcolor (entlast) 1)
    )
)
(princ)
)

守仁格竹GM 发表于 2013-5-11 13:02:29

收藏 以后用到了再看

lvbin2ooo 发表于 2013-5-17 22:11:07

非常感谢。。这个是非常好的东西

pengfei2010 发表于 2013-6-26 19:09:38

您好 正在学习矩阵,看到高飞鸟大师的以下代码 想了好几天都不明白!求教他也不在
;;;-----------------------------------------------------------;;
;;; 矩阵转置                                                ;;
;;; MAT:trp Transpose a matrix -Doug Wilson-                  ;;
;;; 输入:矩阵                                                ;;
;;; 输出:转置后的矩阵                                        ;;
;;;-----------------------------------------------------------;;
(defun MAT:trp (m)
(apply 'mapcar (cons 'list m))
)

这个函数无法运行,难道变量m不是表吗?

newer 发表于 2013-6-26 20:25:40

pengfei2010 发表于 2013-6-26 19:09
您好 正在学习矩阵,看到高飞鸟大师的以下代码 想了好几天都不明白!求教他也不在
;;;----------------- ...

参数M是表,但他是矩阵格式的表。4X4数组。

其实,你用XDRX_API的几个矩阵函数,更有助于你理解。

下面是一个单位矩阵的表的格式
命令: (xdrx_matrix_identity 3)
((1.0 0.0 0.0 0.0) (0.0 1.0 0.0 0.0) (0.0 0.0 1.0 0.0) (0.0 0.0 0.0 1.0))

pengfei2010 发表于 2013-6-27 08:17:34

newer 发表于 2013-6-26 20:25
参数M是表,但他是矩阵格式的表。4X4数组。

其实,你用XDRX_API的几个矩阵函数,更有助于你理解。


恩恩,明白了,呵呵谢谢您的回答,以后有问题就请教您哈,再次感谢

pengfei2010 发表于 2013-6-27 08:20:48

newer 发表于 2013-6-26 20:25
参数M是表,但他是矩阵格式的表。4X4数组。

其实,你用XDRX_API的几个矩阵函数,更有助于你理解。


好像不对呀!怎么是4X4呀?作为坐标空间应该是3x3吧,求解释

Highflybird 发表于 2013-6-27 09:51:57

pengfei2010 发表于 2013-6-27 08:20
好像不对呀!怎么是4X4呀?作为坐标空间应该是3x3吧,求解释

不管是LISP还是其他的语言,空间变换的矩阵都是4X4的。
请参考LISP的帮助文件:nentselp. entselp
另外请参考关于对奇次坐标的理解:
http://www.xdcad.net/forum/forum.php?mod=viewthread&tid=668439

zteykmgscqh 发表于 2014-1-3 07:23:34

学习一下啦

dbqtju 发表于 2015-5-11 18:33:17

谢谢。收藏 以后用到了再看

dnbc 发表于 2025-5-8 11:13:00

前来学习学习{:1_12:}{:1_12:}{:1_12:}
页: [1]
查看完整版本: 和晓东一起学矩阵(2)--不同坐标系之间的矩阵变换