Align meshes based on vertex selection via geometry nodes

here I am stuck with vectors again…

I want to make a tool with geometry nodes that aligns 2 objects based on a (ideally matching) vertex selection. First vertex would be used for snapping the objects, second one for initial rotation/scale matching and third for final alignment.

Trying on a simple prototype I sort of managed to get to phase 2, but I’m stuck with the final rotation -



align

Any ideas on what to do to get the cube in place, matching the first cubes position?
geoNodes_align_objects_v001.blend (1.0 MB)

Hi Dan,
you also have to specify which two faces should align with each other. Building up on your nodetree, this is what i came up with.


The Dot Product → Arcosine will give you the angle between the normals of the two faces that should align with each other. You then just have to rotate everything around the adjacent edge (you already got that with the two Sample Index nodes).
Note: You may have to multiply the resulting angle by -1 for a clockwise or counter clockwise rotation.

2 Likes

yep, that’s the rotation I was looking for, thanks!

is there any way to get the correct angle without adjusting it manually? to basically align the 2 otherwise identical objects.

I think this should work:


The Vector Math - Cross Product will give us the tangent of the sampled edge to the face normal. The Math - Sign node after the Vector Math - Dot Product should then tell us in which direction we have to rotate everything (positive or negative).

3 Likes

thanks for that! for some reason it’s not getting me there, but I’m sure it’s just some node noodle hickup on my side. I’ll try to wrap my head around it, I’m not used to trigonometry in 3D :slight_smile:

here’s where I got stuck. Out of curiosity, where does the subtract pi come from?

edit - just to clarify, I’d want them in an identical position fully overlapping each other.

ok, just randomly changing the indices got me there with the cube. not sure if this would only work with cubes or if there’s be a way to make it work with any identical object.

Need to decipher what’s going on, I wish there was some sort of vector visualizer for geo nodes :smiley:

Oh god… for whatever reason i thought that this would be the desired outcome :rofl: Sorry!

I redid that whole thing keeping your initial setup with the multiple Set Position steps in mind:


geoNodes_align_objects_v002.blend (136.4 KB)

We basically have to map one triangle to another triangle. Let’s take the two triangles 2, 0, 4 and 6, 7, 5 (target mesh) as an example:
image

First step is to overlap one of the vertices of each triangle with each other like you already did in your setup. In my setup i overlaped them on the position of the “Vert 2” vertex.

After that we will align one of the adjacent edges from these vertices with each other. We first calculate the edge direction by subtracting the positions:


Then we calculate the Cross Product from these two vectors (this will be our rotation axis for the following Vector Rotate node):
image
We then normalize the two edge directions and get their Dot Product. By normalizing them we can calculate the angle between these vectors with the Arcosine function:

To align the last edge of our triangle we simply copy the previous step but use the other remainding edge direction instead.

Edit: Forget what i wrote in that last sentence… Only works if there’s a 90° angle in the triangle :man_facepalming: will post an update here!

3 Likes

First of all, thanks so much for the detailed explanation and for all the effort you put into this, I really appreciate it! :slight_smile:

I think I get the math part now (apart from the cross product in 3D. 90 degrees on a 2D vector seems straight forward, but in 3D we have 1 more axis… anyhow, got some more reading up to do. :smiley: ), and the need for a right triangle in order for the arc cosine to work. Also, we align one triangle to an adjacent triangle.

What if I would want to align the triangle to the same triangle? In order for the mesh to be in the exact same position (vert IDs matching)? (actually I think it wouldn’t even work if the verts are on the same triangle (or any triangle?), cause you’d have 2 solutions for aligning that.)

The whole backstory was that I got 2 identical 3D scans with different (baked in) transforms and wanted to align the 2 based on identical vert selections. I used this simplified example as a test (it probably wasn’t the best idea to use cubes).

Since I’m mathematically challenged I was trying to make up for it this year by going through the standard curriculum, I’m currently past Algebra II into pre calculus, with vectors coming up soon, hopefully getting me closer to being comfortable with these things.

Thanks again for all your help!

1 Like

Now i see where my mistake was :man_facepalming:

So, everything up to aligning the first edge is fine. After that, we rotate everything around the first aligned edge (duh :man_facepalming:). We then get the normal of each triangle by calculating the cross product of each edge direction. The angle between the two normals (Dot Product and Arcosine) will be our rotation angle. To know whether we have to rotate it clockwise/counterclockwise we simply check if the last edge is pointing in the normal direction (sign of the Dot Product between the two vectors):
grafik

Complete node tree and .blend file:


AlignTriangles.blend (163.1 KB)

Hope that helps (and works :sweat_smile:)

That’s what this setup should be doing, if i understand you correctly? You just have to make sure, that you define the Vert Ids in the same order. Let’s take these two triangles for example:
grafik
We define the left triangle in the order 1 - 2 - 3. If we now want to match the right triangle to this one we have to define it in the order 7 - 8 - 9. This way, 7 will go to 1, 8 will go to 2 and 9 will go to 3. If you do it in the reversed order, everything will be flipped. If you change the order it will get rotated around the triangles normal.
In my setup i went for a counter clockwise definition - keep that in mind when using it.

If you define the triangles in the same order there should only be one possible solution.

4 Likes

This is brilliant, thank you so much!

This will be super useful, I have to deal with lost transforms all the time. (I’m sure it’ll be useful to many others as well who stumble upon the thread.

Takes a while to wrap my head around it but I think I got it now, since the operations are done in sequence there’s just one possible way for the third rotation.

Thanks again, I really appreciate it.

1 Like