Reputation: 1151
I am trying to get the transform matrix from one triangle to another. I am principally concerned about 2D. We will see for 3D later (but open to solutions).
I was reading this answer.
Now please correct me: if my first equilateral triangle, with sides of length 1, has its leftmost point located at the origin, its transform should be the identity matrix.
So, reading the solution above, the transform matrix I am looking for, should be triangle B matrix * inv(Identity) == B matrix.
I cannot wrap my head around that, because it seems wrong. In the given image, I want to transform blue to red, with blue having one point at the origin. The selected point of transform for the red triangle is always the closest to the origin. The last missing image is the last transform merging Blue as Red (same triangle).
I am using Eigen (c++) for my transforms and calculation already.
Thanks
Upvotes: 0
Views: 354
Reputation: 1428
The inverted matrix is generated from the triangle you are translating from, not the destination. I've made a fiddle that demonstrates this here: https://jsfiddle.net/dwch1tju/
Let's say your first triangle has points at [0,0] [0.5,sin(60)] and [1,0] and the second triangle has points at [1.1,0.7] [0.9,1.9] and [1,0]. Then the triangle matrices are:
[[0, 0.5, 1],[0, 0.866, 0], [1, 1, 1]]
[[1.1, 0.9, 2.3], [0.7, 1.9, 0.6], [1, 1, 1]]
Demonstrated by:
const triangleMatrixA = createTriangleMatrix(triangleA);
const triangleMatrixB = createTriangleMatrix(triangleB);
Which creates a transform of
[[0.88, 0.56, -1.36], [0.07, 0.85, -0.67], [0, 0, 1]]
Demonstrated by:
const invertedMatrixB = invertMatrix(triangleMatrixB);
const multiplied = multiplyMatricies(triangleMatrixA, invertedMatrixB)
The fiddle then uses the transform on the three points in the second triangle, to show that they become the points of the first tringle in these lines:
const translatedA = multiplyPointByMatrix(multiplied, triangleB.a);
const translatedB = multiplyPointByMatrix(multiplied, triangleB.b);
const translatedC = multiplyPointByMatrix(multiplied, triangleB.c);
The first generated image in the fiddle shows the original location of the triangles and the second image shows that the translated points place the triangles in the same location.
The answer you link is not inverting a transform. It is inverting a triangle matrix that contains the coordinates of the triangle, then multiplying it by the other triangle matrix to create a transform.
Upvotes: 0