home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / CAD08N06.ZIP / MATRIX.LSP < prev    next >
Lisp/Scheme  |  1993-04-29  |  5KB  |  143 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; CADENCE 6/93   ADVANCED AUTOLISP CONCEPTS
  3. ;; Bill Kramer
  4. ;;
  5. ;; Matrix Math with AutoLISP
  6. ;; Listing 1: Multiplication
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;; M_COL_MULT column matrix multiplicator.  This function takes a
  9. ;; MxN matrix and multiplies a Nx1 matrix or column matrix.  The
  10. ;; typical use of this function is to transform point coordinates
  11. ;; through a transformation matrix.
  12. ;; Result is a Nx1 matrix or nil if the matrices do not match in size.
  13. ;;
  14. (defun M_COL_MULT (A B / U)
  15.   (if (= (length B) (length (car A))) ;;check row size of A against B
  16.    (mapcar ;; returns list with length equal to number of rows in A
  17.     '(lambda (U) ;; U is each row in A as supplied by mapcar
  18.        (apply '+ ;; sum the result of...
  19.          (mapcar '* U B) ;;multiply row from A [in U] by B
  20.        )
  21.     )
  22.     A) ;;parameter for first MAPCAR
  23.   )
  24. )
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26. ;; M_MN_MULT  Multiply MxN matrix by NxM matrix returning a NxN
  27. ;; square matrix.  N must be greater than 1.  This function is
  28. ;; used to merge transformation matrices into a single one.
  29. ;;
  30. ;; result is NxN matrix multiplication or nil if matrices are not
  31. ;;        the right size to multiply.
  32. ;;
  33. (defun M_MN_MULT (A B / U V)
  34.   (if (= (length B) (length (car A))) ;;#rows in B = #cols in A?
  35.      (progn
  36.        (setq B (M_REV B))
  37.        (mapcar '(lambda (U)
  38.           (mapcar '(lambda (V)
  39.             (apply '+
  40.               (mapcar '* U V))) B)) A)
  41.      )
  42.   )
  43. )
  44. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  45. ;; Listing 2: Reverse and Add/subtract
  46. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  47. ;; M_REV  reverses MxN storage order of matrix.  Normal default is to
  48. ;; store matrices in row order.  This routine takes a row order matrix
  49. ;; and returns one with column order, or visa versa.
  50. ;;
  51. (defun M_REV (A / N U V)
  52.   (setq N 0)
  53.   (repeat (length A)
  54.      (setq U (cons (mapcar '(lambda (V) (nth N V)) A) U)
  55.            N (1+ N))
  56.   )
  57.   (reverse U)
  58. )
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60. ;; M_ADD  Addition/subtraction of same sized matrices
  61. ;; op is either '+ or '-
  62. ;; result is matrix result of addition
  63. ;;        or nil if matrices A and B do not match in size.
  64. (defun M_ADD (A B OP)
  65.  (if (and (member OP (list '+ '-)) ;;check valid operation
  66.           (= (length A) (length B))) ;;check #rows for match
  67.     (if (and
  68.           (listp (car A)) ;;check for N>1 condition
  69.           (listp (car B)) ;;both must be N>1
  70.           (= (length (car A)) (length (car B)))) ;;match 1st column count
  71.       (mapcar '(lambda (U V)
  72.          (mapcar OP U V)) A B) ;;nested MxN with N>1
  73.       (if (and (numberp (car A)) (numberp (car B))) ;;else, N=1?
  74.          (mapcar OP A B) ;;single columns[N=1]
  75.       )
  76.     )
  77.  )
  78. )
  79. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  80. ;; Listing 3:  Block Extrema Example for Matrix math
  81. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  82. ;;
  83. (defun MEXAMPLE (EL / ESAVE BP SX SY SZ RT EN X1 X2)
  84.   (setq ESAVE (cdr (assoc -1 EL))
  85.         TRMX (M_MX_BLK (cdr (assoc 10 EL))
  86.                        (cdr (assoc 41 EL))
  87.                        (cdr (assoc 42 EL))
  88.                        (cdr (assoc 43 EL))
  89.                        (cdr (assoc 50 EL))
  90.              )
  91.         EL (tblsearch "BLOCK" (cdr (assoc 2 EL)))
  92.         EN (cdr (assoc -2 EL))
  93.         X1 (list 99999.9999 99999.9999 99999.9999)
  94.         X2 (list -99999.9999 -99999.9999 -99999.9999)
  95.   )
  96.   ;; reads through block looking at group 10,11,12,13 points
  97.   ;; to find the max and min block def points.
  98.   (while EN
  99.     (setq EL (entget EN)
  100.           P1 10)
  101.     (while (assoc P1 EL)
  102.       (setq BP (cdr (assoc P1 EL))
  103.             BP (list (car BP) (cadr BP) (caddr BP) 1.0)
  104.             NP (m_col_mult TRMX BP)
  105.             NP (list (car NP) (cadr NP) (caddr NP))
  106.             EL (subst (cons P1 NP) (assoc P1 EL) EL)
  107.             P1 (1+ P1)
  108.             X1 (list
  109.                  (min (car X1) (car NP))
  110.                  (min (cadr X1) (cadr NP))
  111.                  (min (caddr X1) (caddr NP))
  112.                )
  113.             X2 (list
  114.                  (max (car X2) (car NP))
  115.                  (max (cadr X2) (cadr NP))
  116.                  (max (caddr X2) (caddr NP))
  117.                )
  118.       )
  119.     )
  120.     (setq EN (entnext EN))
  121.   )
  122.   (list X1 X2)
  123. )
  124. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  125. ;; M_MX_BLK Creates matrix to multiple local point list by to obtain
  126. ;; a world coordinate point. Used when transforming coordinates inside
  127. ;; a block definition or similar structure.
  128. ;;
  129. ;; MATRIX is always 4x4
  130. ;;   IP  base point of block insertion [translation point]
  131. ;;   SX..SZ  scaling factors along the X,Y,Z axes.
  132. ;;   RT  rotation about Z axis in radians.
  133. ;;
  134. (defun M_MX_BLK (IP SX SY SZ RT)
  135.   (list
  136.     (list (* SX (cos RT)) (* SY -1.0 (sin RT)) 0.0 (car IP))
  137.     (list (* SX (sin RT)) (* SY (cos RT))      0.0 (cadr IP))
  138.     (list 0.0             0.0                  SZ  (caddr IP))
  139.     (list 0.0             0.0                  0.0 1.0)
  140.   )
  141. )
  142. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  143.