We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Extracting values from a matrix
Page Index Toggle Pages: 1
Extracting values from a matrix (Read 1662 times)
Extracting values from a matrix
Dec 4th, 2008, 2:42am
 
I'm trying to get the rotation value from a matrix. Is there a way to do that? Can the values in the transform matrix be accessed within Processing?
Re: Extracting values from a matrix
Reply #1 - Dec 4th, 2008, 11:28am
 
you can print it

http://processing.org/reference/printMatrix_.html

but i don't see a way of getting hold of it yourself.

ok, source code...

printMatrix is using the modelview which is public in PGraphics3D:

public PMatrix3D modelview;

and the 16 separate elements are public in PMatrix3D

http://dev.processing.org/source/index.cgi/trunk/processing/core/src/processing/core/PMatrix3D.java?rev=4997&view=markup

so you can get to them...

(and now someone who knows the api better than i do will point to the easy way...)
Re: Extracting values from a matrix
Reply #2 - Dec 4th, 2008, 5:16pm
 
Thanks for the hint...

Can anyone out there show me how I would get access to the the matrix object processing is using? I'm afraid it's outside of my technical scope at this moment.

Also, BTW, I'm dealing with a 2D matrix but I'm assuming the process will be the same.

Re: Extracting values from a matrix
Reply #3 - Dec 5th, 2008, 1:14pm
 
There is also applyMatrix().
What are you trying to do
Re: Extracting values from a matrix
Reply #4 - Dec 5th, 2008, 5:05pm
 
I am applying a series of rotate() transforms to draw some shapes but I want to be able to have access to the cumulative rotation amount that has been applied. Currently, I am creating a separate angle variable and incrementing it by each value I am rotating by, but I was just curious if I could get that straight from the matrix.

I would just like to be able to do something like the following:

float rot = viewingMatrix[0];
Re: Extracting values from a matrix
Reply #5 - Dec 5th, 2008, 5:25pm
 
Unfortunately, the Matrix isn't anywhere near to that simple. there's not a simple rotation angle variable, it's all quite complex really even if you do get access to it.
Re: Extracting values from a matrix
Reply #6 - Dec 5th, 2008, 7:47pm
 
Okay, then, how about this one:

Is there a way I can push some arbitrary values through the matrix? Let's say I create a point representing an XY location in my original "canvas" space, I would apply the current active transformation matrix to it to get the location of the point in the transformed space. Pushing two points through this process, I would be able to calculate the translation, rotation and scale...



Re: Extracting values from a matrix
Reply #7 - Mar 5th, 2009, 4:51am
 
I'm with thomashollier: Any possible way of getting the transform of an object after it's been through the transform stack? This doesn't seem like such a strange request, really... Any luck so far?
Re: Extracting values from a matrix
Reply #8 - Apr 24th, 2009, 6:40am
 
  I'm not so expirienced with matrices, but I can say that I did.
1) I think you should create so many temporary variables, how many vars you have, or twice, or three times more. It depends of what kinda transformations you do( for example: tempRotX, saveRotX, tempTranslX, saveTranslX...) for concrete cases and time.
2) Apply matrcie [n1-n16 floats] in void display or apply any matrice before you continue to transform (for example to neutralize, to renew or invoke any matrice you need for concrete case, for multiplying or adding matrices).
3) Matrices is hard topic and the best way is to spend couple days learning matrices.
  Sorry if I couldn't help.

Determining (2D) origin, rotation and scale
Reply #9 - Apr 25th, 2009, 12:24am
 
If you are working in 2D and only need the angle, keeping the angle separately yourself seems like a good option. It might be possible to retrieve the 2D rotation from the transformation matrix, but perhaps only if only certain transformations have been made.

Indeed, the sample code in the ApplyMatrix() documentation gives us a clue:

Code:
float ct = cos(PI/9.0);
float st = sin(PI/9.0);          
// Matrix for rotation around the Y axis
applyMatrix(  ct, 0.0,  st,  0.0,
            0.0, 1.0, 0.0,  0.0,
            -st, 0.0,  ct,  0.0,
            0.0, 0.0, 0.0,  1.0);

If you had access to the transformation matrix, the "ct" and "st" elements from the first row could be used to work out the angle (by applying inverse sin and cos operations and working out what quadrant it is in), but... and this is a big but... that is making a massive assumption about the contents of the rest of the matrix. If the "-st" element, for example, is not the same as the negative of the "st" element, some operation other than a simple rotation must have been performed.

An alternative solution!

I did some tinkering and came up with these handy functions.

Code:

// Returns a 4-element float array with transform information
// { originX, originY, rotationInRadians, scale }
// Only works for P2D display mode
float[] getTransform()
{
 // Origin
 float x0 = screenX(0, 0);
 float y0 = screenY(0, 0);

 // Location of (1, 0) on X-axis
 float x1 = screenX(1, 0);
 float y1 = screenY(1, 0);

 // Rotation determined by angle of X-axis
 float rot = atan2(y1 - y0, x1 - x0);

 // Scale determined by distance of a point 1 unit from the origin
 float sc = dist(x0, y0, x1, y1);

 return new float[] { x0, y0, rot, sc };
}

// Pass a float array containing transform information
// { originX, originY, rotationInRadians, scale }
void setTransform(float[] xyrs)
{
 resetMatrix();
 translate(xyrs[0], xyrs[1]);  // Origin
 rotate(xyrs[2]);
 scale(xyrs[3]);
}


Apologies if there are any typos and it doesn't work; I've had to transcribe this from another PC without net access (and this PC doesn't have Processing installed).

I (re-)wrote this as a function returning an array of values in case you decide you wish to remember multiple transformation states (in which case setting simple variables in-line will be a major hassle!).

Example usage:

Code:
size(640, 480, P2D);  // This only works for P2D mode

float[] txSave1 = null, txSave2 = null;

// Arbitrary translate(x, y), rotate(rot) and scale(s) calls...

txSave1 = getTransform();

float rotation = txSave1[2];  // for example

// More transformations...

txSave2 = getTransform();

// blah...

setTransform(txSave1); // Easy!

// Back at saved transform #1 to do stuff...

pushMatrix(); // call this #3

setTransform(txSave2);

// Back at saved transform #2 to do stuff...

popMatrix();

// Back at saved transform #3 to do stuff...


In testing this, I discovered that non-uniform scaling - using scale(scaleX, scaleY), where scaleX and scaleY aren't equal - in combination with rotations, produces weird output. It seems that sheer is introduced. I tried retrieving scaleY by measuring the distance from the transformed origin to transformed (0,1), but it didn't work out, so I've left it as assuming uniform scaling.

If you want to use non-uniform scaling, you're on your own! ;o)

If you only need the current rotation, you could use a simplified version of the getTransform function.

Code:
float getRotation()
{
return atan2(screenY(1,0)-screenY(0,0), screenX(1,0)-screenX(0,0));
}


-spxl
Page Index Toggle Pages: 1