Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5870

Get plane-aligned flatbox of a triangular mesh face in GHPython?

$
0
0

Hi,

I’m purposfully trying to get the plane-aligned flatbox (bounding box of no volume) of a triangular mesh face. This turned out to be far more challenging than expected! :slight_smile:

import Rhino.Geometry as rg

def get_face_edges(face_vertices):
    """Returns edges as lines for a list of face vertices."""
    edges = []
    for i in range(len(face_vertices) - 2):
        edges.append(rg.Line(face_vertices[i], face_vertices[i + 1]))
    edges.append(rg.Line(face_vertices[-1], face_vertices[0]))
    return edges


if __name__ == "__main__": 
    # Mesh and fIndex are GHPython component inputs
    face = Mesh.Faces[fIndex]  # triangular face
    vertices = [rg.Point3d(v.X, v.Y, v.Z) for v in Mesh.Faces.GetFaceVertices(fIndex)[1:]]

    edge = get_face_edges(vertices)[0] 
    normal = Mesh.FaceNormals[fIndex]

    yaxis = edge.UnitTangent
    xaxis = rg.Vector3d.CrossProduct(yaxis, normal)
    plane = rg.Plane(edge.From, xaxis, yaxis)
    
    fmesh = rg.Mesh()
    for i in range(len(vertices)):
        fmesh.Vertices.Add(vertices[i])
    fmesh.Faces.AddFace(0, 1, 2) 
    
    xform = rg.Transform.ChangeBasis(rg.Plane.WorldXY, plane)
    bbox = fmesh.GetBoundingBox(xform)
    plane_to_world = rg.Transform.ChangeBasis(plane, rg.Plane.WorldXY)
    bbox.Transform(plane_to_world) # inflates the previously flatbox to a 3D bounding box ?
   
    # Output
    a = bbox 

In the WorldXY-plane, the correct flat bounding box appears at the world origin, however when I try to transform it back to the plane of the face, it gets inflated to 3D?

Why is the Transform() method of Rhino.Geometry.BoundingBox different from that of other geometry?

Merry christmas everybody! :santa:

example.gh (9.3 KB)

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 5870

Trending Articles