<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to get angles from transformation matrix (same as returned by nentselp)? in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-get-angles-from-transformation-matrix-same-as-returned-by/m-p/11892564#M31640</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to insert some attachments to blocks (lets call them blockX) in the drawing which can be in any rotation&amp;nbsp; but y axis of the block will always be parallel to the XY plane of WCS (even if nested) . These blocks are nested inside other blocks (lets call them blockY ) from 1 to 3 levels deep. BlockYs can be rotated along their Z axis in either in model space or in another block. Note that these attachments will be inserted into the model space not inside the blocks. Since I want to do this for all relevant blocks in the model space I cannot use nentselp / nentsel. I'm using (ssget "_X" ...).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need the angle between x axis of&amp;nbsp;blockX projected onto XY plane of WCS (no matter the level of nesting)&amp;nbsp;&lt;SPAN&gt;θ,&lt;/SPAN&gt; and angle between the x axis of&amp;nbsp;blockX&amp;nbsp; and the XY plane of WCS&amp;nbsp;(no matter the level of nesting)&amp;nbsp;&lt;STRONG&gt;γ&lt;/STRONG&gt; . Shown in screenshot.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm currently getting the transformation matrix w.r.t WCS using the function 'get-tmatrix (by gile) ( which is same as the one returned by nentselp). But I don't know how to get the angles from this. Please help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the attached drawing file, the red line is the x axis of the respective blocks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ShricharanaB_1-1681368643229.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1201800iC1186E7AB3BE6B16/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ShricharanaB_1-1681368643229.png" alt="ShricharanaB_1-1681368643229.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Apr 2023 07:22:06 GMT</pubDate>
    <dc:creator>ShricharanaB</dc:creator>
    <dc:date>2023-04-13T07:22:06Z</dc:date>
    <item>
      <title>How to get angles from transformation matrix (same as returned by nentselp)?</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-get-angles-from-transformation-matrix-same-as-returned-by/m-p/11892564#M31640</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to insert some attachments to blocks (lets call them blockX) in the drawing which can be in any rotation&amp;nbsp; but y axis of the block will always be parallel to the XY plane of WCS (even if nested) . These blocks are nested inside other blocks (lets call them blockY ) from 1 to 3 levels deep. BlockYs can be rotated along their Z axis in either in model space or in another block. Note that these attachments will be inserted into the model space not inside the blocks. Since I want to do this for all relevant blocks in the model space I cannot use nentselp / nentsel. I'm using (ssget "_X" ...).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need the angle between x axis of&amp;nbsp;blockX projected onto XY plane of WCS (no matter the level of nesting)&amp;nbsp;&lt;SPAN&gt;θ,&lt;/SPAN&gt; and angle between the x axis of&amp;nbsp;blockX&amp;nbsp; and the XY plane of WCS&amp;nbsp;(no matter the level of nesting)&amp;nbsp;&lt;STRONG&gt;γ&lt;/STRONG&gt; . Shown in screenshot.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm currently getting the transformation matrix w.r.t WCS using the function 'get-tmatrix (by gile) ( which is same as the one returned by nentselp). But I don't know how to get the angles from this. Please help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the attached drawing file, the red line is the x axis of the respective blocks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ShricharanaB_1-1681368643229.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1201800iC1186E7AB3BE6B16/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ShricharanaB_1-1681368643229.png" alt="ShricharanaB_1-1681368643229.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2023 07:22:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-get-angles-from-transformation-matrix-same-as-returned-by/m-p/11892564#M31640</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2023-04-13T07:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to get angles from transformation matrix (same as returned by nentselp)?</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-get-angles-from-transformation-matrix-same-as-returned-by/m-p/11892697#M31641</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The angles you're looking for are called &lt;A href="https://en.wikipedia.org/wiki/Euler_angles" target="_blank" rel="noopener"&gt;Euler angles&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The following routines computes the Euler angles from a transformation matrix such as the one returned by nentsel or nentselp with the two mainly used conventions: Proper Euler and Tait-Bryan.&lt;/P&gt;
&lt;P&gt;The angles you're looking for seems to be Yaw and Pitch fromTait-Bryan angles.&lt;/P&gt;
&lt;LI-CODE lang="lisp"&gt;;; gc:butLast
;; Returns the list but the last item
;;
;; Argument
;; l : a list
(defun gc:butLast (l) (reverse (cdr (reverse l))))

;; gc:GetNormal
;; Returns the unit vector of a vector
;; Retourne le vecteur unitaire d'un vecteur
;;
;; Argument
;; v : a vector
(defun gc:GetNormal (v)
 ((lambda (l)
    (if (/= 0 l)
      (mapcar (function (lambda (x) (/ x l))) v)
    )
  )
   (distance '(0. 0. 0.) v)
 )
)

;; TRP
;; Transposes a matrix -Doug Wilson-
;;
;; Argument
;; m : a matrix
(defun trp (m) (apply 'mapcar (cons 'list m)))

