Author Topic: Trans and 3D rotated UCS  (Read 3144 times)

0 Members and 1 Guest are viewing this topic.

wizman

  • Bull Frog
  • Posts: 290
Trans and 3D rotated UCS
« on: August 31, 2020, 02:09:13 PM »
Hi folks, just need a little help.

Why in the UCS of the attached file, the two below are having different results?  Thanks.


(trans '(100 0 0) (trans '(0 0 1) 1 0 t) 0)

(trans '(100 0 0) 1 0 )

ribarm

  • Gator
  • Posts: 3257
  • Marko Ribar, architect
Re: Trans and 3D rotated UCS
« Reply #1 on: August 31, 2020, 02:30:31 PM »
Hint :
These 2 are not equal...

(trans '(0 0 1) 1 0 t)

(trans '(0 0 1) 0 1 t)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

wizman

  • Bull Frog
  • Posts: 290
Re: Trans and 3D rotated UCS
« Reply #2 on: August 31, 2020, 02:52:58 PM »
Hint :
These 2 are not equal...

(trans '(0 0 1) 1 0 t)

(trans '(0 0 1) 0 1 t)

Thanks Marko, I appreciate your quick response but i do not follow your unit vector hint of UCS to World and vice versa.

To my mind i can replace 1 with the UCSZDIR unless i am missing something.




Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Trans and 3D rotated UCS
« Reply #3 on: August 31, 2020, 04:36:23 PM »
A UCS is defined by UCSXDIR, UCSYDIR (and Origin, but it's not the case here). The Z axis is the cross product of X and Y axis.
A normal vector, like (trans '(0 0 1) 1 0 T), might be the same as UCS' Z axis, but the other 2 are following the Arbitrary Axis Algorithm
Quote
Given a unit-length vector to be used as the Z axis of a coordinate system, the arbitrary axis algorithm generates a corresponding X axis for the coordinate system. The Y axis follows by application of the right-hand rule.

So the normal vector alone cannot reproduce the entire UCS definition. Think about rotating your UCS about Z axis. You will get another UCS with the same normal vector, but the coordinates of every point are clearly different

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Trans and 3D rotated UCS
« Reply #4 on: August 31, 2020, 05:25:52 PM »
Good explanation Stefan  :-)

wizman

  • Bull Frog
  • Posts: 290
Re: Trans and 3D rotated UCS
« Reply #5 on: August 31, 2020, 08:15:19 PM »
A UCS is defined by UCSXDIR, UCSYDIR (and Origin, but it's not the case here). The Z axis is the cross product of X and Y axis.
A normal vector, like (trans '(0 0 1) 1 0 T), might be the same as UCS' Z axis, but the other 2 are following the Arbitrary Axis Algorithm
Quote
Given a unit-length vector to be used as the Z axis of a coordinate system, the arbitrary axis algorithm generates a corresponding X axis for the coordinate system. The Y axis follows by application of the right-hand rule.

So the normal vector alone cannot reproduce the entire UCS definition. Think about rotating your UCS about Z axis. You will get another UCS with the same normal vector, but the coordinates of every point are clearly different

Thank you Stefan,

So i cannot always use UCSZDIR inside trans function since it can use a different UCS, other than what is already defined in the drawing, after following the Arbitrary Axis Algorithm and right hand rule.









« Last Edit: August 31, 2020, 10:57:25 PM by wizman »

ribarm

  • Gator
  • Posts: 3257
  • Marko Ribar, architect
Re: Trans and 3D rotated UCS
« Reply #6 on: August 31, 2020, 11:55:56 PM »
A UCS is defined by UCSXDIR, UCSYDIR (and Origin, but it's not the case here). The Z axis is the cross product of X and Y axis.
A normal vector, like (trans '(0 0 1) 1 0 T), might be the same as UCS' Z axis, but the other 2 are following the Arbitrary Axis Algorithm
Quote
Given a unit-length vector to be used as the Z axis of a coordinate system, the arbitrary axis algorithm generates a corresponding X axis for the coordinate system. The Y axis follows by application of the right-hand rule.

So the normal vector alone cannot reproduce the entire UCS definition. Think about rotating your UCS about Z axis. You will get another UCS with the same normal vector, but the coordinates of every point are clearly different

Thank you Stefan,

So i cannot always use UCSZDIR inside trans function since it can use a different UCS, other than what is already defined in the drawing, after following the Arbitrary Axis Algorithm and right hand rule.

Wizman, have you tried what I suggested... IMHO you can always use UCSZDIR inside trans function, only thing you did was wrong setting inside trans for calculating UCSZDIR - you have to swap 1 and 0 like I explained...
« Last Edit: October 24, 2020, 02:28:01 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

wizman

  • Bull Frog
  • Posts: 290
Re: Trans and 3D rotated UCS
« Reply #7 on: September 01, 2020, 12:33:28 AM »
Quote
Wizman, have you tried what I suggested... IMHO you can always use UCSZDIR inside trans function, only thing you did was wrong setting inside trans for calculating UCSZDIR - you have to swap 1 and 0 like I explained...

Hi Marko, do you mean like this? But they are different.

(trans '(100 0 0) 1 0 )
=> (43.3013 25.0 86.6025)

(trans '(100 0 0) (trans '(0 0 1) 0 1 t) 0);swapped
=> (-2.77556e-15 100.0 0.0)

I am under the impression that i can use it also until this.

But the UCSZDIR does not define 3 basis vectors derived by Arbitrary Algorithm Axis.  So this got me thinking.

Thanks.

ribarm

  • Gator
  • Posts: 3257
  • Marko Ribar, architect
Re: Trans and 3D rotated UCS
« Reply #8 on: September 01, 2020, 01:41:28 AM »
Actully Stefan was right, you have to specify all UCS data and UCSXDIR and UCSYDIR and UCSZDIR to get (trans pt 1 0)... It should go like this :

Code - Auto/Visual Lisp: [Select]
  1. (defun _trans-1->0 ( pt / mxv trp mat tmat )
  2.  
  3.   (defun mxv ( m v )
  4.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  5.   )
  6.  
  7.   (defun trp ( l )
  8.     (apply 'mapcar (cons 'list l))
  9.   )
  10.  
  11.   (setq mat
  12.     (list
  13.       (getvar 'ucsxdir)
  14.       (getvar 'ucsydir)
  15.       (trans '(0 0 1) 1 0 t)
  16.     )
  17.   )
  18.  
  19.   (setq tmat (trp mat))
  20.  
  21.   (mxv tmat pt)
  22. )
  23.  
  24. ;|
  25. (trans '(100 0 0) 1 0)
  26.  => (43.3013 25.0 86.6025)
  27.  
  28. (_trans-1->0 '(100 0 0))
  29.  => (43.3013 25.0 86.6025)
  30. |;
  31.  

I hope you understand now how UCSZDIR is rightly obtained...
« Last Edit: October 24, 2020, 02:27:11 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3257
  • Marko Ribar, architect
Re: Trans and 3D rotated UCS
« Reply #9 on: September 01, 2020, 01:47:08 AM »
Actually you should account for origin point too, but in your DWG WCS origin = UCS origin, so my remark is applicable for your case...
To be fully correct you should apply transformation matrix of OCS to point-vector + vector of translation between UCS origin and WCS origin (transformation matrix of OCS to point of UCS origin)...
« Last Edit: September 01, 2020, 02:09:32 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

wizman

  • Bull Frog
  • Posts: 290
Re: Trans and 3D rotated UCS
« Reply #10 on: September 01, 2020, 03:17:15 AM »
Actully Stefan was right, you have to specify all UCS data and UCSXDIR and UCSYDIR and UCSZDIR to get (trans pt 1 0)... It should go like this :

Code - Auto/Visual Lisp: [Select]
  1. (defun _trans-1->0 ( pt / mxv trp mat tmat )
  2.  
  3.   (defun mxv ( m v )
  4.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  5.   )
  6.  
  7.   (defun trp ( l )
  8.     (apply 'mapcar (cons 'list l))
  9.   )
  10.  
  11.   (setq mat
  12.     (list
  13.       (getvar 'ucsxdir)
  14.       (getvar 'ucsydir)
  15.       (trans '(0 0 1) 0 1 t)
  16.     )
  17.   )
  18.  
  19.   (setq tmat (trp mat))
  20.  
  21.   (mxv tmat pt)
  22. )
  23.  
  24. ;|
  25. (trans '(100 0 0) 1 0)
  26.  => (43.3013 25.0 86.6025)
  27.  
  28. (_trans-1->0 '(100 0 0))
  29.  => (43.3013 25.0 86.6025)
  30. |;
  31.  

I hope you understand now how UCSZDIR is rightly obtained...


Thank you Marko.  You now defined completely the 3 basis vectors.

I follow your code except you used transpose instead of inverse of matrix.

ribarm

  • Gator
  • Posts: 3257
  • Marko Ribar, architect
Re: Trans and 3D rotated UCS
« Reply #11 on: September 01, 2020, 06:14:24 AM »
Actully Stefan was right, you have to specify all UCS data and UCSXDIR and UCSYDIR and UCSZDIR to get (trans pt 1 0)... It should go like this :

Code - Auto/Visual Lisp: [Select]
  1. (defun _trans-1->0 ( pt / mxv trp mat tmat )
  2.  
  3.   (defun mxv ( m v )
  4.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  5.   )
  6.  
  7.   (defun trp ( l )
  8.     (apply 'mapcar (cons 'list l))
  9.   )
  10.  
  11.   (setq mat
  12.     (list
  13.       (getvar 'ucsxdir)
  14.       (getvar 'ucsydir)
  15.       (trans '(0 0 1) 1 0 t)
  16.     )
  17.   )
  18.  
  19.   (setq tmat (trp mat))
  20.  
  21.   (mxv tmat pt)
  22. )
  23.  
  24. ;|
  25. (trans '(100 0 0) 1 0)
  26.  => (43.3013 25.0 86.6025)
  27.  
  28. (_trans-1->0 '(100 0 0))
  29.  => (43.3013 25.0 86.6025)
  30. |;
  31.  

I hope you understand now how UCSZDIR is rightly obtained...


Thank you Marko.  You now defined completely the 3 basis vectors.

I follow your code except you used transpose instead of inverse of matrix.

Inverse of that transposed matrix should be used for opposite transformation (_trans-0->1)
« Last Edit: October 24, 2020, 02:28:59 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

wizman

  • Bull Frog
  • Posts: 290
Re: Trans and 3D rotated UCS
« Reply #12 on: September 01, 2020, 06:41:18 AM »
Okay I get it now,

Listing mat gives extrusion vectors in rows but we needed them in columns to proceed correctly with matrix manipulation.

Thank you for your help Marko.  Much Appreciated.

ribarm

  • Gator
  • Posts: 3257
  • Marko Ribar, architect
Re: Trans and 3D rotated UCS
« Reply #13 on: December 18, 2020, 09:10:31 AM »
The problem I see is that I was wrong 'ucszdir = (trans '(0.0 0.0 1.0) 1 0 t) and beside that real problem was this UCS you set behaves as OCS (origin of UCS = origin of WCS) so (trans '(100.0 0.0 0.0) 1 0) = (trans '(100.0 0.0 0.0) 1 0 t)...

Now that I wrote this snippets for understanding what's really going on with (trans pt 1 0) and (trans pt 0 1), I suggest that you study these 2 snippets if it may help you... As soon as you load each one, they should return TT no matter what UCS you are using - that's the real way to understand (UCS origin /= WCS origin)...

Code - Auto/Visual Lisp: [Select]
  1. ;;; Should return T for any UCS in 3D ;;;
  2.  
  3. (defun chkucsmatrixvector1 ( / mxv mat )
  4.  
  5.   (defun mxv ( m v )
  6.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  7.   )
  8.  
  9.   (setq mat
  10.     (apply 'mapcar
  11.       (cons 'list
  12.         (list
  13.           (getvar 'ucsxdir)
  14.           (getvar 'ucsydir)
  15.           (trans '(0.0 0.0 1.0) 1 0 t)
  16.         )
  17.       )
  18.     )
  19.   )
  20.  
  21.   (equal (trans '(10.0 10.0 10.0) 1 0 t) (mxv mat '(10.0 10.0 10.0)) 1e-8)
  22. )
  23.  
  24. (princ (chkucsmatrixvector1))
  25.  
  26. (defun chkucsmatrixvector2 ( / mxv mat invm imat )
  27.  
  28.   (defun mxv ( m v )
  29.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  30.   )
  31.  
  32.   (setq mat
  33.     (apply 'mapcar
  34.       (cons 'list
  35.         (list
  36.           (getvar 'ucsxdir)
  37.           (getvar 'ucsydir)
  38.           (trans '(0.0 0.0 1.0) 1 0 t)
  39.         )
  40.       )
  41.     )
  42.   )
  43.  
  44.   ;; Matrix Inverse  -  gile & Lee Mac
  45.   ;; Uses Gauss-Jordan Elimination to return the inverse of a non-singular nxn matrix.
  46.   ;; Args: m - nxn matrix
  47.  
  48.   (defun invm ( m / c f p r )
  49.      
  50.       (defun f ( p m )
  51.           (mapcar (function (lambda ( x ) (mapcar (function (lambda ( a b ) (- a (* (car x) b)))) (cdr x) p))) m)
  52.       )
  53.       (setq  m (mapcar (function append) m (imat (length m))))
  54.       (while m
  55.           (setq c (mapcar (function (lambda ( x ) (abs (car x)))) m))
  56.           (repeat (vl-position (apply 'max c) c)
  57.               (setq m (append (cdr m) (list (car m))))
  58.           )
  59.           (if (equal 0.0 (caar m) 1e-14)
  60.               (setq m nil
  61.                     r nil
  62.               )
  63.               (setq p (mapcar (function (lambda ( x ) (/ (float x) (caar m)))) (cdar m))
  64.                     m (f p (cdr m))
  65.                     r (cons p (f p r))
  66.               )
  67.           )
  68.       )
  69.       (reverse r)
  70.   )
  71.  
  72.   ;; Identity Matrix  -  Lee Mac
  73.   ;; Args: n - matrix dimension
  74.  
  75.   (defun imat ( n / i j l m )
  76.       (repeat (setq i n)
  77.           (repeat (setq j n)
  78.               (setq l (cons (if (= i j) 1.0 0.0) l)
  79.                     j (1- j)
  80.               )
  81.           )
  82.           (setq m (cons l m)
  83.                 l nil
  84.                 i (1- i)
  85.           )
  86.       )
  87.       m
  88.   )
  89.  
  90.   (equal (trans '(10.0 10.0 10.0) 0 1 t) (mxv (invm mat) '(10.0 10.0 10.0)) 1e-8)
  91. )
  92.  
  93. (princ (chkucsmatrixvector2))
  94.  

Code - Auto/Visual Lisp: [Select]
  1. ;;; Should return T for any UCS in 3D ;;;
  2.  
  3. (defun trans-1-0 ( / mxv mat )
  4.  
  5.   (defun mxv ( m v )
  6.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  7.   )
  8.  
  9.   (setq mat
  10.     (apply 'mapcar
  11.       (cons 'list
  12.         (list
  13.           (getvar 'ucsxdir)
  14.           (getvar 'ucsydir)
  15.           (trans '(0.0 0.0 1.0) 1 0 t)
  16.         )
  17.       )
  18.     )
  19.   )
  20.  
  21.   (equal (trans '(10.0 10.0 10.0) 1 0) (mapcar '+ (trans '(0.0 0.0 0.0) 1 0) (mxv mat '(10.0 10.0 10.0))) 1e-8)
  22. )
  23.  
  24. (princ (trans-1-0))
  25.  
  26. (defun trans-0-1 ( / mxv mat invm imat )
  27.  
  28.   (defun mxv ( m v )
  29.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  30.   )
  31.  
  32.   (setq mat
  33.     (apply 'mapcar
  34.       (cons 'list
  35.         (list
  36.           (getvar 'ucsxdir)
  37.           (getvar 'ucsydir)
  38.           (trans '(0.0 0.0 1.0) 1 0 t)
  39.         )
  40.       )
  41.     )
  42.   )
  43.  
  44.   ;; Matrix Inverse  -  gile & Lee Mac
  45.   ;; Uses Gauss-Jordan Elimination to return the inverse of a non-singular nxn matrix.
  46.   ;; Args: m - nxn matrix
  47.  
  48.   (defun invm ( m / c f p r )
  49.      
  50.       (defun f ( p m )
  51.           (mapcar (function (lambda ( x ) (mapcar (function (lambda ( a b ) (- a (* (car x) b)))) (cdr x) p))) m)
  52.       )
  53.       (setq  m (mapcar (function append) m (imat (length m))))
  54.       (while m
  55.           (setq c (mapcar (function (lambda ( x ) (abs (car x)))) m))
  56.           (repeat (vl-position (apply 'max c) c)
  57.               (setq m (append (cdr m) (list (car m))))
  58.           )
  59.           (if (equal 0.0 (caar m) 1e-14)
  60.               (setq m nil
  61.                     r nil
  62.               )
  63.               (setq p (mapcar (function (lambda ( x ) (/ (float x) (caar m)))) (cdar m))
  64.                     m (f p (cdr m))
  65.                     r (cons p (f p r))
  66.               )
  67.           )
  68.       )
  69.       (reverse r)
  70.   )
  71.  
  72.   ;; Identity Matrix  -  Lee Mac
  73.   ;; Args: n - matrix dimension
  74.  
  75.   (defun imat ( n / i j l m )
  76.       (repeat (setq i n)
  77.           (repeat (setq j n)
  78.               (setq l (cons (if (= i j) 1.0 0.0) l)
  79.                     j (1- j)
  80.               )
  81.           )
  82.           (setq m (cons l m)
  83.                 l nil
  84.                 i (1- i)
  85.           )
  86.       )
  87.       m
  88.   )
  89.  
  90.   (equal (trans '(10.0 10.0 10.0) 0 1) (mapcar '+ (trans '(0.0 0.0 0.0) 0 1) (mxv (invm mat) '(10.0 10.0 10.0))) 1e-8)
  91. )
  92.  
  93. (princ (trans-0-1))
  94.  

HTH. Never late to post if you find out useful...!!!
 :-)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

wizman

  • Bull Frog
  • Posts: 290
Re: Trans and 3D rotated UCS
« Reply #14 on: December 20, 2020, 01:24:44 AM »

HTH. Never late to post if you find out useful...!!!
 :-)

Thank you Marko for following up on this.

Thanks for sharing your expertise on this subject.

Happy Holidays!