Author Topic: Transformation matrix vs Acads matrix  (Read 15660 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Transformation matrix vs Acads matrix
« Reply #15 on: February 06, 2007, 02:44:31 AM »
Hi Tim,

I'm not sure to understand what you want to do with 'ename->4x4Matrix', but it seams to be : return the same 4X4 matrix as the one returned by 'nentselp' on a non nested entity in a reference (block or xref).

Here's the way I do it. Note I'd rather deal with 3X3 matrices than 4X4 matrices for scales and rotations, and then add a fourth column (deplacement) and the last row (0.0 0.0 0.0 1.0).

As said Joe, if the reference is non uniformaly scaled, the matrix shouldn't be used with 'vla-TransformBy'.

I purpose also another way to get OCS axis using 'trans'.

(mapcar   '(lambda (v) (trans v ename 0 T))
         '((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
)
returns the same thing as
(SCO (cdr (assoc 210 entData)))

EDIT: I change 'ename' argument to 'norm' in the (trans ...) expression, because it didn't work with nested entities in 'testCopyObject'.

Code: [Select]
;; Multiply two matrices by Vladimir Nesterovsky
;; Arguments : two matrices
;; Return : the matrices combination
;; Attention : (mxm mat1 mat2) is not equal to (mxm mat2 mat1)
(defun mxm (m q)
  (mapcar '(lambda (r)
     (mapcar '(lambda (l) (apply '+ (mapcar '* l r)))
     (apply 'mapcar (cons 'list q))
     )
   )
  m
  )
)


(defun ename->4x4Matrix (ename / entData ang norm mat)
  (setq entData (entget ename))
  (setq ang (- (cdr (assoc 50 entData))))
  (setq norm (cdr (assoc 210 entData)))
 
  ;; a 3X3 matrix as this returned by (butlast (caddr (nentsel)))
  (setq mat (mxm
      ;; scale
      (list (list (cdr (assoc 41 entData)) 0.0 0.0)
    (list 0.0 (cdr (assoc 42 entData)) 0.0)
    (list 0.0 0.0 (cdr (assoc 43 entData)))
      )
      (mxm
;; rotation on Z axis ...
(list (list (cos ang) (- (sin ang)) 0.0)
      (list (sin ang) (cos ang) 0.0)
      '(0.0 0.0 1.0)
)
;; ... of OCS
(mapcar '(lambda (v) (trans v norm 0 T))
'((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
)
      )
    )
  )

  ;; the 4X4 matrix as this returned by (caddr (nentselp))
  (append
    (mapcar
      '(lambda (v1 v2)
(append v1 (list v2))
       )
     (apply 'mapcar (cons 'list mat)) ; transposed 3X3 matrix
      (trans (cdr (assoc 10 entData)) norm 0) ; deplacement
    )
    (list '(0.0 0.0 0.0 1.0))
  )
)
« Last Edit: February 06, 2007, 12:14:20 PM by gile »
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Transformation matrix vs Acads matrix
« Reply #16 on: February 06, 2007, 11:05:45 AM »
Probably so it is better? 

Code: [Select]
;; the 4X4 matrix as this returned by (caddr (nentselp))
(append (mapcar '(lambda (v1 v2) (append v1 (list v2)))
                (apply 'mapcar (cons 'list mat))
                (trans (cdr (assoc 10 entData)) norm 0)
        ) ;_  mapcar
        (list '(0.0 0.0 0.0 1.0))
) ;_  append

;; My variant
(append
 (apply 'mapcar (cons 'list (append mat (list (trans (cdr (assoc 10 entData)) norm 0)))))
 (list '(0.0 0.0 0.0 1.0))
) ;_  append

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Transformation matrix vs Acads matrix
« Reply #17 on: February 06, 2007, 11:15:27 AM »
Thank you both.  I will look at these when my brain is ready for some real thinking.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Transformation matrix vs Acads matrix
« Reply #18 on: February 06, 2007, 11:22:27 AM »
Very nice Evgeniy, I keep it handy.
But it's not as easy to read than the other one and make it more difficult to understand how the matrix is built (transposed and appended).
« Last Edit: February 06, 2007, 11:35:43 AM by gile »
Speaking English as a French Frog

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Transformation matrix vs Acads matrix
« Reply #19 on: February 06, 2007, 11:38:52 AM »
Gile,

  YOURS WORKS PERFECT!! Now to study it and see where I messed up.  I think I know, I think it's the use of the 'trans'.  Thank you very much.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Transformation matrix vs Acads matrix
