C# plane oriented to mesh geometry

Hey,

I’m applying box mapping to a list of meshes, which have different orientation in the World XY plane.

tm = Rhino.Render.TextureMapping.CreateBoxMapping(plane, new Interval(-width, width), new Interval(-width, width), new Interval(-width, width), true);

I’m struggling with defining a plane for each of the meshes, which would give me the object’s orientation. Similar to how a Gumball can be aligned to objects:

image

Or one can access the underlying planes for some curves:

var circle = new Circle();
plane = circle.Plane;

Any ideas on how to get a plane oriented to mesh geometry?

See attached (and mofify it accordingly);

Mesh_OrientToCPlane_EntryLevel_V1.gh (31.6 KB)

1 Like

Thanks @PeterFotiadis!

I tried a similar approach in my doodles: take 3 vertices which constitute a face of the mesh and create a plane from these. It works with simple meshes, more complex tessellation can potentially result in the plane being aligned with a diagonal edge:

I needed a logic which would always align the texture to the outside edges of the mesh.

Mesh.GetOutlines()

came to rescue.

Then I just need to create the plane using the first, second and last point of the polyline. Here is what I ended up doing:

Polyline outline = mesh.GetOutlines(Plane.WorldXY);
Point3d P0 = outline[0][0];
Point3d P1 = outline[0][1];
Point3d P2 = outline[0][outline[0].Count - 2];

tm = Rhino.Render.TextureMapping.CreateBoxMapping(new Plane(P0, P1, P2), new Interval(-scale, scale), new Interval(-scale, scale), new Interval(-scale, scale), true);

This:

ensures that the plane is always defined with the current pt (origin), x pt (next1) and y pt (prev1). If the shift value is 0 then the vertex N 0 (per face) is taken. So for a quad Face it’s out of question taking a diagonal end as the y pt.

If on the other hand you are after “aligning” vertices on a per mesh face basis a proper recursive approach can cut the mustard (start from face 0 and check all the neighbors : if vertices are not in the order that you want, reorder vertices create a new face > delete the old and insert the new - or rather better : just reorder the neighbor vertices [required for the from plane] accordingly). Obviously this requires some solid rule (not always possible for any situation).

Thanks!
I was trying to find a generalized solution, which would work for all meshes regardless whether they are comprised of tri or quad faces. That’s the main reason why I decided to take the mesh outline rather than individual faces. This way the texture will be aligned to at least one outside edge of the mesh.

BTW: Some meshes are OK for what you want (see attached that allows you to inspect the order on a per face basis). For the rest you need some lines of code [recursion]: try to do it as an entry level challenge (on Face/Face and Face/Vertex connectivity matters).

Mesh_OrientToCPlane_EntryLevel_V1A.gh (125.2 KB)

Thanks @PeterFotiadis, I appreciate your contribution. For my particular application it makes more sense to use a mesh outline but hopefully other people will be able to benefit from your code in the future.