# Points in two triangles to the transform from the first triangle to the second triangle

Hi All

For one of the current projects, I have a geometry that is currently being built physically by students. The geometry is not too accurate in relation to the 3D model that it is being made from.
I therefore need to adjust the vertices in the geometry but doing so would join the subsets of the .x file into one set and I can not color each triangle a separate color.

My plan is to calculate the transform from a generic triangle to each triangle in the geometry. And then apply the spread of transforms from the generic triangle to the others and thus having a spread of triangles I can adjust individually and still color each triangle individually.

Is there a simple way to calculate this, perhaps even a node or plugin.

sunep

triangle2transform.v4p (12.9 kB)

hello sunep,

if its just about to change the color, you can enable the vertex color pin in the VertexBuffer join node and put it there…

otherwise you have to set up a linear equation system and solve it with the GaussJordan (Value) to get your transform matrix.

i’ll give it a try… but can’t promise to come up with a solution today.

I did this once
make your triangle have the coordinates:
(1,0,0)
(0,1,0)
(0,0,1)

then matrix transform: (could be transpose of this, should be obvious when you try though)
A B C 0
E F G 0
I J K 0
0 0 0 1

then you’ll get a triangle at points
(A,B,C)
(E,F,G)
(I,J,K)

but you can recalculate them in a shader if you want

sugoku, are you stoned? ah… no… sorry, i got it after reading it the 4th time. you suggest to store all triangles of the source mesh with the unit vectors and then transform them to their destination position by a matrix formed by the destination vectors… thats a quite pragmatic solution, i like it.

the other way i found when using arbitrary triangles, you would have to calculate a face normal with (b-a)x(c-a) to get 12 equations to solve for the matrix:

m01 m02 m03 m04
m05 m06 m07 m08
m09 m10 m11 m12
0 0 0 1

which will also transform the normals correctly.

wish i was…

yeah sune, who do you ‘Flag as solution’ ? !!

no flags just yet…

I had hoped for a solution requiring slightly less of my rapidly degrading Linear Algebra skills.

so it might take some time for me to digest and since the current project has changed geometry just a bit I no longer need this technique.

Hi,
This is good way to think. I appreciate the task and like to work on it. As soon as possible I would like to share my comments.

everything sounds quite interesting but i dont know what sunep wanted and what the matrix-transform is able to do (what you can not do with regular transform). someone wants to explain a bit more or maybe create a patch?

yes… working on that…

but to explain it a bit, the result would be a usual 4x4 matrix which would transform triangle 1 to triangle 2.
sunep would use it to alter some points of a mesh by hand, and use the matrix to transform the individual subsets of the mesh, instead of using the altered vertics to join a new mesh.

but there is a more general usage of this. say you mark 3 points on mesh and define 3 others somewhere else, then you would get a matrix, which would transform your whole mesh so that the 3 source points match the 3 destination points…

ah oh now i see. thx. yeah that would be a great tool. looking forward to the helppatch;)

is there a solution today, that gives the transform-matrix sune asked for? i didn’t get elliots and tebjans information right…

@sebl, without a test, this sounds like a solution:
catmapper

hm, that one is looking more like a complex homography, but still in 2D.
i think solvePnP (or calibrateProjector) is doing a similar stuff but more complicated, because it’s guessing the matrices from 2D points compared with 3D points.

but maybe i didn’t understand cats extensive contribution…

elliots suggestion is working really fine, so far. tommorrow, the left triangle should become a random one. think about stacking two of those systems into each other…

triangle2affineTransform.v4p (23.8 kB)

hm, i’m a bit confused here… while elliots transform-matrix works well, when applied to a triangle, but fails, if applied to more complex geometry like a box.

any ideas?

@tonfilm:
i think i understand the half of what you explained here:

the other way i found when using arbitrary triangles, you would have to calculate a face normal with (b-a)x(c-a) to get 12 equations to solve for the matrix:

m01 m02 m03 m04
m05 m06 m07 m08
m09 m10 m11 m12
0 0 0 1

i only see one equation not 12…

triangle2affineTransformFail.7z (4.3 kB)

so lets hope i can find the patch from 2010 somewhere on my HDs… i wonder why i didn’t upload it back then… either it’s too messy or it wasn’t working.

ok, i have a solution that works for my purpose really well, after @woei ))pushed me towards ((node:parallelEpiped (transform)

instead of 2 triangles i’m now using 2 3D-coordinates that can be easily noodled into parallelEpiped (transform) and reconstruct a transform matrix.

the attached patch explains it a little - at least, it expains something - in contrast to the existing helppatch. so, where to upload new helppatches?

that also works in principle when you use 2 triangles, but it seems, that this approach gives you one of many possible solutions, so the reconstruction matrix includes weird shears and stretches

ParallelEpiped (Transform Vector) help.v4p (57.6 kB)
AxisAndGrid (DX9).v4p (16.4 kB)

a general linear equation system is of the form A*x=b, where A is NxN square matrix and x and b are vectors of dimension N and * denotes the matrix-vector multiplication. the matrix A and the vector b must be known in order to solve for the vector x.

what we are looking for is the 12 m values of the 4x4 matrix M:

m01 m02 m03 m04
m05 m06 m07 m08
m09 m10 m11 m12
0 0 0 1

so the vector x = (m01, m02, m03, …, m12), and N = 12 here.

now we have to find a matrix A with 12x12 values generated from the source triangle T and a vector b (solution) with 12 values generated from the destination triangle T’. each triangle gives us 12 values, 3 corners ABC + normal n. as we need 12 values for b this sounds good, all of them will probably be in b, but which is where? and how can we get 144 values for A?

this needs some insight into the operation we will perform with the m values later and how the vector-matrix multiplication works:

we know:

M*v = v’

where v is any vector we will transform with the matrix M. let’s have a look how the transformation works for the x value of the result:

m01vx + m02vy + m03vz + m041 = v’x

if we now replace v with a corner of T and v’ with a corner of T’ this is basically equation 1. so the m01…m04 values multiplied with the xyzw values of the source vector are responsible for the x value of the destination vector. this means we can write 4 equations for all the ABCn destination x values that we know:
m01Ax + m02Ay + m03Az + m041 = A’x
m01Bx + m02By + m03Bz + m041 = B’x
m01Cx + m02Cy + m03Cz + m041 = C’x
m01nx + m02ny + m03nz + m040 = n’x
which is the same as:
Ax Ay Az 1 m01 A’x
Bx By Bz 1 * m02 = B’x
Cx Cy Cz 1 m03 C’x
nx ny nz 0 m04 n’x
with these 4 equations we can already solve for m01…m04. And as you can guess, we can do exactly the same for m05…m08 with the y and m09…m12 with z values giving us 12 equations. we can now solve the 3 4x4 systems or write all together into one big 12x12 system which would look like this:

im am not sure which is faster with the solver we have in vvvv. have a look in the patch:

triangle_map.zip (10.4 kB)

wow, thanks for this extensive answer!

i think i have to go through this during the weekend to understand it completely.

and, we should create a wiki-page for that stuff.

its always good to write stuff down, helps me to refresh my linear algebra memories. and its yet another example which shows how awesome mathematics basically is…