« Reply #20 on: February 06, 2007, 11:44:23 AM »
Very nice Evgeniy, I keep it handy.
But it's not as easy to read than the other one and make it more difficult to understand how the matrix is built (transposed and appended).

As (defun mxv .... or (defun mxm...  :-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Transformation matrix vs Acads matrix
« Reply #21 on: February 06, 2007, 12:17:41 PM »
You're welcome, Tim. Glad to give you a hand.

Quote
As (defun mxv .... or (defun mxm...
Very nice too, but not mine. Vladimir Nesterovsky wrote them.
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Transformation matrix vs Acads matrix
« Reply #22 on: February 07, 2007, 04:36:25 AM »
I loosed myself between 'nentsel' and 'nentselp' matrices.

Here's a more direct way to get the 4x4 matrix.

Code: [Select]
(defun Ename->4x4Matrix (ename / entData ang norm)
  (setq entData (entget ename)
ang (cdr (assoc 50 entData))
norm (cdr (assoc 210 entData))
  )
  (append
    (mapcar
      '(lambda (v1 v2)
(append v1 (list v2))
       )
      (mxm
  ;; WCS to OCS 3x3 matrix
  (mapcar '(lambda (v) (trans v 0 norm T))
  '((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
  )
(mxm
  ;; Z axis rotation 3x3 matrix
  (list (list (cos ang) (- (sin ang)) 0.0)
(list (sin ang) (cos ang) 0.0)
'(0.0 0.0 1.0)
  )
;; Scale 3x3 matrix
(list (list (cdr (assoc 41 entData)) 0.0 0.0)
      (list 0.0 (cdr (assoc 42 entData)) 0.0)
      (list 0.0 0.0 (cdr (assoc 43 entData)))
)
)
      )
      ;; Deplacement vector
      (trans (cdr (assoc 10 entData)) norm 0)
    )
    (list '(0.0 0.0 0.0 1.0))
  )
)
« Last Edit: February 07, 2007, 08:13:00 AM by gile »
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Transformation matrix vs Acads matrix
« Reply #23 on: February 07, 2007, 04:52:38 AM »
Excellent work gile!

Code: [Select]
(mxm
 ;; WCS to OCS 3x3 matrix
 (mapcar '(lambda (v) (trans v 0 norm T)) '((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0)))
 ;; Z axis rotation 3x3 matrix
 (list (list (cos ang) (- (sin ang)) 0.0) (list (sin ang) (cos ang) 0.0) '(0.0 0.0 1.0))
) ;_  mxm

I hope will be faster...

Code: [Select]
(append ;; WCS to OCS 3x3 matrix
        (mxm (mapcar '(lambda (v) (trans v 0 norm T)) '((1.0 0.0 0.0) (0.0 1.0 0.0)))
             ;; Z axis rotation 3x3 matrix
             (list (list (cos ang) (- (sin ang))) (list (sin ang) (cos ang)))
        ) ;_  mxm
        (list (trans '(0.0 0.0 1.0) 0 norm T))
) ;_  append

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Transformation matrix vs Acads matrix
« Reply #24 on: February 07, 2007, 06:45:51 AM »
Hi Evgeniy,

There's something wrong in your code, the result of this expression must be a 3x3 matrix.
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Transformation matrix vs Acads matrix
« Reply #25 on: February 07, 2007, 08:29:20 AM »
And another one to get the inverse transformation matrix.

This can also be done using 'ReverseMatrix' (the code have been corrected):
(ReverseMatrix (Ename->4x4Matrix ename)) returns the same result as (Ename->4x4ReverseMatrix ename)

Code: [Select]
;; Apply a transformation matrix to a vector by Vladimir Nesterovsky
;; Arguments: a matrix and a vector
;; Return : the vecor transformed by the matrix

(defun mxv (m v)
  (mapcar '(lambda (r) (apply '+ (mapcar '* r v))) m)
)

;; Multiply two matrices by Vladimir Nesterovsky
;; Arguments : two matrices
;; Return : the matrices combination
;; Attention : (mxm mat1 mat1) is not equal to (mxm mat2 mat1)
(defun mxm (m q)
  (mapcar '(lambda (r)
     (mapcar '(lambda (l) (apply '+ (mapcar '* l r)))
     (apply 'mapcar (cons 'list q))
     )
   )
  m
  )
)

(defun Ename->4x4ReverseMatrix (ename / entData ang norm mat)
  (setq entData (entget ename)
ang (- (cdr (assoc 50 entData)))
norm (cdr (assoc 210 entData))
  )
  (append
    (mapcar
      '(lambda (v1 v2)
(append v1 (list v2))
       )
      (setq mat
     (mxm
       ;; Scale 3x3 matrix
       (list (list (/ 1 (cdr (assoc 41 entData))) 0.0 0.0)
     (list 0.0 (/ 1 (cdr (assoc 42 entData))) 0.0)
     (list 0.0 0.0 (/ 1 (cdr (assoc 43 entData))))
       )
       (mxm
;; Z axis rotation 3x3 matrix
(list (list (cos ang) (- (sin ang)) 0.0)
       (list (sin ang) (cos ang) 0.0)
       '(0.0 0.0 1.0)
)
;; OCS to WCS 3x3 matrix
(mapcar '(lambda (v) (trans v norm 0 T))
'((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
)
       )
     )
      )
      ;; Deplacement vector
      (mapcar '-
      (mxv mat (trans (cdr (assoc 10 entData)) norm 0))
      )
    )
    (list '(0.0 0.0 0.0 1.0))
  )
)


Note: I just discover the Joe Burke's 'ObjMatrix V2', the first version worked only in 2D (Normal = 0.0,0.0,1.0).
Good job, Joe :-)
'ObjMatrix' and 'InverseObjMatrix' do the same as 'Ename->4x4Matrix' and 'Ename->4x4ReverseMatrix'.
« Last Edit: February 07, 2007, 10:07:19 AM by gile »
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Transformation matrix vs Acads matrix
« Reply #26 on: February 07, 2007, 10:46:03 AM »
Hi Evgeniy,

There's something wrong in your code, the result of this expression must be a 3x3 matrix.

Yes, my mistake!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Transformation matrix vs Acads matrix
« Reply #27 on: February 07, 2007, 11:21:55 AM »
Gile,

 Thanks for all the examples.  I am able to follow them, which is a big surprise to me.  I guess I just don't understand how you knew what to do.  How did you know all you needed was the scale matrix (I know you need this one), and the z-rotation matrix (I know you need this one), but you don't need the y or x rotation matrices?  How did you know that you can just get the plan the object is on (OCS) and multiple that by the z-rotation matrix?  I will do a little more digging to try and understand these questions, and then I think I will be done with it.

Thanks again for all the code posted, I learned a lot! 
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Transformation matrix vs Acads matrix
« Reply #28 on: February 07, 2007, 03:25:52 PM »
You're welcome, Tim.

I'm going to try to answer you.
As I said, I began learning transformation matrix process with 'UCS2WCSMatrix' and 'WCS2UCSMatrix' from Doug Broad, and saw how they can be used to align objects from the WCS to the current UCS, for example. If the current UCS is rotated about the 3 axis the transformation matrix make the the object rotate about the 3 axis at one time.
I also try to understand more about OCS and 'trans' function. In the example done in this post, I tried to explain what is the rotation of a block reference (50 DXF code) and its OCS defined by its normal (210 DXF code).
So, transforming an object to an OCS with a transformation matrix is equal to make it rotate as the OCS is rotated about WCS X and Y axis, and the rotation (50 DXF code) of the object is only a rotation about the Z axis of the OCS.
Speaking English as a French Frog

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Transformation matrix vs Acads matrix
« Reply #29 on: February 07, 2007, 03:35:55 PM »
Thanks again Gile.  I have been busy with 'real' work, so I haven't had any time to try and find some answers.  I will look at the post you show when I get a moment.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.