# Transformation of a given point in image through translation and rotation

hi all !!

iâ€™m trying to get the coordinates of a point in an image.
letâ€™s say i know the point i need to know itâ€™s coordinates after a translation and rotation, but i know the coordinates before the transformation and i would like to know where this point has gone after the transformations â€¦

iâ€™ve seen some related posts on the forum but i didnâ€™t figure it out â€¦ in pseudo-code.

``````ofPushMatrix()

ofPoint p;
// that's the point i want to know the coordinates
p.set(px,py);

ofTranslate(t);
ofRotation(r,0,0,1);
ofTranslate(-t);

// here i would like to know where my point p has gone ?
// where has p  ben transformed to ?
``````

Iâ€™ve tried getting the matrix after all the transformations

``````  float myMatrix[ 16 ];
glGetFloatv( GL_MODELVIEW_MATRIX, myMatrix );
mat.set(myMatrix);
ofPopMatrix();
``````

and then trying to mulitply the point by the matrix i got before â€¦

``````ofPushMatrix();
p =   p*mat ;
``````

But the result i got itâ€™s not where it should â€¦

Can anyone understand what iâ€™m trying to express and solve ?
I tried inverting the matrix point multiplication â€¦ but again the result itâ€™s not correct.

?Â¿

Thanks for any clues or advices â€¦

You can check the ofNode class, it has functions showing exactly what you need. It represents a transform, including position (point) rotation and scale.

Also, I think you need to get modelview * projection matrix in order to get that multiplication right. There might be a more direct method in OF that I donâ€™t know of

so i should create a ofNode on the point iâ€™m interested on before transform â€¦
then apply the same transformation iâ€™m doing to the image, but to the ofNode
and then calling getGlobalPosition on the node ?

is that the idea ? iâ€™ve never used ofNode with such functions â€¦
by the way iâ€™m multiplying with the modelview matrix as you suggested in the call :
glGetFloatv( GL_MODELVIEW_MATRIX, myMatrix );
on my pseudo-code â€¦

iâ€™ll give it a try !! thanks a lot for the path !!

do you mean where the point is in 3d or where the point is now on the screen in 2d?

ah sorry, I see now:

the problem I think is that the translations that the camera is doing are baked into the mat at this point. If you look at the GL_MODELVIEW_MATRIX at the start of the draw call, youâ€™ll see this is the case. for example, I have a small window 400x200, mat looks like this at the start:

``````   1,        0,        0,        0
0,        1,        0,        0
0,        0,        1,        0
-200,     -100, -173.205,        1
``````

what you could do is keep around a matrix that you could mirror all tranform commands to, ie:

``````ofMatrix4x4 mat;
mat.makeIdentityMatrix();
ofTranslate(200,0);
mat.translate(200, 0, 0);
cout << ofPoint(100,0) * mat << endl;
``````

I really love ofNode, so @chuckleplantâ€™s advice could also be a good way to go.

hi @zach and all !

thanks for your idea of keeping a matrix with all the transformation commands on it â€¦ just works !
i assume that the reason why getting the GL_MODELVIEW_MATRIX straight on is that

``````the translations that the camera is doing are baked into the mat at this point.
``````

I thought that it was possible to query opengl for the matrix that keeps all the transformations, without any relation to camera â€¦ anyway â€¦ your solution works !

@chuckleplant i didnâ€™t find a way of working it out with the ofNode approach â€¦ ofNode::GetGlobalPosition() seems to donâ€™t do what i expected â€¦ as it seems that getGlobalPosition is not taking in consideration the scale o the node so the coordinates are not as i expected. (iâ€™m doing just a scale and rotation over Z).

by the way iâ€™m not on a 3D space â€¦ just 2D .
many many thanks â€¦ allways learning .

and here is a simple example with what i was trying to get

05_retrieve_matrix_Eloi_.zip (81.0 KB)

here is the same thing (matrix) but also with ofNode and ofVec3f

it is a good example to see transformations with several different objects

05_retrieve_position.zip (53.3 KB)