;; gc:acos
;; Returns the arc cosine of an angle
;;
;; Argument
;; n : le angle cosine
(defun gc:acos (n)
 (cond
   ((equal n 1. 1e-9) 0.)
   ((equal n -1. 1e-9) pi)
   ((&amp;lt; -1. n 1.)
    (atan (sqrt (- 1. (expt n 2))) n)
   )
 )
)

;; gc:asin
;; Returns the arc sine of an angle
;;
;; Argument
;; n : le angle sine
(defun gc:asin (n)
 (cond
   ((equal n 1. 1e-9) (/ pi 2.))
   ((equal n -1. 1e-9) (/ pi -2.))
   ((&amp;lt; -1. n 1.)
    (atan n (sqrt (- 1 (expt n 2))))
   )
 )
)

;; ProperEuler
;; Returns a list containing the the Euler angles in radians (precession, nutation and intrinsic rotation)
;;
;; Argument
;; m : a transformation matrix such as the one returned by nentsel or nentselp
(defun ProperEuler (m / theta psi phi)
   (setq m (mapcar
               'gc:GetNormal
               (if (= (length (car m)) 3)
                   (mapcar 'gc:butlast (trp m))
                   (gc:butlast (mapcar 'gc:butlast m))
               )
           )
   )
   (setq theta (gc:acos (caddr (caddr m))))
   (if (&amp;lt; (abs theta) 1e-7)
       (setq theta 0.
             psi   0.
             phi   (atan (car (cadr m)) (cadr (cadr m)))
       )
       (setq psi (atan (caddr (car m)) (- (caddr (cadr m))))
             phi (atan (car (caddr m)) (cadr (caddr m)))
       )
   )
   (list psi theta phi)
)

;; TaitBryan
;; Returns a list containing the the Tait-Bryan angles in radians (yaw, pitch, roll)
;;
;; Argument
;; m : a transformation matrix such as the one returned by nentsel or nentselp
(defun TaitBryan (m / a b c)
   (setq m (mapcar
               'gc:GetNormal
               (if (= (length (car m)) 3)
                   (mapcar 'gc:butlast (trp m))
                   (gc:butlast (mapcar 'gc:butlast m))
               )
           )
   )
   (setq b (- (gc:asin (car (caddr m)))))
   (cond
       ((&amp;lt; (abs (- b (* pi 0.5))) 1e-7)
        (setq b (* pi 0.5)
              a   (atan (caddr (cadr m)) (cadr (cadr m)))
              c  0.
        )
       )
       ((&amp;lt; (abs (+ b (* pi 0.5))) 1e-7)
        (setq b (* pi -0.5)
              a   (atan (- (caddr (cadr m))) (cadr (cadr m)))
              c  0.
        )
       )
       (T
        (setq a   (atan (car (cadr m)) (car (car m)))
              c  (atan (cadr (caddr m))  (caddr (caddr m)))
        )
       )
   )
   (list a b c)
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an testing command:&lt;/P&gt;
&lt;LI-CODE lang="general"&gt;(defun c:EULERANGLES (/ block matrix)
   (while
       (not
           (and
               (setq block (nentsel "\nSelect a nested entity within a block: "))
               (= (length block) 4)
           )
       )
   )
   (setq matrix (caddr block))
   (mapcar '(lambda (s v) (prompt (strcat "\n" s ": " (angtos v))))
           '("precession" "nutation" "rotation")
           (ProperEuler matrix))
   (mapcar '(lambda (s v) (prompt (strcat "\n" s ": " (angtos v))))
           '("yaw" "pitch" "roll")
           (TaitBryan matrix))
   (princ)
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2023 08:05:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-get-angles-from-transformation-matrix-same-as-returned-by/m-p/11892697#M31641</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-04-13T08:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to get angles from transformation matrix (same as returned by nentselp)?</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-get-angles-from-transformation-matrix-same-as-returned-by/m-p/11892817#M31642</link>
      <description>&lt;P&gt;I knew you were going to answer this! Are you the same Gile that has posted this&amp;nbsp;&lt;STRONG&gt;&lt;A title="theswamp.org" href="http://www.theswamp.org/index.php?topic=13526.0" target="_blank" rel="noopener"&gt;theswamp.org&lt;/A&gt;&amp;nbsp; ?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I seem to be getting the proper angles from the TaitBryan function. Thank you! &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;P.S: Is there any reference you can suggest where I can learn about the transformation matrix and the Euler angles and the relation between the two? I just can't seem to get these concepts into my head.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2023 08:57:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-get-angles-from-transformation-matrix-same-as-returned-by/m-p/11892817#M31642</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2023-04-13T08:57:25Z</dc:date>
    </item>
  </channel>
</rss>

