Compute Rotation and Scale from Transform Object
-
@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).
-
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?
-
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
-
@ruts said:
...when you call the method
euler_angle
you create an empty array under variablexyz
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
or1
or2
, 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 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?
-
You realize that this topic thread is like 4 and 1/2 years old ?
-
@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?
-
-
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 -
An olde typo - use 1 for the second test...
Advertisement