sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Compute Rotation and Scale from Transform Object

    Scheduled Pinned Locked Moved Developers' Forum
    26 Posts 9 Posters 4.6k Views 9 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T Offline
      tomasz
      last edited by

      @martinrinehart said:

      @dburdick said:

      I read your wonderful web-page description of the 4 x 4 matrix - ...

      Very kind words, indeed. Many thanks.

      For those not versed in the Transformation Matrix, my tutorial's Appendix T, introduces it, Appendix MM explains matrix multiplication and Chapter 16 explains how you can live without it (and includes a Matrix class, just in case).

      Thank you Martin for those wonderful tutorials. I wasn't aware that transformation.to_a[15] is a divisor for a translation! As far as I am aware SketchUp itself keeps it equal to 1, but some plugins modify Wt (i.e. Component Stringer).

      Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

      1 Reply Last reply Reply Quote 0
      • R Offline
        Ruts
        last edited by

        I have a question about the euler_angle method, especially about the xyz. I have searched the web and found information about this case, but it didn't result in an answer.

        Here's how I read this piece of code:

        when you call the method euler_angles you create an empty array under variable xyz as parameter. Then nothing happens with the array for the whole piece of code. At the end the array can be ==0 (contains one element which is equal to zero), ==1, ==2 or still empty. How can it be that the array can contain elements?

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          Words in ALLCAPS are reserved for constants.
          Words in Titlecase are for Class and Module Identifiers, (which are also constants. Any word the starts with a capital character is a constant.)

          Bad:
          Selection = Sketchup.active_model.selection[0] Rotation = Selection.transformation.rotXYZ

          Good for variables:
          selection = Sketchup.active_model.selection[0] rotation = selection.transformation.rotXYZ
          😉

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @ruts said:

            ...when you call the method euler_angle you create an empty array under variable xyz as parameter.

            The secret is that Ruby does not really "have" variables, even though the books use that name. Ruby has references that point at objects, and a reference can be made to point at any class of object, and then later be re-assigned to point at any other object of any class, at any time.
            This is referred to as "weakly typed", but really Ruby references are not type locked at all.

            @ruts said:

            Then nothing happens with the array for the whole piece of code.

            Because it (the argument) is just being used as a switch to tell the method what the coder wants as an output. An empty array object is simply the default, which tells the method the calling code expects an array as a return object.

            @ruts said:

            At the end the array can be ==0 (contains one element which is equal to zero), ==1, ==2 or still empty. How can it be that the array can contain elements?

            It cannot. You mis-understand. The method is not testing an array, it is testing a reference to the method argument to see if is pointing at integers 0 or 1 or 2, or still pointing at [] (the default empty array object,) and then returning either the indicated values, or an array of all three.

            IF you call the method with no arguments, or like euler_angle([]) you will get an array of 3 values.
            IF you call the method thus: euler_angle(0) you will get the x value returned.
            IF you call the method thus: euler_angle(1) you will get the y value returned.
            IF you call the method thus: euler_angle(2) you will get the z value returned.

            The 0, 1 and 2 subscripts come from the SketchUp API's extension of the Array class, where 3 element arrays can act like points and vectors.

            Still have not read the book, I see.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • R Offline
              Ruts
              last edited by

              I did delete the rotX/Y/Z methods, that's why I didn't understand where the variable xyz came from and why it could get value = 0/1/2. I do understand how methods with there variables work. Sorry was confused.

              On the other hand, I have been working with the euler_angle method for a while and now that I'm closely getting to a complete script I have noticed this method only partial works (for me?). Does it work for you guys? When I did draw shapes with a given rotation, the method didn't succeed to calculate the right angles for me so I did some research and made some changes. Here's the code I use:

              	def self.rotation(trans)
              		b = [trans.xaxis.to_a, trans.yaxis.to_a, trans.zaxis.to_a]
              		m = b.transpose.flatten!
              		if m[6] != 1 and m[6] != -1
              			ry = -Math.asin(m[6])
              			rx = Math.atan2(m[7]/Math.cos(ry),m[8]/Math.cos(ry))
              			rz = Math.atan2(m[3]/Math.cos(ry),m[0]/Math.cos(ry))
              		else
              			rz = 0
              			phipos = Math.atan2(m[1],m[2])
              			phineg = Math.atan2(-m[1],-m[2])
              			if m[6] == -1
              				ry = Math;;PI/2
              				rx = rz + phipos
              			else
              				ry = -Math;;PI/2
              				rx = -rz + phineg
              			end
              		end   
              	return [rx.radians,ry.radians,rz.radians]
              	end	
              

              First I found this document (page 5) that explains the calculations the way you do it. But it seems that you miss little pieces of code to make it complete.

              When I did calculate the angles it yet didn't calculate the right angles. So I did try some things and found out that the angles that were calculated represent the transpose matrix of the rotation that I need. So I fixed this by transposing the rotation matrix before the calculations.

              With these changes this piece of code does calculate the right angles. Did your code work for you?

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                You realize that this topic thread is like 4 and 1/2 years old ?

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • R Offline
                  Ruts
                  last edited by

                  @dan rathbun said:

                  You realize that this topic thread is like 4 and 1/2 years old ?

                  Yes, I have seen it. That does not mean that people can't come here and look for code that works? That's how I landed here. I was looking for some code that could convert the rotation matrix to euler angles. I found out that this code didn't work for me, so I did correct it and now I share it here just in case people experience the same problem as me. I'm just excited that I finally can contribute a little instead of always asking things!

                  Nothing wrong with that, right?

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    @ruts said:

                    @dan rathbun said:

                    You realize that this topic thread is like 4 and 1/2 years old ?

                    Yes, I have seen it. ... Nothing wrong with that, right?

                    I was referring to your asking questions of original posters, who might not remember, or might not even have been active here for some time.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • J Offline
                      Jernej Vidmar
                      last edited by

                      Hello transformation gurus!

                      just to double check the euler_angle(xyz=[]) method ... is the condition:

                      if m[6] != 1 and m[6]!= 1
                      

                      at the beginning correct? Why double checking the same variable against the same value? Or is it meant to be:

                      if m[6] != 1 and m[6]!= -1
                      

                      ?

                      Cheers,
                      Jernej

                      1 Reply Last reply Reply Quote 0
                      • TIGT Offline
                        TIG Moderator
                        last edited by

                        An olde typo - use 1 for the second test...

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 2 / 2
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement