Affine transform and setNodeToParentTransform

Affine transform and setNodeToParentTransform
0
#1

Hi,

How can I calculate affine transform between three pairs of the corresponding points?

I have 3 pairs of the corresponding points:

cocos2d::Vec2 ps1[3];
cocos2d::Vec2 ps2[3];

I need function

cocos2d::Mat4 getAffineTransform(const cocos2d::Vec2 ps1[3], const cocos2d::Vec2 ps2[3])
{
//compute mat4 for transformation
//ps1[0]->ps2[0]
//ps1[1]->ps2[1]
//ps1[2]->ps2[2]
//TODO:
return cocos2d::Mat4{};
}

and result pass to:

void Node::setNodeToParentTransform(const Mat4& transform)

To transform my cocos2d::Sprite

#2

Check OpenCV’s impl.
https://github.com/opencv/opencv/blob/1913482cf5d257d7da292bb2c15f22d0588d34dc/modules/imgproc/src/imgwarp.cpp#L3090 .

#3

@stevetranby Thank you for answer.
Do you know how to exactly convert cv::Mat matrix 2x3 to cocos2d::Mat4 matrix 4x4?

#4

Here’s the usage code and info:
https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html .

I don’t know for sure, but my guess is the 2x3 matrix it returns would be the upper left sub-matrix of the desired 4x4 transform matrix? It can’t give you rotation unless you had a 3rd input point that you used as the origin for rotation.

// getAffineTransformation's Matrix (2x3)
[[scale_x, shear_x, translate_x]
[shear_y, scale_y, translate_y]]

// if scale negative then it's also reflected 

That’s about as much as I know without research/experimenting myself.